socket.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. void registerHostname (char *name, int namelen); // xdmcp.c
  94. int
  95. CreateWellKnownSockets (void)
  96. {
  97. struct sockaddr_in sock_addr;
  98. char *name, *localHostname();
  99. if (request_port == 0)
  100. return 0;
  101. Debug ("creating socket %d\n", request_port);
  102. xdmcpFd = socket (AF_INET, SOCK_DGRAM, 0);
  103. if (xdmcpFd == -1) {
  104. LogError (ReadCatalog(MC_LOG_SET,MC_LOG_FAIL_SOCK,MC_DEF_LOG_FAIL_SOCK),
  105. request_port);
  106. return 0;
  107. }
  108. name = localHostname ();
  109. registerHostname (name, strlen (name));
  110. RegisterCloseOnFork (xdmcpFd);
  111. /* zero out the entire structure; this avoids 4.4 incompatibilities */
  112. bzero ((char *) &sock_addr, sizeof (sock_addr));
  113. #ifdef BSD44SOCKETS
  114. sock_addr.sin_len = sizeof(sock_addr);
  115. #endif
  116. sock_addr.sin_family = AF_INET;
  117. sock_addr.sin_port = htons ((short) request_port);
  118. sock_addr.sin_addr.s_addr = htonl (INADDR_ANY);
  119. if (bind (xdmcpFd, (struct sockaddr *)&sock_addr, sizeof (sock_addr)) == -1)
  120. {
  121. LogError (ReadCatalog(MC_LOG_SET,MC_LOG_ERR_BIND,MC_DEF_LOG_ERR_BIND),
  122. request_port, errno);
  123. close (xdmcpFd);
  124. xdmcpFd = -1;
  125. return 0;
  126. }
  127. WellKnownSocketsMax = xdmcpFd;
  128. FD_SET (xdmcpFd, &WellKnownSocketsMask);
  129. chooserFd = socket (AF_INET, SOCK_STREAM, 0);
  130. Debug ("Created chooser socket %d\n", chooserFd);
  131. if (chooserFd == -1)
  132. {
  133. LogError ((unsigned char *)"chooser socket creation failed, errno %d\n", errno);
  134. return 0;
  135. }
  136. listen (chooserFd, 5);
  137. if (chooserFd > WellKnownSocketsMax)
  138. WellKnownSocketsMax = chooserFd;
  139. FD_SET (chooserFd, &WellKnownSocketsMask);
  140. }
  141. int
  142. GetChooserAddr (char *addr, int *lenp)
  143. {
  144. struct sockaddr_in in_addr;
  145. int len;
  146. len = sizeof in_addr;
  147. if (getsockname (chooserFd, (struct sockaddr *)&in_addr, &len) < 0)
  148. return -1;
  149. Debug ("Chooser socket port: %d\n", ntohs(in_addr.sin_port));
  150. memmove( addr, (char *) &in_addr, len);
  151. *lenp = len;
  152. return 0;
  153. }