File: cbc-frame.c

package info (click to toggle)
nessus-libraries 1.0.10-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 9,536 kB
  • ctags: 12,585
  • sloc: ansic: 72,626; asm: 25,921; sh: 19,570; makefile: 1,974; cpp: 560; pascal: 536; yacc: 234; lex: 203; lisp: 186; perl: 76; fortran: 24
file content (2122 lines) | stat: -rw-r--r-- 57,392 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
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
/*
 *          Copyright (c) mjh-EDV Beratung, 1996-1999
 *     mjh-EDV Beratung - 63263 Neu-Isenburg - Rosenstrasse 12
 *          Tel +49 6102 328279 - Fax +49 6102 328278
 *                Email info@mjh.teddy-net.com
 *
 *    Author: Jordan Hrycaj <jordan@mjh.teddy-net.com>
 *
 *   $Id: cbc-frame.c,v 1.1 2000/08/14 20:51:36 jordan Exp $
 *
 *   This library is free software; you can redistribute it and/or
 *   modify it under the terms of the GNU Library General Public
 *   License as published by the Free Software Foundation; either
 *   version 2 of the License, or (at your option) any later version.
 *
 *   This library 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
 *   Library General Public License for more details.
 */

#include "common-stuff.h"

#ifdef HAVE_NETDB_H
# include <netdb.h>
#endif

#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif

#if TIME_WITH_SYS_TIME
# include <sys/time.h>
# include <time.h>
#else
# if HAVE_SYS_TIME_H
#  include <sys/time.h>
# else
#  include <time.h>
# endif
#endif

#ifndef HAVE_RAND
#define  rand()  random ()
#define srand() srandom ()
#endif

#ifdef HAVE_PROCESS_H
# include <process.h>
# ifdef HAVE__GETPID
#  define getpid() _getpid ()
# endif
#endif

#ifdef HAVE_ASSERT_H
#include <assert.h>
#else
#define assert(x)
#endif

#ifdef USE_ZLIB_COMPRESSION
#include <zlib.h>
#endif

#include "peks-internal.h"
#include "rand/crandom.h"
#include "messages.h"
#include "rand/rnd-pool.h"
#include "cbc-frame-int.h"
#include "cbc-debug.h"
#include "cbc-ioctl.h"
#include "cbc-compr.h"

/* ------------------------------------------------------------------------- *
 *                      private helpers                                      *
 * ------------------------------------------------------------------------- */

static void
destroy_ioState_links 
  (ioCipher  *desc,
   cipher_state *s)
{
  if (s->block != 0)
    xfree (s->block) ;
  /* call a clean up hook */
  if (s->tcatcher.fn != 0 && s->zombie_t == 0)
    _run_tcatcher (desc, s, 0, 0, 0) ;
  if (s->cipher != 0)
    destroy_cipher (s->cipher) ;
  if (s->frame != 0)
    destroy_frame (s->frame) ;
# ifdef USE_ZLIB_COMPRESSION
  if (s->compr_desc != 0) {
    if (is_sender (desc))
      deflateEnd (s->compr_desc) ;
    else
      inflateEnd (s->compr_desc) ;
    xfree (s->compr_desc) ;
  }
# endif
}


static void
destroy_ioCipher_links 
  (void *c)
{
  ioCipher *desc = c;
  /* destroy threads */
  while (desc->thread != 0) {
    cipher_thread *t = desc->thread ;
    desc->thread = t->next ;
    destroy_ioState_links (desc, &t->state) ;
    xfree (t) ;
  }

  /* destroy the basic stream */
  if (desc->cache != 0)
    xfree (desc->cache) ;
  if (desc->sender != 0)
    xfree (desc->sender) ;
  destroy_ioState_links (desc, &desc->state) ;
}

/* ------------------------------------------------------------------------- *
 *                      private input cache handling                         *
 * ------------------------------------------------------------------------- */

static int
extract_from_io_cache
 (cipher_iocache *ioc,   /* beware of ioc == NULL */
  char           *trg,
  unsigned        len)
{
  /* prevent from a cache underflow */
  if (ioc->fill < len)
    len = ioc->fill ;

  if (len) {
    /* move to target buffer */
    if (len == 1)
      trg [0] = ioc->data [ioc->start] ;
    else
      memcpy (trg, ioc->data + ioc->start, len) ;
    if ((ioc->fill -= len) == 0)
      ioc->start = 0 ;
    else
      ioc->start += len ;
  }

  return len ;
}


static void
append_to_io_cache 
  (cipher_iocache *ioc,
   char           *buf,
   unsigned        len)
{
  /* beware of ioc == NULL */

  unsigned max_buf_usage = (ioc->dim >> 1) ;

  POINT_OF_RANDOM_STACK (1) ;

  /* Appending a text [0..len-1] to the input cache data, we need to 
     check whether we must compact the input cache. This is done by
     a shift left of the text already stored. */
  
  if (max_buf_usage < ioc->start + ioc->fill + len) { 
    
    assert (ioc->fill + len <= ioc->dim) ;

    if (ioc->fill) 
      /* there is no more space for len bytes, so shift left */
      memmove (ioc->data, ioc->data + ioc->start, ioc->fill) ;
    
    /* ok, define the new start of the input cache */ 
    ioc->start = 0 ; 
  }
  
  /* append the input block to the cache */
  memcpy (ioc->data + ioc->start, buf, len) ;
  ioc->fill += len;

  POINT_OF_RANDOM_STACK (7) ;
}

/* note, that this function has another descriptor type */
static int
resize_ioCache
  (ioCipher *desc,
   unsigned   new_size)
{
  cipher_iocache *ioc = desc->cache ;

  POINT_OF_RANDOM_STACK (3) ;
  
  /* prevent from a cache underflow */
  if (ioc->fill >= new_size) {
    errno = errnum (CBC_CACHERES_BLOCKED) ;
    return -1;
  }

  /* shift data to the left */
  if (ioc->start) {
    memmove (ioc->data, ioc->data + ioc->start, ioc->fill) ;
    ioc->start = 0 ;
  }

  /* adjust */
  ioc->dim    = (new_size << 1);
  desc->cache = XREALLOC (ioc, sizeof (cipher_iocache) - 1 + ioc->dim) ;

  POINT_OF_RANDOM_VAR (desc->cache) ;

  return new_size ;
}


/* ------------------------------------------------------------------------- *
 *                      private cbc functions                                *
 * ------------------------------------------------------------------------- */

static void
cbc_encrypt
  (cipher_state       *desc,
   unsigned       char *out,
   const unsigned char  *in)
{
  /* cbc-encrypt (E (), block [], in []) -> out []
   *
   *	block [] := E (block [] XOR in [])
   *	out   [] := block []
   */

  /* block [] := block [] XOR in [] */
  p64xassign (desc->block, in, 0) ;
  if (desc->blen > 8)
    p64xassign (desc->block, in, 1) ;
  
  /* out [] := ENCRYPT (block []) */
  XCRYPT (desc->cipher, out, desc->block);

  /* block [] := out [] */
  memcpy (desc->block, out, desc->blen);
}


static void
cbc_decrypt
  (cipher_state       *desc,
   unsigned       char *out,
   const unsigned char  *in)
{
  /* cbc-decrypt (D (), block [], in []) -> out []
   *
   *	out   [] := D (in []) XOR block []
   *	block [] := in []
   */

  unsigned char buf [16] ;
  XCRYPT (desc->cipher, buf, in);

  /* out [] := D (in []) XOR block [] */
  p64xyassign (out, buf, desc->block, 0) ;
  if (desc->blen > 8)
    p64xyassign (out, buf, desc->block, 1) ;
  
  /* block [] := in [] */
  memcpy (desc->block, in, desc->blen);
}


/* cbc decrypt the first 16 bytes without saving the 
   internal state */

static void
cbc_peep_decrypt16
  (const cipher_state  *desc,
   unsigned       char  *out,
   const unsigned char   *in)
{
  /* cbc-peep-decrypt (D (), block [], in []) -> out []
   *
   * if blen == 16:
   *
   *	out   [] := D (in   []) XOR block []
   *
   * if blen == 8:
   *
   *	out   [] := D (in   []) XOR block []
   *	out+8 [] := D (in+8 []) XOR in    []
   */
  
  cipher_desc *hold = duplicate_cipher (desc->cipher) ;
  XCRYPT (hold, out, in);
  
  /* out [] := D (in []) XOR block [] */
  p64xassign (out, desc->block, 0) ;

  if (desc->blen > 8)

    p64xassign (out, desc->block, 1) ;

  else {

    /*     out+8 [] := D (in+8 []) XOR in [] 
     * <=> out+8 [] := D (in+8 [])
     *     out+8 [] := out+8 [] XOR in []
     */
    XCRYPT (hold, out + 8, in + 8);
    p64xassign (out+8, in, 0) ;
  }

  destroy_cipher (hold);
}


