Mercurial > masqmail
comparison src/interface.c @ 0:08114f7dcc23 0.2.21
this is masqmail-0.2.21 from oliver kurth
author | meillo@marmaro.de |
---|---|
date | Fri, 26 Sep 2008 17:05:23 +0200 |
parents | |
children | 26e34ae9a3e3 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:08114f7dcc23 |
---|---|
1 /* MasqMail | |
2 Copyright (C) 2000 Oliver Kurth | |
3 | |
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. | |
8 | |
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. | |
13 | |
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 */ | |
18 | |
19 #include "masqmail.h" | |
20 | |
21 /* define if you get problems... */ | |
22 /*#define SOCKADDR_OLD 1*/ | |
23 | |
24 gboolean init_sockaddr(struct sockaddr_in *name, interface *iface) | |
25 { | |
26 struct hostent *he; | |
27 struct in_addr ia; | |
28 | |
29 #ifdef SOCKADDR_OLD | |
30 /* here I tried to be intelligent and failed. */ | |
31 if(isalpha(iface->address[0])){ | |
32 if ((he = gethostbyname(iface->address)) == NULL) { | |
33 logwrite(LOG_ALERT, | |
34 "local address '%s' unknown. (deleting)\n", | |
35 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, | |
44 "invalid address '%s': inet_aton() failed (deleting)\n", | |
45 iface->address); | |
46 return FALSE; | |
47 } | |
48 }else{ | |
49 logwrite(LOG_ALERT, | |
50 "invalid address '%s', should begin with a aphanumeric (deleting)\n", | |
51 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. (deleting)\n", iface->address); | |
62 return FALSE; | |
63 } | |
64 memcpy(&(name->sin_addr), he->h_addr, sizeof(name->sin_addr)); | |
65 } | |
66 #endif | |
67 name->sin_family = AF_INET; | |
68 name->sin_port = htons(iface->port); | |
69 | |
70 return TRUE; | |
71 } | |
72 | |
73 int make_server_socket(interface *iface) | |
74 { | |
75 int sock = -1; | |
76 struct sockaddr_in server; | |
77 | |
78 memset(&server, 0, sizeof(struct sockaddr_in)); | |
79 | |
80 /* Create the socket. */ | |
81 sock = socket (PF_INET, SOCK_STREAM, 0); | |
82 if (sock < 0){ | |
83 logwrite(LOG_ALERT, "socket: %s\n", strerror(errno)); | |
84 return -1; | |
85 } | |
86 | |
87 if(init_sockaddr(&server, iface)){ | |
88 /* bind the socket */ | |
89 if (bind (sock, (struct sockaddr *) &server, sizeof (server)) < 0){ | |
90 logwrite(LOG_ALERT, "bind: %s\n", strerror(errno)); | |
91 return -1; | |
92 } | |
93 }else{ | |
94 close(sock); | |
95 return -1; | |
96 } | |
97 | |
98 return sock; | |
99 } |