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 50 72 6f 76 69 64 65 73 20 42 49 4f . * Provides BIO
0050: 20 6c 61 79 65 72 20 74 6f 20 69 6e 74 65 72 66 layer to interf
0060: 61 63 65 20 6f 70 65 6e 73 73 6c 20 74 6f 20 54 ace openssl to T
0070: 63 6c 2e 0a 20 2a 2f 0a 0a 23 69 6e 63 6c 75 64 cl.. */..#includ
0080: 65 20 22 74 6c 73 49 6e 74 2e 68 22 0a 0a 2f 2a e "tlsInt.h"../*
0090: 0a 20 2a 20 46 6f 72 77 61 72 64 20 64 65 63 6c . * Forward decl
00a0: 61 72 61 74 69 6f 6e 73 0a 20 2a 2f 0a 0a 73 74 arations. */..st
00b0: 61 74 69 63 20 69 6e 74 20 42 69 6f 57 72 69 74 atic int BioWrit
00c0: 65 09 5f 41 4e 53 49 5f 41 52 47 53 5f 20 28 28 e._ANSI_ARGS_ ((
00d0: 42 49 4f 20 2a 68 2c 20 43 4f 4e 53 54 20 63 68 BIO *h, CONST ch
00e0: 61 72 20 2a 62 75 66 2c 20 69 6e 74 20 6e 75 6d ar *buf, int num
00f0: 29 29 3b 0a 73 74 61 74 69 63 20 69 6e 74 20 42 ));.static int B
0100: 69 6f 52 65 61 64 09 5f 41 4e 53 49 5f 41 52 47 ioRead._ANSI_ARG
0110: 53 5f 20 28 28 42 49 4f 20 2a 68 2c 20 63 68 61 S_ ((BIO *h, cha
0120: 72 20 2a 62 75 66 2c 20 69 6e 74 20 6e 75 6d 29 r *buf, int num)
0130: 29 3b 0a 73 74 61 74 69 63 20 69 6e 74 20 42 69 );.static int Bi
0140: 6f 50 75 74 73 09 5f 41 4e 53 49 5f 41 52 47 53 oPuts._ANSI_ARGS
0150: 5f 20 28 28 42 49 4f 20 2a 68 2c 20 43 4f 4e 53 _ ((BIO *h, CONS
0160: 54 20 63 68 61 72 20 2a 73 74 72 29 29 3b 0a 73 T char *str));.s
0170: 74 61 74 69 63 20 6c 6f 6e 67 20 42 69 6f 43 74 tatic long BioCt
0180: 72 6c 09 5f 41 4e 53 49 5f 41 52 47 53 5f 20 28 rl._ANSI_ARGS_ (
0190: 28 42 49 4f 20 2a 68 2c 20 69 6e 74 20 63 6d 64 (BIO *h, int cmd
01a0: 2c 20 6c 6f 6e 67 20 61 72 67 31 2c 20 76 6f 69 , long arg1, voi
01b0: 64 20 2a 70 74 72 29 29 3b 0a 73 74 61 74 69 63 d *ptr));.static
01c0: 20 69 6e 74 20 42 69 6f 4e 65 77 09 5f 41 4e 53 int BioNew._ANS
01d0: 49 5f 41 52 47 53 5f 20 28 28 42 49 4f 20 2a 68 I_ARGS_ ((BIO *h
01e0: 29 29 3b 0a 73 74 61 74 69 63 20 69 6e 74 20 42 ));.static int B
01f0: 69 6f 46 72 65 65 09 5f 41 4e 53 49 5f 41 52 47 ioFree._ANSI_ARG
0200: 53 5f 20 28 28 42 49 4f 20 2a 68 29 29 3b 0a 0a S_ ((BIO *h));..
0210: 0a 73 74 61 74 69 63 20 42 49 4f 5f 4d 45 54 48 .static BIO_METH
0220: 4f 44 20 42 69 6f 4d 65 74 68 6f 64 73 20 3d 20 OD BioMethods =
0230: 7b 0a 20 20 20 20 42 49 4f 5f 54 59 50 45 5f 54 {. BIO_TYPE_T
0240: 43 4c 2c 20 22 74 63 6c 22 2c 0a 20 20 20 20 42 CL, "tcl",. B
0250: 69 6f 57 72 69 74 65 2c 0a 20 20 20 20 42 69 6f ioWrite,. Bio
0260: 52 65 61 64 2c 0a 20 20 20 20 42 69 6f 50 75 74 Read,. BioPut
0270: 73 2c 0a 20 20 20 20 4e 55 4c 4c 2c 09 2f 2a 20 s,. NULL,./*
0280: 42 69 6f 47 65 74 73 20 2a 2f 0a 20 20 20 20 42 BioGets */. B
0290: 69 6f 43 74 72 6c 2c 0a 20 20 20 20 42 69 6f 4e ioCtrl,. BioN
02a0: 65 77 2c 0a 20 20 20 20 42 69 6f 46 72 65 65 2c ew,. BioFree,
02b0: 0a 7d 3b 0a 0a 42 49 4f 20 2a 0a 42 49 4f 5f 6e .};..BIO *.BIO_n
02c0: 65 77 5f 74 63 6c 28 73 74 61 74 65 50 74 72 2c ew_tcl(statePtr,
02d0: 20 66 6c 61 67 73 29 0a 20 20 20 20 53 74 61 74 flags). Stat
02e0: 65 20 2a 73 74 61 74 65 50 74 72 3b 0a 20 20 20 e *statePtr;.
02f0: 20 69 6e 74 20 66 6c 61 67 73 3b 0a 7b 0a 20 20 int flags;.{.
0300: 20 20 42 49 4f 20 2a 62 69 6f 3b 0a 0a 20 20 20 BIO *bio;..
0310: 20 62 69 6f 09 09 09 3d 20 42 49 4f 5f 6e 65 77 bio...= BIO_new
0320: 28 26 42 69 6f 4d 65 74 68 6f 64 73 29 3b 0a 20 (&BioMethods);.
0330: 20 20 20 62 69 6f 2d 3e 70 74 72 09 09 3d 20 28 bio->ptr..= (
0340: 63 68 61 72 2a 29 73 74 61 74 65 50 74 72 3b 0a char*)statePtr;.
0350: 20 20 20 20 62 69 6f 2d 3e 69 6e 69 74 09 09 3d bio->init..=
0360: 20 31 3b 0a 20 20 20 20 62 69 6f 2d 3e 73 68 75 1;. bio->shu
0370: 74 64 6f 77 6e 09 3d 20 66 6c 61 67 73 3b 0a 0a tdown.= flags;..
0380: 20 20 20 20 72 65 74 75 72 6e 20 62 69 6f 3b 0a return bio;.
0390: 7d 0a 0a 42 49 4f 5f 4d 45 54 48 4f 44 20 2a 0a }..BIO_METHOD *.
03a0: 42 49 4f 5f 73 5f 74 63 6c 28 29 0a 7b 0a 20 20 BIO_s_tcl().{.
03b0: 20 20 72 65 74 75 72 6e 20 26 42 69 6f 4d 65 74 return &BioMet
03c0: 68 6f 64 73 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 hods;.}..static
03d0: 69 6e 74 0a 42 69 6f 57 72 69 74 65 20 28 62 69 int.BioWrite (bi
03e0: 6f 2c 20 62 75 66 2c 20 62 75 66 4c 65 6e 29 0a o, buf, bufLen).
03f0: 20 20 20 20 42 49 4f 20 2a 62 69 6f 3b 0a 20 20 BIO *bio;.
0400: 20 20 43 4f 4e 53 54 20 63 68 61 72 20 2a 62 75 CONST char *bu
0410: 66 3b 0a 20 20 20 20 69 6e 74 20 62 75 66 4c 65 f;. int bufLe
0420: 6e 3b 0a 7b 0a 20 20 20 20 54 63 6c 5f 43 68 61 n;.{. Tcl_Cha
0430: 6e 6e 65 6c 20 63 68 61 6e 20 3d 20 54 6c 73 5f nnel chan = Tls_
0440: 47 65 74 50 61 72 65 6e 74 28 28 53 74 61 74 65 GetParent((State
0450: 2a 29 28 62 69 6f 2d 3e 70 74 72 29 29 3b 0a 20 *)(bio->ptr));.
0460: 20 20 20 69 6e 74 20 72 65 74 3b 0a 0a 20 20 20 int ret;..
0470: 20 64 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c dprintf(stderr,
0480: 22 5c 6e 42 69 6f 57 72 69 74 65 28 25 70 2c 20 "\nBioWrite(%p,
0490: 3c 62 75 66 3e 2c 20 25 64 29 20 5b 25 70 5d 22 <buf>, %d) [%p]"
04a0: 2c 0a 09 20 20 20 20 28 76 6f 69 64 20 2a 29 20 ,.. (void *)
04b0: 62 69 6f 2c 20 62 75 66 4c 65 6e 2c 20 28 76 6f bio, bufLen, (vo
04c0: 69 64 20 2a 29 20 63 68 61 6e 29 3b 0a 0a 20 20 id *) chan);..
04d0: 20 20 69 66 20 28 63 68 61 6e 6e 65 6c 54 79 70 if (channelTyp
04e0: 65 56 65 72 73 69 6f 6e 20 3d 3d 20 54 4c 53 5f eVersion == TLS_
04f0: 43 48 41 4e 4e 45 4c 5f 56 45 52 53 49 4f 4e 5f CHANNEL_VERSION_
0500: 32 29 20 7b 0a 09 72 65 74 20 3d 20 54 63 6c 5f 2) {..ret = Tcl_
0510: 57 72 69 74 65 52 61 77 28 63 68 61 6e 2c 20 62 WriteRaw(chan, b
0520: 75 66 2c 20 62 75 66 4c 65 6e 29 3b 0a 20 20 20 uf, bufLen);.
0530: 20 7d 20 65 6c 73 65 20 7b 0a 09 72 65 74 20 3d } else {..ret =
0540: 20 54 63 6c 5f 57 72 69 74 65 28 63 68 61 6e 2c Tcl_Write(chan,
0550: 20 62 75 66 2c 20 62 75 66 4c 65 6e 29 3b 0a 20 buf, bufLen);.
0560: 20 20 20 7d 0a 0a 20 20 20 20 64 70 72 69 6e 74 }.. dprint
0570: 66 28 73 74 64 65 72 72 2c 22 5c 6e 5b 25 70 5d f(stderr,"\n[%p]
0580: 20 42 69 6f 57 72 69 74 65 28 25 64 29 20 2d 3e BioWrite(%d) ->
0590: 20 25 64 20 5b 25 64 2e 25 64 5d 22 2c 0a 09 20 %d [%d.%d]",..
05a0: 20 20 20 28 76 6f 69 64 20 2a 29 20 63 68 61 6e (void *) chan
05b0: 2c 20 62 75 66 4c 65 6e 2c 20 72 65 74 2c 20 54 , bufLen, ret, T
05c0: 63 6c 5f 45 6f 66 28 63 68 61 6e 29 2c 20 54 63 cl_Eof(chan), Tc
05d0: 6c 5f 47 65 74 45 72 72 6e 6f 28 29 29 3b 0a 0a l_GetErrno());..
05e0: 20 20 20 20 42 49 4f 5f 63 6c 65 61 72 5f 66 6c BIO_clear_fl
05f0: 61 67 73 28 62 69 6f 2c 20 42 49 4f 5f 46 4c 41 ags(bio, BIO_FLA
0600: 47 53 5f 57 52 49 54 45 7c 42 49 4f 5f 46 4c 41 GS_WRITE|BIO_FLA
0610: 47 53 5f 53 48 4f 55 4c 44 5f 52 45 54 52 59 29 GS_SHOULD_RETRY)
0620: 3b 0a 0a 20 20 20 20 69 66 20 28 72 65 74 20 3d ;.. if (ret =
0630: 3d 20 30 29 20 7b 0a 09 69 66 20 28 21 54 63 6c = 0) {..if (!Tcl
0640: 5f 45 6f 66 28 63 68 61 6e 29 29 20 7b 0a 09 20 _Eof(chan)) {..
0650: 20 20 20 42 49 4f 5f 73 65 74 5f 72 65 74 72 79 BIO_set_retry
0660: 5f 77 72 69 74 65 28 62 69 6f 29 3b 0a 09 20 20 _write(bio);..
0670: 20 20 72 65 74 20 3d 20 2d 31 3b 0a 09 7d 0a 20 ret = -1;..}.
0680: 20 20 20 7d 0a 20 20 20 20 69 66 20 28 42 49 4f }. if (BIO
0690: 5f 73 68 6f 75 6c 64 5f 72 65 61 64 28 62 69 6f _should_read(bio
06a0: 29 29 20 7b 0a 09 42 49 4f 5f 73 65 74 5f 72 65 )) {..BIO_set_re
06b0: 74 72 79 5f 72 65 61 64 28 62 69 6f 29 3b 0a 20 try_read(bio);.
06c0: 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 }. return
06d0: 72 65 74 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 ret;.}..static i
06e0: 6e 74 0a 42 69 6f 52 65 61 64 20 28 62 69 6f 2c nt.BioRead (bio,
06f0: 20 62 75 66 2c 20 62 75 66 4c 65 6e 29 0a 20 20 buf, bufLen).
0700: 20 20 42 49 4f 20 2a 62 69 6f 3b 0a 20 20 20 20 BIO *bio;.
0710: 63 68 61 72 20 2a 62 75 66 3b 0a 20 20 20 20 69 char *buf;. i
0720: 6e 74 20 62 75 66 4c 65 6e 3b 0a 7b 0a 20 20 20 nt bufLen;.{.
0730: 20 54 63 6c 5f 43 68 61 6e 6e 65 6c 20 63 68 61 Tcl_Channel cha
0740: 6e 20 3d 20 54 6c 73 5f 47 65 74 50 61 72 65 6e n = Tls_GetParen
0750: 74 28 28 53 74 61 74 65 2a 29 62 69 6f 2d 3e 70 t((State*)bio->p
0760: 74 72 29 3b 0a 20 20 20 20 69 6e 74 20 72 65 74 tr);. int ret
0770: 20 3d 20 30 3b 0a 0a 20 20 20 20 64 70 72 69 6e = 0;.. dprin
0780: 74 66 28 73 74 64 65 72 72 2c 22 5c 6e 42 69 6f tf(stderr,"\nBio
0790: 52 65 61 64 28 25 70 2c 20 3c 62 75 66 3e 2c 20 Read(%p, <buf>,
07a0: 25 64 29 20 5b 25 70 5d 22 2c 0a 09 20 20 20 20 %d) [%p]",..
07b0: 28 76 6f 69 64 20 2a 29 20 62 69 6f 2c 20 62 75 (void *) bio, bu
07c0: 66 4c 65 6e 2c 20 28 76 6f 69 64 20 2a 29 20 63 fLen, (void *) c
07d0: 68 61 6e 29 3b 0a 0a 20 20 20 20 69 66 20 28 62 han);.. if (b
07e0: 75 66 20 3d 3d 20 4e 55 4c 4c 29 20 72 65 74 75 uf == NULL) retu
07f0: 72 6e 20 30 3b 0a 0a 20 20 20 20 69 66 20 28 63 rn 0;.. if (c
0800: 68 61 6e 6e 65 6c 54 79 70 65 56 65 72 73 69 6f hannelTypeVersio
0810: 6e 20 3d 3d 20 54 4c 53 5f 43 48 41 4e 4e 45 4c n == TLS_CHANNEL
0820: 5f 56 45 52 53 49 4f 4e 5f 32 29 20 7b 0a 09 72 _VERSION_2) {..r
0830: 65 74 20 3d 20 54 63 6c 5f 52 65 61 64 52 61 77 et = Tcl_ReadRaw
0840: 28 63 68 61 6e 2c 20 62 75 66 2c 20 62 75 66 4c (chan, buf, bufL
0850: 65 6e 29 3b 0a 20 20 20 20 7d 20 65 6c 73 65 20 en);. } else
0860: 7b 0a 09 72 65 74 20 3d 20 54 63 6c 5f 52 65 61 {..ret = Tcl_Rea
0870: 64 28 63 68 61 6e 2c 20 62 75 66 2c 20 62 75 66 d(chan, buf, buf
0880: 4c 65 6e 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 Len);. }..
0890: 20 64 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c dprintf(stderr,
08a0: 22 5c 6e 5b 25 70 5d 20 42 69 6f 52 65 61 64 28 "\n[%p] BioRead(
08b0: 25 64 29 20 2d 3e 20 25 64 20 5b 25 64 2e 25 64 %d) -> %d [%d.%d
08c0: 5d 22 2c 0a 09 20 20 20 20 28 76 6f 69 64 20 2a ]",.. (void *
08d0: 29 20 63 68 61 6e 2c 20 62 75 66 4c 65 6e 2c 20 ) chan, bufLen,
08e0: 72 65 74 2c 20 54 63 6c 5f 45 6f 66 28 63 68 61 ret, Tcl_Eof(cha
08f0: 6e 29 2c 20 54 63 6c 5f 47 65 74 45 72 72 6e 6f n), Tcl_GetErrno
0900: 28 29 29 3b 0a 0a 20 20 20 20 42 49 4f 5f 63 6c ());.. BIO_cl
0910: 65 61 72 5f 66 6c 61 67 73 28 62 69 6f 2c 20 42 ear_flags(bio, B
0920: 49 4f 5f 46 4c 41 47 53 5f 52 45 41 44 7c 42 49 IO_FLAGS_READ|BI
0930: 4f 5f 46 4c 41 47 53 5f 53 48 4f 55 4c 44 5f 52 O_FLAGS_SHOULD_R
0940: 45 54 52 59 29 3b 0a 0a 20 20 20 20 69 66 20 28 ETRY);.. if (
0950: 72 65 74 20 3d 3d 20 30 29 20 7b 0a 09 69 66 20 ret == 0) {..if
0960: 28 21 54 63 6c 5f 45 6f 66 28 63 68 61 6e 29 29 (!Tcl_Eof(chan))
0970: 20 7b 0a 09 20 20 20 20 42 49 4f 5f 73 65 74 5f {.. BIO_set_
0980: 72 65 74 72 79 5f 72 65 61 64 28 62 69 6f 29 3b retry_read(bio);
0990: 0a 09 20 20 20 20 72 65 74 20 3d 20 2d 31 3b 0a .. ret = -1;.
09a0: 09 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 20 .}. }. if
09b0: 28 42 49 4f 5f 73 68 6f 75 6c 64 5f 77 72 69 74 (BIO_should_writ
09c0: 65 28 62 69 6f 29 29 20 7b 0a 09 42 49 4f 5f 73 e(bio)) {..BIO_s
09d0: 65 74 5f 72 65 74 72 79 5f 77 72 69 74 65 28 62 et_retry_write(b
09e0: 69 6f 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 io);. }. r
09f0: 65 74 75 72 6e 20 72 65 74 3b 0a 7d 0a 0a 73 74 eturn ret;.}..st
0a00: 61 74 69 63 20 69 6e 74 0a 42 69 6f 50 75 74 73 atic int.BioPuts
0a10: 09 28 62 69 6f 2c 20 73 74 72 29 0a 20 20 20 20 .(bio, str).
0a20: 42 49 4f 20 2a 62 69 6f 3b 0a 20 20 20 20 43 4f BIO *bio;. CO
0a30: 4e 53 54 20 63 68 61 72 20 2a 73 74 72 3b 0a 7b NST char *str;.{
0a40: 0a 20 20 20 20 72 65 74 75 72 6e 20 42 69 6f 57 . return BioW
0a50: 72 69 74 65 28 62 69 6f 2c 20 73 74 72 2c 20 28 rite(bio, str, (
0a60: 69 6e 74 29 20 73 74 72 6c 65 6e 28 73 74 72 29 int) strlen(str)
0a70: 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 6c 6f 6e );.}..static lon
0a80: 67 0a 42 69 6f 43 74 72 6c 09 28 62 69 6f 2c 20 g.BioCtrl.(bio,
0a90: 63 6d 64 2c 20 6e 75 6d 2c 20 70 74 72 29 0a 20 cmd, num, ptr).
0aa0: 20 20 20 42 49 4f 20 2a 62 69 6f 3b 0a 20 20 20 BIO *bio;.
0ab0: 20 69 6e 74 20 63 6d 64 3b 0a 20 20 20 20 6c 6f int cmd;. lo
0ac0: 6e 67 20 6e 75 6d 3b 0a 20 20 20 20 76 6f 69 64 ng num;. void
0ad0: 20 2a 70 74 72 3b 0a 7b 0a 20 20 20 20 54 63 6c *ptr;.{. Tcl
0ae0: 5f 43 68 61 6e 6e 65 6c 20 63 68 61 6e 20 3d 20 _Channel chan =
0af0: 54 6c 73 5f 47 65 74 50 61 72 65 6e 74 28 28 53 Tls_GetParent((S
0b00: 74 61 74 65 2a 29 62 69 6f 2d 3e 70 74 72 29 3b tate*)bio->ptr);
0b10: 0a 20 20 20 20 6c 6f 6e 67 20 72 65 74 20 3d 20 . long ret =
0b20: 31 3b 0a 20 20 20 20 69 6e 74 20 2a 69 70 3b 0a 1;. int *ip;.
0b30: 0a 20 20 20 20 64 70 72 69 6e 74 66 28 73 74 64 . dprintf(std
0b40: 65 72 72 2c 22 5c 6e 42 69 6f 43 74 72 6c 28 25 err,"\nBioCtrl(%
0b50: 70 2c 20 30 78 25 78 2c 20 30 78 25 78 2c 20 25 p, 0x%x, 0x%x, %
0b60: 70 29 22 2c 0a 09 20 20 20 20 28 76 6f 69 64 20 p)",.. (void
0b70: 2a 29 20 62 69 6f 2c 20 28 75 6e 73 69 67 6e 65 *) bio, (unsigne
0b80: 64 20 69 6e 74 29 20 63 6d 64 2c 20 28 75 6e 73 d int) cmd, (uns
0b90: 69 67 6e 65 64 20 69 6e 74 29 20 6e 75 6d 2c 0a igned int) num,.
0ba0: 09 20 20 20 20 28 76 6f 69 64 20 2a 29 20 70 74 . (void *) pt
0bb0: 72 29 3b 0a 0a 20 20 20 20 73 77 69 74 63 68 20 r);.. switch
0bc0: 28 63 6d 64 29 20 7b 0a 20 20 20 20 63 61 73 65 (cmd) {. case
0bd0: 20 42 49 4f 5f 43 54 52 4c 5f 52 45 53 45 54 3a BIO_CTRL_RESET:
0be0: 0a 09 6e 75 6d 20 3d 20 30 3b 0a 20 20 20 20 63 ..num = 0;. c
0bf0: 61 73 65 20 42 49 4f 5f 43 5f 46 49 4c 45 5f 53 ase BIO_C_FILE_S
0c00: 45 45 4b 3a 0a 20 20 20 20 63 61 73 65 20 42 49 EEK:. case BI
0c10: 4f 5f 43 5f 46 49 4c 45 5f 54 45 4c 4c 3a 0a 09 O_C_FILE_TELL:..
0c20: 72 65 74 20 3d 20 30 3b 0a 09 62 72 65 61 6b 3b ret = 0;..break;
0c30: 0a 20 20 20 20 63 61 73 65 20 42 49 4f 5f 43 54 . case BIO_CT
0c40: 52 4c 5f 49 4e 46 4f 3a 0a 09 72 65 74 20 3d 20 RL_INFO:..ret =
0c50: 31 3b 0a 09 62 72 65 61 6b 3b 0a 20 20 20 20 63 1;..break;. c
0c60: 61 73 65 20 42 49 4f 5f 43 5f 53 45 54 5f 46 44 ase BIO_C_SET_FD
0c70: 3a 0a 09 42 69 6f 46 72 65 65 28 62 69 6f 29 3b :..BioFree(bio);
0c80: 0a 09 2f 2a 20 53 65 74 73 20 53 74 61 74 65 2a ../* Sets State*
0c90: 20 2a 2f 0a 09 62 69 6f 2d 3e 70 74 72 09 3d 20 */..bio->ptr.=
0ca0: 2a 28 28 63 68 61 72 20 2a 2a 29 70 74 72 29 3b *((char **)ptr);
0cb0: 0a 09 62 69 6f 2d 3e 73 68 75 74 64 6f 77 6e 09 ..bio->shutdown.
0cc0: 3d 20 28 69 6e 74 29 6e 75 6d 3b 0a 09 62 69 6f = (int)num;..bio
0cd0: 2d 3e 69 6e 69 74 09 3d 20 31 3b 0a 09 62 72 65 ->init.= 1;..bre
0ce0: 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 42 49 4f ak;. case BIO
0cf0: 5f 43 5f 47 45 54 5f 46 44 3a 0a 09 69 66 20 28 _C_GET_FD:..if (
0d00: 62 69 6f 2d 3e 69 6e 69 74 29 20 7b 0a 09 20 20 bio->init) {..
0d10: 20 20 69 70 20 3d 20 28 69 6e 74 20 2a 29 70 74 ip = (int *)pt
0d20: 72 3b 0a 09 20 20 20 20 69 66 20 28 69 70 20 21 r;.. if (ip !
0d30: 3d 20 4e 55 4c 4c 29 20 7b 0a 09 09 2a 69 70 20 = NULL) {...*ip
0d40: 3d 20 62 69 6f 2d 3e 6e 75 6d 3b 0a 09 20 20 20 = bio->num;..
0d50: 20 7d 0a 09 20 20 20 20 72 65 74 20 3d 20 62 69 }.. ret = bi
0d60: 6f 2d 3e 6e 75 6d 3b 0a 09 7d 20 65 6c 73 65 20 o->num;..} else
0d70: 7b 0a 09 20 20 20 20 72 65 74 20 3d 20 2d 31 3b {.. ret = -1;
0d80: 0a 09 7d 0a 09 62 72 65 61 6b 3b 0a 20 20 20 20 ..}..break;.
0d90: 63 61 73 65 20 42 49 4f 5f 43 54 52 4c 5f 47 45 case BIO_CTRL_GE
0da0: 54 5f 43 4c 4f 53 45 3a 0a 09 72 65 74 20 3d 20 T_CLOSE:..ret =
0db0: 62 69 6f 2d 3e 73 68 75 74 64 6f 77 6e 3b 0a 09 bio->shutdown;..
0dc0: 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 break;. case
0dd0: 42 49 4f 5f 43 54 52 4c 5f 53 45 54 5f 43 4c 4f BIO_CTRL_SET_CLO
0de0: 53 45 3a 0a 09 62 69 6f 2d 3e 73 68 75 74 64 6f SE:..bio->shutdo
0df0: 77 6e 20 3d 20 28 69 6e 74 29 6e 75 6d 3b 0a 09 wn = (int)num;..
0e00: 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 break;. case
0e10: 42 49 4f 5f 43 54 52 4c 5f 45 4f 46 3a 0a 09 64 BIO_CTRL_EOF:..d
0e20: 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c 20 22 printf(stderr, "
0e30: 42 49 4f 5f 43 54 52 4c 5f 45 4f 46 5c 6e 22 29 BIO_CTRL_EOF\n")
0e40: 3b 0a 09 72 65 74 20 3d 20 54 63 6c 5f 45 6f 66 ;..ret = Tcl_Eof
0e50: 28 63 68 61 6e 29 3b 0a 09 62 72 65 61 6b 3b 0a (chan);..break;.
0e60: 20 20 20 20 63 61 73 65 20 42 49 4f 5f 43 54 52 case BIO_CTR
0e70: 4c 5f 50 45 4e 44 49 4e 47 3a 0a 09 72 65 74 20 L_PENDING:..ret
0e80: 3d 20 28 54 63 6c 5f 49 6e 70 75 74 42 75 66 66 = (Tcl_InputBuff
0e90: 65 72 65 64 28 63 68 61 6e 29 20 3f 20 31 20 3a ered(chan) ? 1 :
0ea0: 20 30 29 3b 0a 09 64 70 72 69 6e 74 66 28 73 74 0);..dprintf(st
0eb0: 64 65 72 72 2c 20 22 42 49 4f 5f 43 54 52 4c 5f derr, "BIO_CTRL_
0ec0: 50 45 4e 44 49 4e 47 28 25 64 29 5c 6e 22 2c 20 PENDING(%d)\n",
0ed0: 28 69 6e 74 29 20 72 65 74 29 3b 0a 09 62 72 65 (int) ret);..bre
0ee0: 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 42 49 4f ak;. case BIO
0ef0: 5f 43 54 52 4c 5f 57 50 45 4e 44 49 4e 47 3a 0a _CTRL_WPENDING:.
0f00: 09 72 65 74 20 3d 20 30 3b 0a 09 62 72 65 61 6b .ret = 0;..break
0f10: 3b 0a 20 20 20 20 63 61 73 65 20 42 49 4f 5f 43 ;. case BIO_C
0f20: 54 52 4c 5f 44 55 50 3a 0a 09 62 72 65 61 6b 3b TRL_DUP:..break;
0f30: 0a 20 20 20 20 63 61 73 65 20 42 49 4f 5f 43 54 . case BIO_CT
0f40: 52 4c 5f 46 4c 55 53 48 3a 0a 09 64 70 72 69 6e RL_FLUSH:..dprin
0f50: 74 66 28 73 74 64 65 72 72 2c 20 22 42 49 4f 5f tf(stderr, "BIO_
0f60: 43 54 52 4c 5f 46 4c 55 53 48 5c 6e 22 29 3b 0a CTRL_FLUSH\n");.
0f70: 09 69 66 20 28 63 68 61 6e 6e 65 6c 54 79 70 65 .if (channelType
0f80: 56 65 72 73 69 6f 6e 20 3d 3d 20 54 4c 53 5f 43 Version == TLS_C
0f90: 48 41 4e 4e 45 4c 5f 56 45 52 53 49 4f 4e 5f 32 HANNEL_VERSION_2
0fa0: 29 20 7b 0a 09 20 20 20 20 72 65 74 20 3d 20 28 ) {.. ret = (
0fb0: 28 54 63 6c 5f 57 72 69 74 65 52 61 77 28 63 68 (Tcl_WriteRaw(ch
0fc0: 61 6e 2c 20 22 22 2c 20 30 29 20 3e 3d 20 30 29 an, "", 0) >= 0)
0fd0: 20 3f 20 31 20 3a 20 2d 31 29 3b 0a 09 7d 20 65 ? 1 : -1);..} e
0fe0: 6c 73 65 20 7b 0a 09 20 20 20 20 72 65 74 20 3d lse {.. ret =
0ff0: 20 28 28 54 63 6c 5f 46 6c 75 73 68 28 63 68 61 ((Tcl_Flush(cha
1000: 6e 29 20 3d 3d 20 54 43 4c 5f 4f 4b 29 20 3f 20 n) == TCL_OK) ?
1010: 31 20 3a 20 2d 31 29 3b 0a 09 7d 0a 09 62 72 65 1 : -1);..}..bre
1020: 61 6b 3b 0a 20 20 20 20 64 65 66 61 75 6c 74 3a ak;. default:
1030: 0a 09 72 65 74 20 3d 20 30 3b 0a 09 62 72 65 61 ..ret = 0;..brea
1040: 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 k;. }. ret
1050: 75 72 6e 28 72 65 74 29 3b 0a 7d 0a 0a 73 74 61 urn(ret);.}..sta
1060: 74 69 63 20 69 6e 74 0a 42 69 6f 4e 65 77 09 28 tic int.BioNew.(
1070: 62 69 6f 29 0a 20 20 20 20 42 49 4f 20 2a 62 69 bio). BIO *bi
1080: 6f 3b 0a 7b 0a 20 20 20 20 62 69 6f 2d 3e 69 6e o;.{. bio->in
1090: 69 74 09 3d 20 30 3b 0a 20 20 20 20 62 69 6f 2d it.= 0;. bio-
10a0: 3e 6e 75 6d 09 3d 20 30 3b 0a 20 20 20 20 62 69 >num.= 0;. bi
10b0: 6f 2d 3e 70 74 72 09 3d 20 4e 55 4c 4c 3b 0a 20 o->ptr.= NULL;.
10c0: 20 20 20 62 69 6f 2d 3e 66 6c 61 67 73 09 3d 20 bio->flags.=
10d0: 30 3b 0a 0a 20 20 20 20 72 65 74 75 72 6e 20 31 0;.. return 1
10e0: 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 0a ;.}..static int.
10f0: 42 69 6f 46 72 65 65 09 28 62 69 6f 29 0a 20 20 BioFree.(bio).
1100: 20 20 42 49 4f 20 2a 62 69 6f 3b 0a 7b 0a 20 20 BIO *bio;.{.
1110: 20 20 69 66 20 28 62 69 6f 20 3d 3d 20 4e 55 4c if (bio == NUL
1120: 4c 29 20 7b 0a 09 72 65 74 75 72 6e 20 30 3b 0a L) {..return 0;.
1130: 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 20 28 62 }.. if (b
1140: 69 6f 2d 3e 73 68 75 74 64 6f 77 6e 29 20 7b 0a io->shutdown) {.
1150: 09 69 66 20 28 62 69 6f 2d 3e 69 6e 69 74 29 20 .if (bio->init)
1160: 7b 0a 09 20 20 20 20 2f 2a 73 68 75 74 64 6f 77 {.. /*shutdow
1170: 6e 28 62 69 6f 2d 3e 6e 75 6d 2c 20 32 29 20 2a n(bio->num, 2) *
1180: 2f 0a 09 20 20 20 20 2f 2a 63 6c 6f 73 65 73 6f /.. /*closeso
1190: 63 6b 65 74 28 62 69 6f 2d 3e 6e 75 6d 29 20 2a cket(bio->num) *
11a0: 2f 0a 09 7d 0a 09 62 69 6f 2d 3e 69 6e 69 74 09 /..}..bio->init.
11b0: 3d 20 30 3b 0a 09 62 69 6f 2d 3e 66 6c 61 67 73 = 0;..bio->flags
11c0: 09 3d 20 30 3b 0a 09 62 69 6f 2d 3e 6e 75 6d 09 .= 0;..bio->num.
11d0: 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 = 0;. }. r
11e0: 65 74 75 72 6e 20 31 3b 0a 7d 0a eturn 1;.}.