Convert connection functions to return error objects.
[free-sw/xcb/libxcb] / src / xcb_util.c
1 /* Copyright (C) 2001-2004 Bart Massey and Jamey Sharp.
2  *
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:
9  * 
10  * The above copyright notice and this permission notice shall be included in
11  * all copies or substantial portions of the Software.
12  * 
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.
19  * 
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.
24  */
25
26 /* Utility functions implementable using only public APIs. */
27
28 #include <assert.h>
29 #include <sys/types.h>
30 #include <sys/socket.h>
31 #include <sys/un.h>
32 #include <netinet/in.h>
33 #ifdef DNETCONN
34 #include <netdnet/dnetdb.h>
35 #include <netdnet/dn.h>
36 #endif
37 #include <netdb.h>
38 #include <errno.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <unistd.h>
42 #include <string.h>
43
44 #include "xcb.h"
45 #include "xcbext.h"
46 #include "xcbint.h"
47
48 static const int error_connection = 1;
49
50 int XCBPopcount(CARD32 mask)
51 {
52     unsigned long y;
53     y = (mask >> 1) & 033333333333;
54     y = mask - y - ((y >> 1) & 033333333333);
55     return ((y + (y >> 3)) & 030707070707) % 077;
56 }
57
58 int XCBParseDisplay(const char *name, char **host, int *displayp, int *screenp)
59 {
60     int len, display, screen;
61     char *colon, *dot, *end;
62     if(!name || !*name)
63         name = getenv("DISPLAY");
64     if(!name)
65         return 0;
66     colon = strrchr(name, ':');
67     if(!colon)
68         return 0;
69     len = colon - name;
70     ++colon;
71     display = strtoul(colon, &dot, 10);
72     if(dot == colon)
73         return 0;
74     if(*dot == '\0')
75         screen = 0;
76     else
77     {
78         if(*dot != '.')
79             return 0;
80         ++dot;
81         screen = strtoul(dot, &end, 10);
82         if(end == dot || *end != '\0')
83             return 0;
84     }
85     /* At this point, the display string is fully parsed and valid, but
86      * the caller's memory is untouched. */
87
88     *host = malloc(len + 1);
89     if(!*host)
90         return 0;
91     memcpy(*host, name, len);
92     (*host)[len] = '\0';
93     *displayp = display;
94     if(screenp)
95         *screenp = screen;
96     return 1;
97 }
98
99 static int _xcb_open_tcp(const char *host, const unsigned short port);
100 static int _xcb_open_unix(const char *file);
101 #ifdef DNETCONN
102 static int _xcb_open_decnet(const char *host, const unsigned short port);
103 #endif
104
105 static int _xcb_open(const char *host, const int display)
106 {
107     int fd;
108
109     if(*host)
110     {
111 #ifdef DNETCONN
112         if (strchr(host,  ':'))
113         {
114             /* DECnet displays have two colons, so the parser will have left
115                one at the end */
116             char *dnethost = strdup(host);
117
118             dnethost[strlen(dnethost)-1] = '\0';
119             fd = _xcb_open_decnet(dnethost, display);
120             free(dnethost);
121         }
122         else
123 #endif
124         {
125             /* display specifies TCP */
126             unsigned short port = X_TCP_PORT + display;
127             fd = _xcb_open_tcp(host, port);
128         }
129     }
130     else
131     {
132         /* display specifies Unix socket */
133         static const char base[] = "/tmp/.X11-unix/X";
134         char file[sizeof(base) + 20];
135         snprintf(file, sizeof(file), "%s%d", base, display);
136         fd = _xcb_open_unix(file);
137     }
138
139     return fd;
140 }
141
142 #ifdef DNETCONN
143 static int _xcb_open_decnet(const char *host, const unsigned short port)
144 {
145     int fd;
146     struct sockaddr_dn addr;
147     struct accessdata_dn accessdata;
148     struct nodeent *nodeaddr = getnodebyname(host);
149
150     if(!nodeaddr)
151         return -1;
152     addr.sdn_family = AF_DECnet;
153
154     addr.sdn_add.a_len = nodeaddr->n_length;
155     memcpy(addr.sdn_add.a_addr, nodeaddr->n_addr, addr.sdn_add.a_len);
156
157     sprintf((char *)addr.sdn_objname, "X$X%d", port);
158     addr.sdn_objnamel = strlen((char *)addr.sdn_objname);
159     addr.sdn_objnum = 0;
160
161     fd = socket(PF_DECnet, SOCK_STREAM, 0);
162     if(fd == -1)
163         return -1;
164
165     memset(&accessdata, 0, sizeof(accessdata));
166     sprintf((char*)accessdata.acc_acc, "%d", getuid());
167     accessdata.acc_accl = strlen((char *)accessdata.acc_acc);
168     setsockopt(fd, DNPROTO_NSP, SO_CONACCESS, &accessdata, sizeof(accessdata));
169
170     if(connect(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1)
171         return -1;
172     return fd;
173 }
174 #endif
175
176 static int _xcb_open_tcp(const char *host, const unsigned short port)
177 {
178     int fd;
179     struct sockaddr_in addr;
180     struct hostent *hostaddr = gethostbyname(host);
181     if(!hostaddr)
182         return -1;
183     addr.sin_family = AF_INET;
184     addr.sin_port = htons(port);
185     memcpy(&addr.sin_addr, hostaddr->h_addr_list[0], sizeof(addr.sin_addr));
186
187     fd = socket(PF_INET, SOCK_STREAM, 0);
188     if(fd == -1)
189         return -1;
190     if(connect(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1)
191         return -1;
192     return fd;
193 }
194
195 static int _xcb_open_unix(const char *file)
196 {
197     int fd;
198     struct sockaddr_un addr = { AF_UNIX };
199     strcpy(addr.sun_path, file);
200
201     fd = socket(AF_UNIX, SOCK_STREAM, 0);
202     if(fd == -1)
203         return -1;
204     if(connect(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1)
205         return -1;
206     return fd;
207 }
208
209 XCBConnection *XCBConnect(const char *displayname, int *screenp)
210 {
211     int fd, display = 0;
212     char *host;
213     XCBConnection *c;
214     XCBAuthInfo auth;
215
216     if(!XCBParseDisplay(displayname, &host, &display, screenp))
217         return (XCBConnection *) &error_connection;
218     fd = _xcb_open(host, display);
219     free(host);
220     if(fd == -1)
221         return (XCBConnection *) &error_connection;
222
223     _xcb_get_auth_info(fd, &auth);
224     c = XCBConnectToFD(fd, &auth);
225     free(auth.name);
226     free(auth.data);
227     return c;
228 }
229
230 XCBConnection *XCBConnectToDisplayWithAuthInfo(const char *displayname, XCBAuthInfo *auth, int *screenp)
231 {
232     int fd, display = 0;
233     char *host;
234
235     if(!XCBParseDisplay(displayname, &host, &display, screenp))
236         return (XCBConnection *) &error_connection;
237     fd = _xcb_open(host, display);
238     free(host);
239     if(fd == -1)
240         return (XCBConnection *) &error_connection;
241
242     return XCBConnectToFD(fd, auth);
243 }