1 /* Copyright (C) 2001-2004 Bart Massey and Jamey Sharp.
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:
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
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.
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.
26 /* Utility functions implementable using only public APIs. */
29 #include <sys/types.h>
32 #include <netdnet/dnetdb.h>
33 #include <netdnet/dn.h>
43 #include "xcb_windefs.h"
45 #include <sys/socket.h>
47 #include <netinet/in.h>
48 #include <netinet/tcp.h>
57 int xcb_popcount(uint32_t mask)
60 y = (mask >> 1) & 033333333333;
61 y = mask - y - ((y >> 1) & 033333333333);
62 return ((y + (y >> 3)) & 030707070707) % 077;
65 int xcb_sumof(uint8_t *list, int len)
68 for(i=0; i<len; i++) {
75 static int _xcb_parse_display(const char *name, char **host, char **protocol,
76 int *displayp, int *screenp)
78 int len, display, screen;
79 char *slash, *colon, *dot, *end;
82 name = getenv("DISPLAY");
87 if(strncmp(name, "/tmp/launch", 11) == 0)
91 slash = strrchr(name, '/');
96 *protocol = malloc(len + 1);
99 memcpy(*protocol, name, len);
100 (*protocol)[len] = '\0';
107 colon = strrchr(name, ':');
112 display = strtoul(colon, &dot, 10);
122 screen = strtoul(dot, &end, 10);
123 if(end == dot || *end != '\0')
126 /* At this point, the display string is fully parsed and valid, but
127 * the caller's memory is untouched. */
129 *host = malloc(len + 1);
132 memcpy(*host, name, len);
148 int xcb_parse_display(const char *name, char **host, int *displayp,
151 return _xcb_parse_display(name, host, NULL, displayp, screenp);
154 static int _xcb_open_tcp(const char *host, char *protocol, const unsigned short port);
156 static int _xcb_open_unix(char *protocol, const char *file);
159 static int _xcb_open_decnet(const char *host, char *protocol, const unsigned short port);
161 #ifdef HAVE_ABSTRACT_SOCKETS
162 static int _xcb_open_abstract(char *protocol, const char *file, size_t filelen);
165 static int _xcb_open(const char *host, char *protocol, const int display)
168 static const char unix_base[] = "/tmp/.X11-unix/X";
169 const char *base = unix_base;
175 if(strncmp(host, "/tmp/launch", 11) == 0) {
182 if(*host || protocol)
185 /* DECnet displays have two colons, so _xcb_parse_display will have
186 left one at the end. However, an IPv6 address can end with *two*
187 colons, so only treat this as a DECnet display if host ends with
188 exactly one colon. */
189 char *colon = strchr(host, ':');
190 if(colon && *(colon+1) == '\0')
193 return _xcb_open_decnet(host, protocol, display);
198 || strcmp("unix",host)) { /* follow the old unix: rule */
200 /* display specifies TCP */
201 unsigned short port = X_TCP_PORT + display;
202 return _xcb_open_tcp(host, protocol, port);
207 filelen = strlen(base) + 1 + sizeof(display) * 3 + 1;
208 file = malloc(filelen);
212 /* display specifies Unix socket */
214 if(strncmp(base, "/tmp/launch", 11) == 0)
215 actual_filelen = snprintf(file, filelen, "%s:%d", base, display);
218 actual_filelen = snprintf(file, filelen, "%s%d", base, display);
219 if(actual_filelen < 0)
224 /* snprintf may truncate the file */
225 filelen = MIN(actual_filelen, filelen - 1);
226 #ifdef HAVE_ABSTRACT_SOCKETS
227 fd = _xcb_open_abstract(protocol, file, filelen);
228 if (fd >= 0 || (errno != ENOENT && errno != ECONNREFUSED))
235 fd = _xcb_open_unix(protocol, file);
240 return -1; /* if control reaches here then something has gone wrong */
243 static int _xcb_socket(int family, int type, int proto)
248 fd = socket(family, type | SOCK_CLOEXEC, proto);
249 if (fd == -1 && errno == EINVAL)
252 fd = socket(family, type, proto);
255 fcntl(fd, F_SETFD, FD_CLOEXEC);
262 static int _xcb_open_decnet(const char *host, const char *protocol, const unsigned short port)
265 struct sockaddr_dn addr;
266 struct accessdata_dn accessdata;
267 struct nodeent *nodeaddr = getnodebyname(host);
271 if (protocol && strcmp("dnet",protocol))
273 addr.sdn_family = AF_DECnet;
275 addr.sdn_add.a_len = nodeaddr->n_length;
276 memcpy(addr.sdn_add.a_addr, nodeaddr->n_addr, addr.sdn_add.a_len);
278 addr.sdn_objnamel = sprintf((char *)addr.sdn_objname, "X$X%d", port);
279 if(addr.sdn_objnamel < 0)
283 fd = _xcb_socket(PF_DECnet, SOCK_STREAM, 0);
287 memset(&accessdata, 0, sizeof(accessdata));
288 accessdata.acc_accl = sprintf((char*)accessdata.acc_acc, "%d", getuid());
289 if(accessdata.acc_accl < 0)
291 setsockopt(fd, DNPROTO_NSP, SO_CONACCESS, &accessdata, sizeof(accessdata));
293 if(connect(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1) {
301 static int _xcb_open_tcp(const char *host, char *protocol, const unsigned short port)
304 struct addrinfo hints;
305 char service[6]; /* "65535" with the trailing '\0' */
306 struct addrinfo *results, *addr;
309 if (protocol && strcmp("tcp",protocol) && strcmp("inet",protocol)
311 && strcmp("inet6",protocol)
319 memset(&hints, 0, sizeof(hints));
321 hints.ai_flags |= AI_ADDRCONFIG;
323 #ifdef AI_NUMERICSERV
324 hints.ai_flags |= AI_NUMERICSERV;
326 hints.ai_family = AF_UNSPEC;
327 hints.ai_socktype = SOCK_STREAM;
330 /* Allow IPv6 addresses enclosed in brackets. */
331 if(host[0] == '[' && (bracket = strrchr(host, ']')) && bracket[1] == '\0')
335 hints.ai_flags |= AI_NUMERICHOST;
336 hints.ai_family = AF_INET6;
340 snprintf(service, sizeof(service), "%hu", port);
341 if(getaddrinfo(host, service, &hints, &results))
342 /* FIXME: use gai_strerror, and fill in error connection */
345 for(addr = results; addr; addr = addr->ai_next)
347 fd = _xcb_socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
350 setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on));
351 setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof(on));
353 if (connect(fd, addr->ai_addr, addr->ai_addrlen) >= 0)
359 freeaddrinfo(results);
364 static int _xcb_open_unix(char *protocol, const char *file)
367 struct sockaddr_un addr;
369 if (protocol && strcmp("unix",protocol))
372 strcpy(addr.sun_path, file);
373 addr.sun_family = AF_UNIX;
374 #ifdef HAVE_SOCKADDR_SUN_LEN
375 addr.sun_len = SUN_LEN(&addr);
377 fd = _xcb_socket(AF_UNIX, SOCK_STREAM, 0);
380 if(connect(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1) {
388 #ifdef HAVE_ABSTRACT_SOCKETS
389 static int _xcb_open_abstract(char *protocol, const char *file, size_t filelen)
392 struct sockaddr_un addr = {0};
395 if (protocol && strcmp("unix",protocol))
398 strcpy(addr.sun_path + 1, file);
399 addr.sun_family = AF_UNIX;
400 namelen = offsetof(struct sockaddr_un, sun_path) + 1 + filelen;
401 #ifdef HAVE_SOCKADDR_SUN_LEN
402 addr.sun_len = 1 + filelen;
404 fd = _xcb_socket(AF_UNIX, SOCK_STREAM, 0);
407 if (connect(fd, (struct sockaddr *) &addr, namelen) == -1) {
415 xcb_connection_t *xcb_connect(const char *displayname, int *screenp)
417 return xcb_connect_to_display_with_auth_info(displayname, NULL, screenp);
420 xcb_connection_t *xcb_connect_to_display_with_auth_info(const char *displayname, xcb_auth_info_t *auth, int *screenp)
424 char *protocol = NULL;
425 xcb_auth_info_t ourauth;
428 int parsed = _xcb_parse_display(displayname, &host, &protocol, &display, screenp);
431 c = (xcb_connection_t *) &error_connection;
434 fd = _xcb_open(host, protocol, display);
437 c = (xcb_connection_t *) &error_connection;
442 c = xcb_connect_to_fd(fd, auth);
446 if(_xcb_get_auth_info(fd, &ourauth, display))
448 c = xcb_connect_to_fd(fd, &ourauth);
453 c = xcb_connect_to_fd(fd, 0);