]> git.proxmox.com Git - mirror_frr.git/blob - lib/sockunion.c
Merge pull request #10389 from gromit1811/bugfix_9720_ecmp_inter_area
[mirror_frr.git] / lib / sockunion.c
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 *
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
19 */
20
21 #include <zebra.h>
22
23 #include "prefix.h"
24 #include "vty.h"
25 #include "sockunion.h"
26 #include "memory.h"
27 #include "log.h"
28 #include "jhash.h"
29 #include "lib_errors.h"
30 #include "printfrr.h"
31
32 DEFINE_MTYPE_STATIC(LIB, SOCKUNION, "Socket union");
33
34 const char *inet_sutop(const union sockunion *su, char *str)
35 {
36 switch (su->sa.sa_family) {
37 case AF_INET:
38 inet_ntop(AF_INET, &su->sin.sin_addr, str, INET_ADDRSTRLEN);
39 break;
40 case AF_INET6:
41 inet_ntop(AF_INET6, &su->sin6.sin6_addr, str, INET6_ADDRSTRLEN);
42 break;
43 }
44 return str;
45 }
46
47 int str2sockunion(const char *str, union sockunion *su)
48 {
49 int ret;
50
51 if (str == NULL)
52 return -1;
53
54 memset(su, 0, sizeof(union sockunion));
55
56 ret = inet_pton(AF_INET, str, &su->sin.sin_addr);
57 if (ret > 0) /* Valid IPv4 address format. */
58 {
59 su->sin.sin_family = AF_INET;
60 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
61 su->sin.sin_len = sizeof(struct sockaddr_in);
62 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
63 return 0;
64 }
65 ret = inet_pton(AF_INET6, str, &su->sin6.sin6_addr);
66 if (ret > 0) /* Valid IPv6 address format. */
67 {
68 su->sin6.sin6_family = AF_INET6;
69 #ifdef SIN6_LEN
70 su->sin6.sin6_len = sizeof(struct sockaddr_in6);
71 #endif /* SIN6_LEN */
72 return 0;
73 }
74 return -1;
75 }
76
77 const char *sockunion2str(const union sockunion *su, char *buf, size_t len)
78 {
79 switch (sockunion_family(su)) {
80 case AF_UNSPEC:
81 snprintf(buf, len, "(unspec)");
82 return buf;
83 case AF_INET:
84 return inet_ntop(AF_INET, &su->sin.sin_addr, buf, len);
85 case AF_INET6:
86 return inet_ntop(AF_INET6, &su->sin6.sin6_addr, buf, len);
87 }
88 snprintf(buf, len, "(af %d)", sockunion_family(su));
89 return buf;
90 }
91
92 union sockunion *sockunion_str2su(const char *str)
93 {
94 union sockunion *su = XCALLOC(MTYPE_SOCKUNION, sizeof(union sockunion));
95
96 if (!str2sockunion(str, su))
97 return su;
98
99 XFREE(MTYPE_SOCKUNION, su);
100 return NULL;
101 }
102
103 /* Convert IPv4 compatible IPv6 address to IPv4 address. */
104 static void sockunion_normalise_mapped(union sockunion *su)
105 {
106 struct sockaddr_in sin;
107
108 if (su->sa.sa_family == AF_INET6
109 && IN6_IS_ADDR_V4MAPPED(&su->sin6.sin6_addr)) {
110 memset(&sin, 0, sizeof(struct sockaddr_in));
111 sin.sin_family = AF_INET;
112 sin.sin_port = su->sin6.sin6_port;
113 memcpy(&sin.sin_addr, ((char *)&su->sin6.sin6_addr) + 12, 4);
114 memcpy(su, &sin, sizeof(struct sockaddr_in));
115 }
116 }
117
118 /* return sockunion structure : this function should be revised. */
119 static const char *sockunion_log(const union sockunion *su, char *buf,
120 size_t len)
121 {
122 switch (su->sa.sa_family) {
123 case AF_INET:
124 return inet_ntop(AF_INET, &su->sin.sin_addr, buf, len);
125
126 case AF_INET6:
127 return inet_ntop(AF_INET6, &(su->sin6.sin6_addr), buf, len);
128
129 default:
130 snprintf(buf, len, "af_unknown %d ", su->sa.sa_family);
131 return buf;
132 }
133 }
134
135 /* Return socket of sockunion. */
136 int sockunion_socket(const union sockunion *su)
137 {
138 int sock;
139
140 sock = socket(su->sa.sa_family, SOCK_STREAM, 0);
141 if (sock < 0) {
142 char buf[SU_ADDRSTRLEN];
143 flog_err(EC_LIB_SOCKET, "Can't make socket for %s : %s",
144 sockunion_log(su, buf, SU_ADDRSTRLEN),
145 safe_strerror(errno));
146 return -1;
147 }
148
149 return sock;
150 }
151
152 /* Return accepted new socket file descriptor. */
153 int sockunion_accept(int sock, union sockunion *su)
154 {
155 socklen_t len;
156 int client_sock;
157
158 len = sizeof(union sockunion);
159 client_sock = accept(sock, (struct sockaddr *)su, &len);
160
161 sockunion_normalise_mapped(su);
162 return client_sock;
163 }
164
165 /* Return sizeof union sockunion. */
166 int sockunion_sizeof(const union sockunion *su)
167 {
168 int ret;
169
170 ret = 0;
171 switch (su->sa.sa_family) {
172 case AF_INET:
173 ret = sizeof(struct sockaddr_in);
174 break;
175 case AF_INET6:
176 ret = sizeof(struct sockaddr_in6);
177 break;
178 }
179 return ret;
180 }
181
182 /* Performs a non-blocking connect(). */
183 enum connect_result sockunion_connect(int fd, const union sockunion *peersu,
184 unsigned short port, ifindex_t ifindex)
185 {
186 int ret;
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;
197 #ifdef KAME
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 }
202 #endif /* KAME */
203 break;
204 }
205
206 /* Call connect function. */
207 ret = connect(fd, (struct sockaddr *)&su, sockunion_sizeof(&su));
208
209 /* Immediate success */
210 if (ret == 0)
211 return connect_success;
212
213 /* If connect is in progress then return 1 else it's real error. */
214 if (ret < 0) {
215 if (errno != EINPROGRESS) {
216 char str[SU_ADDRSTRLEN];
217 zlog_info("can't connect to %s fd %d : %s",
218 sockunion_log(&su, str, sizeof(str)), fd,
219 safe_strerror(errno));
220 return connect_error;
221 }
222 }
223
224 return connect_in_progress;
225 }
226
227 /* Make socket from sockunion union. */
228 int sockunion_stream_socket(union sockunion *su)
229 {
230 int sock;
231
232 if (su->sa.sa_family == 0)
233 su->sa.sa_family = AF_INET_UNION;
234
235 sock = socket(su->sa.sa_family, SOCK_STREAM, 0);
236
237 if (sock < 0)
238 flog_err(EC_LIB_SOCKET,
239 "can't make socket sockunion_stream_socket");
240
241 return sock;
242 }
243
244 /* Bind socket to specified address. */
245 int sockunion_bind(int sock, union sockunion *su, unsigned short port,
246 union sockunion *su_addr)
247 {
248 int size = 0;
249 int ret;
250
251 if (su->sa.sa_family == AF_INET) {
252 size = sizeof(struct sockaddr_in);
253 su->sin.sin_port = htons(port);
254 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
255 su->sin.sin_len = size;
256 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
257 if (su_addr == NULL)
258 sockunion2ip(su) = htonl(INADDR_ANY);
259 } else if (su->sa.sa_family == AF_INET6) {
260 size = sizeof(struct sockaddr_in6);
261 su->sin6.sin6_port = htons(port);
262 #ifdef SIN6_LEN
263 su->sin6.sin6_len = size;
264 #endif /* SIN6_LEN */
265 if (su_addr == NULL) {
266 #ifdef LINUX_IPV6
267 memset(&su->sin6.sin6_addr, 0, sizeof(struct in6_addr));
268 #else
269 su->sin6.sin6_addr = in6addr_any;
270 #endif /* LINUX_IPV6 */
271 }
272 }
273
274 ret = bind(sock, (struct sockaddr *)su, size);
275 if (ret < 0) {
276 char buf[SU_ADDRSTRLEN];
277 flog_err(EC_LIB_SOCKET, "can't bind socket for %s : %s",
278 sockunion_log(su, buf, SU_ADDRSTRLEN),
279 safe_strerror(errno));
280 }
281
282 return ret;
283 }
284
285 int sockopt_reuseaddr(int sock)
286 {
287 int ret;
288 int on = 1;
289
290 ret = setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&on,
291 sizeof(on));
292 if (ret < 0) {
293 flog_err(EC_LIB_SOCKET,
294 "can't set sockopt SO_REUSEADDR to socket %d", sock);
295 return -1;
296 }
297 return 0;
298 }
299
300 #ifdef SO_REUSEPORT
301 int sockopt_reuseport(int sock)
302 {
303 int ret;
304 int on = 1;
305
306 ret = setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, (void *)&on,
307 sizeof(on));
308 if (ret < 0) {
309 flog_err(EC_LIB_SOCKET,
310 "can't set sockopt SO_REUSEPORT to socket %d", sock);
311 return -1;
312 }
313 return 0;
314 }
315 #else
316 int sockopt_reuseport(int sock)
317 {
318 return 0;
319 }
320 #endif /* 0 */
321
322 int sockopt_ttl(int family, int sock, int ttl)
323 {
324 int ret;
325
326 #ifdef IP_TTL
327 if (family == AF_INET) {
328 ret = setsockopt(sock, IPPROTO_IP, IP_TTL, (void *)&ttl,
329 sizeof(int));
330 if (ret < 0) {
331 flog_err(EC_LIB_SOCKET,
332 "can't set sockopt IP_TTL %d to socket %d",
333 ttl, sock);
334 return -1;
335 }
336 return 0;
337 }
338 #endif /* IP_TTL */
339 if (family == AF_INET6) {
340 ret = setsockopt(sock, IPPROTO_IPV6, IPV6_UNICAST_HOPS,
341 (void *)&ttl, sizeof(int));
342 if (ret < 0) {
343 flog_err(
344 EC_LIB_SOCKET,
345 "can't set sockopt IPV6_UNICAST_HOPS %d to socket %d",
346 ttl, sock);
347 return -1;
348 }
349 return 0;
350 }
351 return 0;
352 }
353
354 /*
355 * This function called setsockopt(.., TCP_CORK,...)
356 * Which on linux is a no-op since it is enabled by
357 * default and on BSD it uses TCP_NOPUSH to do
358 * the same thing( which it was not configured to
359 * use). This cleanup of the api occurred on 8/1/17
360 * I imagine if after more than 1 year of no-one
361 * complaining, and a major upgrade release we
362 * can deprecate and remove this function call
363 */
364 int sockopt_cork(int sock, int onoff)
365 {
366 return 0;
367 }
368
369 int sockopt_minttl(int family, int sock, int minttl)
370 {
371 #ifdef IP_MINTTL
372 if (family == AF_INET) {
373 int ret = setsockopt(sock, IPPROTO_IP, IP_MINTTL, &minttl,
374 sizeof(minttl));
375 if (ret < 0)
376 flog_err(
377 EC_LIB_SOCKET,
378 "can't set sockopt IP_MINTTL to %d on socket %d: %s",
379 minttl, sock, safe_strerror(errno));
380 return ret;
381 }
382 #endif /* IP_MINTTL */
383 #ifdef IPV6_MINHOPCOUNT
384 if (family == AF_INET6) {
385 int ret = setsockopt(sock, IPPROTO_IPV6, IPV6_MINHOPCOUNT,
386 &minttl, sizeof(minttl));
387 if (ret < 0)
388 flog_err(
389 EC_LIB_SOCKET,
390 "can't set sockopt IPV6_MINHOPCOUNT to %d on socket %d: %s",
391 minttl, sock, safe_strerror(errno));
392 return ret;
393 }
394 #endif
395
396 errno = EOPNOTSUPP;
397 return -1;
398 }
399
400 int sockopt_v6only(int family, int sock)
401 {
402 int ret, on = 1;
403
404 #ifdef IPV6_V6ONLY
405 if (family == AF_INET6) {
406 ret = setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, (void *)&on,
407 sizeof(int));
408 if (ret < 0) {
409 flog_err(EC_LIB_SOCKET,
410 "can't set sockopt IPV6_V6ONLY to socket %d",
411 sock);
412 return -1;
413 }
414 return 0;
415 }
416 #endif /* IPV6_V6ONLY */
417 return 0;
418 }
419
420 /* If same family and same prefix return 1. */
421 int sockunion_same(const union sockunion *su1, const union sockunion *su2)
422 {
423 int ret = 0;
424
425 if (su1->sa.sa_family != su2->sa.sa_family)
426 return 0;
427
428 switch (su1->sa.sa_family) {
429 case AF_INET:
430 ret = memcmp(&su1->sin.sin_addr, &su2->sin.sin_addr,
431 sizeof(struct in_addr));
432 break;
433 case AF_INET6:
434 ret = memcmp(&su1->sin6.sin6_addr, &su2->sin6.sin6_addr,
435 sizeof(struct in6_addr));
436 if ((ret == 0) && IN6_IS_ADDR_LINKLOCAL(&su1->sin6.sin6_addr)) {
437 /* compare interface indices */
438 if (su1->sin6.sin6_scope_id && su2->sin6.sin6_scope_id)
439 ret = (su1->sin6.sin6_scope_id
440 == su2->sin6.sin6_scope_id)
441 ? 0
442 : 1;
443 }
444 break;
445 }
446 if (ret == 0)
447 return 1;
448 else
449 return 0;
450 }
451
452 unsigned int sockunion_hash(const union sockunion *su)
453 {
454 switch (sockunion_family(su)) {
455 case AF_INET:
456 return jhash_1word(su->sin.sin_addr.s_addr, 0);
457 case AF_INET6:
458 return jhash2(su->sin6.sin6_addr.s6_addr32,
459 array_size(su->sin6.sin6_addr.s6_addr32), 0);
460 }
461 return 0;
462 }
463
464 size_t family2addrsize(int family)
465 {
466 switch (family) {
467 case AF_INET:
468 return sizeof(struct in_addr);
469 case AF_INET6:
470 return sizeof(struct in6_addr);
471 }
472 return 0;
473 }
474
475 size_t sockunion_get_addrlen(const union sockunion *su)
476 {
477 return family2addrsize(sockunion_family(su));
478 }
479
480 const uint8_t *sockunion_get_addr(const union sockunion *su)
481 {
482 switch (sockunion_family(su)) {
483 case AF_INET:
484 return (const uint8_t *)&su->sin.sin_addr.s_addr;
485 case AF_INET6:
486 return (const uint8_t *)&su->sin6.sin6_addr;
487 }
488 return NULL;
489 }
490
491 void sockunion_set(union sockunion *su, int family, const uint8_t *addr,
492 size_t bytes)
493 {
494 if (family2addrsize(family) != bytes)
495 return;
496
497 sockunion_family(su) = family;
498 switch (family) {
499 case AF_INET:
500 memcpy(&su->sin.sin_addr.s_addr, addr, bytes);
501 break;
502 case AF_INET6:
503 memcpy(&su->sin6.sin6_addr, addr, bytes);
504 break;
505 }
506 }
507
508 /* After TCP connection is established. Get local address and port. */
509 union sockunion *sockunion_getsockname(int fd)
510 {
511 int ret;
512 socklen_t len;
513 union {
514 struct sockaddr sa;
515 struct sockaddr_in sin;
516 struct sockaddr_in6 sin6;
517 char tmp_buffer[128];
518 } name;
519 union sockunion *su;
520
521 memset(&name, 0, sizeof(name));
522 len = sizeof(name);
523
524 ret = getsockname(fd, (struct sockaddr *)&name, &len);
525 if (ret < 0) {
526 flog_err(EC_LIB_SOCKET,
527 "Can't get local address and port by getsockname: %s",
528 safe_strerror(errno));
529 return NULL;
530 }
531
532 if (name.sa.sa_family == AF_INET) {
533 su = XCALLOC(MTYPE_SOCKUNION, sizeof(union sockunion));
534 memcpy(su, &name, sizeof(struct sockaddr_in));
535 return su;
536 }
537 if (name.sa.sa_family == AF_INET6) {
538 su = XCALLOC(MTYPE_SOCKUNION, sizeof(union sockunion));
539 memcpy(su, &name, sizeof(struct sockaddr_in6));
540 sockunion_normalise_mapped(su);
541 return su;
542 }
543 return NULL;
544 }
545
546 /* After TCP connection is established. Get remote address and port. */
547 union sockunion *sockunion_getpeername(int fd)
548 {
549 int ret;
550 socklen_t len;
551 union {
552 struct sockaddr sa;
553 struct sockaddr_in sin;
554 struct sockaddr_in6 sin6;
555 char tmp_buffer[128];
556 } name;
557 union sockunion *su;
558
559 memset(&name, 0, sizeof(name));
560 len = sizeof(name);
561 ret = getpeername(fd, (struct sockaddr *)&name, &len);
562 if (ret < 0) {
563 flog_err(EC_LIB_SOCKET, "Can't get remote address and port: %s",
564 safe_strerror(errno));
565 return NULL;
566 }
567
568 if (name.sa.sa_family == AF_INET) {
569 su = XCALLOC(MTYPE_SOCKUNION, sizeof(union sockunion));
570 memcpy(su, &name, sizeof(struct sockaddr_in));
571 return su;
572 }
573 if (name.sa.sa_family == AF_INET6) {
574 su = XCALLOC(MTYPE_SOCKUNION, sizeof(union sockunion));
575 memcpy(su, &name, sizeof(struct sockaddr_in6));
576 sockunion_normalise_mapped(su);
577 return su;
578 }
579 return NULL;
580 }
581
582 /* Print sockunion structure */
583 static void __attribute__((unused)) sockunion_print(const union sockunion *su)
584 {
585 if (su == NULL)
586 return;
587
588 switch (su->sa.sa_family) {
589 case AF_INET:
590 printf("%pI4\n", &su->sin.sin_addr);
591 break;
592 case AF_INET6:
593 printf("%pI6\n", &su->sin6.sin6_addr);
594 break;
595 #ifdef AF_LINK
596 case AF_LINK: {
597 struct sockaddr_dl *sdl;
598
599 sdl = (struct sockaddr_dl *)&(su->sa);
600 printf("link#%d\n", sdl->sdl_index);
601 } break;
602 #endif /* AF_LINK */
603 default:
604 printf("af_unknown %d\n", su->sa.sa_family);
605 break;
606 }
607 }
608
609 int in6addr_cmp(const struct in6_addr *addr1, const struct in6_addr *addr2)
610 {
611 unsigned int i;
612 const uint8_t *p1, *p2;
613
614 p1 = (const uint8_t *)addr1;
615 p2 = (const uint8_t *)addr2;
616
617 for (i = 0; i < sizeof(struct in6_addr); i++) {
618 if (p1[i] > p2[i])
619 return 1;
620 else if (p1[i] < p2[i])
621 return -1;
622 }
623 return 0;
624 }
625
626 int sockunion_cmp(const union sockunion *su1, const union sockunion *su2)
627 {
628 if (su1->sa.sa_family > su2->sa.sa_family)
629 return 1;
630 if (su1->sa.sa_family < su2->sa.sa_family)
631 return -1;
632
633 if (su1->sa.sa_family == AF_INET) {
634 if (ntohl(sockunion2ip(su1)) == ntohl(sockunion2ip(su2)))
635 return 0;
636 if (ntohl(sockunion2ip(su1)) > ntohl(sockunion2ip(su2)))
637 return 1;
638 else
639 return -1;
640 }
641 if (su1->sa.sa_family == AF_INET6)
642 return in6addr_cmp(&su1->sin6.sin6_addr, &su2->sin6.sin6_addr);
643 return 0;
644 }
645
646 /* Duplicate sockunion. */
647 union sockunion *sockunion_dup(const union sockunion *su)
648 {
649 union sockunion *dup =
650 XCALLOC(MTYPE_SOCKUNION, sizeof(union sockunion));
651 memcpy(dup, su, sizeof(union sockunion));
652 return dup;
653 }
654
655 void sockunion_free(union sockunion *su)
656 {
657 XFREE(MTYPE_SOCKUNION, su);
658 }
659
660 void sockunion_init(union sockunion *su)
661 {
662 memset(su, 0, sizeof(union sockunion));
663 }
664
665 printfrr_ext_autoreg_p("SU", printfrr_psu);
666 static ssize_t printfrr_psu(struct fbuf *buf, struct printfrr_eargs *ea,
667 const void *ptr)
668 {
669 const union sockunion *su = ptr;
670 bool include_port = false, include_scope = false;
671 bool endflags = false;
672 ssize_t ret = 0;
673 char cbuf[INET6_ADDRSTRLEN];
674
675 if (!su)
676 return bputs(buf, "(null)");
677
678 while (!endflags) {
679 switch (*ea->fmt) {
680 case 'p':
681 ea->fmt++;
682 include_port = true;
683 break;
684 case 's':
685 ea->fmt++;
686 include_scope = true;
687 break;
688 default:
689 endflags = true;
690 break;
691 }
692 }
693
694 switch (sockunion_family(su)) {
695 case AF_UNSPEC:
696 ret += bputs(buf, "(unspec)");
697 break;
698 case AF_INET:
699 inet_ntop(AF_INET, &su->sin.sin_addr, cbuf, sizeof(cbuf));
700 ret += bputs(buf, cbuf);
701 if (include_port)
702 ret += bprintfrr(buf, ":%d", ntohs(su->sin.sin_port));
703 break;
704 case AF_INET6:
705 if (include_port)
706 ret += bputch(buf, '[');
707 inet_ntop(AF_INET6, &su->sin6.sin6_addr, cbuf, sizeof(cbuf));
708 ret += bputs(buf, cbuf);
709 if (include_scope && su->sin6.sin6_scope_id)
710 ret += bprintfrr(buf, "%%%u",
711 (unsigned int)su->sin6.sin6_scope_id);
712 if (include_port)
713 ret += bprintfrr(buf, "]:%d",
714 ntohs(su->sin6.sin6_port));
715 break;
716 case AF_UNIX: {
717 int len;
718 #ifdef __linux__
719 if (su->sun.sun_path[0] == '\0' && su->sun.sun_path[1]) {
720 len = strnlen(su->sun.sun_path + 1,
721 sizeof(su->sun.sun_path) - 1);
722 ret += bprintfrr(buf, "@%*pSE", len,
723 su->sun.sun_path + 1);
724 break;
725 }
726 #endif
727 len = strnlen(su->sun.sun_path, sizeof(su->sun.sun_path));
728 ret += bprintfrr(buf, "%*pSE", len, su->sun.sun_path);
729 break;
730 }
731 default:
732 ret += bprintfrr(buf, "(af %d)", sockunion_family(su));
733 }
734
735 return ret;
736 }
737
738 int sockunion_is_null(const union sockunion *su)
739 {
740 unsigned char null_s6_addr[16] = {0};
741
742 switch (sockunion_family(su)) {
743 case AF_UNSPEC:
744 return 1;
745 case AF_INET:
746 return (su->sin.sin_addr.s_addr == 0);
747 case AF_INET6:
748 return !memcmp(su->sin6.sin6_addr.s6_addr, null_s6_addr,
749 sizeof(null_s6_addr));
750 default:
751 return 0;
752 }
753 }
754
755 printfrr_ext_autoreg_i("PF", printfrr_pf);
756 static ssize_t printfrr_pf(struct fbuf *buf, struct printfrr_eargs *ea,
757 uintmax_t val)
758 {
759 switch (val) {
760 case AF_INET:
761 return bputs(buf, "AF_INET");
762 case AF_INET6:
763 return bputs(buf, "AF_INET6");
764 case AF_UNIX:
765 return bputs(buf, "AF_UNIX");
766 #ifdef AF_PACKET
767 case AF_PACKET:
768 return bputs(buf, "AF_PACKET");
769 #endif
770 #ifdef AF_NETLINK
771 case AF_NETLINK:
772 return bputs(buf, "AF_NETLINK");
773 #endif
774 }
775 return bprintfrr(buf, "AF_(%ju)", val);
776 }
777
778 printfrr_ext_autoreg_i("SO", printfrr_so);
779 static ssize_t printfrr_so(struct fbuf *buf, struct printfrr_eargs *ea,
780 uintmax_t val)
781 {
782 switch (val) {
783 case SOCK_STREAM:
784 return bputs(buf, "SOCK_STREAM");
785 case SOCK_DGRAM:
786 return bputs(buf, "SOCK_DGRAM");
787 case SOCK_SEQPACKET:
788 return bputs(buf, "SOCK_SEQPACKET");
789 #ifdef SOCK_RAW
790 case SOCK_RAW:
791 return bputs(buf, "SOCK_RAW");
792 #endif
793 #ifdef SOCK_PACKET
794 case SOCK_PACKET:
795 return bputs(buf, "SOCK_PACKET");
796 #endif
797 }
798 return bprintfrr(buf, "SOCK_(%ju)", val);
799 }