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