File: encapsulate.c

package info (click to toggle)
netpipes 4.2-8.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 940 kB
  • sloc: ansic: 11,895; makefile: 353
file content (1787 lines) | stat: -rw-r--r-- 47,220 bytes parent folder | download
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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
/*

    encapsulate.c, part of
    netpipes: network pipe utilities
    Copyright (C) 1996 Robert Forsman

    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.

    */

static char info[] = "encapsulate: a  utility for sockets\nWritten 1996 by Robert Forsman <thoth@purplefrog.com>\n";

/*
   Encapsulate

   encapsulate is designed to be used in a script spawned as the child
   of a faucet or hose.

  ============================================================

  NEW version of encapsulate now.  It uses the Session Control
  Protocol described in http://sunsite.unc.edu/ses/scp.html .

  ARGS:

  If they don't specify --client or --server, compare your socket ID
  (IP first, then port if IPs match) to the other end.  The
  numerically lower one is the server.  The MSB is the first octet of
  the IP.  If they're identical, you have bigger problems >:)

  For each --outfd on the subprocess, send a SYN packet with a new
  session ID.  For each --infd on the subprocess, set aside a
  structure to wait for a SYN packet from the other end.  Each SYN
  packet will be associated with a file descriptor in the order they
  were specified on the command line.

  The --duplex descriptors are trickier.  They need one session ID,
  but who gets to allocate it?  With --Duplex the "client" (which
  could be local or remote) allocates the session ID and sends the SYN
  packet.  The "server" then sends a SYN packet for the same session
  ID causing the connection to become two-way.

  With --duplex, the local program waits for a SYN from the other side
  and responds with a SYN of its own to duplex the session.  With
  --DUPLEX the local program sends the SYN packet and the remote side
  is expected to duplex the session.  The short flags correspond to
  the long like so:

  --Duplex n		-dn
  --duplex n		-ion
  --DUPLEX n		-oin

  Notice how the order of the i and o correspond to the order in which
  the SYN packets are sent.

  INTEROPERABILITY

  This program is immensely complicated by the fact that I want it to
  interoperate with a server that could also be speaking SCP (with
  some theoretical restrictions to prevent the following problems).

  1) SYN packets to associate with input descriptors could come
  arbitrarily late in the conversaion.

  2) The remote program might not be obeying our rules for the
  --Duplex family of directives (this particular problem, and others,
  could also be caused by command line mismatches on either side of
  the link).

  3) In my reading I haven't found any restrictions on bouncing the
  connection between RO, RW and WO, which would wreak havoc on the
  file-descriptor model `encapsulate' is based on.

  For #1, I plan to just cope.  As long as the SYNs eventually arrive,
  things should work.  #2 is stickier, and I want to figure out an
  appropriate way to give a warning that a deadlock might be
  occurring.  #3 I will handle by ruthless application of RESET
  packets and messages to stderr.


  INCOMING BUFFER: store data from socket before it is sent to the
  subprocess.

  HEADER BUFFER: store headers from new SCP packets.

  OUTGOING BUFFER 1: store data from subprocess before it is
  encapsulated in an SCP packet.  NOTE: once data from a subprocess
  descriptor is stored in here, no other outgoing (subprocess-
  writeable) descriptors should be polled.  There is only one of these
  buffers.  This might result in a kind of starvation, so consider
  doing a round-robin thing with the outgoing subprocess descriptors
  that have data available.

  OUTGOING BUFFER 2: store the SCP packet while it's being written to
  the socket.  While this buffer is in use, no packets can be
  encapsulated, so data can accumulate in outgoing buffer 1.  This is
  good because then we have a bigger packet for next time.

  Note: we'll be able to read arbitrarily-sized SCP packets due to the
  design of the state machine.  We'll be limited in the size of
  packets we send by the size of outgoing buffer 1.  I can hear you
  saying "so what".

 */

#include <stdio.h>
#include	<errno.h>
extern int errno;		/* I hate the errno header file */
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/un.h>

#include "common.h"
#include "memmove.h"

#define EXITCODE_ARGS		127
#define EXITCODE_SYSCALLFAILED	126
#define EXITCODE_EXECFAILED	125
#define EXITCODE_SIGNAL		124
#define EXITCODE_PROTOCOL	123
#define EXITCODE_GOT_RESET	122

#define PACKETCODE_EOF		0
#define PACKETCODE_EOF_WAITING	1
#define PACKETCODE_EXITSTATUS	2

/*typedef	short Nshort;*/

#define SCP_SYN		0x80
#define SCP_FIN		0x40
#define SCP_PUSH	0x20
#define SCP_RESET	0x10

#define SESSION_ENCAP	70

/* encapsulate Control Protocol */
#define ECP_EOF		0
#define ECP_EXITCODE	1

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

static int subproc=0;
static int verbose=0;

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

static void usage()
{
    fprintf(stderr,"Usage : %s --fd n [ --verbose ] [ --subproc [ --infd n ] [ --outfd n ] [ --Duplex n ] [ --duplex n ] [ --DUPLEX n ] [ --prefer-local ] [ --prefer-remote ] [ --local-only ] [ --remote-only ] [ --client ] [ --server ] -[#n][v][s[in][on][dn][ion][oin][l][r][L][R]] <command> <args>  ]\n",
	    progname);
}

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

static int remote_return_code = 0;
static int local_return_code = 0;
static int exitcode_warnings = 0;

static int	child_unreaped = 0;
static int	child_running = 0;
static void probe_child();

static int Im_server_p(sock_fd)
     int	sock_fd;
{
    struct sockaddr_storage	me, him;
    socklen_t len;
    int i;

    len = sizeof(me);
    getsockname(sock_fd, (struct sockaddr*)&me, &len);

    len = sizeof(him);
    getpeername(sock_fd, (struct sockaddr*)&him, &len);

    if(me.ss_family != him.ss_family)
	return (me.ss_family == AF_INET) ? 1 : 0;

    if(me.ss_family == AF_INET)
	i = memcmp(&(((struct sockaddr_in *) &me)->sin_addr),
			&(((struct sockaddr_in *) &me)->sin_addr),
			sizeof(struct in_addr));
    else
	i = memcmp(&((struct sockaddr_in6 *) &me)->sin6_addr,
			&((struct sockaddr_in6 *) &me)->sin6_addr,
			sizeof(struct in6_addr));
    if (i<0)
	return 1;
    else if (i>0)
	return 0;


    if (get_port(&me) < get_port(&him))
	return 1;
    else
	return 0;
}

struct pipeline {
    int	child_fd;
    int pipe[2]; /* [0] is for the parent.  [1] is for the child */
    /* this is different from the pipe system call */
    enum pipe_polarity {
	READABLE,
	DUPLEX/*conditional initiate*/,
	DUPLEX_IO/*they initiate*/,
	DUPLEX_OI/*we initiate*/,
	WRITEABLE
    } code;

    /* once the connections are live we need extra info */
    int	session_id;		/* odd for servers, even for clients */
    int	syn_received;

    int bytes_left;		/* in the packet we've partially read */

