2 * This file is part of DisOrder.
3 * Copyright (C) 2004, 2007, 2008 Richard Kettlewell
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 * @brief Socket address support */
23 #include <sys/types.h>
24 #include <sys/socket.h>
25 #include <netinet/in.h>
26 #include <arpa/inet.h>
34 /** @brief Convert a pair of strings to an address
35 * @param a Pointer to string list
36 * @param pref Hints structure for getaddrinfo, or NULL
37 * @param namep Where to store address description, or NULL
38 * @return Address info structure or NULL on error
40 * This converts one or two strings into an address specification suitable
41 * for passing to socket(), bind() etc.
43 * If there is only one string then it is assumed to be the service
44 * name (port number). If there are two then the first is the host
45 * name and the second the service name.
47 * @p namep is used to return a description of the address suitable
48 * for use in log messages.
50 * If an error occurs a message is logged and a null pointer returned.
52 struct addrinfo *get_address(const struct stringlist *a,
53 const struct addrinfo *pref,
61 byte_xasprintf(&name, "host * service %s", a->s[0]);
62 if((rc = getaddrinfo(0, a->s[0], pref, &res))) {
63 error(0, "getaddrinfo %s: %s", a->s[0], gai_strerror(rc));
68 byte_xasprintf(&name, "host %s service %s", a->s[0], a->s[1]);
69 if((rc = getaddrinfo(a->s[0], a->s[1], pref, &res))) {
70 error(0, "getaddrinfo %s %s: %s", a->s[0], a->s[1], gai_strerror(rc));
75 error(0, "invalid network address specification (n=%d)", a->n);
78 if(!res || (pref && res->ai_socktype != pref->ai_socktype)) {
79 error(0, "getaddrinfo didn't give us a suitable socket address");
89 /** @brief Comparison function for address information
91 * Suitable for qsort().
93 int addrinfocmp(const struct addrinfo *a,
94 const struct addrinfo *b) {
95 const struct sockaddr_in *ina, *inb;
96 const struct sockaddr_in6 *in6a, *in6b;
98 if(a->ai_family != b->ai_family) return a->ai_family - b->ai_family;
99 if(a->ai_socktype != b->ai_socktype) return a->ai_socktype - b->ai_socktype;
100 if(a->ai_protocol != b->ai_protocol) return a->ai_protocol - b->ai_protocol;
101 switch(a->ai_family) {
103 ina = (const struct sockaddr_in *)a->ai_addr;
104 inb = (const struct sockaddr_in *)b->ai_addr;
105 if(ina->sin_port != inb->sin_port) return ina->sin_port - inb->sin_port;
106 return ina->sin_addr.s_addr - inb->sin_addr.s_addr;
109 in6a = (const struct sockaddr_in6 *)a->ai_addr;
110 in6b = (const struct sockaddr_in6 *)b->ai_addr;
111 if(in6a->sin6_port != in6b->sin6_port)
112 return in6a->sin6_port - in6b->sin6_port;
113 return memcmp(&in6a->sin6_addr, &in6b->sin6_addr,
114 sizeof (struct in6_addr));
116 error(0, "unsupported protocol family %d", a->ai_protocol);
117 return memcmp(a->ai_addr, b->ai_addr, a->ai_addrlen); /* kludge */
121 /** @brief Return nonzero if @p sin4 is an IPv4 multicast address */
122 static inline int multicast4(const struct sockaddr_in *sin4) {
123 return IN_MULTICAST(ntohl(sin4->sin_addr.s_addr));
126 /** @brief Return nonzero if @p sin6 is an IPv6 multicast address */
127 static inline int multicast6(const struct sockaddr_in6 *sin6) {
128 return IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr);
131 /** @brief Return true if @p sa represents a multicast address */
132 int multicast(const struct sockaddr *sa) {
133 switch(sa->sa_family) {
135 return multicast4((const struct sockaddr_in *)sa);
137 return multicast6((const struct sockaddr_in6 *)sa);
143 /** @brief Format an IPv4 address */
144 static inline char *format_sockaddr4(const struct sockaddr_in *sin4) {
145 char buffer[1024], *r;
148 byte_xasprintf(&r, "%s port %u",
149 inet_ntop(sin4->sin_family, &sin4->sin_addr,
150 buffer, sizeof buffer),
151 ntohs(sin4->sin_port));
153 byte_xasprintf(&r, "%s",
154 inet_ntop(sin4->sin_family, &sin4->sin_addr,
155 buffer, sizeof buffer));
159 /** @brief Format an IPv6 address */
160 static inline char *format_sockaddr6(const struct sockaddr_in6 *sin6) {
161 char buffer[1024], *r;
164 byte_xasprintf(&r, "%s port %u",
165 inet_ntop(sin6->sin6_family, &sin6->sin6_addr,
166 buffer, sizeof buffer),
167 ntohs(sin6->sin6_port));
169 byte_xasprintf(&r, "%s",
170 inet_ntop(sin6->sin6_family, &sin6->sin6_addr,
171 buffer, sizeof buffer));
175 /** @brief Format a UNIX socket address */
176 static inline char *format_sockaddrun(const struct sockaddr_un *sun) {
177 return xstrdup(sun->sun_path);
180 /** @brief Construct a text description a sockaddr
181 * @param sa Socket address
182 * @return Human-readable form of address
184 char *format_sockaddr(const struct sockaddr *sa) {
185 switch(sa->sa_family) {
187 return format_sockaddr4((const struct sockaddr_in *)sa);
189 return format_sockaddr6((const struct sockaddr_in6 *)sa);
191 return format_sockaddrun((const struct sockaddr_un *)sa);