forked from quinot/taylor-uucp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prott.c
305 lines (235 loc) · 7.83 KB
/
prott.c
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
/* prott.c
The 't' protocol.
Copyright (C) 1991, 1992, 2002 Ian Lance Taylor
This file is part of the Taylor UUCP package.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
The author of the program may be contacted at [email protected].
*/
#include "uucp.h"
#if USE_RCS_ID
const char prott_rcsid[] = "$Id$";
#endif
#include "uudefs.h"
#include "uuconf.h"
#include "conn.h"
#include "trans.h"
#include "system.h"
#include "prot.h"
/* This implementation is based on code written by Rick Adams.
This code implements the 't' protocol, which does no error checking
whatsoever and thus requires an end-to-end verified eight bit
communication line, such as is provided by TCP. Using it with a
modem is unadvisable, since errors can occur between the modem and
the computer. */
/* The buffer size we use. */
#define CTBUFSIZE (1024)
/* The offset in the buffer to the data. */
#define CTFRAMELEN (4)
/* Commands are sent in multiples of this size. */
#define CTPACKSIZE (512)
/* A pointer to the buffer we will use. */
static char *zTbuf;
/* True if we are receiving a file. */
static boolean fTfile;
/* The timeout we use. */
static int cTtimeout = 120;
struct uuconf_cmdtab asTproto_params[] =
{
{ "timeout", UUCONF_CMDTABTYPE_INT, (pointer) &cTtimeout, NULL },
{ NULL, 0, NULL, NULL }
};
/* Local function. */
static boolean ftprocess_data P((struct sdaemon *qdaemon, boolean *pfexit,
size_t *pcneed));
/* Start the protocol. */
boolean
ftstart (struct sdaemon *qdaemon, char **pzlog)
{
*pzlog = NULL;
if (! fconn_set (qdaemon->qconn, PARITYSETTING_NONE,
STRIPSETTING_EIGHTBITS, XONXOFF_OFF))
return FALSE;
zTbuf = (char *) xmalloc (CTBUFSIZE + CTFRAMELEN);
/* The first two bytes of the buffer are always zero. */
zTbuf[0] = 0;
zTbuf[1] = 0;
fTfile = FALSE;
usysdep_sleep (2);
return TRUE;
}
/* Stop the protocol. */
/*ARGSUSED*/
boolean
ftshutdown (struct sdaemon *qdaemon ATTRIBUTE_UNUSED)
{
xfree ((pointer) zTbuf);
zTbuf = NULL;
cTtimeout = 120;
return TRUE;
}
/* Send a command string. We send everything up to and including the
null byte. The number of bytes we send must be a multiple of
TPACKSIZE. */
/*ARGSUSED*/
boolean
ftsendcmd (struct sdaemon *qdaemon, const char *z, int ilocal ATTRIBUTE_UNUSED, int iremote ATTRIBUTE_UNUSED)
{
size_t clen, csend;
char *zalc;
boolean fret;
DEBUG_MESSAGE1 (DEBUG_UUCP_PROTO, "ftsendcmd: Sending command \"%s\"", z);
clen = strlen (z);
/* We need to send the smallest multiple of CTPACKSIZE which is
greater than clen (not equal to clen, since we need room for the
null byte). */
csend = ((clen / CTPACKSIZE) + 1) * CTPACKSIZE;
zalc = zbufalc (csend);
memcpy (zalc, z, clen);
if (csend > clen)
bzero (zalc + clen, csend - clen);
fret = fsend_data (qdaemon->qconn, zalc, csend, TRUE);
ubuffree (zalc);
return fret;
}
/* Get space to be filled with data. We provide a buffer which has
four bytes at the start available to hold the length. */
/*ARGSIGNORED*/
char *
ztgetspace (struct sdaemon *qdaemon ATTRIBUTE_UNUSED, size_t *pclen)
{
*pclen = CTBUFSIZE;
return zTbuf + CTFRAMELEN;
}
/* Send out some data. We are allowed to modify the four bytes
preceding the buffer. This allows us to send the entire block with
header bytes in a single call. */
/*ARGSIGNORED*/
boolean
ftsenddata (struct sdaemon *qdaemon, char *zdata, size_t cdata, int ilocal ATTRIBUTE_UNUSED, int iremote ATTRIBUTE_UNUSED, long int ipos ATTRIBUTE_UNUSED)
{
/* Here we do htonl by hand, since it doesn't exist everywhere. We
know that the amount of data cannot be greater than CTBUFSIZE, so
the first two bytes of this value will always be 0. They were
set to 0 in ftstart so we don't touch them here. This is useful
because we cannot portably right shift by 24 or 16, since we
might be dealing with sixteen bit integers. */
zdata[-2] = (char) ((cdata >> 8) & 0xff);
zdata[-1] = (char) (cdata & 0xff);
/* We pass FALSE to fsend_data since we don't expect the other side
to be sending us anything just now. */
return fsend_data (qdaemon->qconn, zdata - CTFRAMELEN, cdata + CTFRAMELEN,
FALSE);
}
/* Process data and return the amount we need in *pfneed. */
static boolean
ftprocess_data (struct sdaemon *qdaemon, boolean *pfexit, size_t *pcneed)
{
int cinbuf, cfirst, clen;
*pfexit = FALSE;
cinbuf = iPrecend - iPrecstart;
if (cinbuf < 0)
cinbuf += CRECBUFLEN;
if (! fTfile)
{
/* We are not receiving a file. Commands are read in chunks of
CTPACKSIZE. */
while (cinbuf >= CTPACKSIZE)
{
cfirst = CRECBUFLEN - iPrecstart;
if (cfirst > CTPACKSIZE)
cfirst = CTPACKSIZE;
DEBUG_MESSAGE1 (DEBUG_PROTO,
"ftprocess_data: Got %d command bytes",
cfirst);
if (! fgot_data (qdaemon, abPrecbuf + iPrecstart,
(size_t) cfirst, abPrecbuf,
(size_t) CTPACKSIZE - cfirst,
-1, -1, (long) -1, TRUE, pfexit))
return FALSE;
iPrecstart = (iPrecstart + CTPACKSIZE) % CRECBUFLEN;
if (*pfexit)
return TRUE;
cinbuf -= CTPACKSIZE;
}
if (pcneed != NULL)
*pcneed = CTPACKSIZE - cinbuf;
return TRUE;
}
/* Here we are receiving a file. The data comes in blocks. The
first four bytes contain the length, followed by that amount of
data. */
while (cinbuf >= CTFRAMELEN)
{
/* The length is stored in network byte order, MSB first. */
clen = (((((((abPrecbuf[iPrecstart] & 0xff) << 8)
+ (abPrecbuf[(iPrecstart + 1) % CRECBUFLEN] & 0xff)) << 8)
+ (abPrecbuf[(iPrecstart + 2) % CRECBUFLEN] & 0xff)) << 8)
+ (abPrecbuf[(iPrecstart + 3) % CRECBUFLEN] & 0xff));
if (cinbuf < clen + CTFRAMELEN)
{
if (pcneed != NULL)
*pcneed = clen + CTFRAMELEN - cinbuf;
return TRUE;
}
iPrecstart = (iPrecstart + CTFRAMELEN) % CRECBUFLEN;
cfirst = CRECBUFLEN - iPrecstart;
if (cfirst > clen)
cfirst = clen;
DEBUG_MESSAGE1 (DEBUG_PROTO,
"ftprocess_data: Got %d data bytes",
clen);
if (! fgot_data (qdaemon, abPrecbuf + iPrecstart,
(size_t) cfirst, abPrecbuf, (size_t) (clen - cfirst),
-1, -1, (long) -1, TRUE, pfexit))
return FALSE;
iPrecstart = (iPrecstart + clen) % CRECBUFLEN;
if (*pfexit)
return TRUE;
cinbuf -= clen + CTFRAMELEN;
}
if (pcneed != NULL)
*pcneed = CTFRAMELEN - cinbuf;
return TRUE;
}
/* Wait for data to come in and process it until we've reached the end
of a command or a file. */
boolean
ftwait (struct sdaemon *qdaemon)
{
while (TRUE)
{
boolean fexit;
size_t cneed, crec;
if (! ftprocess_data (qdaemon, &fexit, &cneed))
return FALSE;
if (fexit)
return TRUE;
if (! freceive_data (qdaemon->qconn, cneed, &crec, cTtimeout, TRUE))
return FALSE;
if (crec == 0)
{
ulog (LOG_ERROR, "Timed out waiting for data");
return FALSE;
}
}
}
/* File level routine, to set fTfile correctly. */
/*ARGSUSED*/
boolean
ftfile (struct sdaemon *qdaemon ATTRIBUTE_UNUSED, struct stransfer *qtrans ATTRIBUTE_UNUSED, boolean fstart, boolean fsend, long int cbytes ATTRIBUTE_UNUSED, boolean *pfhandled)
{
*pfhandled = FALSE;
if (! fsend)
fTfile = fstart;
return TRUE;
}