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