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)
172 static const char unix_base[] = "/usr/spool/sockets/X11/";
174 static const char unix_base[] = "/tmp/.X11-unix/X";
176 const char *base = unix_base;
182 if(strncmp(host, "/tmp/launch", 11) == 0) {
189 /* If protocol or host is "unix", fall through to Unix socket code below */
190 if ((!protocol || (strcmp("unix",protocol) != 0)) &&
191 (*host != '\0') && (strcmp("unix",host) != 0))
193 /* display specifies TCP */
194 unsigned short port = X_TCP_PORT + display;
195 return _xcb_open_tcp(host, protocol, port);
199 #if defined(HAVE_TSOL_LABEL_H) && defined(HAVE_IS_SYSTEM_LABELED)
200 /* Check special path for Unix sockets under Solaris Trusted Extensions */
201 if (is_system_labeled())
204 const char *tsol_base = "/var/tsol/doors/.X11-unix/X";
205 char tsol_socket[PATH_MAX];
207 snprintf(tsol_socket, sizeof(tsol_socket), "%s%d", tsol_base, display);
209 if (stat(tsol_socket, &sbuf) == 0)
214 filelen = strlen(base) + 1 + sizeof(display) * 3 + 1;
215 file = malloc(filelen);
219 /* display specifies Unix socket */
221 if(strncmp(base, "/tmp/launch", 11) == 0)
222 actual_filelen = snprintf(file, filelen, "%s:%d", base, display);
225 actual_filelen = snprintf(file, filelen, "%s%d", base, display);
226 if(actual_filelen < 0)
231 /* snprintf may truncate the file */
232 filelen = MIN(actual_filelen, filelen - 1);
233 #ifdef HAVE_ABSTRACT_SOCKETS
234 fd = _xcb_open_abstract(protocol, file, filelen);
235 if (fd >= 0 || (errno != ENOENT && errno != ECONNREFUSED))
242 fd = _xcb_open_unix(protocol, file);
245 if (fd < 0 && !protocol && *host == '\0') {
246 unsigned short port = X_TCP_PORT + display;
247 fd = _xcb_open_tcp(host, protocol, port);
252 return -1; /* if control reaches here then something has gone wrong */
255 static int _xcb_socket(int family, int type, int proto)
260 fd = socket(family, type | SOCK_CLOEXEC, proto);
261 if (fd == -1 && errno == EINVAL)
264 fd = socket(family, type, proto);
267 fcntl(fd, F_SETFD, FD_CLOEXEC);
274 static int _xcb_do_connect(int fd, const struct sockaddr* addr, int addrlen) {
280 setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on));
281 setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof(on));
283 return connect(fd, addr, addrlen);
286 static int _xcb_open_tcp(const char *host, char *protocol, const unsigned short port)
290 struct addrinfo hints;
291 char service[6]; /* "65535" with the trailing '\0' */
292 struct addrinfo *results, *addr;
296 if (protocol && strcmp("tcp",protocol) && strcmp("inet",protocol)
298 && strcmp("inet6",protocol)
307 memset(&hints, 0, sizeof(hints));
308 #ifdef AI_NUMERICSERV
309 hints.ai_flags |= AI_NUMERICSERV;
311 hints.ai_family = AF_UNSPEC;
312 hints.ai_socktype = SOCK_STREAM;
315 /* Allow IPv6 addresses enclosed in brackets. */
316 if(host[0] == '[' && (bracket = strrchr(host, ']')) && bracket[1] == '\0')
320 hints.ai_flags |= AI_NUMERICHOST;
321 hints.ai_family = AF_INET6;
325 snprintf(service, sizeof(service), "%hu", port);
326 if(getaddrinfo(host, service, &hints, &results))
327 /* FIXME: use gai_strerror, and fill in error connection */
330 for(addr = results; addr; addr = addr->ai_next)
332 fd = _xcb_socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
333 if (_xcb_do_connect(fd, addr->ai_addr, addr->ai_addrlen) >= 0)
338 freeaddrinfo(results);
343 struct sockaddr_in _s;
344 struct in_addr ** _c;
346 if((_h = gethostbyname(host)) == NULL)
349 _c = (struct in_addr**)_h->h_addr_list;
353 _s.sin_family = AF_INET;
354 _s.sin_port = htons(port);
355 _s.sin_addr = *(*_c);
357 fd = _xcb_socket(_s.sin_family, SOCK_STREAM, 0);
358 if(_xcb_do_connect(fd, (struct sockaddr*)&_s, sizeof(_s)) >= 0)
372 static int _xcb_open_unix(char *protocol, const char *file)
375 struct sockaddr_un addr;
377 if (protocol && strcmp("unix",protocol))
380 strcpy(addr.sun_path, file);
381 addr.sun_family = AF_UNIX;
382 #ifdef HAVE_SOCKADDR_SUN_LEN
383 addr.sun_len = SUN_LEN(&addr);
385 fd = _xcb_socket(AF_UNIX, SOCK_STREAM, 0);
388 if(connect(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1) {
396 #ifdef HAVE_ABSTRACT_SOCKETS
397 static int _xcb_open_abstract(char *protocol, const char *file, size_t filelen)
400 struct sockaddr_un addr = {0};
403 if (protocol && strcmp("unix",protocol))
406 strcpy(addr.sun_path + 1, file);
407 addr.sun_family = AF_UNIX;
408 namelen = offsetof(struct sockaddr_un, sun_path) + 1 + filelen;
409 #ifdef HAVE_SOCKADDR_SUN_LEN
410 addr.sun_len = 1 + filelen;
412 fd = _xcb_socket(AF_UNIX, SOCK_STREAM, 0);
415 if (connect(fd, (struct sockaddr *) &addr, namelen) == -1) {
423 xcb_connection_t *xcb_connect(const char *displayname, int *screenp)
425 return xcb_connect_to_display_with_auth_info(displayname, NULL, screenp);
428 xcb_connection_t *xcb_connect_to_display_with_auth_info(const char *displayname, xcb_auth_info_t *auth, int *screenp)
432 char *protocol = NULL;
433 xcb_auth_info_t ourauth;
436 int parsed = _xcb_parse_display(displayname, &host, &protocol, &display, screenp);
439 c = _xcb_conn_ret_error(XCB_CONN_CLOSED_PARSE_ERR);
444 if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
445 c = _xcb_conn_ret_error(XCB_CONN_ERROR);
449 fd = _xcb_open(host, protocol, display);
453 c = _xcb_conn_ret_error(XCB_CONN_ERROR);
461 c = xcb_connect_to_fd(fd, auth);
465 if(_xcb_get_auth_info(fd, &ourauth, display))
467 c = xcb_connect_to_fd(fd, &ourauth);
472 c = xcb_connect_to_fd(fd, 0);
477 /* Make sure requested screen number is in bounds for this server */
478 if((screenp != NULL) && (*screenp >= (int) c->setup->roots_len)) {
480 c = _xcb_conn_ret_error(XCB_CONN_CLOSED_INVALID_SCREEN);