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: 42 49 4f 20 2a 0a 42 49 4f 5f 6e 65 77 5f 74 63 BIO *.BIO_new_tc
0220: 6c 28 73 74 61 74 65 50 74 72 2c 20 66 6c 61 67 l(statePtr, flag
0230: 73 29 0a 20 20 20 20 53 74 61 74 65 20 2a 73 74 s). State *st
0240: 61 74 65 50 74 72 3b 0a 20 20 20 20 69 6e 74 20 atePtr;. int
0250: 66 6c 61 67 73 3b 0a 7b 0a 20 20 20 20 42 49 4f flags;.{. BIO
0260: 20 2a 62 69 6f 3b 0a 20 20 20 20 73 74 61 74 69 *bio;. stati
0270: 63 20 42 49 4f 5f 4d 45 54 48 4f 44 20 42 69 6f c BIO_METHOD Bio
0280: 4d 65 74 68 6f 64 73 20 3d 20 7b 0a 20 20 20 20 Methods = {.
0290: 20 20 20 20 2e 74 79 70 65 20 3d 20 42 49 4f 5f .type = BIO_
02a0: 54 59 50 45 5f 54 43 4c 2c 0a 09 2e 6e 61 6d 65 TYPE_TCL,...name
02b0: 20 3d 20 22 74 63 6c 22 2c 0a 20 20 20 20 20 20 = "tcl",.
02c0: 20 20 2e 62 77 72 69 74 65 20 3d 20 42 69 6f 57 .bwrite = BioW
02d0: 72 69 74 65 2c 0a 20 20 20 20 20 20 20 20 2e 62 rite,. .b
02e0: 72 65 61 64 20 3d 20 42 69 6f 52 65 61 64 2c 0a read = BioRead,.
02f0: 20 20 20 20 20 20 20 20 2e 62 70 75 74 73 20 3d .bputs =
0300: 20 42 69 6f 50 75 74 73 2c 0a 20 20 20 20 20 20 BioPuts,.
0310: 20 20 2e 63 74 72 6c 20 3d 20 42 69 6f 43 74 72 .ctrl = BioCtr
0320: 6c 2c 0a 20 20 20 20 20 20 20 20 2e 63 72 65 61 l,. .crea
0330: 74 65 20 3d 20 42 69 6f 4e 65 77 2c 0a 20 20 20 te = BioNew,.
0340: 20 20 20 20 20 2e 64 65 73 74 72 6f 79 20 3d 20 .destroy =
0350: 42 69 6f 46 72 65 65 2c 0a 20 20 20 20 7d 3b 0a BioFree,. };.
0360: 0a 20 20 20 20 62 69 6f 09 09 09 3d 20 42 49 4f . bio...= BIO
0370: 5f 6e 65 77 28 26 42 69 6f 4d 65 74 68 6f 64 73 _new(&BioMethods
0380: 29 3b 0a 20 20 20 20 62 69 6f 2d 3e 70 74 72 09 );. bio->ptr.
0390: 09 3d 20 28 63 68 61 72 2a 29 73 74 61 74 65 50 .= (char*)stateP
03a0: 74 72 3b 0a 20 20 20 20 62 69 6f 2d 3e 69 6e 69 tr;. bio->ini
03b0: 74 09 09 3d 20 31 3b 0a 20 20 20 20 62 69 6f 2d t..= 1;. bio-
03c0: 3e 73 68 75 74 64 6f 77 6e 09 3d 20 66 6c 61 67 >shutdown.= flag
03d0: 73 3b 0a 0a 20 20 20 20 72 65 74 75 72 6e 20 62 s;.. return b
03e0: 69 6f 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e io;.}..static in
03f0: 74 0a 42 69 6f 57 72 69 74 65 20 28 62 69 6f 2c t.BioWrite (bio,
0400: 20 62 75 66 2c 20 62 75 66 4c 65 6e 29 0a 20 20 buf, bufLen).
0410: 20 20 42 49 4f 20 2a 62 69 6f 3b 0a 20 20 20 20 BIO *bio;.
0420: 43 4f 4e 53 54 20 63 68 61 72 20 2a 62 75 66 3b CONST char *buf;
0430: 0a 20 20 20 20 69 6e 74 20 62 75 66 4c 65 6e 3b . int bufLen;
0440: 0a 7b 0a 20 20 20 20 54 63 6c 5f 43 68 61 6e 6e .{. Tcl_Chann
0450: 65 6c 20 63 68 61 6e 20 3d 20 54 6c 73 5f 47 65 el chan = Tls_Ge
0460: 74 50 61 72 65 6e 74 28 28 53 74 61 74 65 2a 29 tParent((State*)
0470: 28 62 69 6f 2d 3e 70 74 72 29 29 3b 0a 20 20 20 (bio->ptr));.
0480: 20 69 6e 74 20 72 65 74 3b 0a 0a 20 20 20 20 64 int ret;.. d
0490: 70 72 69 6e 74 66 28 22 42 69 6f 57 72 69 74 65 printf("BioWrite
04a0: 28 25 70 2c 20 3c 62 75 66 3e 2c 20 25 64 29 20 (%p, <buf>, %d)
04b0: 5b 25 70 5d 22 2c 0a 09 20 20 20 20 28 76 6f 69 [%p]",.. (voi
04c0: 64 20 2a 29 20 62 69 6f 2c 20 62 75 66 4c 65 6e d *) bio, bufLen
04d0: 2c 20 28 76 6f 69 64 20 2a 29 20 63 68 61 6e 29 , (void *) chan)
04e0: 3b 0a 0a 20 20 20 20 69 66 20 28 63 68 61 6e 6e ;.. if (chann
04f0: 65 6c 54 79 70 65 56 65 72 73 69 6f 6e 20 3d 3d elTypeVersion ==
0500: 20 54 4c 53 5f 43 48 41 4e 4e 45 4c 5f 56 45 52 TLS_CHANNEL_VER
0510: 53 49 4f 4e 5f 32 29 20 7b 0a 09 72 65 74 20 3d SION_2) {..ret =
0520: 20 54 63 6c 5f 57 72 69 74 65 52 61 77 28 63 68 Tcl_WriteRaw(ch
0530: 61 6e 2c 20 62 75 66 2c 20 62 75 66 4c 65 6e 29 an, buf, bufLen)
0540: 3b 0a 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a 09 ;. } else {..
0550: 72 65 74 20 3d 20 54 63 6c 5f 57 72 69 74 65 28 ret = Tcl_Write(
0560: 63 68 61 6e 2c 20 62 75 66 2c 20 62 75 66 4c 65 chan, buf, bufLe
0570: 6e 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 64 n);. }.. d
0580: 70 72 69 6e 74 66 28 22 5b 25 70 5d 20 42 69 6f printf("[%p] Bio
0590: 57 72 69 74 65 28 25 64 29 20 2d 3e 20 25 64 20 Write(%d) -> %d
05a0: 5b 25 64 2e 25 64 5d 22 2c 0a 09 20 20 20 20 28 [%d.%d]",.. (
05b0: 76 6f 69 64 20 2a 29 20 63 68 61 6e 2c 20 62 75 void *) chan, bu
05c0: 66 4c 65 6e 2c 20 72 65 74 2c 20 54 63 6c 5f 45 fLen, ret, Tcl_E
05d0: 6f 66 28 63 68 61 6e 29 2c 20 54 63 6c 5f 47 65 of(chan), Tcl_Ge
05e0: 74 45 72 72 6e 6f 28 29 29 3b 0a 0a 20 20 20 20 tErrno());..
05f0: 42 49 4f 5f 63 6c 65 61 72 5f 66 6c 61 67 73 28 BIO_clear_flags(
0600: 62 69 6f 2c 20 42 49 4f 5f 46 4c 41 47 53 5f 57 bio, BIO_FLAGS_W
0610: 52 49 54 45 7c 42 49 4f 5f 46 4c 41 47 53 5f 53 RITE|BIO_FLAGS_S
0620: 48 4f 55 4c 44 5f 52 45 54 52 59 29 3b 0a 0a 20 HOULD_RETRY);..
0630: 20 20 20 69 66 20 28 72 65 74 20 3d 3d 20 30 29 if (ret == 0)
0640: 20 7b 0a 09 69 66 20 28 21 54 63 6c 5f 45 6f 66 {..if (!Tcl_Eof
0650: 28 63 68 61 6e 29 29 20 7b 0a 09 20 20 20 20 42 (chan)) {.. B
0660: 49 4f 5f 73 65 74 5f 72 65 74 72 79 5f 77 72 69 IO_set_retry_wri
0670: 74 65 28 62 69 6f 29 3b 0a 09 20 20 20 20 72 65 te(bio);.. re
0680: 74 20 3d 20 2d 31 3b 0a 09 7d 0a 20 20 20 20 7d t = -1;..}. }
0690: 0a 20 20 20 20 69 66 20 28 42 49 4f 5f 73 68 6f . if (BIO_sho
06a0: 75 6c 64 5f 72 65 61 64 28 62 69 6f 29 29 20 7b uld_read(bio)) {
06b0: 0a 09 42 49 4f 5f 73 65 74 5f 72 65 74 72 79 5f ..BIO_set_retry_
06c0: 72 65 61 64 28 62 69 6f 29 3b 0a 20 20 20 20 7d read(bio);. }
06d0: 0a 20 20 20 20 72 65 74 75 72 6e 20 72 65 74 3b . return ret;
06e0: 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 0a 42 .}..static int.B
06f0: 69 6f 52 65 61 64 20 28 62 69 6f 2c 20 62 75 66 ioRead (bio, buf
0700: 2c 20 62 75 66 4c 65 6e 29 0a 20 20 20 20 42 49 , bufLen). BI
0710: 4f 20 2a 62 69 6f 3b 0a 20 20 20 20 63 68 61 72 O *bio;. char
0720: 20 2a 62 75 66 3b 0a 20 20 20 20 69 6e 74 20 62 *buf;. int b
0730: 75 66 4c 65 6e 3b 0a 7b 0a 20 20 20 20 54 63 6c ufLen;.{. Tcl
0740: 5f 43 68 61 6e 6e 65 6c 20 63 68 61 6e 20 3d 20 _Channel chan =
0750: 54 6c 73 5f 47 65 74 50 61 72 65 6e 74 28 28 53 Tls_GetParent((S
0760: 74 61 74 65 2a 29 62 69 6f 2d 3e 70 74 72 29 3b tate*)bio->ptr);
0770: 0a 20 20 20 20 69 6e 74 20 72 65 74 20 3d 20 30 . int ret = 0
0780: 3b 0a 20 20 20 20 69 6e 74 20 74 63 6c 45 6f 66 ;. int tclEof
0790: 43 68 61 6e 3b 0a 0a 20 20 20 20 64 70 72 69 6e Chan;.. dprin
07a0: 74 66 28 22 42 69 6f 52 65 61 64 28 25 70 2c 20 tf("BioRead(%p,
07b0: 3c 62 75 66 3e 2c 20 25 64 29 20 5b 25 70 5d 22 <buf>, %d) [%p]"
07c0: 2c 20 28 76 6f 69 64 20 2a 29 20 62 69 6f 2c 20 , (void *) bio,
07d0: 62 75 66 4c 65 6e 2c 20 28 76 6f 69 64 20 2a 29 bufLen, (void *)
07e0: 20 63 68 61 6e 29 3b 0a 0a 20 20 20 20 69 66 20 chan);.. if
07f0: 28 62 75 66 20 3d 3d 20 4e 55 4c 4c 29 20 72 65 (buf == NULL) re
0800: 74 75 72 6e 20 30 3b 0a 0a 20 20 20 20 69 66 20 turn 0;.. if
0810: 28 63 68 61 6e 6e 65 6c 54 79 70 65 56 65 72 73 (channelTypeVers
0820: 69 6f 6e 20 3d 3d 20 54 4c 53 5f 43 48 41 4e 4e ion == TLS_CHANN
0830: 45 4c 5f 56 45 52 53 49 4f 4e 5f 32 29 20 7b 0a EL_VERSION_2) {.
0840: 09 72 65 74 20 3d 20 54 63 6c 5f 52 65 61 64 52 .ret = Tcl_ReadR
0850: 61 77 28 63 68 61 6e 2c 20 62 75 66 2c 20 62 75 aw(chan, buf, bu
0860: 66 4c 65 6e 29 3b 0a 20 20 20 20 7d 20 65 6c 73 fLen);. } els
0870: 65 20 7b 0a 09 72 65 74 20 3d 20 54 63 6c 5f 52 e {..ret = Tcl_R
0880: 65 61 64 28 63 68 61 6e 2c 20 62 75 66 2c 20 62 ead(chan, buf, b
0890: 75 66 4c 65 6e 29 3b 0a 20 20 20 20 7d 0a 0a 20 ufLen);. }..
08a0: 20 20 20 74 63 6c 45 6f 66 43 68 61 6e 20 3d 20 tclEofChan =
08b0: 54 63 6c 5f 45 6f 66 28 63 68 61 6e 29 3b 0a 0a Tcl_Eof(chan);..
08c0: 20 20 20 20 64 70 72 69 6e 74 66 28 22 5b 25 70 dprintf("[%p
08d0: 5d 20 42 69 6f 52 65 61 64 28 25 64 29 20 2d 3e ] BioRead(%d) ->
08e0: 20 25 64 20 5b 74 63 6c 45 6f 66 3d 25 64 3b 20 %d [tclEof=%d;
08f0: 74 63 6c 45 72 72 6e 6f 3d 25 64 5d 22 2c 0a 09 tclErrno=%d]",..
0900: 20 20 20 20 28 76 6f 69 64 20 2a 29 20 63 68 61 (void *) cha
0910: 6e 2c 20 62 75 66 4c 65 6e 2c 20 72 65 74 2c 20 n, bufLen, ret,
0920: 74 63 6c 45 6f 66 43 68 61 6e 2c 20 54 63 6c 5f tclEofChan, Tcl_
0930: 47 65 74 45 72 72 6e 6f 28 29 29 3b 0a 0a 20 20 GetErrno());..
0940: 20 20 42 49 4f 5f 63 6c 65 61 72 5f 66 6c 61 67 BIO_clear_flag
0950: 73 28 62 69 6f 2c 20 42 49 4f 5f 46 4c 41 47 53 s(bio, BIO_FLAGS
0960: 5f 52 45 41 44 7c 42 49 4f 5f 46 4c 41 47 53 5f _READ|BIO_FLAGS_
0970: 53 48 4f 55 4c 44 5f 52 45 54 52 59 29 3b 0a 0a SHOULD_RETRY);..
0980: 20 20 20 20 69 66 20 28 72 65 74 20 3d 3d 20 30 if (ret == 0
0990: 29 20 7b 0a 09 69 66 20 28 21 74 63 6c 45 6f 66 ) {..if (!tclEof
09a0: 43 68 61 6e 29 20 7b 0a 20 20 20 20 20 20 20 20 Chan) {.
09b0: 20 20 20 20 64 70 72 69 6e 74 66 28 22 47 6f 74 dprintf("Got
09c0: 20 30 20 66 72 6f 6d 20 54 63 6c 5f 52 65 61 64 0 from Tcl_Read
09d0: 20 6f 72 20 54 63 6c 5f 52 65 61 64 52 61 77 2c or Tcl_ReadRaw,
09e0: 20 61 6e 64 20 45 4f 46 20 69 73 20 73 65 74 20 and EOF is set
09f0: 2d 2d 20 72 65 74 20 3d 3d 20 2d 31 20 6e 6f 77 -- ret == -1 now
0a00: 22 29 3b 0a 09 20 20 20 20 42 49 4f 5f 73 65 74 ");.. BIO_set
0a10: 5f 72 65 74 72 79 5f 72 65 61 64 28 62 69 6f 29 _retry_read(bio)
0a20: 3b 0a 09 20 20 20 20 72 65 74 20 3d 20 2d 31 3b ;.. ret = -1;
0a30: 0a 09 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 ..}. }. if
0a40: 20 28 42 49 4f 5f 73 68 6f 75 6c 64 5f 77 72 69 (BIO_should_wri
0a50: 74 65 28 62 69 6f 29 29 20 7b 0a 09 42 49 4f 5f te(bio)) {..BIO_
0a60: 73 65 74 5f 72 65 74 72 79 5f 77 72 69 74 65 28 set_retry_write(
0a70: 62 69 6f 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 bio);. }..
0a80: 20 64 70 72 69 6e 74 66 28 22 42 69 6f 52 65 61 dprintf("BioRea
0a90: 64 28 25 70 2c 20 3c 62 75 66 3e 2c 20 25 64 29 d(%p, <buf>, %d)
0aa0: 20 5b 25 70 5d 20 72 65 74 75 72 6e 69 6e 67 20 [%p] returning
0ab0: 25 69 22 2c 20 28 76 6f 69 64 20 2a 29 20 62 69 %i", (void *) bi
0ac0: 6f 2c 20 62 75 66 4c 65 6e 2c 20 28 76 6f 69 64 o, bufLen, (void
0ad0: 20 2a 29 20 63 68 61 6e 2c 20 72 65 74 29 3b 0a *) chan, ret);.
0ae0: 0a 20 20 20 20 72 65 74 75 72 6e 20 72 65 74 3b . return ret;
0af0: 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 0a 42 .}..static int.B
0b00: 69 6f 50 75 74 73 09 28 62 69 6f 2c 20 73 74 72 ioPuts.(bio, str
0b10: 29 0a 20 20 20 20 42 49 4f 20 2a 62 69 6f 3b 0a ). BIO *bio;.
0b20: 20 20 20 20 43 4f 4e 53 54 20 63 68 61 72 20 2a CONST char *
0b30: 73 74 72 3b 0a 7b 0a 20 20 20 20 72 65 74 75 72 str;.{. retur
0b40: 6e 20 42 69 6f 57 72 69 74 65 28 62 69 6f 2c 20 n BioWrite(bio,
0b50: 73 74 72 2c 20 28 69 6e 74 29 20 73 74 72 6c 65 str, (int) strle
0b60: 6e 28 73 74 72 29 29 3b 0a 7d 0a 0a 73 74 61 74 n(str));.}..stat
0b70: 69 63 20 6c 6f 6e 67 0a 42 69 6f 43 74 72 6c 09 ic long.BioCtrl.
0b80: 28 62 69 6f 2c 20 63 6d 64 2c 20 6e 75 6d 2c 20 (bio, cmd, num,
0b90: 70 74 72 29 0a 20 20 20 20 42 49 4f 20 2a 62 69 ptr). BIO *bi
0ba0: 6f 3b 0a 20 20 20 20 69 6e 74 20 63 6d 64 3b 0a o;. int cmd;.
0bb0: 20 20 20 20 6c 6f 6e 67 20 6e 75 6d 3b 0a 20 20 long num;.
0bc0: 20 20 76 6f 69 64 20 2a 70 74 72 3b 0a 7b 0a 20 void *ptr;.{.
0bd0: 20 20 20 54 63 6c 5f 43 68 61 6e 6e 65 6c 20 63 Tcl_Channel c
0be0: 68 61 6e 20 3d 20 54 6c 73 5f 47 65 74 50 61 72 han = Tls_GetPar
0bf0: 65 6e 74 28 28 53 74 61 74 65 2a 29 62 69 6f 2d ent((State*)bio-
0c00: 3e 70 74 72 29 3b 0a 20 20 20 20 6c 6f 6e 67 20 >ptr);. long
0c10: 72 65 74 20 3d 20 31 3b 0a 20 20 20 20 69 6e 74 ret = 1;. int
0c20: 20 2a 69 70 3b 0a 0a 20 20 20 20 64 70 72 69 6e *ip;.. dprin
0c30: 74 66 28 22 42 69 6f 43 74 72 6c 28 25 70 2c 20 tf("BioCtrl(%p,
0c40: 30 78 25 78 2c 20 30 78 25 78 2c 20 25 70 29 22 0x%x, 0x%x, %p)"
0c50: 2c 0a 09 20 20 20 20 28 76 6f 69 64 20 2a 29 20 ,.. (void *)
0c60: 62 69 6f 2c 20 28 75 6e 73 69 67 6e 65 64 20 69 bio, (unsigned i
0c70: 6e 74 29 20 63 6d 64 2c 20 28 75 6e 73 69 67 6e nt) cmd, (unsign
0c80: 65 64 20 69 6e 74 29 20 6e 75 6d 2c 0a 09 20 20 ed int) num,..
0c90: 20 20 28 76 6f 69 64 20 2a 29 20 70 74 72 29 3b (void *) ptr);
0ca0: 0a 0a 20 20 20 20 73 77 69 74 63 68 20 28 63 6d .. switch (cm
0cb0: 64 29 20 7b 0a 20 20 20 20 63 61 73 65 20 42 49 d) {. case BI
0cc0: 4f 5f 43 54 52 4c 5f 52 45 53 45 54 3a 0a 09 6e O_CTRL_RESET:..n
0cd0: 75 6d 20 3d 20 30 3b 0a 20 20 20 20 63 61 73 65 um = 0;. case
0ce0: 20 42 49 4f 5f 43 5f 46 49 4c 45 5f 53 45 45 4b BIO_C_FILE_SEEK
0cf0: 3a 0a 20 20 20 20 63 61 73 65 20 42 49 4f 5f 43 :. case BIO_C
0d00: 5f 46 49 4c 45 5f 54 45 4c 4c 3a 0a 09 72 65 74 _FILE_TELL:..ret
0d10: 20 3d 20 30 3b 0a 09 62 72 65 61 6b 3b 0a 20 20 = 0;..break;.
0d20: 20 20 63 61 73 65 20 42 49 4f 5f 43 54 52 4c 5f case BIO_CTRL_
0d30: 49 4e 46 4f 3a 0a 09 72 65 74 20 3d 20 31 3b 0a INFO:..ret = 1;.
0d40: 09 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 .break;. case
0d50: 20 42 49 4f 5f 43 5f 53 45 54 5f 46 44 3a 0a 09 BIO_C_SET_FD:..
0d60: 42 69 6f 46 72 65 65 28 62 69 6f 29 3b 0a 09 2f BioFree(bio);../
0d70: 2a 20 53 65 74 73 20 53 74 61 74 65 2a 20 2a 2f * Sets State* */
0d80: 0a 09 62 69 6f 2d 3e 70 74 72 09 3d 20 2a 28 28 ..bio->ptr.= *((
0d90: 63 68 61 72 20 2a 2a 29 70 74 72 29 3b 0a 09 62 char **)ptr);..b
0da0: 69 6f 2d 3e 73 68 75 74 64 6f 77 6e 09 3d 20 28 io->shutdown.= (
0db0: 69 6e 74 29 6e 75 6d 3b 0a 09 62 69 6f 2d 3e 69 int)num;..bio->i
0dc0: 6e 69 74 09 3d 20 31 3b 0a 09 62 72 65 61 6b 3b nit.= 1;..break;
0dd0: 0a 20 20 20 20 63 61 73 65 20 42 49 4f 5f 43 5f . case BIO_C_
0de0: 47 45 54 5f 46 44 3a 0a 09 69 66 20 28 62 69 6f GET_FD:..if (bio
0df0: 2d 3e 69 6e 69 74 29 20 7b 0a 09 20 20 20 20 69 ->init) {.. i
0e00: 70 20 3d 20 28 69 6e 74 20 2a 29 70 74 72 3b 0a p = (int *)ptr;.
0e10: 09 20 20 20 20 69 66 20 28 69 70 20 21 3d 20 4e . if (ip != N
0e20: 55 4c 4c 29 20 7b 0a 09 09 2a 69 70 20 3d 20 62 ULL) {...*ip = b
0e30: 69 6f 2d 3e 6e 75 6d 3b 0a 09 20 20 20 20 7d 0a io->num;.. }.
0e40: 09 20 20 20 20 72 65 74 20 3d 20 62 69 6f 2d 3e . ret = bio->
0e50: 6e 75 6d 3b 0a 09 7d 20 65 6c 73 65 20 7b 0a 09 num;..} else {..
0e60: 20 20 20 20 72 65 74 20 3d 20 2d 31 3b 0a 09 7d ret = -1;..}
0e70: 0a 09 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 ..break;. cas
0e80: 65 20 42 49 4f 5f 43 54 52 4c 5f 47 45 54 5f 43 e BIO_CTRL_GET_C
0e90: 4c 4f 53 45 3a 0a 09 72 65 74 20 3d 20 62 69 6f LOSE:..ret = bio
0ea0: 2d 3e 73 68 75 74 64 6f 77 6e 3b 0a 09 62 72 65 ->shutdown;..bre
0eb0: 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 42 49 4f ak;. case BIO
0ec0: 5f 43 54 52 4c 5f 53 45 54 5f 43 4c 4f 53 45 3a _CTRL_SET_CLOSE:
0ed0: 0a 09 62 69 6f 2d 3e 73 68 75 74 64 6f 77 6e 20 ..bio->shutdown
0ee0: 3d 20 28 69 6e 74 29 6e 75 6d 3b 0a 09 62 72 65 = (int)num;..bre
0ef0: 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 42 49 4f ak;. case BIO
0f00: 5f 43 54 52 4c 5f 45 4f 46 3a 0a 09 64 70 72 69 _CTRL_EOF:..dpri
0f10: 6e 74 66 28 22 42 49 4f 5f 43 54 52 4c 5f 45 4f ntf("BIO_CTRL_EO
0f20: 46 22 29 3b 0a 09 72 65 74 20 3d 20 54 63 6c 5f F");..ret = Tcl_
0f30: 45 6f 66 28 63 68 61 6e 29 3b 0a 09 62 72 65 61 Eof(chan);..brea
0f40: 6b 3b 0a 20 20 20 20 63 61 73 65 20 42 49 4f 5f k;. case BIO_
0f50: 43 54 52 4c 5f 50 45 4e 44 49 4e 47 3a 0a 09 72 CTRL_PENDING:..r
0f60: 65 74 20 3d 20 28 54 63 6c 5f 49 6e 70 75 74 42 et = (Tcl_InputB
0f70: 75 66 66 65 72 65 64 28 63 68 61 6e 29 20 3f 20 uffered(chan) ?
0f80: 31 20 3a 20 30 29 3b 0a 09 64 70 72 69 6e 74 66 1 : 0);..dprintf
0f90: 28 22 42 49 4f 5f 43 54 52 4c 5f 50 45 4e 44 49 ("BIO_CTRL_PENDI
0fa0: 4e 47 28 25 64 29 22 2c 20 28 69 6e 74 29 20 72 NG(%d)", (int) r
0fb0: 65 74 29 3b 0a 09 62 72 65 61 6b 3b 0a 20 20 20 et);..break;.
0fc0: 20 63 61 73 65 20 42 49 4f 5f 43 54 52 4c 5f 57 case BIO_CTRL_W
0fd0: 50 45 4e 44 49 4e 47 3a 0a 09 72 65 74 20 3d 20 PENDING:..ret =
0fe0: 30 3b 0a 09 62 72 65 61 6b 3b 0a 20 20 20 20 63 0;..break;. c
0ff0: 61 73 65 20 42 49 4f 5f 43 54 52 4c 5f 44 55 50 ase BIO_CTRL_DUP
1000: 3a 0a 09 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 :..break;. ca
1010: 73 65 20 42 49 4f 5f 43 54 52 4c 5f 46 4c 55 53 se BIO_CTRL_FLUS
1020: 48 3a 0a 09 64 70 72 69 6e 74 66 28 22 42 49 4f H:..dprintf("BIO
1030: 5f 43 54 52 4c 5f 46 4c 55 53 48 22 29 3b 0a 09 _CTRL_FLUSH");..
1040: 69 66 20 28 63 68 61 6e 6e 65 6c 54 79 70 65 56 if (channelTypeV
1050: 65 72 73 69 6f 6e 20 3d 3d 20 54 4c 53 5f 43 48 ersion == TLS_CH
1060: 41 4e 4e 45 4c 5f 56 45 52 53 49 4f 4e 5f 32 29 ANNEL_VERSION_2)
1070: 20 7b 0a 09 20 20 20 20 72 65 74 20 3d 20 28 28 {.. ret = ((
1080: 54 63 6c 5f 57 72 69 74 65 52 61 77 28 63 68 61 Tcl_WriteRaw(cha
1090: 6e 2c 20 22 22 2c 20 30 29 20 3e 3d 20 30 29 20 n, "", 0) >= 0)
10a0: 3f 20 31 20 3a 20 2d 31 29 3b 0a 09 7d 20 65 6c ? 1 : -1);..} el
10b0: 73 65 20 7b 0a 09 20 20 20 20 72 65 74 20 3d 20 se {.. ret =
10c0: 28 28 54 63 6c 5f 46 6c 75 73 68 28 63 68 61 6e ((Tcl_Flush(chan
10d0: 29 20 3d 3d 20 54 43 4c 5f 4f 4b 29 20 3f 20 31 ) == TCL_OK) ? 1
10e0: 20 3a 20 2d 31 29 3b 0a 09 7d 0a 09 62 72 65 61 : -1);..}..brea
10f0: 6b 3b 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 0a k;. default:.
1100: 09 72 65 74 20 3d 20 30 3b 0a 09 62 72 65 61 6b .ret = 0;..break
1110: 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 ;. }. retu
1120: 72 6e 28 72 65 74 29 3b 0a 7d 0a 0a 73 74 61 74 rn(ret);.}..stat
1130: 69 63 20 69 6e 74 0a 42 69 6f 4e 65 77 09 28 62 ic int.BioNew.(b
1140: 69 6f 29 0a 20 20 20 20 42 49 4f 20 2a 62 69 6f io). BIO *bio
1150: 3b 0a 7b 0a 20 20 20 20 62 69 6f 2d 3e 69 6e 69 ;.{. bio->ini
1160: 74 09 3d 20 30 3b 0a 20 20 20 20 62 69 6f 2d 3e t.= 0;. bio->
1170: 6e 75 6d 09 3d 20 30 3b 0a 20 20 20 20 62 69 6f num.= 0;. bio
1180: 2d 3e 70 74 72 09 3d 20 4e 55 4c 4c 3b 0a 20 20 ->ptr.= NULL;.
1190: 20 20 62 69 6f 2d 3e 66 6c 61 67 73 09 3d 20 30 bio->flags.= 0
11a0: 3b 0a 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b ;.. return 1;
11b0: 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 0a 42 .}..static int.B
11c0: 69 6f 46 72 65 65 09 28 62 69 6f 29 0a 20 20 20 ioFree.(bio).
11d0: 20 42 49 4f 20 2a 62 69 6f 3b 0a 7b 0a 20 20 20 BIO *bio;.{.
11e0: 20 69 66 20 28 62 69 6f 20 3d 3d 20 4e 55 4c 4c if (bio == NULL
11f0: 29 20 7b 0a 09 72 65 74 75 72 6e 20 30 3b 0a 20 ) {..return 0;.
1200: 20 20 20 7d 0a 0a 20 20 20 20 69 66 20 28 62 69 }.. if (bi
1210: 6f 2d 3e 73 68 75 74 64 6f 77 6e 29 20 7b 0a 09 o->shutdown) {..
1220: 69 66 20 28 62 69 6f 2d 3e 69 6e 69 74 29 20 7b if (bio->init) {
1230: 0a 09 20 20 20 20 2f 2a 73 68 75 74 64 6f 77 6e .. /*shutdown
1240: 28 62 69 6f 2d 3e 6e 75 6d 2c 20 32 29 20 2a 2f (bio->num, 2) */
1250: 0a 09 20 20 20 20 2f 2a 63 6c 6f 73 65 73 6f 63 .. /*closesoc
1260: 6b 65 74 28 62 69 6f 2d 3e 6e 75 6d 29 20 2a 2f ket(bio->num) */
1270: 0a 09 7d 0a 09 62 69 6f 2d 3e 69 6e 69 74 09 3d ..}..bio->init.=
1280: 20 30 3b 0a 09 62 69 6f 2d 3e 66 6c 61 67 73 09 0;..bio->flags.
1290: 3d 20 30 3b 0a 09 62 69 6f 2d 3e 6e 75 6d 09 3d = 0;..bio->num.=
12a0: 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 0;. }. re
12b0: 74 75 72 6e 20 31 3b 0a 7d 0a turn 1;.}.