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