1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-09 12:02:21 +02:00

Merge pull request #1696 from m910q/SocketListenWithIP

Require listen IP when calling Socket.Listen()
This commit is contained in:
Brian Fiete 2022-08-27 00:27:03 +02:00 committed by GitHub
commit e56a08a72e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -117,7 +117,7 @@ namespace System.Net
#endif #endif
[CRepr] [CRepr]
struct in_addr public struct IPv4Address
{ {
public uint8 b1; public uint8 b1;
public uint8 b2; public uint8 b2;
@ -144,7 +144,7 @@ namespace System.Net
{ {
public int16 sin_family; public int16 sin_family;
public uint16 sin_port; public uint16 sin_port;
public in_addr sin_addr; public IPv4Address sin_addr;
public char8[8] sin_zero; public char8[8] sin_zero;
} }
@ -340,6 +340,11 @@ namespace System.Net
} }
public Result<void> Listen(int32 port, int32 backlog = 5) public Result<void> Listen(int32 port, int32 backlog = 5)
{
return Listen(.(127, 0, 0, 1), port, backlog);
}
public Result<void> Listen(IPv4Address address, int32 port, int32 backlog = 5)
{ {
Debug.Assert(mHandle == INVALID_SOCKET); Debug.Assert(mHandle == INVALID_SOCKET);
@ -356,7 +361,7 @@ namespace System.Net
SockAddr_in service; SockAddr_in service;
service.sin_family = AF_INET; service.sin_family = AF_INET;
service.sin_addr = in_addr(127, 0, 0, 1); service.sin_addr = address;
service.sin_port = (uint16)htons((int16)port); service.sin_port = (uint16)htons((int16)port);
if (bind(mHandle, &service, sizeof(SockAddr_in)) == SOCKET_ERROR) if (bind(mHandle, &service, sizeof(SockAddr_in)) == SOCKET_ERROR)
@ -388,7 +393,7 @@ namespace System.Net
SockAddr_in sockAddr; SockAddr_in sockAddr;
sockAddr.sin_family = AF_INET; sockAddr.sin_family = AF_INET;
Internal.MemCpy(&sockAddr.sin_addr, hostEnt.h_addr_list[0], sizeof(in_addr)); Internal.MemCpy(&sockAddr.sin_addr, hostEnt.h_addr_list[0], sizeof(IPv4Address));
sockAddr.sin_port = (uint16)htons((int16)port); sockAddr.sin_port = (uint16)htons((int16)port);
if (connect(mHandle, &sockAddr, sizeof(SockAddr_in)) == SOCKET_ERROR) if (connect(mHandle, &sockAddr, sizeof(SockAddr_in)) == SOCKET_ERROR)