aboutsummaryrefslogtreecommitdiff
path: root/nx-X11/lib/xtrans/Xtransdnet.c
blob: a4abf42a4eab0b84e6ed99f008c5a575239c20ee (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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
/*

Copyright 1993, 1994, 1998  The Open Group

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 OPEN GROUP 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 The Open Group 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 Open Group.

*/

/* Copyright 1993, 1994 NCR Corporation - Dayton, Ohio, USA
 *
 * All Rights Reserved
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose and without fee is hereby granted, provided
 * that the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation, and that the name NCR not be used in advertising
 * or publicity pertaining to distribution of the software without specific,
 * written prior permission.  NCR and makes no representations about the
 * suitability of this software for any purpose.  It is provided "as is"
 * without express or implied warranty.
 *
 * NCR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
 * NO EVENT SHALL NCR BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
 * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#ifndef WIN32
#include <netdnet/dn.h>
#include <netdnet/dnetdb.h>
#include <sys/ioctl.h>
#endif /* !WIN32 */

#include <stdio.h>

#ifdef WIN32
#define _WILLWINSOCK_
#define BOOL wBOOL
#undef Status
#define Status wStatus
#include <prgpre.h> /* PATHWORKS header normally in %MSTOOLS%\h\pathwork */
#undef Status
#define Status int
#undef BOOL
#include <nx-X11/Xw32defs.h>
#undef close
#define close closesocket
#endif /* WIN32 */


#if defined(X11_t)
#define DNETOBJ "X$X"
#endif
#if defined(XIM_t)
#define DNETOBJ "IMSERVER$"
#endif
#if defined(FS_t) || defined(FONT_t)
#define DNETOBJ "X$FONT"
#endif
#if defined(ICE_t)
#define DNETOBJ ""
#endif
#if defined(TEST_t)
#define DNETOBJ "X$TEST"
#endif


/*
 * This is the DNET implementation of the X Transport service layer
 */

/*
 * This function gets the local address of the socket and stores it in the
 * XtransConnInfo structure for the connection.
 */

static int
TRANS(DNETGetAddr) (XtransConnInfo ciptr)

{
    struct sockaddr_dn	sockname;
    int			namelen = sizeof(sockname);

    PRMSG (3,"DNETGetAddr(%x)\n", ciptr, 0, 0);

    if (getsockname (ciptr->fd, (struct sockaddr *) &sockname, &namelen) < 0)
    {
	PRMSG (1,"DNETGetAddr: getsockname() failed: %d\n",
	      EGET(), 0, 0);
	return -1;
    }


    /*
     * Everything looks good: fill in the XtransConnInfo structure.
     */

    if ((ciptr->addr = (char *) malloc (namelen)) == NULL)
    {
        PRMSG (1, "DNETGetAddr: Can't allocate space for the addr\n",
	       0, 0, 0);
        return -1;
    }

    ciptr->family = sockname.sdn_family;
    ciptr->addrlen = namelen;
    memcpy (ciptr->addr, &sockname, ciptr->addrlen);

    return 0;
}


/*
 * This function gets the remote address of the socket and stores it in the
 * XtransConnInfo structure for the connection.
 */

static int
TRANS(DNETGetPeerAddr) (XtransConnInfo ciptr)

{
    struct sockaddr_dn	sockname;
    int			namelen = sizeof(sockname);

    PRMSG (3,"DNETGetPeerAddr(%x)\n", ciptr, 0, 0);

    if (getpeername (ciptr->fd, (struct sockaddr *) &sockname, &namelen) < 0)
    {
	PRMSG (1,"DNETGetPeerAddr: getpeername() failed: %d\n",
	      EGET(), 0, 0);
	return -1;
    }

    /*
     * Everything looks good: fill in the XtransConnInfo structure.
     */

    if ((ciptr->peeraddr = (char *) malloc (namelen)) == NULL)
    {
        PRMSG (1,
	      "DNETGetPeerAddr: Can't allocate space for the addr\n",
	      0, 0, 0);
        return -1;
    }

    ciptr->peeraddrlen = namelen;
    memcpy (ciptr->peeraddr, &sockname, ciptr->peeraddrlen);

    return 0;
}


#ifdef TRANS_CLIENT

static XtransConnInfo
TRANS(DNETOpenCOTSClient) (Xtransport *thistrans, char *protocol, 
			   char *host, char *port)

{
    XtransConnInfo	ciptr;

    PRMSG (2,"DNETOpenCOTSClient(%s,%s,%s)\n", protocol, host, port);

    if ((ciptr = (XtransConnInfo) calloc (
	1, sizeof(struct _XtransConnInfo))) == NULL)
    {
	PRMSG (1, "DNETOpenCOTSClient: malloc failed\n", 0, 0, 0);
	return NULL;
    }

    ciptr->index = 0;		/* only one form of DECnet */

    /* nothing else to do here */

    return ciptr;
}

#endif /* TRANS_CLIENT */


#ifdef TRANS_SERVER

static XtransConnInfo
TRANS(DNETOpenCOTSServer) (Xtransport *thistrans, char *protocol, 
			   char *host, char *port)

{
    XtransConnInfo	ciptr;

    PRMSG (2,"DNETOpenCOTSServer(%s,%s,%s)\n", protocol, host, port);

    if ((ciptr = (XtransConnInfo) calloc (
	1, sizeof(struct _XtransConnInfo))) == NULL)
    {
	PRMSG (1, "DNETOpenCOTSServer: malloc failed\n", 0, 0, 0);
	return NULL;
    }

    if ((ciptr->fd = socket (AF_DECnet, SOCK_STREAM, 0)) < 0)
    {
	free ((char *) ciptr);
	return NULL;
    }

    ciptr->index = 0;		/* only one form of DECnet */

    return (ciptr);
}

#endif /* TRANS_SERVER */


#ifdef TRANS_CLIENT

static XtransConnInfo
TRANS(DNETOpenCLTSClient) (Xtransport *thistrans, char *protocol, 
			   char *host, char *port)

{
    XtransConnInfo	ciptr;

    PRMSG (2,"DNETOpenCLTSClient(%s,%s,%s)\n", protocol, host, port);

    if ((ciptr = (XtransConnInfo) calloc (
	1, sizeof (struct _XtransConnInfo))) == NULL)
    {
	PRMSG (1, "DNETOpenCLTSClient: malloc failed\n", 0, 0, 0);
	return NULL;
    }

    ciptr->index = 0;		/* only one form of DECnet */

    /* nothing else to do here */

    return ciptr;
}

#endif /* TRANS_CLIENT */


#ifdef TRANS_SERVER

static XtransConnInfo
TRANS(DNETOpenCLTSServer) (Xtransport *thistrans, char *protocol, 
			   char *host, char *port)

{
    /* NEED TO IMPLEMENT */

    PRMSG (2,"DNETOpenCLTSServer(%s,%s,%s)\n", protocol, host, port);
    return NULL;
}

#endif /* TRANS_SERVER */


#ifdef TRANS_REOPEN

static XtransConnInfo
TRANS(DNETReopenCOTSServer) (Xtransport *thistrans, int fd, char *port)

{
    XtransConnInfo	ciptr;

    PRMSG (2,"DNETReopenCOTSServer(%d,%s)\n", fd, port, 0);

    if ((ciptr = (XtransConnInfo) calloc (
	1, sizeof(struct _XtransConnInfo))) == NULL)
    {
	PRMSG (1, "DNETReopenCOTSServer: malloc failed\n", 0, 0, 0);
	return NULL;
    }

    ciptr->fd = fd;
    ciptr->index = 0;		/* only one form of DECnet */

    return (ciptr);
}

static XtransConnInfo
TRANS(DNETReopenCLTSServer) (Xtransport *thistrans, int fd, char *port)

{
    XtransConnInfo	ciptr;

    PRMSG (2,"DNETReopenCLTSServer(%d,%s)\n", fd, port, 0);

    if ((ciptr = (XtransConnInfo) calloc (
	1, sizeof(struct _XtransConnInfo))) == NULL)
    {
	PRMSG (1, "DNETReopenCLTSServer: malloc failed\n", 0, 0, 0);
	return NULL;
    }

    ciptr->fd = fd;
    ciptr->index = 0;		/* only one form of DECnet */

    return (ciptr);
}

#endif /* TRANS_REOPEN */


static int
TRANS(DNETSetOption) (XtransConnInfo ciptr, int option, int arg)

{
    PRMSG (2,"DNETSetOption(%d,%d,%d)\n", ciptr->fd, option, arg);

    return -1;
}


#ifdef TRANS_SERVER

static int
TRANS(DNETCreateListener) (XtransConnInfo ciptr, char *port, unsigned int flags)

{
    struct sockaddr_dn  dnsock;
    int			fd = ciptr->fd;

    PRMSG (3, "DNETCreateListener(%x,%d)\n", ciptr, fd, 0);

    bzero ((char *) &dnsock, sizeof (dnsock));
    dnsock.sdn_family = AF_DECnet;

    if (port && *port )
	sprintf (dnsock.sdn_objname, "%s%s", DNETOBJ, port);
    else
#ifdef X11_t
	return -1;
#else
	sprintf (dnsock.sdn_objname, "%s%d", DNETOBJ, getpid ());
#endif

    dnsock.sdn_objnamel = strlen (dnsock.sdn_objname);

    if (bind (fd, (struct sockaddr *) &dnsock, sizeof (dnsock)))
    {
	close (fd);
	return -1;
    }

    if (listen (fd, 5))
    {
	close (fd);
	return (-1);
    }


    /* Set a flag to indicate that this connection is a listener */

    ciptr->flags = 1 | (ciptr->flags & TRANS_KEEPFLAGS);

    return 0;
}


static XtransConnInfo
TRANS(DNETAccept) (XtransConnInfo ciptr, int *status)

{
    XtransConnInfo	newciptr;
    struct sockaddr_dn	sockname;
    int			namelen = sizeof(sockname);

    PRMSG (2, "DNETAccept(%x,%d)\n", ciptr, ciptr->fd, 0);

    if ((newciptr = (XtransConnInfo) calloc(
	1, sizeof (struct _XtransConnInfo))) == NULL)
    {
	PRMSG (1, "DNETAccept: malloc failed\n", 0, 0, 0);
	*status = TRANS_ACCEPT_BAD_MALLOC;
	return NULL;
    }

    if((newciptr->fd = accept (ciptr->fd,
	(struct sockaddr *) &sockname, &namelen)) < 0)
    {
	PRMSG (1, "DNETAccept: accept() failed\n", 0, 0, 0);

	free (newciptr);
	*status = TRANS_ACCEPT_FAILED;
	return NULL;
    }

    /*
     * Get this address again because the transport may give a more 
     * specific address now that a connection is established.
     */

    if (TRANS(DNETGetAddr) (newciptr) < 0)
    {
	PRMSG(1,
	"DNETAccept: ...DNETGetAddr() failed:\n", 0, 0, 0);
	close (newciptr->fd);
	free (newciptr);
	*status = TRANS_ACCEPT_MISC_ERROR;
        return NULL;
    }

    if (TRANS(DNETGetPeerAddr) (newciptr) < 0)
    {
	PRMSG(1,
	"DNETAccept: ...DNETGetPeerAddr() failed:\n", 0, 0, 0);

	close (newciptr->fd);
	if (newciptr->addr) free (newciptr->addr);
	free (newciptr);
	*status = TRANS_ACCEPT_MISC_ERROR;
        return NULL;
    }

    *status = 0;

    return newciptr;
}

#endif /* TRANS_SERVER */


#ifdef TRANS_CLIENT

#define OBJBUFSIZE 64

static int
TRANS(DNETConnect) (XtransConnInfo ciptr, char *host, char *port)

{
    char objname[OBJBUFSIZE];

    extern int dnet_conn();
    
    PRMSG (2,"DNETConnect(%d,%s,%s)\n", ciptr->fd, host, port);

#ifdef X11_t
    /*
     * X has a well known port, that is transport dependent. It is easier
     * to handle it here, than try and come up with a transport independent
     * representation that can be passed in and resolved the usual way.
     *
     * The port that is passed here is really a string containing the idisplay
     * from ConnectDisplay().
     */

    if (is_numeric (port))
    {
	short tmpport = (short) atoi (port);

	sprintf (objname, "X$X%d", tmpport);
    }
    else
#endif
	strncpy(objname, port, OBJBUFSIZE);


    /*
     * Do the connect
     */

    if (!host) host = "0";

    if ((ciptr->fd = dnet_conn (host, objname, SOCK_STREAM, 0, 0, 0, 0)) < 0)
    {
	return TRANS_CONNECT_FAILED;
    }


    /*
     * Sync up the address fields of ciptr.
     */

    if (TRANS(DNETGetAddr) (ciptr) < 0)
    {
	PRMSG (1,
	      "DNETConnect: ...DNETGetAddr() failed:\n", 0, 0, 0);
	return TRANS_CONNECT_FAILED;
    }

    if (TRANS(DNETGetPeerAddr) (ciptr) < 0)
    {
	PRMSG (1,
	      "DNETConnect: ...DNETGetPeerAddr() failed:\n",
	      0, 0, 0);
	return TRANS_CONNECT_FAILED;
    }

    return 0;
}

#endif /* TRANS_CLIENT */


static int
TRANS(DNETBytesReadable) (XtransConnInfo ciptr, BytesReadable_t *pend)

{
    PRMSG (2,"DNETBytesReadable(%x,%d,%x)\n", ciptr, ciptr->fd, pend);

#ifdef WIN32
    {
	int ret = ioctlsocket ((SOCKET) ciptr->fd, FIONREAD, (u_long *) pend);
	errno = WSAGetLastError();
	return ret;
    }
#else
    return ioctl(ciptr->fd, FIONREAD, (char *)pend);
#endif /* WIN32 */
}


static int
TRANS(DNETRead) (XtransConnInfo ciptr, char *buf, int size)

{
    PRMSG (2,"DNETRead(%d,%x,%d)\n", ciptr->fd, buf, size);

#ifdef WIN32
    {
	int ret = recv ((SOCKET)ciptr->fd, buf, size, 0);
	errno = WSAGetLastError();
	return ret;
    }
#else
    return read (ciptr->fd, buf, size);
#endif /* WIN32 */
}


static int
TRANS(DNETWrite) (XtransConnInfo ciptr, char *buf, int size)

{
    PRMSG (2,"DNETWrite(%d,%x,%d)\n", ciptr->fd, buf, size);

#ifdef WIN32
    {
	int ret = send ((SOCKET)ciptr->fd, buf, size, 0);
	errno = WSAGetLastError();
	return ret;
    }
#else
    return write (ciptr->fd, buf, size);
#endif /* WIN32 */
}


static int
TRANS(DNETReadv) (XtransConnInfo ciptr, struct iovec *buf, int size)

{
    PRMSG (2,"DNETReadv(%d,%x,%d)\n", ciptr->fd, buf, size);

    return READV (ciptr, buf, size);
}


static int
TRANS(DNETWritev) (XtransConnInfo ciptr, struct iovec *buf, int size)

{
    PRMSG (2,"DNETWritev(%d,%x,%d)\n", ciptr->fd, buf, size);

    return WRITEV (ciptr, buf, size);
}


static int
TRANS(DNETDisconnect) (XtransConnInfo ciptr)

{
    PRMSG (2,"DNETDisconnect(%x,%d)\n", ciptr, ciptr->fd, 0);

#ifdef WIN32
    {
	int ret = shutdown (ciptr->fd, 2);
	errno = WSAGetLastError();
	return ret;
    }
#else
    return shutdown (ciptr->fd, 2); /* disallow further sends and receives */
#endif
}


static int
TRANS(DNETClose) (XtransConnInfo ciptr)

{
    PRMSG (2,"DNETClose(%x,%d)\n", ciptr, ciptr->fd, 0);

#ifdef WIN32
    {
	int ret = close (ciptr->fd);
	errno = WSAGetLastError();
	return ret;
    }
#else
    return close (ciptr->fd);
#endif
}


Xtransport	TRANS(DNETFuncs) = {
    /* DNET Interface */
    "dnet",
    0,
#ifdef TRANS_CLIENT
    TRANS(DNETOpenCOTSClient),
#endif /* TRANS_CLIENT */
#ifdef TRANS_SERVER
    TRANS(DNETOpenCOTSServer),
#endif /* TRANS_SERVER */
#ifdef TRANS_CLIENT
    TRANS(DNETOpenCLTSClient),
#endif /* TRANS_CLIENT */
#ifdef TRANS_SERVER
    TRANS(DNETOpenCLTSServer),
#endif /* TRANS_SERVER */
#ifdef TRANS_REOPEN
    TRANS(DNETReopenCOTSServer),
    TRANS(DNETReopenCLTSServer),
#endif /* TRANS_REOPEN */
    TRANS(DNETSetOption),
#ifdef TRANS_SERVER
    TRANS(DNETCreateListener),
    NULL,		       			/* ResetListener */
    TRANS(DNETAccept),
#endif /* TRANS_SERVER */
#ifdef TRANS_CLIENT
    TRANS(DNETConnect),
#endif /* TRANS_CLIENT */
    TRANS(DNETBytesReadable),
    TRANS(DNETRead),
    TRANS(DNETWrite),
    TRANS(DNETReadv),
    TRANS(DNETWritev),
    TRANS(DNETDisconnect),
    TRANS(DNETClose),
    TRANS(DNETClose),
};