accept.c revision 266e564d
1266e564dSmrg/* $Xorg: accept.c,v 1.4 2001/02/09 02:03:26 xorgcvs Exp $ */
2266e564dSmrg/******************************************************************************
3266e564dSmrg
4266e564dSmrg
5266e564dSmrgCopyright 1993, 1998  The Open Group
6266e564dSmrg
7266e564dSmrgPermission to use, copy, modify, distribute, and sell this software and its
8266e564dSmrgdocumentation for any purpose is hereby granted without fee, provided that
9266e564dSmrgthe above copyright notice appear in all copies and that both that
10266e564dSmrgcopyright notice and this permission notice appear in supporting
11266e564dSmrgdocumentation.
12266e564dSmrg
13266e564dSmrgThe above copyright notice and this permission notice shall be included in
14266e564dSmrgall copies or substantial portions of the Software.
15266e564dSmrg
16266e564dSmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17266e564dSmrgIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18266e564dSmrgFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
19266e564dSmrgOPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
20266e564dSmrgAN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21266e564dSmrgCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22266e564dSmrg
23266e564dSmrgExcept as contained in this notice, the name of The Open Group shall not be
24266e564dSmrgused in advertising or otherwise to promote the sale, use or other dealings
25266e564dSmrgin this Software without prior written authorization from The Open Group.
26266e564dSmrg
27266e564dSmrgAuthor: Ralph Mor, X Consortium
28266e564dSmrg******************************************************************************/
29266e564dSmrg
30266e564dSmrg#ifdef HAVE_CONFIG_H
31266e564dSmrg#include <config.h>
32266e564dSmrg#endif
33266e564dSmrg#include <X11/ICE/ICElib.h>
34266e564dSmrg#include "ICElibint.h"
35266e564dSmrg#include <X11/Xtrans/Xtrans.h>
36266e564dSmrg
37266e564dSmrg
38266e564dSmrgIceConn
39266e564dSmrgIceAcceptConnection (listenObj, statusRet)
40266e564dSmrg
41266e564dSmrgIceListenObj 	listenObj;
42266e564dSmrgIceAcceptStatus	*statusRet;
43266e564dSmrg
44266e564dSmrg{
45266e564dSmrg    IceConn    		iceConn;
46266e564dSmrg    XtransConnInfo	newconn;
47266e564dSmrg    iceByteOrderMsg 	*pMsg;
48266e564dSmrg    int   		endian, status;
49266e564dSmrg
50266e564dSmrg    /*
51266e564dSmrg     * Accept the connection.
52266e564dSmrg     */
53266e564dSmrg
54266e564dSmrg    if ((newconn = _IceTransAccept (listenObj->trans_conn, &status)) == 0)
55266e564dSmrg    {
56266e564dSmrg	if (status == TRANS_ACCEPT_BAD_MALLOC)
57266e564dSmrg	    *statusRet = IceAcceptBadMalloc;
58266e564dSmrg	else
59266e564dSmrg	    *statusRet = IceAcceptFailure;
60266e564dSmrg	return (NULL);
61266e564dSmrg    }
62266e564dSmrg
63266e564dSmrg
64266e564dSmrg    /*
65266e564dSmrg     * Set close-on-exec so that programs that fork() don't get confused.
66266e564dSmrg     */
67266e564dSmrg
68266e564dSmrg    _IceTransSetOption (newconn, TRANS_CLOSEONEXEC, 1);
69266e564dSmrg
70266e564dSmrg
71266e564dSmrg    /*
72266e564dSmrg     * Create an ICE object for this connection.
73266e564dSmrg     */
74266e564dSmrg
75266e564dSmrg    if ((iceConn = (IceConn) malloc (sizeof (struct _IceConn))) == NULL)
76266e564dSmrg    {
77266e564dSmrg	_IceTransClose (newconn);
78266e564dSmrg	*statusRet = IceAcceptBadMalloc;
79266e564dSmrg	return (NULL);
80266e564dSmrg    }
81266e564dSmrg
82266e564dSmrg    iceConn->listen_obj = listenObj;
83266e564dSmrg
84266e564dSmrg    iceConn->waiting_for_byteorder = True;
85266e564dSmrg    iceConn->connection_status = IceConnectPending;
86266e564dSmrg    iceConn->io_ok = True;
87266e564dSmrg    iceConn->dispatch_level = 0;
88266e564dSmrg    iceConn->context = NULL;
89266e564dSmrg    iceConn->my_ice_version_index = 0;
90266e564dSmrg
91266e564dSmrg    iceConn->trans_conn = newconn;
92266e564dSmrg    iceConn->send_sequence = 0;
93266e564dSmrg    iceConn->receive_sequence = 0;
94266e564dSmrg
95266e564dSmrg    iceConn->connection_string = strdup(listenObj->network_id);
96266e564dSmrg
97266e564dSmrg    if (iceConn->connection_string == NULL)
98266e564dSmrg    {
99266e564dSmrg	_IceTransClose (newconn);
100266e564dSmrg	free ((char *) iceConn);
101266e564dSmrg	*statusRet = IceAcceptBadMalloc;
102266e564dSmrg	return (NULL);
103266e564dSmrg    }
104266e564dSmrg
105266e564dSmrg    iceConn->vendor = NULL;
106266e564dSmrg    iceConn->release = NULL;
107266e564dSmrg
108266e564dSmrg    if ((iceConn->inbuf = iceConn->inbufptr =
109266e564dSmrg	(char *) malloc (ICE_INBUFSIZE)) != NULL)
110266e564dSmrg    {
111266e564dSmrg	iceConn->inbufmax = iceConn->inbuf + ICE_INBUFSIZE;
112266e564dSmrg    }
113266e564dSmrg    else
114266e564dSmrg    {
115266e564dSmrg	_IceTransClose (newconn);
116266e564dSmrg	free ((char *) iceConn);
117266e564dSmrg	*statusRet = IceAcceptBadMalloc;
118266e564dSmrg	return (NULL);
119266e564dSmrg    }
120266e564dSmrg
121266e564dSmrg    if ((iceConn->outbuf = iceConn->outbufptr =
122266e564dSmrg	(char *) malloc (ICE_OUTBUFSIZE)) != NULL)
123266e564dSmrg    {
124266e564dSmrg	iceConn->outbufmax = iceConn->outbuf + ICE_OUTBUFSIZE;
125266e564dSmrg    }
126266e564dSmrg    else
127266e564dSmrg    {
128266e564dSmrg	_IceTransClose (newconn);
129266e564dSmrg	free (iceConn->inbuf);
130266e564dSmrg	free ((char *) iceConn);
131266e564dSmrg	*statusRet = IceAcceptBadMalloc;
132266e564dSmrg	return (NULL);
133266e564dSmrg    }
134266e564dSmrg
135266e564dSmrg    iceConn->scratch = NULL;
136266e564dSmrg    iceConn->scratch_size = 0;
137266e564dSmrg
138266e564dSmrg    iceConn->open_ref_count = 1;
139266e564dSmrg    iceConn->proto_ref_count = 0;
140266e564dSmrg
141266e564dSmrg    iceConn->skip_want_to_close = False;
142266e564dSmrg    iceConn->want_to_close = False;
143266e564dSmrg    iceConn->free_asap = False;
144266e564dSmrg
145266e564dSmrg    iceConn->saved_reply_waits = NULL;
146266e564dSmrg    iceConn->ping_waits = NULL;
147266e564dSmrg
148266e564dSmrg    iceConn->process_msg_info = NULL;
149266e564dSmrg
150266e564dSmrg    iceConn->connect_to_you = NULL;
151266e564dSmrg    iceConn->protosetup_to_you = NULL;
152266e564dSmrg
153266e564dSmrg    iceConn->connect_to_me = NULL;
154266e564dSmrg    iceConn->protosetup_to_me = NULL;
155266e564dSmrg
156266e564dSmrg
157266e564dSmrg    /*
158266e564dSmrg     * Send our byte order.
159266e564dSmrg     */
160266e564dSmrg
161266e564dSmrg    IceGetHeader (iceConn, 0, ICE_ByteOrder,
162266e564dSmrg	SIZEOF (iceByteOrderMsg), iceByteOrderMsg, pMsg);
163266e564dSmrg
164266e564dSmrg    endian = 1;
165266e564dSmrg    if (*(char *) &endian)
166266e564dSmrg	pMsg->byteOrder = IceLSBfirst;
167266e564dSmrg    else
168266e564dSmrg	pMsg->byteOrder = IceMSBfirst;
169266e564dSmrg
170266e564dSmrg    IceFlush (iceConn);
171266e564dSmrg
172266e564dSmrg
173266e564dSmrg    if (_IceWatchProcs)
174266e564dSmrg    {
175266e564dSmrg	/*
176266e564dSmrg	 * Notify the watch procedures that an iceConn was opened.
177266e564dSmrg	 */
178266e564dSmrg
179266e564dSmrg	_IceConnectionOpened (iceConn);
180266e564dSmrg    }
181266e564dSmrg
182266e564dSmrg    *statusRet = IceAcceptSuccess;
183266e564dSmrg
184266e564dSmrg    return (iceConn);
185266e564dSmrg}
186