summaryrefslogtreecommitdiff
path: root/src/tet3/servlib/logon.c
blob: 0a4e04af00defac9c9dc2c58a5b123f1eca06d27 (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
/*
 *      SCCS:  @(#)logon.c	1.7 (96/11/04) 
 *
 *	UniSoft Ltd., London, England
 *
 * (C) Copyright 1992 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

#ifndef lint
static char sccsid[] = "@(#)logon.c	1.7 (96/11/04) TET3 release 3.3";
#endif

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

SCCS:   	@(#)logon.c	1.7 96/11/04 TETware release 3.3
NAME:		logon.c
PRODUCT:	TETware
AUTHOR:		Andrew Dingwall, UniSoft Ltd.
DATE CREATED:	April 1992

DESCRIPTION:
	function to connect to a server and log on to it

MODIFICATIONS:
	Denis McConalogue, UniSoft Limited, September 1993
	added ti_disconnect() function.

	Andrew Dingwall, UniSoft Ltd., November 1993
	enhancements for FIFO transport interface
	removed disconnect stuff


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

#include <stdio.h>
#include <sys/types.h>
#include "dtmac.h"
#include "dtmsg.h"
#include "ptab.h"
#include "error.h"
#include "servlib.h"
#include "dtetlib.h"
#include "tslib.h"

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

/* static function declarations */
static int ti_lo2 PROTOLIST((struct ptab *));


/*
**	tet_ti_logon() - connect to a server and log on to it
**
**	return zero if successful or -1 on error
*/

int tet_ti_logon(pp)
register struct ptab *pp;
{
	register int wantptype = pp->ptr_ptype;

	if ((pp->pt_flags & (PF_CONNECTED | PF_LOGGEDON)) == (PF_CONNECTED | PF_LOGGEDON)) {
		error(0, "already logged on to", tet_r2a(&pp->pt_rid));
		return(-1);
	}
	pp->pt_flags &= ~PF_LOGGEDON;

	/* connect to the server if necessary */
	if ((pp->pt_flags & PF_CONNECTED) == 0) {
		pp->pt_state = PS_CONNECT;
		pp->pt_flags &= ~PF_LOGGEDOFF;
		tet_si_servwait(pp, LO_DELAY);
		if (!(
			pp->pt_state == PS_CONNECT &&
			(pp->pt_flags & PF_ATTENTION)
		)) {
			if (pp->pt_flags & PF_TIMEDOUT) {
				pp->pt_flags &= ~PF_TIMEDOUT;
				error(0, "connect timed out",
					tet_r2a(&pp->pt_rid));
				tet_ts_dead(pp);
			}
			return(-1);
		}
	}

	/* send a logon message and receive a reply */
	pp->ptm_req = OP_LOGON;
	pp->ptm_mtype = MT_NODATA;
	pp->ptm_len = 0;
	tet_si_clientloop(pp, LO_DELAY);

	/* interpret the return codes */
	if (pp->pt_state == PS_DEAD || pp->pt_flags & PF_IOERR)
		return(-1);
	else if (pp->pt_flags & PF_TIMEDOUT) {
		pp->pt_flags &= ~PF_TIMEDOUT;
		error(0, "server logon timed out", tet_r2a(&pp->pt_rid));
		return(-1);
	}

	/* store the remid from the reply message */
	pp->ptr_sysid = pp->ptm_sysid;
	pp->ptr_pid = pp->ptm_pid;
	pp->ptr_ptype = pp->ptm_ptype;
	pp->pt_flags &= ~PF_LOGGEDOFF;

	/* handle the return codes */
	switch (pp->ptm_rc) {
	case ER_OK:
		pp->pt_flags |= PF_LOGGEDON;
		break;
	case ER_LOGON:
		error(0, "server refused logon", tet_r2a(&pp->pt_rid));
		break;
	case ER_MAGIC:
		error(0, "server and client incompatible",
			tet_r2a(&pp->pt_rid));
		break;
	case ER_ERR:
		error(0, "server error", tet_r2a(&pp->pt_rid));
		break;
	default:
		error(0, "unexpected server reply code",
			tet_ptrepcode(pp->ptm_rc));
		break;
	}

	if (pp->ptr_ptype != wantptype) {
		error(0, "wanted to log on to", tet_ptptype(wantptype));
		error(0, "but found", tet_r2a(&pp->pt_rid));
		if (pp->pt_flags & PF_LOGGEDON)
			(void) tet_ti_logoff(pp, 1);
		tet_ts_dead(pp);
	}

	return((pp->pt_flags & PF_LOGGEDON) ? 0 : -1);
}

/*
**	tet_ti_logoff() - log off from a server
**
**	the connection is left open if stayopen is non-zero,
**	otherwise it is closed
**
**	return 0 if successful or -1 on error
*/

int tet_ti_logoff(pp, stayopen)
struct ptab *pp;
int stayopen;
{
	register int rc;

	if (pp->pt_flags & PF_LOGGEDON)
		rc = ti_lo2(pp);
	else
		rc = 0;

	pp->pt_flags = (pp->pt_flags & ~PF_LOGGEDON) | PF_LOGGEDOFF;

	if (!stayopen)
		tet_ts_dead(pp);

	return(rc);
}

/*
**	ti_lo2() - extend the tet_ti_logoff() processing
**
**	return 0 if successful or -1 on error
*/

static int ti_lo2(pp)
register struct ptab *pp;
{
	char *errmsg;

	/* send a logoff message and receive a reply */
	pp->ptm_req = OP_LOGOFF;
	pp->ptm_mtype = MT_NODATA;
	pp->ptm_len = 0;
	tet_si_clientloop(pp, TALK_DELAY);

	/* interpret the return codes */
	if (pp->pt_state == PS_DEAD || pp->pt_flags & PF_IOERR) {
		tet_ts_dead(pp);
		return(-1);
	}
	else if (pp->pt_flags & PF_TIMEDOUT) {
		pp->pt_flags &= ~PF_TIMEDOUT;
		error(0, "server logoff timed out", tet_r2a(&pp->pt_rid));
		return(-1);
	}
	else {
		switch (pp->ptm_rc) {
		case ER_OK:
			return(0);
		case ER_ERR:
			errmsg = "server error";
			break;
		default:
			error(0, "unexpected server reply code",
				tet_ptrepcode(pp->ptm_rc));
			return(-1);
		}
	}

	error(0, errmsg, tet_r2a(&pp->pt_rid));
	return(-1);
}