rev |
line source |
meillo@0
|
1 /* MasqMail
|
meillo@0
|
2 Copyright (C) 2000 Oliver Kurth
|
meillo@0
|
3
|
meillo@0
|
4 This program is free software; you can redistribute it and/or modify
|
meillo@0
|
5 it under the terms of the GNU General Public License as published by
|
meillo@0
|
6 the Free Software Foundation; either version 2 of the License, or
|
meillo@0
|
7 (at your option) any later version.
|
meillo@0
|
8
|
meillo@0
|
9 This program is distributed in the hope that it will be useful,
|
meillo@0
|
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
meillo@0
|
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
meillo@0
|
12 GNU General Public License for more details.
|
meillo@0
|
13
|
meillo@0
|
14 You should have received a copy of the GNU General Public License
|
meillo@0
|
15 along with this program; if not, write to the Free Software
|
meillo@0
|
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
meillo@0
|
17 */
|
meillo@0
|
18
|
meillo@0
|
19 #include "masqmail.h"
|
meillo@0
|
20
|
meillo@0
|
21 /* define if you get problems... */
|
meillo@0
|
22 /*#define SOCKADDR_OLD 1*/
|
meillo@0
|
23
|
meillo@10
|
24 gboolean
|
meillo@10
|
25 init_sockaddr(struct sockaddr_in * name, interface * iface)
|
meillo@0
|
26 {
|
meillo@10
|
27 struct hostent *he;
|
meillo@10
|
28 struct in_addr ia;
|
meillo@10
|
29
|
meillo@0
|
30 #ifdef SOCKADDR_OLD
|
meillo@10
|
31 /* here I tried to be intelligent and failed. */
|
meillo@10
|
32 if (isalpha(iface->address[0])) {
|
meillo@10
|
33 if ((he = gethostbyname(iface->address)) == NULL) {
|
meillo@10
|
34 logwrite(LOG_ALERT, "local address '%s' unknown. (deleting)\n", iface->address);
|
meillo@10
|
35 return FALSE;
|
meillo@10
|
36 }
|
meillo@10
|
37 memcpy(&(name->sin_addr), he->h_addr, sizeof(name->sin_addr));
|
meillo@10
|
38 } else if (isdigit(iface->address[0])) {
|
meillo@10
|
39 if (inet_aton(iface->address, &ia)) {
|
meillo@10
|
40 memcpy(&(name->sin_addr), &ia, sizeof(name->sin_addr));
|
meillo@10
|
41 } else {
|
meillo@10
|
42 logwrite(LOG_ALERT, "invalid address '%s': inet_aton() failed (deleting)\n", iface->address);
|
meillo@10
|
43 return FALSE;
|
meillo@10
|
44 }
|
meillo@10
|
45 } else {
|
meillo@10
|
46 logwrite(LOG_ALERT, "invalid address '%s', should begin with a aphanumeric (deleting)\n", iface->address);
|
meillo@10
|
47 return FALSE;
|
meillo@10
|
48 }
|
meillo@0
|
49 #else
|
meillo@10
|
50 /* this is how others to it. I follow the crowd... */
|
meillo@10
|
51 if (inet_aton(iface->address, &ia) != 0) {
|
meillo@10
|
52 /* IP address */
|
meillo@10
|
53 memcpy(&(name->sin_addr), &ia, sizeof(name->sin_addr));
|
meillo@10
|
54 } else {
|
meillo@10
|
55 if ((he = gethostbyname(iface->address)) == NULL) {
|
meillo@10
|
56 logwrite(LOG_ALERT, "local address '%s' unknown. (deleting)\n", iface->address);
|
meillo@10
|
57 return FALSE;
|
meillo@10
|
58 }
|
meillo@10
|
59 memcpy(&(name->sin_addr), he->h_addr, sizeof(name->sin_addr));
|
meillo@10
|
60 }
|
meillo@0
|
61 #endif
|
meillo@10
|
62 name->sin_family = AF_INET;
|
meillo@10
|
63 name->sin_port = htons(iface->port);
|
meillo@0
|
64
|
meillo@10
|
65 return TRUE;
|
meillo@0
|
66 }
|
meillo@0
|
67
|
meillo@10
|
68 int
|
meillo@10
|
69 make_server_socket(interface * iface)
|
meillo@0
|
70 {
|
meillo@10
|
71 int sock = -1;
|
meillo@10
|
72 struct sockaddr_in server;
|
meillo@0
|
73
|
meillo@10
|
74 memset(&server, 0, sizeof(struct sockaddr_in));
|
meillo@10
|
75
|
meillo@10
|
76 /* Create the socket. */
|
meillo@10
|
77 sock = socket(PF_INET, SOCK_STREAM, 0);
|
meillo@10
|
78 if (sock < 0) {
|
meillo@10
|
79 logwrite(LOG_ALERT, "socket: %s\n", strerror(errno));
|
meillo@10
|
80 return -1;
|
meillo@10
|
81 }
|
meillo@10
|
82
|
meillo@10
|
83 if (init_sockaddr(&server, iface)) {
|
meillo@10
|
84 /* bind the socket */
|
meillo@10
|
85 if (bind(sock, (struct sockaddr *) &server, sizeof(server)) < 0) {
|
meillo@10
|
86 logwrite(LOG_ALERT, "bind: %s\n", strerror(errno));
|
meillo@10
|
87 return -1;
|
meillo@10
|
88 }
|
meillo@10
|
89 } else {
|
meillo@10
|
90 close(sock);
|
meillo@10
|
91 return -1;
|
meillo@10
|
92 }
|
meillo@10
|
93
|
meillo@10
|
94 return sock;
|
meillo@0
|
95 }
|