accept.c revision 266e564d
1/* $Xorg: accept.c,v 1.4 2001/02/09 02:03:26 xorgcvs Exp $ */
2/******************************************************************************
3
4
5Copyright 1993, 1998  The Open Group
6
7Permission to use, copy, modify, distribute, and sell this software and its
8documentation for any purpose is hereby granted without fee, provided that
9the above copyright notice appear in all copies and that both that
10copyright notice and this permission notice appear in supporting
11documentation.
12
13The above copyright notice and this permission notice shall be included in
14all copies or substantial portions of the Software.
15
16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
19OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
20AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
23Except as contained in this notice, the name of The Open Group shall not be
24used in advertising or otherwise to promote the sale, use or other dealings
25in this Software without prior written authorization from The Open Group.
26
27Author: Ralph Mor, X Consortium
28******************************************************************************/
29
30#ifdef HAVE_CONFIG_H
31#include <config.h>
32#endif
33#include <X11/ICE/ICElib.h>
34#include "ICElibint.h"
35#include <X11/Xtrans/Xtrans.h>
36
37
38IceConn
39IceAcceptConnection (listenObj, statusRet)
40
41IceListenObj 	listenObj;
42IceAcceptStatus	*statusRet;
43
44{
45    IceConn    		iceConn;
46    XtransConnInfo	newconn;
47    iceByteOrderMsg 	*pMsg;
48    int   		endian, status;
49
50    /*
51     * Accept the connection.
52     */
53
54    if ((newconn = _IceTransAccept (listenObj->trans_conn, &status)) == 0)
55    {
56	if (status == TRANS_ACCEPT_BAD_MALLOC)
57	    *statusRet = IceAcceptBadMalloc;
58	else
59	    *statusRet = IceAcceptFailure;
60	return (NULL);
61    }
62
63
64    /*
65     * Set close-on-exec so that programs that fork() don't get confused.
66     */
67
68    _IceTransSetOption (newconn, TRANS_CLOSEONEXEC, 1);
69
70
71    /*
72     * Create an ICE object for this connection.
73     */
74
75    if ((iceConn = (IceConn) malloc (sizeof (struct _IceConn))) == NULL)
76    {
77	_IceTransClose (newconn);
78	*statusRet = IceAcceptBadMalloc;
79	return (NULL);
80    }
81
82    iceConn->listen_obj = listenObj;
83
84    iceConn->waiting_for_byteorder = True;
85    iceConn->connection_status = IceConnectPending;
86    iceConn->io_ok = True;
87    iceConn->dispatch_level = 0;
88    iceConn->context = NULL;
89    iceConn->my_ice_version_index = 0;
90
91    iceConn->trans_conn = newconn;
92    iceConn->send_sequence = 0;
93    iceConn->receive_sequence = 0;
94
95    iceConn->connection_string = strdup(listenObj->network_id);
96
97    if (iceConn->connection_string == NULL)
98    {
99	_IceTransClose (newconn);
100	free ((char *) iceConn);
101	*statusRet = IceAcceptBadMalloc;
102	return (NULL);
103    }
104
105    iceConn->vendor = NULL;
106    iceConn->release = NULL;
107
108    if ((iceConn->inbuf = iceConn->inbufptr =
109	(char *) malloc (ICE_INBUFSIZE)) != NULL)
110    {
111	iceConn->inbufmax = iceConn->inbuf + ICE_INBUFSIZE;
112    }
113    else
114    {
115	_IceTransClose (newconn);
116	free ((char *) iceConn);
117	*statusRet = IceAcceptBadMalloc;
118	return (NULL);
119    }
120
121    if ((iceConn->outbuf = iceConn->outbufptr =
122	(char *) malloc (ICE_OUTBUFSIZE)) != NULL)
123    {
124	iceConn->outbufmax = iceConn->outbuf + ICE_OUTBUFSIZE;
125    }
126    else
127    {
128	_IceTransClose (newconn);
129	free (iceConn->inbuf);
130	free ((char *) iceConn);
131	*statusRet = IceAcceptBadMalloc;
132	return (NULL);
133    }
134
135    iceConn->scratch = NULL;
136    iceConn->scratch_size = 0;
137
138    iceConn->open_ref_count = 1;
139    iceConn->proto_ref_count = 0;
140
141    iceConn->skip_want_to_close = False;
142    iceConn->want_to_close = False;
143    iceConn->free_asap = False;
144
145    iceConn->saved_reply_waits = NULL;
146    iceConn->ping_waits = NULL;
147
148    iceConn->process_msg_info = NULL;
149
150    iceConn->connect_to_you = NULL;
151    iceConn->protosetup_to_you = NULL;
152
153    iceConn->connect_to_me = NULL;
154    iceConn->protosetup_to_me = NULL;
155
156
157    /*
158     * Send our byte order.
159     */
160
161    IceGetHeader (iceConn, 0, ICE_ByteOrder,
162	SIZEOF (iceByteOrderMsg), iceByteOrderMsg, pMsg);
163
164    endian = 1;
165    if (*(char *) &endian)
166	pMsg->byteOrder = IceLSBfirst;
167    else
168	pMsg->byteOrder = IceMSBfirst;
169
170    IceFlush (iceConn);
171
172
173    if (_IceWatchProcs)
174    {
175	/*
176	 * Notify the watch procedures that an iceConn was opened.
177	 */
178
179	_IceConnectionOpened (iceConn);
180    }
181
182    *statusRet = IceAcceptSuccess;
183
184    return (iceConn);
185}
186