FSOpenServ.c revision da1f2d5d
1/*
2 * Copyright 1990 Network Computing Devices;
3 * Portions Copyright 1987 by Digital Equipment Corporation
4 *
5 * Permission to use, copy, modify, distribute, and sell this software
6 * and its documentation for any purpose is hereby granted without fee,
7 * provided that the above copyright notice appear in all copies and
8 * that both that copyright notice and this permission notice appear
9 * in supporting documentation, and that the names of Network Computing
10 * Devices or Digital not be used in advertising or publicity pertaining
11 * to distribution of the software without specific, written prior
12 * permission. Network Computing Devices or Digital make no representations
13 * about the suitability of this software for any purpose.  It is provided
14 * "as is" without express or implied warranty.
15 *
16 * NETWORK COMPUTING DEVICES AND  DIGITAL DISCLAIM ALL WARRANTIES WITH
17 * REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL NETWORK COMPUTING DEVICES
19 * OR DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES
20 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
21 * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
22 * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
23 * SOFTWARE.
24 */
25
26/*
27
28Copyright 1987, 1994, 1998  The Open Group
29
30Permission to use, copy, modify, distribute, and sell this software and its
31documentation for any purpose is hereby granted without fee, provided that
32the above copyright notice appear in all copies and that both that
33copyright notice and this permission notice appear in supporting
34documentation.
35
36The above copyright notice and this permission notice shall be included in
37all copies or substantial portions of the Software.
38
39THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
40IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
41FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
42OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
43AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
44CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
45
46Except as contained in this notice, the name of The Open Group shall not be
47used in advertising or otherwise to promote the sale, use or other dealings
48in this Software without prior written authorization from The Open Group.
49
50*/
51
52/*
53 * does initial handshake w/ font server
54 */
55
56#ifdef HAVE_CONFIG_H
57#include <config.h>
58#endif
59#include	<stdio.h>
60#include	"FSlibint.h"
61#include 	<X11/Xtrans/Xtrans.h>
62
63static int _FSdebug = 0;
64
65static fsReq _dummy_request = {
66    0, 0, 0
67};
68
69static void OutOfMemory ( FSServer *svr );
70
71FSServer   *_FSHeadOfServerList = NULL;
72
73void _FSFreeServerStructure(FSServer *svr)
74{
75    if (svr->server_name)
76	FSfree(svr->server_name);
77    if (svr->vendor)
78	FSfree(svr->vendor);
79
80    if (svr->buffer)
81	FSfree(svr->buffer);
82
83    FSfree(svr);
84}
85
86static
87void OutOfMemory(
88    FSServer	*svr)
89{
90    if (svr->trans_conn)
91	_FSDisconnectServer(svr->trans_conn);
92    _FSFreeServerStructure(svr);
93    errno = ENOMEM;
94}
95
96/*
97 * connects to a server, makes a FSServer object and returns a pointer
98 * to it
99 */
100
101FSServer   *
102FSOpenServer(const char *server)
103{
104    FSServer   *svr;
105    int         i;
106    int         endian;
107    fsConnClientPrefix client;
108    fsConnSetup prefix;
109    char       *setup = NULL;
110    fsConnSetupAccept conn;
111    char       *auth_data = NULL;
112    unsigned char *alt_data = NULL,
113               *ad;
114    AlternateServer *alts = NULL;
115    unsigned int altlen;
116    char       *vendor_string;
117    unsigned long        setuplength;
118
119    if (server == NULL || *server == '\0') {
120	if ((server = getenv("FONTSERVER")) == NULL) {
121	    return (FSServer *) NULL;
122	}
123    }
124
125    if ((svr = FScalloc(1, sizeof(FSServer))) == NULL) {
126	errno = ENOMEM;
127	return (FSServer *) NULL;
128    }
129
130    if ((svr->server_name = strdup(server)) == NULL) {
131	goto fail;
132    }
133
134    if ((svr->trans_conn = _FSConnectServer(svr->server_name)) == NULL) {
135	goto fail;
136    }
137
138    svr->fd = _FSTransGetConnectionNumber (svr->trans_conn);
139
140    endian = 1;
141    if (*(char *) &endian)
142	client.byteOrder = 'l';
143    else
144	client.byteOrder = 'B';
145    client.major_version = FS_PROTOCOL;
146    client.minor_version = FS_PROTOCOL_MINOR;
147/* XXX -- fix this when we have some auths */
148    client.num_auths = 0;
149    client.auth_len = 0;
150    _FSSendClientPrefix(svr, &client);
151
152/* see if connection was accepted */
153    _FSRead(svr, (char *) &prefix, (long) SIZEOF(fsConnSetup));
154
155    setuplength = prefix.alternate_len << 2;
156    if (setuplength > (SIZE_MAX>>2)
157	|| (alt_data = (unsigned char *)
158	 (setup = FSmalloc(setuplength))) == NULL) {
159	goto fail;
160    }
161    _FSRead(svr, (char *) alt_data, setuplength);
162    ad = alt_data;
163
164#if SIZE_MAX <= UINT_MAX
165    if (prefix.num_alternates > SIZE_MAX / sizeof(AlternateServer)) {
166	goto fail;
167    }
168#endif
169
170    alts = FSmallocarray(prefix.num_alternates, sizeof(AlternateServer));
171    if (!alts) {
172	goto fail;
173    }
174    for (i = 0; i < prefix.num_alternates; i++) {
175	alts[i].subset = (Bool) *ad++;
176	altlen = (unsigned int) *ad++;
177	alts[i].name = FSmalloc(altlen + 1);
178	if (!alts[i].name) {
179	    while (--i >= 0) {
180		FSfree(alts[i].name);
181	    }
182	    goto fail;
183	}
184	memmove(alts[i].name, ad, altlen);
185	alts[i].name[altlen] = '\0';
186	ad += altlen + ((4 - (altlen + 2)) & 3);
187    }
188    FSfree(alt_data);
189    alt_data = NULL;
190
191    svr->alternate_servers = alts;
192    svr->num_alternates = prefix.num_alternates;
193
194    setuplength = prefix.auth_len << 2;
195    if (setuplength > (SIZE_MAX>>2)
196	|| (auth_data = (char *)
197	 (setup = FSmalloc(setuplength))) == NULL) {
198	goto fail;
199    }
200    _FSRead(svr, (char *) auth_data, setuplength);
201
202    if (prefix.status != AuthSuccess) {
203	fprintf(stderr, "%s: connection to \"%s\" refused by server\r\n%s: ",
204		"FSlib", server, "FSlib");
205	goto fail;
206    }
207    /* get rest */
208    _FSRead(svr, (char *) &conn, (long) SIZEOF(fsConnSetupAccept));
209
210    if ((vendor_string = FSmalloc(conn.vendor_len + 1)) == NULL) {
211	goto fail;
212    }
213    _FSReadPad(svr, (char *) vendor_string, conn.vendor_len);
214
215    /* move the data into the FSServer struct */
216    svr->next = (FSServer *) NULL;
217    svr->proto_version = prefix.major_version;
218    svr->release = conn.release_number;
219    svr->max_request_size = conn.max_request_len;
220
221    svr->event_vec[FS_Error] = _FSUnknownWireEvent;
222    svr->event_vec[FS_Reply] = _FSUnknownWireEvent;
223    svr->wire_vec[FS_Error] = _FSUnknownNativeEvent;
224    svr->wire_vec[FS_Reply] = _FSUnknownNativeEvent;
225    for (i = FSLASTEvent; i < 128; i++) {
226	svr->event_vec[i] = _FSUnknownWireEvent;
227	svr->wire_vec[i] = _FSUnknownNativeEvent;
228    }
229    svr->resource_id = 1;
230
231    svr->vendor = vendor_string;
232    svr->vendor[conn.vendor_len] = '\0';
233
234    svr->vnumber = FS_PROTOCOL;
235    svr->request = 0;
236    svr->last_request_read = 0;
237    svr->last_req = (char *) &_dummy_request;
238
239    /* setup the output buffers */
240    if ((svr->bufptr = svr->buffer = FSmalloc(BUFSIZE)) == NULL) {
241	goto fail;
242    }
243    svr->bufmax = svr->buffer + BUFSIZE;
244
245    /* set up input event queue */
246    svr->head = svr->tail = NULL;
247    svr->qlen = 0;
248
249    FSfree(setup);
250    setup = NULL;
251
252    (void) FSSynchronize(svr, _FSdebug);
253
254    svr->next = _FSHeadOfServerList;
255    _FSHeadOfServerList = svr;
256
257    return (svr);
258
259  fail: /* Failure: clean up and return null */
260    FSfree(alts);
261    FSfree(alt_data);
262    FSfree(auth_data);
263    OutOfMemory(svr);
264    return (FSServer *) NULL;
265
266}
267