    struct pipeline *next;	/* linked list */
    enum special_action {
	NOTHING, 
	CLOSE_TO_READ,		/* we got a FIN for this session_id */
	CLOSE_TO_RW,		/* we got a RESET for this session_id */
	CLOSE_TO_WRITE,		/* the child closed the descriptor */
    } specact;
};

struct pipeline	pl_bit_bucket;
struct pipeline	*pl_encapsulate_control;
struct pipeline *pipe_head=0;

int	session_id_;

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

void add_subproc_fd(fd, code, sid)
     int	fd;
     enum pipe_polarity	code;
     int	sid;
{
    struct pipeline	*temp;
    temp = malloc(sizeof(*temp));
    temp->child_fd = fd;
    temp->code = code;
    temp->session_id = sid;
    temp->syn_received = 0;
    temp->bytes_left = 0;
    temp->specact = NOTHING;
    temp->next = 0;
    /* add to tail */
    {
	struct pipeline	**curr;
	for (curr = &pipe_head; *curr; curr = &( (*curr)->next ) )
	    ;
	*curr = temp;
    }
}

void add_control_channel()
{
    struct pipeline	*temp;
    temp = malloc(sizeof(*temp));
    temp->child_fd = -1;
    temp->pipe[0] = -10;
    temp->code = DUPLEX_OI;
    temp->session_id = SESSION_ENCAP;
    temp->syn_received = 0;
    temp->bytes_left = 0;
    temp->specact = NOTHING;
    temp->next = 0;
    /* add to tail */
    {
	struct pipeline	**curr;
	for (curr = &pipe_head; *curr; curr = &( (*curr)->next ) )
	    ;
	*curr = temp;
    }

    pl_encapsulate_control = temp;
}

void remove_pipeline_(pl)
     struct pipeline ** pl;
{
    struct pipeline *tmp;
    tmp = *pl;
    *pl = tmp->next;
    free(tmp);
}

void remove_pipeline(pl)
     struct pipeline * pl;
{
    struct pipeline	**curr;
    for (curr = &pipe_head;
	 *curr && *curr != pl;
	 curr = &( (*curr)->next ) )
	;
    if (0==*curr) {
	fprintf(stderr, "%s: greivous internal error.  attempt to delete unlisted pipeline from queue\n", progname);
	exit(1);
    }
    remove_pipeline_(curr);
}

/* build a buffer full of SYN packets.  The beginning of the buffer is
   the return value, and the length is stored into len_ret. */
static char * prepare_SYNs(len_ret)
     int	*len_ret;
{
    struct pipeline	*curr;
    int	size = 64;
    char *rval = (char*)malloc(size);
    *len_ret = 0;

    for (curr = pipe_head; curr; curr = curr->next) {
	switch (curr->code) {
	case WRITEABLE:
	case DUPLEX_IO:
	    /* we need to wait for the other side to send a SYN for
               these two */
	    /* curr->session_id = -1; it's already -1 */
	    break;
	case READABLE:
	case DUPLEX_OI:
	    if (*len_ret + 8 > size) {
		rval = (char*)realloc(rval, size *=2);
	    }
	    {
		char	*p = rval + *len_ret;
		if (curr->session_id<0) {
		    curr->session_id = session_id_;
		    /* XXX - gotta prevent stompage in later rev */
		    session_id_ += 2; /* odd for servers, even for clients */
		}
		p[0] = SCP_SYN;
		p[1] = (curr->session_id >>16 ) &0xff;
		p[2] = (curr->session_id >> 8 ) &0xff;
		p[3] = (curr->session_id >> 0 ) &0xff;
		p[4] = p[5] = p[6] = p[7] = 0;
		*len_ret += 8;
#ifdef DEBUG
		fprintf(stderr, "%s: prepared SYN for session 0x%06x\n",
			progname, curr->session_id);
#endif
	    }
	    break;
	case DUPLEX:		/* can't happen */
	    abort();
	    exit(1);
	}
    }

    return rval;
}

struct special_packet {
    char	*buf;
    int		len;

    struct special_packet	*next;
};

struct special_packet	*special_packet_queue = 0;


/* stuffs a special packet into the special-packet-queue for sending
   at the next possible moment.  sp and sp->buf must be heap-allocated. */
void prime_packet_queue_(sp)
     struct special_packet	*sp;
{
    struct special_packet	**curr;

    /* add to tail */
    for (curr = &special_packet_queue; *curr; curr = &(*curr)->next)
	;

    *curr = sp;
    sp->next = 0;
}

void prime_packet_queue(buf, len)
     char	*buf;
     int	len;
{
    struct special_packet	*tmp;
    tmp = malloc(sizeof(*tmp));
    tmp->buf = buf;
    tmp->len = len;
    prime_packet_queue_(tmp);
}

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

void maybe_inject_special_packets(buf, len, size)
     char	*buf;
     int	*len;
     int	size;
{
    struct special_packet	*sp;
    if (*len>0)
	return;			/* can't fit one in */

    sp = special_packet_queue;

    if (sp ==0)
	return;			/* no special packets */

    if (sp->len > size) {
	memcpy(buf, sp->buf, size);
	memmove(sp->buf, sp->buf + size, sp->len -size);
	sp->len -= size;
	*len = size;
    } else {
	memcpy(buf, sp->buf, sp->len);
	*len = sp->len;

	special_packet_queue = sp->next;

	free(sp->buf);
	free(sp);
    }
}