/* ------------------------------------------------------------------------- *
 *                  private functions: io function helpers                   *
 * ------------------------------------------------------------------------- */

/* We know, that a cmd is most likely 0, or SEND_CHANNEL_ID. So the following
   macro optimizes the function call determining the argument  length  */

#define get_embedded_cmd_arglen(cmd) \
	((cmd) ? ((cmd)==SEND_CHANNEL_ID ? 2 : _get_emb_cmd_arglen (cmd)) : 0)

static unsigned
_get_emb_cmd_arglen
  (unsigned char cmd)
{
  unsigned len = 0 ;

  if (cmd & SEND_CHANNEL_ID)    len +=  2;
  if (cmd & EXEC_COMMAND)       len += 17;
  if (cmd & CHANGE_BUFFER_SIZE) len +=  4;

  return len ;
}

/* ------------------------------------------------------------------------- *
 *                  private functions: threaded io                           *
 * ------------------------------------------------------------------------- */

static cipher_thread *
duplicate_thread
  (cipher_state *stat)
{
  cipher_thread *new = vmalloc (sizeof (cipher_thread)) ;

  /* copy the current channel state */
  new->state         = *stat ;
  new->state.cipher  = duplicate_cipher (stat->cipher) ;
  new->state.frame   = duplicate_frame  (stat->frame);
  new->state.block   = memcpy (vmalloc (stat->blen), stat->block, stat->blen);

# ifdef USE_ZLIB_COMPRESSION
  /* don't copy commpressor links */
  new->state.compr_desc = 0 ;
  new->state.compr_levl = 0 ;
# endif

  /* are we allowed to clone the call back function, as well ? */
  if (new->state.tcatcher.allow_cloning)
    new->state.tcatcher.allow_cloning = 0 ; /* keep it reset */
  else
    memset (&new->state.tcatcher, 0, sizeof (new->state.tcatcher)) ;

  return new ;
}


static cipher_thread **
oldest_zombie
  (ioCipher     *desc,
   unsigned long keep)
{
  cipher_thread **T = &desc->thread ;
  cipher_thread  *t = *T, **X = 0 ;
  time_t age = 0, now = time (0) ;

  if (t != 0)
    do {
      if (t->state.zombie_t  == 0 || 
	  t->state.thread_id == keep)
	continue ;
      if (age != 0) 
	if (now < t->state.zombie_t) {
	  /* we got a wrap around on the time range
	   *
	   *            wrapped region
	   *          <---------------------------->
	   *            minimal age
	   *          <------------->
	   * |-------|---------------|--------------|
	   *        now           zombie_t
	   */
	  if (now <= age && age <= t->state.zombie_t) 
	    continue ;
	} else
	  /*
	   *    minimal age
	   *  +------------------------------------+
	   *  |                                    |
	   *  +----->                 <------------+
	   *
	   * |-------|---------------|--------------|
	   *      zombie_t          now
	   */
	  if (age <= t->state.zombie_t || now <= age)
	    continue ;
      age = t->state.zombie_t ;
      X   = T ;
    } while (T = &t->next, t = *T, t != 0) ;

  if (X != 0) 
    return X ;

  errno = errnum (CBC_NOSUCH_THREADID);
  return 0 ;
}


static void
zombie_by_thread_pid
  (ioCipher   *desc, 
   unsigned long pid)
{
  cipher_thread *t = desc->thread ;
  time_t now = time (0) ;
  while (t != 0) {
    if (t->state.creator_pid == pid) {
      if (t->state.tcatcher.fn != 0)
	_run_tcatcher (desc, &t->state, 0, 0, 0) ;
      t->state.zombie_t = now ;
    } 
    t = t->next ;
  }
}


static void
zombie_by_thread_id
  (ioCipher *desc, 
   unsigned    id)
{
  cipher_thread *t = desc->thread ;
  time_t now = time (0) ; 
  while (t != 0) {
    if (t->state.thread_id == id) {
      if (t->state.tcatcher.fn != 0)
	_run_tcatcher (desc, &t->state, 0, 0, 0) ;
      t->state.zombie_t = now ;
      return ;
    }
    t = t->next ;
  }
}



static void
rotate_cookie
 (unsigned char *cookie,
  unsigned char    *buf,
  frame_desc      *hash)
{

  /* the last 4 bytes of the cookie [] are shifted left, and replaced by 
     the 4 bytes of buf []. Finally, this new cookie is hashed. */
  
  memcpy (cookie, cookie + 4, 4);
  memcpy (cookie,        buf, 4);
  XCRCFIRST (hash, cookie, 8);
  
  /* store the new cookie in the lookup table */
  memcpy (cookie, XCRCRESULT0 (hash), 8) ;
}

/* ------------------------------------------------------------------------- *
 *                private functions: threaded receiver io                    *
 * ------------------------------------------------------------------------- */

static unsigned
receiver_thread_id_matches
  (cipher_state        *state,
   const unsigned char *inbuf)
{ 
  unsigned char plainbuf [16] ;

  cbc_peep_decrypt16 (state, plainbuf, inbuf) ;

  return  
    (plainbuf [8] & SEND_CHANNEL_ID) &&
    buffer2ushort (plainbuf + 9) == state->thread_id ;
}


static cipher_state *
get_receiver_thread
  (ioCipher *desc,
   char   *cookie)
{
  cipher_thread *t, *u, *v ;
  unsigned matches, master_thread_matches ;

  if (!desc->enable_threads) 
    return &desc->state ;

  /* Search through the linked list of thread records to find matching 
     cookies. Return the corresponding state record if there is exactly 
     one match */
  
  t       = desc->thread ;
  v       = 0 ;		/* predecessor of t, or u */
  u       = 0 ;		/* holds the first match */
  matches = 0 ;		/* counts the number of matches */

  /* start with the basic thread */
  if ((master_thread_matches =
       (memcmp (desc->state.cookie, cookie, 8) == 0)) != 0) {
    /* is it cheaper to decrypt later on ? */
    if (desc->act_threads < CBC_DECRYPT_AT_LIST_SIZE) 
      matches ++ ;
    else
      if (receiver_thread_id_matches (&desc->state, cookie + 8))
	return &desc->state ;
  }

  POINT_OF_RANDOM_VAR (cookie) ;

  /* continue with the list of derived threads */
  if (t != 0) do {
    /* is there a matching cookie ? */
    if (memcmp (t->state.cookie, cookie, 8) == 0) {
      /* is it cheaper to decrypt immediatelely ? */
      if (CBC_DECRYPT_AT_LIST_SIZE < desc->act_threads) {
	if (!receiver_thread_id_matches (&t->state, cookie + 8))
	  continue ;  /* no match---almost never reached, here */
	if (v == 0)
	  return &t->state ;
	/* move the thread t to the beginning of the queue */
	v->next = t->next ;
	t->next = desc->thread ;
	desc->thread = t ;
	return &t->state ;
      }
      /* record the first match */
      if (u == 0) u = t ;
      /* stop searching when we got at least two matches */
      if (matches ++) break ;
    }
    /* record the v as predecessor of t, get next entry */
  } while (v = t, t = t->next, t != 0);
  
  switch (matches) {
  case 0:	/* no match at all */
    return 0 ;
  case 1:	/* exatly one match, return that thread */
    if (master_thread_matches)
      return &desc->state ;
    if (v != 0) {
      /* move the thread u to the beginning of the queue */
      v->next = u->next ;
      u->next = desc->thread ;
      desc->thread = u ;
    }
    return &u->state ;
  }

  /* Now, decode the first part of the next 16 bytes looking
     for a thread-ID */
  if (master_thread_matches) {
    if (receiver_thread_id_matches (&desc->state, cookie + 8))
      return &desc->state ;
    if (matches <= 1)
      return 0;
  }

  /* Look through the other instances assuming u has been set
     to the first matchuing cookie: Continue with the rest of 
     the list of threads while partially decoding contents. */
  while (u != 0) {
    if (memcmp (u->state.cookie, cookie, 8) == 0) {
      if (receiver_thread_id_matches (&u->state, cookie + 8)) {
	if (v == 0)
	  return &u->state ;
	/* move the thread u to the beginning of the queue */
	v->next = u->next ;
	u->next = desc->thread ;
	desc->thread = u ;
	return &u->state ;
      }
    }
    v = u ;
    u = u->next ;
  }

  /* mo match */
  return 0;
}


