tor_multipath_voip/src/packet.h

115 lines
2.7 KiB
C
Raw Permalink Normal View History

#pragma once
2019-05-13 08:02:43 +00:00
#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>
2019-08-09 15:01:28 +00:00
#include "evt_core.h"
#include "url.h"
2020-02-27 15:41:52 +00:00
#define UDP_MTU 65535
/*
* 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,
2019-02-18 20:55:53 +00:00
BP_WRITING
};
2019-05-13 08:02:43 +00:00
enum PKT_CMD {
2019-08-27 12:31:27 +00:00
CMD_UDP_ENCAPSULATED = 1,
CMD_LINK_MONITORING_THUNDER = 2,
CMD_UDP_METADATA_THUNDER = 3,
2019-10-07 14:07:35 +00:00
CMD_LINK_MONITORING_LIGHTNING = 4,
2019-03-15 15:44:47 +00:00
};
2019-08-12 13:28:22 +00:00
enum PKT_FLAGS {
FLAG_READ_NEXT = 1 << 0,
2019-08-29 15:53:49 +00:00
FLAG_RESET = 1 << 1,
2019-08-12 13:28:22 +00:00
};
2019-08-26 10:08:31 +00:00
struct link_info {
2019-08-29 16:11:50 +00:00
uint16_t delta_t;
2019-08-26 10:08:31 +00:00
};
2020-02-05 21:52:50 +00:00
#pragma pack(1)
2019-05-14 09:23:23 +00:00
union abstract_packet {
char raw;
struct {
2019-05-13 08:02:43 +00:00
struct {
2019-05-14 09:23:23 +00:00
uint16_t size;
2019-08-12 13:28:22 +00:00
uint8_t cmd;
uint8_t flags;
2019-05-14 09:23:23 +00:00
} headers;
union {
2019-10-07 14:07:35 +00:00
struct {
uint64_t id;
2019-10-11 09:01:09 +00:00
uint8_t dyn_struct;
2019-10-07 14:07:35 +00:00
} link_monitoring_lightning;
2019-05-14 09:23:23 +00:00
struct {
2019-09-16 22:03:48 +00:00
uint8_t to_increment;
2019-08-26 15:35:23 +00:00
struct link_info links_status;
2019-08-09 15:01:28 +00:00
} link_monitoring_thunder;
2019-05-14 09:23:23 +00:00
struct {
uint16_t id;
2019-08-09 15:01:28 +00:00
} udp_metadata_thunder;
struct {
2019-05-14 09:23:23 +00:00
uint16_t port;
char payload;
2019-08-09 15:01:28 +00:00
} udp_encapsulated;
2019-05-14 09:23:23 +00:00
} content;
} fmt;
};
2021-01-26 14:19:09 +00:00
#pragma pack()
struct buffer_packet {
2019-05-09 17:53:47 +00:00
enum BP_MODE mode;
uint8_t ap_count;
uint16_t aread;
uint16_t awrite;
2019-05-28 09:45:20 +00:00
struct timespec seen;
2020-02-27 15:41:52 +00:00
char ip[UDP_MTU];
};
2019-02-14 16:15:13 +00:00
struct udp_target {
struct sockaddr_in addr;
socklen_t addrlen;
int set;
2019-02-19 14:17:47 +00:00
int ref_count;
2019-02-14 16:15:13 +00:00
};
2019-05-24 12:48:16 +00:00
size_t get_full_size(struct buffer_packet* bp);
2019-08-26 15:35:23 +00:00
union abstract_packet* buffer_append_ap(struct buffer_packet* bp, union abstract_packet* ap);
2019-08-27 12:31:27 +00:00
union abstract_packet* buffer_free_ap(struct buffer_packet* bp);
union abstract_packet* buffer_first_ap(struct buffer_packet* bp);
union abstract_packet* buffer_last_ap(struct buffer_packet* bp);
size_t buffer_full_size(struct buffer_packet* bp);
2019-08-12 13:28:22 +00:00
union abstract_packet* ap_next(union abstract_packet* ap);
2019-08-09 15:01:28 +00:00
enum FD_STATE read_packet_from_tcp(struct evt_core_fdinfo* fd, struct buffer_packet* bp);
enum FD_STATE write_packet_to_tcp(struct evt_core_fdinfo* fd, struct buffer_packet* bp);
enum FD_STATE write_packet_to_udp(struct evt_core_fdinfo* fd, struct buffer_packet* bp, struct udp_target* udp_t);
enum FD_STATE read_packet_from_udp (struct evt_core_fdinfo* fd, struct buffer_packet* bp, struct udp_target* udp_t);
2019-05-24 09:46:23 +00:00
void dump_buffer_packet(struct buffer_packet* bp);
void dump_abstract_packet(union abstract_packet* ap);