Only use AI_NUMERICSERV if defined.
[free-sw/xcb/libxcb] / src / xcb_util.c
1 /* Copyright (C) 2001-2004 Bart Massey and Jamey Sharp.
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining a
4  * copy of this software and associated documentation files (the "Software"),
5  * to deal in the Software without restriction, including without limitation
6  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
7  * and/or sell copies of the Software, and to permit persons to whom the
8  * Software is furnished to do so, subject to the following conditions:
9  * 
10  * The above copyright notice and this permission notice shall be included in
11  * all copies or substantial portions of the Software.
12  * 
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16  * AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
17  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19  * 
20  * Except as contained in this notice, the names of the authors or their
21  * institutions shall not be used in advertising or otherwise to promote the
22  * sale, use or other dealings in this Software without prior written
23  * authorization from the authors.
24  */
25
26 /* Utility functions implementable using only public APIs. */
27
28 #include <assert.h>
29 #include <sys/types.h>
30 #include <sys/socket.h>
31 #include <sys/un.h>
32 #include <netinet/in.h>
33 #ifdef DNETCONN
34 #include <netdnet/dnetdb.h>
35 #include <netdnet/dn.h>
36 #endif
37 #include <netdb.h>
38 #include <errno.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <unistd.h>
42 #include <string.h>
43
44 #include "xcb.h"
45 #include "xcbext.h"
46 #include "xcbint.h"
47
48 static const int error_connection = 1;
49
50 int xcb_popcount(uint32_t mask)
51 {
52     unsigned long y;
53     y = (mask >> 1) & 033333333333;
54     y = mask - y - ((y >> 1) & 033333333333);
55     return ((y + (y >> 3)) & 030707070707) % 077;
56 }
57
58 int xcb_parse_display(const char *name, char **host, int *displayp, int *screenp)
59 {
60     int len, display, screen;
61     char *colon, *dot, *end;
62     if(!name || !*name)
63         name = getenv("DISPLAY");
64     if(!name)
65         return 0;
66     colon = strrchr(name, ':');
67     if(!colon)
68         return 0;
69     len = colon - name;
70     ++colon;
71     display = strtoul(colon, &dot, 10);
72     if(dot == colon)
73         return 0;
74     if(*dot == '\0')
75         screen = 0;
76     else
77     {
78         if(*dot != '.')
79             return 0;
80         ++dot;
81         screen = strtoul(dot, &end, 10);
82         if(end == dot || *end != '\0')
83             return 0;
84     }
85     /* At this point, the display string is fully parsed and valid, but
86      * the caller's memory is untouched. */
87
88     *host = malloc(len + 1);
89     if(!*host)
90         return 0;
91     memcpy(*host, name, len);
92     (*host)[len] = '\0';
93     *displayp = display;
94     if(screenp)
95         *screenp = screen;
96     return 1;
97 }
98
99 static int _xcb_open_tcp(char *host, const unsigned short port);
100 static int _xcb_open_unix(const char *file);
101 #ifdef DNETCONN
102 static int _xcb_open_decnet(const char *host, const unsigned short port);
103 #endif
104
105 static int _xcb_open(char *host, const int display)
106 {
107     int fd;
108
109     if(*host)
110     {
111 #ifdef DNETCONN
112         /* DECnet displays have two colons, so xcb_parse_display will have left
113            one at the end.  However, an IPv6 address can end with *two* colons,
114            so only treat this as a DECnet display if host ends with exactly one
115            colon. */
116         char *colon = strchr(host, ':');
117         if(colon && *(colon+1) == '\0')
118         {
119             *colon = '\0';
120             fd = _xcb_open_decnet(host, display);
121         }
122         else
123 #endif
124         {
125             /* display specifies TCP */
126             unsigned short port = X_TCP_PORT + display;
127             fd = _xcb_open_tcp(host, port);
128         }
129     }
130     else
131     {
132         /* display specifies Unix socket */
133         static const char base[] = "/tmp/.X11-unix/X";
134         char file[sizeof(base) + 20];
135         snprintf(file, sizeof(file), "%s%d", base, display);
136         fd = _xcb_open_unix(file);
137     }
138
139     return fd;
140 }
141
142 #ifdef DNETCONN
143 static int _xcb_open_decnet(const char *host, const unsigned short port)
144 {
145     int fd;
146     struct sockaddr_dn addr;
147     struct accessdata_dn accessdata;
148     struct nodeent *nodeaddr = getnodebyname(host);
149
150     if(!nodeaddr)
151         return -1;
152     addr.sdn_family = AF_DECnet;
153
154     addr.sdn_add.a_len = nodeaddr->n_length;
155     memcpy(addr.sdn_add.a_addr, nodeaddr->n_addr, addr.sdn_add.a_len);
156
157     sprintf((char *)addr.sdn_objname, "X$X%d", port);
158     addr.sdn_objnamel = strlen((char *)addr.sdn_objname);
159     addr.sdn_objnum = 0;
160
161     fd = socket(PF_DECnet, SOCK_STREAM, 0);
162     if(fd == -1)
163         return -1;
164
165     memset(&accessdata, 0, sizeof(accessdata));
166     sprintf((char*)accessdata.acc_acc, "%d", getuid());
167     accessdata.acc_accl = strlen((char *)accessdata.acc_acc);
168     setsockopt(fd, DNPROTO_NSP, SO_CONACCESS, &accessdata, sizeof(accessdata));
169
170     if(connect(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1)
171         return -1;
172     return fd;
173 }
174 #endif
175
176 static int _xcb_open_tcp(char *host, const unsigned short port)
177 {
178     int fd = -1;
179     struct addrinfo hints = { AI_ADDRCONFIG
180 #ifdef AI_NUMERICSERV
181                               | AI_NUMERICSERV
182 #endif
183                               , AF_UNSPEC, SOCK_STREAM };
184     char service[6]; /* "65535" with the trailing '\0' */
185     struct addrinfo *results, *addr;
186     char *bracket;
187     
188     /* Allow IPv6 addresses enclosed in brackets. */
189     if(host[0] == '[' && (bracket = strrchr(host, ']')) && bracket[1] == '\0')
190     {
191         *bracket = '\0';
192         ++host;
193         hints.ai_flags |= AI_NUMERICHOST;
194         hints.ai_family = AF_INET6;
195     }
196
197     snprintf(service, sizeof(service), "%hu", port);
198     if(getaddrinfo(host, service, &hints, &results))
199         /* FIXME: use gai_strerror, and fill in error connection */
200         return -1;
201
202     for(addr = results; addr; addr = addr->ai_next)
203     {
204         fd = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
205         if(fd >= 0 && connect(fd, addr->ai_addr, addr->ai_addrlen) >= 0)
206             break;
207         fd = -1;
208     }
209     freeaddrinfo(results);
210     return fd;
211 }
212
213 static int _xcb_open_unix(const char *file)
214 {
215     int fd;
216     struct sockaddr_un addr = { AF_UNIX };
217     strcpy(addr.sun_path, file);
218
219     fd = socket(AF_UNIX, SOCK_STREAM, 0);
220     if(fd == -1)
221         return -1;
222     if(connect(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1)
223         return -1;
224     return fd;
225 }
226
227 xcb_connection_t *xcb_connect(const char *displayname, int *screenp)
228 {
229     int fd, display = 0;
230     char *host;
231     xcb_connection_t *c;
232     xcb_auth_info_t auth;
233
234     if(!xcb_parse_display(displayname, &host, &display, screenp))
235         return (xcb_connection_t *) &error_connection;
236     fd = _xcb_open(host, display);
237     free(host);
238     if(fd == -1)
239         return (xcb_connection_t *) &error_connection;
240
241     _xcb_get_auth_info(fd, &auth);
242     c = xcb_connect_to_fd(fd, &auth);
243     free(auth.name);
244     free(auth.data);
245     return c;
246 }
247
248 xcb_connection_t *xcb_connect_to_display_with_auth_info(const char *displayname, xcb_auth_info_t *auth, int *screenp)
249 {
250     int fd, display = 0;
251     char *host;
252
253     if(!xcb_parse_display(displayname, &host, &display, screenp))
254         return (xcb_connection_t *) &error_connection;
255     fd = _xcb_open(host, display);
256     free(host);
257     if(fd == -1)
258         return (xcb_connection_t *) &error_connection;
259
260     return xcb_connect_to_fd(fd, auth);
261 }