]>
Commit | Line | Data |
---|---|---|
718e3744 | 1 | /* Socket union related function. |
2 | * Copyright (c) 1997, 98 Kunihiro Ishiguro | |
3 | * | |
4 | * This file is part of GNU Zebra. | |
5 | * | |
6 | * GNU Zebra is free software; you can redistribute it and/or modify it | |
7 | * under the terms of the GNU General Public License as published by the | |
8 | * Free Software Foundation; either version 2, or (at your option) any | |
9 | * later version. | |
10 | * | |
11 | * GNU Zebra is distributed in the hope that it will be useful, but | |
12 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | * General Public License for more details. | |
15 | * | |
896014f4 DL |
16 | * You should have received a copy of the GNU General Public License along |
17 | * with this program; see the file COPYING; if not, write to the Free Software | |
18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
718e3744 | 19 | */ |
20 | ||
21 | #include <zebra.h> | |
22 | ||
23 | #include "prefix.h" | |
24 | #include "vty.h" | |
25 | #include "sockunion.h" | |
26 | #include "memory.h" | |
718e3744 | 27 | #include "log.h" |
7cb5cdbb | 28 | #include "jhash.h" |
718e3744 | 29 | |
4a1ab8e4 DL |
30 | DEFINE_MTYPE_STATIC(LIB, SOCKUNION, "Socket union") |
31 | ||
d62a17ae | 32 | const char *inet_sutop(const union sockunion *su, char *str) |
33 | { | |
34 | switch (su->sa.sa_family) { | |
35 | case AF_INET: | |
36 | inet_ntop(AF_INET, &su->sin.sin_addr, str, INET_ADDRSTRLEN); | |
37 | break; | |
38 | case AF_INET6: | |
39 | inet_ntop(AF_INET6, &su->sin6.sin6_addr, str, INET6_ADDRSTRLEN); | |
40 | break; | |
41 | } | |
42 | return str; | |
718e3744 | 43 | } |
44 | ||
d62a17ae | 45 | int str2sockunion(const char *str, union sockunion *su) |
718e3744 | 46 | { |
d62a17ae | 47 | int ret; |
718e3744 | 48 | |
d1f92e45 | 49 | if (str == NULL) |
50 | return -1; | |
51 | ||
d62a17ae | 52 | memset(su, 0, sizeof(union sockunion)); |
718e3744 | 53 | |
d62a17ae | 54 | ret = inet_pton(AF_INET, str, &su->sin.sin_addr); |
55 | if (ret > 0) /* Valid IPv4 address format. */ | |
56 | { | |
57 | su->sin.sin_family = AF_INET; | |
6f0e3f6e | 58 | #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN |
d62a17ae | 59 | su->sin.sin_len = sizeof(struct sockaddr_in); |
6f0e3f6e | 60 | #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */ |
d62a17ae | 61 | return 0; |
62 | } | |
63 | ret = inet_pton(AF_INET6, str, &su->sin6.sin6_addr); | |
64 | if (ret > 0) /* Valid IPv6 address format. */ | |
65 | { | |
66 | su->sin6.sin6_family = AF_INET6; | |
718e3744 | 67 | #ifdef SIN6_LEN |
d62a17ae | 68 | su->sin6.sin6_len = sizeof(struct sockaddr_in6); |
718e3744 | 69 | #endif /* SIN6_LEN */ |
d62a17ae | 70 | return 0; |
71 | } | |
72 | return -1; | |
718e3744 | 73 | } |
74 | ||
d62a17ae | 75 | const char *sockunion2str(const union sockunion *su, char *buf, size_t len) |
718e3744 | 76 | { |
d62a17ae | 77 | switch (sockunion_family(su)) { |
78 | case AF_UNSPEC: | |
79 | snprintf(buf, len, "(unspec)"); | |
80 | return buf; | |
81 | case AF_INET: | |
82 | return inet_ntop(AF_INET, &su->sin.sin_addr, buf, len); | |
83 | case AF_INET6: | |
84 | return inet_ntop(AF_INET6, &su->sin6.sin6_addr, buf, len); | |
85 | } | |
86 | snprintf(buf, len, "(af %d)", sockunion_family(su)); | |
87 | return buf; | |
718e3744 | 88 | } |
89 | ||
d62a17ae | 90 | union sockunion *sockunion_str2su(const char *str) |
2fb2a455 | 91 | { |
d62a17ae | 92 | union sockunion *su = XCALLOC(MTYPE_SOCKUNION, sizeof(union sockunion)); |
93 | ||
94 | if (!str2sockunion(str, su)) | |
95 | return su; | |
96 | ||
97 | XFREE(MTYPE_SOCKUNION, su); | |
98 | return NULL; | |
2fb2a455 PJ |
99 | } |
100 | ||
1a7dcf42 | 101 | /* Convert IPv4 compatible IPv6 address to IPv4 address. */ |
d62a17ae | 102 | static void sockunion_normalise_mapped(union sockunion *su) |
103 | { | |
104 | struct sockaddr_in sin; | |
105 | ||
106 | if (su->sa.sa_family == AF_INET6 | |
107 | && IN6_IS_ADDR_V4MAPPED(&su->sin6.sin6_addr)) { | |
108 | memset(&sin, 0, sizeof(struct sockaddr_in)); | |
109 | sin.sin_family = AF_INET; | |
110 | sin.sin_port = su->sin6.sin6_port; | |
111 | memcpy(&sin.sin_addr, ((char *)&su->sin6.sin6_addr) + 12, 4); | |
112 | memcpy(su, &sin, sizeof(struct sockaddr_in)); | |
113 | } | |
1a7dcf42 PJ |
114 | } |
115 | ||
fc9707c3 | 116 | /* return sockunion structure : this function should be revised. */ |
d62a17ae | 117 | static const char *sockunion_log(const union sockunion *su, char *buf, |
118 | size_t len) | |
fc9707c3 | 119 | { |
d62a17ae | 120 | switch (su->sa.sa_family) { |
121 | case AF_INET: | |
122 | return inet_ntop(AF_INET, &su->sin.sin_addr, buf, len); | |
fc9707c3 | 123 | |
d62a17ae | 124 | case AF_INET6: |
125 | return inet_ntop(AF_INET6, &(su->sin6.sin6_addr), buf, len); | |
126 | break; | |
fc9707c3 | 127 | |
d62a17ae | 128 | default: |
129 | snprintf(buf, len, "af_unknown %d ", su->sa.sa_family); | |
130 | return buf; | |
131 | } | |
fc9707c3 DW |
132 | } |
133 | ||
718e3744 | 134 | /* Return socket of sockunion. */ |
d62a17ae | 135 | int sockunion_socket(const union sockunion *su) |
718e3744 | 136 | { |
d62a17ae | 137 | int sock; |
718e3744 | 138 | |
d62a17ae | 139 | sock = socket(su->sa.sa_family, SOCK_STREAM, 0); |
140 | if (sock < 0) { | |
141 | char buf[SU_ADDRSTRLEN]; | |
142 | zlog_warn("Can't make socket for %s : %s", | |
143 | sockunion_log(su, buf, SU_ADDRSTRLEN), | |
144 | safe_strerror(errno)); | |
145 | return -1; | |
146 | } | |
718e3744 | 147 | |
d62a17ae | 148 | return sock; |
718e3744 | 149 | } |
150 | ||
151 | /* Return accepted new socket file descriptor. */ | |
d62a17ae | 152 | int sockunion_accept(int sock, union sockunion *su) |
718e3744 | 153 | { |
d62a17ae | 154 | socklen_t len; |
155 | int client_sock; | |
718e3744 | 156 | |
d62a17ae | 157 | len = sizeof(union sockunion); |
158 | client_sock = accept(sock, (struct sockaddr *)su, &len); | |
159 | ||
160 | sockunion_normalise_mapped(su); | |
161 | return client_sock; | |
718e3744 | 162 | } |
163 | ||
164 | /* Return sizeof union sockunion. */ | |
d62a17ae | 165 | static int sockunion_sizeof(const union sockunion *su) |
166 | { | |
167 | int ret; | |
168 | ||
169 | ret = 0; | |
170 | switch (su->sa.sa_family) { | |
171 | case AF_INET: | |
172 | ret = sizeof(struct sockaddr_in); | |
173 | break; | |
174 | case AF_INET6: | |
175 | ret = sizeof(struct sockaddr_in6); | |
176 | break; | |
177 | } | |
178 | return ret; | |
718e3744 | 179 | } |
180 | ||
b11b5772 | 181 | /* Performs a non-blocking connect(). */ |
d62a17ae | 182 | enum connect_result sockunion_connect(int fd, const union sockunion *peersu, |
183 | unsigned short port, ifindex_t ifindex) | |
184 | { | |
185 | int ret; | |
d62a17ae | 186 | union sockunion su; |
187 | ||
188 | memcpy(&su, peersu, sizeof(union sockunion)); | |
189 | ||
190 | switch (su.sa.sa_family) { | |
191 | case AF_INET: | |
192 | su.sin.sin_port = port; | |
193 | break; | |
194 | case AF_INET6: | |
195 | su.sin6.sin6_port = port; | |
718e3744 | 196 | #ifdef KAME |
d62a17ae | 197 | if (IN6_IS_ADDR_LINKLOCAL(&su.sin6.sin6_addr) && ifindex) { |
198 | su.sin6.sin6_scope_id = ifindex; | |
199 | SET_IN6_LINKLOCAL_IFINDEX(su.sin6.sin6_addr, ifindex); | |
200 | } | |
718e3744 | 201 | #endif /* KAME */ |
d62a17ae | 202 | break; |
203 | } | |
204 | ||
d62a17ae | 205 | /* Call connect function. */ |
206 | ret = connect(fd, (struct sockaddr *)&su, sockunion_sizeof(&su)); | |
207 | ||
208 | /* Immediate success */ | |
b11b5772 | 209 | if (ret == 0) |
d62a17ae | 210 | return connect_success; |
d62a17ae | 211 | |
212 | /* If connect is in progress then return 1 else it's real error. */ | |
213 | if (ret < 0) { | |
214 | if (errno != EINPROGRESS) { | |
215 | char str[SU_ADDRSTRLEN]; | |
216 | zlog_info("can't connect to %s fd %d : %s", | |
217 | sockunion_log(&su, str, sizeof str), fd, | |
218 | safe_strerror(errno)); | |
219 | return connect_error; | |
220 | } | |
718e3744 | 221 | } |
718e3744 | 222 | |
d62a17ae | 223 | return connect_in_progress; |
718e3744 | 224 | } |
225 | ||
226 | /* Make socket from sockunion union. */ | |
d62a17ae | 227 | int sockunion_stream_socket(union sockunion *su) |
718e3744 | 228 | { |
d62a17ae | 229 | int sock; |
718e3744 | 230 | |
d62a17ae | 231 | if (su->sa.sa_family == 0) |
232 | su->sa.sa_family = AF_INET_UNION; | |
718e3744 | 233 | |
d62a17ae | 234 | sock = socket(su->sa.sa_family, SOCK_STREAM, 0); |
718e3744 | 235 | |
d62a17ae | 236 | if (sock < 0) |
237 | zlog_warn("can't make socket sockunion_stream_socket"); | |
718e3744 | 238 | |
d62a17ae | 239 | return sock; |
718e3744 | 240 | } |
241 | ||
242 | /* Bind socket to specified address. */ | |
d62a17ae | 243 | int sockunion_bind(int sock, union sockunion *su, unsigned short port, |
244 | union sockunion *su_addr) | |
718e3744 | 245 | { |
d62a17ae | 246 | int size = 0; |
247 | int ret; | |
718e3744 | 248 | |
d62a17ae | 249 | if (su->sa.sa_family == AF_INET) { |
250 | size = sizeof(struct sockaddr_in); | |
251 | su->sin.sin_port = htons(port); | |
6f0e3f6e | 252 | #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN |
d62a17ae | 253 | su->sin.sin_len = size; |
6f0e3f6e | 254 | #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */ |
d62a17ae | 255 | if (su_addr == NULL) |
256 | sockunion2ip(su) = htonl(INADDR_ANY); | |
257 | } else if (su->sa.sa_family == AF_INET6) { | |
258 | size = sizeof(struct sockaddr_in6); | |
259 | su->sin6.sin6_port = htons(port); | |
718e3744 | 260 | #ifdef SIN6_LEN |
d62a17ae | 261 | su->sin6.sin6_len = size; |
718e3744 | 262 | #endif /* SIN6_LEN */ |
d62a17ae | 263 | if (su_addr == NULL) { |
1cbb5dfc | 264 | #ifdef LINUX_IPV6 |
d62a17ae | 265 | memset(&su->sin6.sin6_addr, 0, sizeof(struct in6_addr)); |
718e3744 | 266 | #else |
d62a17ae | 267 | su->sin6.sin6_addr = in6addr_any; |
718e3744 | 268 | #endif /* LINUX_IPV6 */ |
d62a17ae | 269 | } |
718e3744 | 270 | } |
718e3744 | 271 | |
d62a17ae | 272 | ret = bind(sock, (struct sockaddr *)su, size); |
273 | if (ret < 0) { | |
274 | char buf[SU_ADDRSTRLEN]; | |
275 | zlog_warn("can't bind socket for %s : %s", | |
276 | sockunion_log(su, buf, SU_ADDRSTRLEN), | |
277 | safe_strerror(errno)); | |
278 | } | |
718e3744 | 279 | |
d62a17ae | 280 | return ret; |
718e3744 | 281 | } |
282 | ||
d62a17ae | 283 | int sockopt_reuseaddr(int sock) |
718e3744 | 284 | { |
d62a17ae | 285 | int ret; |
286 | int on = 1; | |
718e3744 | 287 | |
d62a17ae | 288 | ret = setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&on, |
289 | sizeof(on)); | |
290 | if (ret < 0) { | |
291 | zlog_warn("can't set sockopt SO_REUSEADDR to socket %d", sock); | |
292 | return -1; | |
293 | } | |
294 | return 0; | |
718e3744 | 295 | } |
296 | ||
297 | #ifdef SO_REUSEPORT | |
d62a17ae | 298 | int sockopt_reuseport(int sock) |
718e3744 | 299 | { |
d62a17ae | 300 | int ret; |
301 | int on = 1; | |
718e3744 | 302 | |
d62a17ae | 303 | ret = setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, (void *)&on, |
304 | sizeof(on)); | |
305 | if (ret < 0) { | |
306 | zlog_warn("can't set sockopt SO_REUSEPORT to socket %d", sock); | |
307 | return -1; | |
308 | } | |
309 | return 0; | |
718e3744 | 310 | } |
311 | #else | |
d62a17ae | 312 | int sockopt_reuseport(int sock) |
718e3744 | 313 | { |
d62a17ae | 314 | return 0; |
718e3744 | 315 | } |
316 | #endif /* 0 */ | |
317 | ||
d62a17ae | 318 | int sockopt_ttl(int family, int sock, int ttl) |
718e3744 | 319 | { |
d62a17ae | 320 | int ret; |
718e3744 | 321 | |
322 | #ifdef IP_TTL | |
d62a17ae | 323 | if (family == AF_INET) { |
324 | ret = setsockopt(sock, IPPROTO_IP, IP_TTL, (void *)&ttl, | |
325 | sizeof(int)); | |
326 | if (ret < 0) { | |
327 | zlog_warn("can't set sockopt IP_TTL %d to socket %d", | |
328 | ttl, sock); | |
329 | return -1; | |
330 | } | |
331 | return 0; | |
718e3744 | 332 | } |
718e3744 | 333 | #endif /* IP_TTL */ |
d62a17ae | 334 | if (family == AF_INET6) { |
335 | ret = setsockopt(sock, IPPROTO_IPV6, IPV6_UNICAST_HOPS, | |
336 | (void *)&ttl, sizeof(int)); | |
337 | if (ret < 0) { | |
338 | zlog_warn( | |
339 | "can't set sockopt IPV6_UNICAST_HOPS %d to socket %d", | |
340 | ttl, sock); | |
341 | return -1; | |
342 | } | |
343 | return 0; | |
718e3744 | 344 | } |
d62a17ae | 345 | return 0; |
718e3744 | 346 | } |
347 | ||
c62232b4 DS |
348 | /* |
349 | * This function called setsockopt(.., TCP_CORK,...) | |
350 | * Which on linux is a no-op since it is enabled by | |
351 | * default and on BSD it uses TCP_NOPUSH to do | |
352 | * the same thing( which it was not configured to | |
353 | * use). This cleanup of the api occured on 8/1/17 | |
354 | * I imagine if after more than 1 year of no-one | |
355 | * complaining, and a major upgrade release we | |
356 | * can deprecate and remove this function call | |
357 | */ | |
d62a17ae | 358 | int sockopt_cork(int sock, int onoff) |
58192df7 | 359 | { |
d62a17ae | 360 | return 0; |
ed40466a DS |
361 | } |
362 | ||
363 | int sockopt_mark_default(int sock, int mark, struct zebra_privs_t *cap) | |
364 | { | |
365 | #ifdef SO_MARK | |
d62a17ae | 366 | int ret; |
ed40466a | 367 | |
d62a17ae | 368 | if (cap->change(ZPRIVS_RAISE)) |
369 | zlog_err("routing_socket: Can't raise privileges"); | |
ed40466a | 370 | |
d62a17ae | 371 | ret = setsockopt(sock, SOL_SOCKET, SO_MARK, &mark, sizeof(mark)); |
ed40466a | 372 | |
d62a17ae | 373 | if (cap->change(ZPRIVS_LOWER)) |
374 | zlog_err("routing_socket: Can't lower privileges"); | |
ed40466a | 375 | |
d62a17ae | 376 | return ret; |
ed40466a | 377 | #else |
d62a17ae | 378 | return 0; |
58192df7 SH |
379 | #endif |
380 | } | |
381 | ||
d62a17ae | 382 | int sockopt_minttl(int family, int sock, int minttl) |
fa411a21 | 383 | { |
89b6d1f8 | 384 | #ifdef IP_MINTTL |
d62a17ae | 385 | if (family == AF_INET) { |
386 | int ret = setsockopt(sock, IPPROTO_IP, IP_MINTTL, &minttl, | |
387 | sizeof(minttl)); | |
388 | if (ret < 0) | |
389 | zlog_warn( | |
390 | "can't set sockopt IP_MINTTL to %d on socket %d: %s", | |
391 | minttl, sock, safe_strerror(errno)); | |
392 | return ret; | |
393 | } | |
d876bdf4 | 394 | #endif /* IP_MINTTL */ |
d8dc5257 | 395 | #ifdef IPV6_MINHOPCOUNT |
d62a17ae | 396 | if (family == AF_INET6) { |
397 | int ret = setsockopt(sock, IPPROTO_IPV6, IPV6_MINHOPCOUNT, | |
398 | &minttl, sizeof(minttl)); | |
399 | if (ret < 0) | |
400 | zlog_warn( | |
401 | "can't set sockopt IPV6_MINHOPCOUNT to %d on socket %d: %s", | |
402 | minttl, sock, safe_strerror(errno)); | |
403 | return ret; | |
404 | } | |
d876bdf4 | 405 | #endif |
fa411a21 | 406 | |
d62a17ae | 407 | errno = EOPNOTSUPP; |
408 | return -1; | |
fa411a21 NH |
409 | } |
410 | ||
d62a17ae | 411 | int sockopt_v6only(int family, int sock) |
ca051269 | 412 | { |
d62a17ae | 413 | int ret, on = 1; |
ca051269 | 414 | |
ca051269 | 415 | #ifdef IPV6_V6ONLY |
d62a17ae | 416 | if (family == AF_INET6) { |
417 | ret = setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, (void *)&on, | |
418 | sizeof(int)); | |
419 | if (ret < 0) { | |
420 | zlog_warn( | |
421 | "can't set sockopt IPV6_V6ONLY " | |
422 | "to socket %d", | |
423 | sock); | |
424 | return -1; | |
425 | } | |
426 | return 0; | |
ca051269 | 427 | } |
ca051269 | 428 | #endif /* IPV6_V6ONLY */ |
d62a17ae | 429 | return 0; |
ca051269 DL |
430 | } |
431 | ||
718e3744 | 432 | /* If same family and same prefix return 1. */ |
d62a17ae | 433 | int sockunion_same(const union sockunion *su1, const union sockunion *su2) |
434 | { | |
435 | int ret = 0; | |
436 | ||
437 | if (su1->sa.sa_family != su2->sa.sa_family) | |
438 | return 0; | |
439 | ||
440 | switch (su1->sa.sa_family) { | |
441 | case AF_INET: | |
442 | ret = memcmp(&su1->sin.sin_addr, &su2->sin.sin_addr, | |
443 | sizeof(struct in_addr)); | |
444 | break; | |
445 | case AF_INET6: | |
446 | ret = memcmp(&su1->sin6.sin6_addr, &su2->sin6.sin6_addr, | |
447 | sizeof(struct in6_addr)); | |
448 | if ((ret == 0) && IN6_IS_ADDR_LINKLOCAL(&su1->sin6.sin6_addr)) { | |
449 | /* compare interface indices */ | |
450 | if (su1->sin6.sin6_scope_id && su2->sin6.sin6_scope_id) | |
451 | ret = (su1->sin6.sin6_scope_id | |
452 | == su2->sin6.sin6_scope_id) | |
453 | ? 0 | |
454 | : 1; | |
455 | } | |
456 | break; | |
f2345335 | 457 | } |
d62a17ae | 458 | if (ret == 0) |
459 | return 1; | |
460 | else | |
461 | return 0; | |
718e3744 | 462 | } |
463 | ||
d62a17ae | 464 | unsigned int sockunion_hash(const union sockunion *su) |
7cb5cdbb | 465 | { |
d62a17ae | 466 | switch (sockunion_family(su)) { |
467 | case AF_INET: | |
468 | return jhash_1word(su->sin.sin_addr.s_addr, 0); | |
469 | case AF_INET6: | |
470 | return jhash2(su->sin6.sin6_addr.s6_addr32, | |
471 | ZEBRA_NUM_OF(su->sin6.sin6_addr.s6_addr32), 0); | |
472 | } | |
473 | return 0; | |
7cb5cdbb TT |
474 | } |
475 | ||
d62a17ae | 476 | size_t family2addrsize(int family) |
95e0999c | 477 | { |
d62a17ae | 478 | switch (family) { |
479 | case AF_INET: | |
480 | return sizeof(struct in_addr); | |
481 | case AF_INET6: | |
482 | return sizeof(struct in6_addr); | |
483 | } | |
484 | return 0; | |
95e0999c TT |
485 | } |
486 | ||
d62a17ae | 487 | size_t sockunion_get_addrlen(const union sockunion *su) |
95e0999c | 488 | { |
d62a17ae | 489 | return family2addrsize(sockunion_family(su)); |
95e0999c TT |
490 | } |
491 | ||
d7c0a89a | 492 | const uint8_t *sockunion_get_addr(const union sockunion *su) |
95e0999c | 493 | { |
d62a17ae | 494 | switch (sockunion_family(su)) { |
495 | case AF_INET: | |
d7c0a89a | 496 | return (const uint8_t *)&su->sin.sin_addr.s_addr; |
d62a17ae | 497 | case AF_INET6: |
d7c0a89a | 498 | return (const uint8_t *)&su->sin6.sin6_addr; |
d62a17ae | 499 | } |
500 | return NULL; | |
95e0999c TT |
501 | } |
502 | ||
d7c0a89a | 503 | void sockunion_set(union sockunion *su, int family, const uint8_t *addr, |
d62a17ae | 504 | size_t bytes) |
95e0999c | 505 | { |
d62a17ae | 506 | if (family2addrsize(family) != bytes) |
507 | return; | |
95e0999c | 508 | |
d62a17ae | 509 | sockunion_family(su) = family; |
510 | switch (family) { | |
511 | case AF_INET: | |
512 | memcpy(&su->sin.sin_addr.s_addr, addr, bytes); | |
513 | break; | |
514 | case AF_INET6: | |
515 | memcpy(&su->sin6.sin6_addr, addr, bytes); | |
516 | break; | |
517 | } | |
95e0999c TT |
518 | } |
519 | ||
718e3744 | 520 | /* After TCP connection is established. Get local address and port. */ |
d62a17ae | 521 | union sockunion *sockunion_getsockname(int fd) |
522 | { | |
523 | int ret; | |
524 | socklen_t len; | |
525 | union { | |
526 | struct sockaddr sa; | |
527 | struct sockaddr_in sin; | |
528 | struct sockaddr_in6 sin6; | |
529 | char tmp_buffer[128]; | |
530 | } name; | |
531 | union sockunion *su; | |
532 | ||
533 | memset(&name, 0, sizeof name); | |
534 | len = sizeof name; | |
535 | ||
536 | ret = getsockname(fd, (struct sockaddr *)&name, &len); | |
537 | if (ret < 0) { | |
538 | zlog_warn("Can't get local address and port by getsockname: %s", | |
539 | safe_strerror(errno)); | |
540 | return NULL; | |
541 | } | |
542 | ||
543 | if (name.sa.sa_family == AF_INET) { | |
544 | su = XCALLOC(MTYPE_SOCKUNION, sizeof(union sockunion)); | |
545 | memcpy(su, &name, sizeof(struct sockaddr_in)); | |
546 | return su; | |
547 | } | |
548 | if (name.sa.sa_family == AF_INET6) { | |
549 | su = XCALLOC(MTYPE_SOCKUNION, sizeof(union sockunion)); | |
550 | memcpy(su, &name, sizeof(struct sockaddr_in6)); | |
551 | sockunion_normalise_mapped(su); | |
552 | return su; | |
553 | } | |
554 | return NULL; | |
718e3744 | 555 | } |
556 | ||
557 | /* After TCP connection is established. Get remote address and port. */ | |
d62a17ae | 558 | union sockunion *sockunion_getpeername(int fd) |
559 | { | |
560 | int ret; | |
561 | socklen_t len; | |
562 | union { | |
563 | struct sockaddr sa; | |
564 | struct sockaddr_in sin; | |
565 | struct sockaddr_in6 sin6; | |
566 | char tmp_buffer[128]; | |
567 | } name; | |
568 | union sockunion *su; | |
569 | ||
570 | memset(&name, 0, sizeof name); | |
571 | len = sizeof name; | |
572 | ret = getpeername(fd, (struct sockaddr *)&name, &len); | |
573 | if (ret < 0) { | |
574 | zlog_warn("Can't get remote address and port: %s", | |
575 | safe_strerror(errno)); | |
576 | return NULL; | |
577 | } | |
578 | ||
579 | if (name.sa.sa_family == AF_INET) { | |
580 | su = XCALLOC(MTYPE_SOCKUNION, sizeof(union sockunion)); | |
581 | memcpy(su, &name, sizeof(struct sockaddr_in)); | |
582 | return su; | |
583 | } | |
584 | if (name.sa.sa_family == AF_INET6) { | |
585 | su = XCALLOC(MTYPE_SOCKUNION, sizeof(union sockunion)); | |
586 | memcpy(su, &name, sizeof(struct sockaddr_in6)); | |
587 | sockunion_normalise_mapped(su); | |
588 | return su; | |
589 | } | |
590 | return NULL; | |
718e3744 | 591 | } |
592 | ||
593 | /* Print sockunion structure */ | |
d62a17ae | 594 | static void __attribute__((unused)) sockunion_print(const union sockunion *su) |
595 | { | |
596 | if (su == NULL) | |
597 | return; | |
598 | ||
599 | switch (su->sa.sa_family) { | |
600 | case AF_INET: | |
601 | printf("%s\n", inet_ntoa(su->sin.sin_addr)); | |
602 | break; | |
603 | case AF_INET6: { | |
604 | char buf[SU_ADDRSTRLEN]; | |
605 | ||
606 | printf("%s\n", inet_ntop(AF_INET6, &(su->sin6.sin6_addr), buf, | |
607 | sizeof(buf))); | |
608 | } break; | |
718e3744 | 609 | |
610 | #ifdef AF_LINK | |
d62a17ae | 611 | case AF_LINK: { |
612 | struct sockaddr_dl *sdl; | |
613 | ||
614 | sdl = (struct sockaddr_dl *)&(su->sa); | |
615 | printf("link#%d\n", sdl->sdl_index); | |
616 | } break; | |
718e3744 | 617 | #endif /* AF_LINK */ |
d62a17ae | 618 | default: |
619 | printf("af_unknown %d\n", su->sa.sa_family); | |
620 | break; | |
621 | } | |
718e3744 | 622 | } |
623 | ||
d62a17ae | 624 | static int in6addr_cmp(const struct in6_addr *addr1, |
625 | const struct in6_addr *addr2) | |
718e3744 | 626 | { |
d62a17ae | 627 | unsigned int i; |
d7c0a89a | 628 | const uint8_t *p1, *p2; |
718e3744 | 629 | |
d7c0a89a QY |
630 | p1 = (const uint8_t *)addr1; |
631 | p2 = (const uint8_t *)addr2; | |
718e3744 | 632 | |
d62a17ae | 633 | for (i = 0; i < sizeof(struct in6_addr); i++) { |
634 | if (p1[i] > p2[i]) | |
635 | return 1; | |
636 | else if (p1[i] < p2[i]) | |
637 | return -1; | |
638 | } | |
639 | return 0; | |
718e3744 | 640 | } |
718e3744 | 641 | |
d62a17ae | 642 | int sockunion_cmp(const union sockunion *su1, const union sockunion *su2) |
718e3744 | 643 | { |
d62a17ae | 644 | if (su1->sa.sa_family > su2->sa.sa_family) |
645 | return 1; | |
646 | if (su1->sa.sa_family < su2->sa.sa_family) | |
647 | return -1; | |
718e3744 | 648 | |
d62a17ae | 649 | if (su1->sa.sa_family == AF_INET) { |
650 | if (ntohl(sockunion2ip(su1)) == ntohl(sockunion2ip(su2))) | |
651 | return 0; | |
652 | if (ntohl(sockunion2ip(su1)) > ntohl(sockunion2ip(su2))) | |
653 | return 1; | |
654 | else | |
655 | return -1; | |
656 | } | |
657 | if (su1->sa.sa_family == AF_INET6) | |
658 | return in6addr_cmp(&su1->sin6.sin6_addr, &su2->sin6.sin6_addr); | |
718e3744 | 659 | return 0; |
718e3744 | 660 | } |
661 | ||
662 | /* Duplicate sockunion. */ | |
d62a17ae | 663 | union sockunion *sockunion_dup(const union sockunion *su) |
718e3744 | 664 | { |
d62a17ae | 665 | union sockunion *dup = |
666 | XCALLOC(MTYPE_SOCKUNION, sizeof(union sockunion)); | |
667 | memcpy(dup, su, sizeof(union sockunion)); | |
668 | return dup; | |
718e3744 | 669 | } |
670 | ||
d62a17ae | 671 | void sockunion_free(union sockunion *su) |
718e3744 | 672 | { |
d62a17ae | 673 | XFREE(MTYPE_SOCKUNION, su); |
718e3744 | 674 | } |
dd793e4a | 675 | |
d62a17ae | 676 | void sockunion_init(union sockunion *su) |
dd793e4a | 677 | { |
d62a17ae | 678 | memset(su, 0, sizeof(union sockunion)); |
dd793e4a | 679 | } |