// tun_if.h - TUN interface management for utun #ifndef TUN_IF_H #define TUN_IF_H #include #include #include #ifdef __cplusplus extern "C" { #endif // TUN interface configuration typedef struct { char ifname[16]; // Interface name (e.g., "tun12") char ip_addr[64]; // IP address with mask (e.g., "10.0.0.1/24") int mtu; // MTU size int fd; // File descriptor uint8_t is_up; // 1 if interface is up // 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 } tun_config_t; /** * @brief Create and configure TUN interface * @param config TUN configuration (ifname can be empty for auto) * @return 0 on success, -1 on error */ int tun_create(tun_config_t *config); /** * @brief Configure IP address on TUN interface * @param ifname Interface name * @param ip_addr IP address with mask (e.g., "10.0.0.1/24") * @return 0 on success, -1 on error */ int tun_set_ip(const char *ifname, const char *ip_addr); /** * @brief Bring TUN interface up * @param ifname Interface name * @return 0 on success, -1 on error */ int tun_set_up(const char *ifname); /** * @brief Set MTU on TUN interface * @param ifname Interface name * @param mtu MTU value * @return 0 on success, -1 on error */ int tun_set_mtu(const char *ifname, int mtu); /** * @brief Read packet from TUN interface * @param fd TUN file descriptor * @param buffer Buffer to store packet * @param size Buffer size * @return Number of bytes read, -1 on error */ ssize_t tun_read(int fd, uint8_t *buffer, size_t size); /** * @brief Write packet to TUN interface * @param fd TUN file descriptor * @param buffer Packet data * @param size Packet size * @return Number of bytes written, -1 on error */ ssize_t tun_write(int fd, const uint8_t *buffer, size_t size); /** * @brief Close TUN interface * @param config TUN configuration */ void tun_close(tun_config_t *config); /** * @brief Get current TUN configuration * @param ifname Interface name * @param config Output configuration * @return 0 on success, -1 on error */ int tun_get_config(const char *ifname, tun_config_t *config); #ifdef __cplusplus } #endif #endif /* TUN_IF_H */