Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Arpa INET Header and Redundant File Removal #25

Merged
merged 11 commits into from
Jan 31, 2025
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.vscode
build/
13 changes: 13 additions & 0 deletions configure
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ SOURCES='
\$(srcdir)/source/getpeername.c
\$(srcdir)/source/getsockname.c
\$(srcdir)/source/getsockopt.c
\$(srcdir)/source/inet_addr.c
\$(srcdir)/source/inet_ntoa.c
\$(srcdir)/source/inet_ntop.c
\$(srcdir)/source/inet_pton.c
\$(srcdir)/source/htonl.c
\$(srcdir)/source/htons.c
\$(srcdir)/source/ntohl.c
\$(srcdir)/source/ntohs.c
\$(srcdir)/source/listen.c
\$(srcdir)/source/recv.c
\$(srcdir)/source/recvfrom.c
Expand All @@ -65,10 +73,15 @@ SOURCES='
INCLUDE_DIRECTORIES='
include
include/sys
include/netinet
include/arpa
'
INCLUDE_FILES='
include/sys/mman.h
include/netinet/in.h
include/arpa/inet.h
include/sys/uio.h
include/sys/socket.h
include/sys/wait.h
include/dlfcn.h
include/spawn.h
Expand Down
21 changes: 21 additions & 0 deletions function_pointer_types.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Function Pointer Type Corrections

Based on the senior's feedback, here are the proper function pointer type definitions per the Windows API docs while maintaining POSIX compatibility:

```c
// Network byte order conversions
uint32_t (*)(uint32_t) for htonl/ntohl
uint16_t (*)(uint16_t) for htons/ntohs

// IP address conversions
unsigned long (*)(const char*) for inet_addr
char* (*)(struct in_addr) for inet_ntoa
const char* (*)(int, const void*, char*, size_t) for inet_ntop (InetNtopA)
int (*)(int, const char*, void*) for inet_pton (InetPtonA)
```

The key points:
1. We maintain POSIX types in our public interface
2. Function pointer types match Windows API signatures
3. No Windows-specific types or headers are used
4. For inet_ntop/pton we use InetNtopA/InetPtonA from ws2tcpip.h
2 changes: 0 additions & 2 deletions include/.gitignore

This file was deleted.

42 changes: 42 additions & 0 deletions include/arpa/inet.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#ifndef _ARPA_INET_H
#define _ARPA_INET_H

#include <inttypes.h>
#include <netinet/in.h>
#include <sys/socket.h>

#ifdef __cplusplus
extern "C" {
#endif

/* Types required by POSIX */
typedef uint16_t in_port_t;
typedef uint32_t in_addr_t;

/* Address-to-string buffer sizes */
#define INET_ADDRSTRLEN 16
#ifdef _IPV6_
#define INET6_ADDRSTRLEN 46
#endif

/* Convert values between host and network byte order */
uint32_t htonl(uint32_t);
uint16_t htons(uint16_t);
uint32_t ntohl(uint32_t);
uint16_t ntohs(uint16_t);

/* Obsolescent functions */
#if !defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) || defined(_XOPEN_SOURCE)
in_addr_t inet_addr(const char *);
char *inet_ntoa(struct in_addr);
#endif

/* Current standard functions */
const char *inet_ntop(int, const void *restrict, char *restrict, socklen_t);
int inet_pton(int, const char *restrict, void *restrict);

#ifdef __cplusplus
}
#endif

#endif /* _ARPA_INET_H */
55 changes: 55 additions & 0 deletions include/netinet/in.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#ifndef _NETINET_IN_H
#define _NETINET_IN_H

#include <stdint.h>
#include <sys/socket.h>

