~ubuntu-branches/ubuntu/wily/sup/wily

« back to all changes in this revision

Viewing changes to scmio.c

  • Committer: Bazaar Package Importer
  • Author(s): Jochen Friedrich
  • Date: 2006-11-02 15:03:30 UTC
  • mfrom: (1.1.1 upstream) (3.1.1 dapper)
  • Revision ID: james.westby@ubuntu.com-20061102150330-yt4yavw4rj1hz8yj
Tags: 20060803-2
Fix CFLAGS, so libwrap is built into sup again.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*      $NetBSD: scmio.c,v 1.16 2006/05/25 02:10:53 christos Exp $      */
 
2
 
1
3
/*
2
4
 * Copyright (c) 1992 Carnegie Mellon University
3
5
 * All Rights Reserved.
4
 
 * 
 
6
 *
5
7
 * Permission to use, copy, modify and distribute this software and its
6
8
 * documentation is hereby granted, provided that both the copyright
7
9
 * notice and this permission notice appear in all copies of the
14
16
 *
15
17
 * Carnegie Mellon requests users of this software to return to
16
18
 *
17
 
 *  Software Distribution Coordinator  or  Software_Distribution@CS.CMU.EDU
 
19
 *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
18
20
 *  School of Computer Science
19
21
 *  Carnegie Mellon University
20
22
 *  Pittsburgh PA 15213-3890
96
98
 *      Any time a process receives a message it does not expect,
97
99
 *      the "readmsg" routine will send a MSGGOAWAY message and
98
100
 *      return SCMEOF.
99
 
 *      
 
101
 *
100
102
 *      Each message has this format:
101
103
 *          ----------    ------------    ------------         ----------
102
104
 *          |msg type|    |count|data|    |count|data|   ...   |ENDCOUNT|
108
110
 *
109
111
 **********************************************************************
110
112
 * HISTORY
111
 
 * $Log: scmio.c,v $
112
 
 * Revision 1.1.1.1  1993/08/21  00:46:33  jkh
113
 
 * Current sup with compression support.
114
 
 *
115
 
 * Revision 1.2  1993/05/24  17:57:26  brezak
116
 
 * Remove netcrypt.c. Remove unneeded files. Cleanup make.
117
 
 *
118
 
 * Revision 1.1.1.1  1993/05/21  14:52:17  cgd
119
 
 * initial import of CMU's SUP to NetBSD
120
 
 *
121
113
 * Revision 1.7  92/09/09  22:04:41  mrt
122
114
 *      Removed the data encryption routines from here to netcrypt.c
123
115
 *      [92/09/09            mrt]
124
 
 * 
 
116
 *
125
117
 * Revision 1.6  92/08/11  12:05:57  mrt
126
 
 *      Brad's changes: Delinted,Added forward declarations of 
 
118
 *      Brad's changes: Delinted,Added forward declarations of
127
119
 *      static functions. Added copyright.
128
120
 *      [92/07/24            mrt]
129
 
 * 
 
121
 *
130
122
 * 27-Dec-87  Glenn Marcy (gm0w) at Carnegie-Mellon University
131
123
 *      Added crosspatch support.
132
124
 *
133
125
 * 28-Jun-87  Glenn Marcy (gm0w) at Carnegie-Mellon University
134
 
 *      Found error in debuging code for readint().
 
126
 *      Found error in debugging code for readint().
135
127
 *
136
128
 * 01-Apr-87  Glenn Marcy (gm0w) at Carnegie-Mellon University
137
129
 *      Added code to readdata to "push" data back into the data buffer.
155
147
 *      Added register variables to decode() for speedup.  Added I/O
156
148
 *      buffering to reduce the number or read/write system calls.
157
149
 *      Removed readmfil/writemfil routines which were not used and were
158
 
 *      not compatable with the other similarly defined routines anyway.
 
150
 *      not compatible with the other similarly defined routines anyway.
159
151
 *
160
152
 * 19-Dec-85  Glenn Marcy (gm0w) at Carnegie-Mellon University
161
153
 *      Created from scm.c I/O and crypt routines.
163
155
 **********************************************************************
164
156
 */
165
157
 
166
 
#include <libc.h>
167
 
#include <stdlib.h>
 
158
#include "libc.h"
168
159
#include <errno.h>
169
160
#include <sys/types.h>
170
161
#include <sys/stat.h>
171
162
#include <sys/file.h>
172
163
#include <sys/time.h>
173
 
#include "sup.h"
 
164
#include <sys/poll.h>
 
165
#include "supcdefs.h"
 
166
#include "supextern.h"
174
167
#include "supmsg.h"
175
 
 
176
 
extern int errno;
 
168
#ifndef INFTIM
 
169
#define INFTIM -1
 
170
#endif
177
171
 
178
172
/*************************
179
173
 ***    M A C R O S    ***
180
174
 *************************/
181
175
 
182
176
/* end of message */
183
 
