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