Skip to content

Commit

Permalink
netserver/tcp: Add getsockname and getpeername
Browse files Browse the repository at this point in the history
Signed-off-by: Dennis Bonke <[email protected]>
  • Loading branch information
Dennisbonke committed Oct 28, 2023
1 parent cd4d5c2 commit b3c1565
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions servers/netserver/src/ip/tcp4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -410,17 +410,54 @@ struct Tcp4Socket {
co_return 0;
}

static async::result<size_t> sockname(void *obj, void *addrPtr, size_t maxAddrLength) {
sockaddr_in sa;
in_addr addr;
auto self = static_cast<Tcp4Socket *>(obj);

memset(&sa, 0, sizeof(struct sockaddr_in));
memset(&addr, 0, sizeof(struct in_addr));
sa.sin_family = AF_INET;
addr.s_addr = htonl(self->localEp_.ipAddress);
sa.sin_addr = addr;
sa.sin_port = self->localEp_.port;

auto destSize = std::min(sizeof(sockaddr_in), maxAddrLength);
memcpy(addrPtr, &sa, destSize);
co_return destSize;
}

static async::result<frg::expected<protocols::fs::Error, size_t>>
peername(void *obj, void *addrPtr, size_t maxAddrLength) {
sockaddr_in sa;
in_addr addr;
auto self = static_cast<Tcp4Socket *>(obj);

memset(&sa, 0, sizeof(struct sockaddr_in));
memset(&addr, 0, sizeof(struct in_addr));
sa.sin_family = AF_INET;
addr.s_addr = htonl(self->remoteEp_.ipAddress);
sa.sin_addr = addr;
sa.sin_port = self->remoteEp_.port;

auto destSize = std::min(sizeof(sockaddr_in), maxAddrLength);
memcpy(addrPtr, &sa, destSize);
co_return destSize;
}

constexpr static protocols::fs::FileOperations ops {
.read = &read,
.write = &write,
.pollWait = &pollWait,
.pollStatus = &pollStatus,
.bind = &bind,
.connect = &connect,
.sockname = &sockname,
.getFileFlags = &getFileFlags,
.setFileFlags = &setFileFlags,
.recvMsg = &recvMsg,
.sendMsg = &sendMsg,
.peername = &peername,
};

bool bindAvailable(uint32_t ipAddress = INADDR_ANY) {
Expand Down

0 comments on commit b3c1565

Please sign in to comment.