]> git.proxmox.com Git - mirror_frr.git/blob - lib/sockunion.h
doc: Add `show ipv6 rpf X:X::X:X` command to docs
[mirror_frr.git] / lib / sockunion.h
1 /*
2 * Socket union header.
3 * Copyright (c) 1997 Kunihiro Ishiguro
4 *
5 * This file is part of GNU Zebra.
6 *
7 * GNU Zebra is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
10 * later version.
11 *
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #ifndef _ZEBRA_SOCKUNION_H
23 #define _ZEBRA_SOCKUNION_H
24
25 #include "privs.h"
26 #include "if.h"
27 #include <sys/un.h>
28 #ifdef __OpenBSD__
29 #include <netmpls/mpls.h>
30 #endif
31
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35
36 union sockunion {
37 struct sockaddr sa;
38 struct sockaddr_in sin;
39 struct sockaddr_in6 sin6;
40 struct sockaddr_un sun;
41 #ifdef __OpenBSD__
42 struct sockaddr_mpls smpls;
43 struct sockaddr_rtlabel rtlabel;
44 #endif
45 };
46
47 enum connect_result { connect_error, connect_success, connect_in_progress };
48
49 /* Default address family. */
50 #define AF_INET_UNION AF_INET6
51
52 /* Sockunion address string length. Same as INET6_ADDRSTRLEN. */
53 #define SU_ADDRSTRLEN 46
54
55 /* Macro to set link local index to the IPv6 address. For KAME IPv6
56 stack. */
57 #ifdef KAME
58 #define IN6_LINKLOCAL_IFINDEX(a) ((a).s6_addr[2] << 8 | (a).s6_addr[3])
59 #define SET_IN6_LINKLOCAL_IFINDEX(a, i) \
60 do { \
61 (a).s6_addr[2] = ((i) >> 8) & 0xff; \
62 (a).s6_addr[3] = (i)&0xff; \
63 } while (0)
64 #else
65 #define IN6_LINKLOCAL_IFINDEX(a)
66 #define SET_IN6_LINKLOCAL_IFINDEX(a, i)
67 #endif /* KAME */
68
69 #define sockunion_family(X) (X)->sa.sa_family
70
71 #define sockunion2ip(X) (X)->sin.sin_addr.s_addr
72
73 /* Prototypes. */
74 extern int str2sockunion(const char *, union sockunion *);
75 extern const char *sockunion2str(const union sockunion *, char *, size_t);
76 int in6addr_cmp(const struct in6_addr *addr1, const struct in6_addr *addr2);
77 extern int sockunion_cmp(const union sockunion *, const union sockunion *);
78 extern int sockunion_same(const union sockunion *, const union sockunion *);
79 extern unsigned int sockunion_hash(const union sockunion *);
80
81 extern size_t family2addrsize(int family);
82 extern size_t sockunion_get_addrlen(const union sockunion *);
83 extern const uint8_t *sockunion_get_addr(const union sockunion *);
84 extern void sockunion_set(union sockunion *, int family, const uint8_t *addr,
85 size_t bytes);
86
87 extern union sockunion *sockunion_str2su(const char *str);
88 extern int sockunion_accept(int sock, union sockunion *);
89 extern int sockunion_sizeof(const union sockunion *su);
90 extern int sockunion_stream_socket(union sockunion *);
91 extern int sockopt_reuseaddr(int);
92 extern int sockopt_reuseport(int);
93 extern int sockopt_v6only(int family, int sock);
94 extern int sockunion_bind(int sock, union sockunion *, unsigned short,
95 union sockunion *);
96 extern int sockopt_ttl(int family, int sock, int ttl);
97 extern int sockopt_minttl(int family, int sock, int minttl);
98 extern int sockunion_socket(const union sockunion *su);
99 extern const char *inet_sutop(const union sockunion *su, char *str);
100 extern enum connect_result sockunion_connect(int fd, const union sockunion *su,
101 unsigned short port, ifindex_t);
102 extern union sockunion *sockunion_getsockname(int);
103 extern union sockunion *sockunion_getpeername(int);
104 extern union sockunion *sockunion_dup(const union sockunion *);
105 extern void sockunion_free(union sockunion *);
106 extern void sockunion_init(union sockunion *);
107 extern int sockunion_is_null(const union sockunion *su);
108
109 #ifdef _FRR_ATTRIBUTE_PRINTFRR
110 #pragma FRR printfrr_ext "%pSU" (union sockunion *)
111 #pragma FRR printfrr_ext "%pSU" (struct sockaddr *)
112 #pragma FRR printfrr_ext "%pSU" (struct sockaddr_storage *)
113 #pragma FRR printfrr_ext "%pSU" (struct sockaddr_in *)
114 #pragma FRR printfrr_ext "%pSU" (struct sockaddr_in6 *)
115 #pragma FRR printfrr_ext "%pSU" (struct sockaddr_un *)
116
117 /* AF_INET/PF_INET & co., using "PF" to avoid confusion with AFI/SAFI */
118 #pragma FRR printfrr_ext "%dPF" (int)
119 /* SOCK_STREAM & co. */
120 #pragma FRR printfrr_ext "%dSO" (int)
121 #endif
122
123 #ifdef __cplusplus
124 }
125 #endif
126
127 #endif /* _ZEBRA_SOCKUNION_H */