xref: /dragonfly/contrib/dhcpcd/src/sa.h (revision fcf6efef)
1 /*
2  * Socket Address handling for dhcpcd
3  * Copyright (c) 2015-2018 Roy Marples <roy@marples.name>
4  * All rights reserved
5 
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #ifndef SA_H
29 #define SA_H
30 
31 #include <sys/socket.h>
32 
33 union sa_ss {
34 	struct sockaddr		sa;
35 	struct sockaddr_in	sin;
36 	struct sockaddr_in6	sin6;
37 };
38 
39 #ifdef BSD
40 #define HAVE_SA_LEN
41 #endif
42 
43 /* Allow for a sockaddr_dl being printed too. */
44 #define INET_MAX_ADDRSTRLEN	(20 * 3)
45 
46 #ifdef INET
47 #define satosin(sa) ((struct sockaddr_in *)(void *)(sa))
48 #define satocsin(sa) ((const struct sockaddr_in *)(const void *)(sa))
49 #endif
50 #ifdef INET6
51 #define satosin6(sa) ((struct sockaddr_in6 *)(void *)(sa))
52 #define satocsin6(sa) ((const struct sockaddr_in6 *)(const void *)(sa))
53 #endif
54 
55 socklen_t sa_addroffset(const struct sockaddr *sa);
56 socklen_t sa_addrlen(const struct sockaddr *sa);
57 bool sa_is_unspecified(const struct sockaddr *);
58 bool sa_is_allones(const struct sockaddr *);
59 bool sa_is_loopback(const struct sockaddr *);
60 void *sa_toaddr(struct sockaddr *);
61 int sa_toprefix(const struct sockaddr *);
62 int sa_fromprefix(struct sockaddr *, int);
63 const char *sa_addrtop(const struct sockaddr *, char *, socklen_t);
64 int sa_cmp(const struct sockaddr *, const struct sockaddr *);
65 void sa_in_init(struct sockaddr *, const struct in_addr *);
66 void sa_in6_init(struct sockaddr *, const struct in6_addr *);
67 
68 #endif
69