2019-02-14 12:50:43 +00:00
|
|
|
#pragma once
|
|
|
|
#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>
|
2019-02-19 09:32:33 +00:00
|
|
|
#include <arpa/inet.h>
|
2019-02-14 12:50:43 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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-02-14 12:50:43 +00:00
|
|
|
};
|
|
|
|
|
2019-03-15 15:44:47 +00:00
|
|
|
enum PKT_FLAGS {
|
2019-03-19 14:39:05 +00:00
|
|
|
PKT_CONTROL = 1 << 0
|
2019-03-15 15:44:47 +00:00
|
|
|
};
|
|
|
|
|
2019-02-14 12:50:43 +00:00
|
|
|
union abstract_packet {
|
|
|
|
char raw;
|
|
|
|
struct {
|
|
|
|
uint16_t size;
|
2019-02-15 17:09:51 +00:00
|
|
|
uint16_t port;
|
2019-03-19 19:48:07 +00:00
|
|
|
uint16_t id;
|
2019-03-13 16:53:46 +00:00
|
|
|
uint8_t bitfield;
|
|
|
|
uint8_t prevlink;
|
|
|
|
uint16_t deltat;
|
2019-03-15 15:44:47 +00:00
|
|
|
uint8_t flags;
|
2019-02-14 12:50:43 +00:00
|
|
|
char payload;
|
|
|
|
} str;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct internet_packet {
|
|
|
|
union abstract_packet ap;
|
|
|
|
char rest[1499]; // MTU = 1500, 1 byte in the union
|
|
|
|
};
|
|
|
|
|
|
|
|
struct buffer_packet {
|
|
|
|
uint8_t mode;
|
|
|
|
uint16_t aread;
|
|
|
|
uint16_t awrite;
|
|
|
|
struct internet_packet ip;
|
|
|
|
};
|
|
|
|
|
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-02-14 12:50:43 +00:00
|
|
|
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);
|
2019-02-19 13:49:44 +00:00
|
|
|
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);
|