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>
39 #include "xcb_windefs.h"
41 #include <arpa/inet.h>
42 #include <sys/socket.h>
44 #include <netinet/in.h>
45 #include <netinet/tcp.h>
54 /* must be after "xcbint.h" to get autoconf #defines */
55 #if defined(HAVE_TSOL_LABEL_H) && defined(HAVE_IS_SYSTEM_LABELED)
56 # include <tsol/label.h>
57 # include <sys/stat.h>
60 int xcb_popcount(uint32_t mask)
63 y = (mask >> 1) & 033333333333;
64 y = mask - y - ((y >> 1) & 033333333333);
65 return ((y + (y >> 3)) & 030707070707) % 077;
68 int xcb_sumof(uint8_t *list, int len)
71 for(i=0; i<len; i++) {
78 static int _xcb_parse_display(const char *name, char **host, char **protocol,
79 int *displayp, int *screenp)
81 int len, display, screen;
82 char *slash, *colon, *dot, *end;
85 name = getenv("DISPLAY");
90 if(strncmp(name, "/tmp/launch", 11) == 0)
94 slash = strrchr(name, '/');
99 *protocol = malloc(len + 1);
102 memcpy(*protocol, name, len);
103 (*protocol)[len] = '\0';
110 colon = strrchr(name, ':');
115 display = strtoul(colon, &dot, 10);
125 screen = strtoul(dot, &end, 10);
126 if(end == dot || *end != '\0')
129 /* At this point, the display string is fully parsed and valid, but
130 * the caller's memory is untouched. */
132 *host = malloc(len + 1);
135 memcpy(*host, name, len);
151 int xcb_parse_display(const char *name, char **host, int *displayp,
154 return _xcb_parse_display(name, host, NULL, displayp, screenp);
157 static int _xcb_open_tcp(const char *host, char *protocol, const unsigned short port);
159 static int _xcb_open_unix(char *protocol, const char *file);
161 #ifdef HAVE_ABSTRACT_SOCKETS
162 static int _xcb_open_abstract(char *protocol, const char *file, size_t filelen);
165 static int _xcb_open(const char *host, char *protocol, const int display)
168 static const char unix_base[] = "/tmp/.X11-unix/X";
169 const char *base = unix_base;
175 if(strncmp(host, "/tmp/launch", 11) == 0) {
182 /* If protocol or host is "unix", fall through to Unix socket code below */
183 if ((!protocol || (strcmp("unix",protocol) != 0)) &&
184 (*host != '\0') && (strcmp("unix",host) != 0))
186 /* display specifies TCP */
187 unsigned short port = X_TCP_PORT + display;
188 return _xcb_open_tcp(host, protocol, port);
192 #if defined(HAVE_TSOL_LABEL_H) && defined(HAVE_IS_SYSTEM_LABELED)
193 /* Check special path for Unix sockets under Solaris Trusted Extensions */
194 if (is_system_labeled())
197 const char *tsol_base = "/var/tsol/doors/.X11-unix/X";
198 char tsol_socket[PATH_MAX];
200 snprintf(tsol_socket, sizeof(tsol_socket), "%s%d", tsol_base, display);
202 if (stat(tsol_socket, &sbuf) == 0)
207 filelen = strlen(base) + 1 + sizeof(display) * 3 + 1;
208 file = malloc(filelen);
212 /* display specifies Unix socket */
214 if(strncmp(base, "/tmp/launch", 11) == 0)
215 actual_filelen = snprintf(file, filelen, "%s:%d", base, display);
218 actual_filelen = snprintf(file, filelen, "%s%d", base, display);
219 if(actual_filelen < 0)
224 /* snprintf may truncate the file */
225 filelen = MIN(actual_filelen, filelen - 1);
226 #ifdef HAVE_ABSTRACT_SOCKETS
227 fd = _xcb_open_abstract(protocol, file, filelen);
228 if (fd >= 0 || (errno != ENOENT && errno != ECONNREFUSED))
235 fd = _xcb_open_unix(protocol, file);
238 if (fd < 0 && !protocol && *host == '\0') {
239 unsigned short port = X_TCP_PORT + display;
240 fd = _xcb_open_tcp(host, protocol, port);
245 return -1; /* if control reaches here then something has gone wrong */
248 static int _xcb_socket(int family, int type, int proto)
253 fd = socket(family, type | SOCK_CLOEXEC, proto);
254 if (fd == -1 && errno == EINVAL)
257 fd = socket(family, type, proto);
260 fcntl(fd, F_SETFD, FD_CLOEXEC);
267 static int _xcb_do_connect(int fd, const struct sockaddr* addr, int addrlen) {
273 setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on));
274 setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof(on));
276 return connect(fd, addr, addrlen);
279 static int _xcb_open_tcp(const char *host, char *protocol, const unsigned short port)
283 struct addrinfo hints;
284 char service[6]; /* "65535" with the trailing '\0' */
285 struct addrinfo *results, *addr;
289 if (protocol && strcmp("tcp",protocol) && strcmp("inet",protocol)
291 && strcmp("inet6",protocol)
300 memset(&hints, 0, sizeof(hints));
301 #ifdef AI_NUMERICSERV
302 hints.ai_flags |= AI_NUMERICSERV;
304 hints.ai_family = AF_UNSPEC;
305 hints.ai_socktype = SOCK_STREAM;
308 /* Allow IPv6 addresses enclosed in brackets. */
309 if(host[0] == '[' && (bracket = strrchr(host, ']')) && bracket[1] == '\0')
313 hints.ai_flags |= AI_NUMERICHOST;
314 hints.ai_family = AF_INET6;
318 snprintf(service, sizeof(service), "%hu", port);
319 if(getaddrinfo(host, service, &hints, &results))
320 /* FIXME: use gai_strerror, and fill in error connection */
323 for(addr = results; addr; addr = addr->ai_next)
325 fd = _xcb_socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
326 if (_xcb_do_connect(fd, addr->ai_addr, addr->ai_addrlen) >= 0)
331 freeaddrinfo(results);
336 struct sockaddr_in _s;
337 struct in_addr ** _c;
339 if((_h = gethostbyname(host)) == NULL)
342 _c = (struct in_addr**)_h->h_addr_list;
346 _s.sin_family = AF_INET;
347 _s.sin_port = htons(port);
348 _s.sin_addr = *(*_c);
350 fd = _xcb_socket(_s.sin_family, SOCK_STREAM, 0);
351 if(_xcb_do_connect(fd, (struct sockaddr*)&_s, sizeof(_s)) >= 0)
365 static int _xcb_open_unix(char *protocol, const char *file)
368 struct sockaddr_un addr;
370 if (protocol && strcmp("unix",protocol))
373 strcpy(addr.sun_path, file);
374 addr.sun_family = AF_UNIX;
375 #ifdef HAVE_SOCKADDR_SUN_LEN
376 addr.sun_len = SUN_LEN(&addr);
378 fd = _xcb_socket(AF_UNIX, SOCK_STREAM, 0);
381 if(connect(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1) {
389 #ifdef HAVE_ABSTRACT_SOCKETS
390 static int _xcb_open_abstract(char *protocol, const char *file, size_t filelen)
393 struct sockaddr_un addr = {0};
396 if (protocol && strcmp("unix",protocol))
399 strcpy(addr.sun_path + 1, file);
400 addr.sun_family = AF_UNIX;
401 namelen = offsetof(struct sockaddr_un, sun_path) + 1 + filelen;
402 #ifdef HAVE_SOCKADDR_SUN_LEN
403 addr.sun_len = 1 + filelen;
405 fd = _xcb_socket(AF_UNIX, SOCK_STREAM, 0);
408 if (connect(fd, (struct sockaddr *) &addr, namelen) == -1) {
416 xcb_connection_t *xcb_connect(const char *displayname, int *screenp)
418 return xcb_connect_to_display_with_auth_info(displayname, NULL, screenp);
421 xcb_connection_t *xcb_connect_to_display_with_auth_info(const char *displayname, xcb_auth_info_t *auth, int *screenp)
425 char *protocol = NULL;
426 xcb_auth_info_t ourauth;
429 int parsed = _xcb_parse_display(displayname, &host, &protocol, &display, screenp);
432 c = _xcb_conn_ret_error(XCB_CONN_CLOSED_PARSE_ERR);
437 if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
438 c = _xcb_conn_ret_error(XCB_CONN_ERROR);
442 fd = _xcb_open(host, protocol, display);
446 c = _xcb_conn_ret_error(XCB_CONN_ERROR);
454 c = xcb_connect_to_fd(fd, auth);
458 if(_xcb_get_auth_info(fd, &ourauth, display))
460 c = xcb_connect_to_fd(fd, &ourauth);
465 c = xcb_connect_to_fd(fd, 0);