rewrite comments into one, unified style
This commit is contained in:
1 parent
6260de0085
commit
467a58c9c6
163 files changed
+3560
-2556
No files matched your search
@@ -1,4 +1,5 @@
|
||||
// posix_socket.c - POSIX-ish socket wrappers over native Sys* sockets.
|
||||
// POSIX compatibility support for posix socket.
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
@@ -7,10 +8,12 @@
|
||||
#include "posix.h"
|
||||
#include "posix_fd.h"
|
||||
|
||||
// Implements the bswap16 operation.
|
||||
static uint16_t bswap16(uint16_t x) {
|
||||
return (uint16_t)((x << 8) | (x >> 8));
|
||||
}
|
||||
|
||||
// Implements the bswap32 operation.
|
||||
static uint32_t bswap32(uint32_t x) {
|
||||
return ((x & 0x000000FFu) << 24) |
|
||||
((x & 0x0000FF00u) << 8) |
|
||||
@@ -18,11 +21,16 @@ static uint32_t bswap32(uint32_t x) {
|
||||
((x & 0xFF000000u) >> 24);
|
||||
}
|
||||
|
||||
// Implements the htons operation.
|
||||
__declspec(dllexport) uint16_t htons(uint16_t x) { return bswap16(x); }
|
||||
// Implements the ntohs operation.
|
||||
__declspec(dllexport) uint16_t ntohs(uint16_t x) { return bswap16(x); }
|
||||
// Implements the htonl operation.
|
||||
__declspec(dllexport) uint32_t htonl(uint32_t x) { return bswap32(x); }
|
||||
// Implements the ntohl operation.
|
||||
__declspec(dllexport) uint32_t ntohl(uint32_t x) { return bswap32(x); }
|
||||
|
||||
// Parses byte.
|
||||
static int ParseByte(const char** p, uint8_t* out) {
|
||||
const char* s = *p;
|
||||
if (*s < '0' || *s > '9') return 0;
|
||||
@@ -37,6 +45,7 @@ static int ParseByte(const char** p, uint8_t* out) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Parses ipv4 Bytes.
|
||||
static int ParseIpv4Bytes(const char* text, uint8_t out[4]) {
|
||||
const char* p = text;
|
||||
for (int i = 0; i < 4; i++) {
|
||||
@@ -49,6 +58,7 @@ static int ParseIpv4Bytes(const char* text, uint8_t out[4]) {
|
||||
return *p == '\0';
|
||||
}
|
||||
|
||||
// Implements the inet addr operation.
|
||||
__declspec(dllexport) in_addr_t inet_addr(const char* text) {
|
||||
uint8_t b[4];
|
||||
if (!text || !ParseIpv4Bytes(text, b)) return INADDR_NONE;
|
||||
@@ -58,6 +68,7 @@ __declspec(dllexport) in_addr_t inet_addr(const char* text) {
|
||||
return out;
|
||||
}
|
||||
|
||||
// Implements the inet ntoa operation.
|
||||
__declspec(dllexport) char* inet_ntoa(struct in_addr in) {
|
||||
static char buf[16];
|
||||
const uint8_t* p = (const uint8_t*)&in.s_addr;
|
||||
@@ -74,6 +85,7 @@ __declspec(dllexport) char* inet_ntoa(struct in_addr in) {
|
||||
return buf;
|
||||
}
|
||||
|
||||
// Implements the sock Addr To Sys operation.
|
||||
static int SockAddrToSys(const struct sockaddr* addr, socklen_t addrlen, SYS_SOCKET_ADDRESS_IPV4* out) {
|
||||
if (!addr || !out || addrlen < sizeof(struct sockaddr_in)) { errno = EINVAL; return -1; }
|
||||
const struct sockaddr_in* in = (const struct sockaddr_in*)addr;
|
||||
@@ -90,6 +102,7 @@ static int SockAddrToSys(const struct sockaddr* addr, socklen_t addrlen, SYS_SOC
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Implements the to Sock Addr operation.
|
||||
static void SysToSockAddr(const SYS_SOCKET_ADDRESS_IPV4* in, struct sockaddr* addr, socklen_t* addrlen) {
|
||||
if (!addr || !addrlen) return;
|
||||
if (*addrlen < sizeof(struct sockaddr_in)) {
|
||||
@@ -109,6 +122,7 @@ static void SysToSockAddr(const SYS_SOCKET_ADDRESS_IPV4* in, struct sockaddr* ad
|
||||
*addrlen = sizeof(struct sockaddr_in);
|
||||
}
|
||||
|
||||
// Implements the socket Fd Get operation.
|
||||
static FD_ENTRY* SocketFdGet(int fd) {
|
||||
FD_ENTRY* e = FdGet(SysGetCurrentPid(), fd);
|
||||
if (!e) return (FD_ENTRY*)0;
|
||||
@@ -116,6 +130,7 @@ static FD_ENTRY* SocketFdGet(int fd) {
|
||||
return e;
|
||||
}
|
||||
|
||||
// Implements the socket operation.
|
||||
__declspec(dllexport) int socket(int domain, int type, int protocol) {
|
||||
uint32_t kh = 0;
|
||||
uint32_t s = SysCreateSocket((uint32_t)domain, (uint32_t)type, (uint32_t)protocol, &kh);
|
||||
@@ -129,6 +144,7 @@ __declspec(dllexport) int socket(int domain, int type, int protocol) {
|
||||
return fd;
|
||||
}
|
||||
|
||||
// Implements the bind operation.
|
||||
__declspec(dllexport) int bind(int fd, const struct sockaddr* addr, socklen_t addrlen) {
|
||||
FD_ENTRY* e = SocketFdGet(fd);
|
||||
if (!e) return -1;
|
||||
@@ -139,6 +155,7 @@ __declspec(dllexport) int bind(int fd, const struct sockaddr* addr, socklen_t ad
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Implements the listen operation.
|
||||
__declspec(dllexport) int listen(int fd, int backlog) {
|
||||
FD_ENTRY* e = SocketFdGet(fd);
|
||||
if (!e) return -1;
|
||||
@@ -147,6 +164,7 @@ __declspec(dllexport) int listen(int fd, int backlog) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Implements the accept operation.
|
||||
__declspec(dllexport) int accept(int fd, struct sockaddr* addr, socklen_t* addrlen) {
|
||||
FD_ENTRY* e = SocketFdGet(fd);
|
||||
if (!e) return -1;
|
||||
@@ -167,6 +185,7 @@ __declspec(dllexport) int accept(int fd, struct sockaddr* addr, socklen_t* addrl
|
||||
return outfd;
|
||||
}
|
||||
|
||||
// Implements the connect operation.
|
||||
__declspec(dllexport) int connect(int fd, const struct sockaddr* addr, socklen_t addrlen) {
|
||||
FD_ENTRY* e = SocketFdGet(fd);
|
||||
if (!e) return -1;
|
||||
@@ -177,6 +196,7 @@ __declspec(dllexport) int connect(int fd, const struct sockaddr* addr, socklen_t
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Implements the send operation.
|
||||
__declspec(dllexport) ssize_t send(int fd, const void* buf, size_t len, int flags) {
|
||||
FD_ENTRY* e = SocketFdGet(fd);
|
||||
if (!e) return -1;
|
||||
@@ -186,6 +206,7 @@ __declspec(dllexport) ssize_t send(int fd, const void* buf, size_t len, int flag
|
||||
return (ssize_t)sent;
|
||||
}
|
||||
|
||||
// Implements the recv operation.
|
||||
__declspec(dllexport) ssize_t recv(int fd, void* buf, size_t len, int flags) {
|
||||
FD_ENTRY* e = SocketFdGet(fd);
|
||||
if (!e) return -1;
|
||||
@@ -240,6 +261,7 @@ __declspec(dllexport) ssize_t recvfrom(int fd, void* buf, size_t len, int flags,
|
||||
return (ssize_t)got;
|
||||
}
|
||||
|
||||
// Implements the shutdown operation.
|
||||
__declspec(dllexport) int shutdown(int fd, int how) {
|
||||
FD_ENTRY* e = SocketFdGet(fd);
|
||||
if (!e) return -1;
|
||||
@@ -248,12 +270,14 @@ __declspec(dllexport) int shutdown(int fd, int how) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Implements the timeval To Ms operation.
|
||||
static uint32_t TimevalToMs(const struct timeval* tv) {
|
||||
if (!tv) return 0;
|
||||
uint64_t ms = (uint64_t)tv->tv_sec * 1000u + (uint64_t)(tv->tv_usec / 1000);
|
||||
return ms > 0xFFFFFFFFu ? 0xFFFFFFFFu : (uint32_t)ms;
|
||||
}
|
||||
|
||||
// Implements the setsockopt operation.
|
||||
__declspec(dllexport) int setsockopt(int fd, int level, int optname, const void* optval, socklen_t optlen) {
|
||||
if (level != SOL_SOCKET || !optval) { errno = EINVAL; return -1; }
|
||||
FD_ENTRY* e = SocketFdGet(fd);
|
||||
@@ -285,6 +309,7 @@ __declspec(dllexport) int setsockopt(int fd, int level, int optname, const void*
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Implements the getsockopt operation.
|
||||
__declspec(dllexport) int getsockopt(int fd, int level, int optname, void* optval, socklen_t* optlen) {
|
||||
if (level != SOL_SOCKET || !optval || !optlen) { errno = EINVAL; return -1; }
|
||||
FD_ENTRY* e = SocketFdGet(fd);
|
||||
@@ -322,6 +347,7 @@ __declspec(dllexport) int getsockopt(int fd, int level, int optname, void* optva
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Polls the requested operation.
|
||||
__declspec(dllexport) int poll(struct pollfd* fds, nfds_t nfds, int timeout) {
|
||||
if (!fds || nfds == 0 || nfds > 64) { errno = EINVAL; return -1; }
|
||||
|
||||
@@ -370,6 +396,7 @@ __declspec(dllexport) int poll(struct pollfd* fds, nfds_t nfds, int timeout) {
|
||||
return out;
|
||||
}
|
||||
|
||||
// Parses port.
|
||||
static unsigned ParsePort(const char* s) {
|
||||
unsigned v = 0;
|
||||
if (!s) return 0;
|
||||
@@ -381,6 +408,7 @@ static unsigned ParsePort(const char* s) {
|
||||
return *s == '\0' ? v : 0;
|
||||
}
|
||||
|
||||
// Implements the resolve Name Ipv4 operation.
|
||||
static int ResolveNameIpv4(const char* name, SYS_SOCKET_ADDRESS_IPV4* out) {
|
||||
uint8_t b[4];
|
||||
if (ParseIpv4Bytes(name, b)) {
|
||||
@@ -402,6 +430,7 @@ static int ResolveNameIpv4(const char* name, SYS_SOCKET_ADDRESS_IPV4* out) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Implements the gethostbyname operation.
|
||||
__declspec(dllexport) struct hostent* gethostbyname(const char* name) {
|
||||
static struct hostent h;
|
||||
static char hname[256];
|
||||
@@ -467,6 +496,7 @@ __declspec(dllexport) int getaddrinfo(const char* node, const char* service,
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Implements the freeaddrinfo operation.
|
||||
__declspec(dllexport) void freeaddrinfo(struct addrinfo* res) {
|
||||
if (res) free(res);
|
||||
}
|
||||
Reference in new issue
Block a user