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>
30 #include <sys/socket.h>
33 #include <netinet/in.h>
34 #include <netinet/tcp.h>
36 #include <netdnet/dnetdb.h>
37 #include <netdnet/dn.h>
52 static const int error_connection = 1;
54 int xcb_popcount(uint32_t mask)
57 y = (mask >> 1) & 033333333333;
58 y = mask - y - ((y >> 1) & 033333333333);
59 return ((y + (y >> 3)) & 030707070707) % 077;
62 static int _xcb_parse_display(const char *name, char **host, char **protocol,
63 int *displayp, int *screenp)
65 int len, display, screen;
66 char *slash, *colon, *dot, *end;
68 name = getenv("DISPLAY");
73 if(strncmp(name, "/tmp/launch", 11) == 0)
77 slash = strrchr(name, '/');
82 *protocol = malloc(len + 1);
85 memcpy(*protocol, name, len);
86 (*protocol)[len] = '\0';
93 colon = strrchr(name, ':');
98 display = strtoul(colon, &dot, 10);
108 screen = strtoul(dot, &end, 10);
109 if(end == dot || *end != '\0')
112 /* At this point, the display string is fully parsed and valid, but
113 * the caller's memory is untouched. */
115 *host = malloc(len + 1);
118 memcpy(*host, name, len);
126 int xcb_parse_display(const char *name, char **host, int *displayp,
129 return _xcb_parse_display(name, host, NULL, displayp, screenp);
132 static int _xcb_open_tcp(char *host, char *protocol, const unsigned short port);
133 static int _xcb_open_unix(char *protocol, const char *file);
135 static int _xcb_open_decnet(const char *host, char *protocol, const unsigned short port);
137 #ifdef HAVE_ABSTRACT_SOCKETS
138 static int _xcb_open_abstract(char *protocol, const char *file, size_t filelen);
141 static int _xcb_open(char *host, char *protocol, const int display)
144 static const char unix_base[] = "/tmp/.X11-unix/X";
145 const char *base = unix_base;
153 if(strncmp(host, "/tmp/launch", 11) == 0) {
159 /* DECnet displays have two colons, so _xcb_parse_display will have
160 left one at the end. However, an IPv6 address can end with *two*
161 colons, so only treat this as a DECnet display if host ends with
162 exactly one colon. */
163 char *colon = strchr(host, ':');
164 if(colon && *(colon+1) == '\0')
167 return _xcb_open_decnet(host, protocol, display);
172 || strcmp("unix",host)) { /* follow the old unix: rule */
174 /* display specifies TCP */
175 unsigned short port = X_TCP_PORT + display;
176 return _xcb_open_tcp(host, protocol, port);
183 filelen = strlen(base) + 1 + sizeof(display) * 3 + 1;
184 file = malloc(filelen);
188 /* display specifies Unix socket */
191 actual_filelen = snprintf(file, filelen, "%s:%d", base, display);
194 actual_filelen = snprintf(file, filelen, "%s%d", base, display);
195 if(actual_filelen < 0)
200 /* snprintf may truncate the file */
201 filelen = MIN(actual_filelen, filelen - 1);
202 #ifdef HAVE_ABSTRACT_SOCKETS
203 fd = _xcb_open_abstract(protocol, file, filelen);
204 if (fd >= 0 || (errno != ENOENT && errno != ECONNREFUSED))
211 fd = _xcb_open_unix(protocol, file);
217 static int _xcb_socket(int family, int type, int proto)
222 fd = socket(family, type | SOCK_CLOEXEC, proto);
223 if (fd == -1 && errno == EINVAL)
226 fd = socket(family, type, proto);
228 fcntl(fd, F_SETFD, FD_CLOEXEC);
234 static int _xcb_open_decnet(const char *host, const char *protocol, const unsigned short port)
237 struct sockaddr_dn addr;
238 struct accessdata_dn accessdata;
239 struct nodeent *nodeaddr = getnodebyname(host);
243 if (protocol && strcmp("dnet",protocol))
245 addr.sdn_family = AF_DECnet;
247 addr.sdn_add.a_len = nodeaddr->n_length;
248 memcpy(addr.sdn_add.a_addr, nodeaddr->n_addr, addr.sdn_add.a_len);
250 addr.sdn_objnamel = sprintf((char *)addr.sdn_objname, "X$X%d", port);
251 if(addr.sdn_objnamel < 0)
255 fd = _xcb_socket(PF_DECnet, SOCK_STREAM, 0);
259 memset(&accessdata, 0, sizeof(accessdata));
260 accessdata.acc_accl = sprintf((char*)accessdata.acc_acc, "%d", getuid());
261 if(accessdata.acc_accl < 0)
263 setsockopt(fd, DNPROTO_NSP, SO_CONACCESS, &accessdata, sizeof(accessdata));
265 if(connect(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1) {
273 static int _xcb_open_tcp(char *host, char *protocol, const unsigned short port)
276 struct addrinfo hints;
277 char service[6]; /* "65535" with the trailing '\0' */
278 struct addrinfo *results, *addr;
281 if (protocol && strcmp("tcp",protocol))
284 memset(&hints, 0, sizeof(hints));
286 hints.ai_flags |= AI_ADDRCONFIG;
288 #ifdef AI_NUMERICSERV
289 hints.ai_flags |= AI_NUMERICSERV;
291 hints.ai_family = AF_UNSPEC;
292 hints.ai_socktype = SOCK_STREAM;
295 /* Allow IPv6 addresses enclosed in brackets. */
296 if(host[0] == '[' && (bracket = strrchr(host, ']')) && bracket[1] == '\0')
300 hints.ai_flags |= AI_NUMERICHOST;
301 hints.ai_family = AF_INET6;
305 snprintf(service, sizeof(service), "%hu", port);
306 if(getaddrinfo(host, service, &hints, &results))
307 /* FIXME: use gai_strerror, and fill in error connection */
310 for(addr = results; addr; addr = addr->ai_next)
312 fd = _xcb_socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
315 setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on));
316 setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof(on));
318 if (connect(fd, addr->ai_addr, addr->ai_addrlen) >= 0)
324 freeaddrinfo(results);
328 static int _xcb_open_unix(char *protocol, const char *file)
331 struct sockaddr_un addr;
333 if (protocol && strcmp("unix",protocol))
336 strcpy(addr.sun_path, file);
337 addr.sun_family = AF_UNIX;
338 #ifdef HAVE_SOCKADDR_SUN_LEN
339 addr.sun_len = SUN_LEN(&addr);
341 fd = _xcb_socket(AF_UNIX, SOCK_STREAM, 0);
344 if(connect(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1) {
351 #ifdef HAVE_ABSTRACT_SOCKETS
352 static int _xcb_open_abstract(char *protocol, const char *file, size_t filelen)
355 struct sockaddr_un addr = {0};
358 if (protocol && strcmp("unix",protocol))
361 strcpy(addr.sun_path + 1, file);
362 addr.sun_family = AF_UNIX;
363 namelen = offsetof(struct sockaddr_un, sun_path) + 1 + filelen;
364 #ifdef HAVE_SOCKADDR_SUN_LEN
365 addr.sun_len = 1 + filelen;
367 fd = _xcb_socket(AF_UNIX, SOCK_STREAM, 0);
370 if (connect(fd, (struct sockaddr *) &addr, namelen) == -1) {
378 xcb_connection_t *xcb_connect(const char *displayname, int *screenp)
380 return xcb_connect_to_display_with_auth_info(displayname, NULL, screenp);
383 xcb_connection_t *xcb_connect_to_display_with_auth_info(const char *displayname, xcb_auth_info_t *auth, int *screenp)
388 xcb_auth_info_t ourauth;
391 int parsed = _xcb_parse_display(displayname, &host, &protocol, &display, screenp);
394 return (xcb_connection_t *) &error_connection;
396 fd = _xcb_open(host, protocol, display);
400 return (xcb_connection_t *) &error_connection;
403 return xcb_connect_to_fd(fd, auth);
405 if(_xcb_get_auth_info(fd, &ourauth, display))
407 c = xcb_connect_to_fd(fd, &ourauth);
412 c = xcb_connect_to_fd(fd, 0);