void build_fd_lists(readfds, writefds,
		    curr_readable, curr_writeable,
		    sock_fd, maxfd,
		    ibuf_len, ibuf_size,
		    obuf_len, obuf_size,
		    obuf2_len, obuf2_off)
     fd_set	*readfds, *writefds;
     struct pipeline	*curr_readable, *curr_writeable;
     int	sock_fd, *maxfd;
     int	ibuf_len, ibuf_size;
     int	obuf_len, obuf_size;
     int	obuf2_len, obuf2_off;
{
	FD_ZERO(readfds);
	FD_ZERO(writefds);
	*maxfd = -1;
#define    MAX_FD(i)	if (*maxfd < (i)) *maxfd = (i)

	/* when can we write to a child? */
	if (ibuf_len>0 &&
	    curr_writeable->pipe[0]>=0 ) {
	    /* curr_writeable != 0 */
	    FD_SET(curr_writeable->pipe[0], writefds);
	    MAX_FD( curr_writeable->pipe[0] );
	}

	/* when can we read from the socket? */
	if ( curr_writeable == 0 
	     ||
	     (ibuf_len<ibuf_size &&
	      curr_writeable &&
	      curr_writeable->bytes_left > 0 )
	    ) {
#if 1
	    FD_SET(sock_fd, readfds);
	    MAX_FD(sock_fd);
#else
	    struct pipeline	*curr;
	    for (curr = pipe_head; curr; curr = curr ? curr->next : 0) {
		switch(curr->code) {
		case DUPLEX_IO:
		case DUPLEX_OI:
		case DUPLEX:	/* DUPLEX is not really possible */
		case WRITEABLE:
		    FD_SET(sock_fd, readfds);
		    MAX_FD(sock_fd);
		    curr = 0;	/* bail from for loop */
		    break;
		case READABLE:
		    /* we don't write to this one */
		    break;
		}
	    }
#endif
	}

	/* when can we read from a child? */
	if ( curr_readable == 0 ) {
	    /* select from ANY incoming child descriptor */
	    struct pipeline	*curr;
	    for (curr = pipe_head; curr; curr = curr->next) {
		if (curr->pipe[0]<0) continue;
		if ( curr->code == DUPLEX_IO &&
		     ! curr->syn_received)
		    continue;
		if ( curr->specact == CLOSE_TO_WRITE ) continue;
		switch(curr->code) {
		case READABLE:
		case DUPLEX_IO:
		case DUPLEX_OI:
		case DUPLEX:	/* DUPLEX is not really possible */
		    FD_SET(curr->pipe[0], readfds);
		    MAX_FD(curr->pipe[0]);
		    break;
		case WRITEABLE:
		    /* we don't read from this one */
		    break;
		}
	    }
	} else if (obuf_len < obuf_size &&
		   /* yes, the curr_readable could be WRITEABLE-only
		      when the child closes its descriptor, but we
		      haven't copied the bytes into the outgoing
		      socket buffer. In that case we don't want to
		      read from the descriptor. curr_readable will be
		      zeroed when the bytes get copied into the
		      outgoing socket buffer */
		   curr_readable->code != WRITEABLE &&
		   !( curr_readable->specact == CLOSE_TO_WRITE
		      || curr_readable->specact == CLOSE_TO_RW)
		   ) {
	    FD_SET(curr_readable->pipe[0], readfds);
	    MAX_FD(curr_readable->pipe[0]);
	}

	/* when can we write to the socket? */
	if (obuf2_len>obuf2_off) {
	    FD_SET(sock_fd, writefds);
	    MAX_FD(sock_fd);
	}
}

void build_packet_header (buf, flags, session, length)
     char	*buf;		/* length 8 */
     int	flags, session;
     unsigned	length;
{
    buf[0] = flags;
    buf[1] = session>>16;
    buf[2] = session>>8;
    buf[3] = session;
    buf[4] = length>>24;
    buf[5] = length>>16;
    buf[6] = length>>8;
    buf[7] = length;
}

void send_reset(struct pipeline *pl)
{
    char	*buf = malloc(8);
    build_packet_header(buf, SCP_RESET, pl->session_id, 0);
    prime_packet_queue(buf, 8);
}

/* returns number of bytes read into buf at offset *buf_len, limited by
   buf_size.  An uncrecoverable error aborts the program */
int read_from_child(pl, buf, buf_len, buf_size)
     struct pipeline	*pl;
     char	*buf;
     int	*buf_len;
     int	buf_size;
{
    int	rval;

#if DEBUG>1
    fprintf(stderr, "%s: read(in[c%d], buf+%d, %d-%d)",
	    progname, pl->child_fd, *buf_len, buf_size, *buf_len);
#endif
    rval = read(pl->pipe[0], buf+*buf_len, buf_size - *buf_len);
#if DEBUG>1
    fprintf(stderr, "=%d;\n", rval);
#endif
    if (rval == 0) {
#ifdef DEBUG
	fprintf(stderr, "%s: closing in[%d=c%d]\n", progname, pl->pipe[0], pl->child_fd);
#endif
	pl->specact = CLOSE_TO_WRITE;
    } else if (rval<0) {
	if (errno == EINTR) {
	    return 0;		/* no biggie, retry later */
	} else {
	    fprintf(stderr, "%s: error during read(in[%d],,).  Aborting.  ",
		    progname, pl->pipe[0]);
	    perror("");
	    exit(EXITCODE_SYSCALLFAILED);
	}
    } else {
	*buf_len += rval;
    }
    return rval;
}

/* returns number of bytes written from buf, limited by (*buf_len).
   It takes care of moving the bytes in the buffer for the case of an
   incomplete write.  An uncrecoverable error aborts the program */
int write_to_child(pl, buf, buf_len)
     struct pipeline	*pl;
     char	*buf;
     int	*buf_len;
{
    int	rval;

#if DEBUG>1
    fprintf(stderr, "%s: write(out[%d/c%d], buf, %d)",
	    progname, pl->pipe[0], pl->child_fd, *buf_len);
#endif
    rval = write(pl->pipe[0], buf, *buf_len);
#if DEBUG>1
    fprintf(stderr, "=%d;\n", rval);
#endif
    if (rval == 0) {
#ifdef DEBUG
	fprintf(stderr, "%s: Inexplicable!  write(out[%d],,%d) returns 0", progname, pl->pipe[0], *buf_len);
#endif
    } else if (rval<0) {
	if (errno == EINTR) {
	    return 0;		/* no biggie, retry later */
	} else if (errno == EPIPE) {
	    /* DOH! fake it */
	    fprintf(stderr, "%s: EPIPE while writing to child fd %d\n", progname, pl->child_fd);
	    exitcode_warnings = EXITCODE_GOT_RESET;
	    pl->specact = CLOSE_TO_READ;
	    *buf_len = 0;
	    send_reset(pl);
	    return 0;		/* this is NOT a normal thing */
	} else {
	    fprintf(stderr, "%s: error during write(out[%d],,).  Aborting.  ",
		    progname, pl->pipe[0]);
	    perror("");
	    exit(EXITCODE_SYSCALLFAILED);
	}
    } else {
	memmove(buf, buf+rval, *buf_len - rval);
	*buf_len -= rval;
    }
    return rval;
}

/* updates obuf_off to reflect how many bytes it copied from obuf to fd */
void write_to_socket(fd, obuf, obuf_off, obuf_len)
     int	fd;
     char	*obuf;
     int	*obuf_off;
     int	obuf_len;
{
    int	rval;

#if DEBUG>1
    fprintf(stderr, "%s: write(sock_fd, buf+%d, %d-%d)",
	    progname, *obuf_off, obuf_len, *obuf_off);
#endif
    rval = write(fd, obuf+ *obuf_off, obuf_len-*obuf_off);
#if DEBUG>1
    fprintf(stderr, "=%d;\n", rval);
#endif
    if (rval==0) {
	fprintf(stderr, "%s: Inexplicable!  write(sock_fd,,%d) returns 0\n",
		progname, obuf_len-*obuf_off);
	exit(EXITCODE_SYSCALLFAILED);
    } else if (rval<0) {
	if (errno!=EINTR) {
	    fprintf(stderr, "%s: error during write(sock_fd,,%d).  Aborting.  ",
		    progname, obuf_len-*obuf_off);
	    perror("");
	    exit(EXITCODE_SYSCALLFAILED);
	}
	/* never mind. try again later */
    } else {
	*obuf_off += rval;
    }
}

/* read bytes from the socket */

