summaryrefslogtreecommitdiff
path: root/src/tet3/tccd/tccd_xt.c
blob: 2be28268495b8b18be840f92511d37e38c3a8870 (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
/*
 *      SCCS:  @(#)tccd_xt.c	1.9 (98/09/01) 
 *
 *	UniSoft Ltd., London, England
 *
 * (C) Copyright 1993 X/Open Company Limited
 * (C) Copyright 1994 UniSoft Limited
 *
 * All rights reserved.  No part of this source code may be reproduced,
 * stored in a retrieval system, or transmitted, in any form or by any
 * means, electronic, mechanical, photocopying, recording or otherwise,
 * except as stated in the end-user licence agreement, without the prior
 * permission of the copyright owners.
 *
 * X/Open and the 'X' symbol are trademarks of X/Open Company Limited in
 * the UK and other countries.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

/************************************************************************

SCCS:   	@(#)tccd_xt.c	1.9 98/09/01 TETware release 3.3
NAME:		tccd_xt.c
PRODUCT:	TETware
AUTHOR:		Denis McConalogue, UniSoft Ltd.
DATE CREATED:	May 1993

DESCRIPTION:
	server-specific functions for tccd XTI version

MODIFICATIONS:
	Andrew Dingwall, UniSoft Ltd., November 1993
	added ptm_mtype assignment
	added malloc tracing

	Andrew Dingwall, UniSoft Ltd., May 1994
	clear malloc'd memory when not all of it will be filled in

	Andrew Dingwall, UniSoft Ltd., November 1994
	updated t_alloc() structure type names in line with latest XTI spec

	Andrew Dingwall, UniSoft Ltd., October 1996
	changes for TETware

	Andrew Dingwall, UniSoft Ltd., July 1998
	Added support for shared API libraries.

	Aaron Plattner, April 2010
	Fixed warnings when compiled with GCC's -Wall option.

************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <signal.h>
#include <sys/wait.h>
#include <unistd.h>
#ifdef TCPTPI
#  include <netdb.h>
#endif
#include <xti.h>
#include "dtmac.h"
#include "dtmsg.h"
#include "ptab.h"
#include "tptab_xt.h"
#include "error.h"
#include "globals.h"
#include "ltoa.h"
#include "tccd.h"
#include "tccd_bs.h"
#include "server.h"
#include "xtilib_xt.h"
#include "tsinfo_xt.h"
#include "servlib.h"
#include "dtetlib.h"

char *tet_tpname = "/dev/tcp";		/* transport interface provider name */
int tet_tpi_mode = -1;			/* transport provider mode */

int tet_listen_fd;			/* listen file descriptor */
struct netbuf *Tccdaddr;		/* TCCD address */
struct t_call *tet_calls[MAX_CONN_IND];	/* to hold connection indications */


/* static function declarations */
static SIG_FUNC_T waitchild PROTOLIST((int));


/*
**	ss_tsargproc() - transport-specific tccd command-line argument
**		processing
**
**	return 0 if only firstarg was used or 1 if both args were used
*/

int ss_tsargproc(firstarg, nextarg)
char *firstarg, *nextarg;
{
	register int rc = 0;
	char *mode;
	static struct netbuf np;	/* XTI TCCD address */
	register struct netbuf *p;

	switch (*(firstarg + 1)) {
	case 'M':
		if (*(firstarg+2))
			mode = firstarg+2;
		else {
			mode = nextarg;
			rc = 1;
		}
		if ((tet_tpi_mode = tet_mode2i(mode)) < 0)
			fatal(0, "invalid TPI mode (-M switch)", mode);
		
		return (rc);
		break;

	case 'p':
		if (*(firstarg+2)) {
			if ((p = tet_lname2addr(firstarg+2)) == (struct netbuf *)0)
				fatal(0, "invalid -p TCCD address",
					firstarg + 2);
	
		}
		else {
			if ((p = tet_lname2addr(nextarg)) == (struct netbuf *)0)
				fatal(0, "invalid -p TCCD address", nextarg);

			rc = 1;
		}
		errno = 0;
		if ((np.buf = (char *) malloc(p->maxlen)) == (char *) 0)
			fatal(errno, "can't malloc TCCD address buffer",
				(char *) 0);
		TRACE2(tet_Tbuf, 6, "allocate np.buf = %s", tet_i2x(np.buf));

		/* all ok - copy static data to safer place */
		np.maxlen	= p->maxlen;
		np.len		= p->len;
		memcpy(np.buf, p->buf, p->len);
		if (np.maxlen > np.len)
			memset(np.buf + np.len, '\0',
				np.maxlen - np.len);
	
		Tccdaddr = &np;
		break;

	case 'P':
		if (*(firstarg + 2))
			tet_tpname = firstarg + 2;
		else {
			tet_tpname = nextarg;
			rc = 1;
		}
                break;

	default:
		fatal(0, "unknown option", firstarg);
		/* NOTREACHED */
	}
	return (rc);
}

/*
**	ss_tsinitb4fork() - tccd XTI initialisation before forking a daemon
*/

void ss_tsinitb4fork()
{

	register int fd;
	struct t_bind  req;
	struct t_bind *ret;

	/* make sure a tccd address has been specified */
	if (!Tccdaddr)
		xt_fatal(0, "tccd transport address (-p option) not specified",
				(char *)0);

	/* make sure a transport mode has been specified */
	if (tet_tpi_mode < 0) {
		tet_tpi_mode = tet_mode2i("tcp");
		error(0, "transport mode defaulting to \"tcp\"", (char *)0);
	}

	/* get a descriptor to listen on */
	if (!tet_tpname || !*tet_tpname)
		fatal(0, "no transport provider interface defined", (char *)0);
	
	if ((fd = t_open(tet_tpname, O_RDWR, (struct t_info *)0)) < 0)
		xt_fatal(t_errno, "can't open transport provider", tet_tpname);


	/* make sure the listen fd is not stdin, stdout or stderr */
	if (fd < 3) {
		errno = 0;
		if ((tet_listen_fd = fcntl(fd, F_DUPFD, 3)) < 3)
			fatal(errno, "can't dup listen fd", (char *) 0);
		close(fd);
	}
	else
		tet_listen_fd = fd;

	/* allocate the bind request and return structures */
	if ((ret = T_ALLOC_BIND(fd)) == (struct t_bind *)0)
		xt_fatal(t_errno, "can't allocate T_BIND", (char *)0);
	TRACE2(tet_Tbuf, 6, "t_alloc() = %s", tet_i2x(ret));
	
	req.qlen	= MAX_CONN_IND;
	req.addr.maxlen	= Tccdaddr->maxlen;
	req.addr.len	= Tccdaddr->len;
	req.addr.buf	= Tccdaddr->buf;

	
	/* bind the file descriptor to the tccd address */
	if (t_bind(tet_listen_fd, &req, ret) < 0)
		xt_fatal(t_errno, "can't bind listen fd",
			tet_i2a(tet_listen_fd));

	/* make sure the address actually bound was the one we want */
	if (!SAME_XTIADDR(&req, ret))
		xt_fatal(0, "can't bind tccd address",
			tet_addr2lname(Tccdaddr));

	if (ret->qlen == 0)
		xt_fatal(0,"bound address can't accept connections", (char *)0);

	/* arrange to accept incoming connections */
	tet_ts_listen(tet_listen_fd);

	/* arrange to reap child daemon processes */
	signal(SIGCHLD, waitchild);
}

/*
**	ts_forkdaemon() - start a daemon process
*/

void ts_forkdaemon()
{

	tet_si_forkdaemon();
	
}

/*
**	ts_logstart() - make a log file entry when the master daemon starts
*/

void ts_logstart()
{

	logent("START", (char *) 0);

}

/*
**	tet_ss_tsconnect() - server-specific connect processing
**
**	return 0 if successful or -1 on error
*/

int tet_ss_tsconnect(pp)
struct ptab *pp;
{
	/* syncd and xresd addresses have alread been stored when
		op_tsinfo() was called (which also allocated the ptabs) -
		if this had not happened, we would not have got past
		tet_ti_logon() */
	switch (pp->ptr_ptype) {
	case PT_SYNCD:
	case PT_XRESD:
		return(0);
	}

	error(0, "don't know how to connect to", tet_ptptype(pp->ptr_ptype));
	return(-1);
}

/*
**	tet_ss_tsaccept() - server-specific accept() processing
*/

void tet_ss_tsaccept()
{

	/* accept the connection unless we are closing down */
	if (tet_listen_fd >= 0)
		tet_ts_accept(tet_listen_fd);

}

/*
**	tet_ss_tsafteraccept() - server-specific things to do after an accept()
**
**	return 0 if successful or -1 on error
*/

int tet_ss_tsafteraccept(pp)
struct ptab *pp;
{
	register struct tptab *tp = (struct tptab *) pp->pt_tdata;
	register char *p;
	register int pid;

	/* log the connection */
	logent("connection received from", tet_addr2lname(&tp->tp_call));


	if ((pid = tet_dofork()) < 0) {
		error(errno, "can't fork", (char *) 0);
		return(-1);
	}
	else if (!pid) {
		/* in child */
		tet_mypid = getpid();
		close(tet_listen_fd);
		tet_listen_fd = -1;
		signal(SIGCHLD, SIG_DFL);
	}
	else {
		/* in parent - return -1 to close the connection and
			free the ptab entry */
		return(-1);
	}


	return(0);
}

/*
**	ss_tslogon() - make sure that we want to accept a logon request
**		from the connected remote host
**
**	return ER_OK if successful or other ER_* error code on error
*/

int ss_tslogon()
{
	/* the XTI version of tccd does not use the systems.equiv file */
	return(ER_OK);
}

/*
**	waitchild() - reap a child daemon
*/


/* ARGSUSED */
static SIG_FUNC_T waitchild(sig)
int sig;
{
	int status;
#ifndef NOTRACE
	register int pid;
#endif

	signal(SIGCHLD, SIG_DFL);

#ifdef NOTRACE
	while (tet_dowait3(&status, WNOHANG) > 0)
		;
#else
	while ((pid = tet_dowait3(&status, WNOHANG)) > 0)
		TRACE4(tet_Ttccd, 2,
			"waitchild reaped %s, status %s, signal %s",
			tet_i2a(pid), tet_i2a((status >> 8) & 0xff),
			tet_i2a(status & 0xff));
#endif

	signal(SIGCHLD, waitchild);
}


/*
**	ts_bs2tsinfo() - call tet_bs2tsinfo()
*/

int ts_bs2tsinfo(from, fromlen, to, tolen)
char *from;
int fromlen;
char **to;
int *tolen;
{
	return(tet_bs2tsinfo(from, fromlen, (struct tsinfo **) to, tolen));
}

/*
**	op_tsinfo() - receive transport-specific data
*/

void op_tsinfo(pp)
register struct ptab *pp;
{
	register struct tsinfo *mp = (struct tsinfo *) pp->ptm_data;
	register struct netbuf *ap;

	/* all reply messages have no data */
	pp->ptm_mtype = MT_NODATA;
	pp->ptm_len = 0;

	/* decide where to store the data;
		allocate a ptab entry for the server if necessary */
	switch (mp->ts_ptype) {
	case PT_SYNCD:
		if (!tet_sdptab) {
			if ((tet_sdptab = tet_ptalloc()) == (struct ptab *) 0) {
				pp->ptm_rc = ER_ERR;
				return;
			}
			tet_sdptab->ptr_sysid = 0;
			tet_sdptab->ptr_ptype = PT_SYNCD;
			tet_sdptab->pt_flags = PF_SERVER;
		}
		ap = &((struct tptab *) tet_sdptab->pt_tdata)->tp_call;
		break;
	case PT_XRESD:
		if (!tet_xdptab) {
			if ((tet_xdptab = tet_ptalloc()) == (struct ptab *) 0) {
				pp->ptm_rc = ER_ERR;
				return;
			}
			tet_xdptab->ptr_sysid = 0;
			tet_xdptab->ptr_ptype = PT_XRESD;
			tet_xdptab->pt_flags = PF_SERVER;
		}
		ap = &((struct tptab *) tet_xdptab->pt_tdata)->tp_call;
		break;
	default:
		error(0, "received tsinfo for unexpected ptype",
			tet_ptptype(mp->ts_ptype));
		pp->ptm_rc = ER_ERR;
		return;
	}

	/* store the data */
	switch(tet_tpi_mode) {
#ifdef TCPTPI
	case TPI_TCP:

		errno = 0;
		if ((ap->buf = (char *) malloc(sizeof (struct sockaddr_in))) == (char *) 0) {
			error(errno, "can't get buffer for tsinfo",
				(char *) 0);
			pp->ptm_rc = ER_ERR;
			return;
		}
		TRACE2(tet_Tbuf, 6, "allocate TCP tsinfo buffer = %s",
			tet_i2x(ap->buf));
		memset((char *) ap->buf, '\0',
			sizeof (struct sockaddr_in));

		((struct sockaddr_in *)ap->buf)->sin_family
			= AF_INET;
		((struct sockaddr_in *)ap->buf)->sin_addr.s_addr
			= htonl(mp->ts.inet.ts_addr);
		((struct sockaddr_in *)ap->buf)->sin_port
			= htons(mp->ts.inet.ts_port);
	
		ap->maxlen  = sizeof (struct sockaddr_in);
		ap->len     = sizeof (struct sockaddr_in);

		TRACE4(tet_Ttccd, 2,
			"received tsinfo for %s: len  = %s, addr = %s",
			tet_ptptype(mp->ts_ptype), tet_i2a(ap->len),
			tet_addr2lname(ap));
		break;
#endif
#ifdef OSITPI
	case TPI_OSICO:

		ap->maxlen = mp->ts.osico.ts_len;
		ap->len	   = mp->ts.osico.ts_len;

		errno = 0;
		if ((ap->buf = (char *) malloc(ap->len)) == (char *) 0) {
			error(errno, "can't get buffer for tsinfo",
				(char *) 0);
			pp->ptm_rc = ER_ERR;
			return;
		}
		TRACE2(tet_Tbuf, 6, "allocate OSICO tsinfo buffer = %s",
			tet_i2x(ap->buf));
		memcpy(ap->buf, mp->ts.osico.ts_nsap, ap->len);

		TRACE4(tet_Ttccd, 2,
			"received tsinfo for %s: len  = %s, addr = %s",
			tet_ptptype(mp->ts_ptype), tet_i2a(ap->len),
			tet_addr2lname(ap));
		break;
#endif
	default:
	
		error(0, "received tsinfo for invalid tpi mode",
				tet_i2a(tet_tpi_mode));
		pp->ptm_rc = ER_ERR;
		return;

	}
	/* all ok so set up the reply message and return */
	pp->ptm_rc = ER_OK;
}