static int
make_recv_thread
  (ioCipher        *desc, 
   cipher_state    *stat, 
   unsigned short     id, 
   unsigned long     pid, 
   unsigned char *cookie,
   unsigned char    *buf)
{
  cipher_thread *t ;
  cipher_iocache *ioc ;

  POINT_OF_RANDOM_VAR (buf) ;

  while (desc->act_threads >= desc->cache->max_threads) {
    /* check for zombies */
    if (_destroy_thread (desc, stat->thread_id, oldest_zombie) < 0) {
      errno = errnum (CBC_NO_MORE_THREADS) ;
      return -1;
    }
    desc->act_threads -- ;
  }
  
  if (desc->enable_threads == 0) {	/* tag basic thread */
    memset (desc->state.cookie, 0, 8) ;
    rotate_cookie (desc->state.cookie, buf, desc->state.frame);
    RECV_NOTIFY /* for debugging */
      (DUMP_CONTROL, "make_recv_thread", "initializing basic thread", 
       desc, stat, 0) ;
    desc->enable_threads ++ ;
  }

  POINT_OF_RANDOM_VAR (cookie) ;

  /* copy the current channel state, assign thread id */
  t = duplicate_thread (stat) ;

  /* assign cookie and thread id */
  memcpy (t->state.cookie, cookie, 8) ;
  t->state.thread_id   = id ;
  t->state.creator_pid = pid ;

  /* insert in list */
  t->next      = desc->thread ;
  desc->thread = t ;
  desc->act_threads ++ ;

  POINT_OF_RANDOM_STACK (7) ;

  /* handle call back function: move from cache to active thread */
  if (t->state.tcatcher.fn == 0) {
    if (desc->cache->tcatcher.fn != 0) {
      t->state.tcatcher            = desc->cache->tcatcher ;
      t->state.tcatcher.active_cid = desc->cache->cid_counter ;
      memset (&desc->cache->tcatcher, 0, sizeof (desc->cache->tcatcher)) ;

      /* call initial catcher, 1 => first call */
      _run_tcatcher (desc, &t->state, 0, 1, 0) ;
    }
  } else
    /* call initial catcher, 2 => cloning */
    _run_tcatcher (desc, &t->state, 0, 2, 0) ;
  
  RECV_NOTIFY /* for debugging */
    (DUMP_THREAD, "make_recv_thread", "creating a new thread", 
     desc, &t->state, 0) ;

# ifdef	CBC_THREADS_HWMBL
  ioc = desc->cache ;
  /* handle call back function: invoke user thread when leaving */
  if (desc->act_threads + ioc->hwmbl_threads >= ioc->max_threads &&
      ioc->vrfy_trap != 0) {

    unsigned pos ;
    int n, m, dim = ioc->vrfy_bypid ? 4 : 8 ;

    /* Get a random entries: in order to repeat this procedure for
       debugging, we need to pick the elements in some reproducible
       order. */

    /* assemble an array of 4 or 8 increasing random numbers */
    srand (desc->act_threads + ioc->vrfy_seed);   
    ioc->vrfy [0]  = (unsigned short)(65536.0*rand ()/(RAND_MAX+1.0));
    ioc->vrfy [0] %= desc->act_threads ;
    ioc->vrfy_dim  = 1 ;

    do {
      unsigned short new = (unsigned short)(65536.0*rand ()/(RAND_MAX+1.0)) ;
      new %= desc->act_threads ;
      
      /* append if it is the largest number */
      if (new > ioc->vrfy [ioc->vrfy_dim-1]) {
	ioc->vrfy [ioc->vrfy_dim ++] = new ;
	continue ;
      }
	
      /* now, new <= ioc->vrfy [ioc->vrfy_dim-1], find the least 
	 position n with new <= ioc->vrfy [n] */ 
      for (n = 0; ioc->vrfy [n] < new; n ++)
	;

      /* want entries to be mutual exclusive */
      if (ioc->vrfy [n] == new)
	continue ;

      /* insert that value */
      memmove (ioc->vrfy+n+1, ioc->vrfy+n, (7-n) * sizeof (unsigned long));
      ioc->vrfy [n] = new ;
      ioc->vrfy_dim ++ ;
      
    } while (ioc->vrfy_dim < dim && ioc->vrfy_dim < desc->act_threads) ;
    
    /* terminate with the threads list size */
    ioc->vrfy [ioc->vrfy_dim] = desc->act_threads ;

    /* Now, given the threads list, the random array implies a partion
       on the index positions n thread list

          P{n} := {i: vrfy [n] <= i <= vrfy [n+1]}, n < vrfy_dim

       Note, that for any i < P{0}, i does not belong to a partition.
       This reflects the fact, that the first entries in the threads
       list are the ones accessed lately. 

       For each partition, find an unmarked element */
    
    t    = desc->thread ; 
    pos  = 0 ; /* position in the linked thread list */
    n    = 0 ; /* read index in the tid array */
    m    = 0 ; /* write index in the tid array */

    /* advance to the start of the first partition */
    while (pos < ioc->vrfy [0]) {
      if (t->state.mark_checked && ioc->vrfy_mark == 0)
	/* possibly sweep marks */
	t->state.mark_checked = 0;
      t = t->next;
      pos ++ ;
    }

    do {
      /* get the first free entry smaller than ioc->vrfy [n] */
      while (pos < ioc->vrfy [n+1]) {
	if (t->state.mark_checked && ioc->vrfy_mark == 0)
	  /* possibly sweep marks */
	  t->state.mark_checked = 0;
	if (t->state.mark_checked == 0)
	  break ;
	t = t->next;
	pos ++ ;
      }
      
      if (pos < ioc->vrfy [n+1] && t->state.mark_checked == 0) {
	/* replace the entry index/address by the process pid */
	ioc->vrfy [m ++] = ioc->vrfy_bypid 
	  ? t->state.creator_pid 
	  : t->state.thread_id ;
	t->state.mark_checked = ioc->vrfy_mark ;
      }

      /* advance to the next stop */
      while (pos < ioc->vrfy [n+1]) {
	if (t->state.mark_checked && ioc->vrfy_mark == 0)
	  /* possibly sweep marks */
	  t->state.mark_checked = 0;
	t = t->next;
	pos ++ ;
      }
    } while (++ n < ioc->vrfy_dim) ;
    
    /* did we need to sweep the marks ? */
    if (ioc->vrfy_mark == 0 && t != 0)
      while ((t = t->next) != 0)
	/* sweep the rest of that table marks */
	t->state.mark_checked = 0;
   
    ioc->vrfy_dim  = m ; /* size of active data in the table */
    ioc->vrfy [m]  = 0 ; /* set terminator */
    ioc->vrfy_how  = 1 ; /* send this verification request */

    /* using a mark & sweep alg: get next mark if it was not
       too long ago since we did the last sweep */
    if (m < n || ++ ioc->vrfy_mark >= (desc->act_threads >> 4) + 10)
      ioc->vrfy_mark = 0 ;
   
    /* reproducible random chain */
    ioc->vrfy_seed = (unsigned short)(65536.0*rand ()/(RAND_MAX+1.0));
  }
# endif
  
  return 0;
}


/* ------------------------------------------------------------------------- *
 *         private functions: receiver embedded command handling             *
 * ------------------------------------------------------------------------- */

/* We know, that a cmd is most likely 0, or SEND_CHANNEL_ID. So the following
   macro optimizes the function call evaluationg the argument */

#define do_embedded_recv_cmds(desc,stat,cmd,buf,p) \
	(cmd ? ((cmd)==SEND_CHANNEL_ID && stat->thread_id==buffer2ushort (p) \
		  ? (desc->cache->got_embedded ++, 2)			     \
		  : _do_emb_recv_cmds (desc, stat, cmd, buf, p)) : 0)

