xref: /openbsd-src/usr.bin/dig/lib/isc/include/isc/sockaddr.h (revision 7df817b3f6b1f79925d2dbe9eb883246e56b4d84)
1 /*
2  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
9  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
10  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
11  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
12  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
13  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
14  * PERFORMANCE OF THIS SOFTWARE.
15  */
16 
17 /* $Id: sockaddr.h,v 1.8 2020/12/21 11:41:09 florian Exp $ */
18 
19 #ifndef ISC_SOCKADDR_H
20 #define ISC_SOCKADDR_H 1
21 
22 /*! \file isc/sockaddr.h */
23 
24 #include <sys/socket.h>
25 #include <netinet/in.h>
26 #include <arpa/inet.h>
27 
28 #include <isc/types.h>
29 #include <sys/un.h>
30 
31 #define ISC_SOCKADDR_CMPADDR	  0x0001	/*%< compare the address
32 						 *   sin_addr/sin6_addr */
33 #define ISC_SOCKADDR_CMPPORT 	  0x0002	/*%< compare the port
34 						 *   sin_port/sin6_port */
35 #define ISC_SOCKADDR_CMPSCOPE     0x0004	/*%< compare the scope
36 						 *   sin6_scope */
37 #define ISC_SOCKADDR_CMPSCOPEZERO 0x0008	/*%< when comparing scopes
38 						 *   zero scopes always match */
39 
40 int
41 isc_sockaddr_compare(const struct sockaddr_storage *a, const struct sockaddr_storage *b,
42 		     unsigned int flags);
43 /*%<
44  * Compare the elements of the two address ('a' and 'b') as specified
45  * by 'flags' and report if they are equal or not.
46  *
47  * 'flags' is set from ISC_SOCKADDR_CMP*.
48  */
49 
50 int
51 isc_sockaddr_equal(const struct sockaddr_storage *a, const struct sockaddr_storage *b);
52 /*%<
53  * Return 1 iff the socket addresses 'a' and 'b' are equal.
54  */
55 
56 int
57 isc_sockaddr_eqaddr(const struct sockaddr_storage *a, const struct sockaddr_storage *b);
58 /*%<
59  * Return 1 iff the address parts of the socket addresses
60  * 'a' and 'b' are equal, ignoring the ports.
61  */
62 
63 void
64 isc_sockaddr_any(struct sockaddr_storage *sockaddr);
65 /*%<
66  * Return the IPv4 wildcard address.
67  */
68 
69 void
70 isc_sockaddr_any6(struct sockaddr_storage *sockaddr);
71 /*%<
72  * Return the IPv6 wildcard address.
73  */
74 
75 void
76 isc_sockaddr_anyofpf(struct sockaddr_storage *sockaddr, int family);
77 /*%<
78  * Set '*sockaddr' to the wildcard address of protocol family
79  * 'family'.
80  *
81  * Requires:
82  * \li	'family' is AF_INET or AF_INET6.
83  */
84 
85 int
86 isc_sockaddr_pf(const struct sockaddr_storage *sockaddr);
87 /*%<
88  * Get the protocol family of 'sockaddr'.
89  *
90  * Requires:
91  *
92  *\li	'sockaddr' is a valid sockaddr with an address family of AF_INET
93  *	or AF_INET6.
94  *
95  * Returns:
96  *
97  *\li	The protocol family of 'sockaddr', e.g. PF_INET or PF_INET6.
98  */
99 
100 in_port_t
101 isc_sockaddr_getport(const struct sockaddr_storage *sockaddr);
102 /*%<
103  * Get the port stored in 'sockaddr'.
104  */
105 
106 isc_result_t
107 isc_sockaddr_totext(const struct sockaddr_storage *sockaddr, isc_buffer_t *target);
108 /*%<
109  * Append a text representation of 'sockaddr' to the buffer 'target'.
110  * The text will include both the IP address (v4 or v6) and the port.
111  * The text is null terminated, but the terminating null is not
112  * part of the buffer's used region.
113  *
114  * Returns:
115  * \li	ISC_R_SUCCESS
116  * \li	ISC_R_NOSPACE	The text or the null termination did not fit.
117  */
118 
119 void
120 isc_sockaddr_format(const struct sockaddr_storage *sa, char *array, unsigned int size);
121 /*%<
122  * Format a human-readable representation of the socket address '*sa'
123  * into the character array 'array', which is of size 'size'.
124  * The resulting string is guaranteed to be null-terminated.
125  */
126 
127 int
128 isc_sockaddr_ismulticast(const struct sockaddr_storage *sa);
129 /*%<
130  * Returns #1 if the address is a multicast address.
131  */
132 
133 int
134 isc_sockaddr_islinklocal(const struct sockaddr_storage *sa);
135 /*%<
136  * Returns 1 if the address is a link local address.
137  */
138 
139 int
140 isc_sockaddr_issitelocal(const struct sockaddr_storage *sa);
141 /*%<
142  * Returns 1 if the address is a sitelocal address.
143  */
144 
145 #define ISC_SOCKADDR_FORMATSIZE \
146 	sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:XXX.XXX.XXX.XXX%SSSSSSSSSS#YYYYY")
147 /*%<
148  * Minimum size of array to pass to isc_sockaddr_format().
149  */
150 
151 #endif /* ISC_SOCKADDR_H */
152