Skip to content

Commit

Permalink
#2418: SecureServerSocket doesn't work with IpV6
Browse files Browse the repository at this point in the history
  • Loading branch information
obiltschnig committed Jun 15, 2021
1 parent 59ae91e commit 7917abf
Show file tree
Hide file tree
Showing 9 changed files with 355 additions and 70 deletions.
4 changes: 2 additions & 2 deletions Net/include/Poco/Net/SocketImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class Net_API SocketImpl: public Poco::RefCountedObject
/// If reuseAddress is true, sets the SO_REUSEADDR
/// socket option.

virtual void bind(const SocketAddress& address, bool reuseAddress, bool reusePort );
virtual void bind(const SocketAddress& address, bool reuseAddress, bool reusePort);
/// Bind a local address to the socket.
///
/// This is usually only done when establishing a server
Expand Down Expand Up @@ -115,7 +115,7 @@ class Net_API SocketImpl: public Poco::RefCountedObject
/// If the library has not been built with IPv6 support,
/// a Poco::NotImplementedException will be thrown.

virtual void bind6(const SocketAddress& address, bool reuseAddress, bool reusePort, bool ipV6Only);
virtual void bind6(const SocketAddress& address, bool reuseAddress, bool reusePort, bool ipV6Only);
/// Bind a local IPv6 address to the socket.
///
/// This is usually only done when establishing a server
Expand Down
66 changes: 56 additions & 10 deletions NetSSL_OpenSSL/include/Poco/Net/SecureServerSocketImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class NetSSL_API SecureServerSocketImpl: public ServerSocketImpl
/// with the client.
///
/// The client socket's address is returned in clientAddr.

void connect(const SocketAddress& address);
/// Not supported by this kind of socket.
///
Expand All @@ -62,8 +62,18 @@ class NetSSL_API SecureServerSocketImpl: public ServerSocketImpl
/// Not supported by this kind of socket.
///
/// Throws a Poco::InvalidAccessException.

void bind(const SocketAddress& address, bool reuseAddress = false, bool reusePort = false);

void bind(const SocketAddress& address, bool reuseAddress = false);
/// Bind a local address to the socket.
///
/// This is usually only done when establishing a server
/// socket. TCP clients should not bind a socket to a
/// specific address.
///
/// If reuseAddress is true, sets the SO_REUSEADDR
/// socket option.

void bind(const SocketAddress& address, bool reuseAddress, bool reusePort);
/// Bind a local address to the socket.
///
/// This is usually only done when establishing a server
Expand All @@ -76,7 +86,43 @@ class NetSSL_API SecureServerSocketImpl: public ServerSocketImpl
/// If reusePort is true, sets the SO_REUSEPORT
/// socket option.


void bind6(const SocketAddress& address, bool reuseAddress = false, bool ipV6Only = false);
/// Bind a local IPv6 address to the socket.
///
/// This is usually only done when establishing a server
/// socket. TCP clients should not bind a socket to a
/// specific address.
///
/// If reuseAddress is true, sets the SO_REUSEADDR
/// socket option.
///
/// The given address must be an IPv6 address. The
/// IPPROTO_IPV6/IPV6_V6ONLY option is set on the socket
/// according to the ipV6Only parameter.
///
/// If the library has not been built with IPv6 support,
/// a Poco::NotImplementedException will be thrown.

void bind6(const SocketAddress& address, bool reuseAddress, bool reusePort, bool ipV6Only);
/// Bind a local IPv6 address to the socket.
///
/// This is usually only done when establishing a server
/// socket. TCP clients should not bind a socket to a
/// specific address.
///
/// If reuseAddress is true, sets the SO_REUSEADDR
/// socket option.
///
/// If reusePort is true, sets the SO_REUSEPORT
/// socket option.
///
/// The given address must be an IPv6 address. The
/// IPPROTO_IPV6/IPV6_V6ONLY option is set on the socket
/// according to the ipV6Only parameter.
///
/// If the library has not been built with IPv6 support,
/// a Poco::NotImplementedException will be thrown.

void listen(int backlog = 64);
/// Puts the socket into listening state.
///
Expand All @@ -89,27 +135,27 @@ class NetSSL_API SecureServerSocketImpl: public ServerSocketImpl

void close();
/// Close the socket.

