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