void read_from_socket(fd, pl, buf, buf_len, buf_size)
     int	fd;
     struct pipeline	*pl;
     char	*buf;
     int	*buf_len, buf_size;
{
    int	desired_read = buf_size - *buf_len;
    int	rval;

    if (pl && desired_read > pl->bytes_left) {
	desired_read = pl->bytes_left;
    }
#if DEBUG>1
    fprintf(stderr, "%s: read(sock_fd, buf+%d, %d)",
	    progname, *buf_len, desired_read);
#endif
    rval = read(fd, buf+*buf_len, desired_read);
#if DEBUG>1
    fprintf(stderr, "=%d;\n", rval);
#endif
    if (rval==0) {
	fprintf(stderr, "%s: encapsulation protocol error reading socket.  Socket closed prematurely by remote end.\n", progname);
	/*sock_closed = 1;*/
	exit(EXITCODE_PROTOCOL);
    } else if (rval<0) {
	if (errno!=EINTR) {
	    fprintf(stderr, "%s: error during read(sock_fd,,%d).  Aborting.  ",
		    progname, desired_read);
	    perror("");
	    exit(EXITCODE_SYSCALLFAILED);
	}
	/* never mind. try again later */
    } else {
	*buf_len += rval;

	if (pl) pl->bytes_left -= rval;
    }

    if (pl == &pl_bit_bucket) {
	/* throw away the bytes */
#ifdef DEBUG
	fprintf(stderr, "%s: %d bytes into the bit bucket\n", progname, *buf_len);
#endif
	*buf_len = 0;
    }
}

void interpret_scp_header(header, pl)
     unsigned char	*header;	/* size 8 */
     struct pipeline	**pl;
{
    int	flags, session_id;
    unsigned int	length;
    struct pipeline	*found = 0;

    flags = header[0];
    session_id = ( header[1] << 16 ) | ( header[2] << 8 ) | header[3];
    length = ( header[4] << 24 ) | ( header[5] << 16 ) | ( header[6] << 8 ) | header[7];

#ifdef DEBUG
    fprintf(stderr, "%s: packet. flags=0x%02x, session=0x%06x, length=%d\n",
	    progname, flags, session_id, length);
#endif

    if ( (flags & (SCP_SYN|0xf)) == SCP_SYN) {
	struct pipeline	*curr;
	found = 0;
	/* see if there's one descriptor that's waiting for this
           particular session_id */
	for (curr = pipe_head; curr && !found; curr = curr->next ) {
	    if (curr->syn_received)
		continue;
	    switch(curr->code) {
	    case WRITEABLE: case DUPLEX_IO: case DUPLEX_OI: case DUPLEX:
		/* DUPLEX not actually possible */
		if (curr->session_id == session_id) {
		    found = curr;
		    found->syn_received = 1;
		}
		break;
	    case READABLE:
		break;		/* like, whatever */
	    }
	}
	if (!found) for (curr = pipe_head; curr && !found; curr = curr->next ) {
	    if (curr->syn_received)
		continue;
	    switch(curr->code) {
	    case WRITEABLE: case DUPLEX_IO: case DUPLEX_OI:
	    case DUPLEX: /* DUPLEX not actually possible */
		if (curr->session_id<0) {
		    found = curr;
		    found->session_id = session_id;
		    found->syn_received = 1;
		}
		break;
	    case READABLE:
		break;		/* like, whatever */
	    }
	}
	if (!found) {
	    fprintf(stderr, "%s: Warning! discarding SYN for session_id=0x%06x, to the bit bucket!\n", progname, session_id);
	    found = &pl_bit_bucket;
	} else {
#ifdef DEBUG
	    fprintf(stderr, "%s: received SYN for session 0x%06x", progname, session_id);
#endif
	    if (found->code == DUPLEX_IO) {
		char	*buf = malloc(8);
		build_packet_header(buf, SCP_SYN, session_id, 0);
		prime_packet_queue(buf, 8);
#ifdef DEBUG
		fprintf(stderr, "; sending one back");
#endif
	    }
#ifdef DEBUG
	    putc('\n', stderr);
#endif
	}
	/* fall through and accept any data that might be in the packet */
    } else if ( (flags & (SCP_FIN|0xf)) == SCP_FIN) {
	struct pipeline	*curr;
	for (curr = pipe_head; curr && !found; curr = curr->next ) {
	    if (! curr->syn_received)
		continue;
	    if (curr->session_id != session_id)
		continue;

	    switch(curr->code) {
	    case WRITEABLE: case DUPLEX_IO: case DUPLEX_OI:
	    case DUPLEX: /* DUPLEX not actually possible */
		found = curr;
		found->specact = CLOSE_TO_READ;
		break;
	    case READABLE:
		break;		/* like, whatever */
	    }
	}
	if (!found) {
	    fprintf(stderr, "%s: Warning! discarding FIN for session_id=0x%06x, to the bit bucket!\n", progname, session_id);
	    found = &pl_bit_bucket;
	} else {
#ifdef DEBUG
	    fprintf(stderr, "%s: received FIN for session 0x%06x\n", progname, session_id);
#endif
	}
    } else if ( (flags & (SCP_PUSH|0xf)) == SCP_PUSH) {
	/* like, whatever */
    } else if ( (flags & (SCP_RESET|0xf)) == SCP_RESET) {
	struct pipeline	*curr;
	/* why are they doing this to me? */
	fprintf(stderr, "%s: RESET received for session_id=%d.  closing descriptor bidirectionally\n", progname, session_id);
	for (curr = pipe_head; curr && !found; curr = curr->next ) {
	    if (! curr->syn_received)
		continue;
	    if (curr->session_id != session_id)
		continue;

	    switch(curr->code) {
	    case READABLE: case DUPLEX_IO: case DUPLEX_OI:
	    case DUPLEX: /* DUPLEX not actually possible */
	    case WRITEABLE:
		found = curr;
		found->specact = CLOSE_TO_RW;
		exitcode_warnings = EXITCODE_GOT_RESET;
		break;
	    }
	}
	if (!found) {
	    fprintf(stderr, "%s: Warning! discarding RESET for session_id=0x%06x, to the bit bucket!\n", progname, session_id);
	    found = &pl_bit_bucket;
	}
    } else if (flags != 0) {
	fprintf(stderr, "%s: Warning! funky flags 0x%02x on packet, to the bit bucket!\n", progname, flags);
	found = &pl_bit_bucket;
    } else {
	struct pipeline	*curr;
	for (curr = pipe_head; curr && !found; curr = curr->next ) {
	    if (curr->session_id == session_id) {
		found = curr;
		break;
	    }
	}
	if (!found) {
	    fprintf(stderr, "%s: Warning! discarding data packet for session_id=0x%06x, to the bit bucket!\n", progname, session_id);
	    found = &pl_bit_bucket;
	}
    }

    if (length) {

	if (found->code == READABLE) {
	    fprintf(stderr, "%s: Got data for session 0x%06x I can't write to.  RESETting\n", progname, session_id);
	    send_reset(found);
	    found = &pl_bit_bucket;
	}

	*pl = found;
	(*pl)->bytes_left = length;
    }
}