int sendBytes(const void* buffer, int length, int flags = 0);
/// Not supported by this kind of socket.
///
/// Throws a Poco::InvalidAccessException.

int receiveBytes(void* buffer, int length, int flags = 0);
/// Not supported by this kind of socket.
///
/// Throws a Poco::InvalidAccessException.

int sendTo(const void* buffer, int length, const SocketAddress& address, int flags = 0);
/// Not supported by this kind of socket.
///
/// Throws a Poco::InvalidAccessException.

int receiveFrom(void* buffer, int length, SocketAddress& address, int flags = 0);
/// Not supported by this kind of socket.
///
/// Throws a Poco::InvalidAccessException.

void sendUrgent(unsigned char data);
/// Not supported by this kind of socket.
///
Expand All @@ -118,7 +164,7 @@ class NetSSL_API SecureServerSocketImpl: public ServerSocketImpl
bool secure() const;
/// Returns true iff the socket's connection is secure
/// (using SSL or TLS).

Context::Ptr context() const;
/// Returns the SSL context used by this socket.

Expand Down
105 changes: 76 additions & 29 deletions NetSSL_OpenSSL/include/Poco/Net/SecureSocketImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,29 +56,39 @@ class NetSSL_API SecureSocketImpl
/// with the client.
///
/// The client socket's address is returned in clientAddr.

void connect(const SocketAddress& address, bool performHandshake);
/// Initializes the socket and establishes a secure connection to
/// Initializes the socket and establishes a secure connection to
/// the TCP server at the given address.
///
/// If performHandshake is true, the SSL handshake is performed immediately
/// If performHandshake is true, the SSL handshake is performed immediately
/// after establishing the connection. Otherwise, the handshake is performed
/// the first time sendBytes(), receiveBytes() or completeHandshake() is called.

void connect(const SocketAddress& address, const Poco::Timespan& timeout, bool performHandshake);
/// Initializes the socket, sets the socket timeout and
/// Initializes the socket, sets the socket timeout and
/// establishes a secure connection to the TCP server at the given address.
///
/// If performHandshake is true, the SSL handshake is performed immediately
/// If performHandshake is true, the SSL handshake is performed immediately
/// after establishing the connection. Otherwise, the handshake is performed
/// the first time sendBytes(), receiveBytes() or completeHandshake() is called.

void connectNB(const SocketAddress& address);
/// Initializes the socket and establishes a secure connection to
/// Initializes the socket and establishes a secure connection to
/// the TCP server at the given address. Prior to opening the
/// connection the socket is set to nonblocking mode.

void bind(const SocketAddress& address, bool reuseAddress = false, bool reusePort = false);
void bind(const SocketAddress& address, bool reuseAddress = false);
/// Bind a local address to the socket.
///
/// This is usually only done when establishing a server
/// socket. TCP clients should not bind a socket to a
/// specific address.
///
/// If reuseAddress is true, sets the SO_REUSEADDR
/// socket option.

void bind(const SocketAddress& address, bool reuseAddress, bool reusePort);
/// Bind a local address to the socket.
///
/// This is usually only done when establishing a server
Expand All @@ -90,7 +100,44 @@ class NetSSL_API SecureSocketImpl
///
/// If reusePort is true, sets the SO_REUSEPORT
/// socket option.


void bind6(const SocketAddress& address, bool reuseAddress = false, bool ipV6Only = false);
/// Bind a local IPv6 address to the socket.
///
/// This is usually only done when establishing a server
/// socket. TCP clients should not bind a socket to a
/// specific address.
///
/// If reuseAddress is true, sets the SO_REUSEADDR
/// socket option.
///
/// The given address must be an IPv6 address. The
/// IPPROTO_IPV6/IPV6_V6ONLY option is set on the socket
/// according to the ipV6Only parameter.
///
/// If the library has not been built with IPv6 support,
/// a Poco::NotImplementedException will be thrown.

void bind6(const SocketAddress& address, bool reuseAddress, bool reusePort, bool ipV6Only);
/// Bind a local IPv6 address to the socket.
///
/// This is usually only done when establishing a server
/// socket. TCP clients should not bind a socket to a
/// specific address.
///
/// If reuseAddress is true, sets the SO_REUSEADDR
/// socket option.
///
/// If reusePort is true, sets the SO_REUSEPORT
/// socket option.
///
/// The given address must be an IPv6 address. The
/// IPPROTO_IPV6/IPV6_V6ONLY option is set on the socket
/// according to the ipV6Only parameter.
///
/// If the library has not been built with IPv6 support,
/// a Poco::NotImplementedException will be thrown.

