Imported Upstream version 1.17
[platform/upstream/krb5.git] / src / include / socket-utils.h
1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /*
3  * Copyright (C) 2001,2005 by the Massachusetts Institute of Technology,
4  * Cambridge, MA, USA.  All Rights Reserved.
5  *
6  * This software is being provided to you, the LICENSEE, by the
7  * Massachusetts Institute of Technology (M.I.T.) under the following
8  * license.  By obtaining, using and/or copying this software, you agree
9  * that you have read, understood, and will comply with these terms and
10  * conditions:
11  *
12  * Export of this software from the United States of America may
13  * require a specific license from the United States Government.
14  * It is the responsibility of any person or organization contemplating
15  * export to obtain such a license before exporting.
16  *
17  * WITHIN THAT CONSTRAINT, permission to use, copy, modify and distribute
18  * this software and its documentation for any purpose and without fee or
19  * royalty is hereby granted, provided that you agree to comply with the
20  * following copyright notice and statements, including the disclaimer, and
21  * that the same appear on ALL copies of the software and documentation,
22  * including modifications that you make for internal use or for
23  * distribution:
24  *
25  * THIS SOFTWARE IS PROVIDED "AS IS", AND M.I.T. MAKES NO REPRESENTATIONS
26  * OR WARRANTIES, EXPRESS OR IMPLIED.  By way of example, but not
27  * limitation, M.I.T. MAKES NO REPRESENTATIONS OR WARRANTIES OF
28  * MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE OR THAT THE USE OF
29  * THE LICENSED SOFTWARE OR DOCUMENTATION WILL NOT INFRINGE ANY THIRD PARTY
30  * PATENTS, COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS.
31  *
32  * The name of the Massachusetts Institute of Technology or M.I.T. may NOT
33  * be used in advertising or publicity pertaining to distribution of the
34  * software.  Title to copyright in this software and any associated
35  * documentation shall at all times remain with M.I.T., and USER agrees to
36  * preserve same.
37  *
38  * Furthermore if you modify this software you must label
39  * your software as modified software and not distribute it in such a
40  * fashion that it might be confused with the original M.I.T. software.
41  */
42
43 #ifndef SOCKET_UTILS_H
44 #define SOCKET_UTILS_H
45
46 /* Some useful stuff cross-platform for manipulating socket addresses.
47    We assume at least ipv4 sockaddr_in support.  The sockaddr_storage
48    stuff comes from the ipv6 socket api enhancements; socklen_t is
49    provided on some systems; the rest is just convenience for internal
50    use in the krb5 tree.
51
52    Do NOT install this file.  */
53
54 /* for HAVE_SOCKLEN_T etc */
55 #include "autoconf.h"
56 /* for sockaddr_storage */
57 #include "port-sockets.h"
58 /* for "inline" if needed */
59 #include "k5-platform.h"
60
61 /*
62  * There's a lot of confusion between pointers to different sockaddr
63  * types, and pointers with different degrees of indirection, as in
64  * the locate_kdc type functions.  Use these function to ensure we
65  * don't do something silly like cast a "sockaddr **" to a
66  * "sockaddr_in *".
67  *
68  * The casts to (void *) are to get GCC to shut up about alignment
69  * increasing.
70  */
71 static inline struct sockaddr_in *sa2sin (struct sockaddr *sa)
72 {
73     return (struct sockaddr_in *) (void *) sa;
74 }
75 static inline struct sockaddr_in6 *sa2sin6 (struct sockaddr *sa)
76 {
77     return (struct sockaddr_in6 *) (void *) sa;
78 }
79 static inline struct sockaddr *ss2sa (struct sockaddr_storage *ss)
80 {
81     return (struct sockaddr *) ss;
82 }
83 static inline struct sockaddr_in *ss2sin (struct sockaddr_storage *ss)
84 {
85     return (struct sockaddr_in *) ss;
86 }
87 static inline struct sockaddr_in6 *ss2sin6 (struct sockaddr_storage *ss)
88 {
89     return (struct sockaddr_in6 *) ss;
90 }
91
92 /* Set the IPv4 or IPv6 port on sa to port.  Do nothing if sa is not an
93  * Internet socket. */
94 static inline void
95 sa_setport(struct sockaddr *sa, uint16_t port)
96 {
97     if (sa->sa_family == AF_INET)
98         sa2sin(sa)->sin_port = htons(port);
99     else if (sa->sa_family == AF_INET6)
100         sa2sin6(sa)->sin6_port = htons(port);
101 }
102
103 /* Get the Internet port number of sa, or 0 if it is not an Internet socket. */
104 static inline uint16_t
105 sa_getport(struct sockaddr *sa)
106 {
107     if (sa->sa_family == AF_INET)
108         return ntohs(sa2sin(sa)->sin_port);
109     else if (sa->sa_family == AF_INET6)
110         return ntohs(sa2sin6(sa)->sin6_port);
111     else
112         return 0;
113 }
114
115 /* Return true if sa is an IPv4 or IPv6 socket address. */
116 static inline int
117 sa_is_inet(struct sockaddr *sa)
118 {
119     return sa->sa_family == AF_INET || sa->sa_family == AF_INET6;
120 }
121
122 /* Return true if sa is an IPv4 or IPv6 wildcard address. */
123 static inline int
124 sa_is_wildcard(struct sockaddr *sa)
125 {
126     if (sa->sa_family == AF_INET6)
127         return IN6_IS_ADDR_UNSPECIFIED(&sa2sin6(sa)->sin6_addr);
128     else if (sa->sa_family == AF_INET)
129         return sa2sin(sa)->sin_addr.s_addr == INADDR_ANY;
130     return 0;
131 }
132
133 /* Return the length of an IPv4 or IPv6 socket structure; abort if it is
134  * neither. */
135 static inline socklen_t
136 sa_socklen(struct sockaddr *sa)
137 {
138     if (sa->sa_family == AF_INET6)
139         return sizeof(struct sockaddr_in6);
140     else if (sa->sa_family == AF_INET)
141         return sizeof(struct sockaddr_in);
142     else
143         abort();
144 }
145
146 #endif /* SOCKET_UTILS_H */