Skip to content

Instantly share code, notes, and snippets.

@waveto
Created February 23, 2010 22:29

Revisions

  1. waveto created this gist Feb 23, 2010.
    73 changes: 73 additions & 0 deletions node.js starttls
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,73 @@
    From 5a42382681452a6fe94c2224cd7a66047f417a96 Mon Sep 17 00:00:00 2001
    From: Rhys Jones <[email protected]>
    Date: Tue, 23 Feb 2010 22:27:35 +0000
    Subject: [PATCH] Allow setSecure() to be called on an existing connection, to support STARTTLS handshaking

    ---
    deps/evcom/evcom.c | 22 ++++++++++++++++++++++
    deps/evcom/evcom.h | 1 +
    src/node_net.cc | 3 +++
    3 files changed, 26 insertions(+), 0 deletions(-)

    diff --git a/deps/evcom/evcom.c b/deps/evcom/evcom.c
    index 1ee02a1..82e2811 100644
    --- a/deps/evcom/evcom.c
    +++ b/deps/evcom/evcom.c
    @@ -1317,6 +1317,28 @@ evcom_stream_connect (evcom_stream *stream, struct sockaddr *address)
    return 0;
    }

    +int
    +evcom_stream_starttls (evcom_stream *stream)
    +{
    +#if EVCOM_HAVE_GNUTLS
    + if (SECURE(stream)) {
    + gnutls_transport_set_lowat(stream->session, 0);
    + gnutls_transport_set_push_function(stream->session, nosigpipe_push);
    + gnutls_transport_set_pull_function(stream->session, pull);
    + gnutls_transport_set_ptr2(stream->session, stream, stream);
    +
    + stream->send_action = stream__handshake;
    + stream->recv_action = stream__handshake;
    +
    + ev_io_start(D_LOOP_(stream) &stream->write_watcher);
    + ev_io_start(D_LOOP_(stream) &stream->read_watcher);
    + }
    +#endif
    +
    + return OKAY;
    +}
    +
    +
    int evcom_stream_pair (evcom_stream *a, evcom_stream *b)
    {
    int sv[2];
    diff --git a/deps/evcom/evcom.h b/deps/evcom/evcom.h
    index fd03a5b..83b44e9 100644
    --- a/deps/evcom/evcom.h
    +++ b/deps/evcom/evcom.h
    @@ -186,6 +186,7 @@ void evcom_stream_init (evcom_stream *);

    int evcom_stream_pair (evcom_stream *a, evcom_stream *b);
    int evcom_stream_connect (evcom_stream *, struct sockaddr *address);
    + int evcom_stream_starttls (evcom_stream *);
    void evcom_stream_assign_fds (evcom_stream *, int recvfd, int sendfd);

    void evcom_stream_attach (EV_P_ evcom_stream *);
    diff --git a/src/node_net.cc b/src/node_net.cc
    index da43517..5141944 100644
    --- a/src/node_net.cc
    +++ b/src/node_net.cc
    @@ -940,6 +940,9 @@ void init_tls_session(evcom_stream* stream_,
    credentials);
    evcom_stream_set_secure_session(stream_,
    stream_->session);
    + if (stream_->flags & EVCOM_CONNECTED) {
    + evcom_stream_starttls(stream_);
    + }
    }


    --
    1.6.4.4