chiark / gitweb /
Build fixes for uaudio ALSA/OSS backends.
[disorder] / lib / addr.c
... / ...
CommitLineData
1/*
2 * This file is part of DisOrder.
3 * Copyright (C) 2004, 2007, 2008 Richard Kettlewell
4 *
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.
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 *
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/>.
17 */
18/** @file lib/addr.c
19 * @brief Socket address support */
20
21#include "common.h"
22
23#include <sys/types.h>
24#include <sys/socket.h>
25#include <netinet/in.h>
26#include <arpa/inet.h>
27#include <sys/un.h>
28
29#include "log.h"
30#include "printf.h"
31#include "addr.h"
32#include "mem.h"
33
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 */
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;
58
59 switch(a->n) {
60 case 1:
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 }
66 break;
67 case 2:
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 }
73 break;
74 default:
75 error(0, "invalid network address specification (n=%d)", a->n);
76 return 0;
77 }
78 if(!res || (pref && res->ai_socktype != pref->ai_socktype)) {
79 error(0, "getaddrinfo didn't give us a suitable socket address");
80 if(res)
81 freeaddrinfo(res);
82 return 0;
83 }
84 if(namep)
85 *namep = name;
86 return res;
87}
88
89/** @brief Comparison function for address information
90 *
91 * Suitable for qsort().
92 */
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;
101 switch(a->ai_family) {
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}
120
121/** @brief Return nonzero if @p sin4 is an IPv4 multicast address */
122static inline int multicast4(const struct sockaddr_in *sin4) {
123 return IN_MULTICAST(ntohl(sin4->sin_addr.s_addr));
124}
125
126/** @brief Return nonzero if @p sin6 is an IPv6 multicast address */
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
143/** @brief Format an IPv4 address */
144static inline char *format_sockaddr4(const struct sockaddr_in *sin4) {
145 char buffer[1024], *r;
146
147 if(sin4->sin_port)
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));
152 else
153 byte_xasprintf(&r, "%s",
154 inet_ntop(sin4->sin_family, &sin4->sin_addr,
155 buffer, sizeof buffer));
156 return r;
157}
158
159/** @brief Format an IPv6 address */
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
175/** @brief Format a UNIX socket address */
176static inline char *format_sockaddrun(const struct sockaddr_un *sun) {
177 return xstrdup(sun->sun_path);
178}
179
180/** @brief Construct a text description a sockaddr
181 * @param sa Socket address
182 * @return Human-readable form of address
183 */
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
197/*
198Local Variables:
199c-basic-offset:2
200comment-column:40
201End:
202*/