chiark / gitweb /
update control buttons when disconnection detected
[disorder] / lib / addr.c
1 /*
2  * This file is part of DisOrder.
3  * Copyright (C) 2004 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 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  */
20 /** @file lib/addr.c Socket address support */
21
22 #include <config.h>
23 #include "types.h"
24
25 #include <stdio.h>
26 #include <string.h>
27 #include <sys/types.h>
28 #include <sys/socket.h>
29 #include <netinet/in.h>
30 #include <netdb.h>
31
32 #include "log.h"
33 #include "printf.h"
34 #include "configuration.h"
35 #include "addr.h"
36
37 /** @brief Convert a pair of strings to an address
38  * @param a Pointer to string list
39  * @param pref Hints structure for getaddrinfo, or NULL
40  * @param namep Where to store address description, or NULL
41  * @return Address info structure or NULL on error
42  *
43  * This converts one or two strings into an address specification suitable
44  * for passing to socket(), bind() etc.
45  *
46  * If there is only one string then it is assumed to be the service
47  * name (port number).  If there are two then the first is the host
48  * name and the second the service name.
49  *
50  * @p namep is used to return a description of the address suitable
51  * for use in log messages.
52  *
53  * If an error occurs a message is logged and a null pointer returned.
54  */
55 struct addrinfo *get_address(const struct stringlist *a,
56                              const struct addrinfo *pref,
57                              char **namep) {
58   struct addrinfo *res;
59   char *name;
60   int rc;
61
62   switch(a->n) {  
63   case 1:
64     byte_xasprintf(&name, "host * service %s", a->s[0]);
65     if((rc = getaddrinfo(0, a->s[0], pref, &res))) {
66       error(0, "getaddrinfo %s: %s", a->s[0], gai_strerror(rc));
67       return 0;
68     }
69     break;
70   case 2:
71     byte_xasprintf(&name, "host %s service %s", a->s[0], a->s[1]);
72     if((rc = getaddrinfo(a->s[0], a->s[1], pref, &res))) {
73       error(0, "getaddrinfo %s %s: %s", a->s[0], a->s[1], gai_strerror(rc));
74       return 0;
75     }
76     break;
77   default:
78     error(0, "invalid network address specification (n=%d)", a->n);
79     return 0;
80   }
81   if(!res || (pref && res->ai_socktype != pref->ai_socktype)) {
82     error(0, "getaddrinfo didn't give us a suitable socket address");
83     if(res)
84       freeaddrinfo(res);
85     return 0;
86   }
87   if(namep)
88     *namep = name;
89   return res;
90 }
91
92 /** @brief Comparison function for address information
93  *
94  * Suitable for qsort().
95  */
96 int addrinfocmp(const struct addrinfo *a,
97                 const struct addrinfo *b) {
98   const struct sockaddr_in *ina, *inb;
99   const struct sockaddr_in6 *in6a, *in6b;
100   
101   if(a->ai_family != b->ai_family) return a->ai_family - b->ai_family;
102   if(a->ai_socktype != b->ai_socktype) return a->ai_socktype - b->ai_socktype;
103   if(a->ai_protocol != b->ai_protocol) return a->ai_protocol - b->ai_protocol;
104   switch(a->ai_protocol) {
105   case PF_INET:
106     ina = (const struct sockaddr_in *)a->ai_addr;
107     inb = (const struct sockaddr_in *)b->ai_addr;
108     if(ina->sin_port != inb->sin_port) return ina->sin_port - inb->sin_port;
109     return ina->sin_addr.s_addr - inb->sin_addr.s_addr;
110     break;
111   case PF_INET6:
112     in6a = (const struct sockaddr_in6 *)a->ai_addr;
113     in6b = (const struct sockaddr_in6 *)b->ai_addr;
114     if(in6a->sin6_port != in6b->sin6_port)
115       return in6a->sin6_port - in6b->sin6_port;
116     return memcmp(&in6a->sin6_addr, &in6b->sin6_addr,
117                   sizeof (struct in6_addr));
118   default:
119     error(0, "unsupported protocol family %d", a->ai_protocol);
120     return memcmp(a->ai_addr, b->ai_addr, a->ai_addrlen); /* kludge */
121   }
122 }
123   
124 /*
125 Local Variables:
126 c-basic-offset:2
127 comment-column:40
128 End:
129 */