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 static const int error_connection = 1;
59 int xcb_popcount(uint32_t mask)
62 y = (mask >> 1) & 033333333333;
63 y = mask - y - ((y >> 1) & 033333333333);
64 return ((y + (y >> 3)) & 030707070707) % 077;
67 static int _xcb_parse_display(const char *name, char **host, char **protocol,
68 int *displayp, int *screenp)
70 int len, display, screen;
71 char *slash, *colon, *dot, *end;
74 name = getenv("DISPLAY");
79 if(strncmp(name, "/tmp/launch", 11) == 0)
83 slash = strrchr(name, '/');
88 *protocol = malloc(len + 1);
91 memcpy(*protocol, name, len);
92 (*protocol)[len] = '\0';
99 colon = strrchr(name, ':');
104 display = strtoul(colon, &dot, 10);
114 screen = strtoul(dot, &end, 10);
115 if(end == dot || *end != '\0')
118 /* At this point, the display string is fully parsed and valid, but
119 * the caller's memory is untouched. */
121 *host = malloc(len + 1);
124 memcpy(*host, name, len);
140 int xcb_parse_display(const char *name, char **host, int *displayp,
143 return _xcb_parse_display(name, host, NULL, displayp, screenp);
146 static int _xcb_open_tcp(const char *host, char *protocol, const unsigned short port);
148 static int _xcb_open_unix(char *protocol, const char *file);
151 static int _xcb_open_decnet(const char *host, char *protocol, const unsigned short port);
153 #ifdef HAVE_ABSTRACT_SOCKETS
154 static int _xcb_open_abstract(char *protocol, const char *file, size_t filelen);
157 static int _xcb_open(const char *host, char *protocol, const int display)
160 static const char unix_base[] = "/tmp/.X11-unix/X";
161 const char *base = unix_base;
167 if(strncmp(host, "/tmp/launch", 11) == 0) {
174 if(*host || protocol)
177 /* DECnet displays have two colons, so _xcb_parse_display will have
178 left one at the end. However, an IPv6 address can end with *two*
179 colons, so only treat this as a DECnet display if host ends with
180 exactly one colon. */
181 char *colon = strchr(host, ':');
182 if(colon && *(colon+1) == '\0')
185 return _xcb_open_decnet(host, protocol, display);
190 || strcmp("unix",host)) { /* follow the old unix: rule */
192 /* display specifies TCP */
193 unsigned short port = X_TCP_PORT + display;
194 return _xcb_open_tcp(host, protocol, port);
199 filelen = strlen(base) + 1 + sizeof(display) * 3 + 1;
200 file = malloc(filelen);
204 /* display specifies Unix socket */
206 if(strncmp(base, "/tmp/launch", 11) == 0)
207 actual_filelen = snprintf(file, filelen, "%s:%d", base, display);
210 actual_filelen = snprintf(file, filelen, "%s%d", base, display);
211 if(actual_filelen < 0)
216 /* snprintf may truncate the file */
217 filelen = MIN(actual_filelen, filelen - 1);
218 #ifdef HAVE_ABSTRACT_SOCKETS
219 fd = _xcb_open_abstract(protocol, file, filelen);
220 if (fd >= 0 || (errno != ENOENT && errno != ECONNREFUSED))
227 fd = _xcb_open_unix(protocol, file);
232 return -1; /* if control reaches here then something has gone wrong */
235 static int _xcb_socket(int family, int type, int proto)
240 fd = socket(family, type | SOCK_CLOEXEC, proto);
241 if (fd == -1 && errno == EINVAL)
244 fd = socket(family, type, proto);
247 fcntl(fd, F_SETFD, FD_CLOEXEC);
254 static int _xcb_open_decnet(const char *host, const char *protocol, const unsigned short port)
257 struct sockaddr_dn addr;
258 struct accessdata_dn accessdata;
259 struct nodeent *nodeaddr = getnodebyname(host);
263 if (protocol && strcmp("dnet",protocol))
265 addr.sdn_family = AF_DECnet;
267 addr.sdn_add.a_len = nodeaddr->n_length;
268 memcpy(addr.sdn_add.a_addr, nodeaddr->n_addr, addr.sdn_add.a_len);
270 addr.sdn_objnamel = sprintf((char *)addr.sdn_objname, "X$X%d", port);
271 if(addr.sdn_objnamel < 0)
275 fd = _xcb_socket(PF_DECnet, SOCK_STREAM, 0);
279 memset(&accessdata, 0, sizeof(accessdata));
280 accessdata.acc_accl = sprintf((char*)accessdata.acc_acc, "%d", getuid());
281 if(accessdata.acc_accl < 0)
283 setsockopt(fd, DNPROTO_NSP, SO_CONACCESS, &accessdata, sizeof(accessdata));
285 if(connect(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1) {
293 static int _xcb_open_tcp(const char *host, char *protocol, const unsigned short port)
296 struct addrinfo hints;
297 char service[6]; /* "65535" with the trailing '\0' */
298 struct addrinfo *results, *addr;
301 if (protocol && strcmp("tcp",protocol) && strcmp("inet",protocol)
303 && strcmp("inet6",protocol)
311 memset(&hints, 0, sizeof(hints));
313 hints.ai_flags |= AI_ADDRCONFIG;
315 #ifdef AI_NUMERICSERV
316 hints.ai_flags |= AI_NUMERICSERV;
318 hints.ai_family = AF_UNSPEC;
319 hints.ai_socktype = SOCK_STREAM;
322 /* Allow IPv6 addresses enclosed in brackets. */
323 if(host[0] == '[' && (bracket = strrchr(host, ']')) && bracket[1] == '\0')
327 hints.ai_flags |= AI_NUMERICHOST;
328 hints.ai_family = AF_INET6;
332 snprintf(service, sizeof(service), "%hu", port);
333 if(getaddrinfo(host, service, &hints, &results))
334 /* FIXME: use gai_strerror, and fill in error connection */
337 for(addr = results; addr; addr = addr->ai_next)
339 fd = _xcb_socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
342 setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on));
343 setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof(on));
345 if (connect(fd, addr->ai_addr, addr->ai_addrlen) >= 0)
351 freeaddrinfo(results);
356 static int _xcb_open_unix(char *protocol, const char *file)
359 struct sockaddr_un addr;
361 if (protocol && strcmp("unix",protocol))
364 strcpy(addr.sun_path, file);
365 addr.sun_family = AF_UNIX;
366 #ifdef HAVE_SOCKADDR_SUN_LEN
367 addr.sun_len = SUN_LEN(&addr);
369 fd = _xcb_socket(AF_UNIX, SOCK_STREAM, 0);
372 if(connect(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1) {
380 #ifdef HAVE_ABSTRACT_SOCKETS
381 static int _xcb_open_abstract(char *protocol, const char *file, size_t filelen)
384 struct sockaddr_un addr = {0};
387 if (protocol && strcmp("unix",protocol))
390 strcpy(addr.sun_path + 1, file);
391 addr.sun_family = AF_UNIX;
392 namelen = offsetof(struct sockaddr_un, sun_path) + 1 + filelen;
393 #ifdef HAVE_SOCKADDR_SUN_LEN
394 addr.sun_len = 1 + filelen;
396 fd = _xcb_socket(AF_UNIX, SOCK_STREAM, 0);
399 if (connect(fd, (struct sockaddr *) &addr, namelen) == -1) {
407 xcb_connection_t *xcb_connect(const char *displayname, int *screenp)
409 return xcb_connect_to_display_with_auth_info(displayname, NULL, screenp);
412 xcb_connection_t *xcb_connect_to_display_with_auth_info(const char *displayname, xcb_auth_info_t *auth, int *screenp)
416 char *protocol = NULL;
417 xcb_auth_info_t ourauth;
420 int parsed = _xcb_parse_display(displayname, &host, &protocol, &display, screenp);
423 c = (xcb_connection_t *) &error_connection;
426 fd = _xcb_open(host, protocol, display);
429 c = (xcb_connection_t *) &error_connection;
434 c = xcb_connect_to_fd(fd, auth);
438 if(_xcb_get_auth_info(fd, &ourauth, display))
440 c = xcb_connect_to_fd(fd, &ourauth);
445 c = xcb_connect_to_fd(fd, 0);