2
* Copyright (C) 1997-2000 Matt Newman <matt@novadigm.com>
4
* $Header: /cvsroot/tls/tls/tlsBIO.c,v 1.7 2004/02/04 04:02:19 razzell Exp $
6
* Provides BIO layer to interface openssl to Tcl.
12
* Forward declarations
15
static int BioWrite _ANSI_ARGS_ ((BIO *h, CONST char *buf, int num));
16
static int BioRead _ANSI_ARGS_ ((BIO *h, char *buf, int num));
17
static int BioPuts _ANSI_ARGS_ ((BIO *h, CONST char *str));
18
static long BioCtrl _ANSI_ARGS_ ((BIO *h, int cmd, long arg1, void *ptr));
19
static int BioNew _ANSI_ARGS_ ((BIO *h));
20
static int BioFree _ANSI_ARGS_ ((BIO *h));
23
static BIO_METHOD BioMethods = {
35
BIO_new_tcl(statePtr, flags)
41
bio = BIO_new(&BioMethods);
42
bio->ptr = (char*)statePtr;
44
bio->shutdown = flags;
56
BioWrite (bio, buf, bufLen)
61
Tcl_Channel chan = Tls_GetParent((State*)(bio->ptr));
64
dprintf(stderr,"\nBioWrite(0x%x, <buf>, %d) [0x%x]",
65
(unsigned int) bio, bufLen, (unsigned int) chan);
67
if (channelTypeVersion == TLS_CHANNEL_VERSION_2) {
68
ret = Tcl_WriteRaw(chan, buf, bufLen);
70
ret = Tcl_Write(chan, buf, bufLen);
73
dprintf(stderr,"\n[0x%x] BioWrite(%d) -> %d [%d.%d]",
74
(unsigned int) chan, bufLen, ret, Tcl_Eof(chan), Tcl_GetErrno());
76
BIO_clear_flags(bio, BIO_FLAGS_WRITE|BIO_FLAGS_SHOULD_RETRY);
80
BIO_set_retry_write(bio);
84
if (BIO_should_read(bio)) {
85
BIO_set_retry_read(bio);
91
BioRead (bio, buf, bufLen)
96
Tcl_Channel chan = Tls_GetParent((State*)bio->ptr);
99
dprintf(stderr,"\nBioRead(0x%x, <buf>, %d) [0x%x]",
100
(unsigned int) bio, bufLen, (unsigned int) chan);
102
if (buf == NULL) return 0;
104
if (channelTypeVersion == TLS_CHANNEL_VERSION_2) {
105
ret = Tcl_ReadRaw(chan, buf, bufLen);
107
ret = Tcl_Read(chan, buf, bufLen);
110
dprintf(stderr,"\n[0x%x] BioRead(%d) -> %d [%d.%d]",
111
(unsigned int) chan, bufLen, ret, Tcl_Eof(chan), Tcl_GetErrno());
113
BIO_clear_flags(bio, BIO_FLAGS_READ|BIO_FLAGS_SHOULD_RETRY);
116
if (!Tcl_Eof(chan)) {
117
BIO_set_retry_read(bio);
121
if (BIO_should_write(bio)) {
122
BIO_set_retry_write(bio);
132
return BioWrite(bio, str, strlen(str));
136
BioCtrl (bio, cmd, num, ptr)
142
Tcl_Channel chan = Tls_GetParent((State*)bio->ptr);
146
dprintf(stderr,"\nBioCtrl(0x%x, 0x%x, 0x%x, 0x%x)",
147
(unsigned int) bio, (unsigned int) cmd, (unsigned int) num,
153
case BIO_C_FILE_SEEK:
154
case BIO_C_FILE_TELL:
163
bio->ptr = *((char **)ptr);
164
bio->shutdown = (int)num;
178
case BIO_CTRL_GET_CLOSE:
181
case BIO_CTRL_SET_CLOSE:
182
bio->shutdown = (int)num;
185
dprintf(stderr, "BIO_CTRL_EOF\n");
188
case BIO_CTRL_PENDING:
189
ret = (Tcl_InputBuffered(chan) ? 1 : 0);
190
dprintf(stderr, "BIO_CTRL_PENDING(%d)\n", (int) ret);
192
case BIO_CTRL_WPENDING:
198
dprintf(stderr, "BIO_CTRL_FLUSH\n");
199
if (channelTypeVersion == TLS_CHANNEL_VERSION_2) {
200
ret = ((Tcl_WriteRaw(chan, "", 0) >= 0) ? 1 : -1);
202
ret = ((Tcl_Flush(chan) == TCL_OK) ? 1 : -1);
234
/*shutdown(bio->num, 2) */
235
/*closesocket(bio->num) */