From 50c733796540d7a9c1a06a055f26f0023e31ad85 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Mon, 20 Aug 2018 16:06:58 +0200 Subject: [PATCH] net/SocketDescriptor: add GetType(), IsStream() --- src/net/SocketDescriptor.cxx | 19 +++++++++++++++++++ src/net/SocketDescriptor.hxx | 14 ++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/src/net/SocketDescriptor.cxx b/src/net/SocketDescriptor.cxx index a572da34e..395ed5401 100644 --- a/src/net/SocketDescriptor.cxx +++ b/src/net/SocketDescriptor.cxx @@ -44,6 +44,25 @@ #include #include +int +SocketDescriptor::GetType() const noexcept +{ + assert(IsDefined()); + + int type; + socklen_t size = sizeof(type); + return getsockopt(fd, SOL_SOCKET, SO_TYPE, + (char *)&type, &size) == 0 + ? type + : -1; +} + +bool +SocketDescriptor::IsStream() const noexcept +{ + return GetType() == SOCK_STREAM; +} + #ifdef _WIN32 void diff --git a/src/net/SocketDescriptor.hxx b/src/net/SocketDescriptor.hxx index ae00a6c16..33bde8c79 100644 --- a/src/net/SocketDescriptor.hxx +++ b/src/net/SocketDescriptor.hxx @@ -82,6 +82,20 @@ public: using FileDescriptor::IsValid; using FileDescriptor::IsSocket; #endif + + /** + * Determine the socket type, i.e. SOCK_STREAM, SOCK_DGRAM or + * SOCK_SEQPACKET. Returns -1 on error. + */ + gcc_pure + int GetType() const noexcept; + + /** + * Is this a stream socket? + */ + gcc_pure + bool IsStream() const noexcept; + using FileDescriptor::Get; using FileDescriptor::Set; using FileDescriptor::Steal;