#ifdef __cplusplus
extern "C" {
#endif

/* Standard Internet Protocol (IP) address types */
typedef uint16_t in_port_t; // Port number type
typedef uint32_t in_addr_t; // IPv4 address type

/* Structure for an IPv4 address */
struct in_addr {
in_addr_t s_addr; // IPv4 address in network byte order
};

/* Socket address structure for IPv4 */
struct sockaddr_in {
sa_family_t sin_family; // Address family (AF_INET for IPv4)
in_port_t sin_port; // Port number in network byte order
struct in_addr sin_addr; // IPv4 address
unsigned char sin_zero[8]; // Padding to match struct sockaddr size
};

/* Constants for address families */
#define AF_INET 2 // IPv4 address family

/* Constants for socket types */
#define IPPROTO_TCP 6 // TCP protocol
#define IPPROTO_UDP 17 // UDP protocol

/* Macros for address manipulation */
#define INADDR_ANY ((in_addr_t)0x00000000) // Bind to any address
#define INADDR_LOOPBACK ((in_addr_t)0x7F000001) // Loopback address (127.0.0.1)
#define INADDR_BROADCAST ((in_addr_t)0xFFFFFFFF) // Broadcast address
#define INADDR_NONE ((in_addr_t)0xFFFFFFFF) // Invalid address

/* Byte-order conversion functions */
uint16_t htons(uint16_t hostshort); // Host to network short
uint16_t ntohs(uint16_t netshort); // Network to host short
uint32_t htonl(uint32_t hostlong); // Host to network long
uint32_t ntohl(uint32_t netlong); // Network to host long

/* Address conversion functions */
in_addr_t inet_addr(const char *cp); // Convert string to IPv4 address
char *inet_ntoa(struct in_addr in); // Convert IPv4 address to string

#ifdef __cplusplus
}
#endif

#endif /* _NETINET_IN_H */
1 change: 1 addition & 0 deletions source/accept.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <sys/socket.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <dlfcn.h>

Expand Down
1 change: 1 addition & 0 deletions source/bind.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <sys/socket.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <dlfcn.h>
#include <stdio.h>
Expand Down
1 change: 1 addition & 0 deletions source/connect.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <sys/socket.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <dlfcn.h>
#include <stdio.h>
Expand Down
16 changes: 16 additions & 0 deletions source/htonl.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <arpa/inet.h>
#include <stdint.h>
#include <fcntl.h>
#include <dlfcn.h>
#include "ws_types.h"

typedef void* HANDLE;
typedef HANDLE SOCKET;

uint32_t htonl(uint32_t hostlong) {
void *Ws2_32 = dlopen("ws2_32.dll", RTLD_LAZY);
fn_htonl_t _htonl = (fn_htonl_t)dlsym(Ws2_32, "htonl");
uint32_t result = _htonl(hostlong);
dlclose(Ws2_32);
return result;
}
16 changes: 16 additions & 0 deletions source/htons.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <arpa/inet.h>
#include <stdint.h>
#include <fcntl.h>
#include <dlfcn.h>
#include "ws_types.h"

typedef void* HANDLE;
typedef HANDLE SOCKET;

uint16_t htons(uint16_t hostshort) {
void *Ws2_32 = dlopen("ws2_32.dll", RTLD_LAZY);
fn_htons_t _htons = (fn_htons_t)dlsym(Ws2_32, "htons");
uint16_t result = _htons(hostshort);
dlclose(Ws2_32);
return result;
}
27 changes: 27 additions & 0 deletions source/inet_addr.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <arpa/inet.h>
#include <dlfcn.h>

// Define the function pointer type for inet_addr
typedef unsigned long (*inet_addr_t)(const char *cp);

in_addr_t inet_addr(const char *cp) {
// Load the ws2_32.dll library
void *Ws2_32 = dlopen("ws2_32.dll", RTLD_LAZY);
if (!Ws2_32) {
return INADDR_NONE; // Return error if DLL cannot be loaded
}

// Get the inet_addr function from the DLL
inet_addr_t _inet_addr = (inet_addr_t)dlsym(Ws2_32, "inet_addr");
if (!_inet_addr) {
dlclose(Ws2_32); // Close the DLL if the function is not found
return INADDR_NONE;
}

// Call the Windows inet_addr function
unsigned long result = _inet_addr(cp);

// Clean up
dlclose(Ws2_32);
return (in_addr_t)result;
}
27 changes: 27 additions & 0 deletions source/inet_ntoa.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <arpa/inet.h>
#include <dlfcn.h>

// Define the function pointer type for inet_ntoa
typedef char *(*inet_ntoa_t)(struct in_addr in);

