Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion ext/include/opentelemetry/ext/http/server/http_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -201,11 +201,18 @@ class HttpServer : private SocketTools::Reactor::SocketCallback

int addListeningPort(int port)
{


SocketTools::SocketAddr addr(0, port);
if (addr.port() == -1)
{
return -1;
}

SocketTools::Socket socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
socket.setNonBlocking();
socket.setReuseAddr();

SocketTools::SocketAddr addr(0, port);
socket.bind(addr);
socket.getsockname(addr);

Expand Down
5 changes: 5 additions & 0 deletions ext/include/opentelemetry/ext/http/server/socket_tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,11 @@ struct SocketAddr

SocketAddr(u_long addr, int port)
{
if (port < 0 || port > 65535)
{
return;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of doing all this, how about changing port to uint16_t ?

sockaddr_in &inet4 = reinterpret_cast<sockaddr_in &>(m_data);
inet4.sin_family = AF_INET;
inet4.sin_port = htons(static_cast<uint16_t>(port));
Expand Down
7 changes: 7 additions & 0 deletions ext/test/http/socket_tools_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ TEST(SocketAddrTest, AcceptsLeadingZeroPort)
EXPECT_EQ(addr.port(), 80);
}

TEST(SocketAddrTest, RejectsOutOfRangeIntegerPorts)
{
ExpectInvalid(SocketTools::SocketAddr(0, -1));
ExpectInvalid(SocketTools::SocketAddr(0, 65536));
ExpectInvalid(SocketTools::SocketAddr(0, 99999));
}

TEST(SocketAddrTest, RejectsOutOfRangePort)
{
SocketTools::SocketAddr addr("127.0.0.1:99999");
Expand Down
Loading