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
|
/* UNIX RFCNB (RFC1001/RFC1002) NEtBIOS implementation
Version 1.0
RFCNB IO Routines ...
Copyright (C) Richard Sharpe 1996
*/
/*
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., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "std-includes.h"
#include "rfcnb-priv.h"
#include "rfcnb-util.h"
#include "rfcnb-io.h"
#include <sys/uio.h>
#include <sys/signal.h>
int RFCNB_Timeout = 0; /* Timeout in seconds ... */
void rfcnb_alarm(int sig)
{
fprintf(stderr, "IO Timed out ...\n");
}
/* Set timeout value and setup signal handling */
int RFCNB_Set_Timeout(int seconds)
{
/* If we are on a Bezerkeley system, use sigvec, else sigaction */
#ifndef SA_RESTART
struct sigvec invec, outvec;
#else
struct sigaction inact, outact;
#endif
RFCNB_Timeout = seconds;
if (RFCNB_Timeout > 0) { /* Set up handler to ignore but not restart */
#ifndef SA_RESTART
invec.sv_handler = (void (*)())rfcnb_alarm;
invec.sv_mask = 0;
invec.sv_flags = SV_INTERRUPT;
if (sigvec(SIGALRM, &invec, &outvec) < 0)
return(-1);
#else
inact.sa_handler = (void (*)())rfcnb_alarm;
memset(&(inact.sa_mask), 0, sizeof(inact.sa_mask));
inact.sa_flags = 0; /* Don't restart */
if (sigaction(SIGALRM, &inact, &outact) < 0)
return(-1);
#endif
}
return(0);
}
/* Discard the rest of an incoming packet as we do not have space for it
in the buffer we allocated or were passed ... */
int RFCNB_Discard_Rest(struct RFCNB_Con *con, int len)
{ char temp[100]; /* Read into here */
int rest, this_read, bytes_read;
/* len is the amount we should read */
#ifdef RFCNB_DEBUG
fprintf(stderr, "Discard_Rest called to discard: %i\n", len);
#endif
rest = len;
while (rest > 0) {
this_read = (rest > sizeof(temp)?sizeof(temp):rest);
bytes_read = read(con -> fd, temp, this_read);
if (bytes_read <= 0) { /* Error so return */
if (bytes_read < 0)
RFCNB_errno = RFCNBE_BadRead;
else
RFCNB_errno = RFCNBE_ConGone;
RFCNB_saved_errno = errno;
return(RFCNBE_Bad);
}
rest = rest - bytes_read;
}
return(0);
}
/* Send an RFCNB packet to the connection.
We just send each of the blocks linked together ...
If we can, try to send it as one iovec ...
*/
int RFCNB_Put_Pkt(struct RFCNB_Con *con, struct RFCNB_Pkt *pkt, int len)
{ int len_sent, tot_sent, this_len;
struct RFCNB_Pkt *pkt_ptr;
char *this_data;
int i;
struct iovec io_list[10]; /* We should never have more */
/* If we do, this will blow up ...*/
/* Try to send the data ... We only send as many bytes as len claims */
/* We should try to stuff it into an IOVEC and send as one write */
pkt_ptr = pkt;
len_sent = tot_sent = 0; /* Nothing sent so far */
i = 0;
while ((pkt_ptr != NULL) & (i < 10)) { /* Watch that magic number! */
this_len = pkt_ptr -> len;
this_data = pkt_ptr -> data;
if ((tot_sent + this_len) > len)
this_len = len - tot_sent; /* Adjust so we don't send too much */
/* Now plug into the iovec ... */
io_list[i].iov_len = this_len;
io_list[i].iov_base = this_data;
i++;
tot_sent += this_len;
if (tot_sent == len) break; /* Let's not send too much */
pkt_ptr = pkt_ptr -> next;
}
#ifdef RFCNB_DEBUG
fprintf(stderr, "Frags = %i, tot_sent = %i\n", i, tot_sent);
#endif
/* Set up an alarm if timeouts are set ... */
if (RFCNB_Timeout > 0)
alarm(RFCNB_Timeout);
if ((len_sent = writev(con -> fd, io_list, i)) < 0) { /* An error */
con -> err = errno;
if (errno == EINTR) /* We were interrupted ... */
RFCNB_errno = RFCNBE_Timeout;
else
RFCNB_errno = RFCNBE_BadWrite;
RFCNB_saved_errno = errno;
return(RFCNBE_Bad);
}
if (len_sent < tot_sent) { /* Less than we wanted */
if (errno == EINTR) /* We were interrupted */
RFCNB_errno = RFCNBE_Timeout;
else
RFCNB_errno = RFCNBE_BadWrite;
RFCNB_saved_errno = errno;
return(RFCNBE_Bad);
}
if (RFCNB_Timeout > 0)
alarm(0); /* Reset that sucker */
#ifdef RFCNB_DEBUG
fprintf(stderr, "Len sent = %i ...\n", len_sent);
RFCNB_Print_Pkt(stderr, "sent", pkt, len_sent); /* Print what send ... */
#endif
return(len_sent);
}
/* Read an RFCNB packet off the connection.
We read the first 4 bytes, that tells us the length, then read the
rest. We should implement a timeout, but we don't just yet
*/
int RFCNB_Get_Pkt(struct RFCNB_Con *con, struct RFCNB_Pkt *pkt, int len)
{ int read_len, pkt_len;
char hdr[RFCNB_Pkt_Hdr_Len]; /* Local space for the header */
struct RFCNB_Pkt *pkt_frag;
int more, this_time, offset, frag_len, this_len;
BOOL seen_keep_alive = TRUE;
/* Read that header straight into the buffer */
if (len < RFCNB_Pkt_Hdr_Len) { /* What a bozo */
#ifdef RFCNB_DEBUG
fprintf(stderr, "Trying to read less than a packet:");
perror("");
#endif
RFCNB_errno = RFCNBE_BadParam;
return(RFCNBE_Bad);
}
/* We discard keep alives here ... */
if (RFCNB_Timeout > 0)
alarm(RFCNB_Timeout);
while (seen_keep_alive) {
if ((read_len = read(con -> fd, hdr, sizeof(hdr))) < 0) { /* Problems */
#ifdef RFCNB_DEBUG
fprintf(stderr, "Reading the packet, we got:");
perror("");
#endif
if (errno == EINTR)
RFCNB_errno = RFCNBE_Timeout;
else
RFCNB_errno = RFCNBE_BadRead;
RFCNB_saved_errno = errno;
return(RFCNBE_Bad);
}
/* Now we check out what we got */
if (read_len == 0) { /* Connection closed, send back eof? */
#ifdef RFCNB_DEBUG
fprintf(stderr, "Connection closed reading\n");
#endif
if (errno == EINTR)
RFCNB_errno = RFCNBE_Timeout;
else
RFCNB_errno = RFCNBE_ConGone;
RFCNB_saved_errno = errno;
return(RFCNBE_Bad);
}
if (RFCNB_Pkt_Type(hdr) == RFCNB_SESSION_KEEP_ALIVE) {
#ifdef RFCNB_DEBUG
fprintf(stderr, "RFCNB KEEP ALIVE received\n");
#endif
}
else {
seen_keep_alive = FALSE;
}
}
/* What if we got less than or equal to a hdr size in bytes? */
if (read_len < sizeof(hdr)) { /* We got a small packet */
/* Now we need to copy the hdr portion we got into the supplied packet */
memcpy(pkt -> data, hdr, read_len); /*Copy data */
#ifdef RFCNB_DEBUG
RFCNB_Print_Pkt(stderr, "rcvd", pkt, read_len);
#endif
return(read_len);
}
/* Now, if we got at least a hdr size, alloc space for rest, if we need it */
pkt_len = RFCNB_Pkt_Len(hdr);
#ifdef RFCNB_DEBUG
fprintf(stderr, "Reading Pkt: Length = %i\n", pkt_len);
#endif
/* Now copy in the hdr */
memcpy(pkt -> data, hdr, sizeof(hdr));
/* Get the rest of the packet ... first figure out how big our buf is? */
/* And make sure that we handle the fragments properly ... Sure should */
/* use an iovec ... */
if (len < pkt_len) /* Only get as much as we have space for */
more = len - RFCNB_Pkt_Hdr_Len;
else
more = pkt_len;
this_time = 0;
/* We read for each fragment ... */
if (pkt -> len == read_len){ /* If this frag was exact size */
pkt_frag = pkt -> next; /* Stick next lot in next frag */
offset = 0; /* then we start at 0 in next */
}
else {
pkt_frag = pkt; /* Otherwise use rest of this frag */
offset = RFCNB_Pkt_Hdr_Len; /* Otherwise skip the header */
}
frag_len = pkt_frag -> len;
if (more <= frag_len) /* If len left to get less than frag space */
this_len = more; /* Get the rest ... */
else
this_len = frag_len - offset;
while (more > 0) {
if ((this_time = read(con -> fd, (pkt_frag -> data) + offset, this_len)) <= 0) { /* Problems */
if (errno == EINTR) {
RFCNB_errno = RFCNB_Timeout;
}
else {
if (this_time < 0)
RFCNB_errno = RFCNBE_BadRead;
else
RFCNB_errno = RFCNBE_ConGone;
}
RFCNB_saved_errno = errno;
return(RFCNBE_Bad);
}
#ifdef RFCNB_DEBUG
fprintf(stderr, "Frag_Len = %i, this_time = %i, this_len = %i, more = %i\n", frag_len,
this_time, this_len, more);
#endif
read_len = read_len + this_time; /* How much have we read ... */
/* Now set up the next part */
if (pkt_frag -> next == NULL) break; /* That's it here */
pkt_frag = pkt_frag -> next;
this_len = pkt_frag -> len;
offset = 0;
more = more - this_time;
}
#ifdef RFCNB_DEBUG
fprintf(stderr,"Pkt Len = %i, read_len = %i\n", pkt_len, read_len);
RFCNB_Print_Pkt(stderr, "rcvd", pkt, read_len + sizeof(hdr));
#endif
if (read_len < (pkt_len + sizeof(hdr))) { /* Discard the rest */
return(RFCNB_Discard_Rest(con, (pkt_len + sizeof(hdr)) - read_len));
}
if (RFCNB_Timeout > 0)
alarm(0); /* Reset that sucker */
return(read_len + sizeof(RFCNB_Hdr));
}
|