Get rid of PATH_MAX and MAXPATHLEN
[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 #ifdef HAVE_ABSTRACT_SOCKETS
144     int fd;
145 #endif
146     static const char unix_base[] = "/tmp/.X11-unix/X";
147     const char *base = unix_base;
148     size_t filelen;
149     char *file = NULL;
150     int actual_filelen;
151
152     if(*host)
153     {
154 #ifdef HAVE_LAUNCHD
155         if(strncmp(host, "/tmp/launch", 11) == 0) {
156             base = host;
157         } else {
158 #endif
159
160 #ifdef DNETCONN
161         /* DECnet displays have two colons, so _xcb_parse_display will have
162            left one at the end.  However, an IPv6 address can end with *two*
163            colons, so only treat this as a DECnet display if host ends with
164            exactly one colon. */
165         char *colon = strchr(host, ':');
166         if(colon && *(colon+1) == '\0')
167         {
168             *colon = '\0';
169             return _xcb_open_decnet(host, protocol, display);
170         }
171         else
172 #endif
173             if (protocol
174                 || strcmp("unix",host)) { /* follow the old unix: rule */
175
176                 /* display specifies TCP */
177                 unsigned short port = X_TCP_PORT + display;
178                 return _xcb_open_tcp(host, protocol, port);
179             }
180 #ifdef HAVE_LAUNCHD
181         }
182 #endif
183     }
184
185     filelen = strlen(base) + 1 + sizeof(display) * 3 + 1;
186     file = malloc(filelen);
187     if(file == NULL)
188         return -1;
189
190     /* display specifies Unix socket */
191 #ifdef HAVE_LAUNCHD
192     if(base == host)
193         actual_filelen = snprintf(file, filelen, "%s:%d", base, display);
194     else
195 #endif
196         actual_filelen = snprintf(file, filelen, "%s%d", base, display);
197     if(actual_filelen < 0)
198     {
199         free(file);
200         return -1;
201     }
202     /* snprintf may truncate the file */
203     filelen = MIN(actual_filelen, filelen - 1);
204 #ifdef HAVE_ABSTRACT_SOCKETS
205     fd = _xcb_open_abstract(protocol, file, filelen);
206     if (fd >= 0 || (errno != ENOENT && errno != ECONNREFUSED))
207     {
208         free(file);
209         return fd;
210     }
211
212 #endif
213     fd = _xcb_open_unix(protocol, file);
214     free(file);
215
216     return fd;
217 }
218
219 static int _xcb_socket(int family, int type, int proto)
220 {
221     int fd;
222
223 #ifdef SOCK_CLOEXEC
224     fd = socket(family, type | SOCK_CLOEXEC, proto);
225     if (fd == -1 && errno == EINVAL)
226 #endif
227     {
228         fd = socket(family, type, proto);
229         if (fd >= 0)
230             fcntl(fd, F_SETFD, FD_CLOEXEC);
231     }
232     return fd;
233 }
234
235 #ifdef DNETCONN
236 static int _xcb_open_decnet(const char *host, const char *protocol, const unsigned short port)
237 {
238     int fd;
239     struct sockaddr_dn addr;
240     struct accessdata_dn accessdata;
241     struct nodeent *nodeaddr = getnodebyname(host);
242
243     if(!nodeaddr)
244         return -1;
245     if (protocol && strcmp("dnet",protocol))
246         return -1;
247     addr.sdn_family = AF_DECnet;
248
249     addr.sdn_add.a_len = nodeaddr->n_length;
250     memcpy(addr.sdn_add.a_addr, nodeaddr->n_addr, addr.sdn_add.a_len);
251
252     addr.sdn_objnamel = sprintf((char *)addr.sdn_objname, "X$X%d", port);
253     if(addr.sdn_objnamel < 0)
254         return -1;
255     addr.sdn_objnum = 0;
256
257     fd = _xcb_socket(PF_DECnet, SOCK_STREAM, 0);
258     if(fd == -1)
259         return -1;
260
261     memset(&accessdata, 0, sizeof(accessdata));
262     accessdata.acc_accl = sprintf((char*)accessdata.acc_acc, "%d", getuid());
263     if(accessdata.acc_accl < 0)
264         return -1;
265     setsockopt(fd, DNPROTO_NSP, SO_CONACCESS, &accessdata, sizeof(accessdata));
266
267     if(connect(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1) {
268         close(fd);
269         return -1;
270     }
271     return fd;
272 }
273 #endif
274
275 static int _xcb_open_tcp(char *host, char *protocol, const unsigned short port)
276 {
277     int fd = -1;
278     struct addrinfo hints;
279     char service[6]; /* "65535" with the trailing '\0' */
280     struct addrinfo *results, *addr;
281     char *bracket;
282
283     if (protocol && strcmp("tcp",protocol))
284         return -1;
285
286     memset(&hints, 0, sizeof(hints));
287 #ifdef AI_ADDRCONFIG
288     hints.ai_flags |= AI_ADDRCONFIG;
289 #endif
290 #ifdef AI_NUMERICSERV
291     hints.ai_flags |= AI_NUMERICSERV;
292 #endif
293     hints.ai_family = AF_UNSPEC;
294     hints.ai_socktype = SOCK_STREAM;
295
296 #ifdef AF_INET6
297     /* Allow IPv6 addresses enclosed in brackets. */
298     if(host[0] == '[' && (bracket = strrchr(host, ']')) && bracket[1] == '\0')
299     {
300         *bracket = '\0';
301         ++host;
302         hints.ai_flags |= AI_NUMERICHOST;
303         hints.ai_family = AF_INET6;
304     }
305 #endif
306
307     snprintf(service, sizeof(service), "%hu", port);
308     if(getaddrinfo(host, service, &hints, &results))
309         /* FIXME: use gai_strerror, and fill in error connection */
310         return -1;
311
312     for(addr = results; addr; addr = addr->ai_next)
313     {
314         fd = _xcb_socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
315         if(fd >= 0) {
316             int on = 1;
317             setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on));
318             setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof(on));
319
320             if (connect(fd, addr->ai_addr, addr->ai_addrlen) >= 0)
321                 break;
322             close(fd);
323             fd = -1;
324         }
325     }
326     freeaddrinfo(results);
327     return fd;
328 }
329
330 static int _xcb_open_unix(char *protocol, const char *file)
331 {
332     int fd;
333     struct sockaddr_un addr;
334
335     if (protocol && strcmp("unix",protocol))
336         return -1;
337
338     strcpy(addr.sun_path, file);
339     addr.sun_family = AF_UNIX;
340 #ifdef HAVE_SOCKADDR_SUN_LEN
341     addr.sun_len = SUN_LEN(&addr);
342 #endif
343     fd = _xcb_socket(AF_UNIX, SOCK_STREAM, 0);
344     if(fd == -1)
345         return -1;
346     if(connect(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1) {
347         close(fd);
348         return -1;
349     }
350     return fd;
351 }
352
353 #ifdef HAVE_ABSTRACT_SOCKETS
354 static int _xcb_open_abstract(char *protocol, const char *file, size_t filelen)
355 {
356     int fd;
357     struct sockaddr_un addr = {0};
358     socklen_t namelen;
359
360     if (protocol && strcmp("unix",protocol))
361         return -1;
362
363     strcpy(addr.sun_path + 1, file);
364     addr.sun_family = AF_UNIX;
365     namelen = offsetof(struct sockaddr_un, sun_path) + 1 + filelen;
366 #ifdef HAVE_SOCKADDR_SUN_LEN
367     addr.sun_len = 1 + filelen;
368 #endif
369     fd = _xcb_socket(AF_UNIX, SOCK_STREAM, 0);
370     if (fd == -1)
371         return -1;
372     if (connect(fd, (struct sockaddr *) &addr, namelen) == -1) {
373         close(fd);
374         return -1;
375     }
376     return fd;
377 }
378 #endif
379
380 xcb_connection_t *xcb_connect(const char *displayname, int *screenp)
381 {
382     return xcb_connect_to_display_with_auth_info(displayname, NULL, screenp);
383 }
384
385 xcb_connection_t *xcb_connect_to_display_with_auth_info(const char *displayname, xcb_auth_info_t *auth, int *screenp)
386 {
387     int fd, display = 0;
388     char *host;
389     char *protocol;
390     xcb_auth_info_t ourauth;
391     xcb_connection_t *c;
392
393     int parsed = _xcb_parse_display(displayname, &host, &protocol, &display, screenp);
394     
395     if(!parsed)
396         return (xcb_connection_t *) &error_connection;
397     else
398         fd = _xcb_open(host, protocol, display);
399     free(host);
400
401     if(fd == -1)
402         return (xcb_connection_t *) &error_connection;
403
404     if(auth)
405         return xcb_connect_to_fd(fd, auth);
406
407     if(_xcb_get_auth_info(fd, &ourauth, display))
408     {
409         c = xcb_connect_to_fd(fd, &ourauth);
410         free(ourauth.name);
411         free(ourauth.data);
412     }
413     else
414         c = xcb_connect_to_fd(fd, 0);
415
416     return c;
417 }