Mercurial > dovecot > core-2.2
annotate src/lib/network.c @ 903:fd8888f6f037 HEAD
Naming style changes, finally got tired of most of the typedefs. Also the
previous enum -> macro change reverted so that we don't use the highest bit
anymore, that's incompatible with old indexes so they will be rebuilt.
author | Timo Sirainen <tss@iki.fi> |
---|---|
date | Sun, 05 Jan 2003 15:09:51 +0200 |
parents | 35abd7a5d381 |
children | 0d9f0e617a1a |
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 | |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
52 int net_ip_compare(const struct ip_addr *ip1, const struct ip_addr *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 */ | |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
67 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
|
68 sin_set_ip(union sockaddr_union *so, const struct ip_addr *ip) |
0 | 69 { |
70 if (ip == NULL) { | |
71 #ifdef HAVE_IPV6 | |
72 so->sin6.sin6_family = AF_INET6; | |
73 so->sin6.sin6_addr = in6addr_any; | |
74 #else | |
75 so->sin.sin_family = AF_INET; | |
76 so->sin.sin_addr.s_addr = INADDR_ANY; | |
77 #endif | |
78 return; | |
79 } | |
80 | |
81 so->sin.sin_family = ip->family; | |
82 #ifdef HAVE_IPV6 | |
83 if (ip->family == AF_INET6) | |
84 memcpy(&so->sin6.sin6_addr, &ip->ip, sizeof(ip->ip)); | |
85 else | |
86 #endif | |
87 memcpy(&so->sin.sin_addr, &ip->ip, 4); | |
88 } | |
89 | |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
90 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
|
91 sin_get_ip(const union sockaddr_union *so, struct ip_addr *ip) |
0 | 92 { |
93 ip->family = so->sin.sin_family; | |
94 | |
95 #ifdef HAVE_IPV6 | |
96 if (ip->family == AF_INET6) | |
97 memcpy(&ip->ip, &so->sin6.sin6_addr, sizeof(ip->ip)); | |
98 else | |
99 #endif | |
100 memcpy(&ip->ip, &so->sin.sin_addr, 4); | |
101 } | |
102 | |
349 | 103 static inline void sin_set_port(union sockaddr_union *so, unsigned int port) |
0 | 104 { |
105 #ifdef HAVE_IPV6 | |
106 if (so->sin.sin_family == AF_INET6) | |
107 so->sin6.sin6_port = htons((unsigned short) port); | |
108 else | |
109 #endif | |
110 so->sin.sin_port = htons((unsigned short) port); | |
111 } | |
112 | |
349 | 113 static inline unsigned int sin_get_port(union sockaddr_union *so) |
0 | 114 { |
115 #ifdef HAVE_IPV6 | |
116 if (so->sin.sin_family == AF_INET6) | |
117 return ntohs(so->sin6.sin6_port); | |
118 #endif | |
119 return ntohs(so->sin.sin_port); | |
120 } | |
121 | |
122 static inline void close_save_errno(int fd) | |
123 { | |
124 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
|
125 (void)close(fd); |
0 | 126 errno = old_errno; |
127 } | |
128 | |
129 /* 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
|
130 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
|
131 const struct ip_addr *my_ip) |
0 | 132 { |
133 union sockaddr_union so; | |
134 int fd, ret, opt = 1; | |
135 | |
136 if (my_ip != NULL && ip->family != my_ip->family) { | |
137 i_warning("net_connect_ip(): ip->family != my_ip->family"); | |
138 my_ip = NULL; | |
139 } | |
140 | |
141 /* create the socket */ | |
142 memset(&so, 0, sizeof(so)); | |
143 so.sin.sin_family = ip->family; | |
144 fd = socket(ip->family, SOCK_STREAM, 0); | |
145 | |
146 if (fd == -1) | |
147 return -1; | |
148 | |
149 /* set socket options */ | |
150 net_set_nonblock(fd, TRUE); | |
151 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); | |
152 setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &opt, sizeof(opt)); | |
153 | |
154 /* set our own address */ | |
155 if (my_ip != NULL) { | |
156 sin_set_ip(&so, my_ip); | |
157 if (bind(fd, &so.sa, SIZEOF_SOCKADDR(so)) == -1) { | |
158 /* failed, set it back to INADDR_ANY */ | |
159 sin_set_ip(&so, NULL); | |
160 bind(fd, &so.sa, SIZEOF_SOCKADDR(so)); | |
161 } | |
162 } | |
163 | |
164 /* connect */ | |
165 sin_set_ip(&so, ip); | |
166 sin_set_port(&so, port); | |
167 ret = connect(fd, &so.sa, SIZEOF_SOCKADDR(so)); | |
168 | |
169 #ifndef WIN32 | |
170 if (ret < 0 && errno != EINPROGRESS) | |
171 #else | |
172 if (ret < 0 && WSAGetLastError() != WSAEWOULDBLOCK) | |
173 #endif | |
174 { | |
175 close_save_errno(fd); | |
176 return -1; | |
177 } | |
178 | |
179 return fd; | |
180 } | |
181 | |
182 int net_connect_unix(const char *path) | |
183 { | |
184 struct sockaddr_un sa; | |
185 int fd, ret; | |
186 | |
807
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
187 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
|
188 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
|
189 if (strocpy(sa.sun_path, path, sizeof(sa.sun_path)) < 0) { |
0 | 190 /* too long path */ |
191 errno = EINVAL; | |
192 return -1; | |
193 } | |
194 | |
195 /* create the socket */ | |
196 fd = socket(PF_UNIX, SOCK_STREAM, 0); | |
197 if (fd == -1) | |
198 return -1; | |
199 | |
200 /* set socket options */ | |
201 net_set_nonblock(fd, TRUE); | |
202 | |
203 /* connect */ | |
204 ret = connect(fd, (struct sockaddr *) &sa, sizeof(sa)); | |
205 if (ret < 0 && errno != EINPROGRESS) { | |
206 close_save_errno(fd); | |
207 return -1; | |
208 } | |
209 | |
210 return fd; | |
211 } | |
212 | |
213 /* Disconnect socket */ | |
214 void net_disconnect(int fd) | |
215 { | |
388
792fc5b3daa4
Send error message if close() fails in net_disconnect().
Timo Sirainen <tss@iki.fi>
parents:
349
diff
changeset
|
216 if (close(fd) < 0) |
792fc5b3daa4
Send error message if close() fails in net_disconnect().
Timo Sirainen <tss@iki.fi>
parents:
349
diff
changeset
|
217 i_error("net_disconnect() failed: %m"); |
0 | 218 } |
219 | |
220 /* Set socket blocking/nonblocking */ | |
34
a3d77e73f99b
fixed compile warnings with some systems
Timo Sirainen <tss@iki.fi>
parents:
0
diff
changeset
|
221 void net_set_nonblock(int fd __attr_unused__, int nonblock __attr_unused__) |
0 | 222 { |
223 #ifdef HAVE_FCNTL | |
224 if (fcntl(fd, F_SETFL, nonblock ? O_NONBLOCK : 0) < 0) | |
628 | 225 i_fatal("net_set_nonblock() failed: %m"); |
0 | 226 #endif |
227 } | |
228 | |
410
1f0e7229ee58
Split IOBuffer into mmaped IBuffer, file IBuffer, memory data IBuffer and
Timo Sirainen <tss@iki.fi>
parents:
404
diff
changeset
|
229 int net_set_cork(int fd __attr_unused__, int cork __attr_unused__) |
0 | 230 { |
231 #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
|
232 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
|
233 #else |
1f0e7229ee58
Split IOBuffer into mmaped IBuffer, file IBuffer, memory data IBuffer and
Timo Sirainen <tss@iki.fi>
parents:
404
diff
changeset
|
234 errno = ENOPROTOOPT; |
1f0e7229ee58
Split IOBuffer into mmaped IBuffer, file IBuffer, memory data IBuffer and
Timo Sirainen <tss@iki.fi>
parents:
404
diff
changeset
|
235 return -1; |
0 | 236 #endif |
237 } | |
238 | |
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 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
|
240 { |
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
680
diff
changeset
|
241 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
|
242 |
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
680
diff
changeset
|
243 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
|
244 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
|
245 } |
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
680
diff
changeset
|
246 |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
247 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
|
248 { |
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
680
diff
changeset
|
249 #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
|
250 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
|
251 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
|
252 #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
|
253 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
|
254 #endif |
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
680
diff
changeset
|
255 } |
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
680
diff
changeset
|
256 |
0 | 257 /* Listen for connections on a socket. if `my_ip' is NULL, listen in any |
258 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
|
259 int net_listen(const struct ip_addr *my_ip, unsigned int *port) |
0 | 260 { |
261 union sockaddr_union so; | |
262 int ret, fd, opt = 1; | |
263 socklen_t len; | |
264 | |
265 i_assert(port != NULL); | |
266 | |
267 memset(&so, 0, sizeof(so)); | |
268 sin_set_port(&so, *port); | |
269 sin_set_ip(&so, my_ip); | |
270 | |
271 /* create the socket */ | |
272 fd = socket(so.sin.sin_family, SOCK_STREAM, 0); | |
273 #ifdef HAVE_IPV6 | |
274 if (fd == -1 && (errno == EINVAL || errno == EAFNOSUPPORT)) { | |
275 /* IPv6 is not supported by OS */ | |
276 so.sin.sin_family = AF_INET; | |
277 so.sin.sin_addr.s_addr = INADDR_ANY; | |
278 | |
279 fd = socket(AF_INET, SOCK_STREAM, 0); | |
280 } | |
281 #endif | |
282 if (fd == -1) | |
283 return -1; | |
284 | |
285 /* set socket options */ | |
286 net_set_nonblock(fd, TRUE); | |
287 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); | |
288 setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &opt, sizeof(opt)); | |
289 | |
290 /* specify the address/port we want to listen in */ | |
291 ret = bind(fd, &so.sa, SIZEOF_SOCKADDR(so)); | |
292 if (ret >= 0) { | |
293 /* get the actual port we started listen */ | |
294 len = SIZEOF_SOCKADDR(so); | |
295 ret = getsockname(fd, &so.sa, &len); | |
296 if (ret >= 0) { | |
297 *port = sin_get_port(&so); | |
298 | |
299 /* start listening */ | |
300 if (listen(fd, LISTEN_BACKLOG) >= 0) | |
301 return fd; | |
302 } | |
303 | |
304 } | |
305 | |
306 /* error */ | |
307 close_save_errno(fd); | |
308 return -1; | |
309 } | |
310 | |
311 int net_listen_unix(const char *path) | |
312 { | |
313 struct sockaddr_un sa; | |
314 int fd; | |
315 | |
807
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
316 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
|
317 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
|
318 if (strocpy(sa.sun_path, path, sizeof(sa.sun_path)) < 0) { |
0 | 319 /* too long path */ |
320 errno = EINVAL; | |
321 return -1; | |
322 } | |
323 | |
324 /* create the socket */ | |
325 fd = socket(PF_UNIX, SOCK_STREAM, 0); | |
326 if (fd == -1) | |
327 return -1; | |
328 | |
329 /* set socket options */ | |
330 net_set_nonblock(fd, TRUE); | |
331 | |
332 /* bind */ | |
333 if (bind(fd, (struct sockaddr *) &sa, sizeof(sa)) == 0) { | |
334 /* start listening */ | |
335 if (listen(fd, LISTEN_BACKLOG) == 0) | |
336 return fd; | |
337 } | |
338 | |
339 close_save_errno(fd); | |
340 return -1; | |
341 } | |
342 | |
343 /* 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
|
344 int net_accept(int fd, struct ip_addr *addr, unsigned int *port) |
0 | 345 { |
346 union sockaddr_union so; | |
347 int ret; | |
348 socklen_t addrlen; | |
349 | |
350 i_assert(fd >= 0); | |
351 | |
352 addrlen = sizeof(so); | |
353 ret = accept(fd, &so.sa, &addrlen); | |
354 | |
680
84e398270f7f
net_accept() returns now -2 for fatal failures.
Timo Sirainen <tss@iki.fi>
parents:
628
diff
changeset
|
355 if (ret < 0) { |
84e398270f7f
net_accept() returns now -2 for fatal failures.
Timo Sirainen <tss@iki.fi>
parents:
628
diff
changeset
|
356 if (errno == EBADF || errno == ENOTSOCK || |
84e398270f7f
net_accept() returns now -2 for fatal failures.
Timo Sirainen <tss@iki.fi>
parents:
628
diff
changeset
|
357 errno == EOPNOTSUPP || errno == EFAULT || errno == EINVAL) |
84e398270f7f
net_accept() returns now -2 for fatal failures.
Timo Sirainen <tss@iki.fi>
parents:
628
diff
changeset
|
358 return -2; |
84e398270f7f
net_accept() returns now -2 for fatal failures.
Timo Sirainen <tss@iki.fi>
parents:
628
diff
changeset
|
359 else |
84e398270f7f
net_accept() returns now -2 for fatal failures.
Timo Sirainen <tss@iki.fi>
parents:
628
diff
changeset
|
360 return -1; |
84e398270f7f
net_accept() returns now -2 for fatal failures.
Timo Sirainen <tss@iki.fi>
parents:
628
diff
changeset
|
361 } |
0 | 362 |
363 if (addr != NULL) sin_get_ip(&so, addr); | |
364 if (port != NULL) *port = sin_get_port(&so); | |
365 | |
159
e0193106a95d
net_accept() set listening socket nonblocking, not the new socket. thanks to
Timo Sirainen <tss@iki.fi>
parents:
64
diff
changeset
|
366 net_set_nonblock(ret, TRUE); |
0 | 367 return ret; |
368 } | |
369 | |
370 /* 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
|
371 ssize_t net_receive(int fd, void *buf, size_t len) |
0 | 372 { |
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
|
373 ssize_t ret; |
0 | 374 |
375 i_assert(fd >= 0); | |
376 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
|
377 i_assert(len <= SSIZE_T_MAX); |
0 | 378 |
379 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
|
380 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
|
381 /* disconnected */ |
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
382 errno = 0; |
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
383 return -2; |
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
384 } |
0 | 385 |
781
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
386 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
|
387 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
|
388 return 0; |
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
389 |
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
390 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
|
391 /* 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
|
392 return -2; |
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
393 } |
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
394 } |
0 | 395 |
396 return ret; | |
397 } | |
398 | |
399 /* 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
|
400 ssize_t net_transmit(int fd, const void *data, size_t len) |
0 | 401 { |
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
|
402 ssize_t ret; |
0 | 403 |
404 i_assert(fd >= 0); | |
405 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
|
406 i_assert(len <= SSIZE_T_MAX); |
0 | 407 |
64
83ae914a583a
added t_strdup_noconst() which can be used instead of (char *) t_strdup().
Timo Sirainen <tss@iki.fi>
parents:
34
diff
changeset
|
408 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
|
409 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
|
410 return 0; |
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
411 |
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
412 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
|
413 return -2; |
0 | 414 |
415 return ret; | |
416 } | |
417 | |
418 /* Get IP addresses for host. ips contains ips_count of IPs, they don't need | |
419 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
|
420 int net_gethostbyname(const char *addr, struct ip_addr **ips, int *ips_count) |
0 | 421 { |
805
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
781
diff
changeset
|
422 /* @UNSAFE */ |
0 | 423 #ifdef HAVE_IPV6 |
424 union sockaddr_union *so; | |
425 struct addrinfo hints, *ai, *origai; | |
426 char hbuf[NI_MAXHOST]; | |
427 int host_error; | |
428 #else | |
429 struct hostent *hp; | |
430 #endif | |
431 int count; | |
432 | |
433 i_assert(addr != NULL); | |
434 i_assert(ips != NULL); | |
435 i_assert(ips_count != NULL); | |
436 | |
437 *ips = NULL; | |
438 *ips_count = 0; | |
439 | |
440 #ifdef HAVE_IPV6 | |
441 memset(&hints, 0, sizeof(struct addrinfo)); | |
442 hints.ai_socktype = SOCK_STREAM; | |
443 | |
444 /* save error to host_error for later use */ | |
445 host_error = getaddrinfo(addr, NULL, &hints, &ai); | |
446 if (host_error != 0) | |
447 return host_error; | |
448 | |
449 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, hbuf, | |
450 sizeof(hbuf), NULL, 0, NI_NUMERICHOST) != 0) | |
451 return 1; | |
452 | |
453 | |
454 /* get number of IPs */ | |
455 origai = ai; | |
456 for (count = 0; ai != NULL; ai = ai->ai_next) | |
457 count++; | |
458 | |
459 *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
|
460 *ips = t_malloc(sizeof(struct ip_addr) * count); |
0 | 461 |
462 count = 0; | |
463 for (ai = origai; ai != NULL; ai = ai->ai_next, count++) { | |
464 so = (union sockaddr_union *) ai->ai_addr; | |
465 | |
466 sin_get_ip(so, ips[count]); | |
467 } | |
468 freeaddrinfo(origai); | |
469 #else | |
470 hp = gethostbyname(addr); | |
471 if (hp == NULL) | |
472 return h_errno; | |
473 | |
474 /* get number of IPs */ | |
475 count = 0; | |
476 while (hp->h_addr_list[count] != NULL) | |
477 count++; | |
478 | |
479 *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
|
480 *ips = t_malloc(sizeof(struct ip_addr) * count); |
0 | 481 |
482 while (count > 0) { | |
483 count--; | |
484 | |
485 (*ips)[count].family = AF_INET; | |
486 memcpy(&(*ips)[count].ip, hp->h_addr_list[count], 4); | |
487 } | |
488 #endif | |
489 | |
490 return 0; | |
491 } | |
492 | |
493 /* 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
|
494 int net_getsockname(int fd, struct ip_addr *addr, unsigned int *port) |
0 | 495 { |
496 union sockaddr_union so; | |
497 socklen_t addrlen; | |
498 | |
499 i_assert(fd >= 0); | |
500 | |
501 addrlen = sizeof(so); | |
502 if (getsockname(fd, (struct sockaddr *) &so, &addrlen) == -1) | |
503 return -1; | |
504 | |
505 if (addr != NULL) sin_get_ip(&so, addr); | |
506 if (port != NULL) *port = sin_get_port(&so); | |
507 | |
508 return 0; | |
509 } | |
510 | |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
511 const char *net_ip2host(const struct ip_addr *ip) |
0 | 512 { |
513 #ifdef HAVE_IPV6 | |
805
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
781
diff
changeset
|
514 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
|
515 |
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
781
diff
changeset
|
516 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
|
517 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
|
518 return NULL; |
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
781
diff
changeset
|
519 |
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
781
diff
changeset
|
520 return t_strdup(host); |
0 | 521 #else |
522 unsigned long ip4; | |
523 | |
805
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
781
diff
changeset
|
524 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
|
525 return NULL; |
0 | 526 |
527 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
|
528 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
|
529 (ip4 & 0xff000000UL) >> 24, |
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
781
diff
changeset
|
530 (ip4 & 0x00ff0000) >> 16, |
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
781
diff
changeset
|
531 (ip4 & 0x0000ff00) >> 8, |
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
781
diff
changeset
|
532 (ip4 & 0x000000ff)); |
0 | 533 #endif |
534 return 0; | |
535 } | |
536 | |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
537 int net_host2ip(const char *host, struct ip_addr *ip) |
0 | 538 { |
539 if (strchr(host, ':') != NULL) { | |
540 /* IPv6 */ | |
541 ip->family = AF_INET6; | |
542 #ifdef HAVE_IPV6 | |
543 if (inet_pton(AF_INET6, host, &ip->ip) == 0) | |
544 return -1; | |
545 #else | |
546 ip->ip.s_addr = 0; | |
547 #endif | |
548 } else { | |
549 /* IPv4 */ | |
550 ip->family = AF_INET; | |
551 if (inet_aton(host, (struct in_addr *) &ip->ip) == 0) | |
552 return -1; | |
553 } | |
554 | |
555 return 0; | |
556 } | |
557 | |
558 /* Get socket error */ | |
559 int net_geterror(int fd) | |
560 { | |
561 int data; | |
562 socklen_t len = sizeof(data); | |
563 | |
564 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &data, &len) == -1) | |
565 return -1; | |
566 | |
567 return data; | |
568 } | |
569 | |
570 /* get error of net_gethostname() */ | |
571 const char *net_gethosterror(int error) | |
572 { | |
573 #ifdef HAVE_IPV6 | |
574 i_assert(error != 0); | |
575 | |
576 if (error == 1) { | |
577 /* getnameinfo() failed */ | |
578 return strerror(errno); | |
579 } | |
580 | |
581 return gai_strerror(error); | |
582 #else | |
583 switch (error) { | |
584 case HOST_NOT_FOUND: | |
585 return "Host not found"; | |
586 case NO_ADDRESS: | |
587 return "No IP address found for name"; | |
588 case NO_RECOVERY: | |
589 return "A non-recovable name server error occurred"; | |
590 case TRY_AGAIN: | |
591 return "A temporary error on an authoritative name server"; | |
592 } | |
593 | |
594 /* unknown error */ | |
595 return NULL; | |
596 #endif | |
597 } | |
598 | |
599 /* return TRUE if host lookup failed because it didn't exist (ie. not | |
600 some error with name server) */ | |
601 int net_hosterror_notfound(int error) | |
602 { | |
603 #ifdef HAVE_IPV6 | |
604 return error != 1 && (error == EAI_NONAME || error == EAI_NODATA); | |
605 #else | |
606 return error == HOST_NOT_FOUND || error == NO_ADDRESS; | |
607 #endif | |
608 } | |
609 | |
610 /* Get name of TCP service */ | |
349 | 611 char *net_getservbyport(unsigned short port) |
0 | 612 { |
613 struct servent *entry; | |
614 | |
349 | 615 entry = getservbyport(htons(port), "tcp"); |
0 | 616 return entry == NULL ? NULL : entry->s_name; |
617 } | |
618 | |
619 int is_ipv4_address(const char *host) | |
620 { | |
621 while (*host != '\0') { | |
622 if (*host != '.' && !i_isdigit(*host)) | |
623 return 0; | |
624 host++; | |
625 } | |
626 | |
627 return 1; | |
628 } | |
629 | |
630 int is_ipv6_address(const char *host) | |
631 { | |
632 while (*host != '\0') { | |
633 if (*host != ':' && !i_isxdigit(*host)) | |
634 return 0; | |
635 host++; | |
636 } | |
637 | |
638 return 1; | |
639 } |