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