static int
_do_emb_recv_cmds 
  (ioCipher     *desc, 
   cipher_state *stat,
   unsigned char  cmd,
   unsigned char *buf,
   unsigned char   *p)
{
  unsigned processed = 0, id = -1 ;
  unsigned long pid ;

  if (cmd & SEND_CHANNEL_ID) {

    /* check for valid channel id */
    if ((id = stat->thread_id) != buffer2ushort (p)) {
      errno = errnum (CBC_REC_ILL_THREADID) ;
      RECV_NOTIFY /* for debugging */
	(DUMP_ERROR, "_do_emb_recv_cmds", "got wrong thread id", 
	 desc, 0, buffer2ushort (p)) ;
      return -1 ;
    }
    desc->cache->got_embedded ++ ;
    processed += 2 ;
    p         += 2 ;
  }
     
  POINT_OF_RANDOM_STACK (3) ;
  
  if (cmd & EXEC_COMMAND) {
    
    switch (* p ++) {
    case EXEC_ASSIGN_PID:
      stat->creator_pid = buffer2ulong (p);
      break ;
      
    case EXEC_VERIFY_THREADS_PID:
#     ifdef CBC_THREADS_HWMBL
      desc->cache->vrfy_bypid = 1 ;
      desc->cache->vrfy_how   = 0 ; /* received this verification request */
      desc->cache->vrfy_dim   = 0 ;
      do {
	if ((desc->cache->vrfy 
	     [desc->cache->vrfy_dim] = buffer2ulong (p)) == 0)
	  break ;
      } while (p += 4, ++ desc->cache->vrfy_dim < 4) ;
#     endif
      break ;

    case EXEC_VERIFY_THREADS:
#     ifdef CBC_THREADS_HWMBL
      desc->cache->vrfy_bypid = 0 ;
      desc->cache->vrfy_how   = 0 ; /* received this verification request */
      desc->cache->vrfy_dim   = 0 ;
      do {
	if ((desc->cache->vrfy [desc->cache->vrfy_dim]=buffer2ushort (p)) == 0)
	  break ;
      } while (p += 2, ++ desc->cache->vrfy_dim < 8) ;
#     endif
      break ;

    case EXEC_CREATE_THREAD:
      if (make_recv_thread 
	  (desc, stat, buffer2ushort (p), buffer2ulong (p+10), p + 2, buf) < 0)
	return -1;
      break ;

    case EXEC_PURGE_THREAD_PID:
      zombie_by_thread_pid (desc, buffer2ulong (p));
      break ;
      
    case EXEC_DESTROY_THREAD_PID:
      pid = buffer2ulong (p) ;
      if (id && (desc->public_destroy == 0 || pid)) {
	errno = errnum (CBC_CANT_KILL_NOTOWN) ;
	return -1 ;
      }
      POINT_OF_RANDOM_STACK (5) ;
      if (_destroy_thread (desc, pid, _thread_ptr_by_pid) < 0) {
	RECV_NOTIFY /* for debugging */
	  (DUMP_ERROR, "_do_emb_recv_cmds", "unable to destroy thread id", 
	   desc, 0, buffer2ulong (p)) ;
	return -1;
      } 
      desc->act_threads -- ;
      while (_destroy_thread (desc, pid, _thread_ptr_by_id) >= 0)
	desc->act_threads -- ;
      RECV_NOTIFY /* for debugging */
	(DUMP_THREAD, "_do_emb_recv_cmds", "destroyed threads by pid", 
	 desc, 0, buffer2ushort (p)) ;
      break ;

    case EXEC_PURGE_THREAD:
      zombie_by_thread_id (desc, buffer2ushort (p));
      break ;
      
    case EXEC_DESTROY_THREAD:
      /* Who is allowed to destroy a thead ?. OK, I agree on the 
	 owner of the thread, but what about the rest ? */
      if (desc->public_destroy == 0 && id && id != buffer2ushort (p)) {
	errno = errnum (CBC_CANT_KILL_NOTOWN) ;
	return -1 ;
      }
      if (_destroy_thread (desc, buffer2ushort (p), _thread_ptr_by_id) < 0) {
	RECV_NOTIFY /* for debugging */
	  (DUMP_ERROR, "_do_emb_recv_cmds", "unable to destroy thread by pid", 
	   desc, 0, buffer2ushort (p)) ;
	return -1 ;
      }
      desc->act_threads -- ; 
      RECV_NOTIFY /* for debugging */
	(DUMP_THREAD, "_do_emb_recv_cmds", "destroyed thread id", 
	 desc, 0, buffer2ushort (p)) ;
      break ;

    case EXEC_CHANGE_SESSION_KEY:
      RECV_NOTIFY /* for debugging */
	(DUMP_CONTROL, "_do_emb_recv_cmds", "changeing the session key",
	 desc, stat, 0);
      if (desc->mkey_active) {
	/* hash the session key with the master key */
	XCRCFIRST (desc->state.frame,          p,                  16);
	XCRCNEXT  (desc->state.frame, desc->mkey, sizeof (desc->mkey));
	p = XCRCRESULT0 (desc->state.frame) ;
      }
      if (change_decryption_key (stat->cipher, p) < 0)
	return -1;
      POINT_OF_RANDOM_STACK (4) ;
      desc->want_key_change = 0 ;
      break ;
      
    default:
      errno = errnum (CBC_ILL_SUBCMD) ;
      return -1;
    }
    
    desc->cache->got_embedded ++ ;
    processed += 17 ;
    p         += 16 ;
  }
  
  POINT_OF_RANDOM_STACK (5) ;

  if (cmd & CHANGE_BUFFER_SIZE) {
    unsigned long l = buffer2ulong (p) ;
  
    /* adjust to something sensible */
    if (l < CBC_IO_BUFLEN_MIN) {
      l = CBC_IO_BUFLEN_MIN ;
    } else {
      if (l > CBC_IO_BUFLEN_MAX)
	l = CBC_IO_BUFLEN_MAX ;
    }

    /* limit_maxblock imposes a limit on the buffer size */
    if (desc->cache->limit_maxblock != 0) 
      
      /* block size ranges from limit_maxblock to state.maxblock */
      if (desc->cache->limit_maxblock < desc->maxblock) {
	if (l < desc->cache->limit_maxblock)
	  l = desc->cache->limit_maxblock ;
	else
	  if (desc->maxblock < l)
	    l = desc->maxblock ;
      } else {

	/* block size ranges from state.maxblock to limit_maxblock */
	if (desc->maxblock <= desc->cache->limit_maxblock) {
	  if (l < desc->maxblock)
	    l = desc->maxblock ;
	  else
	    if (desc->cache->limit_maxblock < l)
	      l = desc->cache->limit_maxblock ;
 	}
      }
    
    /* adjust input cache */
    if (l != desc->maxblock)
      resize_ioCache (desc, desc->maxblock = l) ;

    desc->cache->got_embedded ++ ;
    processed += 4 ;
    /* p      += 4 ; */
  }

  return processed ;
}

/* ------------------------------------------------------------------------- *
 *                  private functions: receiver io functions                 *
 * ------------------------------------------------------------------------- */

