summaryrefslogtreecommitdiffstats
path: root/tdm/backend/socket.c
blob: 151fbc552394af63afff82a56622112d9420ac08 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/*

Copyright 1988, 1998  The Open Group
Copyright 2002 Sun Microsystems, Inc.  All rights reserved.
Copyright 2002,2004 Oswald Buddenhagen <ossi@kde.org>

Permission to use, copy, modify, distribute, and sell this software and its
documentation for any purpose is hereby granted without fee, provided that
the above copyright notice appear in all copies and that both that
copyright notice and this permission notice appear in supporting
documentation.

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

Except as contained in this notice, the name of a copyright holder shall
not be used in advertising or otherwise to promote the sale, use or
other dealings in this Software without prior written authorization
from the copyright holder.

*/

/*
 * xdm - display manager daemon
 * Author: Keith Packard, MIT X Consortium
 *
 * socket.c - Support for BSD sockets
 */

#include "dm.h"

#if defined(XDMCP) && !defined(STREAMSCONN)

#include "dm_error.h"
#include "dm_socket.h"

#include <netdb.h>
#include <arpa/inet.h>

static int c_request_port;

static int
CreateListeningSocket( struct sockaddr *sock_addr, int salen )
{
	int fd;
#if defined(IPv6) && defined(IPPROTO_IPV6) && defined(IPV6_V6ONLY)
	int on = 0;
#endif
	const char *addrstring = "unknown";
#if defined(IPv6) && defined(AF_INET6)
	char addrbuf[INET6_ADDRSTRLEN];
#endif

	if (!request_port)
		return -1;

	if (debugLevel & DEBUG_CORE) {
#if defined(IPv6) && defined(AF_INET6)
		void *ipaddr;
		if (sock_addr->sa_family == AF_INET6)
			ipaddr = & ((struct sockaddr_in6 *)sock_addr)->sin6_addr;
		else
			ipaddr = & ((struct sockaddr_in *)sock_addr)->sin_addr;
		addrstring =
			inet_ntop( sock_addr->sa_family, ipaddr, addrbuf, sizeof(addrbuf) );

#else
		addrstring = inet_ntoa( ((struct sockaddr_in *)sock_addr)->sin_addr );
#endif

		Debug( "creating socket to listen on port %d of address %s\n",
		       request_port, addrstring );
	}

	if ((fd = socket( sock_addr->sa_family, SOCK_DGRAM, 0 )) == -1) {
		LogError( "XDMCP socket creation failed, errno %d\n", errno );
		return -1;
	}
#if defined(IPv6) && defined(IPPROTO_IPV6) && defined(IPV6_V6ONLY)
	setsockopt( fd, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on) );
#endif

	if (bind( fd, sock_addr, salen ) == -1) {
		LogError( "error %d binding socket address %d\n", errno, request_port );
		close( fd );
		return -1;
	}

	RegisterCloseOnFork( fd );
	RegisterInput( fd );
	return fd;
}

struct soctdelist {
	struct soctdelist *next;
	struct soctdelist *mcastgroups;
	struct sockaddr *addr;
	int salen;
	int addrlen;
	int fd;
	int ref; /* referenced bit - see UpdateListenSockets */
};

static struct soctdelist *listensocks;

static void
DestroyListeningSocket( struct soctdelist *s )
{
	struct soctdelist *g, *n;

	if (s->fd >= 0) {
		CloseNClearCloseOnFork( s->fd );
		UnregisterInput( s->fd );
		s->fd = -1;
	}
	if (s->addr) {
		free( s->addr );
		s->addr = NULL;
	}
	for (g = s->mcastgroups; g; g = n) {
		n = g->next;
		if (g->addr)
			free( g->addr );
		free( g );
	}
	s->mcastgroups = NULL;
}

static struct soctdelist*
FindInList( struct soctdelist *list, ARRAY8Ptr addr )
{
	struct soctdelist *s;

	for (s = list; s; s = s->next) {
		if (s->addrlen == addr->length) {
			char *addrdata;

			switch (s->addr->sa_family) {
			case AF_INET:
				addrdata = (char *)
				  &(((struct sockaddr_in *)s->addr)->sin_addr.s_addr);
				break;
#if defined(IPv6) && defined(AF_INET6)
			case AF_INET6:
				addrdata = (char *)
				  &(((struct sockaddr_in6 *)s->addr)->sin6_addr.s6_addr);
				break;
#endif
			default:
				/* Unrecognized address family */
				continue;
			}
			if (!memcmp( addrdata, addr->data, addr->length ))
				return s;
		}
	}
	return NULL;
}