void handle_control_message(buf, len)
     char	*buf;
     int	len;
{
    if (len<1) {
	fprintf(stderr, "%s: Internal Error: control message length <1. \n", progname);
	exit(1);
    }

    switch(buf[0]) {
    case ECP_EOF:
	if (len!=1) {
	    fprintf(stderr, "%s: Protocol Error: control message[0] length(%d) !=1. \n", progname, len);
	    exit(EXITCODE_PROTOCOL);
	}
#ifdef DEBUG
	fprintf(stderr, "%s: Got EOF from remote.\n", progname);
#endif

	if (pl_encapsulate_control->code == WRITEABLE) {
	    remove_pipeline(pl_encapsulate_control);
	    pl_encapsulate_control = 0;
	} else {
	    pl_encapsulate_control->code = READABLE;
	}

	break;

    case ECP_EXITCODE:
	if (len!=2) {
	    fprintf(stderr, "%s: Protocol Error: control message[1] length(%d) !=2. \n", progname, len);
	    exit(EXITCODE_PROTOCOL);
	}
	remote_return_code = (unsigned char )buf[1];
#ifdef DEBUG
	fprintf(stderr, "%s: remote exit status: %d\n",
		progname, remote_return_code);
#endif

	if (pl_encapsulate_control->code == WRITEABLE) {
	    remove_pipeline(pl_encapsulate_control);
	    pl_encapsulate_control = 0;
	} else {
	    pl_encapsulate_control->code = READABLE;
	}

	break;

    default:
	fprintf(stderr, "%s: Protocol Error: unknown control message [%d]. \n", progname, (unsigned char)buf[0]);
	exit(EXITCODE_PROTOCOL);
	break;
    }
}

void perform_special_actions(ibuf_len, obuf_len, curr_readable, curr_writeable)
     int	*ibuf_len;
     int	*obuf_len;
     struct pipeline	**curr_readable, **curr_writeable;
{
    struct pipeline	**curr;
    for (curr = &pipe_head;
	 *curr;
	 curr = curr ? (&(*curr)->next) : &pipe_head ) {
	switch ((*curr)->specact) {
	case NOTHING:
	    break;
	case CLOSE_TO_READ:
	    /* got a FIN packet */
	    if (*ibuf_len>0)
		break;		/* can't drop it yet */
	    if (*curr_writeable == *curr
		|| *curr_readable == *curr) {
		/* fprintf(stderr, "%s: weird, special action CLOSE_TO_READ applied to a curr_ pipeline %lx\n", progname, (long)*curr); */
		break;
	    }
	    switch((*curr)->code) {
	    case WRITEABLE:
#ifdef DEBUG
		fprintf(stderr, "%s: closing W child fd %d\n", progname, (*curr)->child_fd);
#endif
		close((*curr)->pipe[0]);
		remove_pipeline_(curr);
		curr = 0;	/* start scanning from the beginning */
		break;
	    case DUPLEX:	/* DUPLEX not actually possible */
	    case DUPLEX_IO:
	    case DUPLEX_OI:
#ifdef DEBUG
		fprintf(stderr, "%s: converting child fd %d to child-write-only\n", progname, (*curr)->child_fd);
#endif
		(*curr)->specact = NOTHING;
		(*curr)->code = READABLE;
		shutdown((*curr)->pipe[0], 1);

		break;
	    case READABLE:
		fprintf(stderr, "%s: internal error, attempt to CLOSE_TO_READ on a READABLE descriptor\n", progname);
		exit(1);
	    }
	    break;
	case CLOSE_TO_RW:
	    /* got a RESET packet.  get drastic */
	    if ( (*curr)->bytes_left >0) {
		fprintf(stderr, "%s: Freaky, %d bytes_left in CLOSE_TO_RW channel\n", progname, (*curr)->bytes_left);
		break;		/* can't dump it without corrupting the stream */
	    }
	    if (*curr_readable == *curr) {
		*obuf_len = 0;
		*curr_readable = 0;
	    }
	    if (*curr_writeable == *curr) {
		*ibuf_len = 0;
		*curr_writeable = 0;
	    }
#ifdef DEBUG
	    fprintf(stderr, "%s: RESETting child fd %d\n", progname, (*curr)->child_fd);
#endif
	    {
		struct pipeline *temp = *curr;
		close(temp->pipe[0]);
		*curr = temp->next;
		free(temp);
	    }
	    curr = 0;	/* start scanning from the beginning */
	    break;
	case CLOSE_TO_WRITE:
	    /* child closed the descriptor */
	    if (*curr_writeable == *curr
		|| *curr_readable == *curr) {
		/*fprintf(stderr, "%s: weird, special action CLOSE_TO_WRITE applied to a curr_ pipeline %lx\n", progname, (long)*curr);*/
		break;
	    }

	    {
	      char	*buf;
	      int	len;
	      len = 8;
	      buf = malloc(len);
	      build_packet_header(buf, SCP_FIN, (*curr)->session_id, 0);
	      prime_packet_queue(buf, len);
#ifdef DEBUG
	      fprintf(stderr, "%s: sending FIN for session 0x%06x\n", progname, (*curr)->session_id);
#endif
	    }

	    switch((*curr)->code) {
	    case READABLE:
#ifdef DEBUG
		fprintf(stderr, "%s: closing R child fd %d\n", progname, (*curr)->child_fd);
#endif
		{
		    struct pipeline *temp = *curr;
		    temp = *curr;
		    close(temp->pipe[0]);
		    *curr = temp->next;
		    free(temp);
		}
		curr = 0;	/* start scanning from the beginning */
		break;
	    case DUPLEX:	/* DUPLEX not actually possible */
	    case DUPLEX_IO:
	    case DUPLEX_OI:
#ifdef DEBUG
		fprintf(stderr, "%s: converting child fd %d to child-read-only\n", progname, (*curr)->child_fd);
#endif
		(*curr)->specact = NOTHING;
		(*curr)->code = WRITEABLE;
		shutdown((*curr)->pipe[0], 0);

		break;
	    case WRITEABLE:
		fprintf(stderr, "%s: internal error, attempt to CLOSE_TO_WRITE on a WRITEABLE descriptor\n", progname);
		exit(1);
	    }

	    break;
	}
    }
}

#define BUF_SIZE	4096

