]>
git.proxmox.com Git - mirror_frr.git/blob - lib/sockunion.c
1 /* Socket union related function.
2 * Copyright (c) 1997, 98 Kunihiro Ishiguro
4 * This file is part of GNU Zebra.
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
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.
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
25 #include "sockunion.h"
29 #include "lib_errors.h"
32 DEFINE_MTYPE_STATIC(LIB
, SOCKUNION
, "Socket union");
34 const char *inet_sutop(const union sockunion
*su
, char *str
)
36 switch (su
->sa
.sa_family
) {
38 inet_ntop(AF_INET
, &su
->sin
.sin_addr
, str
, INET_ADDRSTRLEN
);
41 inet_ntop(AF_INET6
, &su
->sin6
.sin6_addr
, str
, INET6_ADDRSTRLEN
);
47 int str2sockunion(const char *str
, union sockunion
*su
)
54 memset(su
, 0, sizeof(union sockunion
));
56 ret
= inet_pton(AF_INET
, str
, &su
->sin
.sin_addr
);
57 if (ret
> 0) /* Valid IPv4 address format. */
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 */
65 ret
= inet_pton(AF_INET6
, str
, &su
->sin6
.sin6_addr
);
66 if (ret
> 0) /* Valid IPv6 address format. */
68 su
->sin6
.sin6_family
= AF_INET6
;
70 su
->sin6
.sin6_len
= sizeof(struct sockaddr_in6
);
77 const char *sockunion2str(const union sockunion
*su
, char *buf
, size_t len
)
79 switch (sockunion_family(su
)) {
81 snprintf(buf
, len
, "(unspec)");
84 return inet_ntop(AF_INET
, &su
->sin
.sin_addr
, buf
, len
);
86 return inet_ntop(AF_INET6
, &su
->sin6
.sin6_addr
, buf
, len
);
88 snprintf(buf
, len
, "(af %d)", sockunion_family(su
));
92 union sockunion
*sockunion_str2su(const char *str
)
94 union sockunion
*su
= XCALLOC(MTYPE_SOCKUNION
, sizeof(union sockunion
));
96 if (!str2sockunion(str
, su
))
99 XFREE(MTYPE_SOCKUNION
, su
);
103 /* Convert IPv4 compatible IPv6 address to IPv4 address. */
104 static void sockunion_normalise_mapped(union sockunion
*su
)
106 struct sockaddr_in sin
;
108 if (su
->sa
.sa_family
== AF_INET6
109 && IN6_IS_ADDR_V4MAPPED(&su
->sin6
.sin6_addr
)) {
110 memset(&sin
, 0, sizeof(sin
));
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
));
118 /* return sockunion structure : this function should be revised. */
119 static const char *sockunion_log(const union sockunion
*su
, char *buf
,
122 switch (su
->sa
.sa_family
) {
124 return inet_ntop(AF_INET
, &su
->sin
.sin_addr
, buf
, len
);
127 return inet_ntop(AF_INET6
, &(su
->sin6
.sin6_addr
), buf
, len
);
130 snprintf(buf
, len
, "af_unknown %d ", su
->sa
.sa_family
);
135 /* Return socket of sockunion. */
136 int sockunion_socket(const union sockunion
*su
)
140 sock
= socket(su
->sa
.sa_family
, SOCK_STREAM
, 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
));
152 /* Return accepted new socket file descriptor. */
153 int sockunion_accept(int sock
, union sockunion
*su
)
158 len
= sizeof(union sockunion
);
159 client_sock
= accept(sock
, (struct sockaddr
*)su
, &len
);
161 sockunion_normalise_mapped(su
);
165 /* Return sizeof union sockunion. */
166 int sockunion_sizeof(const union sockunion
*su
)
171 switch (su
->sa
.sa_family
) {
173 ret
= sizeof(struct sockaddr_in
);
176 ret
= sizeof(struct sockaddr_in6
);
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
)
189 memcpy(&su
, peersu
, sizeof(union sockunion
));
191 switch (su
.sa
.sa_family
) {
193 su
.sin
.sin_port
= port
;
196 su
.sin6
.sin6_port
= port
;
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
);
206 /* Call connect function. */
207 ret
= connect(fd
, (struct sockaddr
*)&su
, sockunion_sizeof(&su
));
209 /* Immediate success */
211 return connect_success
;
213 /* If connect is in progress then return 1 else it's real error. */
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
;
224 return connect_in_progress
;
227 /* Make socket from sockunion union. */
228 int sockunion_stream_socket(union sockunion
*su
)
232 if (su
->sa
.sa_family
== 0)
233 su
->sa
.sa_family
= AF_INET_UNION
;
235 sock
= socket(su
->sa
.sa_family
, SOCK_STREAM
, 0);
238 flog_err(EC_LIB_SOCKET
,
239 "can't make socket sockunion_stream_socket");
244 /* Bind socket to specified address. */
245 int sockunion_bind(int sock
, union sockunion
*su
, unsigned short port
,
246 union sockunion
*su_addr
)
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 */
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
);
263 su
->sin6
.sin6_len
= size
;
264 #endif /* SIN6_LEN */
265 if (su_addr
== NULL
) {
267 memset(&su
->sin6
.sin6_addr
, 0, sizeof(struct in6_addr
));
269 su
->sin6
.sin6_addr
= in6addr_any
;
270 #endif /* LINUX_IPV6 */
274 ret
= bind(sock
, (struct sockaddr
*)su
, size
);
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
));
285 int sockopt_reuseaddr(int sock
)
290 ret
= setsockopt(sock
, SOL_SOCKET
, SO_REUSEADDR
, (void *)&on
,
293 flog_err(EC_LIB_SOCKET
,
294 "can't set sockopt SO_REUSEADDR to socket %d", sock
);
301 int sockopt_reuseport(int sock
)
306 ret
= setsockopt(sock
, SOL_SOCKET
, SO_REUSEPORT
, (void *)&on
,
309 flog_err(EC_LIB_SOCKET
,
310 "can't set sockopt SO_REUSEPORT to socket %d", sock
);
316 int sockopt_reuseport(int sock
)
322 int sockopt_ttl(int family
, int sock
, int ttl
)
327 if (family
== AF_INET
) {
328 ret
= setsockopt(sock
, IPPROTO_IP
, IP_TTL
, (void *)&ttl
,
331 flog_err(EC_LIB_SOCKET
,
332 "can't set sockopt IP_TTL %d to socket %d",
339 if (family
== AF_INET6
) {
340 ret
= setsockopt(sock
, IPPROTO_IPV6
, IPV6_UNICAST_HOPS
,
341 (void *)&ttl
, sizeof(int));
345 "can't set sockopt IPV6_UNICAST_HOPS %d to socket %d",
354 int sockopt_minttl(int family
, int sock
, int minttl
)
357 if (family
== AF_INET
) {
358 int ret
= setsockopt(sock
, IPPROTO_IP
, IP_MINTTL
, &minttl
,
363 "can't set sockopt IP_MINTTL to %d on socket %d: %s",
364 minttl
, sock
, safe_strerror(errno
));
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
));
375 "can't set sockopt IPV6_MINHOPCOUNT to %d on socket %d: %s",
376 minttl
, sock
, safe_strerror(errno
));
385 int sockopt_v6only(int family
, int sock
)
390 if (family
== AF_INET6
) {
391 ret
= setsockopt(sock
, IPPROTO_IPV6
, IPV6_V6ONLY
, (void *)&on
,
394 flog_err(EC_LIB_SOCKET
,
395 "can't set sockopt IPV6_V6ONLY to socket %d",
401 #endif /* IPV6_V6ONLY */
405 /* If same family and same prefix return 1. */
406 int sockunion_same(const union sockunion
*su1
, const union sockunion
*su2
)
410 if (su1
->sa
.sa_family
!= su2
->sa
.sa_family
)
413 switch (su1
->sa
.sa_family
) {
415 ret
= memcmp(&su1
->sin
.sin_addr
, &su2
->sin
.sin_addr
,
416 sizeof(struct in_addr
));
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
)
437 unsigned int sockunion_hash(const union sockunion
*su
)
439 switch (sockunion_family(su
)) {
441 return jhash_1word(su
->sin
.sin_addr
.s_addr
, 0);
443 return jhash2(su
->sin6
.sin6_addr
.s6_addr32
,
444 array_size(su
->sin6
.sin6_addr
.s6_addr32
), 0);
449 size_t family2addrsize(int family
)
453 return sizeof(struct in_addr
);
455 return sizeof(struct in6_addr
);
460 size_t sockunion_get_addrlen(const union sockunion
*su
)
462 return family2addrsize(sockunion_family(su
));
465 const uint8_t *sockunion_get_addr(const union sockunion
*su
)
467 switch (sockunion_family(su
)) {
469 return (const uint8_t *)&su
->sin
.sin_addr
.s_addr
;
471 return (const uint8_t *)&su
->sin6
.sin6_addr
;
476 void sockunion_set(union sockunion
*su
, int family
, const uint8_t *addr
,
479 if (family2addrsize(family
) != bytes
)
482 sockunion_family(su
) = family
;
485 memcpy(&su
->sin
.sin_addr
.s_addr
, addr
, bytes
);
488 memcpy(&su
->sin6
.sin6_addr
, addr
, bytes
);
493 /* After TCP connection is established. Get local address and port. */
494 union sockunion
*sockunion_getsockname(int fd
)
500 struct sockaddr_in sin
;
501 struct sockaddr_in6 sin6
;
502 char tmp_buffer
[128];
506 memset(&name
, 0, sizeof(name
));
509 ret
= getsockname(fd
, (struct sockaddr
*)&name
, &len
);
511 flog_err(EC_LIB_SOCKET
,
512 "Can't get local address and port by getsockname: %s",
513 safe_strerror(errno
));
517 if (name
.sa
.sa_family
== AF_INET
) {
518 su
= XCALLOC(MTYPE_SOCKUNION
, sizeof(union sockunion
));
519 memcpy(su
, &name
, sizeof(struct sockaddr_in
));
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
);
531 "Unexpected AFI received(%d) for sockunion_getsockname call for fd: %d",
532 name
.sa
.sa_family
, fd
);
536 /* After TCP connection is established. Get remote address and port. */
537 union sockunion
*sockunion_getpeername(int fd
)
543 struct sockaddr_in sin
;
544 struct sockaddr_in6 sin6
;
545 char tmp_buffer
[128];
549 memset(&name
, 0, sizeof(name
));
551 ret
= getpeername(fd
, (struct sockaddr
*)&name
, &len
);
553 flog_err(EC_LIB_SOCKET
, "Can't get remote address and port: %s",
554 safe_strerror(errno
));
558 if (name
.sa
.sa_family
== AF_INET
) {
559 su
= XCALLOC(MTYPE_SOCKUNION
, sizeof(union sockunion
));
560 memcpy(su
, &name
, sizeof(struct sockaddr_in
));
563 if (name
.sa
.sa_family
== AF_INET6
) {
564 su
= XCALLOC(MTYPE_SOCKUNION
, sizeof(union sockunion
));
565 memcpy(su
, &name
, sizeof(struct sockaddr_in6
));
566 sockunion_normalise_mapped(su
);
572 "Unexpected AFI received(%d) for sockunion_getpeername call for fd: %d",
573 name
.sa
.sa_family
, fd
);
577 /* Print sockunion structure */
578 static void __attribute__((unused
)) sockunion_print(const union sockunion
*su
)
583 switch (su
->sa
.sa_family
) {
585 printf("%pI4\n", &su
->sin
.sin_addr
);
588 printf("%pI6\n", &su
->sin6
.sin6_addr
);
592 struct sockaddr_dl
*sdl
;
594 sdl
= (struct sockaddr_dl
*)&(su
->sa
);
595 printf("link#%d\n", sdl
->sdl_index
);
599 printf("af_unknown %d\n", su
->sa
.sa_family
);
604 int in6addr_cmp(const struct in6_addr
*addr1
, const struct in6_addr
*addr2
)
607 const uint8_t *p1
, *p2
;
609 p1
= (const uint8_t *)addr1
;
610 p2
= (const uint8_t *)addr2
;
612 for (i
= 0; i
< sizeof(struct in6_addr
); i
++) {
615 else if (p1
[i
] < p2
[i
])
621 int sockunion_cmp(const union sockunion
*su1
, const union sockunion
*su2
)
623 if (su1
->sa
.sa_family
> su2
->sa
.sa_family
)
625 if (su1
->sa
.sa_family
< su2
->sa
.sa_family
)
628 if (su1
->sa
.sa_family
== AF_INET
) {
629 if (ntohl(sockunion2ip(su1
)) == ntohl(sockunion2ip(su2
)))
631 if (ntohl(sockunion2ip(su1
)) > ntohl(sockunion2ip(su2
)))
636 if (su1
->sa
.sa_family
== AF_INET6
)
637 return in6addr_cmp(&su1
->sin6
.sin6_addr
, &su2
->sin6
.sin6_addr
);
641 /* Duplicate sockunion. */
642 union sockunion
*sockunion_dup(const union sockunion
*su
)
644 union sockunion
*dup
=
645 XCALLOC(MTYPE_SOCKUNION
, sizeof(union sockunion
));
646 memcpy(dup
, su
, sizeof(union sockunion
));
650 void sockunion_free(union sockunion
*su
)
652 XFREE(MTYPE_SOCKUNION
, su
);
655 void sockunion_init(union sockunion
*su
)
657 memset(su
, 0, sizeof(union sockunion
));
660 printfrr_ext_autoreg_p("SU", printfrr_psu
);
661 static ssize_t
printfrr_psu(struct fbuf
*buf
, struct printfrr_eargs
*ea
,
664 const union sockunion
*su
= ptr
;
665 bool include_port
= false, include_scope
= false;
666 bool endflags
= false;
668 char cbuf
[INET6_ADDRSTRLEN
];
671 return bputs(buf
, "(null)");
681 include_scope
= true;
689 switch (sockunion_family(su
)) {
691 ret
+= bputs(buf
, "(unspec)");
694 inet_ntop(AF_INET
, &su
->sin
.sin_addr
, cbuf
, sizeof(cbuf
));
695 ret
+= bputs(buf
, cbuf
);
697 ret
+= bprintfrr(buf
, ":%d", ntohs(su
->sin
.sin_port
));
701 ret
+= bputch(buf
, '[');
702 inet_ntop(AF_INET6
, &su
->sin6
.sin6_addr
, cbuf
, sizeof(cbuf
));
703 ret
+= bputs(buf
, cbuf
);
704 if (include_scope
&& su
->sin6
.sin6_scope_id
)
705 ret
+= bprintfrr(buf
, "%%%u",
706 (unsigned int)su
->sin6
.sin6_scope_id
);
708 ret
+= bprintfrr(buf
, "]:%d",
709 ntohs(su
->sin6
.sin6_port
));
714 if (su
->sun
.sun_path
[0] == '\0' && su
->sun
.sun_path
[1]) {
715 len
= strnlen(su
->sun
.sun_path
+ 1,
716 sizeof(su
->sun
.sun_path
) - 1);
717 ret
+= bprintfrr(buf
, "@%*pSE", len
,
718 su
->sun
.sun_path
+ 1);
722 len
= strnlen(su
->sun
.sun_path
, sizeof(su
->sun
.sun_path
));
723 ret
+= bprintfrr(buf
, "%*pSE", len
, su
->sun
.sun_path
);
727 ret
+= bprintfrr(buf
, "(af %d)", sockunion_family(su
));
733 int sockunion_is_null(const union sockunion
*su
)
735 unsigned char null_s6_addr
[16] = {0};
737 switch (sockunion_family(su
)) {
741 return (su
->sin
.sin_addr
.s_addr
== 0);
743 return !memcmp(su
->sin6
.sin6_addr
.s6_addr
, null_s6_addr
,
744 sizeof(null_s6_addr
));
750 printfrr_ext_autoreg_i("PF", printfrr_pf
);
751 static ssize_t
printfrr_pf(struct fbuf
*buf
, struct printfrr_eargs
*ea
,
756 return bputs(buf
, "AF_INET");
758 return bputs(buf
, "AF_INET6");
760 return bputs(buf
, "AF_UNIX");
763 return bputs(buf
, "AF_PACKET");
767 return bputs(buf
, "AF_NETLINK");
770 return bprintfrr(buf
, "AF_(%ju)", val
);
773 printfrr_ext_autoreg_i("SO", printfrr_so
);
774 static ssize_t
printfrr_so(struct fbuf
*buf
, struct printfrr_eargs
*ea
,
779 return bputs(buf
, "SOCK_STREAM");
781 return bputs(buf
, "SOCK_DGRAM");
783 return bputs(buf
, "SOCK_SEQPACKET");
786 return bputs(buf
, "SOCK_RAW");
790 return bputs(buf
, "SOCK_PACKET");
793 return bprintfrr(buf
, "SOCK_(%ju)", val
);