summaryrefslogtreecommitdiff
path: root/src/tet3/inetlib/accept.c
blob: e59736dc1537a86e37f7f1272e98b284c4c815ad (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
/*
 *      SCCS:  @(#)accept.c	1.7 (97/07/21) 
 *
 *	UniSoft Ltd., London, England
 *
 * (C) Copyright 1992 X/Open Company 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

#ifndef lint
static char sccsid[] = "@(#)accept.c	1.7 (97/07/21) TET3 release 3.3";
#endif

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

SCCS:   	@(#)accept.c	1.7 97/07/21 TETware release 3.3
NAME:		accept.c
PRODUCT:	TETware
AUTHOR:		Andrew Dingwall, UniSoft Ltd.
DATE CREATED:	April 1992

DESCRIPTION:
	function to accept a new INET connection

MODIFICATIONS:

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

#include <errno.h>
#include <sys/types.h>
#  include <sys/uio.h>
#  include <sys/socket.h>
#  include <netinet/in.h>
#include "dtmac.h"
#include "dtmsg.h"
#include "ptab.h"
#include "tptab_in.h"
#include "ltoa.h"
#include "error.h"
#include "tslib.h"
#include "server.h"
#include "server_in.h"
#include "inetlib_in.h"

#ifdef NEEDsrcFile
static char srcFile[] = __FILE__;	/* file name for error reporting */
#endif

/*
**	tet_ts_accept() - accept a new connection on a listening socket
**		and allocate a ptab entry for it
*/

void tet_ts_accept(lsd)
SOCKET lsd;
{
	struct sockaddr_in sin;
	register struct ptab *pp;
	register struct tptab *tp;
	register SOCKET nsd;
	int len, err;

	TRACE2(tet_Tio, 4, "accept connection on sd %s", tet_i2a(lsd));

	/* allocate a proc table entry for this connection */
	if ((pp = tet_ptalloc()) == (struct ptab *) 0)
		return;

	/* accept the connection */
	do {
		err = 0;
		len = sizeof sin;
		if ((nsd = accept(lsd, (struct sockaddr *) &sin, &len)) == INVALID_SOCKET)
			err = SOCKET_ERRNO;
	} while (nsd < 0 && err == SOCKET_EINTR);

	if (nsd == INVALID_SOCKET) {
		error(errno, "accept() failed on sd", tet_i2a(lsd));
		tet_ptfree(pp);
		return;
	}

	TRACE2(tet_Tio, 4, "accept: new sd = %s", tet_i2a(nsd));

	/* store the remote address */
	tp = (struct tptab *) pp->pt_tdata;
	tp->tp_sd = nsd;
	tp->tp_sin = sin;

	/* call server-specific routine to massage socket */
	if (tet_ss_tsafteraccept(pp) < 0) {
		tet_ts_dead(pp);
		tet_ptfree(pp);
		return;
	}

	/* pass the new ptab entry to the server for registration */
	tet_ss_newptab(pp);

	pp->pt_flags |= PF_CONNECTED;
}