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