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