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