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