Commit | Line | Data |
---|---|---|
460b9539 | 1 | /* |
2 | * This file is part of DisOrder. | |
5aff007d | 3 | * Copyright (C) 2004, 2007, 2008 Richard Kettlewell |
460b9539 | 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 2 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, but | |
11 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | * 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, write to the Free Software | |
17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 | |
18 | * USA | |
19 | */ | |
d6ea854a RK |
20 | /** @file lib/addr.c |
21 | * @brief Socket address support */ | |
460b9539 | 22 | |
05b75f8d | 23 | #include "common.h" |
460b9539 | 24 | |
460b9539 | 25 | #include <sys/types.h> |
26 | #include <sys/socket.h> | |
27 | #include <netinet/in.h> | |
6fba990c RK |
28 | #include <arpa/inet.h> |
29 | #include <sys/un.h> | |
460b9539 | 30 | |
31 | #include "log.h" | |
32 | #include "printf.h" | |
460b9539 | 33 | #include "addr.h" |
6fba990c | 34 | #include "mem.h" |
460b9539 | 35 | |
ec6f8488 | 36 | /** @brief Convert a pair of strings to an address |
37 | * @param a Pointer to string list | |
38 | * @param pref Hints structure for getaddrinfo, or NULL | |
39 | * @param namep Where to store address description, or NULL | |
40 | * @return Address info structure or NULL on error | |
41 | * | |
42 | * This converts one or two strings into an address specification suitable | |
43 | * for passing to socket(), bind() etc. | |
44 | * | |
45 | * If there is only one string then it is assumed to be the service | |
46 | * name (port number). If there are two then the first is the host | |
47 | * name and the second the service name. | |
48 | * | |
49 | * @p namep is used to return a description of the address suitable | |
50 | * for use in log messages. | |
51 | * | |
52 | * If an error occurs a message is logged and a null pointer returned. | |
53 | */ | |
460b9539 | 54 | struct addrinfo *get_address(const struct stringlist *a, |
55 | const struct addrinfo *pref, | |
56 | char **namep) { | |
57 | struct addrinfo *res; | |
58 | char *name; | |
59 | int rc; | |
e83d0967 RK |
60 | |
61 | switch(a->n) { | |
62 | case 1: | |
460b9539 | 63 | byte_xasprintf(&name, "host * service %s", a->s[0]); |
64 | if((rc = getaddrinfo(0, a->s[0], pref, &res))) { | |
65 | error(0, "getaddrinfo %s: %s", a->s[0], gai_strerror(rc)); | |
66 | return 0; | |
67 | } | |
e83d0967 RK |
68 | break; |
69 | case 2: | |
460b9539 | 70 | byte_xasprintf(&name, "host %s service %s", a->s[0], a->s[1]); |
71 | if((rc = getaddrinfo(a->s[0], a->s[1], pref, &res))) { | |
72 | error(0, "getaddrinfo %s %s: %s", a->s[0], a->s[1], gai_strerror(rc)); | |
73 | return 0; | |
74 | } | |
e83d0967 RK |
75 | break; |
76 | default: | |
77 | error(0, "invalid network address specification (n=%d)", a->n); | |
78 | return 0; | |
460b9539 | 79 | } |
e83d0967 RK |
80 | if(!res || (pref && res->ai_socktype != pref->ai_socktype)) { |
81 | error(0, "getaddrinfo didn't give us a suitable socket address"); | |
460b9539 | 82 | if(res) |
83 | freeaddrinfo(res); | |
84 | return 0; | |
85 | } | |
86 | if(namep) | |
87 | *namep = name; | |
88 | return res; | |
89 | } | |
90 | ||
d7b6f0d1 | 91 | /** @brief Comparison function for address information |
92 | * | |
93 | * Suitable for qsort(). | |
94 | */ | |
460b9539 | 95 | int addrinfocmp(const struct addrinfo *a, |
96 | const struct addrinfo *b) { | |
97 | const struct sockaddr_in *ina, *inb; | |
98 | const struct sockaddr_in6 *in6a, *in6b; | |
99 | ||
100 | if(a->ai_family != b->ai_family) return a->ai_family - b->ai_family; | |
101 | if(a->ai_socktype != b->ai_socktype) return a->ai_socktype - b->ai_socktype; | |
102 | if(a->ai_protocol != b->ai_protocol) return a->ai_protocol - b->ai_protocol; | |
136ef3a3 | 103 | switch(a->ai_family) { |
460b9539 | 104 | case PF_INET: |
105 | ina = (const struct sockaddr_in *)a->ai_addr; | |
106 | inb = (const struct sockaddr_in *)b->ai_addr; | |
107 | if(ina->sin_port != inb->sin_port) return ina->sin_port - inb->sin_port; | |
108 | return ina->sin_addr.s_addr - inb->sin_addr.s_addr; | |
109 | break; | |
110 | case PF_INET6: | |
111 | in6a = (const struct sockaddr_in6 *)a->ai_addr; | |
112 | in6b = (const struct sockaddr_in6 *)b->ai_addr; | |
113 | if(in6a->sin6_port != in6b->sin6_port) | |
114 | return in6a->sin6_port - in6b->sin6_port; | |
115 | return memcmp(&in6a->sin6_addr, &in6b->sin6_addr, | |
116 | sizeof (struct in6_addr)); | |
117 | default: | |
118 | error(0, "unsupported protocol family %d", a->ai_protocol); | |
119 | return memcmp(a->ai_addr, b->ai_addr, a->ai_addrlen); /* kludge */ | |
120 | } | |
121 | } | |
6fba990c | 122 | |
0590cedc | 123 | /** @brief Return nonzero if @p sin4 is an IPv4 multicast address */ |
8d516888 RK |
124 | static inline int multicast4(const struct sockaddr_in *sin4) { |
125 | return IN_MULTICAST(ntohl(sin4->sin_addr.s_addr)); | |
6fba990c RK |
126 | } |
127 | ||
0590cedc | 128 | /** @brief Return nonzero if @p sin6 is an IPv6 multicast address */ |
6fba990c RK |
129 | static inline int multicast6(const struct sockaddr_in6 *sin6) { |
130 | return IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr); | |
131 | } | |
132 | ||
133 | /** @brief Return true if @p sa represents a multicast address */ | |
134 | int multicast(const struct sockaddr *sa) { | |
135 | switch(sa->sa_family) { | |
136 | case AF_INET: | |
137 | return multicast4((const struct sockaddr_in *)sa); | |
138 | case AF_INET6: | |
139 | return multicast6((const struct sockaddr_in6 *)sa); | |
140 | default: | |
141 | return 0; | |
142 | } | |
143 | } | |
144 | ||
0590cedc | 145 | /** @brief Format an IPv4 address */ |
8d516888 | 146 | static inline char *format_sockaddr4(const struct sockaddr_in *sin4) { |
6fba990c RK |
147 | char buffer[1024], *r; |
148 | ||
8d516888 | 149 | if(sin4->sin_port) |
6fba990c | 150 | byte_xasprintf(&r, "%s port %u", |
8d516888 | 151 | inet_ntop(sin4->sin_family, &sin4->sin_addr, |
6fba990c | 152 | buffer, sizeof buffer), |
8d516888 | 153 | ntohs(sin4->sin_port)); |
6fba990c RK |
154 | else |
155 | byte_xasprintf(&r, "%s", | |
8d516888 | 156 | inet_ntop(sin4->sin_family, &sin4->sin_addr, |
6fba990c RK |
157 | buffer, sizeof buffer)); |
158 | return r; | |
159 | } | |
160 | ||
0590cedc | 161 | /** @brief Format an IPv6 address */ |
6fba990c RK |
162 | static inline char *format_sockaddr6(const struct sockaddr_in6 *sin6) { |
163 | char buffer[1024], *r; | |
164 | ||
165 | if(sin6->sin6_port) | |
166 | byte_xasprintf(&r, "%s port %u", | |
167 | inet_ntop(sin6->sin6_family, &sin6->sin6_addr, | |
168 | buffer, sizeof buffer), | |
169 | ntohs(sin6->sin6_port)); | |
170 | else | |
171 | byte_xasprintf(&r, "%s", | |
172 | inet_ntop(sin6->sin6_family, &sin6->sin6_addr, | |
173 | buffer, sizeof buffer)); | |
174 | return r; | |
175 | } | |
176 | ||
0590cedc | 177 | /** @brief Format a UNIX socket address */ |
6fba990c RK |
178 | static inline char *format_sockaddrun(const struct sockaddr_un *sun) { |
179 | return xstrdup(sun->sun_path); | |
180 | } | |
181 | ||
0590cedc RK |
182 | /** @brief Construct a text description a sockaddr |
183 | * @param sa Socket address | |
184 | * @return Human-readable form of address | |
185 | */ | |
6fba990c RK |
186 | char *format_sockaddr(const struct sockaddr *sa) { |
187 | switch(sa->sa_family) { | |
188 | case AF_INET: | |
189 | return format_sockaddr4((const struct sockaddr_in *)sa); | |
190 | case AF_INET6: | |
191 | return format_sockaddr6((const struct sockaddr_in6 *)sa); | |
192 | case AF_UNIX: | |
193 | return format_sockaddrun((const struct sockaddr_un *)sa); | |
194 | default: | |
195 | return 0; | |
196 | } | |
197 | } | |
198 | ||
460b9539 | 199 | /* |
200 | Local Variables: | |
201 | c-basic-offset:2 | |
202 | comment-column:40 | |
203 | End: | |
204 | */ |