-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpacket.h
More file actions
63 lines (51 loc) · 1.38 KB
/
Copy pathpacket.h
File metadata and controls
63 lines (51 loc) · 1.38 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
#ifndef _PACKET_H
#define _PACKET_H
#include <stdint.h>
#define HEAD(p) ((Header *)(p))
#define DATA(p, type) ((type *)((uint8_t *)(p) + sizeof(Header)))
#define PACKET_MAX (sizeof(Header) + sizeof(AttachData))
#define data_sz(p) (HEAD(p)->type == ATTACH ? sizeof(AttachData) : \
HEAD(p)->type == ADVERTISE ? sizeof(AdvertiseData) : \
HEAD(p)->type == CONNECT ? sizeof(ConnectData) : \
HEAD(p)->type == KEEPALIVE ? 1 : \
HEAD(p)->type == ERROR ? sizeof(ErrorData) : 0)
#define packet_sz(p) (sizeof(Header) + data_sz(p))
enum {
ATTACH,
ADVERTISE,
CONNECT,
KEEPALIVE,
ERROR = 0xFF
};
enum {
ERROR_UNAUTHORIZED,
ERROR_NOT_FOUND,
ERROR_INVALID_TIMESTAMP,
ERROR_TOO_MANY_ADVERTS,
};
typedef struct __attribute__((packed)) {
uint8_t type;
uint8_t mac[16];
} Header;
typedef struct {
uint8_t e[32]; // ephemeral public key (plaintext)
uint8_t s[32]; // static public key (ciphered: es)
} HandshakeData;
typedef struct __attribute__((packed)) {
HandshakeData hs;
uint8_t t[32]; // target public key (ciphered: ss)
uint8_t mac2[16];
} AttachData;
typedef struct __attribute__((packed)) {
HandshakeData hs;
uint64_t ts_ms;
uint8_t mac2[16];
} AdvertiseData;
typedef struct __attribute__((packed)) {
uint32_t addr; // ciphered: ss
uint16_t port; // ciphered: ss
} ConnectData;
typedef struct {
uint8_t code; // error: ciphered: ss
} ErrorData;
#endif