static int
recfrom_ioCipher_block
  (ioCipher     *desc,
   unsigned char   *p,
   unsigned       cnt,
   int          flags)
{
  int n, len, input_len, is_zombie ;
  unsigned char crc [4], cmd ;
  unsigned processed, padding, mask ;
  cipher_state *thread;

    /* allocate the data bufers */
  unsigned char *inbuf    = ALLOCA (desc->maxblock + PROTO_OVERHEAD) ;
  unsigned char *plainbuf = ALLOCA (desc->maxblock + PROTO_OVERHEAD 
                                                   + COMPR_OVERHEAD) ;

  /* check, whether we run the protocol in threaded mode in which
     case we need lo load the 8 byte cookie followed by the first
     16 byte of the data block */
  unsigned start = desc->enable_threads ? 8 : 0 ;

  /* read the 16 bytes from the data block, possibly there is a leading
     8 bytes thread cookie */
  unsigned arglen = 16 + start ;

  /* use this buffer to read the block header */
  unsigned char *data_start = start ? ALLOCA (arglen) : inbuf ;

  /* get the first packet which has the minimum size 16 bytes */
  for (processed = 0; processed < arglen; processed += n, desc->total += n)
    if ((n = (*desc->iofn) 
	 (desc->fd, data_start + processed, arglen - processed, flags)) <= 0) {
      /* take care of interrupts */
      if (n < 0 && errno == EINTR) {
	RECV_NOTIFY (DUMP_ERROR, "recfrom_ioCipher_block", 
		     "signal while reading first packet", desc, 0, processed);
	n = 0 ;
	continue ;
      }
      /* get ready to leave, otherwise */
      if (start) 
	DEALLOCA (data_start);
      DEALLOCA (plainbuf);
      DEALLOCA    (inbuf);
      if (n == 0 && processed == 0) { /* still the first try ? */
	desc->cache->eof = 1 ;       /* mark end of file */
	return 0 ;
      } 
      errno = errnum (CBC_RECV_NULL_BLOCK) ;
      return -1 ;
    }

  /* get the current thread */
  if (start) {
    
    if ((thread = get_receiver_thread (desc, data_start)) == 0) {
      DEALLOCA (data_start);
      errno = errnum (CBC_NOSUCH_COOKIE) ;
      goto error_return ;
    }

    RECV_NOTIFY /* for debugging */
      (DUMP_COOKIE, "recfrom_ioCipher_block", "got threaded block", 
       desc, thread, 0);

    /* place the data header without the cookie in the input buffer */
    memcpy (inbuf, data_start + start, 16);
    DEALLOCA (data_start);	/* done with this buffer */

  } else {
    /* non-threaded mode */
    thread = & desc->state ;
  }

  /* Calculate the mask corresponding to the block size 8, or 16. So
     x % blen is equivalent to x & mask. */
  mask = (thread->blen - 1) ;
  
  /* As we are on a packet boundary, we can decode the first part. Note
     that the cbc block size is either 16, or 8. In the latter case
     we need to decrypt twice */
  cbc_decrypt (thread, plainbuf, inbuf) ;
  if (thread->blen == 8) 
    cbc_decrypt (thread, plainbuf + 8, inbuf + 8) ;

  /* read the total input block size */
  len = bufferXbuffer2ulong (plainbuf, plainbuf + 4) ;

  /* get the argument size for embedded commands */
  cmd = (plainbuf [8] & EMBD_COMMAND_MASK) ;

  if (len < 16) {
    /* total input block completely rubbish */
    errno = errnum (CBC_RECV_LEN_2SMALL) ;
    goto error_return ;
  }
  if (len > (int)desc->maxblock + PROTO_OVERHEAD) {
    /* total input block size too large */
    errno = errnum (CBC_RECV_LEN_2LARGE) ;
    goto error_return ;
  }
  if ((len & mask) && !(cmd & COMPRESSED_DATA)) {
    /* len not on block boundary */
    errno = errnum (CBC_RECV_LEN_NOBNDRY) ;
    goto error_return ;
  }

  arglen = get_embedded_cmd_arglen (cmd) ;
  
  /* read the padding length */
  padding = (plainbuf [8] & mask) ;

  /* read the rest of data into the temporary input buffer, in case of
     compressed data we read up until the next block boundary */
  for (processed = 16, input_len = ((len + mask) & ~mask);
       (int)processed < input_len; processed += n, desc->total += n)
    if ((n = (*desc->iofn) 
	 (desc->fd, inbuf + processed, input_len - processed, flags)) <= 0) {
#     if 0 
      /* FIXME:  what do we want, really ? Interrupts and timeout can be
	 processed at a higher level, anyway. So there is no need to detect
	 a timeout signal, here */
      if (n < 0 && errno == EINTR) {
	RECV_NOTIFY /* for debugging */
	  (DUMP_ERROR, "recfrom_ioCipher_block", 
	   "got signal while reading data packets", desc, thread, 0);
	n = 0;
	continue ;
      }
#     endif
      if (n < 0) 
	goto error_return ;
      /* n = 0: read only some bytes (less than expected) */
      desc->cache->eof = 1 ;  
      errno = errnum (CBC_RECV_UNEXP_EOF) ;
      goto error_return ;
    }

  /* decode that input to the plain text buffer */
  for (input_len = 16; input_len < (int)processed; input_len += thread->blen)
    cbc_decrypt (thread, plainbuf + input_len, inbuf + input_len) ;

  /* optionally decompress the buffer, all but the first 9 bytes */
# ifdef USE_ZLIB_COMPRESSION
  if (cmd & COMPRESSED_DATA) {
    int offset = 9 + arglen ;

    if (thread->compr_desc == 0 && 
	(thread->compr_desc = _recv_inflateInit ()) == 0)
      goto error_return ;
    n = _recv_inflate 
      (thread->compr_desc, 
       inbuf, desc->maxblock+COMPR_OVERHEAD, plainbuf+offset, len-offset);
    if (n < 0 && errno)
      goto error_return ;
    
    /* readjust the data buffer */
    len += n ;
    memcpy (plainbuf + offset, inbuf, len - offset);

    if (len & mask) {
      /* len not on block boundary */
      errno = errnum (CBC_RECV_LEN_NOBNDRY) ;
      goto error_return ;
    }

    /* adjust datagram size field, so the crc will work */
    ulongXbuffer2buffer (plainbuf + 4, len, plainbuf);
    plainbuf [8] &= ~COMPRESSED_DATA ;

  } else /* end up compression mode ? */
    
    if (thread->compr_desc != 0) {
    
      deflateEnd (thread->compr_desc) ;
      xfree (thread->compr_desc) ;
      thread->compr_desc = 0 ;
    }
  
# else
  if (cmd & COMPRESSED_DATA) {
    errno = errnum (CBC_INFLATE_UNSUPP) ;
    goto error_return ;
  }
# endif /* USE_ZLIB_COMPRESSION */
  
  /* read crc from the input block */
  memcpy (crc, plainbuf + len - 4, 4);

  /* store crc from previos block and recalculate crc */ 
  memcpy (plainbuf + len - 4, thread->chain, 4) ;

  /* compare */
  XCRCFIRST (thread->frame, plainbuf, len);
  if (memcmp (crc, XCRCRESULT (thread->frame), 4) != 0) {
    /* read invalid crc */
    errno = errnum (CBC_RECV_CRCERR) ;
    goto error_return ;
  }
  memcpy (thread->chain, crc, 4) ;

  if (start) {

    /* we need to update the cookie in the lookup table */
    rotate_cookie (thread->cookie, plainbuf, thread->frame) ;

    RECV_NOTIFY /* for debugging */
      (DUMP_COOKIE, "recfrom_ioCipher_block", "updating to new cookie", 
       desc, thread, 0);

    if ((cmd & SEND_CHANNEL_ID) == 0) {
      /* protocol error, id is required in threaded mode */
      errno = errnum (CBC_REC_NO_THREADID) ;
      goto error_return ;
    }
  }
  
  /* start with the embedded commands section */
  data_start = plainbuf + 9 ;

  /* check before the thread is possibly killed by an embedded cmd */
  is_zombie = (start != 0 && thread->zombie_t != 0) ;

  /* read the embedded command arguments and process it */
  if ((n = do_embedded_recv_cmds
       (desc, thread, cmd, plainbuf, data_start)) < 0)
    goto error_return ;

  /* get the start of data payload */
  data_start += n + padding ;

  RECV_TEXT /* for debugging */
    ("recfrom_ioCipher_block", data_start, len-13-padding-arglen);
  
  if (is_zombie) {
    /* the io thread is dead, so drop the data */
    cmd = 0 ;
    n = cnt = 0 ;
    goto ready_for_return;
  }

  /* calculate the size of the data payload */
  if ((n = len - 13 - padding - arglen) == 0) {
    cnt = 0 ;
    goto ready_for_return;
  }

  /* just a shortcut - not really necessary */
  if (desc->cache->fill == 0 && (int)cnt >= n){
    memcpy (p, data_start, cnt = n) ;
    goto ready_for_return;
  }
    
  /* append data block to io buffer */
  append_to_io_cache (desc->cache, data_start, n); 

  /* move input from the cache to the request buffer */
  n = extract_from_io_cache (desc->cache, p, cnt) ;

 ready_for_return:

  /* if we expect a key change, leave the data in the buffer
     and return an error */
  if (desc->want_key_change) {
    errno = errnum (CBC_KEYCHANGE_EXP) ;
    goto error_return ;
  }

  DEALLOCA (plainbuf);
  DEALLOCA    (inbuf);

  if (start != 0 &&			/* if send on a thread */
      (cnt != 0 || cmd == 0) &&		/* and not an admin packet */
      thread->tcatcher.fn != 0 &&	/* and a catcher fn available */
      (n = _run_tcatcher (desc, thread, p, cnt, 1)) < 0)
    n = -1 ;
  
  return n;
  /*@NOTREACHED@*/
  
 error_return:
  DEALLOCA (plainbuf);
  DEALLOCA    (inbuf);
  RECV_ERRNO /* for debugging */
    ("recfrom_ioCipher_block", errno);
  return -1 ;
}


