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