static void main_io_loop(sock_fd)
     int	sock_fd;
{
    char incoming_buf[BUF_SIZE]; /* read from socket, will write to child */
    char outgoing_buf[BUF_SIZE]; /* read from child, will packetize into : */
    char outgoing2_buf[BUF_SIZE+8]; /* packet buf, will write to socket */

    /* bytes in the buffer to child */
    /* this is nonzero only if curr_writeable is nonNULL */
    int	incoming_len=0;

    /* this is nonzero only if curr_readable is nonNULL */
    /* bytes in the buffer from child */
    int	outgoing_len=0;

    /* bytes in the buffer to socket */
    /* this is independent of the curr_ variables */
    int	outgoing2_len=0;
    int	outgoing2_off=0;

    /* for reading SCP headers */
    char	header_buf[8];
    int		header_len;

    fd_set	readfds, writefds;
    int	maxfd;

    struct pipeline	*curr_readable=0; /* child descriptor we're reading */
    struct pipeline	*curr_writeable=0; /* child descriptor we're writing */

    while (1) {
	int	rval;

	build_fd_lists (
			&readfds, &writefds,
			curr_readable, curr_writeable,
			sock_fd, &maxfd,
			incoming_len, sizeof(incoming_len),
			outgoing_len, sizeof(outgoing_buf),
			outgoing2_len, outgoing2_off);

	if (
#if 0
	    maxfd<0
	    || 
#else
	    (!subproc || !child_unreaped)
	    &&
#endif
	    (0==pipe_head && 0 == outgoing2_len
		&& special_packet_queue == 0)
	    ) {
	    if (incoming_len != 0 ||
		outgoing_len != 0 ||
		outgoing2_len != 0) {
		fprintf(stderr, "%s: leftover bytes in buffers at end of encapsulation.  That is bad because it means Bob made a logic error in his code.\n", progname);
	    }
	    break;		/* run out of things to do */
	}

	if (maxfd>0) {
	    struct timeval	tv;
	    tv.tv_sec = 0;
	    tv.tv_usec = 500000;
	    rval = select(maxfd+1, &readfds, &writefds, (fd_set*)0, &tv);

	    if (rval<0) {
		if (errno == EINTR) {
		    continue;
		} else {
		    fprintf(stderr, "%s: error during select: ", progname);
		    perror("");
		    exit(EXITCODE_SYSCALLFAILED);
		}
		/* NOTREACHED */
	    } else if (rval==0) {
		/* got a timeout */

	    }

	    /* read bytes from the child */
	    {
		struct pipeline	*curr;
		for (curr = pipe_head; curr; curr = curr->next) {
		    switch (curr->code) {
		    case READABLE:
		    case DUPLEX_IO:
		    case DUPLEX_OI:
		    case DUPLEX:
			if (curr->pipe[0]>=0 &&
			    FD_ISSET(curr->pipe[0], &readfds) &&
			    (curr==curr_readable || 0==curr_readable)) {
			    if (read_from_child(curr, outgoing_buf, &outgoing_len,
						sizeof(outgoing_buf)) )
				curr_readable = curr;
			}
			break;
		    case WRITEABLE:
			break;
		    }
		}
	    }

	    /* write bytes to the child */
	    if (curr_writeable && incoming_len>0 &&
		curr_writeable->pipe[0] >=0 &&
		FD_ISSET(curr_writeable->pipe[0], &writefds) ) {
		write_to_child(curr_writeable, incoming_buf, &incoming_len);
		if (incoming_len<1 && curr_writeable->bytes_left <1) {
		    curr_writeable = 0;
		}
	    }


	    /* write bytes to the socket */
	    if (FD_ISSET(sock_fd, &writefds)) {
		write_to_socket(sock_fd, outgoing2_buf,
				&outgoing2_off, outgoing2_len);
		if (outgoing2_off >= outgoing2_len) {
		    outgoing2_len = 0;
		    outgoing2_off = 0;
		}
	    }

	    /* read bytes from the socket */
	    if (FD_ISSET(sock_fd, &readfds)) {

		if (curr_writeable) {
		    read_from_socket(sock_fd, curr_writeable,
				     incoming_buf, &incoming_len,
				     sizeof(incoming_buf) );
		} else {
		    read_from_socket(sock_fd, (struct pipeline *)0,
				     header_buf, &header_len,
				     sizeof(header_buf));

		    if (header_len==sizeof(header_buf)) {
			interpret_scp_header(header_buf, &curr_writeable);
			header_len = 0;
		    }
		}
	    }
	}

	maybe_inject_special_packets(outgoing2_buf, &outgoing2_len,
				     sizeof(outgoing2_buf));

	if (outgoing2_len==0 && outgoing_len>0) {
	    build_packet_header(outgoing2_buf, 0, curr_readable->session_id,
				outgoing_len);
	    memcpy(outgoing2_buf + 8, outgoing_buf, outgoing_len);
	    outgoing2_len = outgoing_len + 8;
	    outgoing_len = 0;
	}

	if (curr_readable && outgoing_len==0) {
	    curr_readable = 0;
	}

	if (curr_writeable
	    && curr_writeable->bytes_left == 0
	    && curr_writeable == pl_encapsulate_control
	    && incoming_len>0) {
	    handle_control_message(incoming_buf, incoming_len);
	    incoming_len = 0;
	    curr_writeable = 0;
	}

	if (subproc) {
	    if (!child_running && child_unreaped) {
		probe_child();
	    }
	} else {
	    if (pipe_head != 0 &&
		pipe_head == pl_encapsulate_control &&
		pipe_head->next == 0 &&
		pipe_head->code != WRITEABLE) {
		/* all channels are closed */
		char	*buf;
		int	len = 1;
#ifdef DEBUG
		fprintf(stderr, "%s: sending EOF\n",
			progname);
#endif
		buf = malloc(8+len);
		build_packet_header(buf, 0, SESSION_ENCAP, len);
		buf[8] = ECP_EOF;
		prime_packet_queue(buf, 8+len);

		if (pl_encapsulate_control->code == READABLE) {
		    remove_pipeline(pl_encapsulate_control);
		    pl_encapsulate_control = 0;
		} else {
		    pl_encapsulate_control->code = WRITEABLE;
		}
	    }
	}

	perform_special_actions(&incoming_len, &outgoing_len,
				&curr_readable, &curr_writeable);
    }
}

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

static int	childpid = -1;

static void waitonchild(int)
{
    /* got a SIGCHILD.
       It must be that: */
    child_running = 0;
}

static void probe_child()
{
    if (child_running || !child_unreaped)
	return;

    if ( 0>=wait(&local_return_code)) {
	fprintf(stderr, "%s: wait returned error or zero: ", progname);
	perror("");
	exit(EXITCODE_SYSCALLFAILED);
    }
    if (!WIFEXITED(local_return_code))
	local_return_code = EXITCODE_SIGNAL;
    else
	local_return_code = WEXITSTATUS(local_return_code);

    {
	char	*buf;
	int	len = 2;
#ifdef DEBUG
	fprintf(stderr, "%s: sending exit status %d\n",
		progname, local_return_code);
#endif
	buf = malloc(8+len);
	build_packet_header(buf, 0, SESSION_ENCAP, len);
	buf[8] = ECP_EXITCODE;
	buf[8+1] = local_return_code;
	prime_packet_queue(buf, len+8);

	if (pl_encapsulate_control->code == READABLE) {
	    remove_pipeline(pl_encapsulate_control);
	    pl_encapsulate_control = 0;
	} else {
	    pl_encapsulate_control->code = WRITEABLE;
	}
    }

    child_unreaped = 0;
}

