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