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
|
/*
* SCCS: @(#)rdwr.c 1.7 (96/11/04)
*
* UniSoft Ltd., London, England
*
* (C) Copyright 1993 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
/************************************************************************
SCCS: @(#)rdwr.c 1.7 96/11/04 TETware release 3.3
NAME: rdwr.c
PRODUCT: TETware
AUTHOR: Denis McConalogue, UniSoft Limited.
(based on inetlib/rdwr.c)
DATE CREATED: May 1993
DESCRIPTION:
required transport-specific library interfaces
functions to perform i/o on remote process connections
MODIFICATIONS:
Geoff Clare, UniSoft Ltd., August 1996
Changes for TETware.
Geoff Clare, UniSoft Ltd., Oct 1996
Added tet_ts_startup().
************************************************************************/
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <unistd.h>
#include <xti.h>
#include "dtmac.h"
#include "dtmsg.h"
#include "ptab.h"
#include "tptab_xt.h"
#include "bstring.h"
#include "error.h"
#include "ltoa.h"
#include "dtetlib.h"
#include "tslib.h"
#include "server_bs.h"
#include "xtilib_xt.h"
#ifndef NOTRACE
static char msghdr[] = "message header";
static char msgdata[] = "message data";
#endif
/* static function declarations */
static int bs2md PROTOLIST((char *, struct ptab *));
static int doread PROTOLIST((struct ptab *));
static int md2bs PROTOLIST((struct ptab *));
/*
** tet_ts_rcvmsg() - recieve an XTI message on a file descriptor with
** at least some data to read
**
** set one of PF_INPROGRESS, PF_IODONE or PF_IOERR on return
** change connected process state to PS_DEAD on EOF
**
** return an ER_ code to show the state of the local receive processing
*/
int tet_ts_rcvmsg(pp)
register struct ptab *pp;
{
register struct tptab *tp = (struct tptab *) pp->pt_tdata;
register int rc;
int flags;
/* set up variables if first attempt to read this message */
if ((pp->pt_flags & PF_INPROGRESS) == 0) {
tp->tp_cnt = DM_HDRSZ;
tp->tp_ptr = tp->tp_buf;
pp->pt_flags |= (PF_INPROGRESS | PF_RCVHDR);
TRACE2(tet_Tio, 4, "rcvmsg: read %s bytes header",
tet_i2a(tp->tp_cnt));
}
/* process the next bit of the message */
switch (pp->pt_flags & PF_RCVHDR) {
case PF_RCVHDR:
/* read the message header */
if ((rc = doread(pp)) <= 0) {
if (rc < 0)
pp->pt_flags &= ~PF_RCVHDR;
return(ER_OK);
}
TDUMP(tet_Tio, 10, tp->tp_buf, DM_HDRSZ, msghdr);
pp->pt_flags &= ~PF_RCVHDR;
(void) tet_bs2dtmhdr(tp->tp_buf, &pp->ptm_hdr, DM_HDRSZ);
if (pp->ptm_magic != DTM_MAGIC && pp->ptm_len) {
/* we are probably out of sync with sender */
error(0, "received bad message header,",
"ignored message data");
pp->ptm_len = 0;
}
if (pp->ptm_len <= 0) {
/* no data portion */
pp->pt_flags |= PF_IODONE;
return(ER_OK);
}
if (BUFCHK(&tp->tp_buf, &tp->tp_len, pp->ptm_len) < 0) {
/* can't grow data buffer */
pp->pt_flags |= PF_IODONE;
return(ER_ERR);
}
pp->pt_flags |= PF_INPROGRESS;
tp->tp_ptr = tp->tp_buf;
tp->tp_cnt = pp->ptm_len;
TRACE2(tet_Tio, 4, "rcvmsg: read %s bytes data",
tet_i2a(tp->tp_cnt));
/* fall through */
default:
/* read the message data */
if (doread(pp) <= 0)
return(ER_OK);
TDUMP(tet_Tio, 10, tp->tp_buf, pp->ptm_len, msgdata);
if ((rc = bs2md(tp->tp_buf, pp)) != ER_OK) {
pp->pt_flags |= PF_IODONE;
return(rc);
}
pp->pt_flags |= PF_IODONE;
break;
}
return(ER_OK);
}
/*
** doread() - common read processing for both header and data
**
** return 1 when done
** 0 for not done
** -1 on error
*/
static int doread(pp)
register struct ptab *pp;
{
register struct tptab *tp = (struct tptab *) pp->pt_tdata;
register int cnt, rc = -1;
int flags, save_errno;
TRACE3(tet_Tio, 4, "doread: expect %s bytes on fd %s",
tet_i2a(tp->tp_cnt), tet_i2a(tp->tp_fd));
do {
errno = 0;
cnt = t_rcv(tp->tp_fd, tp->tp_ptr, (unsigned)tp->tp_cnt, &flags);
} while (cnt < 0 && (t_errno == TSYSERR && errno == EINTR));
save_errno = t_errno;
if (cnt < 0) {
/* read error */
TRACE3(tet_Tio, 4, "read error, t_errno = %s, read event = %s",
tet_xterrno2a(save_errno),
tet_xtev2a(t_look(tp->tp_fd)));
switch (save_errno) {
case TLOOK:
/* T_DISCONNECT or T_ORDREL (presumably) */
if (pp->pt_flags & PF_LOGGEDON)
error(0, "unexpected EOF",
tet_r2a(&pp->pt_rid));
pp->pt_state = PS_DEAD;
break;
case TNODATA:
if (pp->pt_flags & PF_NBIO)
return(0);
/* else fall through */
default:
pp->pt_flags |= PF_IOERR;
break;
}
}
else if (cnt == 0) {
/* end-of-file */
TRACE1(tet_Tio, 4, "encountered EOF");
pp->pt_state = PS_DEAD;
}
else if ((tp->tp_cnt -= cnt) > 0) {
/* partial read - come back for more later */
TRACE3(tet_Tio, 4, "partial read: wanted %s, received %s",
tet_i2a(tp->tp_cnt + cnt), tet_i2a(cnt));
tp->tp_ptr += cnt;
return(0);
}
else {
/* all done for now */
TRACE1(tet_Tio, 4, "this read complete");
rc = 1;
}
if (pp->pt_state == PS_DEAD)
pp->pt_flags |= PF_IODONE;
if (pp->pt_flags & PF_IOERR)
error(save_errno, "t_rcv() error", tet_r2a(&pp->pt_rid));
/* all non-zero return codes come here */
pp->pt_flags &= ~PF_INPROGRESS;
return(rc);
}
/*
** tet_ts_sndmsg() - send a message to a connected process
**
** set one of PF_INPROGRESS, PF_IODONE or PF_IOERR on return
**
** return an ER_ code to show the state of the local send processing
*/
int tet_ts_sndmsg(pp)
register struct ptab *pp;
{
register struct tptab *tp = (struct tptab *) pp->pt_tdata;
register int rc, err;
int save_errno;
SIG_T (*pipe_save)();
int flags;
/* set up variables for this message on first time through */
if ((pp->pt_flags & PF_INPROGRESS) == 0) {
do {
err = 0;
tp->tp_cnt = tet_dtmhdr2bs(&pp->ptm_hdr, tp->tp_buf);
if (pp->ptm_len <= 0)
rc = 0;
else if ((rc = md2bs(pp)) < 0)
if (pp->pt_flags & PF_SERVER) {
pp->pt_flags |= PF_IODONE;
return(rc);
}
else {
pp->ptm_rc = ER_ERR;
pp->ptm_len = 0;
TRACE2(tet_Tserv, 3,
"md2bs failed, new rc = %s",
tet_ptrepcode(pp->ptm_rc));
err = 1;
continue;
}
TDUMP(tet_Tio, 10, tp->tp_buf, tp->tp_cnt, msghdr);
if (rc > 0) {
TDUMP(tet_Tio, 10, tp->tp_buf + tp->tp_cnt,
rc, msgdata);
tp->tp_cnt += rc;
}
(void) tet_dmlen2bs(rc, tp->tp_buf);
} while (err);
tp->tp_ptr = tp->tp_buf;
pp->pt_flags |= PF_INPROGRESS;
}
TRACE3(tet_Tio, 4, "sendmsg: want to write %s bytes on fd %s",
tet_i2a(tp->tp_cnt), tet_i2a(tp->tp_fd));
/* write out the message -
signals cause the write to be restarted (in an attempt
to cope with systems without restartable system calls) */
do {
errno = 0;
flags = 0;
rc = t_snd(tp->tp_fd, tp->tp_ptr,
(unsigned) tp->tp_cnt, flags);
} while (rc < 0 && (t_errno == TSYSERR && errno == EINTR));
save_errno = t_errno;
/* set state and flags according to results */
if (rc < 0) {
/* write error */
TRACE2(tet_Tio, 4, "t_snd error, t_errno = %s",
tet_xterrno2a(save_errno));
switch (save_errno) {
case TLOOK:
if (t_look(tp->tp_fd) != T_DISCONNECT) {
/* return value unexpected */
xt_error(save_errno, "t_snd() write error",
tet_r2a(&pp->pt_rid));
pp->pt_flags |= PF_IOERR;
break;
}
/* else fall through */
case TSYSERR:
xt_error(save_errno, "process connection broken",
tet_r2a(&pp->pt_rid));
pp->pt_flags |= PF_IODONE;
pp->pt_state = PS_DEAD;
break;
case TFLOW:
if (pp->pt_flags & PF_NBIO)
return(ER_OK);
/* else fall through */
default:
xt_error(save_errno, "t_snd() write error",
tet_r2a(&pp->pt_rid));
pp->pt_flags |= PF_IOERR;
break;
}
}
else if ((tp->tp_cnt -= rc) > 0) {
/* partial write - try again later */
TRACE3(tet_Tio, 4, "partial write: requested %s, only wrote %s",
tet_i2a(tp->tp_cnt + rc), tet_i2a(rc));
tp->tp_ptr += rc;
return(ER_OK);
}
else {
/* all done */
TRACE1(tet_Tio, 4, "write complete");
pp->pt_flags |= PF_IODONE;
}
/* here if no more data to write now */
pp->pt_flags &= ~PF_INPROGRESS;
return(ER_OK);
}
/*
** bs2md() - convert machine-independent message data to internal format
** depending on message type
**
** return ER_OK if successful, or -ve error code on error
*/
static int bs2md(bp, pp)
char *bp;
struct ptab *pp;
{
register int rc;
if ((pp->pt_flags & PF_SERVER) == 0)
pp->pt_savreq = pp->ptm_req;
switch (pp->pt_savreq) {
case OP_NULL:
pp->ptm_len = 0;
rc = ER_OK;
break;
default:
if ((rc = tet_ss_bs2md(bp, pp)) < 0)
pp->ptm_len = 0;
else {
pp->ptm_len = (short) rc;
rc = ER_OK;
}
break;
}
return(rc);
}
/*
** md2bs() - convert message data to machine-independent format
** depending on message type
**
** return the number of bytes occupied by the message, or -ve error code
** on error
*/
static int md2bs(pp)
register struct ptab *pp;
{
register struct tptab *tp = (struct tptab *) pp->pt_tdata;
register int len = 0;
/* work out the required buffer size */
switch (pp->ptm_req) {
case OP_NULL:
len = pp->ptm_len;
break;
}
/* check that the buffer is big enough */
if (len > 0 && BUFCHK(&tp->tp_buf, &tp->tp_len, tp->tp_cnt + len) < 0)
return(ER_ERR);
/* copy the internal data into the machine-independent buffer */
switch (pp->ptm_req) {
case OP_NULL:
bcopy(pp->ptm_data, tp->tp_buf + tp->tp_cnt, len);
break;
default:
len = tet_ss_md2bs(pp, &tp->tp_buf, &tp->tp_len, tp->tp_cnt);
break;
}
return(len);
}
/*
** tet_ts_dead() - perform transport-specific actions for a
** dead connected process
*/
void tet_ts_dead(pp)
register struct ptab *pp;
{
register struct tptab *tp = (struct tptab *) pp->pt_tdata;
TRACE2(tet_Tio, 4, "tet_ts_dead: close fd %s", tet_i2a(tp->tp_fd));
(void) t_close(tp->tp_fd);
tp->tp_fd = -1;
pp->pt_flags &= ~(PF_CONNECTED | PF_INPROGRESS);
}
/*
** tet_ts_startup() - perform transport-specific actions on startup
*/
void tet_ts_startup()
{
/* nothing */
}
/*
** tet_ts_cleanup() - perform transport-specific actions before exiting
*/
void tet_ts_cleanup()
{
/* nothing */
}
|