Hex Artifact Content

Artifact eab9c4dc8fa6554bca8e1f1ad8e8e443ddf7f75b:


0000: 2f 2a 0a 20 2a 20 43 6f 70 79 72 69 67 68 74 20  /*. * Copyright 
0010: 28 43 29 20 31 39 39 37 2d 32 30 30 30 20 4d 61  (C) 1997-2000 Ma
0020: 74 74 20 4e 65 77 6d 61 6e 20 3c 6d 61 74 74 40  tt Newman <matt@
0030: 6e 6f 76 61 64 69 67 6d 2e 63 6f 6d 3e 0a 20 2a  novadigm.com>. *
0040: 0a 20 2a 20 24 48 65 61 64 65 72 3a 20 2f 68 6f  . * $Header: /ho
0050: 6d 65 2f 72 6b 65 65 6e 65 2f 74 6d 70 2f 63 76  me/rkeene/tmp/cv
0060: 73 32 66 6f 73 73 69 6c 2f 2e 2e 2f 74 63 6c 74  s2fossil/../tclt
0070: 6c 73 2f 74 6c 73 2f 74 6c 73 2f 74 6c 73 42 49  ls/tls/tls/tlsBI
0080: 4f 2e 63 2c 76 20 31 2e 37 20 32 30 30 34 2f 30  O.c,v 1.7 2004/0
0090: 32 2f 30 34 20 30 34 3a 30 32 3a 31 39 20 72 61  2/04 04:02:19 ra
00a0: 7a 7a 65 6c 6c 20 45 78 70 20 24 0a 20 2a 0a 20  zzell Exp $. *. 
00b0: 2a 20 50 72 6f 76 69 64 65 73 20 42 49 4f 20 6c  * Provides BIO l
00c0: 61 79 65 72 20 74 6f 20 69 6e 74 65 72 66 61 63  ayer to interfac
00d0: 65 20 6f 70 65 6e 73 73 6c 20 74 6f 20 54 63 6c  e openssl to Tcl
00e0: 2e 0a 20 2a 2f 0a 0a 23 69 6e 63 6c 75 64 65 20  .. */..#include 
00f0: 22 74 6c 73 49 6e 74 2e 68 22 0a 0a 2f 2a 0a 20  "tlsInt.h"../*. 
0100: 2a 20 46 6f 72 77 61 72 64 20 64 65 63 6c 61 72  * Forward declar
0110: 61 74 69 6f 6e 73 0a 20 2a 2f 0a 0a 73 74 61 74  ations. */..stat
0120: 69 63 20 69 6e 74 20 42 69 6f 57 72 69 74 65 09  ic int BioWrite.
0130: 5f 41 4e 53 49 5f 41 52 47 53 5f 20 28 28 42 49  _ANSI_ARGS_ ((BI
0140: 4f 20 2a 68 2c 20 43 4f 4e 53 54 20 63 68 61 72  O *h, CONST char
0150: 20 2a 62 75 66 2c 20 69 6e 74 20 6e 75 6d 29 29   *buf, int num))
0160: 3b 0a 73 74 61 74 69 63 20 69 6e 74 20 42 69 6f  ;.static int Bio
0170: 52 65 61 64 09 5f 41 4e 53 49 5f 41 52 47 53 5f  Read._ANSI_ARGS_
0180: 20 28 28 42 49 4f 20 2a 68 2c 20 63 68 61 72 20   ((BIO *h, char 
0190: 2a 62 75 66 2c 20 69 6e 74 20 6e 75 6d 29 29 3b  *buf, int num));
01a0: 0a 73 74 61 74 69 63 20 69 6e 74 20 42 69 6f 50  .static int BioP
01b0: 75 74 73 09 5f 41 4e 53 49 5f 41 52 47 53 5f 20  uts._ANSI_ARGS_ 
01c0: 28 28 42 49 4f 20 2a 68 2c 20 43 4f 4e 53 54 20  ((BIO *h, CONST 
01d0: 63 68 61 72 20 2a 73 74 72 29 29 3b 0a 73 74 61  char *str));.sta
01e0: 74 69 63 20 6c 6f 6e 67 20 42 69 6f 43 74 72 6c  tic long BioCtrl
01f0: 09 5f 41 4e 53 49 5f 41 52 47 53 5f 20 28 28 42  ._ANSI_ARGS_ ((B
0200: 49 4f 20 2a 68 2c 20 69 6e 74 20 63 6d 64 2c 20  IO *h, int cmd, 
0210: 6c 6f 6e 67 20 61 72 67 31 2c 20 76 6f 69 64 20  long arg1, void 
0220: 2a 70 74 72 29 29 3b 0a 73 74 61 74 69 63 20 69  *ptr));.static i
0230: 6e 74 20 42 69 6f 4e 65 77 09 5f 41 4e 53 49 5f  nt BioNew._ANSI_
0240: 41 52 47 53 5f 20 28 28 42 49 4f 20 2a 68 29 29  ARGS_ ((BIO *h))
0250: 3b 0a 73 74 61 74 69 63 20 69 6e 74 20 42 69 6f  ;.static int Bio
0260: 46 72 65 65 09 5f 41 4e 53 49 5f 41 52 47 53 5f  Free._ANSI_ARGS_
0270: 20 28 28 42 49 4f 20 2a 68 29 29 3b 0a 0a 0a 73   ((BIO *h));...s
0280: 74 61 74 69 63 20 42 49 4f 5f 4d 45 54 48 4f 44  tatic BIO_METHOD
0290: 20 42 69 6f 4d 65 74 68 6f 64 73 20 3d 20 7b 0a   BioMethods = {.
02a0: 20 20 20 20 42 49 4f 5f 54 59 50 45 5f 54 43 4c      BIO_TYPE_TCL
02b0: 2c 20 22 74 63 6c 22 2c 0a 20 20 20 20 42 69 6f  , "tcl",.    Bio
02c0: 57 72 69 74 65 2c 0a 20 20 20 20 42 69 6f 52 65  Write,.    BioRe
02d0: 61 64 2c 0a 20 20 20 20 42 69 6f 50 75 74 73 2c  ad,.    BioPuts,
02e0: 0a 20 20 20 20 4e 55 4c 4c 2c 09 2f 2a 20 42 69  .    NULL,./* Bi
02f0: 6f 47 65 74 73 20 2a 2f 0a 20 20 20 20 42 69 6f  oGets */.    Bio
0300: 43 74 72 6c 2c 0a 20 20 20 20 42 69 6f 4e 65 77  Ctrl,.    BioNew
0310: 2c 0a 20 20 20 20 42 69 6f 46 72 65 65 2c 0a 7d  ,.    BioFree,.}
0320: 3b 0a 0a 42 49 4f 20 2a 0a 42 49 4f 5f 6e 65 77  ;..BIO *.BIO_new
0330: 5f 74 63 6c 28 73 74 61 74 65 50 74 72 2c 20 66  _tcl(statePtr, f
0340: 6c 61 67 73 29 0a 20 20 20 20 53 74 61 74 65 20  lags).    State 
0350: 2a 73 74 61 74 65 50 74 72 3b 0a 20 20 20 20 69  *statePtr;.    i
0360: 6e 74 20 66 6c 61 67 73 3b 0a 7b 0a 20 20 20 20  nt flags;.{.    
0370: 42 49 4f 20 2a 62 69 6f 3b 0a 0a 20 20 20 20 62  BIO *bio;..    b
0380: 69 6f 09 09 09 3d 20 42 49 4f 5f 6e 65 77 28 26  io...= BIO_new(&
0390: 42 69 6f 4d 65 74 68 6f 64 73 29 3b 0a 20 20 20  BioMethods);.   
03a0: 20 62 69 6f 2d 3e 70 74 72 09 09 3d 20 28 63 68   bio->ptr..= (ch
03b0: 61 72 2a 29 73 74 61 74 65 50 74 72 3b 0a 20 20  ar*)statePtr;.  
03c0: 20 20 62 69 6f 2d 3e 69 6e 69 74 09 09 3d 20 31    bio->init..= 1
03d0: 3b 0a 20 20 20 20 62 69 6f 2d 3e 73 68 75 74 64  ;.    bio->shutd
03e0: 6f 77 6e 09 3d 20 66 6c 61 67 73 3b 0a 0a 20 20  own.= flags;..  
03f0: 20 20 72 65 74 75 72 6e 20 62 69 6f 3b 0a 7d 0a    return bio;.}.
0400: 0a 42 49 4f 5f 4d 45 54 48 4f 44 20 2a 0a 42 49  .BIO_METHOD *.BI
0410: 4f 5f 73 5f 74 63 6c 28 29 0a 7b 0a 20 20 20 20  O_s_tcl().{.    
0420: 72 65 74 75 72 6e 20 26 42 69 6f 4d 65 74 68 6f  return &BioMetho
0430: 64 73 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e  ds;.}..static in
0440: 74 0a 42 69 6f 57 72 69 74 65 20 28 62 69 6f 2c  t.BioWrite (bio,
0450: 20 62 75 66 2c 20 62 75 66 4c 65 6e 29 0a 20 20   buf, bufLen).  
0460: 20 20 42 49 4f 20 2a 62 69 6f 3b 0a 20 20 20 20    BIO *bio;.    
0470: 43 4f 4e 53 54 20 63 68 61 72 20 2a 62 75 66 3b  CONST char *buf;
0480: 0a 20 20 20 20 69 6e 74 20 62 75 66 4c 65 6e 3b  .    int bufLen;
0490: 0a 7b 0a 20 20 20 20 54 63 6c 5f 43 68 61 6e 6e  .{.    Tcl_Chann
04a0: 65 6c 20 63 68 61 6e 20 3d 20 54 6c 73 5f 47 65  el chan = Tls_Ge
04b0: 74 50 61 72 65 6e 74 28 28 53 74 61 74 65 2a 29  tParent((State*)
04c0: 28 62 69 6f 2d 3e 70 74 72 29 29 3b 0a 20 20 20  (bio->ptr));.   
04d0: 20 69 6e 74 20 72 65 74 3b 0a 0a 20 20 20 20 64   int ret;..    d
04e0: 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c 22 5c  printf(stderr,"\
04f0: 6e 42 69 6f 57 72 69 74 65 28 30 78 25 78 2c 20  nBioWrite(0x%x, 
0500: 3c 62 75 66 3e 2c 20 25 64 29 20 5b 30 78 25 78  <buf>, %d) [0x%x
0510: 5d 22 2c 0a 09 20 20 20 20 28 75 6e 73 69 67 6e  ]",..    (unsign
0520: 65 64 20 69 6e 74 29 20 62 69 6f 2c 20 62 75 66  ed int) bio, buf
0530: 4c 65 6e 2c 20 28 75 6e 73 69 67 6e 65 64 20 69  Len, (unsigned i
0540: 6e 74 29 20 63 68 61 6e 29 3b 0a 0a 20 20 20 20  nt) chan);..    
0550: 69 66 20 28 63 68 61 6e 6e 65 6c 54 79 70 65 56  if (channelTypeV
0560: 65 72 73 69 6f 6e 20 3d 3d 20 54 4c 53 5f 43 48  ersion == TLS_CH
0570: 41 4e 4e 45 4c 5f 56 45 52 53 49 4f 4e 5f 32 29  ANNEL_VERSION_2)
0580: 20 7b 0a 09 72 65 74 20 3d 20 54 63 6c 5f 57 72   {..ret = Tcl_Wr
0590: 69 74 65 52 61 77 28 63 68 61 6e 2c 20 62 75 66  iteRaw(chan, buf
05a0: 2c 20 62 75 66 4c 65 6e 29 3b 0a 20 20 20 20 7d  , bufLen);.    }
05b0: 20 65 6c 73 65 20 7b 0a 09 72 65 74 20 3d 20 54   else {..ret = T
05c0: 63 6c 5f 57 72 69 74 65 28 63 68 61 6e 2c 20 62  cl_Write(chan, b
05d0: 75 66 2c 20 62 75 66 4c 65 6e 29 3b 0a 20 20 20  uf, bufLen);.   
05e0: 20 7d 0a 0a 20 20 20 20 64 70 72 69 6e 74 66 28   }..    dprintf(
05f0: 73 74 64 65 72 72 2c 22 5c 6e 5b 30 78 25 78 5d  stderr,"\n[0x%x]
0600: 20 42 69 6f 57 72 69 74 65 28 25 64 29 20 2d 3e   BioWrite(%d) ->
0610: 20 25 64 20 5b 25 64 2e 25 64 5d 22 2c 0a 09 20   %d [%d.%d]",.. 
0620: 20 20 20 28 75 6e 73 69 67 6e 65 64 20 69 6e 74     (unsigned int
0630: 29 20 63 68 61 6e 2c 20 62 75 66 4c 65 6e 2c 20  ) chan, bufLen, 
0640: 72 65 74 2c 20 54 63 6c 5f 45 6f 66 28 63 68 61  ret, Tcl_Eof(cha
0650: 6e 29 2c 20 54 63 6c 5f 47 65 74 45 72 72 6e 6f  n), Tcl_GetErrno
0660: 28 29 29 3b 0a 0a 20 20 20 20 42 49 4f 5f 63 6c  ());..    BIO_cl
0670: 65 61 72 5f 66 6c 61 67 73 28 62 69 6f 2c 20 42  ear_flags(bio, B
0680: 49 4f 5f 46 4c 41 47 53 5f 57 52 49 54 45 7c 42  IO_FLAGS_WRITE|B
0690: 49 4f 5f 46 4c 41 47 53 5f 53 48 4f 55 4c 44 5f  IO_FLAGS_SHOULD_
06a0: 52 45 54 52 59 29 3b 0a 0a 20 20 20 20 69 66 20  RETRY);..    if 
06b0: 28 72 65 74 20 3d 3d 20 30 29 20 7b 0a 09 69 66  (ret == 0) {..if
06c0: 20 28 21 54 63 6c 5f 45 6f 66 28 63 68 61 6e 29   (!Tcl_Eof(chan)
06d0: 29 20 7b 0a 09 20 20 20 20 42 49 4f 5f 73 65 74  ) {..    BIO_set
06e0: 5f 72 65 74 72 79 5f 77 72 69 74 65 28 62 69 6f  _retry_write(bio
06f0: 29 3b 0a 09 20 20 20 20 72 65 74 20 3d 20 2d 31  );..    ret = -1
0700: 3b 0a 09 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69  ;..}.    }.    i
0710: 66 20 28 42 49 4f 5f 73 68 6f 75 6c 64 5f 72 65  f (BIO_should_re
0720: 61 64 28 62 69 6f 29 29 20 7b 0a 09 42 49 4f 5f  ad(bio)) {..BIO_
0730: 73 65 74 5f 72 65 74 72 79 5f 72 65 61 64 28 62  set_retry_read(b
0740: 69 6f 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72  io);.    }.    r
0750: 65 74 75 72 6e 20 72 65 74 3b 0a 7d 0a 0a 73 74  eturn ret;.}..st
0760: 61 74 69 63 20 69 6e 74 0a 42 69 6f 52 65 61 64  atic int.BioRead
0770: 20 28 62 69 6f 2c 20 62 75 66 2c 20 62 75 66 4c   (bio, buf, bufL
0780: 65 6e 29 0a 20 20 20 20 42 49 4f 20 2a 62 69 6f  en).    BIO *bio
0790: 3b 0a 20 20 20 20 63 68 61 72 20 2a 62 75 66 3b  ;.    char *buf;
07a0: 0a 20 20 20 20 69 6e 74 20 62 75 66 4c 65 6e 3b  .    int bufLen;
07b0: 0a 7b 0a 20 20 20 20 54 63 6c 5f 43 68 61 6e 6e  .{.    Tcl_Chann
07c0: 65 6c 20 63 68 61 6e 20 3d 20 54 6c 73 5f 47 65  el chan = Tls_Ge
07d0: 74 50 61 72 65 6e 74 28 28 53 74 61 74 65 2a 29  tParent((State*)
07e0: 62 69 6f 2d 3e 70 74 72 29 3b 0a 20 20 20 20 69  bio->ptr);.    i
07f0: 6e 74 20 72 65 74 20 3d 20 30 3b 0a 0a 20 20 20  nt ret = 0;..   
0800: 20 64 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c   dprintf(stderr,
0810: 22 5c 6e 42 69 6f 52 65 61 64 28 30 78 25 78 2c  "\nBioRead(0x%x,
0820: 20 3c 62 75 66 3e 2c 20 25 64 29 20 5b 30 78 25   <buf>, %d) [0x%
0830: 78 5d 22 2c 0a 09 20 20 20 20 28 75 6e 73 69 67  x]",..    (unsig
0840: 6e 65 64 20 69 6e 74 29 20 62 69 6f 2c 20 62 75  ned int) bio, bu
0850: 66 4c 65 6e 2c 20 28 75 6e 73 69 67 6e 65 64 20  fLen, (unsigned 
0860: 69 6e 74 29 20 63 68 61 6e 29 3b 0a 0a 20 20 20  int) chan);..   
0870: 20 69 66 20 28 62 75 66 20 3d 3d 20 4e 55 4c 4c   if (buf == NULL
0880: 29 20 72 65 74 75 72 6e 20 30 3b 0a 0a 20 20 20  ) return 0;..   
0890: 20 69 66 20 28 63 68 61 6e 6e 65 6c 54 79 70 65   if (channelType
08a0: 56 65 72 73 69 6f 6e 20 3d 3d 20 54 4c 53 5f 43  Version == TLS_C
08b0: 48 41 4e 4e 45 4c 5f 56 45 52 53 49 4f 4e 5f 32  HANNEL_VERSION_2
08c0: 29 20 7b 0a 09 72 65 74 20 3d 20 54 63 6c 5f 52  ) {..ret = Tcl_R
08d0: 65 61 64 52 61 77 28 63 68 61 6e 2c 20 62 75 66  eadRaw(chan, buf
08e0: 2c 20 62 75 66 4c 65 6e 29 3b 0a 20 20 20 20 7d  , bufLen);.    }
08f0: 20 65 6c 73 65 20 7b 0a 09 72 65 74 20 3d 20 54   else {..ret = T
0900: 63 6c 5f 52 65 61 64 28 63 68 61 6e 2c 20 62 75  cl_Read(chan, bu
0910: 66 2c 20 62 75 66 4c 65 6e 29 3b 0a 20 20 20 20  f, bufLen);.    
0920: 7d 0a 0a 20 20 20 20 64 70 72 69 6e 74 66 28 73  }..    dprintf(s
0930: 74 64 65 72 72 2c 22 5c 6e 5b 30 78 25 78 5d 20  tderr,"\n[0x%x] 
0940: 42 69 6f 52 65 61 64 28 25 64 29 20 2d 3e 20 25  BioRead(%d) -> %
0950: 64 20 5b 25 64 2e 25 64 5d 22 2c 0a 09 20 20 20  d [%d.%d]",..   
0960: 20 28 75 6e 73 69 67 6e 65 64 20 69 6e 74 29 20   (unsigned int) 
0970: 63 68 61 6e 2c 20 62 75 66 4c 65 6e 2c 20 72 65  chan, bufLen, re
0980: 74 2c 20 54 63 6c 5f 45 6f 66 28 63 68 61 6e 29  t, Tcl_Eof(chan)
0990: 2c 20 54 63 6c 5f 47 65 74 45 72 72 6e 6f 28 29  , Tcl_GetErrno()
09a0: 29 3b 0a 0a 20 20 20 20 42 49 4f 5f 63 6c 65 61  );..    BIO_clea
09b0: 72 5f 66 6c 61 67 73 28 62 69 6f 2c 20 42 49 4f  r_flags(bio, BIO
09c0: 5f 46 4c 41 47 53 5f 52 45 41 44 7c 42 49 4f 5f  _FLAGS_READ|BIO_
09d0: 46 4c 41 47 53 5f 53 48 4f 55 4c 44 5f 52 45 54  FLAGS_SHOULD_RET
09e0: 52 59 29 3b 0a 0a 20 20 20 20 69 66 20 28 72 65  RY);..    if (re
09f0: 74 20 3d 3d 20 30 29 20 7b 0a 09 69 66 20 28 21  t == 0) {..if (!
0a00: 54 63 6c 5f 45 6f 66 28 63 68 61 6e 29 29 20 7b  Tcl_Eof(chan)) {
0a10: 0a 09 20 20 20 20 42 49 4f 5f 73 65 74 5f 72 65  ..    BIO_set_re
0a20: 74 72 79 5f 72 65 61 64 28 62 69 6f 29 3b 0a 09  try_read(bio);..
0a30: 20 20 20 20 72 65 74 20 3d 20 2d 31 3b 0a 09 7d      ret = -1;..}
0a40: 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 20 28 42  .    }.    if (B
0a50: 49 4f 5f 73 68 6f 75 6c 64 5f 77 72 69 74 65 28  IO_should_write(
0a60: 62 69 6f 29 29 20 7b 0a 09 42 49 4f 5f 73 65 74  bio)) {..BIO_set
0a70: 5f 72 65 74 72 79 5f 77 72 69 74 65 28 62 69 6f  _retry_write(bio
0a80: 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74  );.    }.    ret
0a90: 75 72 6e 20 72 65 74 3b 0a 7d 0a 0a 73 74 61 74  urn ret;.}..stat
0aa0: 69 63 20 69 6e 74 0a 42 69 6f 50 75 74 73 09 28  ic int.BioPuts.(
0ab0: 62 69 6f 2c 20 73 74 72 29 0a 20 20 20 20 42 49  bio, str).    BI
0ac0: 4f 20 2a 62 69 6f 3b 0a 20 20 20 20 43 4f 4e 53  O *bio;.    CONS
0ad0: 54 20 63 68 61 72 20 2a 73 74 72 3b 0a 7b 0a 20  T char *str;.{. 
0ae0: 20 20 20 72 65 74 75 72 6e 20 42 69 6f 57 72 69     return BioWri
0af0: 74 65 28 62 69 6f 2c 20 73 74 72 2c 20 73 74 72  te(bio, str, str
0b00: 6c 65 6e 28 73 74 72 29 29 3b 0a 7d 0a 0a 73 74  len(str));.}..st
0b10: 61 74 69 63 20 6c 6f 6e 67 0a 42 69 6f 43 74 72  atic long.BioCtr
0b20: 6c 09 28 62 69 6f 2c 20 63 6d 64 2c 20 6e 75 6d  l.(bio, cmd, num
0b30: 2c 20 70 74 72 29 0a 20 20 20 20 42 49 4f 20 2a  , ptr).    BIO *
0b40: 62 69 6f 3b 0a 20 20 20 20 69 6e 74 20 63 6d 64  bio;.    int cmd
0b50: 3b 0a 20 20 20 20 6c 6f 6e 67 20 6e 75 6d 3b 0a  ;.    long num;.
0b60: 20 20 20 20 76 6f 69 64 20 2a 70 74 72 3b 0a 7b      void *ptr;.{
0b70: 0a 20 20 20 20 54 63 6c 5f 43 68 61 6e 6e 65 6c  .    Tcl_Channel
0b80: 20 63 68 61 6e 20 3d 20 54 6c 73 5f 47 65 74 50   chan = Tls_GetP
0b90: 61 72 65 6e 74 28 28 53 74 61 74 65 2a 29 62 69  arent((State*)bi
0ba0: 6f 2d 3e 70 74 72 29 3b 0a 20 20 20 20 6c 6f 6e  o->ptr);.    lon
0bb0: 67 20 72 65 74 20 3d 20 31 3b 0a 20 20 20 20 69  g ret = 1;.    i
0bc0: 6e 74 20 2a 69 70 3b 0a 0a 20 20 20 20 64 70 72  nt *ip;..    dpr
0bd0: 69 6e 74 66 28 73 74 64 65 72 72 2c 22 5c 6e 42  intf(stderr,"\nB
0be0: 69 6f 43 74 72 6c 28 30 78 25 78 2c 20 30 78 25  ioCtrl(0x%x, 0x%
0bf0: 78 2c 20 30 78 25 78 2c 20 30 78 25 78 29 22 2c  x, 0x%x, 0x%x)",
0c00: 0a 09 20 20 20 20 28 75 6e 73 69 67 6e 65 64 20  ..    (unsigned 
0c10: 69 6e 74 29 20 62 69 6f 2c 20 28 75 6e 73 69 67  int) bio, (unsig
0c20: 6e 65 64 20 69 6e 74 29 20 63 6d 64 2c 20 28 75  ned int) cmd, (u
0c30: 6e 73 69 67 6e 65 64 20 69 6e 74 29 20 6e 75 6d  nsigned int) num
0c40: 2c 0a 09 20 20 20 20 28 75 6e 73 69 67 6e 65 64  ,..    (unsigned
0c50: 20 69 6e 74 29 20 70 74 72 29 3b 0a 0a 20 20 20   int) ptr);..   
0c60: 20 73 77 69 74 63 68 20 28 63 6d 64 29 20 7b 0a   switch (cmd) {.
0c70: 20 20 20 20 63 61 73 65 20 42 49 4f 5f 43 54 52      case BIO_CTR
0c80: 4c 5f 52 45 53 45 54 3a 0a 09 6e 75 6d 20 3d 20  L_RESET:..num = 
0c90: 30 3b 0a 20 20 20 20 63 61 73 65 20 42 49 4f 5f  0;.    case BIO_
0ca0: 43 5f 46 49 4c 45 5f 53 45 45 4b 3a 0a 20 20 20  C_FILE_SEEK:.   
0cb0: 20 63 61 73 65 20 42 49 4f 5f 43 5f 46 49 4c 45   case BIO_C_FILE
0cc0: 5f 54 45 4c 4c 3a 0a 09 72 65 74 20 3d 20 30 3b  _TELL:..ret = 0;
0cd0: 0a 09 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73  ..break;.    cas
0ce0: 65 20 42 49 4f 5f 43 54 52 4c 5f 49 4e 46 4f 3a  e BIO_CTRL_INFO:
0cf0: 0a 09 72 65 74 20 3d 20 31 3b 0a 09 62 72 65 61  ..ret = 1;..brea
0d00: 6b 3b 0a 20 20 20 20 63 61 73 65 20 42 49 4f 5f  k;.    case BIO_
0d10: 43 5f 53 45 54 5f 46 44 3a 0a 09 42 69 6f 46 72  C_SET_FD:..BioFr
0d20: 65 65 28 62 69 6f 29 3b 0a 09 2f 2a 20 53 65 74  ee(bio);../* Set
0d30: 73 20 53 74 61 74 65 2a 20 2a 2f 0a 09 62 69 6f  s State* */..bio
0d40: 2d 3e 70 74 72 09 3d 20 2a 28 28 63 68 61 72 20  ->ptr.= *((char 
0d50: 2a 2a 29 70 74 72 29 3b 0a 09 62 69 6f 2d 3e 73  **)ptr);..bio->s
0d60: 68 75 74 64 6f 77 6e 09 3d 20 28 69 6e 74 29 6e  hutdown.= (int)n
0d70: 75 6d 3b 0a 09 62 69 6f 2d 3e 69 6e 69 74 09 3d  um;..bio->init.=
0d80: 20 31 3b 0a 09 62 72 65 61 6b 3b 0a 20 20 20 20   1;..break;.    
0d90: 63 61 73 65 20 42 49 4f 5f 43 5f 47 45 54 5f 46  case BIO_C_GET_F
0da0: 44 3a 0a 09 69 66 20 28 62 69 6f 2d 3e 69 6e 69  D:..if (bio->ini
0db0: 74 29 20 7b 0a 09 20 20 20 20 69 70 20 3d 20 28  t) {..    ip = (
0dc0: 69 6e 74 20 2a 29 70 74 72 3b 0a 09 20 20 20 20  int *)ptr;..    
0dd0: 69 66 20 28 69 70 20 21 3d 20 4e 55 4c 4c 29 20  if (ip != NULL) 
0de0: 7b 0a 09 09 2a 69 70 20 3d 20 62 69 6f 2d 3e 6e  {...*ip = bio->n
0df0: 75 6d 3b 0a 09 20 20 20 20 7d 0a 09 20 20 20 20  um;..    }..    
0e00: 72 65 74 20 3d 20 62 69 6f 2d 3e 6e 75 6d 3b 0a  ret = bio->num;.
0e10: 09 7d 20 65 6c 73 65 20 7b 0a 09 20 20 20 20 72  .} else {..    r
0e20: 65 74 20 3d 20 2d 31 3b 0a 09 7d 0a 09 62 72 65  et = -1;..}..bre
0e30: 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 42 49 4f  ak;.    case BIO
0e40: 5f 43 54 52 4c 5f 47 45 54 5f 43 4c 4f 53 45 3a  _CTRL_GET_CLOSE:
0e50: 0a 09 72 65 74 20 3d 20 62 69 6f 2d 3e 73 68 75  ..ret = bio->shu
0e60: 74 64 6f 77 6e 3b 0a 09 62 72 65 61 6b 3b 0a 20  tdown;..break;. 
0e70: 20 20 20 63 61 73 65 20 42 49 4f 5f 43 54 52 4c     case BIO_CTRL
0e80: 5f 53 45 54 5f 43 4c 4f 53 45 3a 0a 09 62 69 6f  _SET_CLOSE:..bio
0e90: 2d 3e 73 68 75 74 64 6f 77 6e 20 3d 20 28 69 6e  ->shutdown = (in
0ea0: 74 29 6e 75 6d 3b 0a 09 62 72 65 61 6b 3b 0a 20  t)num;..break;. 
0eb0: 20 20 20 63 61 73 65 20 42 49 4f 5f 43 54 52 4c     case BIO_CTRL
0ec0: 5f 45 4f 46 3a 0a 09 64 70 72 69 6e 74 66 28 73  _EOF:..dprintf(s
0ed0: 74 64 65 72 72 2c 20 22 42 49 4f 5f 43 54 52 4c  tderr, "BIO_CTRL
0ee0: 5f 45 4f 46 5c 6e 22 29 3b 0a 09 72 65 74 20 3d  _EOF\n");..ret =
0ef0: 20 54 63 6c 5f 45 6f 66 28 63 68 61 6e 29 3b 0a   Tcl_Eof(chan);.
0f00: 09 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65  .break;.    case
0f10: 20 42 49 4f 5f 43 54 52 4c 5f 50 45 4e 44 49 4e   BIO_CTRL_PENDIN
0f20: 47 3a 0a 09 72 65 74 20 3d 20 28 54 63 6c 5f 49  G:..ret = (Tcl_I
0f30: 6e 70 75 74 42 75 66 66 65 72 65 64 28 63 68 61  nputBuffered(cha
0f40: 6e 29 20 3f 20 31 20 3a 20 30 29 3b 0a 09 64 70  n) ? 1 : 0);..dp
0f50: 72 69 6e 74 66 28 73 74 64 65 72 72 2c 20 22 42  rintf(stderr, "B
0f60: 49 4f 5f 43 54 52 4c 5f 50 45 4e 44 49 4e 47 28  IO_CTRL_PENDING(
0f70: 25 64 29 5c 6e 22 2c 20 28 69 6e 74 29 20 72 65  %d)\n", (int) re
0f80: 74 29 3b 0a 09 62 72 65 61 6b 3b 0a 20 20 20 20  t);..break;.    
0f90: 63 61 73 65 20 42 49 4f 5f 43 54 52 4c 5f 57 50  case BIO_CTRL_WP
0fa0: 45 4e 44 49 4e 47 3a 0a 09 72 65 74 20 3d 20 30  ENDING:..ret = 0
0fb0: 3b 0a 09 62 72 65 61 6b 3b 0a 20 20 20 20 63 61  ;..break;.    ca
0fc0: 73 65 20 42 49 4f 5f 43 54 52 4c 5f 44 55 50 3a  se BIO_CTRL_DUP:
0fd0: 0a 09 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73  ..break;.    cas
0fe0: 65 20 42 49 4f 5f 43 54 52 4c 5f 46 4c 55 53 48  e BIO_CTRL_FLUSH
0ff0: 3a 0a 09 64 70 72 69 6e 74 66 28 73 74 64 65 72  :..dprintf(stder
1000: 72 2c 20 22 42 49 4f 5f 43 54 52 4c 5f 46 4c 55  r, "BIO_CTRL_FLU
1010: 53 48 5c 6e 22 29 3b 0a 09 69 66 20 28 63 68 61  SH\n");..if (cha
1020: 6e 6e 65 6c 54 79 70 65 56 65 72 73 69 6f 6e 20  nnelTypeVersion 
1030: 3d 3d 20 54 4c 53 5f 43 48 41 4e 4e 45 4c 5f 56  == TLS_CHANNEL_V
1040: 45 52 53 49 4f 4e 5f 32 29 20 7b 0a 09 20 20 20  ERSION_2) {..   
1050: 20 72 65 74 20 3d 20 28 28 54 63 6c 5f 57 72 69   ret = ((Tcl_Wri
1060: 74 65 52 61 77 28 63 68 61 6e 2c 20 22 22 2c 20  teRaw(chan, "", 
1070: 30 29 20 3e 3d 20 30 29 20 3f 20 31 20 3a 20 2d  0) >= 0) ? 1 : -
1080: 31 29 3b 0a 09 7d 20 65 6c 73 65 20 7b 0a 09 20  1);..} else {.. 
1090: 20 20 20 72 65 74 20 3d 20 28 28 54 63 6c 5f 46     ret = ((Tcl_F
10a0: 6c 75 73 68 28 63 68 61 6e 29 20 3d 3d 20 54 43  lush(chan) == TC
10b0: 4c 5f 4f 4b 29 20 3f 20 31 20 3a 20 2d 31 29 3b  L_OK) ? 1 : -1);
10c0: 0a 09 7d 0a 09 62 72 65 61 6b 3b 0a 20 20 20 20  ..}..break;.    
10d0: 64 65 66 61 75 6c 74 3a 0a 09 72 65 74 20 3d 20  default:..ret = 
10e0: 30 3b 0a 09 62 72 65 61 6b 3b 0a 20 20 20 20 7d  0;..break;.    }
10f0: 0a 20 20 20 20 72 65 74 75 72 6e 28 72 65 74 29  .    return(ret)
1100: 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 0a  ;.}..static int.
1110: 42 69 6f 4e 65 77 09 28 62 69 6f 29 0a 20 20 20  BioNew.(bio).   
1120: 20 42 49 4f 20 2a 62 69 6f 3b 0a 7b 0a 20 20 20   BIO *bio;.{.   
1130: 20 62 69 6f 2d 3e 69 6e 69 74 09 3d 20 30 3b 0a   bio->init.= 0;.
1140: 20 20 20 20 62 69 6f 2d 3e 6e 75 6d 09 3d 20 30      bio->num.= 0
1150: 3b 0a 20 20 20 20 62 69 6f 2d 3e 70 74 72 09 3d  ;.    bio->ptr.=
1160: 20 4e 55 4c 4c 3b 0a 20 20 20 20 62 69 6f 2d 3e   NULL;.    bio->
1170: 66 6c 61 67 73 09 3d 20 30 3b 0a 0a 20 20 20 20  flags.= 0;..    
1180: 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a 73 74 61  return 1;.}..sta
1190: 74 69 63 20 69 6e 74 0a 42 69 6f 46 72 65 65 09  tic int.BioFree.
11a0: 28 62 69 6f 29 0a 20 20 20 20 42 49 4f 20 2a 62  (bio).    BIO *b
11b0: 69 6f 3b 0a 7b 0a 20 20 20 20 69 66 20 28 62 69  io;.{.    if (bi
11c0: 6f 20 3d 3d 20 4e 55 4c 4c 29 20 7b 0a 09 72 65  o == NULL) {..re
11d0: 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 0a 20  turn 0;.    }.. 
11e0: 20 20 20 69 66 20 28 62 69 6f 2d 3e 73 68 75 74     if (bio->shut
11f0: 64 6f 77 6e 29 20 7b 0a 09 69 66 20 28 62 69 6f  down) {..if (bio
1200: 2d 3e 69 6e 69 74 29 20 7b 0a 09 20 20 20 20 2f  ->init) {..    /
1210: 2a 73 68 75 74 64 6f 77 6e 28 62 69 6f 2d 3e 6e  *shutdown(bio->n
1220: 75 6d 2c 20 32 29 20 2a 2f 0a 09 20 20 20 20 2f  um, 2) */..    /
1230: 2a 63 6c 6f 73 65 73 6f 63 6b 65 74 28 62 69 6f  *closesocket(bio
1240: 2d 3e 6e 75 6d 29 20 2a 2f 0a 09 7d 0a 09 62 69  ->num) */..}..bi
1250: 6f 2d 3e 69 6e 69 74 09 3d 20 30 3b 0a 09 62 69  o->init.= 0;..bi
1260: 6f 2d 3e 66 6c 61 67 73 09 3d 20 30 3b 0a 09 62  o->flags.= 0;..b
1270: 69 6f 2d 3e 6e 75 6d 09 3d 20 30 3b 0a 20 20 20  io->num.= 0;.   
1280: 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b   }.    return 1;
1290: 0a 7d 0a                                         .}.