Diff

Differences From Artifact [46ff3e28a8]:

To Artifact [f0f140b4c0]:


50
51
52
53
54
55
56
57

58
59
60
61
62
63
64
65
66
67
68
69
70
71
72



73
74
75
76
77

78
79
80
81
82
83
84

85
86
87
88
89
90




91
92


93


94

95
96
97
98
99
100
101
50
51
52
53
54
55
56

57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79

80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98


99
100
101
102
103

104
105
106
107
108
109
110
111







-
+















+
+
+




-
+







+






+
+
+
+
-
-
+
+

+
+
-
+







    }
    return(0);
}

/*
 *-------------------------------------------------------------------
 *
 * TlsCloseProc --
 * TlsClose2Proc --
 *
 *    This procedure is invoked by the generic IO level to perform
 *    channel-type-specific cleanup when a SSL socket based channel
 *    is closed.
 *
 *    Note: we leave the underlying socket alone, is this right?
 *
 * Results:
 *    0 if successful or POSIX error code if failed.
 *
 * Side effects:
 *    Closes the socket of the channel.
 *
 *-------------------------------------------------------------------
 */
#if TCL_MAJOR_VERSION > 8
#   define TlsCloseProc NULL /* No longer neccessary in Tcl 9 */
#else
static int TlsCloseProc(
    void *instanceData,
    TCL_UNUSED(Tcl_Interp *))
{
    State *statePtr = (State *) instanceData;
    State *statePtr = (State *)instanceData;

    dprintf("TlsCloseProc(%p)", statePtr);

    Tls_Clean(statePtr);
    Tcl_EventuallyFree(statePtr, Tls_Free);
    return TCL_OK;
}
#endif

static int TlsClose2Proc(
    void *instanceData,    /* The socket state. */
    Tcl_Interp *interp,		/* For errors - can be NULL. */
    int flags)			/* Flags to close read and/or write side of channel */
{
    State *statePtr = (State *)instanceData;

    dprintf("TlsClose2Proc(%p)", statePtr);

    if (!(flags&(TCL_CLOSE_READ|TCL_CLOSE_WRITE))) {
	return TlsCloseProc(instanceData, interp);
    if ((flags&(TCL_CLOSE_READ|TCL_CLOSE_WRITE))) {
	return EINVAL;
    }
    Tls_Clean(statePtr);
    Tcl_EventuallyFree(statePtr, Tls_Free);
    return EINVAL;
    return TCL_OK;
}

/*
 *------------------------------------------------------*
 *
 * Tls_WaitForConnect --
 *
136
137
138
139
140
141
142
143
144

145
146
147
148
149
150
151
146
147
148
149
150
151
152


153
154
155
156
157
158
159
160







-
-
+







	return(-1);
    }

    for (;;) {
	/* Not initialized yet! */
	if (statePtr->flags & TLS_TCL_SERVER) {
	    dprintf("Calling SSL_accept()");

		err = SSL_accept(statePtr->ssl);
	    err = SSL_accept(statePtr->ssl);
	} else {
	    dprintf("Calling SSL_connect()");
	    err = SSL_connect(statePtr->ssl);
	}

	if (err > 0) {
	    dprintf("That seems to have gone okay");
221
222
223
224
225
226
227
228

229
230
231
232
233
234
235
230
231
232
233
234
235
236

237
238
239
240
241
242
243
244







-
+







		if (*errorCodePtr == ECONNRESET) {
		    *errorCodePtr = ECONNABORTED;
		}
		}

		statePtr->flags |= TLS_TCL_HANDSHAKE_FAILED;

		return(-1);
		return -1;
	case SSL_ERROR_SSL:
	    dprintf("Got permanent fatal SSL error, aborting immediately");
		Tls_Error(statePtr, (char *)ERR_reason_error_string(ERR_get_error()));
	    statePtr->flags |= TLS_TCL_HANDSHAKE_FAILED;
	    *errorCodePtr = ECONNABORTED;
	    return(-1);
	case SSL_ERROR_WANT_CONNECT:
528
529
530
531
532
533
534
535

536
537
538
539
540
541
542
537
538
539
540
541
542
543

544
545
546
547
548
549
550
551







-
+







	    break;
	case SSL_ERROR_SSL:
	    Tls_Error(statePtr, TCLTLS_SSL_ERROR(statePtr->ssl, written));
	    *errorCodePtr = ECONNABORTED;
	    written = -1;
	    break;
	default:
	    dprintf(" unknown err: %d", err);
	    dprintf("unknown error: %d", err);
	    break;
    }

    dprintf("Output(%d) -> %d", toWrite, written);
    return(written);
}

