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.
 
 
 
 
 
 

101 lines
3.5 KiB

#ifndef TEST_UDP_SOCKET_H
#define TEST_UDP_SOCKET_H
#include <sys/socket.h>
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#define TEST_UDP_MAX_PACKET_SIZE 65536
#define TEST_UDP_MAX_QUEUE_SIZE 1000
// Test UDP packet structure
struct test_udp_packet {
uint8_t* data;
size_t len;
struct sockaddr_storage addr;
socklen_t addr_len;
struct test_udp_packet* next;
};
// Virtual UDP socket for testing
struct test_udp_socket {
int fd; // Virtual file descriptor
int family; // Address family (AF_INET or AF_INET6)
struct sockaddr_storage local_addr;
bool bound;
bool nonblocking;
// Receive queue (packets waiting to be read)
struct test_udp_packet* recv_queue_head;
struct test_udp_packet* recv_queue_tail;
size_t recv_queue_size;
// Statistics
struct {
size_t packets_sent;
size_t packets_received;
size_t bytes_sent;
size_t bytes_received;
size_t send_errors;
size_t recv_errors;
} stats;
// Callback for outgoing packets
void (*packet_sent)(const uint8_t* data, size_t len,
const struct sockaddr* dest_addr, socklen_t addr_len,
void* context);
void* context;
};
// Create virtual UDP socket
struct test_udp_socket* test_udp_socket_create(int family);
// Destroy virtual UDP socket
void test_udp_socket_destroy(struct test_udp_socket* sock);
// Bind virtual socket
int test_udp_socket_bind(struct test_udp_socket* sock, const struct sockaddr* addr, socklen_t len);
// Set socket options (limited implementation for testing)
int test_udp_socket_setsockopt(struct test_udp_socket* sock, int level, int optname,
const void *optval, socklen_t optlen);
// Get socket options
int test_udp_socket_getsockopt(struct test_udp_socket* sock, int level, int optname,
void *optval, socklen_t *optlen);
// Set non-blocking mode
int test_udp_socket_set_nonblocking(struct test_udp_socket* sock, bool nonblocking);
// Send packet using virtual socket
ssize_t test_udp_socket_sendto(struct test_udp_socket* sock, const void *buf, size_t len, int flags,
const struct sockaddr *dest_addr, socklen_t addr_len);
// Receive packet from virtual socket
ssize_t test_udp_socket_recvfrom(struct test_udp_socket* sock, void *buf, size_t len, int flags,
struct sockaddr *src_addr, socklen_t *addr_len);
// Inject packet into receive queue (simulates incoming packet)
int test_udp_socket_inject(struct test_udp_socket* sock,
const uint8_t* data, size_t len,
const struct sockaddr* src_addr, socklen_t addr_len);
// Get virtual socket file descriptor
int test_udp_socket_get_fd(struct test_udp_socket* sock);
// Get socket statistics
void test_udp_socket_get_stats(struct test_udp_socket* sock, size_t* packets_sent,
size_t* packets_received, size_t* bytes_sent,
size_t* bytes_received, size_t* send_errors, size_t* recv_errors);
// Reset socket statistics
void test_udp_socket_reset_stats(struct test_udp_socket* sock);
// Global virtual socket registry for fd-based operations
struct test_udp_socket* test_udp_socket_find_by_fd(int fd);
void test_udp_socket_register(struct test_udp_socket* sock);
void test_udp_socket_unregister(struct test_udp_socket* sock);
#endif // TEST_UDP_SOCKET_H