-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpacket.h
111 lines (90 loc) · 2.5 KB
/
packet.h
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#ifndef PACKET_H
#define PACKET_H
#include <string.h>
#include <stdint.h>
#ifndef _WIN32
#include <arpa/inet.h>
#else
#include <winsock2.h>
#include <ws2tcpip.h>
#endif
#ifndef __packed
#define __packed __attribute__((__packed__))
#endif
#define MAX_SIZE_MSG 4096
#define HEADER_SIZE 4
#undef INET_ADDRSTRLEN
#define INET_ADDRSTRLEN 16
#define IP4_IDENTITY_SIZE INET_ADDRSTRLEN + sizeof(":65535")
enum {
/* sent packet from client to server */
CL_PKT_MSG = 10,
/* broadcast packet from server to active client */
SR_PKT_MSG_ID = 20,
/* send event info from server to client */
SR_PKT_JOIN = 21,
SR_PKT_LEAVE = 22,
};
struct packet_msg {
uint16_t len;
char data[];
} __packed;
/* Why does the struct order is matter here? see https://t.me/GNUWeeb/854569 for further explanation */
struct packet_msg_id {
char identity[IP4_IDENTITY_SIZE];
struct packet_msg msg;
} __packed;
struct packet_msg_event {
char identity[IP4_IDENTITY_SIZE];
} __packed;
struct packet {
uint8_t type;
uint8_t __pad;
uint16_t len;
union {
/* broadcast data from server to clients */
struct packet_msg_id msg_id;
/* send data from client to server */
struct packet_msg msg;
/* join and leave notifier */
struct packet_msg_event event;
char __raw_buf[4096 + MAX_SIZE_MSG];
};
};
static inline size_t prep_pkt_msg_id(struct packet *pkt, const char *id, const char *msg, size_t len)
{
size_t body_len;
body_len = sizeof(pkt->msg_id) + len;
pkt->type = SR_PKT_MSG_ID;
pkt->len = htons(body_len);
pkt->msg_id.msg.len = htons(len);
strncpy(pkt->msg_id.identity, id, sizeof(pkt->msg_id.identity));
strncpy(pkt->msg_id.msg.data, msg, len);
return HEADER_SIZE + body_len;
}
static inline size_t prep_pkt_msg(struct packet pkt, const char *msg, size_t len)
{
/* Make it dynamic/flexible, Just send occupied size */
size_t body_len;
body_len = sizeof(pkt.msg) + len;
pkt.type = CL_PKT_MSG;
pkt.len = htons(body_len);
pkt.msg.len = htons(len);
memcpy(pkt.msg.data, msg, len);
return HEADER_SIZE + body_len;
}
static inline size_t prep_pkt_msg_join(struct packet *pkt, const char *id)
{
pkt->type = SR_PKT_JOIN;
pkt->len = htons(sizeof(pkt->event));
strncpy(pkt->event.identity, id, sizeof(pkt->event.identity));
return HEADER_SIZE + sizeof(pkt->event);
}
static inline size_t prep_pkt_msg_left(struct packet *pkt, const char *id)
{
pkt->type = SR_PKT_LEAVE;
pkt->len = htons(sizeof(pkt->event));
strncpy(pkt->event.identity, id, sizeof(pkt->event.identity));
return HEADER_SIZE + sizeof(pkt->event);
}
#endif