560
561
562
563
564
565
566
567

568
569
570
571
572
573
574
575
576

577
578
579
580
581
582
583
569
570
571
572
573
574
575

576
577
578
579
580
581
582
583
584

585
586
587
588
589
590
591
592







-
+








-
+







 */
static int
TlsGetOptionProc(
    void *instanceData,	/* Socket state. */
    Tcl_Interp *interp,		/* For errors - can be NULL. */
    const char *optionName,	/* Name of the option to retrieve the value for, or
				 * NULL to get all options and their values. */
    Tcl_DString *dsPtr)	/* Where to store the computed value initialized by caller. */
    Tcl_DString *optionValue)	/* Where to store the computed value initialized by caller. */
{
    State *statePtr = (State *) instanceData;

    Tcl_Channel downChan = Tls_GetParent(statePtr, TLS_TCL_FASTPATH);
    Tcl_DriverGetOptionProc *getOptionProc;

    getOptionProc = Tcl_ChannelGetOptionProc(Tcl_GetChannelType(downChan));
    if (getOptionProc != NULL) {
	return (*getOptionProc)(Tcl_GetChannelInstanceData(downChan), interp, optionName, dsPtr);
	return (*getOptionProc)(Tcl_GetChannelInstanceData(downChan), interp, optionName, optionValue);
    } else if (optionName == (char*) NULL) {
	/*
	 * Request is query for all options, this is ok.
	 */
	return TCL_OK;
    }
    /*
753
754
755
756
757
758
759
760
761
762
763
764

765
766
767


768
769
770


771
772

773
774

775
776
777


778
779
780
781
782

783
784
785
786
787


788
789

790
791
792
793

794
795
796



797
798

799
800
801
802
803
804
805

806
807


808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824

825
826
827
828
829
830




831
832
833
834
835
836
837
838
839
840
841








842
843
844
845

846
847
848
849
850
851
852
853
854
855
856
857
858
859
860

861
862
863
864
865
866
867
762
763
764
765
766
767
768

769
770
771

772
773


774
775



776
777
778

779


780
781


782
783
784
785
786


787





788
789
790

791

792


793



794
795
796
797

798



799
800


801


802
803

804
805
806













807
808
809




810
811
812
813











814
815
816
817
818
819
820
821




822















823
824
825
826
827
828
829
830







-



-
+

-
-
+
+
-
-
-
+
+

-
+
-
-
+

-
-
+
+



-
-
+
-
-
-
-
-
+
+

-
+
-

-
-
+
-
-
-
+
+
+

-
+
-
-
-


-
-
+
-
-
+
+
-



-
-
-
-
-
-
-
-
-
-
-
-
-
+


-
-
-
-
+
+
+
+
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+







    }

    dprintf("Returning %i", mask);

    return(mask);
}

#if 0
/*
 *------------------------------------------------------*
 *
 *      TlsChannelHandler --
 *    TlsChannelHandlerTimer --
 *
 *      ------------------------------------------------*
 *      Handler called by Tcl as a result of
 *    ------------------------------------------------*
 *    Called by the notifier (-> timer) to flush out
 *      Tcl_CreateChannelHandler - to inform us of activity
 *      on the underlying channel.
 *      ------------------------------------------------*
 *    information waiting in channel buffers.
 *    ------------------------------------------------*
 *
 *      Sideeffects:
 *    Side effects:
 *              May generate subsequent calls to
 *              Tcl_NotifyChannel.
 *        As of 'TlsChannelHandler'.
 *
 *      Result:
 *              None.
 *    Result:
 *        None.
 *
 *------------------------------------------------------*
 */

static void
static void TlsChannelHandlerTimer(void *clientData) {
TlsChannelHandler (clientData, mask)
    void *    clientData;
    int            mask;
{
    State *statePtr = (State *) clientData;
    State *statePtr = (State *)clientData;
    int mask = 0;

    dprintf("HANDLER(0x%x)", mask);
    dprintf("Called");
    Tcl_Preserve(statePtr);

    if (mask & TCL_READABLE) {
	BIO_set_flags(statePtr->p_bio, BIO_FLAGS_READ);
    statePtr->timer = (Tcl_TimerToken) NULL;
    } else {
	BIO_clear_flags(statePtr->p_bio, BIO_FLAGS_READ);
    }

    if (BIO_wpending(statePtr->bio)) {
	dprintf("[chan=%p] BIO writable", statePtr->self);

    if (mask & TCL_WRITABLE) {
	mask |= TCL_WRITABLE;
	BIO_set_flags(statePtr->p_bio, BIO_FLAGS_WRITE);
    } else {
	BIO_clear_flags(statePtr->p_bio, BIO_FLAGS_WRITE);
    }

    mask = 0;
    if (BIO_wpending(statePtr->bio)) {
    if (BIO_pending(statePtr->bio)) {
	mask |= TCL_WRITABLE;
    }
	dprintf("[chan=%p] BIO readable", statePtr->self);

    if (BIO_pending(statePtr->bio)) {
	mask |= TCL_READABLE;
    }

    /*
     * The following NotifyChannel calls seems to be important, but
     * we don't know why.  It looks like if the mask is ever non-zero
     * that it will enter an infinite loop.
     *
     * Notify the upper channel of the current BIO state so the event
     * continues to propagate up the chain.
     *
     * stanton: It looks like this could result in an infinite loop if
     * the upper channel doesn't cause ChannelHandler to be removed
     * before Tcl_NotifyChannel calls channel handlers on the lower channel.
     */

    dprintf("Notifying ourselves");
    Tcl_NotifyChannel(statePtr->self, mask);

    if (statePtr->timer != (Tcl_TimerToken)NULL) {
	Tcl_DeleteTimerHandler(statePtr->timer);
	statePtr->timer = (Tcl_TimerToken)NULL;
    }
    dprintf("Returning");

    return;
}
    if ((mask & TCL_READABLE) && Tcl_InputBuffered(statePtr->self) > 0) {
	/*
	 * Data is waiting, flush it out in short time
	 */
	statePtr->timer = Tcl_CreateTimerHandler(TLS_TCL_DELAY,
		TlsChannelHandlerTimer, statePtr);
    }
    Tcl_Release(statePtr);
}
#endif


Tcl_Channel Tls_GetParent(State *statePtr, int maskFlags) {
    dprintf("Requested to get parent of channel %p", statePtr->self);

    if ((statePtr->flags & ~maskFlags) & TLS_TCL_FASTPATH) {
	dprintf("Asked to get the parent channel while we are using FastPath -- returning NULL");
	return(NULL);
    }
/*
 *------------------------------------------------------*
 *
 *	TlsChannelHandlerTimer --
    return Tcl_GetStackedChannel(statePtr->self);
 *
 *	------------------------------------------------*
 *	Called by the notifier (-> timer) to flush out
 *	information waiting in channel buffers.
 *	------------------------------------------------*
 *
 *	Sideeffects:
 *		As of 'TlsChannelHandler'.
 *
 *	Result:
 *		None.
 *
 *------------------------------------------------------*
 */

}

/*
 *-------------------------------------------------------------------
 *
 * Tls_ChannelType --
 *
 *    Return the correct TLS channel driver info
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
856
857
858
859
860
861
862















































-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
    NULL,			/* Thread action */
    NULL			/* Truncate */
};

const Tcl_ChannelType *Tls_ChannelType(void) {
    return &tlsChannelType;
}


static void TlsChannelHandlerTimer(void *clientData) {
	State *statePtr = (State *) clientData;
	int mask = 0;

	dprintf("Called");

	statePtr->timer = (Tcl_TimerToken) NULL;

	if (BIO_wpending(statePtr->bio)) {
		dprintf("[chan=%p] BIO writable", statePtr->self);

		mask |= TCL_WRITABLE;
	}

	if (BIO_pending(statePtr->bio)) {
		dprintf("[chan=%p] BIO readable", statePtr->self);

		mask |= TCL_READABLE;
	}

	dprintf("Notifying ourselves");
	Tcl_NotifyChannel(statePtr->self, mask);

	dprintf("Returning");

	return;
}

Tcl_Channel Tls_GetParent(State *statePtr, int maskFlags) {
	dprintf("Requested to get parent of channel %p", statePtr->self);

	if ((statePtr->flags & ~maskFlags) & TLS_TCL_FASTPATH) {
		dprintf("Asked to get the parent channel while we are using FastPath -- returning NULL");
		return(NULL);
	}

	return(Tcl_GetStackedChannel(statePtr->self));
}