From: John Hodge Date: Sat, 8 Mar 2014 06:04:25 +0000 (+0800) Subject: Usermode/libpsocket - Header wrapping, byte ordering functions X-Git-Url: https://git.ucc.asn.au/?a=commitdiff_plain;h=70d0f9abb42fb0b25f35b3fca91159e5c3a90316;p=tpg%2Facess2.git Usermode/libpsocket - Header wrapping, byte ordering functions --- diff --git a/Usermode/Libraries/libpsocket.so_src/Makefile b/Usermode/Libraries/libpsocket.so_src/Makefile index bd7f38d0..0ad326e5 100644 --- a/Usermode/Libraries/libpsocket.so_src/Makefile +++ b/Usermode/Libraries/libpsocket.so_src/Makefile @@ -6,7 +6,7 @@ CPPFLAGS += CFLAGS += -Wall LDFLAGS += -lc -soname libpsocket.so -lnet -OBJ = main.o getaddrinfo.o socket.o pton.o +OBJ = main.o getaddrinfo.o socket.o pton.o byteordering.o BIN = libpsocket.so include ../Makefile.tpl diff --git a/Usermode/Libraries/libpsocket.so_src/byteordering.c b/Usermode/Libraries/libpsocket.so_src/byteordering.c new file mode 100644 index 00000000..0c8b99df --- /dev/null +++ b/Usermode/Libraries/libpsocket.so_src/byteordering.c @@ -0,0 +1,58 @@ +/* + * Acess2 POSIX Sockets Library + * - By John Hodge (thePowersGang) + * + * byteordering.c + * - hton/ntoh + */ +#include + +// === CODE === +static uint32_t Flip32(uint32_t val) +{ + return (((val >> 24) & 0xFF) << 0) + | (((val >> 16) & 0xFF) << 8) + | (((val >> 8) & 0xFF) << 16) + | (((val >> 0) & 0xFF) << 24) + ; +} + +static uint16_t Flip16(uint16_t val) +{ + return (val >> 8) | (val << 8); +} + +uint32_t htonl(uint32_t hostlong) +{ + #if BIG_ENDIAN + return hostlong; + #else + return Flip32(hostlong); + #endif +} +uint16_t htons(uint16_t hostshort) +{ + #if BIG_ENDIAN + return hostshort; + #else + return Flip16(hostshort); + #endif +} +uint32_t ntohl(uint32_t netlong) +{ + #if BIG_ENDIAN + return netlong; + #else + return Flip32(netlong); + #endif +} +uint16_t ntohs(uint16_t netshort) +{ + #if BIG_ENDIAN + return netshort; + #else + return Flip16(netshort); + #endif +} + + diff --git a/Usermode/Libraries/libpsocket.so_src/getaddrinfo.c b/Usermode/Libraries/libpsocket.so_src/getaddrinfo.c index aa8128d9..fd935038 100644 --- a/Usermode/Libraries/libpsocket.so_src/getaddrinfo.c +++ b/Usermode/Libraries/libpsocket.so_src/getaddrinfo.c @@ -235,3 +235,8 @@ const char *gai_strerror(int errnum) } } +struct hostent *gethostbyname(const char *name) +{ + return NULL; +} + diff --git a/Usermode/Libraries/libpsocket.so_src/include_exp/arpa/inet.h b/Usermode/Libraries/libpsocket.so_src/include_exp/arpa/inet.h index effc2883..ea303ddb 100644 --- a/Usermode/Libraries/libpsocket.so_src/include_exp/arpa/inet.h +++ b/Usermode/Libraries/libpsocket.so_src/include_exp/arpa/inet.h @@ -11,6 +11,10 @@ #include #include // Should be inttypes.h? +#ifdef __cplusplus +extern "C" { +#endif + extern uint32_t htonl(uint32_t hostlong); extern uint16_t htons(uint16_t hostshort); extern uint32_t ntohl(uint32_t netlong); @@ -23,5 +27,9 @@ extern in_addr_t inet_netof(struct in_addr in); extern in_addr_t inet_network(const char *cp); extern char *inet_ntoa(struct in_addr in); +#ifdef __cplusplus +} +#endif + #endif diff --git a/Usermode/Libraries/libpsocket.so_src/include_exp/netdb.h b/Usermode/Libraries/libpsocket.so_src/include_exp/netdb.h index 95f6f2e1..943d514d 100644 --- a/Usermode/Libraries/libpsocket.so_src/include_exp/netdb.h +++ b/Usermode/Libraries/libpsocket.so_src/include_exp/netdb.h @@ -3,6 +3,11 @@ #include +#ifdef __cplusplus +extern "C" { +#endif + + struct hostent { char *h_name; @@ -11,6 +16,7 @@ struct hostent int h_length; char **h_addr_list; }; +#define h_addr h_addr_list[0] // backwards compataibility struct netent { @@ -96,9 +102,16 @@ const char *gai_strerror(int errorcode); extern struct servent *getservbyname(const char *name, const char *proto); extern struct servent *getservbyport(int port, const char *proto); +extern struct hostent *gethostbyname(const char *name); +extern struct hostent *gethostbyaddr(const void *addr, socklen_t len, int type); + extern void setservent(int stayopen); extern struct servent *getservent(void); extern void enservent(void); +#ifdef __cplusplus +} +#endif + #endif diff --git a/Usermode/Libraries/libpsocket.so_src/include_exp/netinet/in.h b/Usermode/Libraries/libpsocket.so_src/include_exp/netinet/in.h index d69ce86d..0f4e887f 100644 --- a/Usermode/Libraries/libpsocket.so_src/include_exp/netinet/in.h +++ b/Usermode/Libraries/libpsocket.so_src/include_exp/netinet/in.h @@ -58,5 +58,7 @@ struct sockaddr_in6 uint32_t sin6_scope_id; }; +#include // for hton*/ntoh* (bochs) + #endif diff --git a/Usermode/Libraries/libpsocket.so_src/include_exp/sys/socket.h b/Usermode/Libraries/libpsocket.so_src/include_exp/sys/socket.h index deb1f51e..d9ce4ecd 100644 --- a/Usermode/Libraries/libpsocket.so_src/include_exp/sys/socket.h +++ b/Usermode/Libraries/libpsocket.so_src/include_exp/sys/socket.h @@ -14,6 +14,10 @@ #endif #include // uint32_t +#ifdef __cplusplus +extern "C" { +#endif + typedef uint32_t socklen_t; typedef enum @@ -23,8 +27,9 @@ typedef enum AF_LOCAL = 2, AF_INET = 4, AF_INET6 = 6, -} sa_family_t; +}; #define AF_UNIX AF_LOCAL +typedef uint8_t sa_family_t; // I would use an enum, but cast issues struct sockaddr { @@ -141,6 +146,8 @@ extern int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen); */ extern int listen(int sockfd, int backlog); +#define SOMAXCONN 128 // Maximum size of backlog (actually far higher) + /** * \brief Accept an incoming connection */ @@ -164,5 +171,9 @@ extern void herror(const char *s); extern const char *hstrerror(int err); extern struct hostent *gethostent(void); +#ifdef __cplusplus +} +#endif + #endif