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
--- /dev/null
+/*
+ * Acess2 POSIX Sockets Library
+ * - By John Hodge (thePowersGang)
+ *
+ * byteordering.c
+ * - hton/ntoh
+ */
+#include <arpa/inet.h>
+
+// === 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
+}
+
+
}
}
+struct hostent *gethostbyname(const char *name)
+{
+ return NULL;
+}
+
#include <netinet/in.h>
#include <stdint.h> // 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);
extern in_addr_t inet_network(const char *cp);
extern char *inet_ntoa(struct in_addr in);
+#ifdef __cplusplus
+}
+#endif
+
#endif
#include <sys/socket.h>
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+
struct hostent
{
char *h_name;
int h_length;
char **h_addr_list;
};
+#define h_addr h_addr_list[0] // backwards compataibility
struct netent
{
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
uint32_t sin6_scope_id;
};
+#include <arpa/inet.h> // for hton*/ntoh* (bochs)
+
#endif
#endif
#include <stdint.h> // uint32_t
+#ifdef __cplusplus
+extern "C" {
+#endif
+
typedef uint32_t socklen_t;
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
{
*/
extern int listen(int sockfd, int backlog);
+#define SOMAXCONN 128 // Maximum size of backlog (actually far higher)
+
/**
* \brief Accept an incoming connection
*/
extern const char *hstrerror(int err);
extern struct hostent *gethostent(void);
+#ifdef __cplusplus
+}
+#endif
+
#endif