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