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