You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

81 lines
2.8 KiB

// tun_if.h - Cross-platform TUN interface management for utun
#ifndef TUN_IF_H
#define TUN_IF_H
#include <stdint.h>
#include <stddef.h>
#include <sys/types.h>
// Forward declarations
struct UASYNC;
struct utun_config;
struct ll_queue;
struct memory_pool;
#ifdef __cplusplus
extern "C" {
#endif
#define TUN_MTU_DEFAULT 1500
#define TUN_MAX_PACKET_SIZE 1500
// TUN interface handle - opaque structure
struct tun_if {
char ifname[16]; // Interface name (e.g., "tun12" or "utun")
int fd; // Linux: file descriptor, Windows: -1 (unused)
void* platform_handle; // Windows: WINTUN_SESSION_HANDLE
void* adapter_handle; // Windows: WINTUN_ADAPTER_HANDLE
struct UASYNC* ua; // uasync instance
void* socket_id; // Socket ID from uasync_add_socket
struct memory_pool* pool; // Pool for ETCP_FRAGMENT structures
struct ll_queue* output_queue; // Queue for incoming packets from TUN (to routing)
struct ll_queue* input_queue; // Queue for outgoing packets to TUN (from routing)
// Statistics
uint64_t bytes_read; // Bytes read from TUN
uint64_t bytes_written; // Bytes written to TUN
uint32_t packets_read; // Packets read from TUN
uint32_t packets_written; // Packets written to TUN
uint32_t read_errors; // Read errors
uint32_t write_errors; // Write errors
};
/**
* @brief Initialize TUN interface
* Creates TUN device, sets MTU=1500, brings interface up,
* configures IP from config, registers read callback in uasync,
* creates memory pool and input queue for incoming packets.
* @param ua uasync instance for event handling
* @param config Configuration containing tun_ip and other settings
* @return Pointer to tun_if on success, NULL on error
*/
struct tun_if* tun_init(struct UASYNC* ua, struct utun_config* config);
/**
* @brief Close TUN interface and free resources
* Unregisters socket from uasync, closes fd, drains and frees
* all packets from output_queue, destroys pool and queue.
* @param tun TUN interface handle
*/
void tun_close(struct tun_if* tun);
/**
* @brief Write packet to TUN interface
* @param tun TUN interface handle
* @param buf Packet data
* @param len Packet size
* @return Number of bytes written, -1 on error
*/
ssize_t tun_write(struct tun_if* tun, const uint8_t* buf, size_t len);
// Platform-specific internal functions (implemented in tun_linux.c / tun_windows.c)
int tun_platform_init(struct tun_if* tun, const char* ifname, const char* ip_str, int mtu);
void tun_platform_cleanup(struct tun_if* tun);
ssize_t tun_platform_read(struct tun_if* tun, uint8_t* buf, size_t len);
ssize_t tun_platform_write(struct tun_if* tun, const uint8_t* buf, size_t len);
int tun_platform_get_poll_fd(struct tun_if* tun);
#ifdef __cplusplus
}
#endif
#endif /* TUN_IF_H */