Mercurial > dovecot > core-2.2
annotate src/lib/network.c @ 5358:a977ad033903 HEAD
net_listen_unix() API changed a bit. -1 is now returned for errors that can
be ignored (remote closed the socket) and -2 for everything else.
author | Timo Sirainen <tss@iki.fi> |
---|---|
date | Sun, 18 Mar 2007 04:00:59 +0200 |
parents | 7ee0bb1b2d97 |
children | dc8d38798296 |
rev | line source |
---|---|
3621
3ae2df67459c
Added fd_set_nonblock() and changed net_set_nonblock() to use it.
Timo Sirainen <tss@iki.fi>
parents:
3577
diff
changeset
|
1 /* Copyright (c) 1999-2005 Timo Sirainen */ |
0 | 2 |
3 #include "lib.h" | |
4011
323c09be5f00
Moved dupliated close_save_errno() code to public close_keep_errno() function.
Timo Sirainen <timo.sirainen@movial.fi>
parents:
3863
diff
changeset
|
4 #include "close-keep-errno.h" |
3621
3ae2df67459c
Added fd_set_nonblock() and changed net_set_nonblock() to use it.
Timo Sirainen <tss@iki.fi>
parents:
3577
diff
changeset
|
5 #include "fd-set-nonblock.h" |
0 | 6 #include "network.h" |
7 | |
8 #include <unistd.h> | |
9 #include <fcntl.h> | |
10 #include <ctype.h> | |
11 #include <sys/un.h> | |
12 #include <netinet/tcp.h> | |
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 | |
3863
55df57c028d4
Added "bool" type and changed all ints that were used as booleans to bool.
Timo Sirainen <tss@iki.fi>
parents:
3681
diff
changeset
|
29 bool 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 | |
3400
ddfa507bb74f
Don't return random data if net_getpeername() or net_getsockname() is used
Timo Sirainen <tss@iki.fi>
parents:
3373
diff
changeset
|
77 if (ip->family == AF_INET) |
0 | 78 memcpy(&ip->ip, &so->sin.sin_addr, 4); |
3400
ddfa507bb74f
Don't return random data if net_getpeername() or net_getsockname() is used
Timo Sirainen <tss@iki.fi>
parents:
3373
diff
changeset
|
79 else |
ddfa507bb74f
Don't return random data if net_getpeername() or net_getsockname() is used
Timo Sirainen <tss@iki.fi>
parents:
3373
diff
changeset
|
80 memset(&ip->ip, 0, sizeof(ip->ip)); |
0 | 81 } |
82 | |
349 | 83 static inline void sin_set_port(union sockaddr_union *so, unsigned int port) |
0 | 84 { |
85 #ifdef HAVE_IPV6 | |
86 if (so->sin.sin_family == AF_INET6) | |
87 so->sin6.sin6_port = htons((unsigned short) port); | |
88 else | |
89 #endif | |
90 so->sin.sin_port = htons((unsigned short) port); | |
91 } | |
92 | |
349 | 93 static inline unsigned int sin_get_port(union sockaddr_union *so) |
0 | 94 { |
95 #ifdef HAVE_IPV6 | |
96 if (so->sin.sin_family == AF_INET6) | |
97 return ntohs(so->sin6.sin6_port); | |
98 #endif | |
3400
ddfa507bb74f
Don't return random data if net_getpeername() or net_getsockname() is used
Timo Sirainen <tss@iki.fi>
parents:
3373
diff
changeset
|
99 if (so->sin.sin_family == AF_INET) |
ddfa507bb74f
Don't return random data if net_getpeername() or net_getsockname() is used
Timo Sirainen <tss@iki.fi>
parents:
3373
diff
changeset
|
100 return ntohs(so->sin.sin_port); |
ddfa507bb74f
Don't return random data if net_getpeername() or net_getsockname() is used
Timo Sirainen <tss@iki.fi>
parents:
3373
diff
changeset
|
101 |
ddfa507bb74f
Don't return random data if net_getpeername() or net_getsockname() is used
Timo Sirainen <tss@iki.fi>
parents:
3373
diff
changeset
|
102 return 0; |
0 | 103 } |
104 | |
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 | |
3523
d1ed3b3548db
Added some more error messages. Also if net_connect_ip() gives my_ip
Timo Sirainen <tss@iki.fi>
parents:
3400
diff
changeset
|
121 if (fd == -1) { |
d1ed3b3548db
Added some more error messages. Also if net_connect_ip() gives my_ip
Timo Sirainen <tss@iki.fi>
parents:
3400
diff
changeset
|
122 i_error("socket() failed: %m"); |
0 | 123 return -1; |
3523
d1ed3b3548db
Added some more error messages. Also if net_connect_ip() gives my_ip
Timo Sirainen <tss@iki.fi>
parents:
3400
diff
changeset
|
124 } |
0 | 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 */ | |
3523
d1ed3b3548db
Added some more error messages. Also if net_connect_ip() gives my_ip
Timo Sirainen <tss@iki.fi>
parents:
3400
diff
changeset
|
136 i_error("bind(%s) failed: %m", net_ip2addr(my_ip)); |
4011
323c09be5f00
Moved dupliated close_save_errno() code to public close_keep_errno() function.
Timo Sirainen <timo.sirainen@movial.fi>
parents:
3863
diff
changeset
|
137 close_keep_errno(fd); |
3523
d1ed3b3548db
Added some more error messages. Also if net_connect_ip() gives my_ip
Timo Sirainen <tss@iki.fi>
parents:
3400
diff
changeset
|
138 return -1; |
0 | 139 } |
140 } | |
141 | |
142 /* connect */ | |
143 sin_set_ip(&so, ip); | |
144 sin_set_port(&so, port); | |
145 ret = connect(fd, &so.sa, SIZEOF_SOCKADDR(so)); | |
146 | |
147 #ifndef WIN32 | |
148 if (ret < 0 && errno != EINPROGRESS) | |
149 #else | |
150 if (ret < 0 && WSAGetLastError() != WSAEWOULDBLOCK) | |
151 #endif | |
152 { | |
4011
323c09be5f00
Moved dupliated close_save_errno() code to public close_keep_errno() function.
Timo Sirainen <timo.sirainen@movial.fi>
parents:
3863
diff
changeset
|
153 close_keep_errno(fd); |
0 | 154 return -1; |
155 } | |
156 | |
157 return fd; | |
158 } | |
159 | |
160 int net_connect_unix(const char *path) | |
161 { | |
162 struct sockaddr_un sa; | |
163 int fd, ret; | |
164 | |
807
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
165 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
|
166 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
|
167 if (strocpy(sa.sun_path, path, sizeof(sa.sun_path)) < 0) { |
0 | 168 /* too long path */ |
169 errno = EINVAL; | |
170 return -1; | |
171 } | |
172 | |
173 /* create the socket */ | |
174 fd = socket(PF_UNIX, SOCK_STREAM, 0); | |
3523
d1ed3b3548db
Added some more error messages. Also if net_connect_ip() gives my_ip
Timo Sirainen <tss@iki.fi>
parents:
3400
diff
changeset
|
175 if (fd == -1) { |
d1ed3b3548db
Added some more error messages. Also if net_connect_ip() gives my_ip
Timo Sirainen <tss@iki.fi>
parents:
3400
diff
changeset
|
176 i_error("socket(%s) failed: %m", path); |
0 | 177 return -1; |
3523
d1ed3b3548db
Added some more error messages. Also if net_connect_ip() gives my_ip
Timo Sirainen <tss@iki.fi>
parents:
3400
diff
changeset
|
178 } |
0 | 179 |
3076
8d15fea729c2
Use nonblocking connecting for unix sockets.
Timo Sirainen <tss@iki.fi>
parents:
3075
diff
changeset
|
180 net_set_nonblock(fd, TRUE); |
8d15fea729c2
Use nonblocking connecting for unix sockets.
Timo Sirainen <tss@iki.fi>
parents:
3075
diff
changeset
|
181 |
0 | 182 /* connect */ |
4018
989dd95aa8b9
Enable -Wstrict-aliasing=2 warning and fixed a couple of warnings it gave.
Timo Sirainen <tss@iki.fi>
parents:
4011
diff
changeset
|
183 ret = connect(fd, (void *)&sa, sizeof(sa)); |
0 | 184 if (ret < 0 && errno != EINPROGRESS) { |
4011
323c09be5f00
Moved dupliated close_save_errno() code to public close_keep_errno() function.
Timo Sirainen <timo.sirainen@movial.fi>
parents:
3863
diff
changeset
|
185 close_keep_errno(fd); |
0 | 186 return -1; |
187 } | |
188 | |
189 return fd; | |
190 } | |
191 | |
192 void net_disconnect(int fd) | |
193 { | |
388
792fc5b3daa4
Send error message if close() fails in net_disconnect().
Timo Sirainen <tss@iki.fi>
parents:
349
diff
changeset
|
194 if (close(fd) < 0) |
792fc5b3daa4
Send error message if close() fails in net_disconnect().
Timo Sirainen <tss@iki.fi>
parents:
349
diff
changeset
|
195 i_error("net_disconnect() failed: %m"); |
0 | 196 } |
197 | |
3863
55df57c028d4
Added "bool" type and changed all ints that were used as booleans to bool.
Timo Sirainen <tss@iki.fi>
parents:
3681
diff
changeset
|
198 void net_set_nonblock(int fd, bool nonblock) |
0 | 199 { |
3621
3ae2df67459c
Added fd_set_nonblock() and changed net_set_nonblock() to use it.
Timo Sirainen <tss@iki.fi>
parents:
3577
diff
changeset
|
200 if (fd_set_nonblock(fd, nonblock) < 0) |
3ae2df67459c
Added fd_set_nonblock() and changed net_set_nonblock() to use it.
Timo Sirainen <tss@iki.fi>
parents:
3577
diff
changeset
|
201 i_fatal("fd_set_nonblock(%d) failed: %m", fd); |
0 | 202 } |
203 | |
3863
55df57c028d4
Added "bool" type and changed all ints that were used as booleans to bool.
Timo Sirainen <tss@iki.fi>
parents:
3681
diff
changeset
|
204 int net_set_cork(int fd __attr_unused__, bool cork __attr_unused__) |
0 | 205 { |
206 #ifdef TCP_CORK | |
3863
55df57c028d4
Added "bool" type and changed all ints that were used as booleans to bool.
Timo Sirainen <tss@iki.fi>
parents:
3681
diff
changeset
|
207 int val = cork; |
55df57c028d4
Added "bool" type and changed all ints that were used as booleans to bool.
Timo Sirainen <tss@iki.fi>
parents:
3681
diff
changeset
|
208 |
55df57c028d4
Added "bool" type and changed all ints that were used as booleans to bool.
Timo Sirainen <tss@iki.fi>
parents:
3681
diff
changeset
|
209 return setsockopt(fd, IPPROTO_TCP, TCP_CORK, &val, sizeof(val)); |
410
1f0e7229ee58
Split IOBuffer into mmaped IBuffer, file IBuffer, memory data IBuffer and
Timo Sirainen <tss@iki.fi>
parents:
404
diff
changeset
|
210 #else |
1f0e7229ee58
Split IOBuffer into mmaped IBuffer, file IBuffer, memory data IBuffer and
Timo Sirainen <tss@iki.fi>
parents:
404
diff
changeset
|
211 errno = ENOPROTOOPT; |
1f0e7229ee58
Split IOBuffer into mmaped IBuffer, file IBuffer, memory data IBuffer and
Timo Sirainen <tss@iki.fi>
parents:
404
diff
changeset
|
212 return -1; |
0 | 213 #endif |
214 } | |
215 | |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
216 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
|
217 { |
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
680
diff
changeset
|
218 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
|
219 |
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
680
diff
changeset
|
220 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
|
221 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
|
222 } |
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
680
diff
changeset
|
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_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
|
225 { |
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
680
diff
changeset
|
226 #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
|
227 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
|
228 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
|
229 #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
|
230 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
|
231 #endif |
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 |
3075
9cb91ed5a110
Added backlog parameter for net_listen*().
Timo Sirainen <tss@iki.fi>
parents:
2639
diff
changeset
|
234 int net_listen(const struct ip_addr *my_ip, unsigned int *port, int backlog) |
0 | 235 { |
236 union sockaddr_union so; | |
237 int ret, fd, opt = 1; | |
238 socklen_t len; | |
239 | |
240 i_assert(port != NULL); | |
241 | |
242 memset(&so, 0, sizeof(so)); | |
243 sin_set_port(&so, *port); | |
244 sin_set_ip(&so, my_ip); | |
245 | |
246 /* create the socket */ | |
247 fd = socket(so.sin.sin_family, SOCK_STREAM, 0); | |
248 #ifdef HAVE_IPV6 | |
249 if (fd == -1 && (errno == EINVAL || errno == EAFNOSUPPORT)) { | |
250 /* IPv6 is not supported by OS */ | |
251 so.sin.sin_family = AF_INET; | |
252 so.sin.sin_addr.s_addr = INADDR_ANY; | |
253 | |
254 fd = socket(AF_INET, SOCK_STREAM, 0); | |
255 } | |
256 #endif | |
3523
d1ed3b3548db
Added some more error messages. Also if net_connect_ip() gives my_ip
Timo Sirainen <tss@iki.fi>
parents:
3400
diff
changeset
|
257 if (fd == -1) { |
d1ed3b3548db
Added some more error messages. Also if net_connect_ip() gives my_ip
Timo Sirainen <tss@iki.fi>
parents:
3400
diff
changeset
|
258 i_error("socket() failed: %m"); |
0 | 259 return -1; |
3523
d1ed3b3548db
Added some more error messages. Also if net_connect_ip() gives my_ip
Timo Sirainen <tss@iki.fi>
parents:
3400
diff
changeset
|
260 } |
0 | 261 |
262 /* set socket options */ | |
263 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); | |
264 setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &opt, sizeof(opt)); | |
265 | |
3373
690dcc51cdc1
if using IPv6, bind both on the IPv4 and IPv6 addresses if IPV6_V6ONLY is
Timo Sirainen <tss@iki.fi>
parents:
3076
diff
changeset
|
266 /* if using IPv6, bind both on the IPv4 and IPv6 addresses */ |
690dcc51cdc1
if using IPv6, bind both on the IPv4 and IPv6 addresses if IPV6_V6ONLY is
Timo Sirainen <tss@iki.fi>
parents:
3076
diff
changeset
|
267 #ifdef IPV6_V6ONLY |
690dcc51cdc1
if using IPv6, bind both on the IPv4 and IPv6 addresses if IPV6_V6ONLY is
Timo Sirainen <tss@iki.fi>
parents:
3076
diff
changeset
|
268 if (so.sin.sin_family == AF_INET6) { |
690dcc51cdc1
if using IPv6, bind both on the IPv4 and IPv6 addresses if IPV6_V6ONLY is
Timo Sirainen <tss@iki.fi>
parents:
3076
diff
changeset
|
269 opt = 0; |
690dcc51cdc1
if using IPv6, bind both on the IPv4 and IPv6 addresses if IPV6_V6ONLY is
Timo Sirainen <tss@iki.fi>
parents:
3076
diff
changeset
|
270 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &opt, sizeof(opt)); |
690dcc51cdc1
if using IPv6, bind both on the IPv4 and IPv6 addresses if IPV6_V6ONLY is
Timo Sirainen <tss@iki.fi>
parents:
3076
diff
changeset
|
271 } |
690dcc51cdc1
if using IPv6, bind both on the IPv4 and IPv6 addresses if IPV6_V6ONLY is
Timo Sirainen <tss@iki.fi>
parents:
3076
diff
changeset
|
272 #endif |
0 | 273 /* specify the address/port we want to listen in */ |
274 ret = bind(fd, &so.sa, SIZEOF_SOCKADDR(so)); | |
3537
5ff3b88db948
net_listen(): Print bind() errors only if it's not EADDRINUSE. Also do the
Timo Sirainen <tss@iki.fi>
parents:
3523
diff
changeset
|
275 if (ret < 0) { |
5ff3b88db948
net_listen(): Print bind() errors only if it's not EADDRINUSE. Also do the
Timo Sirainen <tss@iki.fi>
parents:
3523
diff
changeset
|
276 if (errno != EADDRINUSE) { |
5ff3b88db948
net_listen(): Print bind() errors only if it's not EADDRINUSE. Also do the
Timo Sirainen <tss@iki.fi>
parents:
3523
diff
changeset
|
277 i_error("bind(%s, %u) failed: %m", |
5ff3b88db948
net_listen(): Print bind() errors only if it's not EADDRINUSE. Also do the
Timo Sirainen <tss@iki.fi>
parents:
3523
diff
changeset
|
278 net_ip2addr(my_ip), *port); |
5ff3b88db948
net_listen(): Print bind() errors only if it's not EADDRINUSE. Also do the
Timo Sirainen <tss@iki.fi>
parents:
3523
diff
changeset
|
279 } |
5ff3b88db948
net_listen(): Print bind() errors only if it's not EADDRINUSE. Also do the
Timo Sirainen <tss@iki.fi>
parents:
3523
diff
changeset
|
280 } else { |
0 | 281 /* get the actual port we started listen */ |
282 len = SIZEOF_SOCKADDR(so); | |
283 ret = getsockname(fd, &so.sa, &len); | |
284 if (ret >= 0) { | |
285 *port = sin_get_port(&so); | |
286 | |
287 /* start listening */ | |
3075
9cb91ed5a110
Added backlog parameter for net_listen*().
Timo Sirainen <tss@iki.fi>
parents:
2639
diff
changeset
|
288 if (listen(fd, backlog) >= 0) |
3537
5ff3b88db948
net_listen(): Print bind() errors only if it's not EADDRINUSE. Also do the
Timo Sirainen <tss@iki.fi>
parents:
3523
diff
changeset
|
289 return fd; |
5ff3b88db948
net_listen(): Print bind() errors only if it's not EADDRINUSE. Also do the
Timo Sirainen <tss@iki.fi>
parents:
3523
diff
changeset
|
290 |
5ff3b88db948
net_listen(): Print bind() errors only if it's not EADDRINUSE. Also do the
Timo Sirainen <tss@iki.fi>
parents:
3523
diff
changeset
|
291 if (errno != EADDRINUSE) |
5ff3b88db948
net_listen(): Print bind() errors only if it's not EADDRINUSE. Also do the
Timo Sirainen <tss@iki.fi>
parents:
3523
diff
changeset
|
292 i_error("listen() failed: %m"); |
0 | 293 } |
294 } | |
295 | |
296 /* error */ | |
4011
323c09be5f00
Moved dupliated close_save_errno() code to public close_keep_errno() function.
Timo Sirainen <timo.sirainen@movial.fi>
parents:
3863
diff
changeset
|
297 close_keep_errno(fd); |
0 | 298 return -1; |
299 } | |
300 | |
3075
9cb91ed5a110
Added backlog parameter for net_listen*().
Timo Sirainen <tss@iki.fi>
parents:
2639
diff
changeset
|
301 int net_listen_unix(const char *path, int backlog) |
0 | 302 { |
303 struct sockaddr_un sa; | |
304 int fd; | |
305 | |
807
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
306 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
|
307 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
|
308 if (strocpy(sa.sun_path, path, sizeof(sa.sun_path)) < 0) { |
0 | 309 /* too long path */ |
310 errno = EINVAL; | |
311 return -1; | |
312 } | |
313 | |
314 /* create the socket */ | |
315 fd = socket(PF_UNIX, SOCK_STREAM, 0); | |
3523
d1ed3b3548db
Added some more error messages. Also if net_connect_ip() gives my_ip
Timo Sirainen <tss@iki.fi>
parents:
3400
diff
changeset
|
316 if (fd == -1) { |
d1ed3b3548db
Added some more error messages. Also if net_connect_ip() gives my_ip
Timo Sirainen <tss@iki.fi>
parents:
3400
diff
changeset
|
317 i_error("socket() failed: %m"); |
0 | 318 return -1; |
3523
d1ed3b3548db
Added some more error messages. Also if net_connect_ip() gives my_ip
Timo Sirainen <tss@iki.fi>
parents:
3400
diff
changeset
|
319 } |
0 | 320 |
321 /* bind */ | |
4018
989dd95aa8b9
Enable -Wstrict-aliasing=2 warning and fixed a couple of warnings it gave.
Timo Sirainen <tss@iki.fi>
parents:
4011
diff
changeset
|
322 if (bind(fd, (void *)&sa, sizeof(sa)) < 0) { |
3545
0b6539bc22b9
Don't log extra bind() errors for net_listen_unix() either
Timo Sirainen <tss@iki.fi>
parents:
3537
diff
changeset
|
323 if (errno != EADDRINUSE) |
0b6539bc22b9
Don't log extra bind() errors for net_listen_unix() either
Timo Sirainen <tss@iki.fi>
parents:
3537
diff
changeset
|
324 i_error("bind(%s) failed: %m", path); |
0b6539bc22b9
Don't log extra bind() errors for net_listen_unix() either
Timo Sirainen <tss@iki.fi>
parents:
3537
diff
changeset
|
325 } else { |
0 | 326 /* start listening */ |
3075
9cb91ed5a110
Added backlog parameter for net_listen*().
Timo Sirainen <tss@iki.fi>
parents:
2639
diff
changeset
|
327 if (listen(fd, backlog) == 0) |
0 | 328 return fd; |
3545
0b6539bc22b9
Don't log extra bind() errors for net_listen_unix() either
Timo Sirainen <tss@iki.fi>
parents:
3537
diff
changeset
|
329 |
0b6539bc22b9
Don't log extra bind() errors for net_listen_unix() either
Timo Sirainen <tss@iki.fi>
parents:
3537
diff
changeset
|
330 if (errno != EADDRINUSE) |
0b6539bc22b9
Don't log extra bind() errors for net_listen_unix() either
Timo Sirainen <tss@iki.fi>
parents:
3537
diff
changeset
|
331 i_error("listen() failed: %m"); |
0 | 332 } |
333 | |
4011
323c09be5f00
Moved dupliated close_save_errno() code to public close_keep_errno() function.
Timo Sirainen <timo.sirainen@movial.fi>
parents:
3863
diff
changeset
|
334 close_keep_errno(fd); |
0 | 335 return -1; |
336 } | |
337 | |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
338 int net_accept(int fd, struct ip_addr *addr, unsigned int *port) |
0 | 339 { |
340 union sockaddr_union so; | |
341 int ret; | |
342 socklen_t addrlen; | |
343 | |
344 i_assert(fd >= 0); | |
345 | |
346 addrlen = sizeof(so); | |
347 ret = accept(fd, &so.sa, &addrlen); | |
348 | |
680
84e398270f7f
net_accept() returns now -2 for fatal failures.
Timo Sirainen <tss@iki.fi>
parents:
628
diff
changeset
|
349 if (ret < 0) { |
5358
a977ad033903
net_listen_unix() API changed a bit. -1 is now returned for errors that can
Timo Sirainen <tss@iki.fi>
parents:
4745
diff
changeset
|
350 if (errno == EAGAIN || errno == ECONNABORTED) |
a977ad033903
net_listen_unix() API changed a bit. -1 is now returned for errors that can
Timo Sirainen <tss@iki.fi>
parents:
4745
diff
changeset
|
351 return -1; |
a977ad033903
net_listen_unix() API changed a bit. -1 is now returned for errors that can
Timo Sirainen <tss@iki.fi>
parents:
4745
diff
changeset
|
352 else |
680
84e398270f7f
net_accept() returns now -2 for fatal failures.
Timo Sirainen <tss@iki.fi>
parents:
628
diff
changeset
|
353 return -2; |
84e398270f7f
net_accept() returns now -2 for fatal failures.
Timo Sirainen <tss@iki.fi>
parents:
628
diff
changeset
|
354 } |
0 | 355 |
356 if (addr != NULL) sin_get_ip(&so, addr); | |
357 if (port != NULL) *port = sin_get_port(&so); | |
358 | |
359 return ret; | |
360 } | |
361 | |
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
|
362 ssize_t net_receive(int fd, void *buf, size_t len) |
0 | 363 { |
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
|
364 ssize_t ret; |
0 | 365 |
366 i_assert(fd >= 0); | |
367 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
|
368 i_assert(len <= SSIZE_T_MAX); |
0 | 369 |
3681
771cd6776d4b
Use read() instead of recv() in net_receive(), so it works even with
Timo Sirainen <tss@iki.fi>
parents:
3621
diff
changeset
|
370 ret = read(fd, buf, len); |
781
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
371 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
|
372 /* disconnected */ |
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
373 errno = 0; |
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
374 return -2; |
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
375 } |
0 | 376 |
781
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
377 if (ret < 0) { |
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
378 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
|
379 return 0; |
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
380 |
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
381 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
|
382 /* 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
|
383 return -2; |
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
384 } |
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
385 } |
0 | 386 |
387 return ret; | |
388 } | |
389 | |
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
|
390 ssize_t net_transmit(int fd, const void *data, size_t len) |
0 | 391 { |
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
|
392 ssize_t ret; |
0 | 393 |
394 i_assert(fd >= 0); | |
395 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
|
396 i_assert(len <= SSIZE_T_MAX); |
0 | 397 |
64
83ae914a583a
added t_strdup_noconst() which can be used instead of (char *) t_strdup().
Timo Sirainen <tss@iki.fi>
parents:
34
diff
changeset
|
398 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
|
399 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
|
400 return 0; |
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
401 |
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
402 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
|
403 return -2; |
0 | 404 |
405 return ret; | |
406 } | |
407 | |
4745
7ee0bb1b2d97
Changed ips_count to unsigned int in net_gethostbyname(). Also removed
Timo Sirainen <tss@iki.fi>
parents:
4699
diff
changeset
|
408 int net_gethostbyname(const char *addr, struct ip_addr **ips, |
7ee0bb1b2d97
Changed ips_count to unsigned int in net_gethostbyname(). Also removed
Timo Sirainen <tss@iki.fi>
parents:
4699
diff
changeset
|
409 unsigned int *ips_count) |
0 | 410 { |
805
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
781
diff
changeset
|
411 /* @UNSAFE */ |
0 | 412 #ifdef HAVE_IPV6 |
413 union sockaddr_union *so; | |
414 struct addrinfo hints, *ai, *origai; | |
415 char hbuf[NI_MAXHOST]; | |
416 int host_error; | |
417 #else | |
418 struct hostent *hp; | |
419 #endif | |
420 int count; | |
421 | |
422 i_assert(addr != NULL); | |
423 i_assert(ips != NULL); | |
424 i_assert(ips_count != NULL); | |
425 | |
426 *ips = NULL; | |
427 *ips_count = 0; | |
428 | |
429 #ifdef HAVE_IPV6 | |
430 memset(&hints, 0, sizeof(struct addrinfo)); | |
431 hints.ai_socktype = SOCK_STREAM; | |
432 | |
433 /* save error to host_error for later use */ | |
434 host_error = getaddrinfo(addr, NULL, &hints, &ai); | |
435 if (host_error != 0) | |
436 return host_error; | |
437 | |
438 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, hbuf, | |
439 sizeof(hbuf), NULL, 0, NI_NUMERICHOST) != 0) | |
440 return 1; | |
441 | |
442 | |
443 /* get number of IPs */ | |
444 origai = ai; | |
445 for (count = 0; ai != NULL; ai = ai->ai_next) | |
446 count++; | |
447 | |
448 *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
|
449 *ips = t_malloc(sizeof(struct ip_addr) * count); |
0 | 450 |
451 count = 0; | |
452 for (ai = origai; ai != NULL; ai = ai->ai_next, count++) { | |
453 so = (union sockaddr_union *) ai->ai_addr; | |
454 | |
2113
c5817f302aa6
net_gethostbyname() was broken with IPv6 enabled (wasn't really used with
Timo Sirainen <tss@iki.fi>
parents:
2098
diff
changeset
|
455 sin_get_ip(so, &(*ips)[count]); |
0 | 456 } |
457 freeaddrinfo(origai); | |
458 #else | |
459 hp = gethostbyname(addr); | |
460 if (hp == NULL) | |
461 return h_errno; | |
462 | |
463 /* get number of IPs */ | |
464 count = 0; | |
465 while (hp->h_addr_list[count] != NULL) | |
466 count++; | |
467 | |
468 *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
|
469 *ips = t_malloc(sizeof(struct ip_addr) * count); |
0 | 470 |
471 while (count > 0) { | |
472 count--; | |
473 | |
474 (*ips)[count].family = AF_INET; | |
475 memcpy(&(*ips)[count].ip, hp->h_addr_list[count], 4); | |
476 } | |
477 #endif | |
478 | |
479 return 0; | |
480 } | |
481 | |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
482 int net_getsockname(int fd, struct ip_addr *addr, unsigned int *port) |
0 | 483 { |
484 union sockaddr_union so; | |
485 socklen_t addrlen; | |
486 | |
487 i_assert(fd >= 0); | |
488 | |
489 addrlen = sizeof(so); | |
4018
989dd95aa8b9
Enable -Wstrict-aliasing=2 warning and fixed a couple of warnings it gave.
Timo Sirainen <tss@iki.fi>
parents:
4011
diff
changeset
|
490 if (getsockname(fd, &so.sa, &addrlen) == -1) |
0 | 491 return -1; |
492 | |
493 if (addr != NULL) sin_get_ip(&so, addr); | |
494 if (port != NULL) *port = sin_get_port(&so); | |
495 | |
496 return 0; | |
497 } | |
498 | |
2098 | 499 int net_getpeername(int fd, struct ip_addr *addr, unsigned int *port) |
500 { | |
501 union sockaddr_union so; | |
502 socklen_t addrlen; | |
503 | |
504 i_assert(fd >= 0); | |
505 | |
506 addrlen = sizeof(so); | |
4018
989dd95aa8b9
Enable -Wstrict-aliasing=2 warning and fixed a couple of warnings it gave.
Timo Sirainen <tss@iki.fi>
parents:
4011
diff
changeset
|
507 if (getpeername(fd, &so.sa, &addrlen) == -1) |
2098 | 508 return -1; |
509 | |
510 if (addr != NULL) sin_get_ip(&so, addr); | |
511 if (port != NULL) *port = sin_get_port(&so); | |
512 | |
513 return 0; | |
514 } | |
515 | |
1485
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
1268
diff
changeset
|
516 const char *net_ip2addr(const struct ip_addr *ip) |
0 | 517 { |
518 #ifdef HAVE_IPV6 | |
1485
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
1268
diff
changeset
|
519 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
|
520 |
1485
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
1268
diff
changeset
|
521 addr[MAX_IP_LEN] = '\0'; |
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
1268
diff
changeset
|
522 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
|
523 return NULL; |
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
781
diff
changeset
|
524 |
1485
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
1268
diff
changeset
|
525 return t_strdup(addr); |
0 | 526 #else |
527 unsigned long ip4; | |
528 | |
805
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
781
diff
changeset
|
529 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
|
530 return NULL; |
0 | 531 |
532 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
|
533 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
|
534 (ip4 & 0xff000000UL) >> 24, |
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
781
diff
changeset
|
535 (ip4 & 0x00ff0000) >> 16, |
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
781
diff
changeset
|
536 (ip4 & 0x0000ff00) >> 8, |
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
781
diff
changeset
|
537 (ip4 & 0x000000ff)); |
0 | 538 #endif |
539 } | |
540 | |
1485
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
1268
diff
changeset
|
541 int net_addr2ip(const char *addr, struct ip_addr *ip) |
0 | 542 { |
1485
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
1268
diff
changeset
|
543 if (strchr(addr, ':') != NULL) { |
0 | 544 /* IPv6 */ |
545 ip->family = AF_INET6; | |
546 #ifdef HAVE_IPV6 | |
1485
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
1268
diff
changeset
|
547 if (inet_pton(AF_INET6, addr, &ip->ip) == 0) |
0 | 548 return -1; |
549 #else | |
550 ip->ip.s_addr = 0; | |
551 #endif | |
552 } else { | |
553 /* IPv4 */ | |
554 ip->family = AF_INET; | |
1485
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
1268
diff
changeset
|
555 if (inet_aton(addr, (struct in_addr *) &ip->ip) == 0) |
0 | 556 return -1; |
557 } | |
558 | |
559 return 0; | |
560 } | |
561 | |
4684
4f1df4b90500
Added net_ipv6_mapped_ipv4_convert().
Timo Sirainen <tss@iki.fi>
parents:
4018
diff
changeset
|
562 int net_ipv6_mapped_ipv4_convert(const struct ip_addr *src, |
4f1df4b90500
Added net_ipv6_mapped_ipv4_convert().
Timo Sirainen <tss@iki.fi>
parents:
4018
diff
changeset
|
563 struct ip_addr *dest) |
4f1df4b90500
Added net_ipv6_mapped_ipv4_convert().
Timo Sirainen <tss@iki.fi>
parents:
4018
diff
changeset
|
564 { |
4f1df4b90500
Added net_ipv6_mapped_ipv4_convert().
Timo Sirainen <tss@iki.fi>
parents:
4018
diff
changeset
|
565 #ifdef HAVE_IPV6 |
4699
9e4cc68b71b2
Fix net_ipv6_mapped_ipv4_convert() to be actually portable.
Timo Sirainen <tss@iki.fi>
parents:
4684
diff
changeset
|
566 static uint8_t v4_prefix[] = |
9e4cc68b71b2
Fix net_ipv6_mapped_ipv4_convert() to be actually portable.
Timo Sirainen <tss@iki.fi>
parents:
4684
diff
changeset
|
567 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff }; |
9e4cc68b71b2
Fix net_ipv6_mapped_ipv4_convert() to be actually portable.
Timo Sirainen <tss@iki.fi>
parents:
4684
diff
changeset
|
568 |
4684
4f1df4b90500
Added net_ipv6_mapped_ipv4_convert().
Timo Sirainen <tss@iki.fi>
parents:
4018
diff
changeset
|
569 if (!IPADDR_IS_V6(src)) |
4f1df4b90500
Added net_ipv6_mapped_ipv4_convert().
Timo Sirainen <tss@iki.fi>
parents:
4018
diff
changeset
|
570 return -1; |
4699
9e4cc68b71b2
Fix net_ipv6_mapped_ipv4_convert() to be actually portable.
Timo Sirainen <tss@iki.fi>
parents:
4684
diff
changeset
|
571 if (memcmp(src->ip.s6_addr, v4_prefix, sizeof(v4_prefix)) != 0) |
4684
4f1df4b90500
Added net_ipv6_mapped_ipv4_convert().
Timo Sirainen <tss@iki.fi>
parents:
4018
diff
changeset
|
572 return -1; |
4f1df4b90500
Added net_ipv6_mapped_ipv4_convert().
Timo Sirainen <tss@iki.fi>
parents:
4018
diff
changeset
|
573 |
4f1df4b90500
Added net_ipv6_mapped_ipv4_convert().
Timo Sirainen <tss@iki.fi>
parents:
4018
diff
changeset
|
574 dest->family = AF_INET; |
4699
9e4cc68b71b2
Fix net_ipv6_mapped_ipv4_convert() to be actually portable.
Timo Sirainen <tss@iki.fi>
parents:
4684
diff
changeset
|
575 memcpy(&dest->ip, &src->ip.s6_addr[3*4], 4); |
4684
4f1df4b90500
Added net_ipv6_mapped_ipv4_convert().
Timo Sirainen <tss@iki.fi>
parents:
4018
diff
changeset
|
576 return 0; |
4f1df4b90500
Added net_ipv6_mapped_ipv4_convert().
Timo Sirainen <tss@iki.fi>
parents:
4018
diff
changeset
|
577 #else |
4f1df4b90500
Added net_ipv6_mapped_ipv4_convert().
Timo Sirainen <tss@iki.fi>
parents:
4018
diff
changeset
|
578 return -1; |
4f1df4b90500
Added net_ipv6_mapped_ipv4_convert().
Timo Sirainen <tss@iki.fi>
parents:
4018
diff
changeset
|
579 #endif |
4f1df4b90500
Added net_ipv6_mapped_ipv4_convert().
Timo Sirainen <tss@iki.fi>
parents:
4018
diff
changeset
|
580 } |
4f1df4b90500
Added net_ipv6_mapped_ipv4_convert().
Timo Sirainen <tss@iki.fi>
parents:
4018
diff
changeset
|
581 |
0 | 582 int net_geterror(int fd) |
583 { | |
584 int data; | |
585 socklen_t len = sizeof(data); | |
586 | |
587 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &data, &len) == -1) | |
588 return -1; | |
589 | |
590 return data; | |
591 } | |
592 | |
593 const char *net_gethosterror(int error) | |
594 { | |
595 #ifdef HAVE_IPV6 | |
596 i_assert(error != 0); | |
597 | |
598 if (error == 1) { | |
599 /* getnameinfo() failed */ | |
600 return strerror(errno); | |
601 } | |
602 | |
603 return gai_strerror(error); | |
604 #else | |
605 switch (error) { | |
606 case HOST_NOT_FOUND: | |
607 return "Host not found"; | |
608 case NO_ADDRESS: | |
609 return "No IP address found for name"; | |
610 case NO_RECOVERY: | |
611 return "A non-recovable name server error occurred"; | |
612 case TRY_AGAIN: | |
613 return "A temporary error on an authoritative name server"; | |
614 } | |
615 | |
616 /* unknown error */ | |
617 return NULL; | |
618 #endif | |
619 } | |
620 | |
621 int net_hosterror_notfound(int error) | |
622 { | |
623 #ifdef HAVE_IPV6 | |
1915
79790750c349
importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
1871
diff
changeset
|
624 #ifdef EAI_NODATA /* NODATA is depricated */ |
0 | 625 return error != 1 && (error == EAI_NONAME || error == EAI_NODATA); |
626 #else | |
1915
79790750c349
importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
1871
diff
changeset
|
627 return error != 1 && (error == EAI_NONAME); |
79790750c349
importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
1871
diff
changeset
|
628 #endif |
79790750c349
importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
1871
diff
changeset
|
629 #else |
0 | 630 return error == HOST_NOT_FOUND || error == NO_ADDRESS; |
631 #endif | |
632 } | |
633 | |
2113
c5817f302aa6
net_gethostbyname() was broken with IPv6 enabled (wasn't really used with
Timo Sirainen <tss@iki.fi>
parents:
2098
diff
changeset
|
634 const char *net_getservbyport(unsigned short port) |
0 | 635 { |
636 struct servent *entry; | |
637 | |
349 | 638 entry = getservbyport(htons(port), "tcp"); |
0 | 639 return entry == NULL ? NULL : entry->s_name; |
640 } | |
641 | |
3863
55df57c028d4
Added "bool" type and changed all ints that were used as booleans to bool.
Timo Sirainen <tss@iki.fi>
parents:
3681
diff
changeset
|
642 bool is_ipv4_address(const char *addr) |
0 | 643 { |
1485
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
1268
diff
changeset
|
644 while (*addr != '\0') { |
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
1268
diff
changeset
|
645 if (*addr != '.' && !i_isdigit(*addr)) |
3863
55df57c028d4
Added "bool" type and changed all ints that were used as booleans to bool.
Timo Sirainen <tss@iki.fi>
parents:
3681
diff
changeset
|
646 return FALSE; |
1485
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
1268
diff
changeset
|
647 addr++; |
0 | 648 } |
649 | |
3863
55df57c028d4
Added "bool" type and changed all ints that were used as booleans to bool.
Timo Sirainen <tss@iki.fi>
parents:
3681
diff
changeset
|
650 return TRUE; |
0 | 651 } |
652 | |
3863
55df57c028d4
Added "bool" type and changed all ints that were used as booleans to bool.
Timo Sirainen <tss@iki.fi>
parents:
3681
diff
changeset
|
653 bool is_ipv6_address(const char *addr) |
0 | 654 { |
1485
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
1268
diff
changeset
|
655 while (*addr != '\0') { |
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
1268
diff
changeset
|
656 if (*addr != ':' && !i_isxdigit(*addr)) |
3863
55df57c028d4
Added "bool" type and changed all ints that were used as booleans to bool.
Timo Sirainen <tss@iki.fi>
parents:
3681
diff
changeset
|
657 return FALSE; |
1485
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
1268
diff
changeset
|
658 addr++; |
0 | 659 } |
660 | |
3863
55df57c028d4
Added "bool" type and changed all ints that were used as booleans to bool.
Timo Sirainen <tss@iki.fi>
parents:
3681
diff
changeset
|
661 return TRUE; |
0 | 662 } |