LiHosts.c revision e9fcaa8a
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 2004 Oracle and/or its affiliates. All rights reserved.
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/*
66 * can be freed using XFree.
67 */
68
69XHostAddress *XListHosts (
70    register Display *dpy,
71    int *nhosts,	/* RETURN */
72    Bool *enabled)	/* RETURN */
73{
74    register XHostAddress *outbuf = NULL, *op;
75    xListHostsReply reply;
76    long nbytes;
77    unsigned char *buf, *bp;
78    register unsigned i;
79    register xListHostsReq *req;
80    XServerInterpretedAddress *sip;
81
82    *nhosts = 0;
83    LockDisplay(dpy);
84    GetReq (ListHosts, req);
85
86    if (!_XReply (dpy, (xReply *) &reply, 0, xFalse)) {
87       UnlockDisplay(dpy);
88       SyncHandle();
89       return (XHostAddress *) NULL;
90    }
91
92    if (reply.nHosts) {
93	nbytes = reply.length << 2;	/* compute number of bytes in reply */
94
95	op = outbuf = (XHostAddress *)
96	    Xmalloc((unsigned) (nbytes +
97	      (reply.nHosts * sizeof(XHostAddress)) +
98	      (reply.nHosts * sizeof(XServerInterpretedAddress))));
99
100	if (! outbuf) {
101	    _XEatData(dpy, (unsigned long) nbytes);
102	    UnlockDisplay(dpy);
103	    SyncHandle();
104	    return (XHostAddress *) NULL;
105	}
106	sip = (XServerInterpretedAddress *)
107	 (((unsigned char  *) outbuf) + (reply.nHosts * sizeof(XHostAddress)));
108	bp = buf = ((unsigned char  *) sip)
109	  + (reply.nHosts * sizeof(XServerInterpretedAddress));
110
111	_XRead (dpy, (char *) buf, nbytes);
112
113	for (i = 0; i < reply.nHosts; i++) {
114#ifdef WORD64
115	    xHostEntry xhe;
116	    memcpy((char *)&xhe, bp, SIZEOF(xHostEntry));
117	    op->family = xhe.family;
118	    op->length = xhe.length;
119#else
120	    op->family = ((xHostEntry *) bp)->family;
121	    op->length =((xHostEntry *) bp)->length;
122#endif
123	    if (op->family == FamilyServerInterpreted) {
124		char *tp = (char *) (bp + SIZEOF(xHostEntry));
125		char *vp = memchr(tp, 0, op->length);
126
127		if (vp != NULL) {
128		    sip->type = tp;
129		    sip->typelength = vp - tp;
130		    sip->value = vp + 1;
131		    sip->valuelength = op->length - (sip->typelength + 1);
132		} else {
133		    sip->type = sip->value = NULL;
134		    sip->typelength = sip->valuelength = 0;
135		}
136		op->address = (char *) sip;
137		sip++;
138	    } else {
139		op->address = (char *) (bp + SIZEOF(xHostEntry));
140	    }
141	    bp += SIZEOF(xHostEntry) + (((op->length + 3) >> 2) << 2);
142	    op++;
143	}
144    }
145
146    *enabled = reply.enabled;
147    *nhosts = reply.nHosts;
148    UnlockDisplay(dpy);
149    SyncHandle();
150    return (outbuf);
151}
152
153
154
155
156
157