Mercurial > dovecot > core-2.2
annotate src/lib/network.c @ 3863:55df57c028d4 HEAD
Added "bool" type and changed all ints that were used as booleans to bool.
author | Timo Sirainen <tss@iki.fi> |
---|---|
date | Fri, 13 Jan 2006 22:25:57 +0200 |
parents | 771cd6776d4b |
children | 323c09be5f00 |
rev | line source |
---|---|
3621
3ae2df67459c
Added fd_set_nonblock() and changed net_set_nonblock() to use it.
Timo Sirainen <tss@iki.fi>
parents:
3577
diff
changeset
|
1 /* Copyright (c) 1999-2005 Timo Sirainen */ |
0 | 2 |
3 #include "lib.h" | |
3621
3ae2df67459c
Added fd_set_nonblock() and changed net_set_nonblock() to use it.
Timo Sirainen <tss@iki.fi>
parents:
3577
diff
changeset
|
4 #include "fd-set-nonblock.h" |
0 | 5 #include "network.h" |
6 | |
7 #include <unistd.h> | |
8 #include <fcntl.h> | |
9 #include <ctype.h> | |
10 #include <sys/un.h> | |
11 #include <netinet/tcp.h> | |
12 | |
13 union sockaddr_union { | |
14 struct sockaddr sa; | |
15 struct sockaddr_in sin; | |
16 #ifdef HAVE_IPV6 | |
17 struct sockaddr_in6 sin6; | |
18 #endif | |
19 }; | |
20 | |
21 #ifdef HAVE_IPV6 | |
22 # define SIZEOF_SOCKADDR(so) ((so).sa.sa_family == AF_INET6 ? \ | |
23 sizeof(so.sin6) : sizeof(so.sin)) | |
24 #else | |
25 # define SIZEOF_SOCKADDR(so) (sizeof(so.sin)) | |
26 #endif | |
27 | |
3863
55df57c028d4
Added "bool" type and changed all ints that were used as booleans to bool.
Timo Sirainen <tss@iki.fi>
parents:
3681
diff
changeset
|
28 bool net_ip_compare(const struct ip_addr *ip1, const struct ip_addr *ip2) |
0 | 29 { |
30 if (ip1->family != ip2->family) | |
31 return 0; | |
32 | |
33 #ifdef HAVE_IPV6 | |
34 if (ip1->family == AF_INET6) | |
35 return memcmp(&ip1->ip, &ip2->ip, sizeof(ip1->ip)) == 0; | |
36 #endif | |
37 | |
38 return memcmp(&ip1->ip, &ip2->ip, 4) == 0; | |
39 } | |
40 | |
41 | |
42 /* copy IP to sockaddr */ | |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
43 static inline void |
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
44 sin_set_ip(union sockaddr_union *so, const struct ip_addr *ip) |
0 | 45 { |
46 if (ip == NULL) { | |
47 #ifdef HAVE_IPV6 | |
48 so->sin6.sin6_family = AF_INET6; | |
49 so->sin6.sin6_addr = in6addr_any; | |
50 #else | |
51 so->sin.sin_family = AF_INET; | |
52 so->sin.sin_addr.s_addr = INADDR_ANY; | |
53 #endif | |
54 return; | |
55 } | |
56 | |
57 so->sin.sin_family = ip->family; | |
58 #ifdef HAVE_IPV6 | |
59 if (ip->family == AF_INET6) | |
60 memcpy(&so->sin6.sin6_addr, &ip->ip, sizeof(ip->ip)); | |
61 else | |
62 #endif | |
63 memcpy(&so->sin.sin_addr, &ip->ip, 4); | |
64 } | |
65 | |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
66 static inline void |
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
67 sin_get_ip(const union sockaddr_union *so, struct ip_addr *ip) |
0 | 68 { |
69 ip->family = so->sin.sin_family; | |
70 | |
71 #ifdef HAVE_IPV6 | |
72 if (ip->family == AF_INET6) | |
73 memcpy(&ip->ip, &so->sin6.sin6_addr, sizeof(ip->ip)); | |
74 else | |
75 #endif | |
3400
ddfa507bb74f
Don't return random data if net_getpeername() or net_getsockname() is used
Timo Sirainen <tss@iki.fi>
parents:
3373
diff
changeset
|
76 if (ip->family == AF_INET) |
0 | 77 memcpy(&ip->ip, &so->sin.sin_addr, 4); |
3400
ddfa507bb74f
Don't return random data if net_getpeername() or net_getsockname() is used
Timo Sirainen <tss@iki.fi>
parents:
3373
diff
changeset
|
78 else |
ddfa507bb74f
Don't return random data if net_getpeername() or net_getsockname() is used
Timo Sirainen <tss@iki.fi>
parents:
3373
diff
changeset
|
79 memset(&ip->ip, 0, sizeof(ip->ip)); |
0 | 80 } |
81 | |
349 | 82 static inline void sin_set_port(union sockaddr_union *so, unsigned int port) |
0 | 83 { |
84 #ifdef HAVE_IPV6 | |
85 if (so->sin.sin_family == AF_INET6) | |
86 so->sin6.sin6_port = htons((unsigned short) port); | |
87 else | |
88 #endif | |
89 so->sin.sin_port = htons((unsigned short) port); | |
90 } | |
91 | |
349 | 92 static inline unsigned int sin_get_port(union sockaddr_union *so) |
0 | 93 { |
94 #ifdef HAVE_IPV6 | |
95 if (so->sin.sin_family == AF_INET6) | |
96 return ntohs(so->sin6.sin6_port); | |
97 #endif | |
3400
ddfa507bb74f
Don't return random data if net_getpeername() or net_getsockname() is used
Timo Sirainen <tss@iki.fi>
parents:
3373
diff
changeset
|
98 if (so->sin.sin_family == AF_INET) |
ddfa507bb74f
Don't return random data if net_getpeername() or net_getsockname() is used
Timo Sirainen <tss@iki.fi>
parents:
3373
diff
changeset
|
99 return ntohs(so->sin.sin_port); |
ddfa507bb74f
Don't return random data if net_getpeername() or net_getsockname() is used
Timo Sirainen <tss@iki.fi>
parents:
3373
diff
changeset
|
100 |
ddfa507bb74f
Don't return random data if net_getpeername() or net_getsockname() is used
Timo Sirainen <tss@iki.fi>
parents:
3373
diff
changeset
|
101 return 0; |
0 | 102 } |
103 | |
104 static inline void close_save_errno(int fd) | |
105 { | |
106 int old_errno = errno; | |
805
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
781
diff
changeset
|
107 (void)close(fd); |
0 | 108 errno = old_errno; |
109 } | |
110 | |
111 /* Connect to socket with ip address */ | |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
112 int net_connect_ip(const struct ip_addr *ip, unsigned int port, |
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
113 const struct ip_addr *my_ip) |
0 | 114 { |
115 union sockaddr_union so; | |
116 int fd, ret, opt = 1; | |
117 | |
118 if (my_ip != NULL && ip->family != my_ip->family) { | |
119 i_warning("net_connect_ip(): ip->family != my_ip->family"); | |
120 my_ip = NULL; | |
121 } | |
122 | |
123 /* create the socket */ | |
124 memset(&so, 0, sizeof(so)); | |
125 so.sin.sin_family = ip->family; | |
126 fd = socket(ip->family, SOCK_STREAM, 0); | |
127 | |
3523
d1ed3b3548db
Added some more error messages. Also if net_connect_ip() gives my_ip
Timo Sirainen <tss@iki.fi>
parents:
3400
diff
changeset
|
128 if (fd == -1) { |
d1ed3b3548db
Added some more error messages. Also if net_connect_ip() gives my_ip
Timo Sirainen <tss@iki.fi>
parents:
3400
diff
changeset
|
129 i_error("socket() failed: %m"); |
0 | 130 return -1; |
3523
d1ed3b3548db
Added some more error messages. Also if net_connect_ip() gives my_ip
Timo Sirainen <tss@iki.fi>
parents:
3400
diff
changeset
|
131 } |
0 | 132 |
133 /* set socket options */ | |
134 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); | |
135 setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &opt, sizeof(opt)); | |
2639
a307ddd22a3a
net_connect() should be nonblocking (currently unused in Dovecot)
Timo Sirainen <tss@iki.fi>
parents:
2113
diff
changeset
|
136 net_set_nonblock(fd, TRUE); |
0 | 137 |
138 /* set our own address */ | |
139 if (my_ip != NULL) { | |
140 sin_set_ip(&so, my_ip); | |
141 if (bind(fd, &so.sa, SIZEOF_SOCKADDR(so)) == -1) { | |
142 /* failed, set it back to INADDR_ANY */ | |
3523
d1ed3b3548db
Added some more error messages. Also if net_connect_ip() gives my_ip
Timo Sirainen <tss@iki.fi>
parents:
3400
diff
changeset
|
143 i_error("bind(%s) failed: %m", net_ip2addr(my_ip)); |
d1ed3b3548db
Added some more error messages. Also if net_connect_ip() gives my_ip
Timo Sirainen <tss@iki.fi>
parents:
3400
diff
changeset
|
144 close_save_errno(fd); |
d1ed3b3548db
Added some more error messages. Also if net_connect_ip() gives my_ip
Timo Sirainen <tss@iki.fi>
parents:
3400
diff
changeset
|
145 return -1; |
0 | 146 } |
147 } | |
148 | |
149 /* connect */ | |
150 sin_set_ip(&so, ip); | |
151 sin_set_port(&so, port); | |
152 ret = connect(fd, &so.sa, SIZEOF_SOCKADDR(so)); | |
153 | |
154 #ifndef WIN32 | |
155 if (ret < 0 && errno != EINPROGRESS) | |
156 #else | |
157 if (ret < 0 && WSAGetLastError() != WSAEWOULDBLOCK) | |
158 #endif | |
159 { | |
160 close_save_errno(fd); | |
161 return -1; | |
162 } | |
163 | |
164 return fd; | |
165 } | |
166 | |
167 int net_connect_unix(const char *path) | |
168 { | |
169 struct sockaddr_un sa; | |
170 int fd, ret; | |
171 | |
807
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
172 memset(&sa, 0, sizeof(sa)); |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
173 sa.sun_family = AF_UNIX; |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
174 if (strocpy(sa.sun_path, path, sizeof(sa.sun_path)) < 0) { |
0 | 175 /* too long path */ |
176 errno = EINVAL; | |
177 return -1; | |
178 } | |
179 | |
180 /* create the socket */ | |
181 fd = socket(PF_UNIX, SOCK_STREAM, 0); | |
3523
d1ed3b3548db
Added some more error messages. Also if net_connect_ip() gives my_ip
Timo Sirainen <tss@iki.fi>
parents:
3400
diff
changeset
|
182 if (fd == -1) { |
d1ed3b3548db
Added some more error messages. Also if net_connect_ip() gives my_ip
Timo Sirainen <tss@iki.fi>
parents:
3400
diff
changeset
|
183 i_error("socket(%s) failed: %m", path); |
0 | 184 return -1; |
3523
d1ed3b3548db
Added some more error messages. Also if net_connect_ip() gives my_ip
Timo Sirainen <tss@iki.fi>
parents:
3400
diff
changeset
|
185 } |
0 | 186 |
3076
8d15fea729c2
Use nonblocking connecting for unix sockets.
Timo Sirainen <tss@iki.fi>
parents:
3075
diff
changeset
|
187 net_set_nonblock(fd, TRUE); |
8d15fea729c2
Use nonblocking connecting for unix sockets.
Timo Sirainen <tss@iki.fi>
parents:
3075
diff
changeset
|
188 |
0 | 189 /* connect */ |
190 ret = connect(fd, (struct sockaddr *) &sa, sizeof(sa)); | |
191 if (ret < 0 && errno != EINPROGRESS) { | |
192 close_save_errno(fd); | |
193 return -1; | |
194 } | |
195 | |
196 return fd; | |
197 } | |
198 | |
199 /* Disconnect socket */ | |
200 void net_disconnect(int fd) | |
201 { | |
388
792fc5b3daa4
Send error message if close() fails in net_disconnect().
Timo Sirainen <tss@iki.fi>
parents:
349
diff
changeset
|
202 if (close(fd) < 0) |
792fc5b3daa4
Send error message if close() fails in net_disconnect().
Timo Sirainen <tss@iki.fi>
parents:
349
diff
changeset
|
203 i_error("net_disconnect() failed: %m"); |
0 | 204 } |
205 | |
206 /* Set socket blocking/nonblocking */ | |
3863
55df57c028d4
Added "bool" type and changed all ints that were used as booleans to bool.
Timo Sirainen <tss@iki.fi>
parents:
3681
diff
changeset
|
207 void net_set_nonblock(int fd, bool nonblock) |
0 | 208 { |
3621
3ae2df67459c
Added fd_set_nonblock() and changed net_set_nonblock() to use it.
Timo Sirainen <tss@iki.fi>
parents:
3577
diff
changeset
|
209 if (fd_set_nonblock(fd, nonblock) < 0) |
3ae2df67459c
Added fd_set_nonblock() and changed net_set_nonblock() to use it.
Timo Sirainen <tss@iki.fi>
parents:
3577
diff
changeset
|
210 i_fatal("fd_set_nonblock(%d) failed: %m", fd); |
0 | 211 } |
212 | |
3863
55df57c028d4
Added "bool" type and changed all ints that were used as booleans to bool.
Timo Sirainen <tss@iki.fi>
parents:
3681
diff
changeset
|
213 int net_set_cork(int fd __attr_unused__, bool cork __attr_unused__) |
0 | 214 { |
215 #ifdef TCP_CORK | |
3863
55df57c028d4
Added "bool" type and changed all ints that were used as booleans to bool.
Timo Sirainen <tss@iki.fi>
parents:
3681
diff
changeset
|
216 int val = cork; |
55df57c028d4
Added "bool" type and changed all ints that were used as booleans to bool.
Timo Sirainen <tss@iki.fi>
parents:
3681
diff
changeset
|
217 |
55df57c028d4
Added "bool" type and changed all ints that were used as booleans to bool.
Timo Sirainen <tss@iki.fi>
parents:
3681
diff
changeset
|
218 return setsockopt(fd, IPPROTO_TCP, TCP_CORK, &val, sizeof(val)); |
410
1f0e7229ee58
Split IOBuffer into mmaped IBuffer, file IBuffer, memory data IBuffer and
Timo Sirainen <tss@iki.fi>
parents:
404
diff
changeset
|
219 #else |
1f0e7229ee58
Split IOBuffer into mmaped IBuffer, file IBuffer, memory data IBuffer and
Timo Sirainen <tss@iki.fi>
parents:
404
diff
changeset
|
220 errno = ENOPROTOOPT; |
1f0e7229ee58
Split IOBuffer into mmaped IBuffer, file IBuffer, memory data IBuffer and
Timo Sirainen <tss@iki.fi>
parents:
404
diff
changeset
|
221 return -1; |
0 | 222 #endif |
223 } | |
224 | |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
225 void net_get_ip_any4(struct ip_addr *ip) |
780
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
680
diff
changeset
|
226 { |
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
680
diff
changeset
|
227 struct in_addr *in_ip = (struct in_addr *) &ip->ip; |
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
680
diff
changeset
|
228 |
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
680
diff
changeset
|
229 ip->family = AF_INET; |
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
680
diff
changeset
|
230 in_ip->s_addr = INADDR_ANY; |
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
680
diff
changeset
|
231 } |
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
680
diff
changeset
|
232 |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
233 void net_get_ip_any6(struct ip_addr *ip) |
780
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
680
diff
changeset
|
234 { |
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
680
diff
changeset
|
235 #ifdef HAVE_IPV6 |
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
680
diff
changeset
|
236 ip->family = AF_INET6; |
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
680
diff
changeset
|
237 ip->ip = in6addr_any; |
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
680
diff
changeset
|
238 #else |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
239 memset(ip, 0, sizeof(struct ip_addr)); |
780
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
680
diff
changeset
|
240 #endif |
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
680
diff
changeset
|
241 } |
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
680
diff
changeset
|
242 |
0 | 243 /* Listen for connections on a socket. if `my_ip' is NULL, listen in any |
244 address. */ | |
3075
9cb91ed5a110
Added backlog parameter for net_listen*().
Timo Sirainen <tss@iki.fi>
parents:
2639
diff
changeset
|
245 int net_listen(const struct ip_addr *my_ip, unsigned int *port, int backlog) |
0 | 246 { |
247 union sockaddr_union so; | |
248 int ret, fd, opt = 1; | |
249 socklen_t len; | |
250 | |
251 i_assert(port != NULL); | |
252 | |
253 memset(&so, 0, sizeof(so)); | |
254 sin_set_port(&so, *port); | |
255 sin_set_ip(&so, my_ip); | |
256 | |
257 /* create the socket */ | |
258 fd = socket(so.sin.sin_family, SOCK_STREAM, 0); | |
259 #ifdef HAVE_IPV6 | |
260 if (fd == -1 && (errno == EINVAL || errno == EAFNOSUPPORT)) { | |
261 /* IPv6 is not supported by OS */ | |
262 so.sin.sin_family = AF_INET; | |
263 so.sin.sin_addr.s_addr = INADDR_ANY; | |
264 | |
265 fd = socket(AF_INET, SOCK_STREAM, 0); | |
266 } | |
267 #endif | |
3523
d1ed3b3548db
Added some more error messages. Also if net_connect_ip() gives my_ip
Timo Sirainen <tss@iki.fi>
parents:
3400
diff
changeset
|
268 if (fd == -1) { |
d1ed3b3548db
Added some more error messages. Also if net_connect_ip() gives my_ip
Timo Sirainen <tss@iki.fi>
parents:
3400
diff
changeset
|
269 i_error("socket() failed: %m"); |
0 | 270 return -1; |
3523
d1ed3b3548db
Added some more error messages. Also if net_connect_ip() gives my_ip
Timo Sirainen <tss@iki.fi>
parents:
3400
diff
changeset
|
271 } |
0 | 272 |
273 /* set socket options */ | |
274 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); | |
275 setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &opt, sizeof(opt)); | |
276 | |
3373
690dcc51cdc1
if using IPv6, bind both on the IPv4 and IPv6 addresses if IPV6_V6ONLY is
Timo Sirainen <tss@iki.fi>
parents:
3076
diff
changeset
|
277 /* if using IPv6, bind both on the IPv4 and IPv6 addresses */ |
690dcc51cdc1
if using IPv6, bind both on the IPv4 and IPv6 addresses if IPV6_V6ONLY is
Timo Sirainen <tss@iki.fi>
parents:
3076
diff
changeset
|
278 #ifdef IPV6_V6ONLY |
690dcc51cdc1
if using IPv6, bind both on the IPv4 and IPv6 addresses if IPV6_V6ONLY is
Timo Sirainen <tss@iki.fi>
parents:
3076
diff
changeset
|
279 if (so.sin.sin_family == AF_INET6) { |
690dcc51cdc1
if using IPv6, bind both on the IPv4 and IPv6 addresses if IPV6_V6ONLY is
Timo Sirainen <tss@iki.fi>
parents:
3076
diff
changeset
|
280 opt = 0; |
690dcc51cdc1
if using IPv6, bind both on the IPv4 and IPv6 addresses if IPV6_V6ONLY is
Timo Sirainen <tss@iki.fi>
parents:
3076
diff
changeset
|
281 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &opt, sizeof(opt)); |
690dcc51cdc1
if using IPv6, bind both on the IPv4 and IPv6 addresses if IPV6_V6ONLY is
Timo Sirainen <tss@iki.fi>
parents:
3076
diff
changeset
|
282 } |
690dcc51cdc1
if using IPv6, bind both on the IPv4 and IPv6 addresses if IPV6_V6ONLY is
Timo Sirainen <tss@iki.fi>
parents:
3076
diff
changeset
|
283 #endif |
0 | 284 /* specify the address/port we want to listen in */ |
285 ret = bind(fd, &so.sa, SIZEOF_SOCKADDR(so)); | |
3537
5ff3b88db948
net_listen(): Print bind() errors only if it's not EADDRINUSE. Also do the
Timo Sirainen <tss@iki.fi>
parents:
3523
diff
changeset
|
286 if (ret < 0) { |
5ff3b88db948
net_listen(): Print bind() errors only if it's not EADDRINUSE. Also do the
Timo Sirainen <tss@iki.fi>
parents:
3523
diff
changeset
|
287 if (errno != EADDRINUSE) { |
5ff3b88db948
net_listen(): Print bind() errors only if it's not EADDRINUSE. Also do the
Timo Sirainen <tss@iki.fi>
parents:
3523
diff
changeset
|
288 i_error("bind(%s, %u) failed: %m", |
5ff3b88db948
net_listen(): Print bind() errors only if it's not EADDRINUSE. Also do the
Timo Sirainen <tss@iki.fi>
parents:
3523
diff
changeset
|
289 net_ip2addr(my_ip), *port); |
5ff3b88db948
net_listen(): Print bind() errors only if it's not EADDRINUSE. Also do the
Timo Sirainen <tss@iki.fi>
parents:
3523
diff
changeset
|
290 } |
5ff3b88db948
net_listen(): Print bind() errors only if it's not EADDRINUSE. Also do the
Timo Sirainen <tss@iki.fi>
parents:
3523
diff
changeset
|
291 } else { |
0 | 292 /* get the actual port we started listen */ |
293 len = SIZEOF_SOCKADDR(so); | |
294 ret = getsockname(fd, &so.sa, &len); | |
295 if (ret >= 0) { | |
296 *port = sin_get_port(&so); | |
297 | |
298 /* start listening */ | |
3075
9cb91ed5a110
Added backlog parameter for net_listen*().
Timo Sirainen <tss@iki.fi>
parents:
2639
diff
changeset
|
299 if (listen(fd, backlog) >= 0) |
3537
5ff3b88db948
net_listen(): Print bind() errors only if it's not EADDRINUSE. Also do the
Timo Sirainen <tss@iki.fi>
parents:
3523
diff
changeset
|
300 return fd; |
5ff3b88db948
net_listen(): Print bind() errors only if it's not EADDRINUSE. Also do the
Timo Sirainen <tss@iki.fi>
parents:
3523
diff
changeset
|
301 |
5ff3b88db948
net_listen(): Print bind() errors only if it's not EADDRINUSE. Also do the
Timo Sirainen <tss@iki.fi>
parents:
3523
diff
changeset
|
302 if (errno != EADDRINUSE) |
5ff3b88db948
net_listen(): Print bind() errors only if it's not EADDRINUSE. Also do the
Timo Sirainen <tss@iki.fi>
parents:
3523
diff
changeset
|
303 i_error("listen() failed: %m"); |
0 | 304 } |
305 } | |
306 | |
307 /* error */ | |
308 close_save_errno(fd); | |
309 return -1; | |
310 } | |
311 | |
3075
9cb91ed5a110
Added backlog parameter for net_listen*().
Timo Sirainen <tss@iki.fi>
parents:
2639
diff
changeset
|
312 int net_listen_unix(const char *path, int backlog) |
0 | 313 { |
314 struct sockaddr_un sa; | |
315 int fd; | |
316 | |
807
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
317 memset(&sa, 0, sizeof(sa)); |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
318 sa.sun_family = AF_UNIX; |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
319 if (strocpy(sa.sun_path, path, sizeof(sa.sun_path)) < 0) { |
0 | 320 /* too long path */ |
321 errno = EINVAL; | |
322 return -1; | |
323 } | |
324 | |
325 /* create the socket */ | |
326 fd = socket(PF_UNIX, SOCK_STREAM, 0); | |
3523
d1ed3b3548db
Added some more error messages. Also if net_connect_ip() gives my_ip
Timo Sirainen <tss@iki.fi>
parents:
3400
diff
changeset
|
327 if (fd == -1) { |
d1ed3b3548db
Added some more error messages. Also if net_connect_ip() gives my_ip
Timo Sirainen <tss@iki.fi>
parents:
3400
diff
changeset
|
328 i_error("socket() failed: %m"); |
0 | 329 return -1; |
3523
d1ed3b3548db
Added some more error messages. Also if net_connect_ip() gives my_ip
Timo Sirainen <tss@iki.fi>
parents:
3400
diff
changeset
|
330 } |
0 | 331 |
332 /* bind */ | |
3545
0b6539bc22b9
Don't log extra bind() errors for net_listen_unix() either
Timo Sirainen <tss@iki.fi>
parents:
3537
diff
changeset
|
333 if (bind(fd, (struct sockaddr *) &sa, sizeof(sa)) < 0) { |
0b6539bc22b9
Don't log extra bind() errors for net_listen_unix() either
Timo Sirainen <tss@iki.fi>
parents:
3537
diff
changeset
|
334 if (errno != EADDRINUSE) |
0b6539bc22b9
Don't log extra bind() errors for net_listen_unix() either
Timo Sirainen <tss@iki.fi>
parents:
3537
diff
changeset
|
335 i_error("bind(%s) failed: %m", path); |
0b6539bc22b9
Don't log extra bind() errors for net_listen_unix() either
Timo Sirainen <tss@iki.fi>
parents:
3537
diff
changeset
|
336 } else { |
0 | 337 /* start listening */ |
3075
9cb91ed5a110
Added backlog parameter for net_listen*().
Timo Sirainen <tss@iki.fi>
parents:
2639
diff
changeset
|
338 if (listen(fd, backlog) == 0) |
0 | 339 return fd; |
3545
0b6539bc22b9
Don't log extra bind() errors for net_listen_unix() either
Timo Sirainen <tss@iki.fi>
parents:
3537
diff
changeset
|
340 |
0b6539bc22b9
Don't log extra bind() errors for net_listen_unix() either
Timo Sirainen <tss@iki.fi>
parents:
3537
diff
changeset
|
341 if (errno != EADDRINUSE) |
0b6539bc22b9
Don't log extra bind() errors for net_listen_unix() either
Timo Sirainen <tss@iki.fi>
parents:
3537
diff
changeset
|
342 i_error("listen() failed: %m"); |
0 | 343 } |
344 | |
345 close_save_errno(fd); | |
346 return -1; | |
347 } | |
348 | |
349 /* Accept a connection on a socket */ | |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
350 int net_accept(int fd, struct ip_addr *addr, unsigned int *port) |
0 | 351 { |
352 union sockaddr_union so; | |
353 int ret; | |
354 socklen_t addrlen; | |
355 | |
356 i_assert(fd >= 0); | |
357 | |
358 addrlen = sizeof(so); | |
359 ret = accept(fd, &so.sa, &addrlen); | |
360 | |
680
84e398270f7f
net_accept() returns now -2 for fatal failures.
Timo Sirainen <tss@iki.fi>
parents:
628
diff
changeset
|
361 if (ret < 0) { |
84e398270f7f
net_accept() returns now -2 for fatal failures.
Timo Sirainen <tss@iki.fi>
parents:
628
diff
changeset
|
362 if (errno == EBADF || errno == ENOTSOCK || |
84e398270f7f
net_accept() returns now -2 for fatal failures.
Timo Sirainen <tss@iki.fi>
parents:
628
diff
changeset
|
363 errno == EOPNOTSUPP || errno == EFAULT || errno == EINVAL) |
84e398270f7f
net_accept() returns now -2 for fatal failures.
Timo Sirainen <tss@iki.fi>
parents:
628
diff
changeset
|
364 return -2; |
84e398270f7f
net_accept() returns now -2 for fatal failures.
Timo Sirainen <tss@iki.fi>
parents:
628
diff
changeset
|
365 else |
84e398270f7f
net_accept() returns now -2 for fatal failures.
Timo Sirainen <tss@iki.fi>
parents:
628
diff
changeset
|
366 return -1; |
84e398270f7f
net_accept() returns now -2 for fatal failures.
Timo Sirainen <tss@iki.fi>
parents:
628
diff
changeset
|
367 } |
0 | 368 |
369 if (addr != NULL) sin_get_ip(&so, addr); | |
370 if (port != NULL) *port = sin_get_port(&so); | |
371 | |
372 return ret; | |
373 } | |
374 | |
375 /* Read data from socket, return number of bytes read, -1 = error */ | |
183
4a7ab9e94f25
size_t fixes for lib/. Changed OFF_T_FORMAT to PRIuOFF_T which is more
Timo Sirainen <tss@iki.fi>
parents:
159
diff
changeset
|
376 ssize_t net_receive(int fd, void *buf, size_t len) |
0 | 377 { |
183
4a7ab9e94f25
size_t fixes for lib/. Changed OFF_T_FORMAT to PRIuOFF_T which is more
Timo Sirainen <tss@iki.fi>
parents:
159
diff
changeset
|
378 ssize_t ret; |
0 | 379 |
380 i_assert(fd >= 0); | |
381 i_assert(buf != NULL); | |
183
4a7ab9e94f25
size_t fixes for lib/. Changed OFF_T_FORMAT to PRIuOFF_T which is more
Timo Sirainen <tss@iki.fi>
parents:
159
diff
changeset
|
382 i_assert(len <= SSIZE_T_MAX); |
0 | 383 |
3681
771cd6776d4b
Use read() instead of recv() in net_receive(), so it works even with
Timo Sirainen <tss@iki.fi>
parents:
3621
diff
changeset
|
384 ret = read(fd, buf, len); |
781
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
385 if (ret == 0) { |
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
386 /* disconnected */ |
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
387 errno = 0; |
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
388 return -2; |
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
389 } |
0 | 390 |
781
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
391 if (ret < 0) { |
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
392 if (errno == EINTR || errno == EAGAIN) |
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
393 return 0; |
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
394 |
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
395 if (errno == ECONNRESET || errno == ETIMEDOUT) { |
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
396 /* treat as disconnection */ |
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
397 return -2; |
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
398 } |
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
399 } |
0 | 400 |
401 return ret; | |
402 } | |
403 | |
404 /* Transmit data, return number of bytes sent, -1 = error */ | |
183
4a7ab9e94f25
size_t fixes for lib/. Changed OFF_T_FORMAT to PRIuOFF_T which is more
Timo Sirainen <tss@iki.fi>
parents:
159
diff
changeset
|
405 ssize_t net_transmit(int fd, const void *data, size_t len) |
0 | 406 { |
183
4a7ab9e94f25
size_t fixes for lib/. Changed OFF_T_FORMAT to PRIuOFF_T which is more
Timo Sirainen <tss@iki.fi>
parents:
159
diff
changeset
|
407 ssize_t ret; |
0 | 408 |
409 i_assert(fd >= 0); | |
410 i_assert(data != NULL); | |
183
4a7ab9e94f25
size_t fixes for lib/. Changed OFF_T_FORMAT to PRIuOFF_T which is more
Timo Sirainen <tss@iki.fi>
parents:
159
diff
changeset
|
411 i_assert(len <= SSIZE_T_MAX); |
0 | 412 |
64
83ae914a583a
added t_strdup_noconst() which can be used instead of (char *) t_strdup().
Timo Sirainen <tss@iki.fi>
parents:
34
diff
changeset
|
413 ret = send(fd, data, len, 0); |
781
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
414 if (ret == -1 && (errno == EINTR || errno == EAGAIN)) |
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
415 return 0; |
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
416 |
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
417 if (errno == EPIPE) |
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
418 return -2; |
0 | 419 |
420 return ret; | |
421 } | |
422 | |
423 /* Get IP addresses for host. ips contains ips_count of IPs, they don't need | |
424 to be free'd. Returns 0 = ok, others = error code for net_gethosterror() */ | |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
425 int net_gethostbyname(const char *addr, struct ip_addr **ips, int *ips_count) |
0 | 426 { |
805
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
781
diff
changeset
|
427 /* @UNSAFE */ |
0 | 428 #ifdef HAVE_IPV6 |
429 union sockaddr_union *so; | |
430 struct addrinfo hints, *ai, *origai; | |
431 char hbuf[NI_MAXHOST]; | |
432 int host_error; | |
433 #else | |
434 struct hostent *hp; | |
435 #endif | |
436 int count; | |
437 | |
438 i_assert(addr != NULL); | |
439 i_assert(ips != NULL); | |
440 i_assert(ips_count != NULL); | |
441 | |
442 *ips = NULL; | |
443 *ips_count = 0; | |
444 | |
445 #ifdef HAVE_IPV6 | |
446 memset(&hints, 0, sizeof(struct addrinfo)); | |
447 hints.ai_socktype = SOCK_STREAM; | |
448 | |
449 /* save error to host_error for later use */ | |
450 host_error = getaddrinfo(addr, NULL, &hints, &ai); | |
451 if (host_error != 0) | |
452 return host_error; | |
453 | |
454 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, hbuf, | |
455 sizeof(hbuf), NULL, 0, NI_NUMERICHOST) != 0) | |
456 return 1; | |
457 | |
458 | |
459 /* get number of IPs */ | |
460 origai = ai; | |
461 for (count = 0; ai != NULL; ai = ai->ai_next) | |
462 count++; | |
463 | |
464 *ips_count = count; | |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
465 *ips = t_malloc(sizeof(struct ip_addr) * count); |
0 | 466 |
467 count = 0; | |
468 for (ai = origai; ai != NULL; ai = ai->ai_next, count++) { | |
469 so = (union sockaddr_union *) ai->ai_addr; | |
470 | |
2113
c5817f302aa6
net_gethostbyname() was broken with IPv6 enabled (wasn't really used with
Timo Sirainen <tss@iki.fi>
parents:
2098
diff
changeset
|
471 sin_get_ip(so, &(*ips)[count]); |
0 | 472 } |
473 freeaddrinfo(origai); | |
474 #else | |
475 hp = gethostbyname(addr); | |
476 if (hp == NULL) | |
477 return h_errno; | |
478 | |
479 /* get number of IPs */ | |
480 count = 0; | |
481 while (hp->h_addr_list[count] != NULL) | |
482 count++; | |
483 | |
484 *ips_count = count; | |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
485 *ips = t_malloc(sizeof(struct ip_addr) * count); |
0 | 486 |
487 while (count > 0) { | |
488 count--; | |
489 | |
490 (*ips)[count].family = AF_INET; | |
491 memcpy(&(*ips)[count].ip, hp->h_addr_list[count], 4); | |
492 } | |
493 #endif | |
494 | |
495 return 0; | |
496 } | |
497 | |
498 /* Get socket address/port */ | |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
499 int net_getsockname(int fd, struct ip_addr *addr, unsigned int *port) |
0 | 500 { |
501 union sockaddr_union so; | |
502 socklen_t addrlen; | |
503 | |
504 i_assert(fd >= 0); | |
505 | |
506 addrlen = sizeof(so); | |
507 if (getsockname(fd, (struct sockaddr *) &so, &addrlen) == -1) | |
508 return -1; | |
509 | |
510 if (addr != NULL) sin_get_ip(&so, addr); | |
511 if (port != NULL) *port = sin_get_port(&so); | |
512 | |
513 return 0; | |
514 } | |
515 | |
2098 | 516 int net_getpeername(int fd, struct ip_addr *addr, unsigned int *port) |
517 { | |
518 union sockaddr_union so; | |
519 socklen_t addrlen; | |
520 | |
521 i_assert(fd >= 0); | |
522 | |
523 addrlen = sizeof(so); | |
524 if (getpeername(fd, (struct sockaddr *) &so, &addrlen) == -1) | |
525 return -1; | |
526 | |
527 if (addr != NULL) sin_get_ip(&so, addr); | |
528 if (port != NULL) *port = sin_get_port(&so); | |
529 | |
530 return 0; | |
531 } | |
532 | |
1485
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
1268
diff
changeset
|
533 const char *net_ip2addr(const struct ip_addr *ip) |
0 | 534 { |
535 #ifdef HAVE_IPV6 | |
1485
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
1268
diff
changeset
|
536 char addr[MAX_IP_LEN+1]; |
805
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
781
diff
changeset
|
537 |
1485
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
1268
diff
changeset
|
538 addr[MAX_IP_LEN] = '\0'; |
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
1268
diff
changeset
|
539 if (inet_ntop(ip->family, &ip->ip, addr, MAX_IP_LEN) == NULL) |
805
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
781
diff
changeset
|
540 return NULL; |
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
781
diff
changeset
|
541 |
1485
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
1268
diff
changeset
|
542 return t_strdup(addr); |
0 | 543 #else |
544 unsigned long ip4; | |
545 | |
805
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
781
diff
changeset
|
546 if (ip->family != AF_INET) |
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
781
diff
changeset
|
547 return NULL; |
0 | 548 |
549 ip4 = ntohl(ip->ip.s_addr); | |
805
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
781
diff
changeset
|
550 return t_strdup_printf("%lu.%lu.%lu.%lu", |
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
781
diff
changeset
|
551 (ip4 & 0xff000000UL) >> 24, |
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
781
diff
changeset
|
552 (ip4 & 0x00ff0000) >> 16, |
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
781
diff
changeset
|
553 (ip4 & 0x0000ff00) >> 8, |
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
781
diff
changeset
|
554 (ip4 & 0x000000ff)); |
0 | 555 #endif |
556 } | |
557 | |
1485
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
1268
diff
changeset
|
558 int net_addr2ip(const char *addr, struct ip_addr *ip) |
0 | 559 { |
1485
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
1268
diff
changeset
|
560 if (strchr(addr, ':') != NULL) { |
0 | 561 /* IPv6 */ |
562 ip->family = AF_INET6; | |
563 #ifdef HAVE_IPV6 | |
1485
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
1268
diff
changeset
|
564 if (inet_pton(AF_INET6, addr, &ip->ip) == 0) |
0 | 565 return -1; |
566 #else | |
567 ip->ip.s_addr = 0; | |
568 #endif | |
569 } else { | |
570 /* IPv4 */ | |
571 ip->family = AF_INET; | |
1485
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
1268
diff
changeset
|
572 if (inet_aton(addr, (struct in_addr *) &ip->ip) == 0) |
0 | 573 return -1; |
574 } | |
575 | |
576 return 0; | |
577 } | |
578 | |
579 /* Get socket error */ | |
580 int net_geterror(int fd) | |
581 { | |
582 int data; | |
583 socklen_t len = sizeof(data); | |
584 | |
585 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &data, &len) == -1) | |
586 return -1; | |
587 | |
588 return data; | |
589 } | |
590 | |
591 /* get error of net_gethostname() */ | |
592 const char *net_gethosterror(int error) | |
593 { | |
594 #ifdef HAVE_IPV6 | |
595 i_assert(error != 0); | |
596 | |
597 if (error == 1) { | |
598 /* getnameinfo() failed */ | |
599 return strerror(errno); | |
600 } | |
601 | |
602 return gai_strerror(error); | |
603 #else | |
604 switch (error) { | |
605 case HOST_NOT_FOUND: | |
606 return "Host not found"; | |
607 case NO_ADDRESS: | |
608 return "No IP address found for name"; | |
609 case NO_RECOVERY: | |
610 return "A non-recovable name server error occurred"; | |
611 case TRY_AGAIN: | |
612 return "A temporary error on an authoritative name server"; | |
613 } | |
614 | |
615 /* unknown error */ | |
616 return NULL; | |
617 #endif | |
618 } | |
619 | |
620 /* return TRUE if host lookup failed because it didn't exist (ie. not | |
621 some error with name server) */ | |
622 int net_hosterror_notfound(int error) | |
623 { | |
624 #ifdef HAVE_IPV6 | |
1915
79790750c349
importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
1871
diff
changeset
|
625 #ifdef EAI_NODATA /* NODATA is depricated */ |
0 | 626 return error != 1 && (error == EAI_NONAME || error == EAI_NODATA); |
627 #else | |
1915
79790750c349
importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
1871
diff
changeset
|
628 return error != 1 && (error == EAI_NONAME); |
79790750c349
importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
1871
diff
changeset
|
629 #endif |
79790750c349
importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
1871
diff
changeset
|
630 #else |
0 | 631 return error == HOST_NOT_FOUND || error == NO_ADDRESS; |
632 #endif | |
633 } | |
634 | |
635 /* Get name of TCP service */ | |
2113
c5817f302aa6
net_gethostbyname() was broken with IPv6 enabled (wasn't really used with
Timo Sirainen <tss@iki.fi>
parents:
2098
diff
changeset
|
636 const char *net_getservbyport(unsigned short port) |
0 | 637 { |
638 struct servent *entry; | |
639 | |
349 | 640 entry = getservbyport(htons(port), "tcp"); |
0 | 641 return entry == NULL ? NULL : entry->s_name; |
642 } | |
643 | |
3863
55df57c028d4
Added "bool" type and changed all ints that were used as booleans to bool.
Timo Sirainen <tss@iki.fi>
parents:
3681
diff
changeset
|
644 bool is_ipv4_address(const char *addr) |
0 | 645 { |
1485
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
1268
diff
changeset
|
646 while (*addr != '\0') { |
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
1268
diff
changeset
|
647 if (*addr != '.' && !i_isdigit(*addr)) |
3863
55df57c028d4
Added "bool" type and changed all ints that were used as booleans to bool.
Timo Sirainen <tss@iki.fi>
parents:
3681
diff
changeset
|
648 return FALSE; |
1485
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
1268
diff
changeset
|
649 addr++; |
0 | 650 } |
651 | |
3863
55df57c028d4
Added "bool" type and changed all ints that were used as booleans to bool.
Timo Sirainen <tss@iki.fi>
parents:
3681
diff
changeset
|
652 return TRUE; |
0 | 653 } |
654 | |
3863
55df57c028d4
Added "bool" type and changed all ints that were used as booleans to bool.
Timo Sirainen <tss@iki.fi>
parents:
3681
diff
changeset
|
655 bool is_ipv6_address(const char *addr) |
0 | 656 { |
1485
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
1268
diff
changeset
|
657 while (*addr != '\0') { |
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
1268
diff
changeset
|
658 if (*addr != ':' && !i_isxdigit(*addr)) |
3863
55df57c028d4
Added "bool" type and changed all ints that were used as booleans to bool.
Timo Sirainen <tss@iki.fi>
parents:
3681
diff
changeset
|
659 return FALSE; |
1485
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
1268
diff
changeset
|
660 addr++; |
0 | 661 } |
662 | |
3863
55df57c028d4
Added "bool" type and changed all ints that were used as booleans to bool.
Timo Sirainen <tss@iki.fi>
parents:
3681
diff
changeset
|
663 return TRUE; |
0 | 664 } |