-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathftprot.h
More file actions
90 lines (68 loc) · 2.43 KB
/
ftprot.h
File metadata and controls
90 lines (68 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
* File: ftprot.h
* Date: 14.10.2025
* Author: Marek Pazúr
* Login: xpazurm00
*/
#pragma once
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
#include <sys/socket.h>
#include <netinet/ip.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>
#include <netinet/icmp6.h>
/* Encryption key string */
#define KEY "xpazurm00"
/* Initialisation vector size */
#define IV_SIZE 16
/* Encryption key size, 32B = 256bit */
#define KEY_SIZE 32
/* Fragment/Packet window circular buffer capacity, 22.4KB - 24KB */
#define WINDOW_SIZE 16
/* Macros for getting protocol header sizes statically */
#define IPV4_SIZE sizeof(struct ip)
#define IPV6_SIZE 0 // Considered 0, since kernel doesn't include it in recv/recvfrom
#define ICMP_SIZE sizeof(struct icmphdr)
#define ICMPV6_SIZE sizeof(struct icmp6_hdr)
/* Inline functions for getting protocol header sizes dynamically */
static inline int ipvh_size(int v) { return (v == AF_INET) ? IPV4_SIZE : IPV6_SIZE; }
static inline int icmph_size(int v) { return (v == AF_INET) ? ICMP_SIZE : ICMPV6_SIZE; }
/* Secret protocol diagram
*
* | | | | |
* | IPv* header | ICMPv* header | SFTP header | Payload |
* | | | | |
* 20/40 bytes 8 bytes 8 bytes ~1.4 kb
*/
/* Packet type */
enum type {
META, // Metadata packet (necessary file info)
FILE_CHUNK, // Data packet
ACK, // Acknowledgement
ERROR, // Error message
FIN // Communication finalization
};
/* Secret file transfer header, 8 bytes*/
struct sftp_hdr {
uint16_t type; // SFTP packet type
uint16_t seq; // Packet ID - sequence number
uint16_t s_id; // Session id - client server communication
uint16_t size; // Data size
};
#define SFTP_SIZE sizeof(struct sftp_hdr)
/**** Protocol functions ****/
/* Checksum */
uint16_t calculate_checksum(void *addr, size_t size);
/* Add icmp header */
void add_icmph(uint8_t *packet, const int ip_v, bool request);
/* Add sftp header */
void add_sftp_hdr(uint8_t *packet, const uint16_t type, const uint16_t seq, const uint16_t size, const uint16_t s_id);
/* Helper functions */
void set_session_id(uint8_t *packet, uint16_t s_id);
uint16_t get_seq(uint8_t *packet);
uint16_t get_type(uint8_t *packet);
uint16_t get_size(uint8_t *packet);
/* Create state message to server */
size_t msg_pkt(uint8_t *packet, int ip_v, int type, uint16_t seq, uint16_t s_id);