#define ENDCOUNT (-1)                   /* end of message marker */
184
 
#define NULLCOUNT (-2)                  /* used for sending NULL pointer */
 
177
#define ENDCOUNT (-1)           /* end of message marker */
 
178
#define NULLCOUNT (-2)          /* used for sending NULL pointer */
185
179
 
186
 
#define RETRIES 15                      /* # of times to retry io */
187
 
#define FILEXFER 2048                   /* block transfer size */
 
180
#define RETRIES 15              /* # of times to retry io */
 
181
#define FILEXFER 2048           /* block transfer size */
188
182
#define XFERSIZE(count) ((count > FILEXFER) ? FILEXFER : count)
189
183
 
190
184
/*********************************************
191
185
 ***    G L O B A L   V A R I A B L E S    ***
192
186
 *********************************************/
193
187
 
194
 
extern int scmerr ();                   /* error printing routine */
195
 
extern int netfile;                     /* network file descriptor */
196
 
 
197
 
int scmdebug;                           /* scm debug flag */
198
 
 
199
 
int cryptflag;                          /* whether to encrypt/decrypt data */
200
 
char *cryptbuf;                         /* buffer for data encryption/decryption */
201
 
 
202
 
extern char *goawayreason;              /* reason for goaway message */
 
188
extern int netfile;             /* network file descriptor */
 
189
 
 
190
int scmdebug;                   /* scm debug flag */
 
191
 
 
192
int cryptflag;                  /* whether to encrypt/decrypt data */
 
193
char *cryptbuf;                 /* buffer for data encryption/decryption */
 
194
 
 
195
extern char *goawayreason;      /* reason for goaway message */
203
196
 
204
197
struct buf {
205
 
        char b_data[FILEXFER];          /* buffered data */
206
 
        char *b_ptr;                    /* pointer to end of buffer */
207
 
        int b_cnt;                      /* number of bytes in buffer */
208
 
} buffers[2];
209
 
struct buf *bufptr;                     /* buffer pointer */
 
198
        char b_data[FILEXFER];  /* buffered data */
 
199
        char *b_ptr;            /* pointer to end of buffer */
 
200
        int b_cnt;              /* number of bytes in buffer */
 
201
}   buffers[2];
 
202
struct buf *bufptr;             /* buffer pointer */
 
203
 
 
204
static int writedata(int, char *);
 
205
static int writeblock(int, char *);
 
206
static int readdata(int, char *);
 
207
static int readcount(int *);
210
208
 
211
209
 
212
210
/***********************************************
213
211
 ***    O U T P U T   T O   N E T W O R K    ***
214
212
 ***********************************************/
215
213
 
216
 
static
217
 
writedata (count,data)          /* write raw data to network */
218
 
int count;
219
 
char *data;
220
 