static struct soctdelist *
CreateSoctdelistEntry( ARRAY8Ptr addr )
{
	struct soctdelist *s;

	if (!(s = Calloc( 1, sizeof(struct soctdelist) )))
		return NULL;

	if (addr->length == 4) { /* IPv4 */
		struct sockaddr_in *sin4;
		sin4 = Calloc( 1, sizeof(struct sockaddr_in) );
#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
		sin4->sin_len = sizeof(struct sockaddr_in);
#endif
		s->addr = (struct sockaddr *)sin4;
		s->salen = sizeof(struct sockaddr_in);
		s->addrlen = sizeof(struct in_addr);
		sin4->sin_family = AF_INET;
		sin4->sin_port = htons( (short)request_port );
		memcpy( &sin4->sin_addr, addr->data, addr->length );
	}
#if defined(IPv6) && defined(AF_INET6)
	else if (addr->length == 16) { /* IPv6 */
		struct sockaddr_in6 *sin6;
		sin6 = Calloc( 1, sizeof(struct sockaddr_in6) );
#ifdef SIN6_LEN
		sin6->sin6_len = sizeof(struct sockaddr_in6);
#endif
		s->addr = (struct sockaddr *)sin6;
		s->salen = sizeof(struct sockaddr_in6);
		s->addrlen = sizeof(struct in6_addr);
		sin6->sin6_family = AF_INET6;
		sin6->sin6_port = htons( (short)request_port );
		memcpy( &sin6->sin6_addr, addr->data, addr->length );
	}
#endif
	else {
		/* Unknown address type */
		free( s );
		s = NULL;
	}
	return s;
}

static void
UpdateListener( ARRAY8Ptr addr, void **closure )
{
	struct soctdelist *s;

	*closure = NULL;

	if (addr == NULL) {
		ARRAY8 tmpaddr;
		struct in_addr in;
#if defined(IPv6) && defined(AF_INET6)
		struct in6_addr in6 = in6addr_any;
		tmpaddr.length = sizeof(in6);
		tmpaddr.data = (CARD8Ptr) &in6;
		UpdateListener( &tmpaddr, closure );
		if (*closure)
			return;
#endif
		in.s_addr = htonl( INADDR_ANY );
		tmpaddr.length = sizeof(in);
		tmpaddr.data = (CARD8Ptr) &in;
		UpdateListener( &tmpaddr, closure );
		return;
	}

	if (c_request_port == request_port &&
	    (s = FindInList( listensocks, addr )))
	{
		*closure = (void *)s;
		s->ref = 1;
		return;
	}

	if (!(s = CreateSoctdelistEntry( addr )))
		return;

	if ((s->fd = CreateListeningSocket( s->addr, s->salen )) < 0) {
		free( s->addr );
		free( s );
		return;
	}
	s->ref = 1;
	s->next = listensocks;
	listensocks = s;
	*closure = (void *)s;
}

#define JOIN_MCAST_GROUP 0
#define LEAVE_MCAST_GROUP 1

