]>
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"
30 DEFINE_MTYPE_STATIC(LIB
, SOCKUNION
, "Socket union")
33 inet_sutop (const union sockunion
*su
, char *str
)
35 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
);
48 str2sockunion (const char *str
, union sockunion
*su
)
52 memset (su
, 0, sizeof (union sockunion
));
54 ret
= inet_pton (AF_INET
, str
, &su
->sin
.sin_addr
);
55 if (ret
> 0) /* Valid IPv4 address format. */
57 su
->sin
.sin_family
= AF_INET
;
58 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
59 su
->sin
.sin_len
= sizeof(struct sockaddr_in
);
60 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
63 ret
= inet_pton (AF_INET6
, str
, &su
->sin6
.sin6_addr
);
64 if (ret
> 0) /* Valid IPv6 address format. */
66 su
->sin6
.sin6_family
= AF_INET6
;
68 su
->sin6
.sin6_len
= sizeof(struct sockaddr_in6
);
76 sockunion2str (const union sockunion
*su
, char *buf
, size_t len
)
78 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
));
93 sockunion_str2su (const char *str
)
95 union sockunion
*su
= XCALLOC (MTYPE_SOCKUNION
, sizeof (union sockunion
));
97 if (!str2sockunion (str
, su
))
100 XFREE (MTYPE_SOCKUNION
, su
);
104 /* Convert IPv4 compatible IPv6 address to IPv4 address. */
106 sockunion_normalise_mapped (union sockunion
*su
)
108 struct sockaddr_in sin
;
110 if (su
->sa
.sa_family
== AF_INET6
111 && IN6_IS_ADDR_V4MAPPED (&su
->sin6
.sin6_addr
))
113 memset (&sin
, 0, sizeof (struct sockaddr_in
));
114 sin
.sin_family
= AF_INET
;
115 sin
.sin_port
= su
->sin6
.sin6_port
;
116 memcpy (&sin
.sin_addr
, ((char *)&su
->sin6
.sin6_addr
) + 12, 4);
117 memcpy (su
, &sin
, sizeof (struct sockaddr_in
));
121 /* return sockunion structure : this function should be revised. */
123 sockunion_log (const union sockunion
*su
, char *buf
, size_t len
)
125 switch (su
->sa
.sa_family
)
128 return inet_ntop(AF_INET
, &su
->sin
.sin_addr
, buf
, len
);
131 return inet_ntop(AF_INET6
, &(su
->sin6
.sin6_addr
), buf
, len
);
135 snprintf (buf
, len
, "af_unknown %d ", su
->sa
.sa_family
);
140 /* Return socket of sockunion. */
142 sockunion_socket (const union sockunion
*su
)
146 sock
= socket (su
->sa
.sa_family
, SOCK_STREAM
, 0);
149 char buf
[SU_ADDRSTRLEN
];
150 zlog_warn("Can't make socket for %s : %s",
151 sockunion_log(su
, buf
, SU_ADDRSTRLEN
), safe_strerror(errno
));
158 /* Return accepted new socket file descriptor. */
160 sockunion_accept (int sock
, union sockunion
*su
)
165 len
= sizeof (union sockunion
);
166 client_sock
= accept (sock
, (struct sockaddr
*) su
, &len
);
168 sockunion_normalise_mapped (su
);
172 /* Return sizeof union sockunion. */
174 sockunion_sizeof (const union sockunion
*su
)
179 switch (su
->sa
.sa_family
)
182 ret
= sizeof (struct sockaddr_in
);
185 ret
= sizeof (struct sockaddr_in6
);
191 /* sockunion_connect returns
194 1 : connect is in progress */
196 sockunion_connect (int fd
, const union sockunion
*peersu
, unsigned short port
,
203 memcpy (&su
, peersu
, sizeof (union sockunion
));
205 switch (su
.sa
.sa_family
)
208 su
.sin
.sin_port
= port
;
211 su
.sin6
.sin6_port
= port
;
213 if (IN6_IS_ADDR_LINKLOCAL(&su
.sin6
.sin6_addr
) && ifindex
)
215 su
.sin6
.sin6_scope_id
= ifindex
;
216 SET_IN6_LINKLOCAL_IFINDEX (su
.sin6
.sin6_addr
, ifindex
);
222 /* Make socket non-block. */
223 val
= fcntl (fd
, F_GETFL
, 0);
224 fcntl (fd
, F_SETFL
, val
|O_NONBLOCK
);
226 /* Call connect function. */
227 ret
= connect (fd
, (struct sockaddr
*) &su
, sockunion_sizeof (&su
));
229 /* Immediate success */
232 fcntl (fd
, F_SETFL
, val
);
233 return connect_success
;
236 /* If connect is in progress then return 1 else it's real error. */
239 if (errno
!= EINPROGRESS
)
241 char str
[SU_ADDRSTRLEN
];
242 zlog_info ("can't connect to %s fd %d : %s",
243 sockunion_log (&su
, str
, sizeof str
),
244 fd
, safe_strerror (errno
));
245 return connect_error
;
249 fcntl (fd
, F_SETFL
, val
);
251 return connect_in_progress
;
254 /* Make socket from sockunion union. */
256 sockunion_stream_socket (union sockunion
*su
)
260 if (su
->sa
.sa_family
== 0)
261 su
->sa
.sa_family
= AF_INET_UNION
;
263 sock
= socket (su
->sa
.sa_family
, SOCK_STREAM
, 0);
266 zlog_warn("can't make socket sockunion_stream_socket");
271 /* Bind socket to specified address. */
273 sockunion_bind (int sock
, union sockunion
*su
, unsigned short port
,
274 union sockunion
*su_addr
)
279 if (su
->sa
.sa_family
== AF_INET
)
281 size
= sizeof (struct sockaddr_in
);
282 su
->sin
.sin_port
= htons (port
);
283 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
284 su
->sin
.sin_len
= size
;
285 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
287 sockunion2ip (su
) = htonl (INADDR_ANY
);
289 else if (su
->sa
.sa_family
== AF_INET6
)
291 size
= sizeof (struct sockaddr_in6
);
292 su
->sin6
.sin6_port
= htons (port
);
294 su
->sin6
.sin6_len
= size
;
295 #endif /* SIN6_LEN */
299 memset (&su
->sin6
.sin6_addr
, 0, sizeof (struct in6_addr
));
301 su
->sin6
.sin6_addr
= in6addr_any
;
302 #endif /* LINUX_IPV6 */
306 ret
= bind (sock
, (struct sockaddr
*)su
, size
);
309 char buf
[SU_ADDRSTRLEN
];
310 zlog_warn("can't bind socket for %s : %s",
311 sockunion_log(su
, buf
, SU_ADDRSTRLEN
), safe_strerror(errno
));
318 sockopt_reuseaddr (int sock
)
323 ret
= setsockopt (sock
, SOL_SOCKET
, SO_REUSEADDR
,
324 (void *) &on
, sizeof (on
));
327 zlog_warn("can't set sockopt SO_REUSEADDR to socket %d", sock
);
335 sockopt_reuseport (int sock
)
340 ret
= setsockopt (sock
, SOL_SOCKET
, SO_REUSEPORT
,
341 (void *) &on
, sizeof (on
));
344 zlog_warn("can't set sockopt SO_REUSEPORT to socket %d", sock
);
351 sockopt_reuseport (int sock
)
358 sockopt_ttl (int family
, int sock
, int ttl
)
363 if (family
== AF_INET
)
365 ret
= setsockopt (sock
, IPPROTO_IP
, IP_TTL
,
366 (void *) &ttl
, sizeof (int));
369 zlog_warn("can't set sockopt IP_TTL %d to socket %d", ttl
, sock
);
375 if (family
== AF_INET6
)
377 ret
= setsockopt (sock
, IPPROTO_IPV6
, IPV6_UNICAST_HOPS
,
378 (void *) &ttl
, sizeof (int));
381 zlog_warn("can't set sockopt IPV6_UNICAST_HOPS %d to socket %d",
391 sockopt_cork (int sock
, int onoff
)
394 return setsockopt (sock
, IPPROTO_TCP
, TCP_CORK
, &onoff
, sizeof(onoff
));
400 int sockopt_mark_default(int sock
, int mark
, struct zebra_privs_t
*cap
)
405 if ( cap
->change (ZPRIVS_RAISE
) )
406 zlog_err ("routing_socket: Can't raise privileges");
408 ret
= setsockopt(sock
, SOL_SOCKET
, SO_MARK
, &mark
, sizeof(mark
));
410 if ( cap
->change (ZPRIVS_LOWER
) )
411 zlog_err ("routing_socket: Can't lower privileges");
420 sockopt_minttl (int family
, int sock
, int minttl
)
423 if (family
== AF_INET
)
425 int ret
= setsockopt (sock
, IPPROTO_IP
, IP_MINTTL
, &minttl
, sizeof(minttl
));
427 zlog_warn("can't set sockopt IP_MINTTL to %d on socket %d: %s",
428 minttl
, sock
, safe_strerror(errno
));
431 #endif /* IP_MINTTL */
432 #ifdef IPV6_MINHOPCOUNT
433 if (family
== AF_INET6
)
435 int ret
= setsockopt (sock
, IPPROTO_IPV6
, IPV6_MINHOPCOUNT
, &minttl
, sizeof(minttl
));
437 zlog_warn("can't set sockopt IPV6_MINHOPCOUNT to %d on socket %d: %s",
438 minttl
, sock
, safe_strerror(errno
));
448 sockopt_v6only (int family
, int sock
)
453 if (family
== AF_INET6
)
455 ret
= setsockopt (sock
, IPPROTO_IPV6
, IPV6_V6ONLY
,
456 (void *) &on
, sizeof (int));
459 zlog_warn("can't set sockopt IPV6_V6ONLY " "to socket %d",
465 #endif /* IPV6_V6ONLY */
469 /* If same family and same prefix return 1. */
471 sockunion_same (const union sockunion
*su1
, const union sockunion
*su2
)
475 if (su1
->sa
.sa_family
!= su2
->sa
.sa_family
)
478 switch (su1
->sa
.sa_family
)
481 ret
= memcmp (&su1
->sin
.sin_addr
, &su2
->sin
.sin_addr
,
482 sizeof (struct in_addr
));
485 ret
= memcmp (&su1
->sin6
.sin6_addr
, &su2
->sin6
.sin6_addr
,
486 sizeof (struct in6_addr
));
487 if ((ret
== 0) && IN6_IS_ADDR_LINKLOCAL(&su1
->sin6
.sin6_addr
))
489 /* compare interface indices */
490 if (su1
->sin6
.sin6_scope_id
&& su2
->sin6
.sin6_scope_id
)
491 ret
= (su1
->sin6
.sin6_scope_id
== su2
->sin6
.sin6_scope_id
) ? 0 : 1;
502 sockunion_hash (const union sockunion
*su
)
504 switch (sockunion_family(su
))
507 return jhash_1word(su
->sin
.sin_addr
.s_addr
, 0);
509 return jhash2(su
->sin6
.sin6_addr
.s6_addr32
, ZEBRA_NUM_OF(su
->sin6
.sin6_addr
.s6_addr32
), 0);
515 family2addrsize(int family
)
520 return sizeof(struct in_addr
);
522 return sizeof(struct in6_addr
);
528 sockunion_get_addrlen(const union sockunion
*su
)
530 return family2addrsize(sockunion_family(su
));
534 sockunion_get_addr(const union sockunion
*su
)
536 switch (sockunion_family(su
))
539 return (const u_char
*) &su
->sin
.sin_addr
.s_addr
;
541 return (const u_char
*) &su
->sin6
.sin6_addr
;
547 sockunion_set(union sockunion
*su
, int family
, const u_char
*addr
, size_t bytes
)
549 if (family2addrsize(family
) != bytes
)
552 sockunion_family(su
) = family
;
556 memcpy(&su
->sin
.sin_addr
.s_addr
, addr
, bytes
);
559 memcpy(&su
->sin6
.sin6_addr
, addr
, bytes
);
564 /* After TCP connection is established. Get local address and port. */
566 sockunion_getsockname (int fd
)
573 struct sockaddr_in sin
;
574 struct sockaddr_in6 sin6
;
575 char tmp_buffer
[128];
579 memset (&name
, 0, sizeof name
);
582 ret
= getsockname (fd
, (struct sockaddr
*)&name
, &len
);
585 zlog_warn ("Can't get local address and port by getsockname: %s",
586 safe_strerror (errno
));
590 if (name
.sa
.sa_family
== AF_INET
)
592 su
= XCALLOC (MTYPE_SOCKUNION
, sizeof (union sockunion
));
593 memcpy (su
, &name
, sizeof (struct sockaddr_in
));
596 if (name
.sa
.sa_family
== AF_INET6
)
598 su
= XCALLOC (MTYPE_SOCKUNION
, sizeof (union sockunion
));
599 memcpy (su
, &name
, sizeof (struct sockaddr_in6
));
600 sockunion_normalise_mapped (su
);
606 /* After TCP connection is established. Get remote address and port. */
608 sockunion_getpeername (int fd
)
615 struct sockaddr_in sin
;
616 struct sockaddr_in6 sin6
;
617 char tmp_buffer
[128];
621 memset (&name
, 0, sizeof name
);
623 ret
= getpeername (fd
, (struct sockaddr
*)&name
, &len
);
626 zlog_warn("Can't get remote address and port: %s", safe_strerror(errno
));
630 if (name
.sa
.sa_family
== AF_INET
)
632 su
= XCALLOC (MTYPE_SOCKUNION
, sizeof (union sockunion
));
633 memcpy (su
, &name
, sizeof (struct sockaddr_in
));
636 if (name
.sa
.sa_family
== AF_INET6
)
638 su
= XCALLOC (MTYPE_SOCKUNION
, sizeof (union sockunion
));
639 memcpy (su
, &name
, sizeof (struct sockaddr_in6
));
640 sockunion_normalise_mapped (su
);
646 /* Print sockunion structure */
647 static void __attribute__ ((unused
))
648 sockunion_print (const union sockunion
*su
)
653 switch (su
->sa
.sa_family
)
656 printf ("%s\n", inet_ntoa (su
->sin
.sin_addr
));
660 char buf
[SU_ADDRSTRLEN
];
662 printf ("%s\n", inet_ntop (AF_INET6
, &(su
->sin6
.sin6_addr
),
670 struct sockaddr_dl
*sdl
;
672 sdl
= (struct sockaddr_dl
*)&(su
->sa
);
673 printf ("link#%d\n", sdl
->sdl_index
);
678 printf ("af_unknown %d\n", su
->sa
.sa_family
);
684 in6addr_cmp (const struct in6_addr
*addr1
, const struct in6_addr
*addr2
)
687 const u_char
*p1
, *p2
;
689 p1
= (const u_char
*)addr1
;
690 p2
= (const u_char
*)addr2
;
692 for (i
= 0; i
< sizeof (struct in6_addr
); i
++)
696 else if (p1
[i
] < p2
[i
])
703 sockunion_cmp (const union sockunion
*su1
, const union sockunion
*su2
)
705 if (su1
->sa
.sa_family
> su2
->sa
.sa_family
)
707 if (su1
->sa
.sa_family
< su2
->sa
.sa_family
)
710 if (su1
->sa
.sa_family
== AF_INET
)
712 if (ntohl (sockunion2ip (su1
)) == ntohl (sockunion2ip (su2
)))
714 if (ntohl (sockunion2ip (su1
)) > ntohl (sockunion2ip (su2
)))
719 if (su1
->sa
.sa_family
== AF_INET6
)
720 return in6addr_cmp (&su1
->sin6
.sin6_addr
, &su2
->sin6
.sin6_addr
);
724 /* Duplicate sockunion. */
726 sockunion_dup (const union sockunion
*su
)
728 union sockunion
*dup
= XCALLOC (MTYPE_SOCKUNION
, sizeof (union sockunion
));
729 memcpy (dup
, su
, sizeof (union sockunion
));
734 sockunion_free (union sockunion
*su
)
736 XFREE (MTYPE_SOCKUNION
, su
);
740 sockunion_init (union sockunion
*su
)
742 memset(su
, 0, sizeof(union sockunion
));