]>
Commit | Line | Data |
---|---|---|
1 | // SPDX-License-Identifier: GPL-2.0-or-later | |
2 | /* | |
3 | * Socket union header. | |
4 | * Copyright (c) 1997 Kunihiro Ishiguro | |
5 | */ | |
6 | ||
7 | #ifndef _ZEBRA_SOCKUNION_H | |
8 | #define _ZEBRA_SOCKUNION_H | |
9 | ||
10 | #include "privs.h" | |
11 | #include "if.h" | |
12 | #include <sys/un.h> | |
13 | #ifdef __OpenBSD__ | |
14 | #include <netmpls/mpls.h> | |
15 | #endif | |
16 | ||
17 | #ifdef __cplusplus | |
18 | extern "C" { | |
19 | #endif | |
20 | ||
21 | union sockunion { | |
22 | struct sockaddr sa; | |
23 | struct sockaddr_in sin; | |
24 | struct sockaddr_in6 sin6; | |
25 | struct sockaddr_un sun; | |
26 | #ifdef __OpenBSD__ | |
27 | struct sockaddr_mpls smpls; | |
28 | struct sockaddr_rtlabel rtlabel; | |
29 | #endif | |
30 | }; | |
31 | ||
32 | enum connect_result { connect_error, connect_success, connect_in_progress }; | |
33 | ||
34 | /* Default address family. */ | |
35 | #define AF_INET_UNION AF_INET6 | |
36 | ||
37 | /* Sockunion address string length. Same as INET6_ADDRSTRLEN. */ | |
38 | #define SU_ADDRSTRLEN 46 | |
39 | ||
40 | /* Macro to set link local index to the IPv6 address. For KAME IPv6 | |
41 | stack. */ | |
42 | #ifdef KAME | |
43 | #define IN6_LINKLOCAL_IFINDEX(a) ((a).s6_addr[2] << 8 | (a).s6_addr[3]) | |
44 | #define SET_IN6_LINKLOCAL_IFINDEX(a, i) \ | |
45 | do { \ | |
46 | (a).s6_addr[2] = ((i) >> 8) & 0xff; \ | |
47 | (a).s6_addr[3] = (i)&0xff; \ | |
48 | } while (0) | |
49 | #else | |
50 | #define IN6_LINKLOCAL_IFINDEX(a) | |
51 | #define SET_IN6_LINKLOCAL_IFINDEX(a, i) | |
52 | #endif /* KAME */ | |
53 | ||
54 | #define sockunion_family(X) (X)->sa.sa_family | |
55 | ||
56 | #define sockunion2ip(X) (X)->sin.sin_addr.s_addr | |
57 | ||
58 | /* Prototypes. */ | |
59 | extern int str2sockunion(const char *, union sockunion *); | |
60 | extern const char *sockunion2str(const union sockunion *, char *, size_t); | |
61 | int in6addr_cmp(const struct in6_addr *addr1, const struct in6_addr *addr2); | |
62 | extern int sockunion_cmp(const union sockunion *, const union sockunion *); | |
63 | extern int sockunion_same(const union sockunion *, const union sockunion *); | |
64 | extern unsigned int sockunion_hash(const union sockunion *); | |
65 | ||
66 | extern size_t family2addrsize(int family); | |
67 | extern size_t sockunion_get_addrlen(const union sockunion *); | |
68 | extern const uint8_t *sockunion_get_addr(const union sockunion *); | |
69 | extern void sockunion_set(union sockunion *, int family, const uint8_t *addr, | |
70 | size_t bytes); | |
71 | ||
72 | extern union sockunion *sockunion_str2su(const char *str); | |
73 | extern int sockunion_accept(int sock, union sockunion *); | |
74 | extern int sockunion_sizeof(const union sockunion *su); | |
75 | extern int sockunion_stream_socket(union sockunion *); | |
76 | extern int sockopt_reuseaddr(int); | |
77 | extern int sockopt_reuseport(int); | |
78 | extern int sockopt_v6only(int family, int sock); | |
79 | extern int sockunion_bind(int sock, union sockunion *, unsigned short, | |
80 | union sockunion *); | |
81 | extern int sockopt_ttl(int family, int sock, int ttl); | |
82 | extern int sockopt_minttl(int family, int sock, int minttl); | |
83 | extern int sockunion_socket(const union sockunion *su); | |
84 | extern const char *inet_sutop(const union sockunion *su, char *str); | |
85 | extern enum connect_result sockunion_connect(int fd, const union sockunion *su, | |
86 | unsigned short port, ifindex_t); | |
87 | extern union sockunion *sockunion_getsockname(int); | |
88 | extern union sockunion *sockunion_getpeername(int); | |
89 | extern union sockunion *sockunion_dup(const union sockunion *); | |
90 | extern void sockunion_free(union sockunion *); | |
91 | extern void sockunion_init(union sockunion *); | |
92 | extern int sockunion_is_null(const union sockunion *su); | |
93 | ||
94 | #ifdef _FRR_ATTRIBUTE_PRINTFRR | |
95 | #pragma FRR printfrr_ext "%pSU" (union sockunion *) | |
96 | #pragma FRR printfrr_ext "%pSU" (struct sockaddr *) | |
97 | #pragma FRR printfrr_ext "%pSU" (struct sockaddr_storage *) | |
98 | #pragma FRR printfrr_ext "%pSU" (struct sockaddr_in *) | |
99 | #pragma FRR printfrr_ext "%pSU" (struct sockaddr_in6 *) | |
100 | #pragma FRR printfrr_ext "%pSU" (struct sockaddr_un *) | |
101 | ||
102 | /* AF_INET/PF_INET & co., using "PF" to avoid confusion with AFI/SAFI */ | |
103 | #pragma FRR printfrr_ext "%dPF" (int) | |
104 | /* SOCK_STREAM & co. */ | |
105 | #pragma FRR printfrr_ext "%dSO" (int) | |
106 | #endif | |
107 | ||
108 | #ifdef __cplusplus | |
109 | } | |
110 | #endif | |
111 | ||
112 | #endif /* _ZEBRA_SOCKUNION_H */ |