masqmail

view src/interface.c @ 394:c8e3d1a79313

Refactoring in conf.c: read_rval().
author markus schnalke <meillo@marmaro.de>
date Sat, 18 Feb 2012 19:32:40 +0100
parents 41958685480d
children 164468dd0953
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[0])) {
34 if ((he = gethostbyname(iface->address)) == NULL) {
35 logwrite(LOG_ALERT, "local address '%s' unknown. (deleting)\n", iface->address);
36 return FALSE;
37 }
38 memcpy(&(name->sin_addr), he->h_addr, sizeof(name->sin_addr));
39 } else if (isdigit(iface->address[0])) {
40 if (inet_aton(iface->address, &ia)) {
41 memcpy(&(name->sin_addr), &ia, sizeof(name->sin_addr));
42 } else {
43 logwrite(LOG_ALERT, "invalid address '%s': inet_aton() failed (deleting)\n", iface->address);
44 return FALSE;
45 }
46 } else {
47 logwrite(LOG_ALERT, "invalid address '%s', should begin with a aphanumeric (deleting)\n", iface->address);
48 return FALSE;
49 }
50 #else
51 /* this is how others to it. I follow the crowd... */
52 if (inet_aton(iface->address, &ia) != 0) {
53 /* IP address */
54 memcpy(&(name->sin_addr), &ia, sizeof(name->sin_addr));
55 } else {
56 if ((he = gethostbyname(iface->address)) == NULL) {
57 logwrite(LOG_ALERT, "local address '%s' unknown. (deleting)\n", iface->address);
58 return FALSE;
59 }
60 memcpy(&(name->sin_addr), he->h_addr, sizeof(name->sin_addr));
61 }
62 #endif
63 name->sin_family = AF_INET;
64 name->sin_port = htons(iface->port);
66 return TRUE;
67 }
69 int
70 make_server_socket(interface *iface)
71 {
72 int sock = -1;
73 struct sockaddr_in server;
75 memset(&server, 0, sizeof(struct sockaddr_in));
77 /* Create the socket. */
78 sock = socket(PF_INET, SOCK_STREAM, 0);
79 if (sock < 0) {
80 logwrite(LOG_ALERT, "socket: %s\n", strerror(errno));
81 return -1;
82 }
84 if (init_sockaddr(&server, iface)) {
85 /* bind the socket */
86 if (bind(sock, (struct sockaddr *) &server, sizeof(server)) < 0) {
87 logwrite(LOG_ALERT, "bind: %s\n", strerror(errno));
88 return -1;
89 }
90 } else {
91 close(sock);
92 return -1;
93 }
95 return sock;
96 }