]> git.proxmox.com Git - mirror_frr.git/blob - lib/sockunion.c
Merge pull request #10724 from opensourcerouting/lib-rotate-logs
[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 int sockopt_minttl(int family, int sock, int minttl)
355 {
356 #ifdef IP_MINTTL
357 if (family == AF_INET) {
358 int ret = setsockopt(sock, IPPROTO_IP, IP_MINTTL, &minttl,
359 sizeof(minttl));
360 if (ret < 0)
361 flog_err(
362 EC_LIB_SOCKET,
363 "can't set sockopt IP_MINTTL to %d on socket %d: %s",
364 minttl, sock, safe_strerror(errno));
365 return ret;
366 }
367 #endif /* IP_MINTTL */
368 #ifdef IPV6_MINHOPCOUNT
369 if (family == AF_INET6) {
370 int ret = setsockopt(sock, IPPROTO_IPV6, IPV6_MINHOPCOUNT,
371 &minttl, sizeof(minttl));
372 if (ret < 0)
373 flog_err(
374 EC_LIB_SOCKET,
375 "can't set sockopt IPV6_MINHOPCOUNT to %d on socket %d: %s",
376 minttl, sock, safe_strerror(errno));
377 return ret;
378 }
379 #endif
380
381 errno = EOPNOTSUPP;
382 return -1;
383 }
384
385 int sockopt_v6only(int family, int sock)
386 {
387 int ret, on = 1;
388
389 #ifdef IPV6_V6ONLY
390 if (family == AF_INET6) {
391 ret = setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, (void *)&on,
392 sizeof(int));
393 if (ret < 0) {
394 flog_err(EC_LIB_SOCKET,
395 "can't set sockopt IPV6_V6ONLY to socket %d",
396 sock);
397 return -1;
398 }
399 return 0;
400 }
401 #endif /* IPV6_V6ONLY */
402 return 0;
403 }
404
405 /* If same family and same prefix return 1. */
406 int sockunion_same(const union sockunion *su1, const union sockunion *su2)
407 {
408 int ret = 0;
409
410 if (su1->sa.sa_family != su2->sa.sa_family)
411 return 0;
412
413 switch (su1->sa.sa_family) {
414 case AF_INET:
415 ret = memcmp(&su1->sin.sin_addr, &su2->sin.sin_addr,
416 sizeof(struct in_addr));
417 break;
418 case AF_INET6:
419 ret = memcmp(&su1->sin6.sin6_addr, &su2->sin6.sin6_addr,
420 sizeof(struct in6_addr));
421 if ((ret == 0) && IN6_IS_ADDR_LINKLOCAL(&su1->sin6.sin6_addr)) {
422 /* compare interface indices */
423 if (su1->sin6.sin6_scope_id && su2->sin6.sin6_scope_id)
424 ret = (su1->sin6.sin6_scope_id
425 == su2->sin6.sin6_scope_id)
426 ? 0
427 : 1;
428 }
429 break;
430 }
431 if (ret == 0)
432 return 1;
433 else
434 return 0;
435 }
436
437 unsigned int sockunion_hash(const union sockunion *su)
438 {
439 switch (sockunion_family(su)) {
440 case AF_INET:
441 return jhash_1word(su->sin.sin_addr.s_addr, 0);
442 case AF_INET6:
443 return jhash2(su->sin6.sin6_addr.s6_addr32,
444 array_size(su->sin6.sin6_addr.s6_addr32), 0);
445 }
446 return 0;
447 }
448
449 size_t family2addrsize(int family)
450 {
451 switch (family) {
452 case AF_INET:
453 return sizeof(struct in_addr);
454 case AF_INET6:
455 return sizeof(struct in6_addr);
456 }
457 return 0;
458 }
459
460 size_t sockunion_get_addrlen(const union sockunion *su)
461 {
462 return family2addrsize(sockunion_family(su));
463 }
464
465 const uint8_t *sockunion_get_addr(const union sockunion *su)
466 {
467 switch (sockunion_family(su)) {
468 case AF_INET:
469 return (const uint8_t *)&su->sin.sin_addr.s_addr;
470 case AF_INET6:
471 return (const uint8_t *)&su->sin6.sin6_addr;
472 }
473 return NULL;
474 }
475
476 void sockunion_set(union sockunion *su, int family, const uint8_t *addr,
477 size_t bytes)
478 {
479 if (family2addrsize(family) != bytes)
480 return;
481
482 sockunion_family(su) = family;
483 switch (family) {
484 case AF_INET:
485 memcpy(&su->sin.sin_addr.s_addr, addr, bytes);
486 break;
487 case AF_INET6:
488 memcpy(&su->sin6.sin6_addr, addr, bytes);
489 break;
490 }
491 }
492
493 /* After TCP connection is established. Get local address and port. */
494 union sockunion *sockunion_getsockname(int fd)
495 {
496 int ret;
497 socklen_t len;
498 union {
499 struct sockaddr sa;
500 struct sockaddr_in sin;
501 struct sockaddr_in6 sin6;
502 char tmp_buffer[128];
503 } name;
504 union sockunion *su;
505
506 memset(&name, 0, sizeof(name));
507 len = sizeof(name);
508
509 ret = getsockname(fd, (struct sockaddr *)&name, &len);
510 if (ret < 0) {
511 flog_err(EC_LIB_SOCKET,
512 "Can't get local address and port by getsockname: %s",
513 safe_strerror(errno));
514 return NULL;
515 }
516
517 if (name.sa.sa_family == AF_INET) {
518 su = XCALLOC(MTYPE_SOCKUNION, sizeof(union sockunion));
519 memcpy(su, &name, sizeof(struct sockaddr_in));
520 return su;
521 }
522 if (name.sa.sa_family == AF_INET6) {
523 su = XCALLOC(MTYPE_SOCKUNION, sizeof(union sockunion));
524 memcpy(su, &name, sizeof(struct sockaddr_in6));
525 sockunion_normalise_mapped(su);
526 return su;
527 }
528 return NULL;
529 }
530
531 /* After TCP connection is established. Get remote address and port. */
532 union sockunion *sockunion_getpeername(int fd)
533 {
534 int ret;
535 socklen_t len;
536 union {
537 struct sockaddr sa;
538 struct sockaddr_in sin;
539 struct sockaddr_in6 sin6;
540 char tmp_buffer[128];
541 } name;
542 union sockunion *su;
543
544 memset(&name, 0, sizeof(name));
545 len = sizeof(name);
546 ret = getpeername(fd, (struct sockaddr *)&name, &len);
547 if (ret < 0) {
548 flog_err(EC_LIB_SOCKET, "Can't get remote address and port: %s",
549 safe_strerror(errno));
550 return NULL;
551 }
552
553 if (name.sa.sa_family == AF_INET) {
554 su = XCALLOC(MTYPE_SOCKUNION, sizeof(union sockunion));
555 memcpy(su, &name, sizeof(struct sockaddr_in));
556 return su;
557 }
558 if (name.sa.sa_family == AF_INET6) {
559 su = XCALLOC(MTYPE_SOCKUNION, sizeof(union sockunion));
560 memcpy(su, &name, sizeof(struct sockaddr_in6));
561 sockunion_normalise_mapped(su);
562 return su;
563 }
564 return NULL;
565 }
566
567 /* Print sockunion structure */
568 static void __attribute__((unused)) sockunion_print(const union sockunion *su)
569 {
570 if (su == NULL)
571 return;
572
573 switch (su->sa.sa_family) {
574 case AF_INET:
575 printf("%pI4\n", &su->sin.sin_addr);
576 break;
577 case AF_INET6:
578 printf("%pI6\n", &su->sin6.sin6_addr);
579 break;
580 #ifdef AF_LINK
581 case AF_LINK: {
582 struct sockaddr_dl *sdl;
583
584 sdl = (struct sockaddr_dl *)&(su->sa);
585 printf("link#%d\n", sdl->sdl_index);
586 } break;
587 #endif /* AF_LINK */
588 default:
589 printf("af_unknown %d\n", su->sa.sa_family);
590 break;
591 }
592 }
593
594 int in6addr_cmp(const struct in6_addr *addr1, const struct in6_addr *addr2)
595 {
596 unsigned int i;
597 const uint8_t *p1, *p2;
598
599 p1 = (const uint8_t *)addr1;
600 p2 = (const uint8_t *)addr2;
601
602 for (i = 0; i < sizeof(struct in6_addr); i++) {
603 if (p1[i] > p2[i])
604 return 1;
605 else if (p1[i] < p2[i])
606 return -1;
607 }
608 return 0;
609 }
610
611 int sockunion_cmp(const union sockunion *su1, const union sockunion *su2)
612 {
613 if (su1->sa.sa_family > su2->sa.sa_family)
614 return 1;
615 if (su1->sa.sa_family < su2->sa.sa_family)
616 return -1;
617
618 if (su1->sa.sa_family == AF_INET) {
619 if (ntohl(sockunion2ip(su1)) == ntohl(sockunion2ip(su2)))
620 return 0;
621 if (ntohl(sockunion2ip(su1)) > ntohl(sockunion2ip(su2)))
622 return 1;
623 else
624 return -1;
625 }
626 if (su1->sa.sa_family == AF_INET6)
627 return in6addr_cmp(&su1->sin6.sin6_addr, &su2->sin6.sin6_addr);
628 return 0;
629 }
630
631 /* Duplicate sockunion. */
632 union sockunion *sockunion_dup(const union sockunion *su)
633 {
634 union sockunion *dup =
635 XCALLOC(MTYPE_SOCKUNION, sizeof(union sockunion));
636 memcpy(dup, su, sizeof(union sockunion));
637 return dup;
638 }
639
640 void sockunion_free(union sockunion *su)
641 {
642 XFREE(MTYPE_SOCKUNION, su);
643 }
644
645 void sockunion_init(union sockunion *su)
646 {
647 memset(su, 0, sizeof(union sockunion));
648 }
649
650 printfrr_ext_autoreg_p("SU", printfrr_psu);
651 static ssize_t printfrr_psu(struct fbuf *buf, struct printfrr_eargs *ea,
652 const void *ptr)
653 {
654 const union sockunion *su = ptr;
655 bool include_port = false, include_scope = false;
656 bool endflags = false;
657 ssize_t ret = 0;
658 char cbuf[INET6_ADDRSTRLEN];
659
660 if (!su)
661 return bputs(buf, "(null)");
662
663 while (!endflags) {
664 switch (*ea->fmt) {
665 case 'p':
666 ea->fmt++;
667 include_port = true;
668 break;
669 case 's':
670 ea->fmt++;
671 include_scope = true;
672 break;
673 default:
674 endflags = true;
675 break;
676 }
677 }
678
679 switch (sockunion_family(su)) {
680 case AF_UNSPEC:
681 ret += bputs(buf, "(unspec)");
682 break;
683 case AF_INET:
684 inet_ntop(AF_INET, &su->sin.sin_addr, cbuf, sizeof(cbuf));
685 ret += bputs(buf, cbuf);
686 if (include_port)
687 ret += bprintfrr(buf, ":%d", ntohs(su->sin.sin_port));
688 break;
689 case AF_INET6:
690 if (include_port)
691 ret += bputch(buf, '[');
692 inet_ntop(AF_INET6, &su->sin6.sin6_addr, cbuf, sizeof(cbuf));
693 ret += bputs(buf, cbuf);
694 if (include_scope && su->sin6.sin6_scope_id)
695 ret += bprintfrr(buf, "%%%u",
696 (unsigned int)su->sin6.sin6_scope_id);
697 if (include_port)
698 ret += bprintfrr(buf, "]:%d",
699 ntohs(su->sin6.sin6_port));
700 break;
701 case AF_UNIX: {
702 int len;
703 #ifdef __linux__
704 if (su->sun.sun_path[0] == '\0' && su->sun.sun_path[1]) {
705 len = strnlen(su->sun.sun_path + 1,
706 sizeof(su->sun.sun_path) - 1);
707 ret += bprintfrr(buf, "@%*pSE", len,
708 su->sun.sun_path + 1);
709 break;
710 }
711 #endif
712 len = strnlen(su->sun.sun_path, sizeof(su->sun.sun_path));
713 ret += bprintfrr(buf, "%*pSE", len, su->sun.sun_path);
714 break;
715 }
716 default:
717 ret += bprintfrr(buf, "(af %d)", sockunion_family(su));
718 }
719
720 return ret;
721 }
722
723 int sockunion_is_null(const union sockunion *su)
724 {
725 unsigned char null_s6_addr[16] = {0};
726
727 switch (sockunion_family(su)) {
728 case AF_UNSPEC:
729 return 1;
730 case AF_INET:
731 return (su->sin.sin_addr.s_addr == 0);
732 case AF_INET6:
733 return !memcmp(su->sin6.sin6_addr.s6_addr, null_s6_addr,
734 sizeof(null_s6_addr));
735 default:
736 return 0;
737 }
738 }
739
740 printfrr_ext_autoreg_i("PF", printfrr_pf);
741 static ssize_t printfrr_pf(struct fbuf *buf, struct printfrr_eargs *ea,
742 uintmax_t val)
743 {
744 switch (val) {
745 case AF_INET:
746 return bputs(buf, "AF_INET");
747 case AF_INET6:
748 return bputs(buf, "AF_INET6");
749 case AF_UNIX:
750 return bputs(buf, "AF_UNIX");
751 #ifdef AF_PACKET
752 case AF_PACKET:
753 return bputs(buf, "AF_PACKET");
754 #endif
755 #ifdef AF_NETLINK
756 case AF_NETLINK:
757 return bputs(buf, "AF_NETLINK");
758 #endif
759 }
760 return bprintfrr(buf, "AF_(%ju)", val);
761 }
762
763 printfrr_ext_autoreg_i("SO", printfrr_so);
764 static ssize_t printfrr_so(struct fbuf *buf, struct printfrr_eargs *ea,
765 uintmax_t val)
766 {
767 switch (val) {
768 case SOCK_STREAM:
769 return bputs(buf, "SOCK_STREAM");
770 case SOCK_DGRAM:
771 return bputs(buf, "SOCK_DGRAM");
772 case SOCK_SEQPACKET:
773 return bputs(buf, "SOCK_SEQPACKET");
774 #ifdef SOCK_RAW
775 case SOCK_RAW:
776 return bputs(buf, "SOCK_RAW");
777 #endif
778 #ifdef SOCK_PACKET
779 case SOCK_PACKET:
780 return bputs(buf, "SOCK_PACKET");
781 #endif
782 }
783 return bprintfrr(buf, "SOCK_(%ju)", val);
784 }