-
Notifications
You must be signed in to change notification settings - Fork 8
/
packet_tools.c
102 lines (92 loc) · 2.65 KB
/
packet_tools.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
/*
* soliloque-server, an open source implementation of the TeamSpeak protocol.
* Copyright (C) 2009 Hugo Camboulive <hugo.camboulive AT gmail.com>
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include "packet_tools.h"
#include "compat.h"
#include "crc.h"
#include "log.h"
/**
* Add a crc to a given data packet.
*
* @param data the packet
* @param len the length of the packet
* @param offset the offset where we want to write our checksum.
*/
void packet_add_crc(char *data, size_t len, unsigned int offset)
{
char *ptr = data + offset;
uint32_t new_crc;
uint32_t *crc_ptr = (uint32_t *)ptr;
*crc_ptr = 0x00000000;
new_crc = GUINT32_TO_LE(crc_32(data, len, 0xEDB88320));
*crc_ptr = new_crc;
}
/**
* Check the crc of a packet
*
* @param data the packet
* @param len the length of the packet
* @param offset the offset where the checksum is located.
*/
int packet_check_crc(char *data, size_t len, unsigned int offset)
{
char *buff, *ptr;
uint32_t old_crc;
uint32_t new_crc;
uint32_t *crc_ptr;
buff = (char *)calloc(len, sizeof(char));
if (buff == NULL) {
logger(LOG_WARN, "packet_check_crc, buffer allocation failed : %s.", strerror(errno));
return 0;
}
memcpy(buff, data, len);
ptr = buff + offset;
crc_ptr = (uint32_t *)ptr;
old_crc = *crc_ptr;
*crc_ptr = 0x00000000;
new_crc = GUINT32_TO_LE(crc_32(buff, len, 0xEDB88320));
free(buff);
return new_crc == old_crc;
}
/**
* Add a crc to a packet at the default offset
* Most packets have the checksum at start+20 bytes,
* but some have it at start+16 bytes.
*
* @param data the packet
* @param len the length of the packet
*/
void packet_add_crc_d(char *data, size_t len)
{
packet_add_crc(data, len, 20);
}
/**
* Check the crc of a packet at the default offset
* Most packets have the checksum at start+20 bytes,
* but some have it at start+16 bytes.
*
* @param data the packet
* @param len the length of the packet
*/
int packet_check_crc_d(char *data, size_t len)
{
return packet_check_crc(data, len, 20);
}