-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpacket.c
120 lines (102 loc) · 3.27 KB
/
packet.c
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
112
113
114
115
116
117
118
119
120
/*
Copyright (c) 2006 Florian Wesch <[email protected]>. All Rights Reserved.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/
#ifdef WIN32
#include <winsock2.h>
#else
#include <arpa/inet.h>
#endif
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include "packet.h"
void packet_rewind(packet_t *packet) {
packet->offset = 0;
}
void packet_init(packet_t *packet, int type) {
packet->type = type;
packet_rewind(packet);
}
int packet_read08(packet_t *packet, uint8_t *data) {
if (packet->len - packet->offset < 1) {
fprintf(stderr, "reading beyond end\n");
return 0;
}
*data = packet->data[packet->offset];
packet->offset += 1;
return 1;
}
int packet_read16(packet_t *packet, uint16_t *data) {
uint8_t byte;
if (!packet_read08(packet, &byte)) return 0;
*data = byte & 0x7F;
if (byte & 0x80) {
if (!packet_read08(packet, &byte)) return 0;
*data |= byte << 7;
}
return 1;
}
int packet_read32(packet_t *packet, uint32_t *data) {
if (packet->len - packet->offset < 4) {
fprintf(stderr, "reading beyond end\n");
return 0;
}
*data = ntohl(*(uint32_t*)&packet->data[packet->offset]);
packet->offset += 4;
return 1;
}
int packet_readXX(packet_t *packet, void *data, int len) {
if (packet->len - packet->offset < len) {
fprintf(stderr, "reading beyond end\n");
return 0;
}
memcpy(data, &packet->data[packet->offset], len);
packet->offset += len;
return 1;
}
int packet_write08(packet_t *packet, uint8_t data) {
if (sizeof(packet->data) - packet->offset <= 1) {
fprintf(stderr, "packet too full\n");
return 0;
}
*((uint8_t*)&packet->data[packet->offset]) = data;
packet->offset += 1;
return 1;
}
int packet_write16(packet_t *packet, uint16_t data) {
assert(data <= 0x7FFF);
if (data <= 0x7F)
return packet_write08(packet, data);
else
return packet_write08(packet, (data & 0x7F) | 0x80) &&
packet_write08(packet, data >> 7);
}
int packet_write32(packet_t *packet, uint32_t data) {
if (sizeof(packet->data) - packet->offset <= 4) {
fprintf(stderr, "packet too full\n");
return 0;
}
*((uint32_t*)&packet->data[packet->offset]) = htonl(data);
packet->offset += 4;
return 1;
}
int packet_writeXX(packet_t *packet, const void *data, int len) {
if (sizeof(packet->data) - packet->offset <= len) {
fprintf(stderr, "packet too full\n");
return 0;
}
memcpy(&packet->data[packet->offset], data, len);
packet->offset += len;
return 1;
}