static void spawn_child(cmd, sockfd)
     char	**cmd;
     int	sockfd;
{
    struct pipeline	*curr;

    signal(SIGCHLD,waitonchild);
    child_running = 1;		/* well, not yet. */
    child_unreaped = 1;

    /* We're about to allocate a big stack of descriptors.  Let's make
       sure we don't step on on our own toes.  Dup descriptor 0 onto
       each of the descriptors so that our allocations won't get one
       of them.  If you don't have a descriptor 0, then you're a FREAK! */
    /* Uhoh.  Some of them may already be connected to the parent.
       Bob attaches some funky things in funky places. */
    /* The way I tell if a descriptor has been allocated or not is I
       select() on it.  If I get EBADF, the descriptor has not been
       allocated and I can stomp on it before the fork.  If I don't,
       then I won't stomp on it till after the fork. */
    {
	for (curr = pipe_head; curr; curr = curr->next) {
	    if (curr->child_fd<0)
		continue;
	    if (!valid_descriptor(curr->child_fd))
		dup2(0, curr->child_fd); /* "reserve" it */
	}
    }

    for (curr = pipe_head; curr; curr = curr->next) {
	if (curr->child_fd<0)
	    continue;		/* skip this special channel */
	switch (curr->code) {
	case READABLE:
	case WRITEABLE:
	    if (pipe(curr->pipe) !=0) {
		fprintf(stderr, "%s: totally failed to pipe(2): ", progname);
		perror("");
		exit (EXITCODE_SYSCALLFAILED);
	    }
	    break;
	case DUPLEX:
	case DUPLEX_IO:
	case DUPLEX_OI:
	    if (0!=socketpair(AF_UNIX, SOCK_STREAM, 0/*let it choose*/,
			      curr->pipe)) {
		fprintf(stderr, "%s: totally failed to socketpair(2): ",
			progname);
		perror("");
		exit (EXITCODE_SYSCALLFAILED);
	    }
	}
	if (curr->code == WRITEABLE) {
	    /* we need to invert the polarity for this case, eh, Geordi? */
	    int	t;
	    t = curr->pipe[0];
	    curr->pipe[0] = curr->pipe[1];
	    curr->pipe[1] = t;
	}
    }

    childpid = fork();
    if (childpid<0) {
	fprintf(stderr, "%s: unable to fork: ", progname);
	perror("");
	/* I would clear child_running, but, look at the next line */
	exit(EXITCODE_SYSCALLFAILED);
    }

    /* now there's a child running (assuming no race conditions, which
       is why I set it up above and not here.  I'm stupid, but
       paranoid). */

    if (childpid==0) {
	/* child */
	close(sockfd);		/* can't have the child accidentally
				   stomping on our conversation. */
	for (curr = pipe_head; curr; curr = curr->next) {
	    if (curr->child_fd<0)
		continue;		/* skip this special channel */
	    close(curr->pipe[0]);
	    dup2(curr->pipe[1], curr->child_fd);
	    close(curr->pipe[1]);
	}

	execvp(*cmd, cmd);
	fprintf(stderr, "%s: Unable to exec %s: ", progname, *cmd);
	perror("");

	exit(EXITCODE_EXECFAILED);

    } else {
	/* parent */
	for (curr = pipe_head; curr; curr = curr->next) {
	    if (curr->child_fd<0)
		continue;		/* skip this special channel */
	    close(curr->pipe[1]);
	}
    }
}

static void rig_single()
{
    struct pipeline	*curr;
    /* Dear mother of god.  I have to invert the polarity of all the
       pipes.  How the hell am I going to explain this in the manual
       page? */

    for (curr = pipe_head; curr; curr = curr->next) {
	switch (curr->code) {
	case READABLE:
	    curr->code = WRITEABLE;
	    break;
	case WRITEABLE:
	    curr->code = READABLE;
	    break;
	default:
	    /* I don't need to diddle the duplex cases really */
	    break;
	}

	/* so that select and I/O will work */
	curr->pipe[0] = curr->child_fd;
	curr->pipe[1] = -1;
    }
    
}

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

static int scan_flag_numeric_fd(s, fdp)
    char *s;
    int	*fdp;
{
    int	n;
    if (1 != sscanf(s, "%i%n", fdp, &n)) {
	fprintf(stderr, "%s: parse error in file descriptor list at '%s'\n", progname, s);
	exit(EXITCODE_ARGS);
    }
    return n;
}

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

enum mergereturns_ {
    UNINITIALIZED,
    PREFER_LOCAL,		/* if both local and remote processes
				   error, return the local code */
    PREFER_REMOTE,		/* if both local and remote processes
				   error, return the remote code.  */
    LOCAL_ONLY,			/* return the exit code of the local
				   process, ignoring the return code
				   of the remote process. */
    REMOTE_ONLY,		/* return the exit code of the remote
				   process, ignoring the return code
				   of the local process. */
} merge_returns = UNINITIALIZED;

static int	sockfd = -1;
static int	im_server = -1;

int main (argc,argv)
     int argc;
     char ** argv;
     
