tango.net.device.SSLSocket

License:
BSD style:

author:
Jeff Davey

class SSLSocket: tango.net.device.Socket.Socket;
SSLSocket is a sub-class of Socket. It's purpose is to provide SSL encryption at the socket level as well as easily fit into existing Tango network applications that may already be using Socket.

SSLSocket requires the OpenSSL library, and uses a dynamic binding to the library. You can find the library at http://www.openssl.org and a Win32 specific port at http://www.slproweb.com/products/Win32OpenSSL.html.

SSLSockets have two modes:

1. Client mode, useful for connecting to existing servers, but not accepting new connections. Accepting a new connection will cause the library to stall on a write on connection.

2. Server mode, useful for creating an SSL server, but not connecting to an existing server. Connection will cause the library to stall on a read on connection.

Example SSL client
    auto s = new SSLSocket;
    if (s.connect("www.yahoo.com", 443))
    {
        char[1024] buff;

        s.write("GET / HTTP/1.0\r\n\r\n");
        auto bytesRead = s.read(buff);
        if (bytesRead != s.Eof)
            Stdout.formatln("received: {}", buff[0..bytesRead]);
    }


this(bool config = true);
Create a default Client Mode SSLSocket.

void detach();
Release this SSLSocket.

As per Socket.detach.

size_t write(const(void)[] src);
Writes the passed buffer to the underlying socket stream. This will block until socket error.

As per Socket.write

size_t read(void[] dst);
Reads from the underlying socket stream. If needed, setTimeout will set the max length of time the read will take before returning.

As per Socket.read

SSLSocket shutdown();
Shuts down the underlying socket for reading and writing.

As per Socket.shutdown

void setCtx(SSLCtx ctx, bool clientMode = true);
Used in conjuction with the above ctor with the create flag disabled. It is useful for accepting a new socket into a SSLSocket, and then re-using the Server's existing SSLCtx.

Params:
SSLCtx ctx SSLCtx class as provided by PKI
bool clientMode if true, the socket will be in Client Mode, Server otherwise.

class SSLServerSocket: tango.net.device.Socket.ServerSocket;
SSLServerSocket is a sub-class of ServerSocket. It's purpose is to provide SSL encryption at the socket level as well as easily tie into existing Tango applications that may already be using ServerSocket.

SSLServerSocket requires the OpenSSL library, and uses a dynamic binding to the library. You can find the library at http://www.openssl.org and a Win32 specific port at http://www.slproweb.com/products/Win32OpenSSL.html.

Example SSL server
    auto cert = new Certificate(cast(char[])File.get("public.pem"));
    auto pkey = new PrivateKey(cast(char[])File.get("private.pem"));
    auto ctx = new SSLCtx;
    ctx.certificate(cert).privateKey(pkey);
    auto server = new SSLServerSocket(443, ctx);
    for(;;)
    {
        auto sc = server.accept;
        sc.write("HTTP/1.1 200\r\n\r\n<b>Hello World</b>");
        sc.shutdown.close;
    }


this(ushort port, SSLCtx ctx, int backlog = 32, bool reuse = false);


this(Address addr, SSLCtx ctx, int backlog = 32, bool reuse = false);
Constructs a new SSLServerSocket. This constructor is similar to ServerSocket, except it takes a SSLCtx as provided by PKI.

Params:
Address addr the address to bind and listen on.
SSLCtx ctx the provided SSLCtx
int backlog the number of connections to backlog before refusing connection
bool reuse if enabled, allow rebinding of existing ip/port

SSLSocket accept(SSLSocket recipient = null);
Accepts a new connection and copies the provided server SSLCtx to a new SSLSocket.


Page generated by Ddoc. Copyright (c) 2008 Jeff Davey. All rights reserved