1/*
2
3Copyright 1996, 1998  The Open Group
4
5Permission to use, copy, modify, distribute, and sell this software and its
6documentation for any purpose is hereby granted without fee, provided that
7the above copyright notice appear in all copies and that both that
8copyright notice and this permission notice appear in supporting
9documentation.
10
11The above copyright notice and this permission notice shall be included in
12all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
17OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20
21Except as contained in this notice, the name of The Open Group shall not be
22used in advertising or otherwise to promote the sale, use or other dealings
23in this Software without prior written authorization from The Open Group.
24
25*/
26
27
28/* Author: Ralph Mor, X Consortium */
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#include <stdio.h>
37
38
39Status
40IceListenForWellKnownConnections (
41	char		*port,
42	int		*countRet,
43	IceListenObj	**listenObjsRet,
44	int		errorLength,
45	char		*errorStringRet
46)
47{
48    struct _IceListenObj	*listenObjs;
49    char			*networkId;
50    int				transCount, partial, i, j;
51    Status			status = 1;
52    XtransConnInfo		*transConns = NULL;
53
54
55    if ((_IceTransMakeAllCOTSServerListeners (port, &partial,
56	&transCount, &transConns) < 0) || (transCount < 1))
57    {
58	*listenObjsRet = NULL;
59	*countRet = 0;
60
61	if (errorStringRet && errorLength > 0) {
62            strncpy (errorStringRet,
63		"Cannot establish any listening sockets", errorLength);
64	    errorStringRet[errorLength - 1] = '\0';
65	}
66
67	free (transConns);
68	return (0);
69    }
70
71    listenObjs = calloc (transCount, sizeof (struct _IceListenObj));
72    if (listenObjs == NULL)
73    {
74	for (i = 0; i < transCount; i++)
75	    _IceTransClose (transConns[i]);
76	free (transConns);
77
78	strncpy (errorStringRet, "Malloc failed", errorLength);
79	return (0);
80    }
81
82    *countRet = 0;
83
84    for (i = 0; i < transCount; i++)
85    {
86	networkId = _IceTransGetMyNetworkId (transConns[i]);
87
88	if (networkId)
89	{
90	    listenObjs[*countRet].trans_conn = transConns[i];
91	    listenObjs[*countRet].network_id = networkId;
92
93	    (*countRet)++;
94	}
95    }
96
97    if (*countRet == 0)
98    {
99	*listenObjsRet = NULL;
100
101	if (errorStringRet && errorLength > 0) {
102            strncpy (errorStringRet,
103		"Cannot establish any listening sockets", errorLength);
104	    errorStringRet[errorLength - 1] = '\0';
105	}
106
107	status = 0;
108    }
109    else
110    {
111	*listenObjsRet = malloc (*countRet * sizeof (IceListenObj));
112
113	if (*listenObjsRet == NULL)
114	{
115	    if (errorStringRet && errorLength > 0) {
116		strncpy (errorStringRet, "Malloc failed", errorLength);
117		errorStringRet[errorLength - 1] = '\0';
118	    }
119
120	    status = 0;
121	}
122	else
123	{
124	    for (i = 0; i < *countRet; i++)
125	    {
126		(*listenObjsRet)[i] = malloc (sizeof (struct _IceListenObj));
127
128		if ((*listenObjsRet)[i] == NULL)
129		{
130		    if (errorStringRet && errorLength > 0) {
131		        strncpy (errorStringRet, "Malloc failed", errorLength);
132			errorStringRet[errorLength - 1] = '\0';
133		    }
134
135		    for (j = 0; j < i; j++)
136			free ((*listenObjsRet)[j]);
137
138		    free (*listenObjsRet);
139		    *listenObjsRet = NULL;
140
141		    status = 0;
142		    break;
143		}
144		else
145		{
146		    *((*listenObjsRet)[i]) = listenObjs[i];
147		}
148	    }
149	}
150    }
151
152    if (status == 1)
153    {
154	if (errorStringRet && errorLength > 0)
155	    *errorStringRet = '\0';
156
157	for (i = 0; i < *countRet; i++)
158	{
159	    (*listenObjsRet)[i]->host_based_auth_proc = NULL;
160	}
161    }
162    else
163    {
164	for (i = 0; i < transCount; i++)
165	    _IceTransClose (transConns[i]);
166    }
167
168    free (listenObjs);
169    free (transConns);
170
171    return (status);
172}
173