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