Fix GCC error on undeclared variable when not using abstract socket
[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 <limits.h>
32 #include <sys/un.h>
33 #include <netinet/in.h>
34 #include <netinet/tcp.h>
35 #ifdef DNETCONN
36 #include <netdnet/dnetdb.h>
37 #include <netdnet/dn.h>
38 #endif
39 #include <netdb.h>
40 #include <errno.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <stddef.h>
44 #include <unistd.h>
45 #include <fcntl.h>
46 #include <string.h>
47
48 #include "xcb.h"
49 #include "xcbext.h"
50 #include "xcbint.h"
51
52 static const int error_connection = 1;
53
54 int xcb_popcount(uint32_t mask)
55 {
56     uint32_t y;
57     y = (mask >> 1) & 033333333333;
58     y = mask - y - ((y >> 1) & 033333333333);
59     return ((y + (y >> 3)) & 030707070707) % 077;
60 }
61
62 static int _xcb_parse_display(const char *name, char **host, char **protocol,
63                       int *displayp, int *screenp)
64 {
65     int len, display, screen;
66     char *slash, *colon, *dot, *end;
67     if(!name || !*name)
68         name = getenv("DISPLAY");
69     if(!name)
70         return 0;
71
72 #ifdef HAVE_LAUNCHD
73     if(strncmp(name, "/tmp/launch", 11) == 0)
74         slash = NULL;
75     else
76 #endif
77     slash = strrchr(name, '/');
78
79     if (slash) {
80         len = slash - name;
81         if (protocol) {
82             *protocol = malloc(len + 1);
83             if(!*protocol)
84                 return 0;
85             memcpy(*protocol, name, len);
86             (*protocol)[len] = '\0';
87         }
88         name = slash + 1;
89     } else
90         if (protocol)
91             *protocol = NULL;
92
93     colon = strrchr(name, ':');
94     if(!colon)
95         return 0;
96     len = colon - name;
97     ++colon;
98     display = strtoul(colon, &dot, 10);
99     if(dot == colon)
100         return 0;
101     if(*dot == '\0')
102         screen = 0;
103     else
104     {
105         if(*dot != '.')
106             return 0;
107         ++dot;
108         screen = strtoul(dot, &end, 10);
109         if(end == dot || *end != '\0')
110             return 0;
111     }
112     /* At this point, the display string is fully parsed and valid, but
113      * the caller's memory is untouched. */
114
115     *host = malloc(len + 1);
116     if(!*host)
117         return 0;
118     memcpy(*host, name, len);
119     (*host)[len] = '\0';
120     *displayp = display;
121     if(screenp)
122         *screenp = screen;
123     return 1;
124 }
125
126 int xcb_parse_display(const char *name, char **host, int *displayp,
127                              int *screenp)
128 {
129     return _xcb_parse_display(name, host, NULL, displayp, screenp);
130 }
131
132 static int _xcb_open_tcp(char *host, char *protocol, const unsigned short port);
133 static int _xcb_open_unix(char *protocol, const char *file);
134 #ifdef DNETCONN
135 static int _xcb_open_decnet(const char *host, char *protocol, const unsigned short port);
136 #endif
137 #ifdef HAVE_ABSTRACT_SOCKETS
138 static int _xcb_open_abstract(char *protocol, const char *file, size_t filelen);
139 #endif
140
141 static int _xcb_open(char *host, char *protocol, const int display)
142 {
143     int fd;
144     static const char unix_base[] = "/tmp/.X11-unix/X";
145     const char *base = unix_base;
146     size_t filelen;
147     char *file = NULL;
148     int actual_filelen;
149
150     if(*host)
151     {
152 #ifdef HAVE_LAUNCHD
153         if(strncmp(host, "/tmp/launch", 11) == 0) {
154             base = host;
155         } else {
156 #endif
157
158 #ifdef DNETCONN
159         /* DECnet displays have two colons, so _xcb_parse_display will have
160            left one at the end.  However, an IPv6 address can end with *two*
161            colons, so only treat this as a DECnet display if host ends with
162            exactly one colon. */
163         char *colon = strchr(host, ':');
164         if(colon && *(colon+1) == '\0')
165         {
166             *colon = '\0';
167             return _xcb_open_decnet(host, protocol, display);
168         }
169         else
170 #endif
171             if (protocol
172                 || strcmp("unix",host)) { /* follow the old unix: rule */
173
174                 /* display specifies TCP */
175                 unsigned short port = X_TCP_PORT + display;
176                 return _xcb_open_tcp(host, protocol, port);
177             }
178 #ifdef HAVE_LAUNCHD
179         }
180 #endif
181     }
182
183     filelen = strlen(base) + 1 + sizeof(display) * 3 + 1;
184     file = malloc(filelen);
185     if(file == NULL)
186         return -1;
187
188     /* display specifies Unix socket */
189 #ifdef HAVE_LAUNCHD
190     if(base == host)
191         actual_filelen = snprintf(file, filelen, "%s:%d", base, display);
192     else
193 #endif
194         actual_filelen = snprintf(file, filelen, "%s%d", base, display);
195     if(actual_filelen < 0)
196     {
197         free(file);
198         return -1;
199     }
200     /* snprintf may truncate the file */
201     filelen = MIN(actual_filelen, filelen - 1);
202 #ifdef HAVE_ABSTRACT_SOCKETS
203     fd = _xcb_open_abstract(protocol, file, filelen);
204     if (fd >= 0 || (errno != ENOENT && errno != ECONNREFUSED))
205     {
206         free(file);
207         return fd;
208     }
209
210 #endif
211     fd = _xcb_open_unix(protocol, file);
212     free(file);
213
214     return fd;
215 }
216
217 static int _xcb_socket(int family, int type, int proto)
218 {
219     int fd;
220
221 #ifdef SOCK_CLOEXEC
222     fd = socket(family, type | SOCK_CLOEXEC, proto);
223     if (fd == -1 && errno == EINVAL)
224 #endif
225     {
226         fd = socket(family, type, proto);
227         if (fd >= 0)
228             fcntl(fd, F_SETFD, FD_CLOEXEC);
229     }
230     return fd;
231 }
232
233 #ifdef DNETCONN
234 static int _xcb_open_decnet(const char *host, const char *protocol, const unsigned short port)
235 {
236     int fd;
237     struct sockaddr_dn addr;
238     struct accessdata_dn accessdata;
239     struct nodeent *nodeaddr = getnodebyname(host);
240
241     if(!nodeaddr)
242         return -1;
243     if (protocol && strcmp("dnet",protocol))
244         return -1;
245     addr.sdn_family = AF_DECnet;
246
247     addr.sdn_add.a_len = nodeaddr->n_length;
248     memcpy(addr.sdn_add.a_addr, nodeaddr->n_addr, addr.sdn_add.a_len);
249
250     addr.sdn_objnamel = sprintf((char *)addr.sdn_objname, "X$X%d", port);
251     if(addr.sdn_objnamel < 0)
252         return -1;
253     addr.sdn_objnum = 0;
254
255     fd = _xcb_socket(PF_DECnet, SOCK_STREAM, 0);
256     if(fd == -1)
257         return -1;
258
259     memset(&accessdata, 0, sizeof(accessdata));
260     accessdata.acc_accl = sprintf((char*)accessdata.acc_acc, "%d", getuid());
261     if(accessdata.acc_accl < 0)
262         return -1;
263     setsockopt(fd, DNPROTO_NSP, SO_CONACCESS, &accessdata, sizeof(accessdata));
264
265     if(connect(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1) {
266         close(fd);
267         return -1;
268     }
269     return fd;
270 }
271 #endif
272
273 static int _xcb_open_tcp(char *host, char *protocol, const unsigned short port)
274 {
275     int fd = -1;
276     struct addrinfo hints;
277     char service[6]; /* "65535" with the trailing '\0' */
278     struct addrinfo *results, *addr;
279     char *bracket;
280
281     if (protocol && strcmp("tcp",protocol))
282         return -1;
283
284     memset(&hints, 0, sizeof(hints));
285 #ifdef AI_ADDRCONFIG
286     hints.ai_flags |= AI_ADDRCONFIG;
287 #endif
288 #ifdef AI_NUMERICSERV
289     hints.ai_flags |= AI_NUMERICSERV;
290 #endif
291     hints.ai_family = AF_UNSPEC;
292     hints.ai_socktype = SOCK_STREAM;
293
294 #ifdef AF_INET6
295     /* Allow IPv6 addresses enclosed in brackets. */
296     if(host[0] == '[' && (bracket = strrchr(host, ']')) && bracket[1] == '\0')
297     {
298         *bracket = '\0';
299         ++host;
300         hints.ai_flags |= AI_NUMERICHOST;
301         hints.ai_family = AF_INET6;
302     }
303 #endif
304
305     snprintf(service, sizeof(service), "%hu", port);
306     if(getaddrinfo(host, service, &hints, &results))
307         /* FIXME: use gai_strerror, and fill in error connection */
308         return -1;
309
310     for(addr = results; addr; addr = addr->ai_next)
311     {
312         fd = _xcb_socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
313         if(fd >= 0) {
314             int on = 1;
315             setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on));
316             setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof(on));
317
318             if (connect(fd, addr->ai_addr, addr->ai_addrlen) >= 0)
319                 break;
320             close(fd);
321             fd = -1;
322         }
323     }
324     freeaddrinfo(results);
325     return fd;
326 }
327
328 static int _xcb_open_unix(char *protocol, const char *file)
329 {
330     int fd;
331     struct sockaddr_un addr;
332
333     if (protocol && strcmp("unix",protocol))
334         return -1;
335
336     strcpy(addr.sun_path, file);
337     addr.sun_family = AF_UNIX;
338 #ifdef HAVE_SOCKADDR_SUN_LEN
339     addr.sun_len = SUN_LEN(&addr);
340 #endif
341     fd = _xcb_socket(AF_UNIX, SOCK_STREAM, 0);
342     if(fd == -1)
343         return -1;
344     if(connect(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1) {
345         close(fd);
346         return -1;
347     }
348     return fd;
349 }
350
351 #ifdef HAVE_ABSTRACT_SOCKETS
352 static int _xcb_open_abstract(char *protocol, const char *file, size_t filelen)
353 {
354     int fd;
355     struct sockaddr_un addr = {0};
356     socklen_t namelen;
357
358     if (protocol && strcmp("unix",protocol))
359         return -1;
360
361     strcpy(addr.sun_path + 1, file);
362     addr.sun_family = AF_UNIX;
363     namelen = offsetof(struct sockaddr_un, sun_path) + 1 + filelen;
364 #ifdef HAVE_SOCKADDR_SUN_LEN
365     addr.sun_len = 1 + filelen;
366 #endif
367     fd = _xcb_socket(AF_UNIX, SOCK_STREAM, 0);
368     if (fd == -1)
369         return -1;
370     if (connect(fd, (struct sockaddr *) &addr, namelen) == -1) {
371         close(fd);
372         return -1;
373     }
374     return fd;
375 }
376 #endif
377
378 xcb_connection_t *xcb_connect(const char *displayname, int *screenp)
379 {
380     return xcb_connect_to_display_with_auth_info(displayname, NULL, screenp);
381 }
382
383 xcb_connection_t *xcb_connect_to_display_with_auth_info(const char *displayname, xcb_auth_info_t *auth, int *screenp)
384 {
385     int fd, display = 0;
386     char *host;
387     char *protocol;
388     xcb_auth_info_t ourauth;
389     xcb_connection_t *c;
390
391     int parsed = _xcb_parse_display(displayname, &host, &protocol, &display, screenp);
392     
393     if(!parsed)
394         return (xcb_connection_t *) &error_connection;
395     else
396         fd = _xcb_open(host, protocol, display);
397     free(host);
398
399     if(fd == -1)
400         return (xcb_connection_t *) &error_connection;
401
402     if(auth)
403         return xcb_connect_to_fd(fd, auth);
404
405     if(_xcb_get_auth_info(fd, &ourauth, display))
406     {
407         c = xcb_connect_to_fd(fd, &ourauth);
408         free(ourauth.name);
409         free(ourauth.data);
410     }
411     else
412         c = xcb_connect_to_fd(fd, 0);
413
414     return c;
415 }