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. */
33 #include <sys/types.h>
43 #include "xcb_windefs.h"
45 #include <arpa/inet.h>
46 #include <sys/socket.h>
48 #include <netinet/in.h>
49 #include <netinet/tcp.h>
58 #if defined(HAVE_TSOL_LABEL_H) && defined(HAVE_IS_SYSTEM_LABELED)
59 # include <tsol/label.h>
60 # include <sys/stat.h>
63 int xcb_popcount(uint32_t mask)
66 y = (mask >> 1) & 033333333333;
67 y = mask - y - ((y >> 1) & 033333333333);
68 return ((y + (y >> 3)) & 030707070707) % 077;
71 int xcb_sumof(uint8_t *list, int len)
74 for(i=0; i<len; i++) {
81 static int _xcb_parse_display(const char *name, char **host, char **protocol,
82 int *displayp, int *screenp)
84 int len, display, screen;
85 char *slash, *colon, *dot, *end;
88 name = getenv("DISPLAY");
93 if(strncmp(name, "/tmp/launch", 11) == 0)
97 slash = strrchr(name, '/');
102 *protocol = malloc(len + 1);
105 memcpy(*protocol, name, len);
106 (*protocol)[len] = '\0';
113 colon = strrchr(name, ':');
118 display = strtoul(colon, &dot, 10);
128 screen = strtoul(dot, &end, 10);
129 if(end == dot || *end != '\0')
132 /* At this point, the display string is fully parsed and valid, but
133 * the caller's memory is untouched. */
135 *host = malloc(len + 1);
138 memcpy(*host, name, len);
154 int xcb_parse_display(const char *name, char **host, int *displayp,
157 return _xcb_parse_display(name, host, NULL, displayp, screenp);
160 static int _xcb_open_tcp(const char *host, char *protocol, const unsigned short port);
162 static int _xcb_open_unix(char *protocol, const char *file);
164 #ifdef HAVE_ABSTRACT_SOCKETS
165 static int _xcb_open_abstract(char *protocol, const char *file, size_t filelen);
168 static int _xcb_open(const char *host, char *protocol, const int display)
171 static const char unix_base[] = "/tmp/.X11-unix/X";
172 const char *base = unix_base;
178 if(strncmp(host, "/tmp/launch", 11) == 0) {
185 /* If protocol or host is "unix", fall through to Unix socket code below */
186 if ((!protocol || (strcmp("unix",protocol) != 0)) &&
187 (*host != '\0') && (strcmp("unix",host) != 0))
189 /* display specifies TCP */
190 unsigned short port = X_TCP_PORT + display;
191 return _xcb_open_tcp(host, protocol, port);
195 #if defined(HAVE_TSOL_LABEL_H) && defined(HAVE_IS_SYSTEM_LABELED)
196 /* Check special path for Unix sockets under Solaris Trusted Extensions */
197 if (is_system_labeled())
200 const char *tsol_base = "/var/tsol/doors/.X11-unix/X";
201 char tsol_socket[PATH_MAX];
203 snprintf(tsol_socket, sizeof(tsol_socket), "%s%d", tsol_base, display);
205 if (stat(tsol_socket, &sbuf) == 0)
210 filelen = strlen(base) + 1 + sizeof(display) * 3 + 1;
211 file = malloc(filelen);
215 /* display specifies Unix socket */
217 if(strncmp(base, "/tmp/launch", 11) == 0)
218 actual_filelen = snprintf(file, filelen, "%s:%d", base, display);
221 actual_filelen = snprintf(file, filelen, "%s%d", base, display);
222 if(actual_filelen < 0)
227 /* snprintf may truncate the file */
228 filelen = MIN(actual_filelen, filelen - 1);
229 #ifdef HAVE_ABSTRACT_SOCKETS
230 fd = _xcb_open_abstract(protocol, file, filelen);
231 if (fd >= 0 || (errno != ENOENT && errno != ECONNREFUSED))
238 fd = _xcb_open_unix(protocol, file);
241 if (fd < 0 && !protocol && *host == '\0') {
242 unsigned short port = X_TCP_PORT + display;
243 fd = _xcb_open_tcp(host, protocol, port);
248 return -1; /* if control reaches here then something has gone wrong */
251 static int _xcb_socket(int family, int type, int proto)
256 fd = socket(family, type | SOCK_CLOEXEC, proto);
257 if (fd == -1 && errno == EINVAL)
260 fd = socket(family, type, proto);
263 fcntl(fd, F_SETFD, FD_CLOEXEC);
270 static int _xcb_do_connect(int fd, const struct sockaddr* addr, int addrlen) {
276 setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on));
277 setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof(on));
279 return connect(fd, addr, addrlen);
282 static int _xcb_open_tcp(const char *host, char *protocol, const unsigned short port)
286 struct addrinfo hints;
287 char service[6]; /* "65535" with the trailing '\0' */
288 struct addrinfo *results, *addr;
292 if (protocol && strcmp("tcp",protocol) && strcmp("inet",protocol)
294 && strcmp("inet6",protocol)
303 memset(&hints, 0, sizeof(hints));
304 #ifdef AI_NUMERICSERV
305 hints.ai_flags |= AI_NUMERICSERV;
307 hints.ai_family = AF_UNSPEC;
308 hints.ai_socktype = SOCK_STREAM;
311 /* Allow IPv6 addresses enclosed in brackets. */
312 if(host[0] == '[' && (bracket = strrchr(host, ']')) && bracket[1] == '\0')
316 hints.ai_flags |= AI_NUMERICHOST;
317 hints.ai_family = AF_INET6;
321 snprintf(service, sizeof(service), "%hu", port);
322 if(getaddrinfo(host, service, &hints, &results))
323 /* FIXME: use gai_strerror, and fill in error connection */
326 for(addr = results; addr; addr = addr->ai_next)
328 fd = _xcb_socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
329 if (_xcb_do_connect(fd, addr->ai_addr, addr->ai_addrlen) >= 0)
334 freeaddrinfo(results);
339 struct sockaddr_in _s;
340 struct in_addr ** _c;
342 if((_h = gethostbyname(host)) == NULL)
345 _c = (struct in_addr**)_h->h_addr_list;
349 _s.sin_family = AF_INET;
350 _s.sin_port = htons(port);
351 _s.sin_addr = *(*_c);
353 fd = _xcb_socket(_s.sin_family, SOCK_STREAM, 0);
354 if(_xcb_do_connect(fd, (struct sockaddr*)&_s, sizeof(_s)) >= 0)
368 static int _xcb_open_unix(char *protocol, const char *file)
371 struct sockaddr_un addr;
373 if (protocol && strcmp("unix",protocol))
376 strcpy(addr.sun_path, file);
377 addr.sun_family = AF_UNIX;
378 #ifdef HAVE_SOCKADDR_SUN_LEN
379 addr.sun_len = SUN_LEN(&addr);
381 fd = _xcb_socket(AF_UNIX, SOCK_STREAM, 0);
384 if(connect(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1) {
392 #ifdef HAVE_ABSTRACT_SOCKETS
393 static int _xcb_open_abstract(char *protocol, const char *file, size_t filelen)
396 struct sockaddr_un addr = {0};
399 if (protocol && strcmp("unix",protocol))
402 strcpy(addr.sun_path + 1, file);
403 addr.sun_family = AF_UNIX;
404 namelen = offsetof(struct sockaddr_un, sun_path) + 1 + filelen;
405 #ifdef HAVE_SOCKADDR_SUN_LEN
406 addr.sun_len = 1 + filelen;
408 fd = _xcb_socket(AF_UNIX, SOCK_STREAM, 0);
411 if (connect(fd, (struct sockaddr *) &addr, namelen) == -1) {
419 xcb_connection_t *xcb_connect(const char *displayname, int *screenp)
421 return xcb_connect_to_display_with_auth_info(displayname, NULL, screenp);
424 xcb_connection_t *xcb_connect_to_display_with_auth_info(const char *displayname, xcb_auth_info_t *auth, int *screenp)
428 char *protocol = NULL;
429 xcb_auth_info_t ourauth;
432 int parsed = _xcb_parse_display(displayname, &host, &protocol, &display, screenp);
435 c = _xcb_conn_ret_error(XCB_CONN_CLOSED_PARSE_ERR);
440 if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
441 c = _xcb_conn_ret_error(XCB_CONN_ERROR);
445 fd = _xcb_open(host, protocol, display);
449 c = _xcb_conn_ret_error(XCB_CONN_ERROR);
457 c = xcb_connect_to_fd(fd, auth);
461 if(_xcb_get_auth_info(fd, &ourauth, display))
463 c = xcb_connect_to_fd(fd, &ourauth);
468 c = xcb_connect_to_fd(fd, 0);
473 /* Make sure requested screen number is in bounds for this server */
474 if((screenp != NULL) && (*screenp >= (int) c->setup->roots_len)) {
476 c = _xcb_conn_ret_error(XCB_CONN_CLOSED_INVALID_SCREEN);