static int
recfrom_ioCipher
  (void      *c,
   char    *buf,
   unsigned cnt,
   int    flags)
{
# define desc ((ioCipher *)c)

  cipher_iocache *ioc = desc->cache ;
  int n ;

  /* check, whether there is some old stuff, left over */
  if (ioc->fill)
    return extract_from_io_cache (ioc, buf, cnt) ;
  
  /* check whether we reached end-of-file, already and
     check, whether we are invoked by a call back function */
  if (is_eof_recv (desc) || is_recursive_recv (desc))
    return 0 ;

  for (;;) {
    
    /* detect embedded commands */
    ioc->got_embedded = 0 ;

    /* read next data packet */
    n = recfrom_ioCipher_block (desc, buf, cnt, flags) ;

    /* early check for threads table overflow */
#   ifdef CBC_THREADS_HWMBL
    /* the cache buffer may have been resized, so reload it */
    if ((ioc = desc->cache)->vrfy_dim && ioc->vrfy_trap != 0)
      (*ioc->vrfy_trap) (desc->fd, ioc->vrfy, ioc->vrfy_bypid, ioc->vrfy_how);
    ioc->vrfy_dim = 0 ;
#   endif

    /* continue on no data but embedded commands (unless disabled) */
    if (n || ioc->stop_on_empty || !ioc->got_embedded)
      break ;
  }

  POINT_OF_RANDOM_STACK (7) ;

  if (n > 0)
    desc->payload += n ; /* accounting */

  return n ;
  
# undef desc /* from void* -> ioCipher* type cast */
}


/* ------------------------------------------------------------------------- *
 *          private functions: sender embedded command handling              *
 * ------------------------------------------------------------------------- */


static unsigned
store_embedded_commands 
  (ioCipher    *desc,
   unsigned char cmd,
   unsigned char  *p)
{
  unsigned processed = 0;

  if (cmd & SEND_CHANNEL_ID) {
    ushort2buffer (p, desc->sender->active_thread) ;
    SEND_NOTIFY /* for debugging */
      (DUMP_CONTROL, "store_embedded_commands", 
       "sending threaded", desc, 0, buffer2ushort (p));
    p         += 2 ;
    processed += 2 ;
  }

  POINT_OF_RANDOM_VAR (p) ;

  if (cmd & EXEC_COMMAND) {
    * p ++ = desc->sender->exe ;
    memcpy (p, desc->sender->buf, 16);
    p         += 16 ;
    processed += 17 ;
  }

  if (cmd & CHANGE_BUFFER_SIZE) {
    ulong2buffer (p, desc->sender->maxblock) ;
    processed += 4 ;
    /* p      += 4 ; */
  }

  return processed ;
}


static void 
post_process_sender_cmds 
  (ioCipher       *desc,
   cipher_state *thread,
   unsigned char    cmd,
   unsigned char   *buf)
{
  /* post process embedded command args */
  char *p;

  POINT_OF_RANDOM_VAR (buf) ;

  if (cmd & EXEC_COMMAND) {

    switch (desc->sender->exe) {
    case EXEC_CHANGE_SESSION_KEY:
      SEND_NOTIFY /* for debugging */
	(DUMP_KEYS, "post_process_sender_cmds", "changeing session keys", 
	 desc, thread, 0);
      p = desc->sender->buf ;
      if (desc->mkey_active) {
	/* hash the session key with the master key */
	XCRCFIRST (desc->state.frame,          p,                  16);
	XCRCNEXT  (desc->state.frame, desc->mkey, sizeof (desc->mkey));
	p = XCRCRESULT0 (desc->state.frame) ;
      }
      change_encryption_key (thread->cipher, p) ;
      break ;

    case EXEC_CREATE_THREAD:
      /* do we need to switch from the non-threaded to the threaded mode ? */
      if (! desc->enable_threads) {
	memset (desc->state.cookie, 0, 8) ;
	rotate_cookie (desc->state.cookie, buf, desc->state.frame);
	desc->enable_threads ++ ;
	SEND_NOTIFY /* for debugging */
	  (DUMP_THREAD, "post_process_sender_cmds", 
	   "creating basic thread", desc, thread, 0);
      }
    }
  }

  POINT_OF_RANDOM_STACK (11) ;

  if (cmd & CHANGE_BUFFER_SIZE) {
    desc->maxblock = desc->sender->maxblock ;
    desc->sender->maxblock = 0 ;
  }

  desc->sender->bitlist = 0 ;
}


/* ------------------------------------------------------------------------- *
 *                   private functions: sender io functions                  *
 * ------------------------------------------------------------------------- */

static int
sendby_ioCipher_block
  (ioCipher       *desc,
   cipher_state *thread,
   const char        *p,
   unsigned         cnt,
   int            flags)
{
  int n, processed ;
  char *data_start ;

  /* check whether we need to send a cookie header */
  unsigned start = desc->enable_threads ? 8 : 0 ;

  /* get embedded command bits
     (a threaded sender always sends the channel id) */
  unsigned char cmd = ((desc->sender->bitlist & EMBD_COMMAND_MASK) |
		       (start ? SEND_CHANNEL_ID : 0)) ;

  /* allocate the io buffer */
  unsigned char *plainbuf = ALLOCA (desc->maxblock + PROTO_OVERHEAD) ;
  unsigned char  *sendbuf = ALLOCA (desc->maxblock + PROTO_OVERHEAD + start) ;

  /* get the argument size for embedded commands */
  unsigned arglen = get_embedded_cmd_arglen (cmd) ;
  
  /* Calculate the mask corresponding to the block size 8, or 16. So
     x % blen is equivalent to x & mask. */
  unsigned mask = thread->blen - 1 ;

  /* Calculate the padding length so that the totel block length is a
     multiple of blen, i.e. find x so that 13 + (cnt+arglen) + x == 0 
     (mod blen), so x == - 13 - (cnt+arglen) (mod blen).  Using the fact,
     that blen is 8, or 16 we gain x == 19 - (cnt+arglen) (mod blen) */
  unsigned padding = (19 - cnt - arglen) & mask ;

  /* Calculate the total block length rounded up to the next multiple
     of blen (the value of padding has been figured out, already.) */
  int len = 13 + padding + arglen + cnt ;

  POINT_OF_RANDOM_VAR (data_start) ;

  /* there first 4 bytes are random followed by the xor-part of the
     4 byte length header */
  fast_random_bytes (plainbuf,  4) ;
  ulongXbuffer2buffer (plainbuf + 4, len, plainbuf) ;
  
  /* the least significant four bits carry the padding length */
  plainbuf [8]  = padding ;

  /* store the most significant 4 bits for embedded commands */
  plainbuf [8] |= cmd ;

  /* add the embedded command arguments */
  data_start = plainbuf + 9 ;
  if (cmd)
    data_start += store_embedded_commands (desc, cmd, data_start) ;
  
  /* add the padding data, already initialized */
  if (padding) {
    fast_random_bytes (data_start, padding) ;
    data_start += padding ;
  }
  
  /* add the argument text */
  if (cnt)
    memcpy (data_start, p, cnt);

  POINT_OF_RANDOM_STACK (9) ;

  /* add (temporary) CRC from last block */
  memcpy (plainbuf + len - 4, thread->chain, 4) ;

  /* Calculate the crc trailer for this block while saving the new crc */
  XCRCFIRST (thread->frame, plainbuf, len);
  memcpy (thread->chain, XCRCRESULT (thread->frame), 4) ;

  /* overwrite serial count by crc trailor */
  memcpy (plainbuf + len - 4, thread->chain, 4) ;

  SEND_TEXT ("sendby_ioCipher_block", data_start, cnt);

# ifdef USE_ZLIB_COMPRESSION
  /* optionally compress the buffer, all but the first 16 bytes */
  if (thread->compr_desc != 0) {
    int offset = 9 + arglen ;

    n = _send_deflate /* note that n might come out negative, as well */
      (thread->compr_desc,
       sendbuf, desc->maxblock, plainbuf+offset, len-offset) ;
    
    if (n < 0 && errno) {
      cnt = -1 ;
      goto end_return ;
    }
    len -= n ;

    /* readjust the data buffer */
    memcpy (plainbuf + offset, sendbuf, len - offset);
    ulongXbuffer2buffer (plainbuf + 4, len, plainbuf);
    plainbuf [8] |= COMPRESSED_DATA ;

    /* pad to the next block boundary */
    if ((padding = ((thread->blen - len) & mask)) != 0) {
      fast_random_bytes (plainbuf + len, padding) ;
      len += padding ;
    }
  }
# endif /* USE_ZLIB_COMPRESSION */

  /* check whether we need to send a cookie header */
  if (start) {

    SEND_NOTIFY /* for debugging */
      (DUMP_COOKIE, "sendby_ioCipher_block", "sending threaded", 
       desc, thread, 0);

    /* store the current cookie in the send buffer */
    memcpy (sendbuf, thread->cookie, 8) ;
    
    /* get some new values for the cookie and hash that */
    rotate_cookie (thread->cookie, plainbuf, thread->frame) ;

    SEND_NOTIFY /* for debugging */
      (DUMP_COOKIE, "sendby_ioCipher_block", "generated new cookie", 
       desc, thread, 0);
  }
  
  POINT_OF_RANDOM_VAR (sendbuf) ;

  /* encrypt the whole lot blockwise, place it after the thread cookie */
  for (processed = 0; processed < len; processed += thread->blen)
    cbc_encrypt (thread, sendbuf + start + processed, plainbuf + processed) ;

  /* do some post processing and clean up */
  if (cmd)
    post_process_sender_cmds (desc, thread, cmd, plainbuf) ;

  POINT_OF_RANDOM_VAR (plainbuf) ;

  /* send that stuff including the thread cookie */
  for (processed = 0, len += start; len > 0; processed += n, len -= n) 
    if ((n = (*desc->iofn) (desc->fd, sendbuf + processed, len, flags)) <= 0) {
      if (n < 0 && errno == EINTR) {
	RECV_NOTIFY /* for debugging */
	  (DUMP_ERROR, "recfrom_ioCipher_block", 
	   "got signal while sending data packets", desc, thread, 0);
	n = 0;
	continue ;
      }
      DEALLOCA (plainbuf);
      DEALLOCA  (sendbuf);
      return n;
    } else
      desc->total += n ; /* accounting */

 end_return:
  POINT_OF_RANDOM_STACK (3) ;
  SEND_DRAIN_DELAY (); /* for debugging */
  DEALLOCA (plainbuf);
  DEALLOCA  (sendbuf);
  return cnt ;
}


