#ifndef UDP_SOCKET_H
#define UDP_SOCKET_H
#include <stdint.h>
#include <stdbool.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include "vma_common.h"
typedef struct {
int socket_fd; vma_options_t vma_options; struct sockaddr_in local_addr; struct sockaddr_in remote_addr; bool is_bound; bool is_connected; uint64_t rx_packets; uint64_t tx_packets; uint64_t rx_bytes; uint64_t tx_bytes; } udp_socket_t;
typedef struct {
void* data; size_t length; struct sockaddr_in src_addr; uint64_t timestamp; } udp_packet_t;
typedef enum {
UDP_SUCCESS = 0,
UDP_ERROR_SOCKET_CREATE = -1,
UDP_ERROR_SOCKET_OPTION = -2,
UDP_ERROR_BIND = -3,
UDP_ERROR_CONNECT = -4,
UDP_ERROR_SEND = -5,
UDP_ERROR_RECV = -6,
UDP_ERROR_TIMEOUT = -7,
UDP_ERROR_INVALID_PARAM = -8,
UDP_ERROR_NOT_INITIALIZED = -9,
UDP_ERROR_CLOSED = -10
} udp_result_t;
udp_result_t udp_socket_init(udp_socket_t* socket, const vma_options_t* options);
udp_result_t udp_socket_close(udp_socket_t* socket);
udp_result_t udp_socket_bind(udp_socket_t* socket, const char* ip, uint16_t port);
udp_result_t udp_socket_connect(udp_socket_t* socket, const char* ip, uint16_t port);
udp_result_t udp_socket_send(udp_socket_t* socket, const void* data, size_t length, size_t* bytes_sent);
udp_result_t udp_socket_sendto(udp_socket_t* socket, const void* data, size_t length,
const char* ip, uint16_t port, size_t* bytes_sent);
udp_result_t udp_socket_recv(udp_socket_t* socket, void* buffer, size_t buffer_size,
int timeout_ms, size_t* bytes_received);
udp_result_t udp_socket_recvfrom(udp_socket_t* socket, udp_packet_t* packet,
void* buffer, size_t buffer_size, int timeout_ms);
udp_result_t udp_socket_setopt(udp_socket_t* socket, int level, int optname,
const void* optval, socklen_t optlen);
udp_result_t udp_socket_get_stats(udp_socket_t* socket, uint64_t* rx_packets,
uint64_t* tx_packets, uint64_t* rx_bytes,
uint64_t* tx_bytes);
#endif