📄 pmtp.h
字号:
#include <netinet/in.h>#include <sys/time.h>/* * Used in the (rather primitive) traffic shaper to smooth out traffic bursts */#define SMOOTHING_SECONDS 5/* * This must be set to at least 255 becode each encoding run results * in 255 packets */#define MAX_PACKET_BUFFER_SIZE 300/* * The only parameter to tweak here is NROOTS. NROOTS specifies how * many packets can be lost out of the NN=255 packets for the decoder * to still be able to recover the original data. Note that this also * affects the overhead which is (NROOTS/NN)%. * DON'T FIDDLE with the other paramters as this will kill the codec. */#define NN 255#define SYMSIZE 8#define GENPOLY 0x187#define FCS 1#define PRIM 1#define NROOTS 8#define IP_HEADER_SIZE 20#define UDP_HEADER_SIZE 8#define PMTP_HEADER_SIZE 8/* * PMTP_BITRATE controls the sending rate of sendfile. This must be * set to a multiple of 8000. */#define PMTP_BITRATE 64000/* * PMTP_PAYLOAD should be as big as possible but because UDP is used * care must be taken that the path mtu supports the payload size. So * watch out for those GRE/IPSEC tunnels! */#define PMTP_PAYLOAD 1400/* * This is the maximum packets of a new segment that we receive before * we start processing the current (incomplete) segment */#define MAX_PACKETS_LOST NROOTS/* * With PMTP_SIMULATE_ERRORS you can simulate packet-loss. Set to 0 to disable. */#define PMTP_SIMULATE_ERRORS 4/* * The group and port to which a session is multicasted. Note that * we don't use the SSM range (232.0.0.0/8) yet because we don't support * IGMPv3 yet */#define PMTP_GROUP "233.5.5.5"#define PMTP_PORT 12001/* * When computing tigertrees, how big are the chunks of a file that we * process at a time */#define TIGERTREE_CHUNKSIZE 256000/* * These are the different debugging levels. `OR' them together to set * the DEBUG level */#define DEBUG 4163#define DEBUG_ERASURES 1#define DEBUG_PACKETS_MISSED 2#define DEBUG_PACKETS_RECEIVED 4#define DEBUG_CODING 8#define DEBUG_BLOCKS 16#define DEBUG_CODING_DUMP 32#define DEBUG_SEGMENTS 64#define DEBUG_PACKETS_SENT 128#define DEBUG_BUFFER_ADD 256#define DEBUG_BUFFER_TRANSMIT 512#define DEBUG_BUFFER_REMOVE 1024#define DEBUG_UPLOAD_PROCESSING 2048#define DEBUG_PMSP 4096#define DEBUG_PACKETS_SHA 8192#define DEBUG_SEGMENTS_SHA 16384/* * This defines the header off-sets for the PMTP and PMSP packets. Don't * mess with them. */#define PMTP_HEADER_PACKETID 0#define PMTP_HEADER_SESSIONID 2 #define PMTP_HEADER_SEGMENTID 4#define PMSP_HEADER_VERSION 0#define PMSP_HEADER_CODINGID 0#define PMSP_HEADER_SESSIONID 2#define PMSP_HEADER_PAYLOAD 4#define PMSP_HEADER_START 6#define PMSP_HEADER_SOURCE 8#define PMSP_HEADER_GROUP 12#define PMSP_HEADER_PORT 16#define PMSP_HEADER_BITRATE 18#define PMSP_HEADER_FILESIZE 20#define PMSP_HEADER_FILETIGERTREE 28#define PMSP_HEADER_PARTOFFSET 52#define PMSP_HEADER_PARTLENGTH 56#define PMSP_HEADER_PARTTIGERTREE 60#define PMSP_HEADER_FILENAME 84/* * State mechanism for sessions. Immature & incomplete */#define PMSP_STATE_INIT 1 /* No session info received yet */#define PMSP_STATE_INVALID 2 /* Invalid session info */#define PMSP_STATE_CONFLICT 4 /* Conflicting info for session id */#define PMSP_STATE_LEARNED 8 /* Received session information */#define PMSP_STATE_ACTIVE 16 /* Download of session has started */#define PMSP_STATE_COMPLETED 32 /* Download has been completed */#define PMSP_STATE_FAILED 64 /* Download has failed *//* * Used by traffic shaper */struct packet { unsigned char *packet; uint16_t length;};/* * Used by traffic shaper */struct packet_buffer { struct packet **packets; uint16_t size;};/* * This defines a session. Perhaps it should be renamed. Matches mostly * with the PMSP header, except for the socket family and protocol. */struct pmsp_header { uint8_t status; uint8_t version : 4; uint16_t codingid : 12; uint16_t sessionid; uint16_t payload; uint16_t start; sa_family_t family; uint32_t source; uint32_t group; uint16_t protocol; uint16_t port; uint32_t bitrate; uint64_t filesize; unsigned char file_tigertree[24], part_tigertree[24]; uint32_t partoffset, partlength; unsigned char filename[128];};/* * Each upload is represented with this information. Used a.o. by the * traffic shaper */struct pmtp_upload { struct timeval previous_invocation; uint32_t bytes_sent; uint32_t bytes_sent_interval; uint32_t credits; uint32_t packets_sent; int s; struct sockaddr_in s_in; int fd; struct packet_buffer buffer; struct pmsp_header session;};/* * The receiver receives traffic from multiple segments while downloading * because packets can be lost or re-ordered. Each segment is tracked * in the pmtp_segment structure */struct pmtp_segment { uint32_t segmentid; uint32_t packets_received; /* duplicates are not counted here */ uint32_t duplicate_packets; uint8_t received_packets[NN]; uint16_t payload_size; unsigned char *packets; void *codec;};/* * Header Encoding / decoding functions */uint8_t decode_header_8 (unsigned char* header);uint16_t decode_header_16 (unsigned char* header);uint32_t decode_header_32 (unsigned char* header);uint8_t encode_header_8 (unsigned char* header, uint8_t value);uint16_t encode_header_16 (unsigned char* header, uint16_t value);uint32_t encode_header_32 (unsigned char* header, uint32_t value);/* * Tiger Tree functions */uint64_t get_tigertree (unsigned char *tt, unsigned char *filename, uint64_t offset, uint64_t length);/* * Reed-Solomon Encoding / decoding functions for PMTP segments */uint32_t init_segment (struct pmtp_segment *segment, void *codec);uint32_t free_segment (struct pmtp_segment *segment);uint8_t * encode_segment (struct pmtp_segment *segment, uint8_t nroots);uint32_t decode_segment (struct pmtp_segment *segment, int fd, uint16_t nroots, uint32_t bytes_left);int32_t buffer_segment(struct pmtp_upload *upload,struct pmtp_segment *segment);uint16_t init_packet_buffer (struct packet_buffer *buffer);uint16_t free_packet_buffer (struct packet_buffer *buffer);int16_t buffer_packet (struct packet_buffer *buffer, unsigned char *packet, uint16_t length);uint16_t send_buffer (int s, struct sockaddr_in *s_in, struct packet_buffer *buffer, uint32_t budget, uint32_t *packets, uint32_t *bytes_sent);uint16_t remove_packet (struct packet_buffer *buffer, uint16_t position);void send_packet (int s, struct sockaddr_in *s_in, unsigned char *packet, uint16_t length);uint16_t buffer_packets_free (struct packet_buffer *buffer);/* * Misc functions */off_t get_filesize (const unsigned char *filename);/* * Debugging related functions */unsigned char * get_printable_hex (unsigned char *dest, unsigned char *string, uint16_t length);unsigned char * get_printable_sha (unsigned char *dest, unsigned char *string, uint16_t length);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -