Add support for DECnet. Still needs configure-script options to enable.
[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 #ifdef DNETCONN
33 #include <netdnet/dnetdb.h>
34 #include <netdnet/dn.h>
35 #endif
36 #include <netdb.h>
37 #include <errno.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <unistd.h>
41 #include <string.h>
42
43 #include "xcb.h"
44 #include "xcbext.h"
45 #include "xcbint.h"
46
47 int XCBPopcount(CARD32 mask)
48 {
49     unsigned long y;
50     y = (mask >> 1) & 033333333333;
51     y = mask - y - ((y >> 1) & 033333333333);
52     return ((y + (y >> 3)) & 030707070707) % 077;
53 }
54
55 int XCBParseDisplay(const char *name, char **host, int *displayp, int *screenp)
56 {
57     int len, display, screen;
58     char *colon, *dot, *end;
59     if(!name || !*name)
60         name = getenv("DISPLAY");
61     if(!name)
62         return 0;
63     colon = strrchr(name, ':');
64     if(!colon)
65         return 0;
66     len = colon - name;
67     ++colon;
68     display = strtoul(colon, &dot, 10);
69     if(dot == colon)
70         return 0;
71     if(*dot == '\0')
72         screen = 0;
73     else
74     {
75         if(*dot != '.')
76             return 0;
77         ++dot;
78         screen = strtoul(dot, &end, 10);
79         if(end == dot || *end != '\0')
80             return 0;
81     }
82     /* At this point, the display string is fully parsed and valid, but
83      * the caller's memory is untouched. */
84
85     *host = malloc(len + 1);
86     if(!*host)
87         return 0;
88     memcpy(*host, name, len);
89     (*host)[len] = '\0';
90     *displayp = display;
91     if(screenp)
92         *screenp = screen;
93     return 1;
94 }
95
96 static int _xcb_open_tcp(const char *host, const unsigned short port);
97 static int _xcb_open_unix(const char *file);
98 #ifdef DNETCONN
99 static int _xcb_open_decnet(const char *host, const unsigned short port);
100 #endif
101
102 static int _xcb_open(const char *host, const int display)
103 {
104     int fd;
105
106     if(*host)
107     {
108 #ifdef DNETCONN
109         if (strchr(host,  ':'))
110         {
111             /* DECnet displays have two colons, so the parser will have left
112                one at the end */
113             char *dnethost = strdup(host);
114
115             dnethost[strlen(dnethost)-1] = '\0';
116             fd = _xcb_open_decnet(dnethost, display);
117             free(dnethost);
118         }
119         else
120 #endif
121         {
122             /* display specifies TCP */
123             unsigned short port = X_TCP_PORT + display;
124             fd = _xcb_open_tcp(host, port);
125         }
126     }
127     else
128     {
129         /* display specifies Unix socket */
130         static const char base[] = "/tmp/.X11-unix/X";
131         char file[sizeof(base) + 20];
132         snprintf(file, sizeof(file), "%s%d", base, display);
133         fd = _xcb_open_unix(file);
134     }
135
136     return fd;
137 }
138
139 #ifdef DNETCONN
140 static int _xcb_open_decnet(const char *host, const unsigned short port)
141 {
142     int fd;
143     struct sockaddr_dn addr;
144     struct accessdata_dn accessdata;
145     struct nodeent *nodeaddr = getnodebyname(host);
146
147     if(!nodeaddr)
148         return -1;
149     addr.sdn_family = AF_DECnet;
150
151     addr.sdn_add.a_len = nodeaddr->n_length;
152     memcpy(addr.sdn_add.a_addr, nodeaddr->n_addr, addr.sdn_add.a_len);
153
154     sprintf((char *)addr.sdn_objname, "X$X%d", port);
155     addr.sdn_objnamel = strlen((char *)addr.sdn_objname);
156     addr.sdn_objnum = 0;
157
158     fd = socket(PF_DECnet, SOCK_STREAM, 0);
159     if(fd == -1)
160         return -1;
161
162     memset(&accessdata, 0, sizeof(accessdata));
163     sprintf((char*)accessdata.acc_acc, "%d", getuid());
164     accessdata.acc_accl = strlen((char *)accessdata.acc_acc);
165     setsockopt(fd, DNPROTO_NSP, SO_CONACCESS, &accessdata, sizeof(accessdata));
166
167     if(connect(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1)
168         return -1;
169     return fd;
170 }
171 #endif
172
173 static int _xcb_open_tcp(const char *host, const unsigned short port)
174 {
175     int fd;
176     struct sockaddr_in addr;
177     struct hostent *hostaddr = gethostbyname(host);
178     if(!hostaddr)
179         return -1;
180     addr.sin_family = AF_INET;
181     addr.sin_port = htons(port);
182     memcpy(&addr.sin_addr, hostaddr->h_addr_list[0], sizeof(addr.sin_addr));
183
184     fd = socket(PF_INET, SOCK_STREAM, 0);
185     if(fd == -1)
186         return -1;
187     if(connect(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1)
188         return -1;
189     return fd;
190 }
191
192 static int _xcb_open_unix(const char *file)
193 {
194     int fd;
195     struct sockaddr_un addr = { AF_UNIX };
196     strcpy(addr.sun_path, file);
197
198     fd = socket(AF_UNIX, SOCK_STREAM, 0);
199     if(fd == -1)
200         return -1;
201     if(connect(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1)
202         return -1;
203     return fd;
204 }
205
206 XCBConnection *XCBConnect(const char *displayname, int *screenp)
207 {
208     int fd, display = 0;
209     char *host;
210     XCBConnection *c;
211     XCBAuthInfo auth;
212
213     if(!XCBParseDisplay(displayname, &host, &display, screenp))
214         return 0;
215     fd = _xcb_open(host, display);
216     free(host);
217     if(fd == -1)
218         return 0;
219
220     _xcb_get_auth_info(fd, &auth);
221     c = XCBConnectToFD(fd, &auth);
222     free(auth.name);
223     free(auth.data);
224     return c;
225 }
226
227 XCBConnection *XCBConnectToDisplayWithAuthInfo(const char *displayname, XCBAuthInfo *auth, int *screenp)
228 {
229     int fd, display = 0;
230     char *host;
231
232     if(!XCBParseDisplay(displayname, &host, &display, screenp))
233         return 0;
234     fd = _xcb_open(host, display);
235     free(host);
236     if(fd == -1)
237         return 0;
238
239     return XCBConnectToFD(fd, auth);
240 }
241
242 int XCBSync(XCBConnection *c, XCBGenericError **e)
243 {
244     XCBGetInputFocusRep *reply = XCBGetInputFocusReply(c, XCBGetInputFocus(c), e);
245     free(reply);
246     return reply != 0;
247 }
248
249
250
251
252 /* backwards compatible interfaces: remove before 1.0 release */
253 XCBConnection *XCBConnectBasic()
254 {
255     XCBConnection *c = XCBConnect(0, 0);
256     if(c)
257         return c;
258     perror("XCBConnect");
259     abort();
260 }
261
262 int XCBOpen(const char *host, const int display)
263 {
264         return _xcb_open(host, display);
265 }
266
267 int XCBOpenTCP(const char *host, const unsigned short port)
268 {
269         return _xcb_open_tcp(host, port);
270 }
271
272 int XCBOpenUnix(const char *file)
273 {
274         return _xcb_open_unix(file);
275 }
276
277 int XCBGetAuthInfo(int fd, XCBAuthInfo *info)
278 {
279         return _xcb_get_auth_info(fd, info);
280 }