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