first commit
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <endian.h>
|
||||
#include "defs.h"
|
||||
#include "icmp.h"
|
||||
|
||||
#define IP_PLUGGED_PROTOCOLS_MAX 4
|
||||
|
||||
typedef struct {
|
||||
#if __BYTE_ORDER == __LITTLE_ENDIAN
|
||||
uint8_t header_length: 4;
|
||||
uint8_t version: 4;
|
||||
#endif
|
||||
#if __BYTE_ORDER == __BIG_ENDIAN
|
||||
uint8_t version: 4;
|
||||
uint8_t header_length: 4;
|
||||
#endif
|
||||
uint8_t type_of_service;
|
||||
uint16_t total_length;
|
||||
uint16_t id;
|
||||
uint16_t fragment_offset;
|
||||
uint8_t time_to_live;
|
||||
uint8_t protocol;
|
||||
uint16_t checksum;
|
||||
uint32_t src_ip;
|
||||
uint32_t dst_ip;
|
||||
char payload[];
|
||||
} ip_packet_t;
|
||||
static_assert(sizeof(ip_packet_t) == 20);
|
||||
|
||||
typedef enum {
|
||||
IP_PROTOCOL_ICMP = 1,
|
||||
IP_PROTOCOL_TCP = 6,
|
||||
IP_PROTOCOL_UDP = 17,
|
||||
} ip_protocol_t;
|
||||
|
||||
typedef struct {
|
||||
uint8_t protocol;
|
||||
void *data;
|
||||
void (*process_packet)(void*, ip_address_t, const void*, size_t);
|
||||
} ip_plugged_protocol_t;
|
||||
|
||||
typedef struct {
|
||||
ip_address_t ip;
|
||||
|
||||
uint32_t next_id;
|
||||
|
||||
void *output_ptr;
|
||||
size_t output_max;
|
||||
|
||||
icmp_state_t icmp_state;
|
||||
|
||||
void *send_data;
|
||||
void (*send)(void*, ip_address_t, size_t);
|
||||
|
||||
size_t plugged_protocols_count;
|
||||
ip_plugged_protocol_t plugged_protocols[IP_PLUGGED_PROTOCOLS_MAX];
|
||||
} ip_state_t;
|
||||
|
||||
void ip_seconds_passed(ip_state_t *state, size_t seconds);
|
||||
void ip_change_output_buffer(ip_state_t *state, void *ptr, size_t max);
|
||||
void ip_init(ip_state_t *state, ip_address_t ip, void *send_data, void (*send)(void*, ip_address_t, size_t));
|
||||
void ip_free(ip_state_t *state);
|
||||
int ip_send(ip_state_t *state, ip_protocol_t protocol, ip_address_t dst, bool no_fragm, const void *src, size_t len);
|
||||
void ip_process_packet(ip_state_t *state, const void *packet, size_t len);
|
||||
bool ip_plug_protocol(ip_state_t *ip_state, uint8_t protocol, void *data, void (*process_packet)(void *data, ip_address_t sender, const void *packet, size_t len));
|
||||
Reference in New Issue
Block a user