{
    set_progname(argv[0]);

#if 0
    if (sizeof(Nshort) != 2) {
	fprintf(stderr, "%s: greivous porting error. sizeof(Nshort) == %d, not 2.\n",
		progname, sizeof(Nshort));
	exit(EXITCODE_ARGS);
    }
#endif

    while (argc>1) {
	char *arg = argv[1];
	if (arg[0] != '-') {
	    break;
	}
	arg++;
	if (arg[0] == '-') {
	    arg++;
	    if (0==strcmp(arg, "verbose")) {
		verbose = 1;
		argv++; argc--;
	    } else if (0==strcmp(arg, "fd")) {
		argv++; argc--;
		if (argc<2) {
		    fprintf(stderr, "%s: --fd requires file number for socket.\n",
			    progname);
		    usage();
		    exit(EXITCODE_ARGS);
		} else if (sockfd>=0) {
		    fprintf(stderr, "%s: --fd can only be specified once\n",
			    progname);
		    usage();
		    exit(EXITCODE_ARGS);
		} else  {
		    sockfd = atoi(argv[1]);
		    argv++; argc--;
		}
	    } else if (0==strcmp(arg, "subproc")) {
		subproc = 1;
		argv++; argc--;
	    } else if (0==strcmp(arg, "infd") ||
		       0==strcmp(arg, "outfd") ||
		       0==strcmp(arg, "duplex") ||
		       0==strcmp(arg, "Duplex") ||
		       0==strcmp(arg, "DUPLEX")) {
		long	fd, sid;
		char	*p;
		int	err = 0;
		enum pipe_polarity	pol = -1;
		argv++; argc--;
		if (argc<2 || argv[1][0] == 0) {
		    err = 1;
		} else {
		    fd = strtol(argv[1], &p, 0);
		    if (*p==0) {
			sid = -1;
		    } else if (p[0] == '=' && p[1] != 0) {
			sid = strtol(p+1, &p, 0);
			if (*p != 0) {
			    err = 1;
			}
		    } else {
			err = 1;
		    }
		}
		if (err) {
		    fprintf(stderr, "%s: %s requires descriptor number as argument\n", progname, arg-2);
		    usage();
		    exit(EXITCODE_ARGS);
		}

		{
		  if      (0==strcmp(arg, "infd"))   pol = WRITEABLE;
		  else if (0==strcmp(arg, "outfd"))  pol = READABLE;
		  else if (0==strcmp(arg, "duplex")) pol = DUPLEX_IO;
		  else if (0==strcmp(arg, "Duplex")) pol = DUPLEX;
		  else if (0==strcmp(arg, "DUPLEX")) pol = DUPLEX_OI;
		}
		if (pol == -1) {
		    fprintf(stderr, "%s: code error, polarity uninitialized %s:%d\n", progname, __FILE__, __LINE__);
		    abort();
		}
		add_subproc_fd(fd, pol, -1);
		argv++; argc--;
	    } else if (0==strcmp(arg, "prefer-local")||
		       0==strcmp(arg, "preferlocal")) {
		merge_returns = PREFER_LOCAL;
		argv++; argc--;
	    } else if (0==strcmp(arg, "prefer-remote")||
		       0==strcmp(arg, "preferremote")) {
		merge_returns = PREFER_REMOTE;
		argv++; argc--;
	    } else if (0==strcmp(arg, "local-only")||
		       0==strcmp(arg, "localonly")) {
		merge_returns = LOCAL_ONLY;
		argv++; argc--;
	    } else if (0==strcmp(arg, "remote-only")||
		       0==strcmp(arg, "remoteonly")) {
		merge_returns = REMOTE_ONLY;
		argv++; argc--;
	    } else if (0==strcmp(arg, "client")) {
		im_server = 0;
		argv++; argc--;
	    } else if (0==strcmp(arg, "server")) {
		im_server = 1;
		argv++; argc--;
	    } else {
		/* unknown -- flag.  Assume it's a command :) */
		break;
	    }
	} else {
	    /* it's a set of single dash flags. */
	    do { switch (arg[0]) {
	    case '#':
		arg += scan_flag_numeric_fd(arg+1, &sockfd);
		break;
	    case 'v':
		verbose = 1;
		break;
	    case 's':
		subproc=1;
		break;
	    case 'i':
		if (arg[1] == 'o') {
		    int	fd;
		    arg += scan_flag_numeric_fd(arg+2, &fd);
		    add_subproc_fd(fd, DUPLEX_IO, -1);
		} else {
		    int	fd;
		    arg += scan_flag_numeric_fd(arg+1, &fd);
		    add_subproc_fd(fd, WRITEABLE, -1);
		}
		break;
	    case 'o':
		if (arg[1] == 'i') {
		    int	fd;
		    arg += scan_flag_numeric_fd(arg+2, &fd);
		    add_subproc_fd(fd, DUPLEX_OI, -1);
		} else {
		    int	fd;
		    arg += scan_flag_numeric_fd(arg+1, &fd);
		    add_subproc_fd(fd, READABLE, -1);
		}
		break;
	    case 'd':
		{
		    int	fd;
		    arg += scan_flag_numeric_fd(arg+1, &fd);
		    add_subproc_fd(fd, DUPLEX, -1);
		}
		break;
	    case 'l':
		merge_returns = PREFER_LOCAL;
		break;
	    case 'r':
		merge_returns = PREFER_REMOTE;
		break;
	    case 'L':
		merge_returns = LOCAL_ONLY;
		break;
	    case 'R':
		merge_returns = REMOTE_ONLY;
		break;
	    case 0:
		fprintf(stderr, "%s: blank compact flag.\n", progname);
		/* fall through */
	    default:
		fprintf(stderr, "%s: Unknown compact flag beginning %s\n", progname, arg);
		usage();
		exit (EXITCODE_ARGS);
	    } arg++;
	    } while (arg[0]);

	    argv++;
	    argc--;
	}
    }

    /* argv+1 points to an unrecognized flag that must be the
       subprocess cmd and arguments. */

    if (argc>1 && !subproc) {
	fprintf(stderr, "%s: Unknown flag %s\n", progname, argv[1]);
	usage();
	exit (EXITCODE_ARGS);
    }

    if (sockfd<0) {
	fprintf(stderr, "%s:  I must know the file number for the socket.\n",
		progname);
	usage();
	exit(EXITCODE_ARGS);
    }

    if (subproc) {
	if (merge_returns == UNINITIALIZED)
	    merge_returns = PREFER_LOCAL;
	/* check to make sure at least one descriptor is rerouted */
	if (pipe_head == 0) {
	    fprintf(stderr, "%s: must redirect at least one descriptor of subprocess.\n", progname);
	    usage();
	    exit(EXITCODE_ARGS);
	}
    } else {
	if (pipe_head == 0) {
	    /* rig the degenerate case */
	    add_subproc_fd(0, WRITEABLE, -1);
	    add_subproc_fd(1, READABLE, -1);
	}
	if (merge_returns == PREFER_LOCAL ||
	    merge_returns == PREFER_REMOTE ||
	    merge_returns == LOCAL_ONLY ||
	    merge_returns == REMOTE_ONLY) {
	    fprintf(stderr, "%s: no local process to get a return code from\n", progname);
	    usage();
	    exit (EXITCODE_ARGS);
	}

	merge_returns = LOCAL_ONLY;
    }

    add_control_channel();	/* for passing exit status.  Is DUPLEX_OI. */

    if (verbose)
        emit_version("encapsulate", 1996);

    if (im_server<0) {
	im_server = Im_server_p(sockfd);
    }

    {
	struct pipeline	*curr;
	for (curr = pipe_head; curr; curr = curr->next) {
	    if (curr->code == DUPLEX)
		curr->code = im_server ? DUPLEX_IO : DUPLEX_OI;
	}

	/* might as well initialize our session_id counter */
	session_id_ = im_server ? 1025 : 1024;
	/* below 1024 is reserved */
    }


    if (subproc)
	spawn_child(argv+1, sockfd);
    else {
	/* I have to invert the polarity of writable and readable
	   channels, but not duplex.  Also have to copy the child_fd
	   to pipe[0].  What a hellish mess. */
	rig_single(); }

    signal(SIGPIPE, SIG_IGN);	/* handle the error returns */

    {
	char	*buf;
	int	len;
	buf = prepare_SYNs( &len);
	prime_packet_queue(buf, len);
    }

    main_io_loop(sockfd);

#ifdef DEBUG
    fprintf(stderr, "%s: end of IO loop\n", progname);
#endif

#if 0
    while (child_running) {
	pause();
    }
    probe_child();
#endif

    if (local_return_code ==0)
	local_return_code = exitcode_warnings;

    switch (merge_returns) {
    case PREFER_LOCAL:
	if (local_return_code!=0)
	    exit (local_return_code);
	else
	    exit (remote_return_code);
	/* NOTREACHED */
    case PREFER_REMOTE:
	if (remote_return_code!=0)
	    exit (remote_return_code);
	else
	    exit (local_return_code);
	/* NOTREACHED */
    case LOCAL_ONLY:
	exit(local_return_code);
    case REMOTE_ONLY:
	exit(remote_return_code);
    default:
	fprintf(stderr, "%s: logic error.  merge_returns has bogus value.\n",
		progname);
	exit(EXITCODE_ARGS);
    }
}