{
221
 
        register int x,tries;
222
 
        register struct buf *bp;
 
214
static int
 
215
writedata(int count, char *data)
 
216
{                               /* write raw data to network */
 
217
        int x, tries;
 
218
        struct buf *bp;
223
219
 
224
220
        if (bufptr) {
225
221
                if (bufptr->b_cnt + count <= FILEXFER) {
226
 
                        bcopy (data,bufptr->b_ptr,count);
 
222
                        memcpy(bufptr->b_ptr, data, count);
227
223
                        bufptr->b_cnt += count;
228
224
                        bufptr->b_ptr += count;
229
225
                        return (SCMOK);
230
226
                }
231
227
                bp = (bufptr == buffers) ? &buffers[1] : buffers;
232
 
                bcopy (data,bp->b_data,count);
 
228
                memcpy(bp->b_data, data, count);
233
229
                bp->b_cnt = count;
234
230
                bp->b_ptr = bp->b_data + count;
235
231
                data = bufptr->b_data;
241
237
        tries = 0;
242
238
        for (;;) {
243
239
                errno = 0;
244
 
                x = write (netfile,data,count);
245
 
                if (x > 0)  break;
246
 
                if (errno)  break;
247
 
                if (++tries > RETRIES)  break;
 
240
                x = write(netfile, data, count);
 
241
                if (x > 0)
 
242
                        break;
 
243
                if (errno)
 
244
                        break;
 
245
                if (++tries > RETRIES)
 
246
                        break;
248
247
                if (scmdebug > 0)
249
 
                        logerr ("SCM Retrying failed network write");
 
248
                        logerr("SCM Retrying failed network write");
250
249
        }
251
250
        if (x <= 0) {
252
251
                if (errno == EPIPE)
253
 
                        return (scmerr (-1,"Network write timed out"));
 
252
                        return (scmerr(-1, "Network write timed out"));
254
253
                if (errno)
255
 
                        return (scmerr (errno,"Write error on network"));
256
 
                return (scmerr (-1,"Write retries failed"));
 
254
                        return (scmerr(errno, "Write error on network"));
 
255
                return (scmerr(-1, "Write retries failed"));
257
256
        }
258
257
        if (x != count)
259
 
                return (scmerr (-1,"Write error on network returned %d on write of %d",x,count));
 
258
                return (scmerr(-1, "Write error on network returned %d on write of %d", x, count));
260
259
        return (SCMOK);
261
260
}
262
261
 
263
 
static
264
 
writeblock (count,data)         /* write data block */
265
 
int count;
266
 
char *data;
267
 
{
268
 
        register int x;
 
262
static int
 
263
writeblock(int count, char *data)
 
264
{                               /* write data block */
 
265
        int x;
269
266
        int y = byteswap(count);
270
267
 
271
 
        x = writedata (sizeof(int),(char *)&y);
272
 
        if (x == SCMOK)  x = writedata (count,data);
 
268
        x = writedata(sizeof(int), (char *) &y);
 
269
        if (x == SCMOK)
 
270
                x = writedata(count, data);
273
271
        return (x);
274
272
}
275
273
 
276
 
writemsg (msg)          /* write start of message */
277
 
int msg;
278
 
{
 
274
int
 
275
writemsg(int msg)
 
276
{                               /* write start of message */
279
277
        int x;
280
278
 
281
279
        if (scmdebug > 1)
282
 
                loginfo ("SCM Writing message %d",msg);
 
280
                loginfo("SCM Writing message %d", msg);
283
281
        if (bufptr)
284
 
                return (scmerr (-1,"Buffering already enabled"));
 
282
                return (scmerr(-1, "Buffering already enabled"));
285
283
        bufptr = buffers;
286
284
        bufptr->b_ptr = bufptr->b_data;
287
285
        bufptr->b_cnt = 0;
288
 
        x = byteswap (msg);
289
 
        return (writedata(sizeof(int),(char *)&x));
 
286
        x = byteswap(msg);
 
287
        return (writedata(sizeof(int), (char *) &x));
290
288
}
291
289
 
292
 
writemend ()            /* write end of message */
293
 
{
294
 
        register int count;
295
 
        register char *data;
 
290
int
 
291
writemend(void)
 
292
{                               /* write end of message */
 
293
        int count;
 
294
        char *data;
296
295
        int x;
297
296
 
298
 
        x = byteswap (ENDCOUNT);
299
 
        x = writedata (sizeof(int),(char *)&x);
300
 
        if (x != SCMOK)  return (x);
 
297
        x = byteswap(ENDCOUNT);
 
298
        x = writedata(sizeof(int), (char *) &x);
 
299
        if (x != SCMOK)
 
300
                return (x);
301
301
        if (bufptr == NULL)
302
 
                return (scmerr (-1,"Buffering already disabled"));
 
302
                return (scmerr(-1, "Buffering already disabled"));
303
303
        if (bufptr->b_cnt == 0) {
304
304
                bufptr = NULL;
305
305
                return (SCMOK);
307
307
        data = bufptr->b_data;
308
308
        count = bufptr->b_cnt;
309
309
        bufptr = NULL;
310
 
        return (writedata (count, data));
 
310
        return (writedata(count, data));
311
311
}
312
312
 
313
 
writeint (i)            /* write int as data block */
314
 
int i;
315
 
{
 
313
int
 
314
writeint(int i)
 
315
{                               /* write int as data block */
316
316
        int x;
317
317
        if (scmdebug > 2)
318
 
                loginfo ("SCM Writing integer %d",i);
 
318
                loginfo("SCM Writing integer %d", i);
319
319
        x = byteswap(i);
320
 
        return (writeblock(sizeof(int),(char *)&x));
 
320
        return (writeblock(sizeof(int), (char *) &x));
321
321
}
322
322
 
323
 
writestring (p)         /* write string as data block */
324
 
char *p;
325
 
{
326
 
        register int len,x;
 
323
int
 
324
writestring(char *p)
 
325
{                               /* write string as data block */
 
326
        int len, x;
327
327
        if (p == NULL) {
328
328
                int y = byteswap(NULLCOUNT);
329
329
                if (scmdebug > 2)
330
 
                        loginfo ("SCM Writing string NULL");
331
 
                return (writedata (sizeof(int),(char *)&y));
 
330
                        loginfo("SCM Writing string NULL");
 
331
                return (writedata(sizeof(int), (char *) &y));
332
332
        }
333
333
        if (scmdebug > 2)
334
 
                loginfo ("SCM Writing string %s",p);
335
 
        len = strlen (p);
 
334
                loginfo("SCM Writing string %s", p);
 
335
        len = strlen(p);
336
336
        if (cryptflag) {
337
 
                x = getcryptbuf (len+1);
 
337
                x = getcryptbuf(len + 1);
338
338
                if (x != SCMOK)
339
339
                        return (x);
340
 
                encode (p,cryptbuf,len);
 
340
                encode(p, cryptbuf, len);
341
341
                p = cryptbuf;
342
342
        }
343
 
        return (writeblock(len,p));
 
343
        return (writeblock(len, p));
344
344
}
345
345
 
346
 
writefile (f)           /* write open file as a data block */
347
 
int f;
348
 
{
 
346
int
 
347
writefile(int f)
 
348
{                               /* write open file as a data block */
349
349
        char buf[FILEXFER];
350
 
        register int number,sum,filesize,x;
 
350
        int number = 0, sum = 0, filesize, x;
351
351
        int y;
352
352
        struct stat statbuf;
353
353
 
354
 
        if (fstat(f,&statbuf) < 0)
355
 
                return (scmerr (errno,"Can't access open file for message"));
 
354
        if (fstat(f, &statbuf) < 0)
 
355
                return (scmerr(errno, "Can't access open file for message"));
356
356
        filesize = statbuf.st_size;
357
357
        y = byteswap(filesize);
358
 
        x = writedata (sizeof(int),(char *)&y);
 
358
        x = writedata(sizeof(int), (char *) &y);
359
359
 
360
 
        if (cryptflag)  x = getcryptbuf (FILEXFER);
 
360
        if (cryptflag)
 
361
                x = getcryptbuf(FILEXFER);
361
362
 
362
363
        if (x == SCMOK) {
363
364
                sum = 0;
364
365
                do {
365
 
                        number = read (f,buf,FILEXFER);
 
366
                        number = read(f, buf, FILEXFER);
366
367
                        if (number > 0) {
367
368
                                if (cryptflag) {
368
 
                                        encode (buf,cryptbuf,number);
369
 
                                        x = writedata (number,cryptbuf);
370
 
                                }
371
 
                                else {
372
 
                                        x = writedata (number,buf);
 
369
                                        encode(buf, cryptbuf, number);
 
370
                                        x = writedata(number, cryptbuf);
 
371
                                } else {
 
372
                                        x = writedata(number, buf);
373
373
                                }
374
374
                                sum += number;
375
375
                        }
376
376
                } while (x == SCMOK && number > 0);
377
377
        }
378
378
        if (sum != filesize)
379
 
                return (scmerr (-1,"File size error on output message"));
 
379
                return (scmerr(-1, "File size error on output message"));
380
380
        if (number < 0)
381
 
                return (scmerr (errno,"Read error on file output message"));
382
 
        return (x);
383
 
}
384
 
 
385
 
writemnull (msg)        /* write message with no data */
386
 
int msg;
387
 
{
388
 
        register int x;
389
 
        x = writemsg (msg);
390
 
        if (x == SCMOK)  x = writemend ();
391
 
        return (x);
392
 
}
393
 
 
394
 
writemint (msg,i)               /* write message of one int */
395
 
int msg,i;
396
 
{
397
 
        register int x;
398
 
        x = writemsg (msg);
399
 
        if (x == SCMOK)  x = writeint (i);
400
 
        if (x == SCMOK)  x = writemend ();
401
 
        return (x);
402
 
}
403
 
 
404
 
writemstr (msg,p)               /* write message of one string */
405
 
int msg;
406
 
char *p;
407
 
{
408
 
        register int x;
409
 
        x = writemsg (msg);
410
 
        if (x == SCMOK)  x = writestring (p);
411
 
        if (x == SCMOK)  x = writemend ();
412
 
        return (x);
413
 
}
414
 
 
 
381
                return (scmerr(errno, "Read error on file output message"));
 
382
        return (x);
 
383
}
 
384
 
 
385
int
 
386
writemnull(int msg)
 
387
{                               /* write message with no data */
 
388
        int x;
 
389
        x = writemsg(msg);
 
390
        if (x == SCMOK)
 
391
                x = writemend();
 
392
        return (x);
 
393
}
 
394
 
 
395
int
 
396
writemint(int msg, int i)
 
397
{                               /* write message of one int */
 
398
        int x;
 
399
        x = writemsg(msg);
 
400
        if (x == SCMOK)
 
401
                x = writeint(i);
 
402
        if (x == SCMOK)
 
403
                x = writemend();
 
404
        return (x);
 
405
}
 
406
 
 
407
int
 
408
writemstr(int msg, char *p)
 
409
{                               /* write message of one string */
 
410
        int x;
 
411
        x = writemsg(msg);
 
412
        if (x == SCMOK)
 
413
                x = writestring(p);
 
414
        if (x == SCMOK)
 
415
                x = writemend();
 
416
        return (x);
 
417
}
415
418
/*************************************************
416
419
 ***    I N P U T   F R O M   N E T W O R K    ***
417
420
 *************************************************/
418
421
 
419
 
static
420
 
readdata (count,data)           /* read raw data from network */
421
 
int count;
422
 
char *data;
423
 
{
424
 
        register char *p;
425
 
        register int n,m,x;
426
 
        int tries;
 
422
static int
 
423
readdata(int count, char *data)
 
424
{                               /* read raw data from network */
 
425
        char *p;
 
426
        int c, n, m, x;
427
427
        static int bufcnt = 0;
428
428
        static char *bufptr;
429
429
        static char buffer[FILEXFER];
430
 
        static int imask;
431
 
        static struct timeval timout;
 
430
        struct pollfd set[1];
432
431
 
433
432
        if (count < 0) {
434
433
                if (bufptr + count < buffer)
435
 
                        return (scmerr (-1,"No space in buffer %d",count));
 
434
                        return (scmerr(-1, "No space in buffer %d", count));
436
435
                bufptr += count;
437
436
                bufcnt -= count;
438
 
                bcopy (data,bufptr,-count);
 
437
                memcpy(bufptr, data, -count);
439
438
                return (SCMOK);
440
439
        }
441
440
        if (count == 0 && data == NULL) {
443
442
                return (SCMOK);
444
443
        }
445
444
        if (count <= bufcnt) {
446
 
                bcopy (bufptr,data,count);
 
445
                memcpy(data, bufptr, count);
447
446
                bufptr += count;
448
447
                bufcnt -= count;
449
448
                return (SCMOK);
450
449
        }
451
450
        if (bufcnt > 0) {
452
 
                bcopy (bufptr,data,bufcnt);
 
451
                memcpy(data, bufptr, bufcnt);
453
452
                data += bufcnt;
454
453
                count -= bufcnt;
455
454
        }
456
455
        bufptr = buffer;
457
456
        bufcnt = 0;
458
 
        timout.tv_usec = 0;
459
 
        timout.tv_sec = 2*60*60;
 
457
        set[0].fd = netfile;
 
458
        set[0].events = POLLIN;
460
459
        p = buffer;
461
460
        n = FILEXFER;
462
461
        m = count;
463
462
        while (m > 0) {
464
 
                tries = 0;
465
 
                for (;;) {
466
 
                        imask = 1 << netfile;
467
 
                        if (select(32,(fd_set *)&imask,(fd_set *)0,(fd_set *)0,&timout) < 0)
468
 
                                imask = 1;
469
 
                        errno = 0;
470
 
                        if (imask)
471
 
                                x = read (netfile,p,n);
472
 
                        else
473
 
                                return (scmerr (-1,"Timeout on network input"));
474
 
                        if (x > 0)  break;
475
 
                        if (x == 0)
476
 
                                return (scmerr (-1,"Premature EOF on network input"));
477
 
                        if (errno)  break;
478
 
                        if (++tries > RETRIES)  break;
479
 
                        if (scmdebug > 0)
480
 
                                loginfo ("SCM Retrying failed network read");
481
 
                }
482
 
                if (x < 0) {
483
 
                        if (errno)
484
 
                                return (scmerr (errno,"Read error on network"));
485
 
                        return (scmerr (-1,"Read retries failed"));
486
 
                }
 
463
                while ((c = poll(set, 1, 2 * 60 * 60 * 1000)) < 1) {
 
464
                        if (c == 0)
 
465
                                return (scmerr(-1, "Timeout on network input"));
 
466
                        if (errno != EINTR)
 
467
                                sleep(5);
 
468
                }
 
469
                x = read(netfile, p, n);
 
470
                if (x == 0)
 
471
                        return (scmerr(-1, "Premature EOF on network input"));
 
472
                if (x < 0)
 
473
                        return (scmerr(errno, "Read error on network"));
487
474
                p += x;
488
475
                n -= x;
489
476
                m -= x;
490
477
                bufcnt += x;
491
478
        }
492
 
        bcopy (bufptr,data,count);
 
479
        memcpy(data, bufptr, count);
493
480
        bufptr += count;
494
481
        bufcnt -= count;
495
482
        return (SCMOK);
496
483
}
497
484
 
498
 
static
499
 
int readcount (count)                   /* read count of data block */
500
 
int *count;
501
 
{
502
 
        register int x;
503
 
        int y;
504
 
        x = readdata (sizeof(int),(char *)&y);
505
 
        if (x != SCMOK)  return (x);
506
 
        *count = byteswap(y);
507
 
        return (SCMOK);
508
 
}
509
 
 
510
 
int prereadcount (count)                /* preread count of data block */
511
 
int *count;
512
 
{
513
 
        register int x;
514
 
        int y;
515
 
        x = readdata (sizeof(int),(char *)&y);
516
 
        if (x != SCMOK)  return (x);
517
 
        x = readdata (-sizeof(int),(char *)&y);
518
 
        if (x != SCMOK)  return (x);
519
 
        *count = byteswap(y);
520
 
        return (SCMOK);
521
 
}
522
 
 
523
 
readflush ()
524
 
{
525
 
        return (readdata (0, (char *)NULL));
526
 
}
527
 
 
528
 
readmsg (msg)           /* read header for expected message */
529
 
int msg;                /* if message is unexpected, send back SCMHUH */
530
 
{
531
 
        register int x;
 
485
static int 
 
486
readcount(int *count)
 
487
{                               /* read count of data block */
 
488
        int x;
 
489
        int y;
 
490
        x = readdata(sizeof(int), (char *) &y);
 
491
        if (x != SCMOK)
 
492
                return (x);
 
493
        *count = byteswap(y);
 
494
        return (SCMOK);
 
495
}
 
496
 
 
497
int 
 
498
prereadcount(int *count)
 
499
{                               /* preread count of data block */
 
500
        int x;
 
501
        int y;
 
502
        x = readdata(sizeof(int), (char *) &y);
 
503
        if (x != SCMOK)
 
504
                return (x);
 
505
        x = readdata(-((int) (sizeof(int))), (char *) &y);
 
506
        if (x != SCMOK)
 
507
                return (x);
 
508
        *count = byteswap(y);
 
509
        return (SCMOK);
 
510
}
 
511
 
 
512
int
 
513
readflush(void)
 
514
{
 
515
        return (readdata(0, (char *) NULL));
 
516
}
 
517
 
 
518
int
 
519
readmsg(int msg)
 
520
{                               /* read header for expected message */
 
521
        /* if message is unexpected, send back SCMHUH */
 
522
        int x;
532
523
        int m;
533
524
        if (scmdebug > 1)
534
 
                loginfo ("SCM Reading message %d",msg);
535
 
        x = readdata (sizeof(int),(char *)&m);  /* msg type */
536
 
        if (x != SCMOK)  return (x);
 
525
                loginfo("SCM Reading message %d", msg);
 
526
        x = readdata(sizeof(int), (char *) &m); /* msg type */
 
527
        if (x != SCMOK)
 
528
                return (x);
537
529
        m = byteswap(m);
538
 
        if (m == msg)  return (x);
 
530
        if (m == msg)
 
531
                return (x);
539
532
 
540
533
        /* check for MSGGOAWAY in case he noticed problems first */
541
534
        if (m != MSGGOAWAY)
542
 
                return (scmerr (-1,"Received unexpected message %d",m));
543
 
        (void) netcrypt ((char *)NULL);
544
 
        (void) readstring (&goawayreason);
545
 
        (void) readmend ();
 
535
                return (scmerr(-1, "Received unexpected message %d", m));
 
536
        (void) netcrypt((char *) NULL);
 
537
        (void) readstring(&goawayreason);
 
538
        (void) readmend();
546
539
        if (goawayreason == NULL)
547
540
                return (SCMEOF);
548
 
        logerr ("SCM GOAWAY %s",goawayreason);
 
541
        logerr("SCM GOAWAY %s", goawayreason);
549
542
        return (SCMEOF);
550
543
}
551
544
 
552
 
readmend ()
 
545
int
 
546
readmend(void)
553
547
{
554
 
        register int x;
 
548
        int x;
555
549
        int y;
556
 
        x = readdata (sizeof(int),(char *)&y);
 
550
        x = readdata(sizeof(int), (char *) &y);
557
551
        y = byteswap(y);
558
552
        if (x == SCMOK && y != ENDCOUNT)
559
 
                return (scmerr (-1,"Error reading end of message"));
 
553
                return (scmerr(-1, "Error reading end of message"));
560
554
        return (x);
561
555
}
562
556
 
563
 
readskip ()                     /* skip over one input block */
564
 
{
565
 
        register int x;
 
557
int
 
558
readskip(void)
 
559
{                               /* skip over one input block */
 
560
        int x;
566
561
        int n;
567
562
        char buf[FILEXFER];
568
 
        x = readcount (&n);
569
 
        if (x != SCMOK)  return (x);
 
563
        x = readcount(&n);
 
564
        if (x != SCMOK)
 
565
                return (x);
570
566
        if (n < 0)
571
 
                return (scmerr (-1,"Invalid message count %d",n));
 
567
                return (scmerr(-1, "Invalid message count %d", n));
572
568
        while (x == SCMOK && n > 0) {
573
 
                x = readdata (XFERSIZE(n),buf);
 
569
                x = readdata(XFERSIZE(n), buf);
574
570
                n -= XFERSIZE(n);
575
571
        }
576
572
        return (x);
577
573
}
578
574
 
579
 
int readint (buf)               /* read int data block */
580
 
int *buf;
581
 
{
582
 
        register int x;
 
575
int 
 
576
readint(int *buf)
 
577
{                               /* read int data block */
 
578
        int x;
583
579
        int y;
584
 
        x = readcount (&y);
585
 
        if (x != SCMOK)  return (x);
 
580
        x = readcount(&y);
 
581
        if (x != SCMOK)
 
582
                return (x);
586
583
        if (y < 0)
587
 
                return (scmerr (-1,"Invalid message count %d",y));
 
584
                return (scmerr(-1, "Invalid message count %d", y));
588
585
        if (y != sizeof(int))
589
 
                return (scmerr (-1,"Size error for int message is %d",y));
590
 
        x = readdata (sizeof(int),(char *)&y);
 
586
                return (scmerr(-1, "Size error for int message is %d", y));
 
587
        x = readdata(sizeof(int), (char *) &y);
591
588
        (*buf) = byteswap(y);
592
589
        if (scmdebug > 2)
593
 
                loginfo ("SCM Reading integer %d",*buf);
 
590
                loginfo("SCM Reading integer %d", *buf);
594
591
        return (x);
595
592
}
596
593
 
597
 
int readstring (buf)    /* read string data block */
598
 
register char **buf;
599
 
{
600
 
        register int x;
 
594
int 
 
595
readstring(char **buf)
 
596
{                               /* read string data block */
 
597
        int x;
601
598
        int count;
602
 
        register char *p;
 
599
        char *p;
603
600
 
604
 
        x = readcount (&count);
605
 
        if (x != SCMOK)  return (x);
 
601
        x = readcount(&count);
 
602
        if (x != SCMOK)
 
603
                return (x);
606
604
        if (count == NULLCOUNT) {
607
605
                if (scmdebug > 2)
608
 
                        loginfo ("SCM Reading string NULL");
 
606
                        loginfo("SCM Reading string NULL");
609
607
                *buf = NULL;
610
608
                return (SCMOK);
611
609
        }
612
610
        if (count < 0)
613
 
                return (scmerr (-1,"Invalid message count %d",count));
 
611
                return (scmerr(-1, "Invalid message count %d", count));
614
612
        if (scmdebug > 3)
615
 
                loginfo ("SCM Reading string count %d",count);
616
 
        if ((p = (char *)malloc ((unsigned)count+1)) == NULL)
617
 
                return (scmerr (-1,"Can't malloc %d bytes for string",count));
 
613
                loginfo("SCM Reading string count %d", count);
 
614
        if ((p = (char *) malloc((unsigned) count + 1)) == NULL)
 
615
                return (scmerr(-1, "Can't malloc %d bytes for string", count));
618
616
        if (cryptflag) {
619
 
                x = getcryptbuf (count+1);
620
 
                if (x == SCMOK)  x = readdata (count,cryptbuf);
621
 
                if (x != SCMOK)  return (x);
622
 
        if (scmdebug > 3)
623
 
                printf ("SCM Reading encrypted string %s\n",cryptbuf);
624
 
                decode (cryptbuf,p,count);
625
 
        }
626
 
        else {
627
 
                x = readdata (count,p);
628
 
                if (x != SCMOK)  return (x);
 
617
                x = getcryptbuf(count + 1);
 
618
                if (x == SCMOK)
 
619
                        x = readdata(count, cryptbuf);
 
620
                if (x != SCMOK) {
 
621
                        free(p);
 
622
                        return (x);
 
623
                }
 
624
                if (scmdebug > 3)
 
625
                        printf("SCM Reading encrypted string %s\n", cryptbuf);
 
626
                decode(cryptbuf, p, count);
 
627
        } else {
 
628
                x = readdata(count, p);
 
629
                if (x != SCMOK) {
 
630
                        free(p);
 
631
                        return (x);
 
632
                }
629
633
        }
630
634
        p[count] = 0;           /* NULL at end of string */
631
635
        *buf = p;
632
636
        if (scmdebug > 2)
633
 
                loginfo ("SCM Reading string %s",*buf);
 
637
                loginfo("SCM Reading string %s", *buf);
634
638
        return (SCMOK);
635
639
}
636
640
 
637
 
readfile (f)            /* read data block into open file */
638
 
int f;
639
 
{
640
 
        register int x;
 
641
int
 
642
readfile(int f)
 
643
{                               /* read data block into open file */
 
644
        int x;
641
645
        int count;
642
646
        char buf[FILEXFER];
643
647
 
644
648
        if (cryptflag) {
645
 
                x = getcryptbuf (FILEXFER);
646
 
                if (x != SCMOK)  return (x);
 
649
                x = getcryptbuf(FILEXFER);
 
650
                if (x != SCMOK)
 
651
                        return (x);
647
652
        }
648
 
        x = readcount (&count);
649
 
        if (x != SCMOK)  return (x);
 
653
        x = readcount(&count);
 
654
        if (x != SCMOK)
 
655
                return (x);
650
656
        if (count < 0)
651
 
                return (scmerr (-1,"Invalid message count %d",count));
 
657
                return (scmerr(-1, "Invalid message count %d", count));
652
658
        while (x == SCMOK && count > 0) {
653
659
                if (cryptflag) {
654
 
                        x = readdata (XFERSIZE(count),cryptbuf);
655
 
                        if (x == SCMOK)  decode (cryptbuf,buf,XFERSIZE(count));
656
 
                }
657
 
                else
658
 
                        x = readdata (XFERSIZE(count),buf);
 
660
                        x = readdata(XFERSIZE(count), cryptbuf);
 
661
                        if (x == SCMOK)
 
662
                                decode(cryptbuf, buf, XFERSIZE(count));
 
663
                } else
 
664
                        x = readdata(XFERSIZE(count), buf);
659
665
                if (x == SCMOK) {
660
 
                        (void) write (f,buf,XFERSIZE(count));
 
666
                        (void) write(f, buf, XFERSIZE(count));
661
667
                        count -= XFERSIZE(count);
662
668
                }
663
669
        }
664
670
        return (x);
665
671
}
666
672
 
667
 
readmnull (msg)         /* read null message */
668
 
int msg;
669
 
{
670
 
        register int x;
671
 
        x = readmsg (msg);
672
 
        if (x == SCMOK)  x = readmend ();
673
 
        return (x);
674
 
}
675
 
 
676
 
readmint (msg,buf)              /* read int message */
677
 
int msg,*buf;
678
 
{
679
 
        register int x;
680
 
        x = readmsg (msg);
681
 
        if (x == SCMOK)  x = readint (buf);
682
 
        if (x == SCMOK)  x = readmend ();
683
 
        return (x);
684
 
}
685
 
 
686
 
int readmstr (msg,buf)          /* read string message */
687
 
int msg;
688
 
char **buf;
689
 
{
690
 
        register int x;
691
 
        x = readmsg (msg);
692
 
        if (x == SCMOK)  x = readstring (buf);
693
 
        if (x == SCMOK)  x = readmend ();
694
 
        return (x);
695
 
}
696
 
 
 
673
int
 
674
readmnull(int msg)
 
675
{                               /* read null message */
 
676
        int x;
 
677
        x = readmsg(msg);
 
678
        if (x == SCMOK)
 
679
                x = readmend();
 
680
        return (x);
 
681
}
 
682
 
 
683
int
 
684
readmint(int msg, int *buf)
 
685
{                               /* read int message */
 
686
        int x;
 
687
        x = readmsg(msg);
 
688
        if (x == SCMOK)
 
689
                x = readint(buf);
 
690
        if (x == SCMOK)
 
691
                x = readmend();
 
692
        return (x);
 
693
}
 
694
 
 
695
int 
 
696
readmstr(int msg, char **buf)
 
697
{                               /* read string message */
 
698
        int x;
 
699
        x = readmsg(msg);
 
700
        if (x == SCMOK)
 
701
                x = readstring(buf);
 
702
        if (x == SCMOK)
 
703
                x = readmend();
 
704
        return (x);
 
705
}
697
706
/**********************************
698
707
 ***    C R O S S P A T C H     ***
699
708
 **********************************/
700
709
 
701
 
crosspatch ()
 
710
void
 
711
crosspatch(void)
702
712
{
703
 
        fd_set ibits, obits, xbits;
704
 
        register int c;
 
713
        struct pollfd set[2];
 
714
        int c;
705
715
        char buf[STRINGLENGTH];
706
716
 
 
717
        set[0].fd = STDIN_FILENO;
 
718
        set[0].events = POLLIN;
 
719
        set[1].fd = netfile;
 
720
        set[1].events = POLLIN;
707
721
        for (;;) {
708
 
                FD_ZERO (&ibits);
709
 
                FD_ZERO (&obits);
710
 
                FD_ZERO (&xbits);
711
 
                FD_SET (0,&ibits);
712
 
                FD_SET (netfile,&ibits);
713
 
                if ((c = select(16, &ibits, &obits, &xbits,
714
 
                                (struct timeval *)NULL)) < 1) {
 
722
                if ((c = poll(set, 2, INFTIM)) < 1) {
715
723
                        if (c == -1) {
716
724
                                if (errno == EINTR) {
717
725
                                        continue;
718
726
                                }
719
727
                        }
720
 
                        sleep (5);
 
728
                        sleep(5);
721
729
                        continue;
722
730
                }
723
 
                if (FD_ISSET (netfile,&ibits)) {
724
 
                        c = read (netfile,buf,sizeof (buf));
 
731
                if (set[1].revents & POLLIN) {
 
732
                        c = read(netfile, buf, sizeof(buf));
725
733
                        if (c < 0 && errno == EWOULDBLOCK)
726
734
                                c = 0;
727
735
                        else {
728
736
                                if (c <= 0) {
729
737
                                        break;
730
738
                                }
731
 
                                (void) write (1,buf,c);
 
739
                                (void) write(1, buf, c);
732
740
                        }
733
741
                }
734
 
                if (FD_ISSET(0, &ibits)) {
735
 
                        c = read (0,buf,sizeof (buf));
 
742
                if (set[0].revents & POLLIN) {
 
743
                        c = read(0, buf, sizeof(buf));
736
744
                        if (c < 0 && errno == EWOULDBLOCK)
737
745
                                c = 0;
738
746
                        else {
739
747
                                if (c <= 0)
740
748
                                        break;
741
 
                                (void) write (netfile,buf,c);
 
749
                                (void) write(netfile, buf, c);
742
750
                        }
743
751
                }
744
752
        }