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 int xcb_sumof(uint8_t *list, int len)
70 for(i=0; i<len; i++) {
77 static int _xcb_parse_display(const char *name, char **host, char **protocol,
78 int *displayp, int *screenp)
80 int len, display, screen;
81 char *slash, *colon, *dot, *end;
84 name = getenv("DISPLAY");
89 if(strncmp(name, "/tmp/launch", 11) == 0)
93 slash = strrchr(name, '/');
98 *protocol = malloc(len + 1);
101 memcpy(*protocol, name, len);
102 (*protocol)[len] = '\0';
109 colon = strrchr(name, ':');
114 display = strtoul(colon, &dot, 10);
124 screen = strtoul(dot, &end, 10);
125 if(end == dot || *end != '\0')
128 /* At this point, the display string is fully parsed and valid, but
129 * the caller's memory is untouched. */
131 *host = malloc(len + 1);
134 memcpy(*host, name, len);
150 int xcb_parse_display(const char *name, char **host, int *displayp,
153 return _xcb_parse_display(name, host, NULL, displayp, screenp);
156 static int _xcb_open_tcp(const char *host, char *protocol, const unsigned short port);
158 static int _xcb_open_unix(char *protocol, const char *file);
161 static int _xcb_open_decnet(const char *host, char *protocol, const unsigned short port);
163 #ifdef HAVE_ABSTRACT_SOCKETS
164 static int _xcb_open_abstract(char *protocol, const char *file, size_t filelen);
167 static int _xcb_open(const char *host, char *protocol, const int display)
170 static const char unix_base[] = "/tmp/.X11-unix/X";
171 const char *base = unix_base;
177 if(strncmp(host, "/tmp/launch", 11) == 0) {
184 if(*host || protocol)
187 /* DECnet displays have two colons, so _xcb_parse_display will have
188 left one at the end. However, an IPv6 address can end with *two*
189 colons, so only treat this as a DECnet display if host ends with
190 exactly one colon. */
191 char *colon = strchr(host, ':');
192 if(colon && *(colon+1) == '\0')
195 return _xcb_open_decnet(host, protocol, display);
200 || strcmp("unix",host)) { /* follow the old unix: rule */
202 /* display specifies TCP */
203 unsigned short port = X_TCP_PORT + display;
204 return _xcb_open_tcp(host, protocol, port);
209 filelen = strlen(base) + 1 + sizeof(display) * 3 + 1;
210 file = malloc(filelen);
214 /* display specifies Unix socket */
216 if(strncmp(base, "/tmp/launch", 11) == 0)
217 actual_filelen = snprintf(file, filelen, "%s:%d", base, display);
220 actual_filelen = snprintf(file, filelen, "%s%d", base, display);
221 if(actual_filelen < 0)
226 /* snprintf may truncate the file */
227 filelen = MIN(actual_filelen, filelen - 1);
228 #ifdef HAVE_ABSTRACT_SOCKETS
229 fd = _xcb_open_abstract(protocol, file, filelen);
230 if (fd >= 0 || (errno != ENOENT && errno != ECONNREFUSED))
237 fd = _xcb_open_unix(protocol, file);
242 return -1; /* if control reaches here then something has gone wrong */
245 static int _xcb_socket(int family, int type, int proto)
250 fd = socket(family, type | SOCK_CLOEXEC, proto);
251 if (fd == -1 && errno == EINVAL)
254 fd = socket(family, type, proto);
257 fcntl(fd, F_SETFD, FD_CLOEXEC);
264 static int _xcb_open_decnet(const char *host, const char *protocol, const unsigned short port)
267 struct sockaddr_dn addr;
268 struct accessdata_dn accessdata;
269 struct nodeent *nodeaddr = getnodebyname(host);
273 if (protocol && strcmp("dnet",protocol))
275 addr.sdn_family = AF_DECnet;
277 addr.sdn_add.a_len = nodeaddr->n_length;
278 memcpy(addr.sdn_add.a_addr, nodeaddr->n_addr, addr.sdn_add.a_len);
280 addr.sdn_objnamel = sprintf((char *)addr.sdn_objname, "X$X%d", port);
281 if(addr.sdn_objnamel < 0)
285 fd = _xcb_socket(PF_DECnet, SOCK_STREAM, 0);
289 memset(&accessdata, 0, sizeof(accessdata));
290 accessdata.acc_accl = sprintf((char*)accessdata.acc_acc, "%d", getuid());
291 if(accessdata.acc_accl < 0)
293 setsockopt(fd, DNPROTO_NSP, SO_CONACCESS, &accessdata, sizeof(accessdata));
295 if(connect(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1) {
303 static int _xcb_open_tcp(const char *host, char *protocol, const unsigned short port)
306 struct addrinfo hints;
307 char service[6]; /* "65535" with the trailing '\0' */
308 struct addrinfo *results, *addr;
311 if (protocol && strcmp("tcp",protocol) && strcmp("inet",protocol)
313 && strcmp("inet6",protocol)
321 memset(&hints, 0, sizeof(hints));
323 hints.ai_flags |= AI_ADDRCONFIG;
325 #ifdef AI_NUMERICSERV
326 hints.ai_flags |= AI_NUMERICSERV;
328 hints.ai_family = AF_UNSPEC;
329 hints.ai_socktype = SOCK_STREAM;
332 /* Allow IPv6 addresses enclosed in brackets. */
333 if(host[0] == '[' && (bracket = strrchr(host, ']')) && bracket[1] == '\0')
337 hints.ai_flags |= AI_NUMERICHOST;
338 hints.ai_family = AF_INET6;
342 snprintf(service, sizeof(service), "%hu", port);
343 if(getaddrinfo(host, service, &hints, &results))
344 /* FIXME: use gai_strerror, and fill in error connection */
347 for(addr = results; addr; addr = addr->ai_next)
349 fd = _xcb_socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
352 setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on));
353 setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof(on));
355 if (connect(fd, addr->ai_addr, addr->ai_addrlen) >= 0)
361 freeaddrinfo(results);
366 static int _xcb_open_unix(char *protocol, const char *file)
369 struct sockaddr_un addr;
371 if (protocol && strcmp("unix",protocol))
374 strcpy(addr.sun_path, file);
375 addr.sun_family = AF_UNIX;
376 #ifdef HAVE_SOCKADDR_SUN_LEN
377 addr.sun_len = SUN_LEN(&addr);
379 fd = _xcb_socket(AF_UNIX, SOCK_STREAM, 0);
382 if(connect(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1) {
390 #ifdef HAVE_ABSTRACT_SOCKETS
391 static int _xcb_open_abstract(char *protocol, const char *file, size_t filelen)
394 struct sockaddr_un addr = {0};
397 if (protocol && strcmp("unix",protocol))
400 strcpy(addr.sun_path + 1, file);
401 addr.sun_family = AF_UNIX;
402 namelen = offsetof(struct sockaddr_un, sun_path) + 1 + filelen;
403 #ifdef HAVE_SOCKADDR_SUN_LEN
404 addr.sun_len = 1 + filelen;
406 fd = _xcb_socket(AF_UNIX, SOCK_STREAM, 0);
409 if (connect(fd, (struct sockaddr *) &addr, namelen) == -1) {
417 xcb_connection_t *xcb_connect(const char *displayname, int *screenp)
419 return xcb_connect_to_display_with_auth_info(displayname, NULL, screenp);
422 xcb_connection_t *xcb_connect_to_display_with_auth_info(const char *displayname, xcb_auth_info_t *auth, int *screenp)
426 char *protocol = NULL;
427 xcb_auth_info_t ourauth;
430 int parsed = _xcb_parse_display(displayname, &host, &protocol, &display, screenp);
433 c = (xcb_connection_t *) &error_connection;
436 fd = _xcb_open(host, protocol, display);
439 c = (xcb_connection_t *) &error_connection;
444 c = xcb_connect_to_fd(fd, auth);
448 if(_xcb_get_auth_info(fd, &ourauth, display))
450 c = xcb_connect_to_fd(fd, &ourauth);
455 c = xcb_connect_to_fd(fd, 0);