chiark / gitweb /
Merge uaudio stragglers.
[disorder] / lib / addr.c
CommitLineData
460b9539 1/*
2 * This file is part of DisOrder.
5aff007d 3 * Copyright (C) 2004, 2007, 2008 Richard Kettlewell
460b9539 4 *
e7eb3a27 5 * This program is free software: you can redistribute it and/or modify
460b9539 6 * it under the terms of the GNU General Public License as published by
e7eb3a27 7 * the Free Software Foundation, either version 3 of the License, or
460b9539 8 * (at your option) any later version.
e7eb3a27
RK
9 *
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.
14 *
460b9539 15 * You should have received a copy of the GNU General Public License
e7eb3a27 16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
460b9539 17 */
d6ea854a
RK
18/** @file lib/addr.c
19 * @brief Socket address support */
460b9539 20
05b75f8d 21#include "common.h"
460b9539 22
460b9539 23#include <sys/types.h>
24#include <sys/socket.h>
25#include <netinet/in.h>
6fba990c
RK
26#include <arpa/inet.h>
27#include <sys/un.h>
460b9539 28
29#include "log.h"
30#include "printf.h"
460b9539 31#include "addr.h"
6fba990c 32#include "mem.h"
460b9539 33
ec6f8488 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
39 *
40 * This converts one or two strings into an address specification suitable
41 * for passing to socket(), bind() etc.
42 *
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.
46 *
47 * @p namep is used to return a description of the address suitable
48 * for use in log messages.
49 *
50 * If an error occurs a message is logged and a null pointer returned.
51 */
460b9539 52struct addrinfo *get_address(const struct stringlist *a,
53 const struct addrinfo *pref,
54 char **namep) {
55 struct addrinfo *res;
56 char *name;
57 int rc;
e83d0967
RK
58
59 switch(a->n) {
60 case 1:
460b9539 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));
64 return 0;
65 }
e83d0967
RK
66 break;
67 case 2:
460b9539 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));
71 return 0;
72 }
e83d0967
RK
73 break;
74 default:
75 error(0, "invalid network address specification (n=%d)", a->n);
76 return 0;
460b9539 77 }
e83d0967
RK
78 if(!res || (pref && res->ai_socktype != pref->ai_socktype)) {
79 error(0, "getaddrinfo didn't give us a suitable socket address");
460b9539 80 if(res)
81 freeaddrinfo(res);
82 return 0;
83 }
84 if(namep)
85 *namep = name;
86 return res;
87}
88
d7b6f0d1 89/** @brief Comparison function for address information
90 *
91 * Suitable for qsort().
92 */
460b9539 93int addrinfocmp(const struct addrinfo *a,
94 const struct addrinfo *b) {
95 const struct sockaddr_in *ina, *inb;
96 const struct sockaddr_in6 *in6a, *in6b;
97
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;
136ef3a3 101 switch(a->ai_family) {
460b9539 102 case PF_INET:
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;
107 break;
108 case PF_INET6:
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));
115 default:
116 error(0, "unsupported protocol family %d", a->ai_protocol);
117 return memcmp(a->ai_addr, b->ai_addr, a->ai_addrlen); /* kludge */
118 }
119}
6fba990c 120
0590cedc 121/** @brief Return nonzero if @p sin4 is an IPv4 multicast address */
8d516888
RK
122static inline int multicast4(const struct sockaddr_in *sin4) {
123 return IN_MULTICAST(ntohl(sin4->sin_addr.s_addr));
6fba990c
RK
124}
125
0590cedc 126/** @brief Return nonzero if @p sin6 is an IPv6 multicast address */
6fba990c
RK
127static inline int multicast6(const struct sockaddr_in6 *sin6) {
128 return IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr);
129}
130
131/** @brief Return true if @p sa represents a multicast address */
132int multicast(const struct sockaddr *sa) {
133 switch(sa->sa_family) {
134 case AF_INET:
135 return multicast4((const struct sockaddr_in *)sa);
136 case AF_INET6:
137 return multicast6((const struct sockaddr_in6 *)sa);
138 default:
139 return 0;
140 }
141}
142
0590cedc 143/** @brief Format an IPv4 address */
8d516888 144static inline char *format_sockaddr4(const struct sockaddr_in *sin4) {
6fba990c
RK
145 char buffer[1024], *r;
146
8d516888 147 if(sin4->sin_port)
6fba990c 148 byte_xasprintf(&r, "%s port %u",
8d516888 149 inet_ntop(sin4->sin_family, &sin4->sin_addr,
6fba990c 150 buffer, sizeof buffer),
8d516888 151 ntohs(sin4->sin_port));
6fba990c
RK
152 else
153 byte_xasprintf(&r, "%s",
8d516888 154 inet_ntop(sin4->sin_family, &sin4->sin_addr,
6fba990c
RK
155 buffer, sizeof buffer));
156 return r;
157}
158
0590cedc 159/** @brief Format an IPv6 address */
6fba990c
RK
160static inline char *format_sockaddr6(const struct sockaddr_in6 *sin6) {
161 char buffer[1024], *r;
162
163 if(sin6->sin6_port)
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));
168 else
169 byte_xasprintf(&r, "%s",
170 inet_ntop(sin6->sin6_family, &sin6->sin6_addr,
171 buffer, sizeof buffer));
172 return r;
173}
174
0590cedc 175/** @brief Format a UNIX socket address */
6fba990c
RK
176static inline char *format_sockaddrun(const struct sockaddr_un *sun) {
177 return xstrdup(sun->sun_path);
178}
179
0590cedc
RK
180/** @brief Construct a text description a sockaddr
181 * @param sa Socket address
182 * @return Human-readable form of address
183 */
6fba990c
RK
184char *format_sockaddr(const struct sockaddr *sa) {
185 switch(sa->sa_family) {
186 case AF_INET:
187 return format_sockaddr4((const struct sockaddr_in *)sa);
188 case AF_INET6:
189 return format_sockaddr6((const struct sockaddr_in6 *)sa);
190 case AF_UNIX:
191 return format_sockaddrun((const struct sockaddr_un *)sa);
192 default:
193 return 0;
194 }
195}
196
460b9539 197/*
198Local Variables:
199c-basic-offset:2
200comment-column:40
201End:
202*/