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