static void
ChangeMcastMembership( struct soctdelist *s, struct soctdelist *g, int op )
{
	int sockopt;

	switch (s->addr->sa_family)
	{
		case AF_INET:
		{
			struct ip_mreq mreq;
			memcpy( &mreq.imr_multiaddr,
			        &((struct sockaddr_in *)g->addr)->sin_addr,
			        sizeof(struct in_addr) );
			memcpy( &mreq.imr_interface,
			        &((struct sockaddr_in *)s->addr)->sin_addr,
			        sizeof(struct in_addr) );
			if (op == JOIN_MCAST_GROUP)
				sockopt = IP_ADD_MEMBERSHIP;
			else
				sockopt = IP_DROP_MEMBERSHIP;
			if (setsockopt( s->fd, IPPROTO_IP, sockopt,
			                &mreq, sizeof(mreq) ) < 0) {
				LogError( "XDMCP socket multicast %s to %s failed, errno %d\n",
				          (op == JOIN_MCAST_GROUP) ? "join" : "drop",
				          inet_ntoa( ((struct sockaddr_in *)g->addr)->sin_addr ),
				          errno );
			} else if (debugLevel & DEBUG_CORE) {
				Debug( "XDMCP socket multicast %s to %s succeeded\n",
				       (op == JOIN_MCAST_GROUP) ? "join" : "drop",
				       inet_ntoa( ((struct sockaddr_in *)g->addr)->sin_addr ) );
			}
			return;
		}
#if defined(IPv6) && defined(AF_INET6)
# ifndef IPV6_JOIN_GROUP
#  define IPV6_JOIN_GROUP IPV6_ADD_MEMBERSHIP
# endif
# ifndef IPV6_LEAVE_GROUP
#  define IPV6_LEAVE_GROUP IPV6_DROP_MEMBERSHIP
# endif
		case AF_INET6:
		{
			struct ipv6_mreq mreq6;
			memcpy( &mreq6.ipv6mr_multiaddr,
			        &((struct sockaddr_in6 *)g->addr)->sin6_addr,
			        sizeof(struct in6_addr) );
			mreq6.ipv6mr_interface = 0;  /* TODO: fix this */
			if (op == JOIN_MCAST_GROUP)
				sockopt = IPV6_JOIN_GROUP;
			else
				sockopt = IPV6_LEAVE_GROUP;
			if (setsockopt( s->fd, IPPROTO_IPV6, sockopt,
			                &mreq6, sizeof(mreq6) ) < 0)
			{
				int saveerr = errno;
				char addrbuf[INET6_ADDRSTRLEN];

				inet_ntop( s->addr->sa_family,
				           &((struct sockaddr_in6 *)g->addr)->sin6_addr,
				           addrbuf, sizeof(addrbuf) );

				LogError( "XDMCP socket multicast %s to %s failed, errno %d\n",
				          (op == JOIN_MCAST_GROUP) ? "join" : "drop", addrbuf,
				          saveerr );
			} else if (debugLevel & DEBUG_CORE) {
				char addrbuf[INET6_ADDRSTRLEN];

				inet_ntop( s->addr->sa_family,
				           &((struct sockaddr_in6 *)g->addr)->sin6_addr,
				           addrbuf, sizeof(addrbuf) );

				Debug( "XDMCP socket multicast %s to %s succeeded\n",
				       (op == JOIN_MCAST_GROUP) ? "join" : "drop", addrbuf );
			}
			return;
		}
#endif
	}
}

static void
UpdateMcastGroup( ARRAY8Ptr addr, void **closure )
{
	struct soctdelist *s = (struct soctdelist *)*closure;
	struct soctdelist *g;

	if (!s)
		return;

	/* Already in the group, mark & continue */
	if ((g = FindInList( s->mcastgroups, addr ))) {
		g->ref = 1;
		return;
	}

	/* Need to join the group */
	if (!(g = CreateSoctdelistEntry( addr )))
		return;

	ChangeMcastMembership( s, g, JOIN_MCAST_GROUP );
	free( g );
}

/* Open or close listening sockets to match the current settings read in
   from the access database. */
void
UpdateListenSockets( void )
{
	struct soctdelist *s, *g, **ls, **lg;
	void *tmpPtr = NULL;

	/* Clear Ref bits - any not marked by UpdateCallback will be closed */
	for (s = listensocks; s; s = s->next) {
		s->ref = 0;
		for (g = s->mcastgroups; g; g = g->next)
			g->ref = 0;
	}
	ForEachListenAddr( UpdateListener, UpdateMcastGroup, &tmpPtr );
	c_request_port = request_port;
	for (ls = &listensocks; (s = *ls); )
		if (!s->ref) {
			DestroyListeningSocket( s );
			*ls = s->next;
			free( s );
		} else {
			ls = &s->next;
			for (lg = &s->mcastgroups; (g = *lg); )
				if (!g->ref) {
					ChangeMcastMembership( s, g, LEAVE_MCAST_GROUP );
					*lg = g->next;
					free( g );
				} else
					lg = &g->next;
		}
}

int
AnyListenSockets( void )
{
	return listensocks != NULL;
}

int
ProcessListenSockets( FD_TYPE *reads )
{
	struct soctdelist *s;
	int ret = 0;

	for (s = listensocks; s; s = s->next)
		if (FD_ISSET( s->fd, reads )) {
			ProcessRequestSocket( s->fd );
			ret = 1;
		}
	return ret;
}

#endif /* !STREAMSCONN && XDMCP */