1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
|
/*
* Copyright 2009 Oracle. All rights reserved.
*
* This file is part of nfs-utils.
*
* nfs-utils is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* nfs-utils is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with nfs-utils. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef NFS_UTILS_SOCKADDR_H
#define NFS_UTILS_SOCKADDR_H
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <sys/socket.h>
#include <netinet/in.h>
/*
* This type is for defining buffers that contain network socket
* addresses.
*
* Casting a "struct sockaddr *" to the address of a "struct
* sockaddr_storage" breaks C aliasing rules. The "union
* nfs_sockaddr" type follows C aliasing rules yet specifically
* allows converting pointers to it between "struct sockaddr *"
* and a few other network sockaddr-related pointer types.
*
* Note that this union is much smaller than a sockaddr_storage.
* It should be used only for AF_INET or AF_INET6 socket addresses.
* An AF_LOCAL sockaddr_un, for example, will clearly not fit into
* a buffer of this type.
*/
union nfs_sockaddr {
struct sockaddr sa;
struct sockaddr_in s4;
struct sockaddr_in6 s6;
};
#if SIZEOF_SOCKLEN_T - 0 == 0
#define socklen_t unsigned int
#endif
#define SIZEOF_SOCKADDR_UNKNOWN (socklen_t)0
#define SIZEOF_SOCKADDR_IN (socklen_t)sizeof(struct sockaddr_in)
#ifdef IPV6_SUPPORTED
#define SIZEOF_SOCKADDR_IN6 (socklen_t)sizeof(struct sockaddr_in6)
#else /* !IPV6_SUPPORTED */
#define SIZEOF_SOCKADDR_IN6 SIZEOF_SOCKADDR_UNKNOWN
#endif /* !IPV6_SUPPORTED */
/**
* nfs_sockaddr_length - return the size in bytes of a socket address
* @sap: pointer to socket address
*
* Returns the size in bytes of @sap, or zero if the family is
* not recognized.
*/
static inline socklen_t
nfs_sockaddr_length(const struct sockaddr *sap)
{
switch (sap->sa_family) {
case AF_INET:
return SIZEOF_SOCKADDR_IN;
case AF_INET6:
return SIZEOF_SOCKADDR_IN6;
}
return SIZEOF_SOCKADDR_UNKNOWN;
}
static inline uint16_t
get_port4(const struct sockaddr *sap)
{
const struct sockaddr_in *sin = (const struct sockaddr_in *)sap;
return ntohs(sin->sin_port);
}
#ifdef IPV6_SUPPORTED
static inline uint16_t
get_port6(const struct sockaddr *sap)
{
const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)sap;
return ntohs(sin6->sin6_port);
}
#else /* !IPV6_SUPPORTED */
static inline uint16_t
get_port6(__attribute__ ((unused)) const struct sockaddr *sap)
{
return 0;
}
#endif /* !IPV6_SUPPORTED */
/**
* nfs_get_port - extract port value from a socket address
* @sap: pointer to socket address
*
* Returns port value in host byte order, or zero if the
* socket address contains an unrecognized family.
*/
static inline uint16_t
nfs_get_port(const struct sockaddr *sap)
{
switch (sap->sa_family) {
case AF_INET:
return get_port4(sap);
case AF_INET6:
return get_port6(sap);
}
return 0;
}
static inline void
set_port4(struct sockaddr *sap, const uint16_t port)
{
struct sockaddr_in *sin = (struct sockaddr_in *)sap;
sin->sin_port = htons(port);
}
#ifdef IPV6_SUPPORTED
static inline void
set_port6(struct sockaddr *sap, const uint16_t port)
{
struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
sin6->sin6_port = htons(port);
}
#else /* !IPV6_SUPPORTED */
static inline void
set_port6(__attribute__ ((unused)) struct sockaddr *sap,
__attribute__ ((unused)) const uint16_t port)
{
}
#endif /* !IPV6_SUPPORTED */
/**
* nfs_set_port - set port value in a socket address
* @sap: pointer to socket address
* @port: port value to set
*
*/
static inline void
nfs_set_port(struct sockaddr *sap, const uint16_t port)
{
switch (sap->sa_family) {
case AF_INET:
set_port4(sap, port);
break;
case AF_INET6:
set_port6(sap, port);
break;
}
}
/**
* nfs_is_v4_loopback - test to see if socket address is AF_INET loopback
* @sap: pointer to socket address
*
* Returns true if the socket address is the standard IPv4 loopback
* address; otherwise false is returned.
*/
static inline _Bool
nfs_is_v4_loopback(const struct sockaddr *sap)
{
const struct sockaddr_in *sin = (const struct sockaddr_in *)sap;
if (sin->sin_family != AF_INET)
return false;
if (sin->sin_addr.s_addr != htonl(INADDR_LOOPBACK))
return false;
return true;
}
static inline _Bool
compare_sockaddr4(const struct sockaddr *sa1, const struct sockaddr *sa2)
{
const struct sockaddr_in *sin1 = (const struct sockaddr_in *)sa1;
const struct sockaddr_in *sin2 = (const struct sockaddr_in *)sa2;
return sin1->sin_addr.s_addr == sin2->sin_addr.s_addr;
}
#ifdef IPV6_SUPPORTED
static inline _Bool
compare_sockaddr6(const struct sockaddr *sa1, const struct sockaddr *sa2)
{
const struct sockaddr_in6 *sin1 = (const struct sockaddr_in6 *)sa1;
const struct sockaddr_in6 *sin2 = (const struct sockaddr_in6 *)sa2;
const struct in6_addr *saddr1 = &sin1->sin6_addr;
const struct in6_addr *saddr2 = &sin2->sin6_addr;
if (IN6_IS_ADDR_LINKLOCAL(saddr1) && IN6_IS_ADDR_LINKLOCAL(saddr2))
if (sin1->sin6_scope_id != sin2->sin6_scope_id)
return false;
return IN6_ARE_ADDR_EQUAL(saddr1, saddr2);
}
#else /* !IPV6_SUPPORTED */
static inline _Bool
compare_sockaddr6(__attribute__ ((unused)) const struct sockaddr *sa1,
__attribute__ ((unused)) const struct sockaddr *sa2)
{
return false;
}
#endif /* !IPV6_SUPPORTED */
/**
* nfs_compare_sockaddr - compare two socket addresses for equality
* @sa1: pointer to a socket address
* @sa2: pointer to a socket address
*
* Returns true if the two socket addresses contain equivalent
* network addresses; otherwise false is returned.
*/
static inline _Bool
nfs_compare_sockaddr(const struct sockaddr *sa1, const struct sockaddr *sa2)
{
if (sa1 == NULL || sa2 == NULL)
return false;
if (sa1->sa_family == sa2->sa_family)
switch (sa1->sa_family) {
case AF_INET:
return compare_sockaddr4(sa1, sa2);
case AF_INET6:
return compare_sockaddr6(sa1, sa2);
}
return false;
}
#endif /* !NFS_UTILS_SOCKADDR_H */
|