static int
sendby_ioCipher
  (void      *c,
   char    *msg,
   unsigned len,
   int    flags)
{
  ioCipher *desc = c;
  int n, bytes = 0 ;

  /* get the current thread */
  cipher_state * c_stat = _get_current_sender_thread (desc) ;

  if (c_stat == 0) {
    errno = errnum (CBC_NOSUCH_THREADID) ;
    return -1 ;
  }

  POINT_OF_RANDOM_STACK (9) ;

  /* test, whether we need to change the session key while we can send an
     embedded command (if the automatic change feature is enabled, at all) */
  if ((desc->sender->bitlist & EXEC_COMMAND) == 0 && c_stat->key_sched) {
    
    unsigned long now = time (0) ;
    unsigned next ;

    /* check for a time wrap, all we do here is forcing a key change */
    if (desc->sender->time_adjust > now) {
      cipher_thread *t = desc->thread;
      desc->sender->time_adjust = 
	desc->state.next_change = now ;
      while (t) {
	t->state.next_change = now ;
	t = t->next ;
      }
      POINT_OF_RANDOM_VAR (t) ;
    }

    if (now >= c_stat->next_change) {

      SEND_NOTIFY /* for debugging */
	(DUMP_KEYS, "sendby_ioCipher", "forcing a key change", desc, 0, 0);
      
      /* generate random key, make embedded command */
      prime_random_bytes (desc->sender->buf, desc->state.cipher->keylen) ;
      desc->sender->exe      = EXEC_CHANGE_SESSION_KEY ;
      desc->sender->bitlist |= EXEC_COMMAND ;

      POINT_OF_RANDOM_STACK (5) ;

      /* set time for the next key change */
      fast_random_bytes ((void *)&next, sizeof (next)) ;
      c_stat->next_change = now + (next % c_stat->key_sched);
      if (c_stat->next_change < desc->sender->time_adjust)
	c_stat->next_change = desc->sender->time_adjust ;
    }
  }
    
  /* need to fragment ? */
  while (len > desc->maxblock) {
    if ((n = sendby_ioCipher_block 
	 (desc, c_stat, msg, desc->maxblock, flags)) < 0)
      return n ;
    bytes         += n ;
    desc->payload += n ; /* accounting */
    len -= desc->maxblock ;
  }

  POINT_OF_RANDOM_VAR (msg) ;
  
  if ((n = sendby_ioCipher_block (desc, c_stat, msg, len, flags)) < 0)
    return n ;
  desc->payload += n ; /* accounting */

  POINT_OF_RANDOM_VAR (c_stat) ;

  return bytes + n ;
}

/* ------------------------------------------------------------------------- *
 *                   private functions: io layer management                  *
 * ------------------------------------------------------------------------- */
 
static int
cbc_initialize_any
  (ioCipher     *layer,
   void            *fd,
   int (*io) (void *, void *, unsigned, int),
   cipher_desc *cipher,
   frame_desc   *frame,
   char      chain [4])
{
  if (!supported_block_length (cipher->blocklen)) {
    errno = errnum (CBC_UNSUPPORTED_BLEN) ;
    return -1;
  }

  POINT_OF_RANDOM_STACK (7) ;

  if (cipher->keylen > 16) {
    errno = errnum (CBC_UNSUPPORTED_KLEN) ;
    return -1;
  }

  if ((layer->iofn = io) == 0) {
    errno = errnum (CBC_NEED_IOFN) ;
    return -1;
  }

  /* install cbc function initialized cbc buffer */
  layer->state.cipher = cipher ;
  layer->state.blen   = cipher->blocklen ;
  layer->state.block  = vmalloc (layer->state.blen) ;

  POINT_OF_RANDOM_VAR (fd) ;
  
  /* install frame */
  layer->state.frame = frame ;
  memcpy (layer->state.chain, chain, 4) ;
  
  /* set maximal block length */
  layer->maxblock = CBC_IO_BUFLEN ;

  /* save current file handle */
  layer->fd = fd ;

  POINT_OF_RANDOM_STACK (2) ;

  return 0;
}


static int
cbc_initialize_receiver
  (void       *context,
   void            *fd,
   int (*io) (void*, void*, unsigned, int),
   cipher_desc *cipher,
   frame_desc   *frame,
   char      chain [4])
{
  ioCipher *layer = context;

  POINT_OF_RANDOM_STACK (1) ;

  if (cbc_initialize_any (layer, fd, io, cipher, frame, chain) < 0)
    return -1;

  /* is receiver, install input cache */
  layer->cache = pmalloc (sizeof (cipher_iocache) + 2*CBC_IO_BUFLEN - 1);
  layer->cache->dim = 2*CBC_IO_BUFLEN ;

  POINT_OF_RANDOM_STACK (3) ;

  /* limit the maximal number of threads (apart from the main stream) */
  layer->cache->max_threads   = CBC_THREADS_MAX ;
  layer->cache->hwmbl_threads = CBC_THREADS_HWMBL; /* h' water mark belw max */

  return 0 ;
}

static int
cbc_initialize_sender
  (void       *context,
   void            *fd,
   int (*io) (void*, void*, unsigned, int),
   cipher_desc *cipher,
   frame_desc   *frame,
   char      chain [4])
{
  ioCipher *layer = context;

  POINT_OF_RANDOM_STACK (3) ;

  if (cbc_initialize_any (layer, fd, io, cipher, frame, chain) < 0)
    return -1;

  /* allocate the command buffer */
  layer->sender = pmalloc (sizeof (cipher_command)) ;

  /* store the current time, needed to face the 1/19/2038 problem */
  layer->sender->time_adjust = time (0) ;
  layer->state.next_change   = layer->sender->time_adjust + CBC_KEYTTL_SECS ;
  layer->state.key_sched     = CBC_KEYTTL_SECS ;

  POINT_OF_RANDOM_STACK (5) ;
  
  return 0 ;
}

/* ------------------------------------------------------------------------- *
 *                 private functions: io layer management                    *
 * ------------------------------------------------------------------------- */

