netaddr.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  6. * These libraries and programs are free software; you can
  7. * redistribute them and/or modify them under the terms of the GNU
  8. * Lesser General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. * These libraries and programs are distributed in the hope that
  13. * they will be useful, but WITHOUT ANY WARRANTY; without even the
  14. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  15. * PURPOSE. See the GNU Lesser General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with these libraries and programs; if not, write
  20. * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  21. * Floor, Boston, MA 02110-1301 USA
  22. */
  23. /* $TOG: netaddr.c /main/5 1997/03/14 13:44:57 barstow $ */
  24. /* (c) Copyright 1997 The Open Group */
  25. /* *
  26. * (c) Copyright 1993, 1994 Hewlett-Packard Company *
  27. * (c) Copyright 1993, 1994 International Business Machines Corp. *
  28. * (c) Copyright 1993, 1994 Sun Microsystems, Inc. *
  29. * (c) Copyright 1993, 1994 Novell, Inc. *
  30. */
  31. /*
  32. * @DEC_COPYRIGHT@
  33. */
  34. /*
  35. * HISTORY
  36. * $Log$
  37. * Revision 1.1.2.2 1995/04/21 13:05:31 Peter_Derr
  38. * dtlogin auth key fixes from deltacde
  39. * [1995/04/12 19:21:13 Peter_Derr]
  40. *
  41. * R6 version used for XDMCP improvements
  42. * [1995/04/12 18:32:12 Peter_Derr]
  43. *
  44. * $EndLog$
  45. */
  46. /*
  47. Copyright (c) 1991 X Consortium
  48. Permission is hereby granted, free of charge, to any person obtaining
  49. a copy of this software and associated documentation files (the
  50. "Software"), to deal in the Software without restriction, including
  51. without limitation the rights to use, copy, modify, merge, publish,
  52. distribute, sublicense, and/or sell copies of the Software, and to
  53. permit persons to whom the Software is furnished to do so, subject to
  54. the following conditions:
  55. The above copyright notice and this permission notice shall be included
  56. in all copies or substantial portions of the Software.
  57. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  58. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  59. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  60. IN NO EVENT SHALL THE X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR
  61. OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  62. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  63. OTHER DEALINGS IN THE SOFTWARE.
  64. Except as contained in this notice, the name of the X Consortium shall
  65. not be used in advertising or otherwise to promote the sale, use or
  66. other dealings in this Software without prior written authorization
  67. from the X Consortium.
  68. */
  69. /*
  70. * xdm - X display manager
  71. *
  72. * netaddr.c - Interpretation of XdmcpNetaddr object.
  73. */
  74. #include "dm.h"
  75. #include <X11/X.h> /* FamilyInternet, etc. */
  76. #include <sys/socket.h> /* struct sockaddr */
  77. #include <netinet/in.h> /* struct sockaddr_in */
  78. #ifdef UNIXCONN
  79. #include <sys/un.h> /* struct sockaddr_un */
  80. #endif
  81. #ifdef DNETCONN
  82. #include <netdnet/dn.h> /* struct sockaddr_dn */
  83. #endif
  84. /* given an XdmcpNetaddr, returns the socket protocol family used,
  85. e.g., AF_INET */
  86. int NetaddrFamily(XdmcpNetaddr netaddrp)
  87. {
  88. #ifdef STREAMSCONN
  89. short family = *(short *)netaddrp;
  90. return family;
  91. #else
  92. return ((struct sockaddr *)netaddrp)->sa_family;
  93. #endif
  94. }
  95. /* given an XdmcpNetaddr, returns a pointer to the TCP/UDP port used
  96. and sets *lenp to the length of the address
  97. or 0 if not using TCP or UDP. */
  98. char * NetaddrPort(XdmcpNetaddr netaddrp, int *lenp)
  99. {
  100. #ifdef STREAMSCONN
  101. *lenp = 2;
  102. return netaddrp+2;
  103. #else
  104. switch (NetaddrFamily(netaddrp))
  105. {
  106. case AF_INET:
  107. *lenp = 2;
  108. return (char *)&(((struct sockaddr_in *)netaddrp)->sin_port);
  109. default:
  110. *lenp = 0;
  111. return NULL;
  112. }
  113. #endif
  114. }
  115. /* given an XdmcpNetaddr, returns a pointer to the network address
  116. and sets *lenp to the length of the address */
  117. char * NetaddrAddress(XdmcpNetaddr netaddrp, int *lenp)
  118. {
  119. #ifdef STREAMSCONN
  120. *lenp = 4;
  121. return netaddrp+4;
  122. #else
  123. switch (NetaddrFamily(netaddrp)) {
  124. #ifdef UNIXCONN
  125. case AF_UNIX:
  126. *lenp = strlen(((struct sockaddr_un *)netaddrp)->sun_path);
  127. return (char *) (((struct sockaddr_un *)netaddrp)->sun_path);
  128. #endif
  129. #ifdef TCPCONN
  130. case AF_INET:
  131. *lenp = sizeof (struct in_addr);
  132. return (char *) &(((struct sockaddr_in *)netaddrp)->sin_addr);
  133. #endif
  134. #ifdef DNETCONN
  135. case AF_DECnet:
  136. *lenp = sizeof (struct dn_naddr);
  137. return (char *) &(((struct sockaddr_dn *)netaddrp)->sdn_add);
  138. #endif
  139. #ifdef AF_CHAOS
  140. case AF_CHAOS:
  141. #endif
  142. default:
  143. *lenp = 0;
  144. return NULL;
  145. }
  146. #endif /* STREAMSCONN else */
  147. }
  148. /* given an XdmcpNetaddr, sets *addr to the network address used and
  149. sets *len to the number of bytes in addr.
  150. Returns the X protocol family used, e.g., FamilyInternet */
  151. int ConvertAddr (XdmcpNetaddr saddr, int *len, char **addr)
  152. {
  153. int retval;
  154. if (len == NULL)
  155. return -1;
  156. *addr = NetaddrAddress(saddr, len);
  157. #ifdef STREAMSCONN
  158. /* kludge */
  159. if (NetaddrFamily(saddr) == 2)
  160. retval = FamilyInternet;
  161. #else
  162. switch (NetaddrFamily(saddr))
  163. {
  164. #ifdef AF_UNSPEC
  165. case AF_UNSPEC:
  166. retval = FamilyLocal;
  167. break;
  168. #endif
  169. #ifdef AF_UNIX
  170. case AF_UNIX:
  171. retval = FamilyLocal;
  172. break;
  173. #endif
  174. #ifdef TCPCONN
  175. case AF_INET:
  176. retval = FamilyInternet;
  177. break;
  178. #endif
  179. #ifdef DNETCONN
  180. case AF_DECnet:
  181. retval = FamilyDECnet;
  182. break;
  183. #endif
  184. #ifdef AF_CHAOS
  185. case AF_CHAOS:
  186. retval = FamilyChaos;
  187. break;
  188. #endif
  189. default:
  190. retval = -1;
  191. break;
  192. }
  193. #endif /* STREAMSCONN else */
  194. Debug ("ConvertAddr returning %d for family %d\n", retval,
  195. NetaddrFamily(saddr));
  196. return retval;
  197. }
  198. int
  199. addressEqual (XdmcpNetaddr a1, int len1, XdmcpNetaddr a2, int len2)
  200. {
  201. int partlen1, partlen2;
  202. char *part1, *part2;
  203. if (len1 != len2)
  204. {
  205. return FALSE;
  206. }
  207. if (NetaddrFamily(a1) != NetaddrFamily(a2))
  208. {
  209. return FALSE;
  210. }
  211. part1 = NetaddrPort(a1, &partlen1);
  212. part2 = NetaddrPort(a2, &partlen2);
  213. if (partlen1 != partlen2 || memcmp(part1, part2, partlen1) != 0)
  214. {
  215. return FALSE;
  216. }
  217. part1 = NetaddrAddress(a1, &partlen1);
  218. part2 = NetaddrAddress(a2, &partlen2);
  219. if (partlen1 != partlen2 || memcmp(part1, part2, partlen1) != 0)
  220. {
  221. return FALSE;
  222. }
  223. return TRUE;
  224. }
  225. #ifdef DEBUG
  226. /*ARGSUSED*/
  227. PrintSockAddr (struct sockaddr *a, int len)
  228. {
  229. unsigned char *t, *p;
  230. Debug ("family %d, ", a->sa_family);
  231. switch (a->sa_family) {
  232. #ifdef AF_INET
  233. case AF_INET:
  234. p = (unsigned char *) &((struct sockaddr_in *) a)->sin_port;
  235. t = (unsigned char *) &((struct sockaddr_in *) a)->sin_addr;
  236. Debug ("port %d, host %d.%d.%d.%d\n",
  237. (p[0] << 8) + p[1], t[0], t[1], t[2], t[3]);
  238. break;
  239. }
  240. #endif
  241. }
  242. #endif