1/*
2
3Copyright 1986, 1998  The Open Group
4
5All rights reserved.
6
7Permission is hereby granted, free of charge, to any person obtaining a
8copy of this software and associated documentation files (the
9"Software"), to deal in the Software without restriction, including
10without limitation the rights to use, copy, modify, merge, publish,
11distribute, and/or sell copies of the Software, and to permit persons
12to whom the Software is furnished to do so, provided that the above
13copyright notice(s) and this permission notice appear in all copies of
14the Software and that both the above copyright notice(s) and this
15permission notice appear in supporting documentation.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
20OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
21HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
22INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
23FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
24NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
25WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26
27Except as contained in this notice, the name of a copyright holder
28shall not be used in advertising or otherwise to promote the sale, use
29or other dealings in this Software without prior written authorization
30of the copyright holder.
31
32X Window System is a trademark of The Open Group.
33
34*/
35
36/*
37 * Copyright (c) 2004, Oracle and/or its affiliates.
38 *
39 * Permission is hereby granted, free of charge, to any person obtaining a
40 * copy of this software and associated documentation files (the "Software"),
41 * to deal in the Software without restriction, including without limitation
42 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
43 * and/or sell copies of the Software, and to permit persons to whom the
44 * Software is furnished to do so, subject to the following conditions:
45 *
46 * The above copyright notice and this permission notice (including the next
47 * paragraph) shall be included in all copies or substantial portions of the
48 * Software.
49 *
50 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
51 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
52 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
53 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
54 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
55 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
56 * DEALINGS IN THE SOFTWARE.
57 */
58
59/* This can really be considered an os dependent routine */
60
61#ifdef HAVE_CONFIG_H
62#include <config.h>
63#endif
64#include "Xlibint.h"
65#include <limits.h>
66
67/*
68 * can be freed using XFree.
69 */
70
71XHostAddress *XListHosts (
72    register Display *dpy,
73    int *nhosts,	/* RETURN */
74    Bool *enabled)	/* RETURN */
75{
76    register XHostAddress *outbuf = NULL, *op;
77    xListHostsReply reply;
78    unsigned char *buf, *bp;
79    register unsigned i;
80    _X_UNUSED register xListHostsReq *req;
81    XServerInterpretedAddress *sip;
82
83    *nhosts = 0;
84    LockDisplay(dpy);
85    GetReq (ListHosts, req);
86
87    if (!_XReply (dpy, (xReply *) &reply, 0, xFalse)) {
88       UnlockDisplay(dpy);
89       SyncHandle();
90       return (XHostAddress *) NULL;
91    }
92
93    if (reply.nHosts) {
94	unsigned long nbytes = reply.length << 2; /* number of bytes in reply */
95	const unsigned long max_hosts = INT_MAX /
96	    (sizeof(XHostAddress) + sizeof(XServerInterpretedAddress));
97
98	if (reply.nHosts < max_hosts) {
99	    unsigned long hostbytes = reply.nHosts *
100		(sizeof(XHostAddress) + sizeof(XServerInterpretedAddress));
101
102	    if (reply.length < (INT_MAX >> 2) &&
103		(hostbytes >> 2) < ((INT_MAX >> 2) - reply.length))
104		outbuf = Xmalloc(nbytes + hostbytes);
105	}
106
107	if (! outbuf) {
108	    _XEatDataWords(dpy, reply.length);
109	    UnlockDisplay(dpy);
110	    SyncHandle();
111	    return (XHostAddress *) NULL;
112	}
113	op = outbuf;
114	sip = (XServerInterpretedAddress *)
115	 (((unsigned char  *) outbuf) + (reply.nHosts * sizeof(XHostAddress)));
116	bp = buf = ((unsigned char  *) sip)
117	  + (reply.nHosts * sizeof(XServerInterpretedAddress));
118
119	_XRead (dpy, (char *) buf, nbytes);
120
121	for (i = 0; i < reply.nHosts; i++) {
122	    if (bp > buf + nbytes - SIZEOF(xHostEntry))
123		goto fail;
124	    op->family = ((xHostEntry *) bp)->family;
125	    op->length =((xHostEntry *) bp)->length;
126	    if (op->family == FamilyServerInterpreted) {
127		char *tp = (char *) (bp + SIZEOF(xHostEntry));
128		char *vp;
129		if (tp > (char *) (buf + nbytes - op->length))
130		    goto fail;
131		vp = memchr(tp, 0, op->length);
132
133		if (vp != NULL) {
134		    sip->type = tp;
135		    sip->typelength = vp - tp;
136		    sip->value = vp + 1;
137		    sip->valuelength = op->length - (sip->typelength + 1);
138		} else {
139		    sip->type = sip->value = NULL;
140		    sip->typelength = sip->valuelength = 0;
141		}
142		op->address = (char *) sip;
143		sip++;
144	    } else {
145		op->address = (char *) (bp + SIZEOF(xHostEntry));
146		if (op->address > (char *) (buf + nbytes - op->length))
147		    goto fail;
148	    }
149	    bp += SIZEOF(xHostEntry) + (((op->length + 3) >> 2) << 2);
150	    op++;
151	}
152    }
153
154    *enabled = reply.enabled;
155    *nhosts = reply.nHosts;
156    UnlockDisplay(dpy);
157    SyncHandle();
158    return (outbuf);
159fail:
160    *enabled = reply.enabled;
161    *nhosts = 0;
162    Xfree(outbuf);
163    return (NULL);
164}
165