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