tor_multipath_voip/src/packet.h
2019-05-24 14:48:16 +02:00

92 lines
1.9 KiB
C

#pragma once
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdint.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>
#include <netinet/in.h>
#include <arpa/inet.h>
/*
* man 7 udp about receive operation on UDP sockets:
*
* > All receive operations return only one packet. When the packet is smaller than the passed
* > buffer, only that much data is returned; when it is bigger, the packet is truncated and the
* > MSG_TRUNC flag is set. MSG_WAITALL is not supported.
*/
enum FD_STATE {
FDS_READY,
FDS_AGAIN,
FDS_ERR
};
enum BP_MODE {
BP_READING,
BP_WRITING
};
enum PKT_CMD {
CMD_HEALTH,
CMD_CLEAR,
CMD_XOR
};
union abstract_packet {
char raw;
struct {
struct {
uint16_t size;
enum PKT_CMD cmd;
} headers;
union {
struct {
uint16_t id;
uint8_t bitfield;
uint8_t prevlink;
uint16_t deltat;
uint16_t min_blocked_pkt;
} health;
struct {
uint16_t id;
uint16_t port;
char payload;
} clear;
} content;
} fmt;
};
struct internet_packet {
union abstract_packet ap;
char rest[1499]; // MTU = 1500, 1 byte in the union as payload
};
struct buffer_packet {
enum BP_MODE mode;
uint8_t ap_count;
uint16_t aread;
uint16_t awrite;
struct internet_packet ip;
};
struct udp_target {
struct sockaddr_in addr;
socklen_t addrlen;
int set;
int ref_count;
};
size_t get_full_size(struct buffer_packet* bp);
enum FD_STATE read_packet_from_tcp(int fd, struct buffer_packet* bp);
enum FD_STATE write_packet_to_tcp(int fd, struct buffer_packet* bp);
enum FD_STATE write_packet_to_udp(int fd, struct buffer_packet* bp, struct udp_target* udp_t);
enum FD_STATE read_packet_from_udp (int fd, struct buffer_packet* bp, struct udp_target* udp_t);
void dump_buffer_packet(struct buffer_packet* bp);
void dump_abstract_packet(union abstract_packet* ap);