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