static void
destroy_all_notifying_threads
  (void *c)
{
  long pid = 0;
  POINT_OF_RANDOM_STACK (5) ;
  if (((ioCipher*)c)->enable_threads)
    _destroy_thread_any ((ioCipher*)c, &pid, -1, 1) ;
  destroy_ioCipher_links ((ioCipher*)c) ;
  POINT_OF_RANDOM_STACK (3) ;
}

/* ------------------------------------------------------------------------- *
 *                global functions: send embedded commands                   *
 * ------------------------------------------------------------------------- */

int
_send_exec_command
  (ioCipher *desc,
   int        cmd,
   char   *argbuf,
   unsigned   len)
{
  cipher_state *stat ;

  /* get the current protocol thread */
  if ((stat = _get_current_sender_thread (desc)) == 0) { 
    /* should not happen */
    errno = errnum (CBC_NOSUCH_THREADID) ;
    return -1;
  }

  desc->sender->bitlist |= EXEC_COMMAND ;
  desc->sender->exe      = cmd ;
  
  if (len) {
    if (len > 16)
      len = 16 ;
    /* fill argument buffer */
    memcpy (desc->sender->buf, argbuf, len) ;
  }
  
  if (len < 16) { /* padding */
    /* memset (desc->sender->buf + len, 0, 16 - len) ; */
    fast_random_bytes (desc->sender->buf + len,  16 - len) ;
  }
  if (sendby_ioCipher_block (desc, stat, 0, 0, 0) < 0) {
    SEND_NOTIFY /* for debugging */
      (DUMP_THREAD, "send_exec_command", "Error: cannot send to receiver", 
       desc, 0, 0);
    return -1 ;
  }

  return 0;
}


int
_send_exec_short_command
  (ioCipher   *desc,
   int          cmd,
   unsigned short i)
{
  char xbuf [2] ;
  ushort2buffer (xbuf, i) ;
  return _send_exec_command (desc, cmd, xbuf, 2) ;
}


int
_send_exec_long_command
  (ioCipher   *desc,
   int          cmd,
   unsigned long  l)
{
  char xbuf [4] ;
  ulong2buffer (xbuf, l) ;
  return _send_exec_command (desc, cmd, xbuf, 4) ;
}

/* ------------------------------------------------------------------------- *
 *                  global functions: threaded io                           *
 * ------------------------------------------------------------------------- */

cipher_thread *
_duplicate_thread
  (cipher_state *stat)
{
  cipher_thread *new = vmalloc (sizeof (cipher_thread)) ;

  /* copy the current channel state */
  new->state         = *stat ;
  new->state.cipher  = duplicate_cipher (stat->cipher) ;
  new->state.frame   = duplicate_frame  (stat->frame);
  new->state.block   = memcpy (vmalloc (stat->blen), stat->block, stat->blen);

# ifdef USE_ZLIB_COMPRESSION
  /* don't copy commpressor links */
  new->state.compr_desc = 0 ;
  new->state.compr_levl = 0 ;
# endif

  /* are we allowed to clone the call back function, as well ? */
  if (new->state.tcatcher.allow_cloning)
    new->state.tcatcher.allow_cloning = 0 ; /* keep it reset */
  else
    memset (&new->state.tcatcher, 0, sizeof (new->state.tcatcher)) ;

  return new ;
}

cipher_thread **
_thread_ptr_by_id
  (ioCipher   *desc,
   unsigned long id)
{
  cipher_thread *t, **T = &desc->thread ;

  if (id > 0) {

    /* find that thread id */
    while ((t = *T, t != 0) && t->state.thread_id != id)
      T = & t->next ;

    if (t != 0) 
      return T ;
  }

  errno = errnum (CBC_NOSUCH_THREADID);
  return 0 ;
}

cipher_thread **
_thread_ptr_by_pid
  (ioCipher    *desc,
   unsigned long pid)
{
  cipher_thread *t, **T = &desc->thread ;

  if (pid) {
    /* find that thread id */
    while ((t = *T, t != 0) && t->state.creator_pid != pid)
      T = & t->next ;
  } else
    /* get the first one, if pid == 0 */
    t = *T ;

  if (t != 0) 
    return T ;

  errno = errnum (CBC_NOSUCH_THREADID);
  return 0 ;
}

cipher_thread **
_thread_ptr_by_tid
  (ioCipher    *desc,
   unsigned long tid)
{
  cipher_thread *t, **T = &desc->thread ;

  if (tid) {
    /* find that thread catcher id */
    while ((t = *T, t != 0) && t->state.tcatcher.active_cid != tid)
      T = & t->next ;
  } else
    /* get the first one, if tid == 0 */
    t = *T ;

  if (t != 0) 
    return T ;

  errno = errnum (CBC_NOSUCH_THREADID);
  return 0 ;
}

cipher_thread *
_unlink_thread
  (ioCipher   *desc,
   unsigned long id,
   cipher_thread **(*find) (ioCipher *,unsigned long))
{
  cipher_thread *t, **T = (*find) (desc, id) ;

  if (T == 0) 
    return 0;

  /* unlink */
  t = *T ;
  *T = t->next ;
  return t;
}

int
_destroy_thread
  (ioCipher   *desc, 
   unsigned long id,
   cipher_thread **(*find) (ioCipher *,unsigned long))
{
  cipher_thread *t = _unlink_thread (desc, id, find);

  if (t == 0) {
    errno = errnum (CBC_ILL_THREADID) ;
    return -1 ;
  }

  destroy_ioState_links (desc, &t->state) ;
  xfree (t) ;

  /* set id to something that exists */
  if (desc->sender != 0 &&
      desc->sender->active_thread == id)
    desc->sender->active_thread = 0 ;

  return 0 ;
}

/* ------------------------------------------------------------------------- *
 *                global functions: threaded sender io                       *
 * ------------------------------------------------------------------------- */

cipher_state * 
_get_current_sender_thread
  (ioCipher *desc)
{
  unsigned id = desc->sender->active_thread ;
  cipher_thread *t ;
  
  if (id == 0) 
    return &desc->state ;

  POINT_OF_RANDOM_VAR (t);

  t = desc->thread ;
  while (t != 0) {
    if (t->state.thread_id == id)
      return &t->state ;
    t = t->next ;
  }

  POINT_OF_RANDOM_STACK (7) ;
  
  return 0 ;
}

/* ------------------------------------------------------------------------- *
 *                      global functions: misc                               *
 * ------------------------------------------------------------------------- */

int
_run_tcatcher 
  (ioCipher*      desc,
   cipher_state* state,
   char*           buf,
   unsigned        len,
   int       recursion)
{
  int (*fn)(char*,unsigned,void*,void**) = 0;
  int n, save ;

  if (state == 0 ||
      state->tcatcher.fn == 0 || state->zombie_t != 0 ||
      desc->cache == 0)
    return -1;

  /* save the current id - works recursively */
  save = desc->cache->tcatcher.active_cid ;

  /* mark the catcher running */
  desc->cache->tcatcher.active_cid = state->tcatcher.active_cid ;

  /* no recursion (e.g. in the final clean up state) */
  fn = state->tcatcher.fn ;
  if (recursion == 0)
    state->tcatcher.fn = 0 ;
  
  n = (*fn) (buf, len, desc->fd, &state->tcatcher.environment) ;

  /* restore */
  if (recursion == 0)
    state->tcatcher.fn = fn ;
  
  /* unmark the catcher */
  desc->cache->tcatcher.active_cid = save ;

  return n;
}

/* ------------------------------------------------------------------------- *
 *                 public functions: call back functions                     *
 * ------------------------------------------------------------------------- */


char *
cbc_get_info 
  (unsigned is_sender,
   size_t *contextsize,
   int (**cbc_open) (void*, void*, int(*)(void*,void*,unsigned,int),
		     cipher_desc*, frame_desc*, char[4]),
   int (**cbc_rdwr) (void *, char *, unsigned, int),
   int (**cbc_ioctl)(void *, io_ctrl_request, void*),
   void(**cbc_close)(void *))
{
  if (is_sender) {
    *cbc_open  = cbc_initialize_sender ;
    *cbc_rdwr  = sendby_ioCipher  ;
    *cbc_close = destroy_all_notifying_threads ;
  } else {
    *cbc_open  = cbc_initialize_receiver ;
    *cbc_rdwr  = recfrom_ioCipher  ;
    *cbc_close = destroy_ioCipher_links ;
  }

  *contextsize = sizeof (ioCipher) ;
  *cbc_ioctl   = _io_control ;

  return is_sender ? "cbc-send" : "cbc-recv" ;
}