void listen(int backlog = 64);
/// Puts the socket into listening state.
///
Expand All @@ -108,42 +155,42 @@ class NetSSL_API SecureSocketImpl

void close();
/// Close the socket.

void abort();
/// Aborts the connection by closing the
/// underlying TCP connection. No orderly SSL shutdown
/// is performed.

int sendBytes(const void* buffer, int length, int flags = 0);
/// Sends the contents of the given buffer through
/// the socket. Any specified flags are ignored.
///
/// Returns the number of bytes sent, which may be
/// less than the number of bytes specified.

int receiveBytes(void* buffer, int length, int flags = 0);
/// Receives data from the socket and stores it
/// in buffer. Up to length bytes are received.
///
/// Returns the number of bytes received.

int available() const;
/// Returns the number of bytes available from the
/// SSL buffer for immediate reading.

int completeHandshake();
/// Completes the SSL handshake.
///
/// If the SSL connection was the result of an accept(),
/// the server-side handshake is completed, otherwise
/// a client-side handshake is performed.
/// a client-side handshake is performed.

poco_socket_t sockfd();
/// Returns the underlying socket descriptor.

X509* peerCertificate() const;
/// Returns the peer's certificate.

Context::Ptr context() const;
/// Returns the SSL context used for this socket.

Expand All @@ -158,17 +205,17 @@ class NetSSL_API SecureSocketImpl

void setPeerHostName(const std::string& hostName);
/// Sets the peer host name for certificate validation purposes.

const std::string& getPeerHostName() const;
/// Returns the peer host name.

Session::Ptr currentSession();
/// Returns the SSL session of the current connection,
/// for reuse in a future connection (if session caching
/// is enabled).
///
/// If no connection is established, returns null.

void useSession(Session::Ptr pSession);
/// Sets the SSL session to use for the next
/// connection. Setting a previously saved Session
Expand All @@ -178,31 +225,31 @@ class NetSSL_API SecureSocketImpl
/// can be given.
///
/// Must be called before connect() to be effective.

bool sessionWasReused();
/// Returns true iff a reused session was negotiated during
/// the handshake.

protected:
void acceptSSL();
/// Performs a server-side SSL handshake and certificate verification.

void connectSSL(bool performHandshake);
/// Performs a client-side SSL handshake and establishes a secure
/// Performs a client-side SSL handshake and establishes a secure
/// connection over an already existing TCP connection.

long verifyPeerCertificateImpl(const std::string& hostName);
/// Performs post-connect (or post-accept) peer certificate validation.

static bool isLocalHost(const std::string& hostName);
/// Returns true iff the given host name is the local host
/// Returns true iff the given host name is the local host
/// (either "localhost" or "127.0.0.1").

bool mustRetry(int rc);
/// Returns true if the last operation should be retried,
/// otherwise false.
///
/// In case of an SSL_ERROR_WANT_READ error, and if the socket is
/// In case of an SSL_ERROR_WANT_READ error, and if the socket is
/// blocking, waits for the underlying socket to become readable.
///
/// In case of an SSL_ERROR_WANT_WRITE error, and if the socket is
Expand All @@ -216,15 +263,15 @@ class NetSSL_API SecureSocketImpl
/// Handles an SSL error by throwing an appropriate exception.

void reset();
/// Prepares the socket for re-use.
/// Prepares the socket for re-use.
///
/// After closing and resetting a socket, the socket can
/// be used for a new connection.
///
/// Note that simply closing a socket is not sufficient
/// to be able to re-use it again.

private:
private:
SecureSocketImpl(const SecureSocketImpl&);
SecureSocketImpl& operator = (const SecureSocketImpl&);

Expand All @@ -234,7 +281,7 @@ class NetSSL_API SecureSocketImpl
bool _needHandshake;
std::string _peerHostName;
Session::Ptr _pSession;

friend class SecureStreamSocketImpl;
};

Expand Down
Loading

0 comments on commit 7917abf

Please sign in to comment.