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 23 69 e "tlsInt.h"..#i
0090: 66 64 65 66 20 54 43 4c 54 4c 53 5f 4f 50 45 4e fdef TCLTLS_OPEN
00a0: 53 53 4c 5f 50 52 45 5f 31 5f 31 5f 41 50 49 0a SSL_PRE_1_1_API.
00b0: 23 64 65 66 69 6e 65 20 42 49 4f 5f 67 65 74 5f #define BIO_get_
00c0: 64 61 74 61 28 62 69 6f 29 20 20 20 20 20 20 20 data(bio)
00d0: 20 20 20 20 20 20 20 20 20 28 28 62 69 6f 29 2d ((bio)-
00e0: 3e 70 74 72 29 0a 23 64 65 66 69 6e 65 20 42 49 >ptr).#define BI
00f0: 4f 5f 67 65 74 5f 69 6e 69 74 28 62 69 6f 29 20 O_get_init(bio)
0100: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 (
0110: 28 62 69 6f 29 2d 3e 69 6e 69 74 29 0a 23 64 65 (bio)->init).#de
0120: 66 69 6e 65 20 42 49 4f 5f 67 65 74 5f 73 68 75 fine BIO_get_shu
0130: 74 64 6f 77 6e 28 62 69 6f 29 20 20 20 20 20 20 tdown(bio)
0140: 20 20 20 20 20 20 28 28 62 69 6f 29 2d 3e 73 68 ((bio)->sh
0150: 75 74 64 6f 77 6e 29 0a 23 64 65 66 69 6e 65 20 utdown).#define
0160: 42 49 4f 5f 73 65 74 5f 64 61 74 61 28 62 69 6f BIO_set_data(bio
0170: 2c 20 76 61 6c 29 20 20 20 20 20 20 20 20 20 20 , val)
0180: 20 28 62 69 6f 29 2d 3e 70 74 72 20 3d 20 28 76 (bio)->ptr = (v
0190: 61 6c 29 0a 23 64 65 66 69 6e 65 20 42 49 4f 5f al).#define BIO_
01a0: 73 65 74 5f 69 6e 69 74 28 62 69 6f 2c 20 76 61 set_init(bio, va
01b0: 6c 29 20 20 20 20 20 20 20 20 20 20 20 28 62 69 l) (bi
01c0: 6f 29 2d 3e 69 6e 69 74 20 3d 20 28 76 61 6c 29 o)->init = (val)
01d0: 0a 23 64 65 66 69 6e 65 20 42 49 4f 5f 73 65 74 .#define BIO_set
01e0: 5f 73 68 75 74 64 6f 77 6e 28 62 69 6f 2c 20 76 _shutdown(bio, v
01f0: 61 6c 29 20 20 20 20 20 20 20 28 62 69 6f 29 2d al) (bio)-
0200: 3e 73 68 75 74 64 6f 77 6e 20 3d 20 28 76 61 6c >shutdown = (val
0210: 29 0a 0a 2f 2a 20 58 58 58 3a 20 54 68 69 73 20 )../* XXX: This
0220: 61 73 73 75 6d 65 73 20 74 68 65 20 76 61 72 69 assumes the vari
0230: 61 62 6c 65 20 62 65 69 6e 67 20 61 73 73 69 67 able being assig
0240: 6e 65 64 20 74 6f 20 69 73 20 42 69 6f 4d 65 74 ned to is BioMet
0250: 68 6f 64 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 hods */.#define
0260: 42 49 4f 5f 6d 65 74 68 5f 6e 65 77 28 74 79 70 BIO_meth_new(typ
0270: 65 5f 2c 20 6e 61 6d 65 5f 29 20 20 20 20 20 20 e_, name_)
0280: 20 28 42 49 4f 5f 4d 45 54 48 4f 44 20 2a 29 54 (BIO_METHOD *)T
0290: 63 6c 5f 41 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 cl_Alloc(sizeof(
02a0: 42 49 4f 5f 4d 45 54 48 4f 44 29 29 3b 20 5c 0a BIO_METHOD)); \.
02b0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
02c0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
02d0: 20 20 20 20 20 20 20 20 20 6d 65 6d 73 65 74 28 memset(
02e0: 42 69 6f 4d 65 74 68 6f 64 73 2c 20 30 2c 20 73 BioMethods, 0, s
02f0: 69 7a 65 6f 66 28 42 49 4f 5f 4d 45 54 48 4f 44 izeof(BIO_METHOD
0300: 29 29 3b 20 5c 0a 20 20 20 20 20 20 20 20 20 20 )); \.
0310: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
0320: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 42 B
0330: 69 6f 4d 65 74 68 6f 64 73 2d 3e 74 79 70 65 20 ioMethods->type
0340: 3d 20 74 79 70 65 5f 3b 20 5c 0a 20 20 20 20 20 = type_; \.
0350: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
0360: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
0370: 20 20 20 20 42 69 6f 4d 65 74 68 6f 64 73 2d 3e BioMethods->
0380: 6e 61 6d 65 20 3d 20 6e 61 6d 65 5f 3b 0a 23 64 name = name_;.#d
0390: 65 66 69 6e 65 20 42 49 4f 5f 6d 65 74 68 5f 73 efine BIO_meth_s
03a0: 65 74 5f 77 72 69 74 65 28 62 69 6f 2c 20 76 61 et_write(bio, va
03b0: 6c 29 20 20 20 20 20 28 62 69 6f 29 2d 3e 62 77 l) (bio)->bw
03c0: 72 69 74 65 20 3d 20 76 61 6c 3b 0a 23 64 65 66 rite = val;.#def
03d0: 69 6e 65 20 42 49 4f 5f 6d 65 74 68 5f 73 65 74 ine BIO_meth_set
03e0: 5f 72 65 61 64 28 62 69 6f 2c 20 76 61 6c 29 20 _read(bio, val)
03f0: 20 20 20 20 20 28 62 69 6f 29 2d 3e 62 72 65 61 (bio)->brea
0400: 64 20 3d 20 76 61 6c 3b 0a 23 64 65 66 69 6e 65 d = val;.#define
0410: 20 42 49 4f 5f 6d 65 74 68 5f 73 65 74 5f 70 75 BIO_meth_set_pu
0420: 74 73 28 62 69 6f 2c 20 76 61 6c 29 20 20 20 20 ts(bio, val)
0430: 20 20 28 62 69 6f 29 2d 3e 62 70 75 74 73 20 3d (bio)->bputs =
0440: 20 76 61 6c 3b 0a 23 64 65 66 69 6e 65 20 42 49 val;.#define BI
0450: 4f 5f 6d 65 74 68 5f 73 65 74 5f 63 74 72 6c 28 O_meth_set_ctrl(
0460: 62 69 6f 2c 20 76 61 6c 29 20 20 20 20 20 20 28 bio, val) (
0470: 62 69 6f 29 2d 3e 63 74 72 6c 20 3d 20 76 61 6c bio)->ctrl = val
0480: 3b 0a 23 64 65 66 69 6e 65 20 42 49 4f 5f 6d 65 ;.#define BIO_me
0490: 74 68 5f 73 65 74 5f 63 72 65 61 74 65 28 62 69 th_set_create(bi
04a0: 6f 2c 20 76 61 6c 29 20 20 20 20 28 62 69 6f 29 o, val) (bio)
04b0: 2d 3e 63 72 65 61 74 65 20 3d 20 76 61 6c 3b 0a ->create = val;.
04c0: 23 64 65 66 69 6e 65 20 42 49 4f 5f 6d 65 74 68 #define BIO_meth
04d0: 5f 73 65 74 5f 64 65 73 74 72 6f 79 28 62 69 6f _set_destroy(bio
04e0: 2c 20 76 61 6c 29 20 20 20 28 62 69 6f 29 2d 3e , val) (bio)->
04f0: 64 65 73 74 72 6f 79 20 3d 20 76 61 6c 3b 0a 23 destroy = val;.#
0500: 65 6e 64 69 66 0a 0a 2f 2a 0a 20 2a 20 46 6f 72 endif../*. * For
0510: 77 61 72 64 20 64 65 63 6c 61 72 61 74 69 6f 6e ward declaration
0520: 73 0a 20 2a 2f 0a 0a 73 74 61 74 69 63 20 69 6e s. */..static in
0530: 74 20 42 69 6f 57 72 69 74 65 09 5f 41 4e 53 49 t BioWrite._ANSI
0540: 5f 41 52 47 53 5f 20 28 28 42 49 4f 20 2a 68 2c _ARGS_ ((BIO *h,
0550: 20 43 4f 4e 53 54 20 63 68 61 72 20 2a 62 75 66 CONST char *buf
0560: 2c 20 69 6e 74 20 6e 75 6d 29 29 3b 0a 73 74 61 , int num));.sta
0570: 74 69 63 20 69 6e 74 20 42 69 6f 52 65 61 64 09 tic int BioRead.
0580: 5f 41 4e 53 49 5f 41 52 47 53 5f 20 28 28 42 49 _ANSI_ARGS_ ((BI
0590: 4f 20 2a 68 2c 20 63 68 61 72 20 2a 62 75 66 2c O *h, char *buf,
05a0: 20 69 6e 74 20 6e 75 6d 29 29 3b 0a 73 74 61 74 int num));.stat
05b0: 69 63 20 69 6e 74 20 42 69 6f 50 75 74 73 09 5f ic int BioPuts._
05c0: 41 4e 53 49 5f 41 52 47 53 5f 20 28 28 42 49 4f ANSI_ARGS_ ((BIO
05d0: 20 2a 68 2c 20 43 4f 4e 53 54 20 63 68 61 72 20 *h, CONST char
05e0: 2a 73 74 72 29 29 3b 0a 73 74 61 74 69 63 20 6c *str));.static l
05f0: 6f 6e 67 20 42 69 6f 43 74 72 6c 09 5f 41 4e 53 ong BioCtrl._ANS
0600: 49 5f 41 52 47 53 5f 20 28 28 42 49 4f 20 2a 68 I_ARGS_ ((BIO *h
0610: 2c 20 69 6e 74 20 63 6d 64 2c 20 6c 6f 6e 67 20 , int cmd, long
0620: 61 72 67 31 2c 20 76 6f 69 64 20 2a 70 74 72 29 arg1, void *ptr)
0630: 29 3b 0a 73 74 61 74 69 63 20 69 6e 74 20 42 69 );.static int Bi
0640: 6f 4e 65 77 09 5f 41 4e 53 49 5f 41 52 47 53 5f oNew._ANSI_ARGS_
0650: 20 28 28 42 49 4f 20 2a 68 29 29 3b 0a 73 74 61 ((BIO *h));.sta
0660: 74 69 63 20 69 6e 74 20 42 69 6f 46 72 65 65 09 tic int BioFree.
0670: 5f 41 4e 53 49 5f 41 52 47 53 5f 20 28 28 42 49 _ANSI_ARGS_ ((BI
0680: 4f 20 2a 68 29 29 3b 0a 0a 42 49 4f 20 2a 20 42 O *h));..BIO * B
0690: 49 4f 5f 6e 65 77 5f 74 63 6c 28 53 74 61 74 65 IO_new_tcl(State
06a0: 20 2a 73 74 61 74 65 50 74 72 2c 20 69 6e 74 20 *statePtr, int
06b0: 66 6c 61 67 73 29 20 7b 0a 20 20 20 20 42 49 4f flags) {. BIO
06c0: 20 2a 62 69 6f 3b 0a 20 20 20 20 73 74 61 74 69 *bio;. stati
06d0: 63 20 42 49 4f 5f 4d 45 54 48 4f 44 20 2a 42 69 c BIO_METHOD *Bi
06e0: 6f 4d 65 74 68 6f 64 73 20 3d 20 4e 55 4c 4c 3b oMethods = NULL;
06f0: 0a 0a 20 20 20 20 64 70 72 69 6e 74 66 28 22 42 .. dprintf("B
0700: 49 4f 5f 6e 65 77 5f 74 63 6c 28 29 20 63 61 6c IO_new_tcl() cal
0710: 6c 65 64 22 29 3b 0a 0a 20 20 20 20 69 66 20 28 led");.. if (
0720: 42 69 6f 4d 65 74 68 6f 64 73 20 3d 3d 20 4e 55 BioMethods == NU
0730: 4c 4c 29 20 7b 0a 20 20 20 20 20 20 20 20 42 69 LL) {. Bi
0740: 6f 4d 65 74 68 6f 64 73 20 3d 20 42 49 4f 5f 6d oMethods = BIO_m
0750: 65 74 68 5f 6e 65 77 28 42 49 4f 5f 54 59 50 45 eth_new(BIO_TYPE
0760: 5f 54 43 4c 2c 20 22 74 63 6c 22 29 3b 0a 20 20 _TCL, "tcl");.
0770: 20 20 20 20 20 20 42 49 4f 5f 6d 65 74 68 5f 73 BIO_meth_s
0780: 65 74 5f 77 72 69 74 65 28 42 69 6f 4d 65 74 68 et_write(BioMeth
0790: 6f 64 73 2c 20 42 69 6f 57 72 69 74 65 29 3b 0a ods, BioWrite);.
07a0: 20 20 20 20 20 20 20 20 42 49 4f 5f 6d 65 74 68 BIO_meth
07b0: 5f 73 65 74 5f 72 65 61 64 28 42 69 6f 4d 65 74 _set_read(BioMet
07c0: 68 6f 64 73 2c 20 42 69 6f 52 65 61 64 29 3b 0a hods, BioRead);.
07d0: 20 20 20 20 20 20 20 20 42 49 4f 5f 6d 65 74 68 BIO_meth
07e0: 5f 73 65 74 5f 70 75 74 73 28 42 69 6f 4d 65 74 _set_puts(BioMet
07f0: 68 6f 64 73 2c 20 42 69 6f 50 75 74 73 29 3b 0a hods, BioPuts);.
0800: 20 20 20 20 20 20 20 20 42 49 4f 5f 6d 65 74 68 BIO_meth
0810: 5f 73 65 74 5f 63 74 72 6c 28 42 69 6f 4d 65 74 _set_ctrl(BioMet
0820: 68 6f 64 73 2c 20 42 69 6f 43 74 72 6c 29 3b 0a hods, BioCtrl);.
0830: 20 20 20 20 20 20 20 20 42 49 4f 5f 6d 65 74 68 BIO_meth
0840: 5f 73 65 74 5f 63 72 65 61 74 65 28 42 69 6f 4d _set_create(BioM
0850: 65 74 68 6f 64 73 2c 20 42 69 6f 4e 65 77 29 3b ethods, BioNew);
0860: 0a 20 20 20 20 20 20 20 20 42 49 4f 5f 6d 65 74 . BIO_met
0870: 68 5f 73 65 74 5f 64 65 73 74 72 6f 79 28 42 69 h_set_destroy(Bi
0880: 6f 4d 65 74 68 6f 64 73 2c 20 42 69 6f 46 72 65 oMethods, BioFre
0890: 65 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 62 e);. }.. b
08a0: 69 6f 20 3d 20 42 49 4f 5f 6e 65 77 28 42 69 6f io = BIO_new(Bio
08b0: 4d 65 74 68 6f 64 73 29 3b 0a 0a 20 20 20 20 42 Methods);.. B
08c0: 49 4f 5f 73 65 74 5f 64 61 74 61 28 62 69 6f 2c IO_set_data(bio,
08d0: 20 73 74 61 74 65 50 74 72 29 3b 0a 20 20 20 20 statePtr);.
08e0: 42 49 4f 5f 73 65 74 5f 69 6e 69 74 28 62 69 6f BIO_set_init(bio
08f0: 2c 20 31 29 3b 0a 20 20 20 20 42 49 4f 5f 73 65 , 1);. BIO_se
0900: 74 5f 73 68 75 74 64 6f 77 6e 28 62 69 6f 2c 20 t_shutdown(bio,
0910: 66 6c 61 67 73 29 3b 0a 0a 20 20 20 20 72 65 74 flags);.. ret
0920: 75 72 6e 20 62 69 6f 3b 0a 7d 0a 0a 73 74 61 74 urn bio;.}..stat
0930: 69 63 20 69 6e 74 20 42 69 6f 57 72 69 74 65 28 ic int BioWrite(
0940: 42 49 4f 20 2a 62 69 6f 2c 20 43 4f 4e 53 54 20 BIO *bio, CONST
0950: 63 68 61 72 20 2a 62 75 66 2c 20 69 6e 74 20 62 char *buf, int b
0960: 75 66 4c 65 6e 29 20 7b 0a 20 20 20 20 54 63 6c ufLen) {. Tcl
0970: 5f 43 68 61 6e 6e 65 6c 20 63 68 61 6e 20 3d 20 _Channel chan =
0980: 54 6c 73 5f 47 65 74 50 61 72 65 6e 74 28 28 53 Tls_GetParent((S
0990: 74 61 74 65 2a 29 42 49 4f 5f 67 65 74 5f 64 61 tate*)BIO_get_da
09a0: 74 61 28 62 69 6f 29 29 3b 0a 20 20 20 20 69 6e ta(bio));. in
09b0: 74 20 72 65 74 3b 0a 0a 20 20 20 20 64 70 72 69 t ret;.. dpri
09c0: 6e 74 66 28 22 42 69 6f 57 72 69 74 65 28 25 70 ntf("BioWrite(%p
09d0: 2c 20 3c 62 75 66 3e 2c 20 25 64 29 20 5b 25 70 , <buf>, %d) [%p
09e0: 5d 22 2c 0a 09 20 20 20 20 28 76 6f 69 64 20 2a ]",.. (void *
09f0: 29 20 62 69 6f 2c 20 62 75 66 4c 65 6e 2c 20 28 ) bio, bufLen, (
0a00: 76 6f 69 64 20 2a 29 20 63 68 61 6e 29 3b 0a 0a void *) chan);..
0a10: 20 20 20 20 69 66 20 28 63 68 61 6e 6e 65 6c 54 if (channelT
0a20: 79 70 65 56 65 72 73 69 6f 6e 20 3d 3d 20 54 4c ypeVersion == TL
0a30: 53 5f 43 48 41 4e 4e 45 4c 5f 56 45 52 53 49 4f S_CHANNEL_VERSIO
0a40: 4e 5f 32 29 20 7b 0a 09 72 65 74 20 3d 20 54 63 N_2) {..ret = Tc
0a50: 6c 5f 57 72 69 74 65 52 61 77 28 63 68 61 6e 2c l_WriteRaw(chan,
0a60: 20 62 75 66 2c 20 62 75 66 4c 65 6e 29 3b 0a 20 buf, bufLen);.
0a70: 20 20 20 7d 20 65 6c 73 65 20 7b 0a 09 72 65 74 } else {..ret
0a80: 20 3d 20 54 63 6c 5f 57 72 69 74 65 28 63 68 61 = Tcl_Write(cha
0a90: 6e 2c 20 62 75 66 2c 20 62 75 66 4c 65 6e 29 3b n, buf, bufLen);
0aa0: 0a 20 20 20 20 7d 0a 0a 20 20 20 20 64 70 72 69 . }.. dpri
0ab0: 6e 74 66 28 22 5b 25 70 5d 20 42 69 6f 57 72 69 ntf("[%p] BioWri
0ac0: 74 65 28 25 64 29 20 2d 3e 20 25 64 20 5b 25 64 te(%d) -> %d [%d
0ad0: 2e 25 64 5d 22 2c 0a 09 20 20 20 20 28 76 6f 69 .%d]",.. (voi
0ae0: 64 20 2a 29 20 63 68 61 6e 2c 20 62 75 66 4c 65 d *) chan, bufLe
0af0: 6e 2c 20 72 65 74 2c 20 54 63 6c 5f 45 6f 66 28 n, ret, Tcl_Eof(
0b00: 63 68 61 6e 29 2c 20 54 63 6c 5f 47 65 74 45 72 chan), Tcl_GetEr
0b10: 72 6e 6f 28 29 29 3b 0a 0a 20 20 20 20 42 49 4f rno());.. BIO
0b20: 5f 63 6c 65 61 72 5f 66 6c 61 67 73 28 62 69 6f _clear_flags(bio
0b30: 2c 20 42 49 4f 5f 46 4c 41 47 53 5f 57 52 49 54 , BIO_FLAGS_WRIT
0b40: 45 7c 42 49 4f 5f 46 4c 41 47 53 5f 53 48 4f 55 E|BIO_FLAGS_SHOU
0b50: 4c 44 5f 52 45 54 52 59 29 3b 0a 0a 20 20 20 20 LD_RETRY);..
0b60: 69 66 20 28 72 65 74 20 3d 3d 20 30 29 20 7b 0a if (ret == 0) {.
0b70: 09 69 66 20 28 21 54 63 6c 5f 45 6f 66 28 63 68 .if (!Tcl_Eof(ch
0b80: 61 6e 29 29 20 7b 0a 09 20 20 20 20 42 49 4f 5f an)) {.. BIO_
0b90: 73 65 74 5f 72 65 74 72 79 5f 77 72 69 74 65 28 set_retry_write(
0ba0: 62 69 6f 29 3b 0a 09 20 20 20 20 72 65 74 20 3d bio);.. ret =
0bb0: 20 2d 31 3b 0a 09 7d 0a 20 20 20 20 7d 0a 20 20 -1;..}. }.
0bc0: 20 20 69 66 20 28 42 49 4f 5f 73 68 6f 75 6c 64 if (BIO_should
0bd0: 5f 72 65 61 64 28 62 69 6f 29 29 20 7b 0a 09 42 _read(bio)) {..B
0be0: 49 4f 5f 73 65 74 5f 72 65 74 72 79 5f 72 65 61 IO_set_retry_rea
0bf0: 64 28 62 69 6f 29 3b 0a 20 20 20 20 7d 0a 20 20 d(bio);. }.
0c00: 20 20 72 65 74 75 72 6e 20 72 65 74 3b 0a 7d 0a return ret;.}.
0c10: 0a 73 74 61 74 69 63 20 69 6e 74 20 42 69 6f 52 .static int BioR
0c20: 65 61 64 28 42 49 4f 20 2a 62 69 6f 2c 20 63 68 ead(BIO *bio, ch
0c30: 61 72 20 2a 62 75 66 2c 20 69 6e 74 20 62 75 66 ar *buf, int buf
0c40: 4c 65 6e 29 20 7b 0a 20 20 20 20 54 63 6c 5f 43 Len) {. Tcl_C
0c50: 68 61 6e 6e 65 6c 20 63 68 61 6e 20 3d 20 54 6c hannel chan = Tl
0c60: 73 5f 47 65 74 50 61 72 65 6e 74 28 28 53 74 61 s_GetParent((Sta
0c70: 74 65 2a 29 42 49 4f 5f 67 65 74 5f 64 61 74 61 te*)BIO_get_data
0c80: 28 62 69 6f 29 29 3b 0a 20 20 20 20 69 6e 74 20 (bio));. int
0c90: 72 65 74 20 3d 20 30 3b 0a 20 20 20 20 69 6e 74 ret = 0;. int
0ca0: 20 74 63 6c 45 6f 66 43 68 61 6e 3b 0a 0a 20 20 tclEofChan;..
0cb0: 20 20 64 70 72 69 6e 74 66 28 22 42 69 6f 52 65 dprintf("BioRe
0cc0: 61 64 28 25 70 2c 20 3c 62 75 66 3e 2c 20 25 64 ad(%p, <buf>, %d
0cd0: 29 20 5b 25 70 5d 22 2c 20 28 76 6f 69 64 20 2a ) [%p]", (void *
0ce0: 29 20 62 69 6f 2c 20 62 75 66 4c 65 6e 2c 20 28 ) bio, bufLen, (
0cf0: 76 6f 69 64 20 2a 29 20 63 68 61 6e 29 3b 0a 0a void *) chan);..
0d00: 20 20 20 20 69 66 20 28 62 75 66 20 3d 3d 20 4e if (buf == N
0d10: 55 4c 4c 29 20 72 65 74 75 72 6e 20 30 3b 0a 0a ULL) return 0;..
0d20: 20 20 20 20 69 66 20 28 63 68 61 6e 6e 65 6c 54 if (channelT
0d30: 79 70 65 56 65 72 73 69 6f 6e 20 3d 3d 20 54 4c ypeVersion == TL
0d40: 53 5f 43 48 41 4e 4e 45 4c 5f 56 45 52 53 49 4f S_CHANNEL_VERSIO
0d50: 4e 5f 32 29 20 7b 0a 09 72 65 74 20 3d 20 54 63 N_2) {..ret = Tc
0d60: 6c 5f 52 65 61 64 52 61 77 28 63 68 61 6e 2c 20 l_ReadRaw(chan,
0d70: 62 75 66 2c 20 62 75 66 4c 65 6e 29 3b 0a 20 20 buf, bufLen);.
0d80: 20 20 7d 20 65 6c 73 65 20 7b 0a 09 72 65 74 20 } else {..ret
0d90: 3d 20 54 63 6c 5f 52 65 61 64 28 63 68 61 6e 2c = Tcl_Read(chan,
0da0: 20 62 75 66 2c 20 62 75 66 4c 65 6e 29 3b 0a 20 buf, bufLen);.
0db0: 20 20 20 7d 0a 0a 20 20 20 20 74 63 6c 45 6f 66 }.. tclEof
0dc0: 43 68 61 6e 20 3d 20 54 63 6c 5f 45 6f 66 28 63 Chan = Tcl_Eof(c
0dd0: 68 61 6e 29 3b 0a 0a 20 20 20 20 64 70 72 69 6e han);.. dprin
0de0: 74 66 28 22 5b 25 70 5d 20 42 69 6f 52 65 61 64 tf("[%p] BioRead
0df0: 28 25 64 29 20 2d 3e 20 25 64 20 5b 74 63 6c 45 (%d) -> %d [tclE
0e00: 6f 66 3d 25 64 3b 20 74 63 6c 45 72 72 6e 6f 3d of=%d; tclErrno=
0e10: 25 64 5d 22 2c 0a 09 20 20 20 20 28 76 6f 69 64 %d]",.. (void
0e20: 20 2a 29 20 63 68 61 6e 2c 20 62 75 66 4c 65 6e *) chan, bufLen
0e30: 2c 20 72 65 74 2c 20 74 63 6c 45 6f 66 43 68 61 , ret, tclEofCha
0e40: 6e 2c 20 54 63 6c 5f 47 65 74 45 72 72 6e 6f 28 n, Tcl_GetErrno(
0e50: 29 29 3b 0a 0a 20 20 20 20 42 49 4f 5f 63 6c 65 ));.. BIO_cle
0e60: 61 72 5f 66 6c 61 67 73 28 62 69 6f 2c 20 42 49 ar_flags(bio, BI
0e70: 4f 5f 46 4c 41 47 53 5f 52 45 41 44 7c 42 49 4f O_FLAGS_READ|BIO
0e80: 5f 46 4c 41 47 53 5f 53 48 4f 55 4c 44 5f 52 45 _FLAGS_SHOULD_RE
0e90: 54 52 59 29 3b 0a 0a 20 20 20 20 69 66 20 28 72 TRY);.. if (r
0ea0: 65 74 20 3d 3d 20 30 29 20 7b 0a 09 69 66 20 28 et == 0) {..if (
0eb0: 21 74 63 6c 45 6f 66 43 68 61 6e 29 20 7b 0a 20 !tclEofChan) {.
0ec0: 20 20 20 20 20 20 20 20 20 20 20 64 70 72 69 6e dprin
0ed0: 74 66 28 22 47 6f 74 20 30 20 66 72 6f 6d 20 54 tf("Got 0 from T
0ee0: 63 6c 5f 52 65 61 64 20 6f 72 20 54 63 6c 5f 52 cl_Read or Tcl_R
0ef0: 65 61 64 52 61 77 2c 20 61 6e 64 20 45 4f 46 20 eadRaw, and EOF
0f00: 69 73 20 6e 6f 74 20 73 65 74 20 2d 2d 20 72 65 is not set -- re
0f10: 74 20 3d 3d 20 2d 31 20 6e 6f 77 22 29 3b 0a 09 t == -1 now");..
0f20: 20 20 20 20 42 49 4f 5f 73 65 74 5f 72 65 74 72 BIO_set_retr
0f30: 79 5f 72 65 61 64 28 62 69 6f 29 3b 0a 09 20 20 y_read(bio);..
0f40: 20 20 72 65 74 20 3d 20 2d 31 3b 0a 09 7d 20 65 ret = -1;..} e
0f50: 6c 73 65 20 7b 0a 20 20 20 20 20 20 20 20 20 20 lse {.
0f60: 20 20 64 70 72 69 6e 74 66 28 22 47 6f 74 20 30 dprintf("Got 0
0f70: 20 66 72 6f 6d 20 54 63 6c 5f 52 65 61 64 20 6f from Tcl_Read o
0f80: 72 20 54 63 6c 5f 52 65 61 64 52 61 77 2c 20 61 r Tcl_ReadRaw, a
0f90: 6e 64 20 45 4f 46 20 69 73 20 73 65 74 22 29 3b nd EOF is set");
0fa0: 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 7d . }. }
0fb0: 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 20 20 20 else {.
0fc0: 64 70 72 69 6e 74 66 28 22 47 6f 74 20 6e 6f 6e dprintf("Got non
0fd0: 2d 7a 65 72 6f 20 66 72 6f 6d 20 54 63 6c 5f 52 -zero from Tcl_R
0fe0: 65 61 64 20 6f 72 20 54 63 6c 5f 52 65 61 64 52 ead or Tcl_ReadR
0ff0: 61 77 20 3d 3d 20 72 65 74 20 3d 3d 20 25 69 22 aw == ret == %i"
1000: 2c 20 72 65 74 29 3b 0a 20 20 20 20 7d 0a 20 20 , ret);. }.
1010: 20 20 69 66 20 28 42 49 4f 5f 73 68 6f 75 6c 64 if (BIO_should
1020: 5f 77 72 69 74 65 28 62 69 6f 29 29 20 7b 0a 09 _write(bio)) {..
1030: 42 49 4f 5f 73 65 74 5f 72 65 74 72 79 5f 77 72 BIO_set_retry_wr
1040: 69 74 65 28 62 69 6f 29 3b 0a 20 20 20 20 7d 0a ite(bio);. }.
1050: 0a 20 20 20 20 64 70 72 69 6e 74 66 28 22 42 69 . dprintf("Bi
1060: 6f 52 65 61 64 28 25 70 2c 20 3c 62 75 66 3e 2c oRead(%p, <buf>,
1070: 20 25 64 29 20 5b 25 70 5d 20 72 65 74 75 72 6e %d) [%p] return
1080: 69 6e 67 20 25 69 22 2c 20 28 76 6f 69 64 20 2a ing %i", (void *
1090: 29 20 62 69 6f 2c 20 62 75 66 4c 65 6e 2c 20 28 ) bio, bufLen, (
10a0: 76 6f 69 64 20 2a 29 20 63 68 61 6e 2c 20 72 65 void *) chan, re
10b0: 74 29 3b 0a 0a 20 20 20 20 72 65 74 75 72 6e 20 t);.. return
10c0: 72 65 74 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 ret;.}..static i
10d0: 6e 74 20 42 69 6f 50 75 74 73 28 42 49 4f 20 2a nt BioPuts(BIO *
10e0: 62 69 6f 2c 20 43 4f 4e 53 54 20 63 68 61 72 20 bio, CONST char
10f0: 2a 73 74 72 29 20 7b 0a 20 20 20 20 64 70 72 69 *str) {. dpri
1100: 6e 74 66 28 22 42 69 6f 50 75 74 73 28 25 70 2c ntf("BioPuts(%p,
1110: 20 3c 73 74 72 69 6e 67 3a 25 70 3e 29 20 63 61 <string:%p>) ca
1120: 6c 6c 65 64 22 2c 20 62 69 6f 2c 20 73 74 72 29 lled", bio, str)
1130: 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 42 69 6f ;. return Bio
1140: 57 72 69 74 65 28 62 69 6f 2c 20 73 74 72 2c 20 Write(bio, str,
1150: 28 69 6e 74 29 20 73 74 72 6c 65 6e 28 73 74 72 (int) strlen(str
1160: 29 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 6c 6f ));.}..static lo
1170: 6e 67 20 42 69 6f 43 74 72 6c 28 42 49 4f 20 2a ng BioCtrl(BIO *
1180: 62 69 6f 2c 20 69 6e 74 20 63 6d 64 2c 20 6c 6f bio, int cmd, lo
1190: 6e 67 20 6e 75 6d 2c 20 76 6f 69 64 20 2a 70 74 ng num, void *pt
11a0: 72 29 20 7b 0a 20 20 20 20 54 63 6c 5f 43 68 61 r) {. Tcl_Cha
11b0: 6e 6e 65 6c 20 63 68 61 6e 20 3d 20 54 6c 73 5f nnel chan = Tls_
11c0: 47 65 74 50 61 72 65 6e 74 28 28 53 74 61 74 65 GetParent((State
11d0: 2a 29 42 49 4f 5f 67 65 74 5f 64 61 74 61 28 62 *)BIO_get_data(b
11e0: 69 6f 29 29 3b 0a 20 20 20 20 6c 6f 6e 67 20 72 io));. long r
11f0: 65 74 20 3d 20 31 3b 0a 0a 20 20 20 20 64 70 72 et = 1;.. dpr
1200: 69 6e 74 66 28 22 42 69 6f 43 74 72 6c 28 25 70 intf("BioCtrl(%p
1210: 2c 20 30 78 25 78 2c 20 30 78 25 78 2c 20 25 70 , 0x%x, 0x%x, %p
1220: 29 22 2c 0a 09 20 20 20 20 28 76 6f 69 64 20 2a )",.. (void *
1230: 29 20 62 69 6f 2c 20 28 75 6e 73 69 67 6e 65 64 ) bio, (unsigned
1240: 20 69 6e 74 29 20 63 6d 64 2c 20 28 75 6e 73 69 int) cmd, (unsi
1250: 67 6e 65 64 20 69 6e 74 29 20 6e 75 6d 2c 0a 09 gned int) num,..
1260: 20 20 20 20 28 76 6f 69 64 20 2a 29 20 70 74 72 (void *) ptr
1270: 29 3b 0a 0a 20 20 20 20 73 77 69 74 63 68 20 28 );.. switch (
1280: 63 6d 64 29 20 7b 0a 20 20 20 20 63 61 73 65 20 cmd) {. case
1290: 42 49 4f 5f 43 54 52 4c 5f 52 45 53 45 54 3a 0a BIO_CTRL_RESET:.
12a0: 09 6e 75 6d 20 3d 20 30 3b 0a 20 20 20 20 63 61 .num = 0;. ca
12b0: 73 65 20 42 49 4f 5f 43 5f 46 49 4c 45 5f 53 45 se BIO_C_FILE_SE
12c0: 45 4b 3a 0a 20 20 20 20 63 61 73 65 20 42 49 4f EK:. case BIO
12d0: 5f 43 5f 46 49 4c 45 5f 54 45 4c 4c 3a 0a 09 72 _C_FILE_TELL:..r
12e0: 65 74 20 3d 20 30 3b 0a 09 62 72 65 61 6b 3b 0a et = 0;..break;.
12f0: 20 20 20 20 63 61 73 65 20 42 49 4f 5f 43 54 52 case BIO_CTR
1300: 4c 5f 49 4e 46 4f 3a 0a 09 72 65 74 20 3d 20 31 L_INFO:..ret = 1
1310: 3b 0a 09 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 ;..break;. ca
1320: 73 65 20 42 49 4f 5f 43 5f 53 45 54 5f 46 44 3a se BIO_C_SET_FD:
1330: 0a 20 20 20 20 20 20 20 20 64 70 72 69 6e 74 66 . dprintf
1340: 28 22 55 6e 73 75 70 70 6f 72 74 65 64 20 63 61 ("Unsupported ca
1350: 6c 6c 3a 20 42 49 4f 5f 43 5f 53 45 54 5f 46 44 ll: BIO_C_SET_FD
1360: 22 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 20 ");. ret
1370: 3d 20 2d 31 3b 0a 20 20 20 20 20 20 20 20 62 72 = -1;. br
1380: 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 42 49 eak;. case BI
1390: 4f 5f 43 5f 47 45 54 5f 46 44 3a 0a 20 20 20 20 O_C_GET_FD:.
13a0: 20 20 20 20 64 70 72 69 6e 74 66 28 22 55 6e 73 dprintf("Uns
13b0: 75 70 70 6f 72 74 65 64 20 63 61 6c 6c 3a 20 42 upported call: B
13c0: 49 4f 5f 43 5f 47 45 54 5f 46 44 22 29 3b 0a 20 IO_C_GET_FD");.
13d0: 20 20 20 20 20 20 20 72 65 74 20 3d 20 2d 31 3b ret = -1;
13e0: 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a . break;.
13f0: 20 20 20 20 63 61 73 65 20 42 49 4f 5f 43 54 52 case BIO_CTR
1400: 4c 5f 47 45 54 5f 43 4c 4f 53 45 3a 0a 09 72 65 L_GET_CLOSE:..re
1410: 74 20 3d 20 42 49 4f 5f 67 65 74 5f 73 68 75 74 t = BIO_get_shut
1420: 64 6f 77 6e 28 62 69 6f 29 3b 0a 09 62 72 65 61 down(bio);..brea
1430: 6b 3b 0a 20 20 20 20 63 61 73 65 20 42 49 4f 5f k;. case BIO_
1440: 43 54 52 4c 5f 53 45 54 5f 43 4c 4f 53 45 3a 0a CTRL_SET_CLOSE:.
1450: 09 42 49 4f 5f 73 65 74 5f 73 68 75 74 64 6f 77 .BIO_set_shutdow
1460: 6e 28 62 69 6f 2c 20 6e 75 6d 29 3b 0a 09 62 72 n(bio, num);..br
1470: 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 42 49 eak;. case BI
1480: 4f 5f 43 54 52 4c 5f 45 4f 46 3a 0a 09 64 70 72 O_CTRL_EOF:..dpr
1490: 69 6e 74 66 28 22 42 49 4f 5f 43 54 52 4c 5f 45 intf("BIO_CTRL_E
14a0: 4f 46 22 29 3b 0a 09 72 65 74 20 3d 20 54 63 6c OF");..ret = Tcl
14b0: 5f 45 6f 66 28 63 68 61 6e 29 3b 0a 09 62 72 65 _Eof(chan);..bre
14c0: 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 42 49 4f ak;. case BIO
14d0: 5f 43 54 52 4c 5f 50 45 4e 44 49 4e 47 3a 0a 09 _CTRL_PENDING:..
14e0: 72 65 74 20 3d 20 28 54 63 6c 5f 49 6e 70 75 74 ret = (Tcl_Input
14f0: 42 75 66 66 65 72 65 64 28 63 68 61 6e 29 20 3f Buffered(chan) ?
1500: 20 31 20 3a 20 30 29 3b 0a 09 64 70 72 69 6e 74 1 : 0);..dprint
1510: 66 28 22 42 49 4f 5f 43 54 52 4c 5f 50 45 4e 44 f("BIO_CTRL_PEND
1520: 49 4e 47 28 25 64 29 22 2c 20 28 69 6e 74 29 20 ING(%d)", (int)
1530: 72 65 74 29 3b 0a 09 62 72 65 61 6b 3b 0a 20 20 ret);..break;.
1540: 20 20 63 61 73 65 20 42 49 4f 5f 43 54 52 4c 5f case BIO_CTRL_
1550: 57 50 45 4e 44 49 4e 47 3a 0a 09 72 65 74 20 3d WPENDING:..ret =
1560: 20 30 3b 0a 09 62 72 65 61 6b 3b 0a 20 20 20 20 0;..break;.
1570: 63 61 73 65 20 42 49 4f 5f 43 54 52 4c 5f 44 55 case BIO_CTRL_DU
1580: 50 3a 0a 09 62 72 65 61 6b 3b 0a 20 20 20 20 63 P:..break;. c
1590: 61 73 65 20 42 49 4f 5f 43 54 52 4c 5f 46 4c 55 ase BIO_CTRL_FLU
15a0: 53 48 3a 0a 09 64 70 72 69 6e 74 66 28 22 42 49 SH:..dprintf("BI
15b0: 4f 5f 43 54 52 4c 5f 46 4c 55 53 48 22 29 3b 0a O_CTRL_FLUSH");.
15c0: 09 69 66 20 28 63 68 61 6e 6e 65 6c 54 79 70 65 .if (channelType
15d0: 56 65 72 73 69 6f 6e 20 3d 3d 20 54 4c 53 5f 43 Version == TLS_C
15e0: 48 41 4e 4e 45 4c 5f 56 45 52 53 49 4f 4e 5f 32 HANNEL_VERSION_2
15f0: 29 20 7b 0a 09 20 20 20 20 72 65 74 20 3d 20 28 ) {.. ret = (
1600: 28 54 63 6c 5f 57 72 69 74 65 52 61 77 28 63 68 (Tcl_WriteRaw(ch
1610: 61 6e 2c 20 22 22 2c 20 30 29 20 3e 3d 20 30 29 an, "", 0) >= 0)
1620: 20 3f 20 31 20 3a 20 2d 31 29 3b 0a 09 7d 20 65 ? 1 : -1);..} e
1630: 6c 73 65 20 7b 0a 09 20 20 20 20 72 65 74 20 3d lse {.. ret =
1640: 20 28 28 54 63 6c 5f 46 6c 75 73 68 28 63 68 61 ((Tcl_Flush(cha
1650: 6e 29 20 3d 3d 20 54 43 4c 5f 4f 4b 29 20 3f 20 n) == TCL_OK) ?
1660: 31 20 3a 20 2d 31 29 3b 0a 09 7d 0a 20 20 20 20 1 : -1);..}.
1670: 20 20 20 20 64 70 72 69 6e 74 66 28 22 42 49 4f dprintf("BIO
1680: 5f 43 54 52 4c 5f 46 4c 55 53 48 20 72 65 74 75 _CTRL_FLUSH retu
1690: 72 6e 69 6e 67 20 76 61 6c 75 65 20 25 6c 69 22 rning value %li"
16a0: 2c 20 72 65 74 29 3b 0a 09 62 72 65 61 6b 3b 0a , ret);..break;.
16b0: 20 20 20 20 64 65 66 61 75 6c 74 3a 0a 09 72 65 default:..re
16c0: 74 20 3d 20 30 3b 0a 09 62 72 65 61 6b 3b 0a 20 t = 0;..break;.
16d0: 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 28 }. return(
16e0: 72 65 74 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 ret);.}..static
16f0: 69 6e 74 20 42 69 6f 4e 65 77 28 42 49 4f 20 2a int BioNew(BIO *
1700: 62 69 6f 29 20 7b 0a 20 20 20 20 64 70 72 69 6e bio) {. dprin
1710: 74 66 28 22 42 69 6f 4e 65 77 28 25 70 29 20 63 tf("BioNew(%p) c
1720: 61 6c 6c 65 64 22 2c 20 62 69 6f 29 3b 0a 0a 20 alled", bio);..
1730: 20 20 20 42 49 4f 5f 73 65 74 5f 69 6e 69 74 28 BIO_set_init(
1740: 62 69 6f 2c 20 30 29 3b 0a 20 20 20 20 42 49 4f bio, 0);. BIO
1750: 5f 73 65 74 5f 64 61 74 61 28 62 69 6f 2c 20 4e _set_data(bio, N
1760: 55 4c 4c 29 3b 0a 20 20 20 20 42 49 4f 5f 63 6c ULL);. BIO_cl
1770: 65 61 72 5f 66 6c 61 67 73 28 62 69 6f 2c 20 2d ear_flags(bio, -
1780: 31 29 3b 0a 0a 20 20 20 20 72 65 74 75 72 6e 20 1);.. return
1790: 31 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 1;.}..static int
17a0: 20 42 69 6f 46 72 65 65 28 42 49 4f 20 2a 62 69 BioFree(BIO *bi
17b0: 6f 29 20 7b 0a 20 20 20 20 69 66 20 28 62 69 6f o) {. if (bio
17c0: 20 3d 3d 20 4e 55 4c 4c 29 20 7b 0a 09 72 65 74 == NULL) {..ret
17d0: 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 0a 20 20 urn 0;. }..
17e0: 20 20 64 70 72 69 6e 74 66 28 22 42 69 6f 46 72 dprintf("BioFr
17f0: 65 65 28 25 70 29 20 63 61 6c 6c 65 64 22 2c 20 ee(%p) called",
1800: 62 69 6f 29 3b 0a 0a 20 20 20 20 69 66 20 28 42 bio);.. if (B
1810: 49 4f 5f 67 65 74 5f 73 68 75 74 64 6f 77 6e 28 IO_get_shutdown(
1820: 62 69 6f 29 29 20 7b 0a 09 69 66 20 28 42 49 4f bio)) {..if (BIO
1830: 5f 67 65 74 5f 69 6e 69 74 28 62 69 6f 29 29 20 _get_init(bio))
1840: 7b 0a 09 20 20 20 20 2f 2a 73 68 75 74 64 6f 77 {.. /*shutdow
1850: 6e 28 62 69 6f 2d 3e 6e 75 6d 2c 20 32 29 20 2a n(bio->num, 2) *
1860: 2f 0a 09 20 20 20 20 2f 2a 63 6c 6f 73 65 73 6f /.. /*closeso
1870: 63 6b 65 74 28 62 69 6f 2d 3e 6e 75 6d 29 20 2a cket(bio->num) *
1880: 2f 0a 09 7d 0a 20 20 20 20 20 20 20 20 42 49 4f /..}. BIO
1890: 5f 73 65 74 5f 69 6e 69 74 28 62 69 6f 2c 20 30 _set_init(bio, 0
18a0: 29 3b 0a 20 20 20 20 20 20 20 20 42 49 4f 5f 63 );. BIO_c
18b0: 6c 65 61 72 5f 66 6c 61 67 73 28 62 69 6f 2c 20 lear_flags(bio,
18c0: 2d 31 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 -1);. }. r
18d0: 65 74 75 72 6e 20 31 3b 0a 7d 0a eturn 1;.}.