socket.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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: socket.c /main/9 1997/12/23 12:25:32 bill $ */
  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.3 1995/06/06 20:24:26 Chris_Beute
  38. * Code snapshot merge from March 15 and SIA changes
  39. * [1995/05/31 20:15:01 Chris_Beute]
  40. *
  41. * Revision 1.1.2.2 1995/04/21 13:05:38 Peter_Derr
  42. * dtlogin auth key fixes from deltacde
  43. * [1995/04/12 19:21:26 Peter_Derr]
  44. *
  45. * R6 xdm version used to pick up XDMCP improvements.
  46. * [1995/04/12 18:05:54 Peter_Derr]
  47. *
  48. * $EndLog$
  49. */
  50. /*
  51. Copyright (c) 1988 X Consortium
  52. Permission is hereby granted, free of charge, to any person obtaining
  53. a copy of this software and associated documentation files (the
  54. "Software"), to deal in the Software without restriction, including
  55. without limitation the rights to use, copy, modify, merge, publish,
  56. distribute, sublicense, and/or sell copies of the Software, and to
  57. permit persons to whom the Software is furnished to do so, subject to
  58. the following conditions:
  59. The above copyright notice and this permission notice shall be included
  60. in all copies or substantial portions of the Software.
  61. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  62. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  63. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  64. IN NO EVENT SHALL THE X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR
  65. OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  66. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  67. OTHER DEALINGS IN THE SOFTWARE.
  68. Except as contained in this notice, the name of the X Consortium shall
  69. not be used in advertising or otherwise to promote the sale, use or
  70. other dealings in this Software without prior written authorization
  71. from the X Consortium.
  72. */
  73. /*
  74. * xdm - display manager daemon
  75. * Author: Keith Packard, MIT X Consortium
  76. *
  77. * socket.c - Support for BSD sockets
  78. */
  79. #include "dm.h"
  80. #include "vgmsg.h"
  81. #include <errno.h>
  82. #include <sys/socket.h>
  83. #include <netinet/in.h>
  84. #include <sys/un.h>
  85. #include <netdb.h>
  86. #ifdef X_NOT_STDC_ENV
  87. extern int errno;
  88. #endif
  89. extern int xdmcpFd;
  90. extern int chooserFd;
  91. extern FD_TYPE WellKnownSocketsMask;
  92. extern int WellKnownSocketsMax;
  93. CreateWellKnownSockets (void)
  94. {
  95. struct sockaddr_in sock_addr;
  96. char *name, *localHostname();
  97. if (request_port == 0)
  98. return 0;
  99. Debug ("creating socket %d\n", request_port);
  100. xdmcpFd = socket (AF_INET, SOCK_DGRAM, 0);
  101. if (xdmcpFd == -1) {
  102. LogError (ReadCatalog(MC_LOG_SET,MC_LOG_FAIL_SOCK,MC_DEF_LOG_FAIL_SOCK),
  103. request_port);
  104. return 0;
  105. }
  106. name = localHostname ();
  107. registerHostname (name, strlen (name));
  108. RegisterCloseOnFork (xdmcpFd);
  109. /* zero out the entire structure; this avoids 4.4 incompatibilities */
  110. bzero ((char *) &sock_addr, sizeof (sock_addr));
  111. #ifdef BSD44SOCKETS
  112. sock_addr.sin_len = sizeof(sock_addr);
  113. #endif
  114. sock_addr.sin_family = AF_INET;
  115. sock_addr.sin_port = htons ((short) request_port);
  116. sock_addr.sin_addr.s_addr = htonl (INADDR_ANY);
  117. if (bind (xdmcpFd, (struct sockaddr *)&sock_addr, sizeof (sock_addr)) == -1)
  118. {
  119. LogError (ReadCatalog(MC_LOG_SET,MC_LOG_ERR_BIND,MC_DEF_LOG_ERR_BIND),
  120. request_port, errno);
  121. close (xdmcpFd);
  122. xdmcpFd = -1;
  123. return 0;
  124. }
  125. WellKnownSocketsMax = xdmcpFd;
  126. FD_SET (xdmcpFd, &WellKnownSocketsMask);
  127. chooserFd = socket (AF_INET, SOCK_STREAM, 0);
  128. Debug ("Created chooser socket %d\n", chooserFd);
  129. if (chooserFd == -1)
  130. {
  131. LogError ((unsigned char *)"chooser socket creation failed, errno %d\n", errno);
  132. return 0;
  133. }
  134. listen (chooserFd, 5);
  135. if (chooserFd > WellKnownSocketsMax)
  136. WellKnownSocketsMax = chooserFd;
  137. FD_SET (chooserFd, &WellKnownSocketsMask);
  138. }
  139. GetChooserAddr (char *addr, int *lenp)
  140. {
  141. struct sockaddr_in in_addr;
  142. int len;
  143. len = sizeof in_addr;
  144. #if defined (_AIX) && (OSMAJORVERSION==4) && (OSMINORVERSION==2)
  145. if (getsockname (chooserFd, (struct sockaddr *)&in_addr, (size_t *)&len) < 0)
  146. #else
  147. if (getsockname (chooserFd, (struct sockaddr *)&in_addr, &len) < 0)
  148. #endif
  149. return -1;
  150. Debug ("Chooser socket port: %d\n", ntohs(in_addr.sin_port));
  151. memmove( addr, (char *) &in_addr, len);
  152. *lenp = len;
  153. return 0;
  154. }