char *inet_ntoa(struct in_addr in) {
// Load the ws2_32.dll library
void *Ws2_32 = dlopen("ws2_32.dll", RTLD_LAZY);
if (!Ws2_32) {
return NULL; // Return NULL if DLL cannot be loaded
}

// Get the inet_ntoa function from the DLL
inet_ntoa_t _inet_ntoa = (inet_ntoa_t)dlsym(Ws2_32, "inet_ntoa");
if (!_inet_ntoa) {
dlclose(Ws2_32); // Close the DLL if the function is not found
return NULL;
}

// Call the Windows inet_ntoa function
char *result = _inet_ntoa(in);

// Clean up
dlclose(Ws2_32);
return result;
}
27 changes: 27 additions & 0 deletions source/inet_ntop.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <arpa/inet.h>
#include <dlfcn.h>

// Define the function pointer type for inet_ntop
typedef const char *(*inet_ntop_t)(int af, const void *src, char *dst, socklen_t size);

const char *inet_ntop(int af, const void *src, char *dst, socklen_t size) {
// Load the ws2_32.dll library
void *Ws2_32 = dlopen("ws2_32.dll", RTLD_LAZY);
if (!Ws2_32) {
return NULL; // Return NULL if DLL cannot be loaded
}

// Get the inet_ntop function from the DLL
inet_ntop_t _inet_ntop = (inet_ntop_t)dlsym(Ws2_32, "inet_ntop");
if (!_inet_ntop) {
dlclose(Ws2_32); // Close the DLL if the function is not found
return NULL;
}

// Call the Windows inet_ntop function
const char *result = _inet_ntop(af, src, dst, size);

// Clean up
dlclose(Ws2_32);
return result;
}
27 changes: 27 additions & 0 deletions source/inet_pton.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <arpa/inet.h>
#include <dlfcn.h>

// Define the function pointer type for inet_pton
typedef int (*inet_pton_t)(int af, const char *src, void *dst);

int inet_pton(int af, const char *src, void *dst) {
// Load the ws2_32.dll library
void *Ws2_32 = dlopen("ws2_32.dll", RTLD_LAZY);
if (!Ws2_32) {
return -1; // Return error if DLL cannot be loaded
}

// Get the inet_pton function from the DLL
inet_pton_t _inet_pton = (inet_pton_t)dlsym(Ws2_32, "inet_pton");
if (!_inet_pton) {
dlclose(Ws2_32); // Close the DLL if the function is not found
return -1;
}

// Call the Windows inet_pton function
int result = _inet_pton(af, src, dst);

// Clean up
dlclose(Ws2_32);
return result;
}
16 changes: 16 additions & 0 deletions source/ntohl.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <arpa/inet.h>
#include <stdint.h>
#include <fcntl.h>
#include <dlfcn.h>
#include "ws_types.h"

typedef void* HANDLE;
typedef HANDLE SOCKET;

uint32_t ntohl(uint32_t netlong) {
void *Ws2_32 = dlopen("ws2_32.dll", RTLD_LAZY);
fn_ntohl_t _ntohl = (fn_ntohl_t)dlsym(Ws2_32, "ntohl");
uint32_t result = _ntohl(netlong);
dlclose(Ws2_32);
return result;
}
16 changes: 16 additions & 0 deletions source/ntohs.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <arpa/inet.h>
#include <stdint.h>
#include <fcntl.h>
#include <dlfcn.h>
#include "ws_types.h"

typedef void* HANDLE;
typedef HANDLE SOCKET;

uint16_t ntohs(uint16_t netshort) {
void *Ws2_32 = dlopen("ws2_32.dll", RTLD_LAZY);
fn_ntohs_t _ntohs = (fn_ntohs_t)dlsym(Ws2_32, "ntohs");
uint16_t result = _ntohs(netshort);
dlclose(Ws2_32);
return result;
}
1 change: 1 addition & 0 deletions source/recv.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <sys/socket.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <dlfcn.h>

Expand Down
1 change: 1 addition & 0 deletions source/send.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <sys/socket.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <dlfcn.h>

Expand Down
2 changes: 2 additions & 0 deletions source/socket.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include <sys/socket.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <dlfcn.h>
#include <stdio.h>

typedef unsigned short WORD;
typedef void* HANDLE;
Expand Down
Loading