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>
32 #include <netinet/in.h>
34 #include <netdnet/dnetdb.h>
35 #include <netdnet/dn.h>
49 static const int error_connection = 1;
51 int xcb_popcount(uint32_t mask)
54 y = (mask >> 1) & 033333333333;
55 y = mask - y - ((y >> 1) & 033333333333);
56 return ((y + (y >> 3)) & 030707070707) % 077;
59 static int _xcb_parse_display(const char *name, char **host, char **protocol,
60 int *displayp, int *screenp)
62 int len, display, screen;
63 char *slash, *colon, *dot, *end;
65 name = getenv("DISPLAY");
68 slash = strrchr(name, '/');
72 *protocol = malloc(len + 1);
75 memcpy(*protocol, name, len);
76 (*protocol)[len] = '\0';
83 colon = strrchr(name, ':');
88 display = strtoul(colon, &dot, 10);
98 screen = strtoul(dot, &end, 10);
99 if(end == dot || *end != '\0')
102 /* At this point, the display string is fully parsed and valid, but
103 * the caller's memory is untouched. */
105 *host = malloc(len + 1);
108 memcpy(*host, name, len);
116 int xcb_parse_display(const char *name, char **host, int *displayp,
119 return _xcb_parse_display(name, host, NULL, displayp, screenp);
122 static int _xcb_open_tcp(char *host, char *protocol, const unsigned short port);
123 static int _xcb_open_unix(char *protocol, const char *file);
125 static int _xcb_open_decnet(const char *host, char *protocol, const unsigned short port);
127 #ifdef HAVE_ABSTRACT_SOCKETS
128 static int _xcb_open_abstract(char *protocol, const char *file);
131 static int _xcb_open(char *host, char *protocol, const int display)
134 static const char base[] = "/tmp/.X11-unix/X";
135 char file[sizeof(base) + 20];
140 /* DECnet displays have two colons, so _xcb_parse_display will have
141 left one at the end. However, an IPv6 address can end with *two*
142 colons, so only treat this as a DECnet display if host ends with
143 exactly one colon. */
144 char *colon = strchr(host, ':');
145 if(colon && *(colon+1) == '\0')
148 return _xcb_open_decnet(host, protocol, display);
153 || strcmp("unix",host)) { /* follow the old unix: rule */
155 /* display specifies TCP */
156 unsigned short port = X_TCP_PORT + display;
157 return _xcb_open_tcp(host, protocol, port);
161 /* display specifies Unix socket */
162 snprintf(file, sizeof(file), "%s%d", base, display);
163 #ifdef HAVE_ABSTRACT_SOCKETS
164 fd = _xcb_open_abstract(protocol, file);
165 if (fd >= 0 || (errno != ENOENT && errno != ECONNREFUSED))
169 return _xcb_open_unix(protocol, file);
173 static int _xcb_open_decnet(const char *host, const char *protocol, const unsigned short port)
176 struct sockaddr_dn addr;
177 struct accessdata_dn accessdata;
178 struct nodeent *nodeaddr = getnodebyname(host);
182 if (protocol && strcmp("dnet",protocol))
184 addr.sdn_family = AF_DECnet;
186 addr.sdn_add.a_len = nodeaddr->n_length;
187 memcpy(addr.sdn_add.a_addr, nodeaddr->n_addr, addr.sdn_add.a_len);
189 sprintf((char *)addr.sdn_objname, "X$X%d", port);
190 addr.sdn_objnamel = strlen((char *)addr.sdn_objname);
193 fd = socket(PF_DECnet, SOCK_STREAM, 0);
197 memset(&accessdata, 0, sizeof(accessdata));
198 sprintf((char*)accessdata.acc_acc, "%d", getuid());
199 accessdata.acc_accl = strlen((char *)accessdata.acc_acc);
200 setsockopt(fd, DNPROTO_NSP, SO_CONACCESS, &accessdata, sizeof(accessdata));
202 if(connect(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1) {
210 static int _xcb_open_tcp(char *host, char *protocol, const unsigned short port)
213 struct addrinfo hints;
214 char service[6]; /* "65535" with the trailing '\0' */
215 struct addrinfo *results, *addr;
218 if (protocol && strcmp("tcp",protocol))
221 memset(&hints, 0, sizeof(hints));
223 hints.ai_flags |= AI_ADDRCONFIG;
225 #ifdef AI_NUMERICSERV
226 hints.ai_flags |= AI_NUMERICSERV;
228 hints.ai_family = AF_UNSPEC;
229 hints.ai_socktype = SOCK_STREAM;
232 /* Allow IPv6 addresses enclosed in brackets. */
233 if(host[0] == '[' && (bracket = strrchr(host, ']')) && bracket[1] == '\0')
237 hints.ai_flags |= AI_NUMERICHOST;
238 hints.ai_family = AF_INET6;
242 snprintf(service, sizeof(service), "%hu", port);
243 if(getaddrinfo(host, service, &hints, &results))
244 /* FIXME: use gai_strerror, and fill in error connection */
247 for(addr = results; addr; addr = addr->ai_next)
249 fd = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
251 if (connect(fd, addr->ai_addr, addr->ai_addrlen) >= 0)
257 freeaddrinfo(results);
261 static int _xcb_open_unix(char *protocol, const char *file)
264 struct sockaddr_un addr;
266 if (protocol && strcmp("unix",protocol))
269 strcpy(addr.sun_path, file);
270 addr.sun_family = AF_UNIX;
271 #ifdef HAVE_SOCKADDR_SUN_LEN
272 addr.sun_len = SUN_LEN(&addr);
274 fd = socket(AF_UNIX, SOCK_STREAM, 0);
277 if(connect(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1) {
284 #ifdef HAVE_ABSTRACT_SOCKETS
285 static int _xcb_open_abstract(char *protocol, const char *file)
288 struct sockaddr_un addr = {0};
291 if (protocol && strcmp("unix",protocol))
294 strcpy(addr.sun_path + 1, file);
295 addr.sun_family = AF_UNIX;
296 namelen = offsetof(struct sockaddr_un, sun_path) + 1 + strlen(file);
297 #ifdef HAVE_SOCKADDR_SUN_LEN
298 addr.sun_len = 1 + strlen(file);
300 fd = socket(AF_UNIX, SOCK_STREAM, 0);
303 if (connect(fd, (struct sockaddr *) &addr, namelen) == -1) {
311 xcb_connection_t *xcb_connect(const char *displayname, int *screenp)
317 xcb_auth_info_t auth;
319 if(!_xcb_parse_display(displayname, &host, &protocol, &display, screenp))
320 return (xcb_connection_t *) &error_connection;
321 fd = _xcb_open(host, protocol, display);
324 return (xcb_connection_t *) &error_connection;
326 if(_xcb_get_auth_info(fd, &auth, display))
328 c = xcb_connect_to_fd(fd, &auth);
333 c = xcb_connect_to_fd(fd, 0);
337 xcb_connection_t *xcb_connect_to_display_with_auth_info(const char *displayname, xcb_auth_info_t *auth, int *screenp)
343 if(!_xcb_parse_display(displayname, &host, &protocol, &display, screenp))
344 return (xcb_connection_t *) &error_connection;
345 fd = _xcb_open(host, protocol, display);
348 return (xcb_connection_t *) &error_connection;
350 return xcb_connect_to_fd(fd, auth);