]>
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"
31 DEFINE_MTYPE_STATIC(LIB
, SOCKUNION
, "Socket union")
33 const char *inet_sutop(const union sockunion
*su
, char *str
)
35 switch (su
->sa
.sa_family
) {
37 inet_ntop(AF_INET
, &su
->sin
.sin_addr
, str
, INET_ADDRSTRLEN
);
40 inet_ntop(AF_INET6
, &su
->sin6
.sin6_addr
, str
, INET6_ADDRSTRLEN
);
46 int str2sockunion(const char *str
, union sockunion
*su
)
53 memset(su
, 0, sizeof(union sockunion
));
55 ret
= inet_pton(AF_INET
, str
, &su
->sin
.sin_addr
);
56 if (ret
> 0) /* Valid IPv4 address format. */
58 su
->sin
.sin_family
= AF_INET
;
59 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
60 su
->sin
.sin_len
= sizeof(struct sockaddr_in
);
61 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
64 ret
= inet_pton(AF_INET6
, str
, &su
->sin6
.sin6_addr
);
65 if (ret
> 0) /* Valid IPv6 address format. */
67 su
->sin6
.sin6_family
= AF_INET6
;
69 su
->sin6
.sin6_len
= sizeof(struct sockaddr_in6
);
76 const char *sockunion2str(const union sockunion
*su
, char *buf
, size_t len
)
78 switch (sockunion_family(su
)) {
80 snprintf(buf
, len
, "(unspec)");
83 return inet_ntop(AF_INET
, &su
->sin
.sin_addr
, buf
, len
);
85 return inet_ntop(AF_INET6
, &su
->sin6
.sin6_addr
, buf
, len
);
87 snprintf(buf
, len
, "(af %d)", sockunion_family(su
));
91 union sockunion
*sockunion_str2su(const char *str
)
93 union sockunion
*su
= XCALLOC(MTYPE_SOCKUNION
, sizeof(union sockunion
));
95 if (!str2sockunion(str
, su
))
98 XFREE(MTYPE_SOCKUNION
, su
);
102 /* Convert IPv4 compatible IPv6 address to IPv4 address. */
103 static void sockunion_normalise_mapped(union sockunion
*su
)
105 struct sockaddr_in sin
;
107 if (su
->sa
.sa_family
== AF_INET6
108 && IN6_IS_ADDR_V4MAPPED(&su
->sin6
.sin6_addr
)) {
109 memset(&sin
, 0, sizeof(struct sockaddr_in
));
110 sin
.sin_family
= AF_INET
;
111 sin
.sin_port
= su
->sin6
.sin6_port
;
112 memcpy(&sin
.sin_addr
, ((char *)&su
->sin6
.sin6_addr
) + 12, 4);
113 memcpy(su
, &sin
, sizeof(struct sockaddr_in
));
117 /* return sockunion structure : this function should be revised. */
118 static const char *sockunion_log(const union sockunion
*su
, char *buf
,
121 switch (su
->sa
.sa_family
) {
123 return inet_ntop(AF_INET
, &su
->sin
.sin_addr
, buf
, len
);
126 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(LIB_ERR_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 static 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(LIB_ERR_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(LIB_ERR_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(LIB_ERR_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(LIB_ERR_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(LIB_ERR_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",
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 occured 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
364 int sockopt_cork(int sock
, int onoff
)
369 int sockopt_mark_default(int sock
, int mark
, struct zebra_privs_t
*cap
)
374 frr_elevate_privs(cap
) {
375 ret
= setsockopt(sock
, SOL_SOCKET
, SO_MARK
, &mark
,
384 int sockopt_minttl(int family
, int sock
, int minttl
)
387 if (family
== AF_INET
) {
388 int ret
= setsockopt(sock
, IPPROTO_IP
, IP_MINTTL
, &minttl
,
393 "can't set sockopt IP_MINTTL to %d on socket %d: %s",
394 minttl
, sock
, safe_strerror(errno
));
397 #endif /* IP_MINTTL */
398 #ifdef IPV6_MINHOPCOUNT
399 if (family
== AF_INET6
) {
400 int ret
= setsockopt(sock
, IPPROTO_IPV6
, IPV6_MINHOPCOUNT
,
401 &minttl
, sizeof(minttl
));
405 "can't set sockopt IPV6_MINHOPCOUNT to %d on socket %d: %s",
406 minttl
, sock
, safe_strerror(errno
));
415 int sockopt_v6only(int family
, int sock
)
420 if (family
== AF_INET6
) {
421 ret
= setsockopt(sock
, IPPROTO_IPV6
, IPV6_V6ONLY
, (void *)&on
,
424 flog_err(LIB_ERR_SOCKET
,
425 "can't set sockopt IPV6_V6ONLY "
432 #endif /* IPV6_V6ONLY */
436 /* If same family and same prefix return 1. */
437 int sockunion_same(const union sockunion
*su1
, const union sockunion
*su2
)
441 if (su1
->sa
.sa_family
!= su2
->sa
.sa_family
)
444 switch (su1
->sa
.sa_family
) {
446 ret
= memcmp(&su1
->sin
.sin_addr
, &su2
->sin
.sin_addr
,
447 sizeof(struct in_addr
));
450 ret
= memcmp(&su1
->sin6
.sin6_addr
, &su2
->sin6
.sin6_addr
,
451 sizeof(struct in6_addr
));
452 if ((ret
== 0) && IN6_IS_ADDR_LINKLOCAL(&su1
->sin6
.sin6_addr
)) {
453 /* compare interface indices */
454 if (su1
->sin6
.sin6_scope_id
&& su2
->sin6
.sin6_scope_id
)
455 ret
= (su1
->sin6
.sin6_scope_id
456 == su2
->sin6
.sin6_scope_id
)
468 unsigned int sockunion_hash(const union sockunion
*su
)
470 switch (sockunion_family(su
)) {
472 return jhash_1word(su
->sin
.sin_addr
.s_addr
, 0);
474 return jhash2(su
->sin6
.sin6_addr
.s6_addr32
,
475 ZEBRA_NUM_OF(su
->sin6
.sin6_addr
.s6_addr32
), 0);
480 size_t family2addrsize(int family
)
484 return sizeof(struct in_addr
);
486 return sizeof(struct in6_addr
);
491 size_t sockunion_get_addrlen(const union sockunion
*su
)
493 return family2addrsize(sockunion_family(su
));
496 const uint8_t *sockunion_get_addr(const union sockunion
*su
)
498 switch (sockunion_family(su
)) {
500 return (const uint8_t *)&su
->sin
.sin_addr
.s_addr
;
502 return (const uint8_t *)&su
->sin6
.sin6_addr
;
507 void sockunion_set(union sockunion
*su
, int family
, const uint8_t *addr
,
510 if (family2addrsize(family
) != bytes
)
513 sockunion_family(su
) = family
;
516 memcpy(&su
->sin
.sin_addr
.s_addr
, addr
, bytes
);
519 memcpy(&su
->sin6
.sin6_addr
, addr
, bytes
);
524 /* After TCP connection is established. Get local address and port. */
525 union sockunion
*sockunion_getsockname(int fd
)
531 struct sockaddr_in sin
;
532 struct sockaddr_in6 sin6
;
533 char tmp_buffer
[128];
537 memset(&name
, 0, sizeof name
);
540 ret
= getsockname(fd
, (struct sockaddr
*)&name
, &len
);
542 flog_err(LIB_ERR_SOCKET
,
543 "Can't get local address and port by getsockname: %s",
544 safe_strerror(errno
));
548 if (name
.sa
.sa_family
== AF_INET
) {
549 su
= XCALLOC(MTYPE_SOCKUNION
, sizeof(union sockunion
));
550 memcpy(su
, &name
, sizeof(struct sockaddr_in
));
553 if (name
.sa
.sa_family
== AF_INET6
) {
554 su
= XCALLOC(MTYPE_SOCKUNION
, sizeof(union sockunion
));
555 memcpy(su
, &name
, sizeof(struct sockaddr_in6
));
556 sockunion_normalise_mapped(su
);
562 /* After TCP connection is established. Get remote address and port. */
563 union sockunion
*sockunion_getpeername(int fd
)
569 struct sockaddr_in sin
;
570 struct sockaddr_in6 sin6
;
571 char tmp_buffer
[128];
575 memset(&name
, 0, sizeof name
);
577 ret
= getpeername(fd
, (struct sockaddr
*)&name
, &len
);
579 flog_err(LIB_ERR_SOCKET
,
580 "Can't get remote address and port: %s",
581 safe_strerror(errno
));
585 if (name
.sa
.sa_family
== AF_INET
) {
586 su
= XCALLOC(MTYPE_SOCKUNION
, sizeof(union sockunion
));
587 memcpy(su
, &name
, sizeof(struct sockaddr_in
));
590 if (name
.sa
.sa_family
== AF_INET6
) {
591 su
= XCALLOC(MTYPE_SOCKUNION
, sizeof(union sockunion
));
592 memcpy(su
, &name
, sizeof(struct sockaddr_in6
));
593 sockunion_normalise_mapped(su
);
599 /* Print sockunion structure */
600 static void __attribute__((unused
)) sockunion_print(const union sockunion
*su
)
605 switch (su
->sa
.sa_family
) {
607 printf("%s\n", inet_ntoa(su
->sin
.sin_addr
));
610 char buf
[SU_ADDRSTRLEN
];
612 printf("%s\n", inet_ntop(AF_INET6
, &(su
->sin6
.sin6_addr
), buf
,
618 struct sockaddr_dl
*sdl
;
620 sdl
= (struct sockaddr_dl
*)&(su
->sa
);
621 printf("link#%d\n", sdl
->sdl_index
);
625 printf("af_unknown %d\n", su
->sa
.sa_family
);
630 static int in6addr_cmp(const struct in6_addr
*addr1
,
631 const struct in6_addr
*addr2
)
634 const uint8_t *p1
, *p2
;
636 p1
= (const uint8_t *)addr1
;
637 p2
= (const uint8_t *)addr2
;
639 for (i
= 0; i
< sizeof(struct in6_addr
); i
++) {
642 else if (p1
[i
] < p2
[i
])
648 int sockunion_cmp(const union sockunion
*su1
, const union sockunion
*su2
)
650 if (su1
->sa
.sa_family
> su2
->sa
.sa_family
)
652 if (su1
->sa
.sa_family
< su2
->sa
.sa_family
)
655 if (su1
->sa
.sa_family
== AF_INET
) {
656 if (ntohl(sockunion2ip(su1
)) == ntohl(sockunion2ip(su2
)))
658 if (ntohl(sockunion2ip(su1
)) > ntohl(sockunion2ip(su2
)))
663 if (su1
->sa
.sa_family
== AF_INET6
)
664 return in6addr_cmp(&su1
->sin6
.sin6_addr
, &su2
->sin6
.sin6_addr
);
668 /* Duplicate sockunion. */
669 union sockunion
*sockunion_dup(const union sockunion
*su
)
671 union sockunion
*dup
=
672 XCALLOC(MTYPE_SOCKUNION
, sizeof(union sockunion
));
673 memcpy(dup
, su
, sizeof(union sockunion
));
677 void sockunion_free(union sockunion
*su
)
679 XFREE(MTYPE_SOCKUNION
, su
);
682 void sockunion_init(union sockunion
*su
)
684 memset(su
, 0, sizeof(union sockunion
));