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 2 of the License, or
8 * (at your option) any later version.
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.
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
21 * @brief Socket address support */
28 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <netinet/in.h>
32 #include <arpa/inet.h>
37 #include "configuration.h"
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
47 * This converts one or two strings into an address specification suitable
48 * for passing to socket(), bind() etc.
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.
54 * @p namep is used to return a description of the address suitable
55 * for use in log messages.
57 * If an error occurs a message is logged and a null pointer returned.
59 struct addrinfo
*get_address(const struct stringlist
*a
,
60 const struct addrinfo
*pref
,
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
));
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
));
82 error(0, "invalid network address specification (n=%d)", a
->n
);
85 if(!res
|| (pref
&& res
->ai_socktype
!= pref
->ai_socktype
)) {
86 error(0, "getaddrinfo didn't give us a suitable socket address");
96 /** @brief Comparison function for address information
98 * Suitable for qsort().
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
;
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_family
) {
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
;
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
));
123 error(0, "unsupported protocol family %d", a
->ai_protocol
);
124 return memcmp(a
->ai_addr
, b
->ai_addr
, a
->ai_addrlen
); /* kludge */
128 /** @brief Return nonzero if @p sin4 is an IPv4 multicast address */
129 static inline int multicast4(const struct sockaddr_in
*sin4
) {
130 return IN_MULTICAST(ntohl(sin4
->sin_addr
.s_addr
));
133 /** @brief Return nonzero if @p sin6 is an IPv6 multicast address */
134 static inline int multicast6(const struct sockaddr_in6
*sin6
) {
135 return IN6_IS_ADDR_MULTICAST(&sin6
->sin6_addr
);
138 /** @brief Return true if @p sa represents a multicast address */
139 int multicast(const struct sockaddr
*sa
) {
140 switch(sa
->sa_family
) {
142 return multicast4((const struct sockaddr_in
*)sa
);
144 return multicast6((const struct sockaddr_in6
*)sa
);
150 /** @brief Format an IPv4 address */
151 static inline char *format_sockaddr4(const struct sockaddr_in
*sin4
) {
152 char buffer
[1024], *r
;
155 byte_xasprintf(&r
, "%s port %u",
156 inet_ntop(sin4
->sin_family
, &sin4
->sin_addr
,
157 buffer
, sizeof buffer
),
158 ntohs(sin4
->sin_port
));
160 byte_xasprintf(&r
, "%s",
161 inet_ntop(sin4
->sin_family
, &sin4
->sin_addr
,
162 buffer
, sizeof buffer
));
166 /** @brief Format an IPv6 address */
167 static inline char *format_sockaddr6(const struct sockaddr_in6
*sin6
) {
168 char buffer
[1024], *r
;
171 byte_xasprintf(&r
, "%s port %u",
172 inet_ntop(sin6
->sin6_family
, &sin6
->sin6_addr
,
173 buffer
, sizeof buffer
),
174 ntohs(sin6
->sin6_port
));
176 byte_xasprintf(&r
, "%s",
177 inet_ntop(sin6
->sin6_family
, &sin6
->sin6_addr
,
178 buffer
, sizeof buffer
));
182 /** @brief Format a UNIX socket address */
183 static inline char *format_sockaddrun(const struct sockaddr_un
*sun
) {
184 return xstrdup(sun
->sun_path
);
187 /** @brief Construct a text description a sockaddr
188 * @param sa Socket address
189 * @return Human-readable form of address
191 char *format_sockaddr(const struct sockaddr
*sa
) {
192 switch(sa
->sa_family
) {
194 return format_sockaddr4((const struct sockaddr_in
*)sa
);
196 return format_sockaddr6((const struct sockaddr_in6
*)sa
);
198 return format_sockaddrun((const struct sockaddr_un
*)sa
);