Mercurial > dovecot > core-2.2
annotate src/lib/network.c @ 807:35abd7a5d381 HEAD
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Added str_path() and str_ppath() functions. i_snprintf() now returns only -1
or 0 depending on if buffer got full. dec2str() returns the string allocated
from data stack. Instead of just casting to (long) or (int), we now use
dec2str() with printf-like functions. Added o_stream_send_str(). Added
strocpy() and replaced all strcpy()s and strncpy()s with it.
Pretty much untested, hope it doesn't break too badly :)
author | Timo Sirainen <tss@iki.fi> |
---|---|
date | Thu, 19 Dec 2002 03:02:34 +0200 |
parents | 5ac361acb316 |
children | fd8888f6f037 |
rev | line source |
---|---|
0 | 1 /* |
2 network.c : Network stuff with IPv6 support | |
3 | |
4 Copyright (c) 1999-2002 Timo Sirainen | |
5 | |
6 Permission is hereby granted, free of charge, to any person obtaining | |
7 a copy of this software and associated documentation files (the | |
8 "Software"), to deal in the Software without restriction, including | |
9 without limitation the rights to use, copy, modify, merge, publish, | |
10 distribute, sublicense, and/or sell copies of the Software, and to | |
11 permit persons to whom the Software is furnished to do so, subject to | |
12 the following conditions: | |
13 | |
14 The above copyright notice and this permission notice shall be | |
15 included in all copies or substantial portions of the Software. | |
16 | |
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | |
18 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | |
20 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY | |
21 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, | |
22 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | |
23 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
24 */ | |
25 | |
26 #include "lib.h" | |
27 #include "network.h" | |
28 | |
29 #include <unistd.h> | |
30 #include <fcntl.h> | |
31 #include <ctype.h> | |
32 #include <sys/un.h> | |
33 #include <netinet/tcp.h> | |
34 | |
35 #define LISTEN_BACKLOG 8 | |
36 | |
37 union sockaddr_union { | |
38 struct sockaddr sa; | |
39 struct sockaddr_in sin; | |
40 #ifdef HAVE_IPV6 | |
41 struct sockaddr_in6 sin6; | |
42 #endif | |
43 }; | |
44 | |
45 #ifdef HAVE_IPV6 | |
46 # define SIZEOF_SOCKADDR(so) ((so).sa.sa_family == AF_INET6 ? \ | |
47 sizeof(so.sin6) : sizeof(so.sin)) | |
48 #else | |
49 # define SIZEOF_SOCKADDR(so) (sizeof(so.sin)) | |
50 #endif | |
51 | |
780
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
680
diff
changeset
|
52 int net_ip_compare(const IPADDR *ip1, const IPADDR *ip2) |
0 | 53 { |
54 if (ip1->family != ip2->family) | |
55 return 0; | |
56 | |
57 #ifdef HAVE_IPV6 | |
58 if (ip1->family == AF_INET6) | |
59 return memcmp(&ip1->ip, &ip2->ip, sizeof(ip1->ip)) == 0; | |
60 #endif | |
61 | |
62 return memcmp(&ip1->ip, &ip2->ip, 4) == 0; | |
63 } | |
64 | |
65 | |
66 /* copy IP to sockaddr */ | |
67 static inline void sin_set_ip(union sockaddr_union *so, const IPADDR *ip) | |
68 { | |
69 if (ip == NULL) { | |
70 #ifdef HAVE_IPV6 | |
71 so->sin6.sin6_family = AF_INET6; | |
72 so->sin6.sin6_addr = in6addr_any; | |
73 #else | |
74 so->sin.sin_family = AF_INET; | |
75 so->sin.sin_addr.s_addr = INADDR_ANY; | |
76 #endif | |
77 return; | |
78 } | |
79 | |
80 so->sin.sin_family = ip->family; | |
81 #ifdef HAVE_IPV6 | |
82 if (ip->family == AF_INET6) | |
83 memcpy(&so->sin6.sin6_addr, &ip->ip, sizeof(ip->ip)); | |
84 else | |
85 #endif | |
86 memcpy(&so->sin.sin_addr, &ip->ip, 4); | |
87 } | |
88 | |
89 static inline void sin_get_ip(const union sockaddr_union *so, IPADDR *ip) | |
90 { | |
91 ip->family = so->sin.sin_family; | |
92 | |
93 #ifdef HAVE_IPV6 | |
94 if (ip->family == AF_INET6) | |
95 memcpy(&ip->ip, &so->sin6.sin6_addr, sizeof(ip->ip)); | |
96 else | |
97 #endif | |
98 memcpy(&ip->ip, &so->sin.sin_addr, 4); | |
99 } | |
100 | |
349 | 101 static inline void sin_set_port(union sockaddr_union *so, unsigned int port) |
0 | 102 { |
103 #ifdef HAVE_IPV6 | |
104 if (so->sin.sin_family == AF_INET6) | |
105 so->sin6.sin6_port = htons((unsigned short) port); | |
106 else | |
107 #endif | |
108 so->sin.sin_port = htons((unsigned short) port); | |
109 } | |
110 | |
349 | 111 static inline unsigned int sin_get_port(union sockaddr_union *so) |
0 | 112 { |
113 #ifdef HAVE_IPV6 | |
114 if (so->sin.sin_family == AF_INET6) | |
115 return ntohs(so->sin6.sin6_port); | |
116 #endif | |
117 return ntohs(so->sin.sin_port); | |
118 } | |
119 | |
120 static inline void close_save_errno(int fd) | |
121 { | |
122 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
|
123 (void)close(fd); |
0 | 124 errno = old_errno; |
125 } | |
126 | |
127 /* Connect to socket with ip address */ | |
780
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
680
diff
changeset
|
128 int net_connect_ip(const IPADDR *ip, unsigned int port, const IPADDR *my_ip) |
0 | 129 { |
130 union sockaddr_union so; | |
131 int fd, ret, opt = 1; | |
132 | |
133 if (my_ip != NULL && ip->family != my_ip->family) { | |
134 i_warning("net_connect_ip(): ip->family != my_ip->family"); | |
135 my_ip = NULL; | |
136 } | |
137 | |
138 /* create the socket */ | |
139 memset(&so, 0, sizeof(so)); | |
140 so.sin.sin_family = ip->family; | |
141 fd = socket(ip->family, SOCK_STREAM, 0); | |
142 | |
143 if (fd == -1) | |
144 return -1; | |
145 | |
146 /* set socket options */ | |
147 net_set_nonblock(fd, TRUE); | |
148 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); | |
149 setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &opt, sizeof(opt)); | |
150 | |
151 /* set our own address */ | |
152 if (my_ip != NULL) { | |
153 sin_set_ip(&so, my_ip); | |
154 if (bind(fd, &so.sa, SIZEOF_SOCKADDR(so)) == -1) { | |
155 /* failed, set it back to INADDR_ANY */ | |
156 sin_set_ip(&so, NULL); | |
157 bind(fd, &so.sa, SIZEOF_SOCKADDR(so)); | |
158 } | |
159 } | |
160 | |
161 /* connect */ | |
162 sin_set_ip(&so, ip); | |
163 sin_set_port(&so, port); | |
164 ret = connect(fd, &so.sa, SIZEOF_SOCKADDR(so)); | |
165 | |
166 #ifndef WIN32 | |
167 if (ret < 0 && errno != EINPROGRESS) | |
168 #else | |
169 if (ret < 0 && WSAGetLastError() != WSAEWOULDBLOCK) | |
170 #endif | |
171 { | |
172 close_save_errno(fd); | |
173 return -1; | |
174 } | |
175 | |
176 return fd; | |
177 } | |
178 | |
179 int net_connect_unix(const char *path) | |
180 { | |
181 struct sockaddr_un sa; | |
182 int fd, ret; | |
183 | |
807
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
184 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
|
185 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
|
186 if (strocpy(sa.sun_path, path, sizeof(sa.sun_path)) < 0) { |
0 | 187 /* too long path */ |
188 errno = EINVAL; | |
189 return -1; | |
190 } | |
191 | |
192 /* create the socket */ | |
193 fd = socket(PF_UNIX, SOCK_STREAM, 0); | |
194 if (fd == -1) | |
195 return -1; | |
196 | |
197 /* set socket options */ | |
198 net_set_nonblock(fd, TRUE); | |
199 | |
200 /* connect */ | |
201 ret = connect(fd, (struct sockaddr *) &sa, sizeof(sa)); | |
202 if (ret < 0 && errno != EINPROGRESS) { | |
203 close_save_errno(fd); | |
204 return -1; | |
205 } | |
206 | |
207 return fd; | |
208 } | |
209 | |
210 /* Disconnect socket */ | |
211 void net_disconnect(int fd) | |
212 { | |
388
792fc5b3daa4
Send error message if close() fails in net_disconnect().
Timo Sirainen <tss@iki.fi>
parents:
349
diff
changeset
|
213 if (close(fd) < 0) |
792fc5b3daa4
Send error message if close() fails in net_disconnect().
Timo Sirainen <tss@iki.fi>
parents:
349
diff
changeset
|
214 i_error("net_disconnect() failed: %m"); |
0 | 215 } |
216 | |
217 /* Set socket blocking/nonblocking */ | |
34
a3d77e73f99b
fixed compile warnings with some systems
Timo Sirainen <tss@iki.fi>
parents:
0
diff
changeset
|
218 void net_set_nonblock(int fd __attr_unused__, int nonblock __attr_unused__) |
0 | 219 { |
220 #ifdef HAVE_FCNTL | |
221 if (fcntl(fd, F_SETFL, nonblock ? O_NONBLOCK : 0) < 0) | |
628 | 222 i_fatal("net_set_nonblock() failed: %m"); |
0 | 223 #endif |
224 } | |
225 | |
410
1f0e7229ee58
Split IOBuffer into mmaped IBuffer, file IBuffer, memory data IBuffer and
Timo Sirainen <tss@iki.fi>
parents:
404
diff
changeset
|
226 int net_set_cork(int fd __attr_unused__, int cork __attr_unused__) |
0 | 227 { |
228 #ifdef TCP_CORK | |
410
1f0e7229ee58
Split IOBuffer into mmaped IBuffer, file IBuffer, memory data IBuffer and
Timo Sirainen <tss@iki.fi>
parents:
404
diff
changeset
|
229 return setsockopt(fd, SOL_TCP, TCP_CORK, &cork, sizeof(cork)); |
1f0e7229ee58
Split IOBuffer into mmaped IBuffer, file IBuffer, memory data IBuffer and
Timo Sirainen <tss@iki.fi>
parents:
404
diff
changeset
|
230 #else |
1f0e7229ee58
Split IOBuffer into mmaped IBuffer, file IBuffer, memory data IBuffer and
Timo Sirainen <tss@iki.fi>
parents:
404
diff
changeset
|
231 errno = ENOPROTOOPT; |
1f0e7229ee58
Split IOBuffer into mmaped IBuffer, file IBuffer, memory data IBuffer and
Timo Sirainen <tss@iki.fi>
parents:
404
diff
changeset
|
232 return -1; |
0 | 233 #endif |
234 } | |
235 | |
780
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
680
diff
changeset
|
236 void net_get_ip_any4(IPADDR *ip) |
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
680
diff
changeset
|
237 { |
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
680
diff
changeset
|
238 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
|
239 |
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
680
diff
changeset
|
240 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
|
241 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
|
242 } |
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
680
diff
changeset
|
243 |
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
680
diff
changeset
|
244 void net_get_ip_any6(IPADDR *ip) |
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
680
diff
changeset
|
245 { |
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
680
diff
changeset
|
246 #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
|
247 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
|
248 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
|
249 #else |
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
680
diff
changeset
|
250 memset(ip, 0, sizeof(IPADDR)); |
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
680
diff
changeset
|
251 #endif |
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
680
diff
changeset
|
252 } |
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
680
diff
changeset
|
253 |
0 | 254 /* Listen for connections on a socket. if `my_ip' is NULL, listen in any |
255 address. */ | |
780
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
680
diff
changeset
|
256 int net_listen(const IPADDR *my_ip, unsigned int *port) |
0 | 257 { |
258 union sockaddr_union so; | |
259 int ret, fd, opt = 1; | |
260 socklen_t len; | |
261 | |
262 i_assert(port != NULL); | |
263 | |
264 memset(&so, 0, sizeof(so)); | |
265 sin_set_port(&so, *port); | |
266 sin_set_ip(&so, my_ip); | |
267 | |
268 /* create the socket */ | |
269 fd = socket(so.sin.sin_family, SOCK_STREAM, 0); | |
270 #ifdef HAVE_IPV6 | |
271 if (fd == -1 && (errno == EINVAL || errno == EAFNOSUPPORT)) { | |
272 /* IPv6 is not supported by OS */ | |
273 so.sin.sin_family = AF_INET; | |
274 so.sin.sin_addr.s_addr = INADDR_ANY; | |
275 | |
276 fd = socket(AF_INET, SOCK_STREAM, 0); | |
277 } | |
278 #endif | |
279 if (fd == -1) | |
280 return -1; | |
281 | |
282 /* set socket options */ | |
283 net_set_nonblock(fd, TRUE); | |
284 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); | |
285 setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &opt, sizeof(opt)); | |
286 | |
287 /* specify the address/port we want to listen in */ | |
288 ret = bind(fd, &so.sa, SIZEOF_SOCKADDR(so)); | |
289 if (ret >= 0) { | |
290 /* get the actual port we started listen */ | |
291 len = SIZEOF_SOCKADDR(so); | |
292 ret = getsockname(fd, &so.sa, &len); | |
293 if (ret >= 0) { | |
294 *port = sin_get_port(&so); | |
295 | |
296 /* start listening */ | |
297 if (listen(fd, LISTEN_BACKLOG) >= 0) | |
298 return fd; | |
299 } | |
300 | |
301 } | |
302 | |
303 /* error */ | |
304 close_save_errno(fd); | |
305 return -1; | |
306 } | |
307 | |
308 int net_listen_unix(const char *path) | |
309 { | |
310 struct sockaddr_un sa; | |
311 int fd; | |
312 | |
807
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
313 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
|
314 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
|
315 if (strocpy(sa.sun_path, path, sizeof(sa.sun_path)) < 0) { |
0 | 316 /* too long path */ |
317 errno = EINVAL; | |
318 return -1; | |
319 } | |
320 | |
321 /* create the socket */ | |
322 fd = socket(PF_UNIX, SOCK_STREAM, 0); | |
323 if (fd == -1) | |
324 return -1; | |
325 | |
326 /* set socket options */ | |
327 net_set_nonblock(fd, TRUE); | |
328 | |
329 /* bind */ | |
330 if (bind(fd, (struct sockaddr *) &sa, sizeof(sa)) == 0) { | |
331 /* start listening */ | |
332 if (listen(fd, LISTEN_BACKLOG) == 0) | |
333 return fd; | |
334 } | |
335 | |
336 close_save_errno(fd); | |
337 return -1; | |
338 } | |
339 | |
340 /* Accept a connection on a socket */ | |
349 | 341 int net_accept(int fd, IPADDR *addr, unsigned int *port) |
0 | 342 { |
343 union sockaddr_union so; | |
344 int ret; | |
345 socklen_t addrlen; | |
346 | |
347 i_assert(fd >= 0); | |
348 | |
349 addrlen = sizeof(so); | |
350 ret = accept(fd, &so.sa, &addrlen); | |
351 | |
680
84e398270f7f
net_accept() returns now -2 for fatal failures.
Timo Sirainen <tss@iki.fi>
parents:
628
diff
changeset
|
352 if (ret < 0) { |
84e398270f7f
net_accept() returns now -2 for fatal failures.
Timo Sirainen <tss@iki.fi>
parents:
628
diff
changeset
|
353 if (errno == EBADF || errno == ENOTSOCK || |
84e398270f7f
net_accept() returns now -2 for fatal failures.
Timo Sirainen <tss@iki.fi>
parents:
628
diff
changeset
|
354 errno == EOPNOTSUPP || errno == EFAULT || errno == EINVAL) |
84e398270f7f
net_accept() returns now -2 for fatal failures.
Timo Sirainen <tss@iki.fi>
parents:
628
diff
changeset
|
355 return -2; |
84e398270f7f
net_accept() returns now -2 for fatal failures.
Timo Sirainen <tss@iki.fi>
parents:
628
diff
changeset
|
356 else |
84e398270f7f
net_accept() returns now -2 for fatal failures.
Timo Sirainen <tss@iki.fi>
parents:
628
diff
changeset
|
357 return -1; |
84e398270f7f
net_accept() returns now -2 for fatal failures.
Timo Sirainen <tss@iki.fi>
parents:
628
diff
changeset
|
358 } |
0 | 359 |
360 if (addr != NULL) sin_get_ip(&so, addr); | |
361 if (port != NULL) *port = sin_get_port(&so); | |
362 | |
159
e0193106a95d
net_accept() set listening socket nonblocking, not the new socket. thanks to
Timo Sirainen <tss@iki.fi>
parents:
64
diff
changeset
|
363 net_set_nonblock(ret, TRUE); |
0 | 364 return ret; |
365 } | |
366 | |
367 /* 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
|
368 ssize_t net_receive(int fd, void *buf, size_t len) |
0 | 369 { |
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
|
370 ssize_t ret; |
0 | 371 |
372 i_assert(fd >= 0); | |
373 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
|
374 i_assert(len <= SSIZE_T_MAX); |
0 | 375 |
376 ret = recv(fd, buf, 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
|
377 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
|
378 /* disconnected */ |
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
379 errno = 0; |
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
380 return -2; |
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
381 } |
0 | 382 |
781
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
383 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
|
384 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
|
385 return 0; |
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
386 |
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
387 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
|
388 /* 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
|
389 return -2; |
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
390 } |
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
391 } |
0 | 392 |
393 return ret; | |
394 } | |
395 | |
396 /* 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
|
397 ssize_t net_transmit(int fd, const void *data, size_t len) |
0 | 398 { |
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
|
399 ssize_t ret; |
0 | 400 |
401 i_assert(fd >= 0); | |
402 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
|
403 i_assert(len <= SSIZE_T_MAX); |
0 | 404 |
64
83ae914a583a
added t_strdup_noconst() which can be used instead of (char *) t_strdup().
Timo Sirainen <tss@iki.fi>
parents:
34
diff
changeset
|
405 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
|
406 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
|
407 return 0; |
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
408 |
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
409 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
|
410 return -2; |
0 | 411 |
412 return ret; | |
413 } | |
414 | |
415 /* Get IP addresses for host. ips contains ips_count of IPs, they don't need | |
416 to be free'd. Returns 0 = ok, others = error code for net_gethosterror() */ | |
417 int net_gethostbyname(const char *addr, IPADDR **ips, int *ips_count) | |
418 { | |
805
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
781
diff
changeset
|
419 /* @UNSAFE */ |
0 | 420 #ifdef HAVE_IPV6 |
421 union sockaddr_union *so; | |
422 struct addrinfo hints, *ai, *origai; | |
423 char hbuf[NI_MAXHOST]; | |
424 int host_error; | |
425 #else | |
426 struct hostent *hp; | |
427 #endif | |
428 int count; | |
429 | |
430 i_assert(addr != NULL); | |
431 i_assert(ips != NULL); | |
432 i_assert(ips_count != NULL); | |
433 | |
434 *ips = NULL; | |
435 *ips_count = 0; | |
436 | |
437 #ifdef HAVE_IPV6 | |
438 memset(&hints, 0, sizeof(struct addrinfo)); | |
439 hints.ai_socktype = SOCK_STREAM; | |
440 | |
441 /* save error to host_error for later use */ | |
442 host_error = getaddrinfo(addr, NULL, &hints, &ai); | |
443 if (host_error != 0) | |
444 return host_error; | |
445 | |
446 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, hbuf, | |
447 sizeof(hbuf), NULL, 0, NI_NUMERICHOST) != 0) | |
448 return 1; | |
449 | |
450 | |
451 /* get number of IPs */ | |
452 origai = ai; | |
453 for (count = 0; ai != NULL; ai = ai->ai_next) | |
454 count++; | |
455 | |
456 *ips_count = count; | |
457 *ips = t_malloc(sizeof(IPADDR) * count); | |
458 | |
459 count = 0; | |
460 for (ai = origai; ai != NULL; ai = ai->ai_next, count++) { | |
461 so = (union sockaddr_union *) ai->ai_addr; | |
462 | |
463 sin_get_ip(so, ips[count]); | |
464 } | |
465 freeaddrinfo(origai); | |
466 #else | |
467 hp = gethostbyname(addr); | |
468 if (hp == NULL) | |
469 return h_errno; | |
470 | |
471 /* get number of IPs */ | |
472 count = 0; | |
473 while (hp->h_addr_list[count] != NULL) | |
474 count++; | |
475 | |
476 *ips_count = count; | |
477 *ips = t_malloc(sizeof(IPADDR) * count); | |
478 | |
479 while (count > 0) { | |
480 count--; | |
481 | |
482 (*ips)[count].family = AF_INET; | |
483 memcpy(&(*ips)[count].ip, hp->h_addr_list[count], 4); | |
484 } | |
485 #endif | |
486 | |
487 return 0; | |
488 } | |
489 | |
490 /* Get socket address/port */ | |
349 | 491 int net_getsockname(int fd, IPADDR *addr, unsigned int *port) |
0 | 492 { |
493 union sockaddr_union so; | |
494 socklen_t addrlen; | |
495 | |
496 i_assert(fd >= 0); | |
497 | |
498 addrlen = sizeof(so); | |
499 if (getsockname(fd, (struct sockaddr *) &so, &addrlen) == -1) | |
500 return -1; | |
501 | |
502 if (addr != NULL) sin_get_ip(&so, addr); | |
503 if (port != NULL) *port = sin_get_port(&so); | |
504 | |
505 return 0; | |
506 } | |
507 | |
805
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
781
diff
changeset
|
508 const char *net_ip2host(const IPADDR *ip) |
0 | 509 { |
510 #ifdef HAVE_IPV6 | |
805
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
781
diff
changeset
|
511 char host[MAX_IP_LEN+1]; |
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
781
diff
changeset
|
512 |
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
781
diff
changeset
|
513 host[MAX_IP_LEN] = '\0'; |
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
781
diff
changeset
|
514 if (inet_ntop(ip->family, &ip->ip, host, MAX_IP_LEN) == NULL) |
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
781
diff
changeset
|
515 return NULL; |
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
781
diff
changeset
|
516 |
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
781
diff
changeset
|
517 return t_strdup(host); |
0 | 518 #else |
519 unsigned long ip4; | |
520 | |
805
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
781
diff
changeset
|
521 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
|
522 return NULL; |
0 | 523 |
524 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
|
525 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
|
526 (ip4 & 0xff000000UL) >> 24, |
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
781
diff
changeset
|
527 (ip4 & 0x00ff0000) >> 16, |
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
781
diff
changeset
|
528 (ip4 & 0x0000ff00) >> 8, |
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
781
diff
changeset
|
529 (ip4 & 0x000000ff)); |
0 | 530 #endif |
531 return 0; | |
532 } | |
533 | |
534 int net_host2ip(const char *host, IPADDR *ip) | |
535 { | |
536 if (strchr(host, ':') != NULL) { | |
537 /* IPv6 */ | |
538 ip->family = AF_INET6; | |
539 #ifdef HAVE_IPV6 | |
540 if (inet_pton(AF_INET6, host, &ip->ip) == 0) | |
541 return -1; | |
542 #else | |
543 ip->ip.s_addr = 0; | |
544 #endif | |
545 } else { | |
546 /* IPv4 */ | |
547 ip->family = AF_INET; | |
548 if (inet_aton(host, (struct in_addr *) &ip->ip) == 0) | |
549 return -1; | |
550 } | |
551 | |
552 return 0; | |
553 } | |
554 | |
555 /* Get socket error */ | |
556 int net_geterror(int fd) | |
557 { | |
558 int data; | |
559 socklen_t len = sizeof(data); | |
560 | |
561 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &data, &len) == -1) | |
562 return -1; | |
563 | |
564 return data; | |
565 } | |
566 | |
567 /* get error of net_gethostname() */ | |
568 const char *net_gethosterror(int error) | |
569 { | |
570 #ifdef HAVE_IPV6 | |
571 i_assert(error != 0); | |
572 | |
573 if (error == 1) { | |
574 /* getnameinfo() failed */ | |
575 return strerror(errno); | |
576 } | |
577 | |
578 return gai_strerror(error); | |
579 #else | |
580 switch (error) { | |
581 case HOST_NOT_FOUND: | |
582 return "Host not found"; | |
583 case NO_ADDRESS: | |
584 return "No IP address found for name"; | |
585 case NO_RECOVERY: | |
586 return "A non-recovable name server error occurred"; | |
587 case TRY_AGAIN: | |
588 return "A temporary error on an authoritative name server"; | |
589 } | |
590 | |
591 /* unknown error */ | |
592 return NULL; | |
593 #endif | |
594 } | |
595 | |
596 /* return TRUE if host lookup failed because it didn't exist (ie. not | |
597 some error with name server) */ | |
598 int net_hosterror_notfound(int error) | |
599 { | |
600 #ifdef HAVE_IPV6 | |
601 return error != 1 && (error == EAI_NONAME || error == EAI_NODATA); | |
602 #else | |
603 return error == HOST_NOT_FOUND || error == NO_ADDRESS; | |
604 #endif | |
605 } | |
606 | |
607 /* Get name of TCP service */ | |
349 | 608 char *net_getservbyport(unsigned short port) |
0 | 609 { |
610 struct servent *entry; | |
611 | |
349 | 612 entry = getservbyport(htons(port), "tcp"); |
0 | 613 return entry == NULL ? NULL : entry->s_name; |
614 } | |
615 | |
616 int is_ipv4_address(const char *host) | |
617 { | |
618 while (*host != '\0') { | |
619 if (*host != '.' && !i_isdigit(*host)) | |
620 return 0; | |
621 host++; | |
622 } | |
623 | |
624 return 1; | |
625 } | |
626 | |
627 int is_ipv6_address(const char *host) | |
628 { | |
629 while (*host != '\0') { | |
630 if (*host != ':' && !i_isxdigit(*host)) | |
631 return 0; | |
632 host++; | |
633 } | |
634 | |
635 return 1; | |
636 } |