forked from adam-palmer1/tundeep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecv.c
159 lines (140 loc) · 3.24 KB
/
recv.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
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 "def.h"
char recvbuf[MAX_PCAP_SIZ];
char *rdata(int fd, int len)
{
int nread;
char *retbuf = malloc(len);
if (udpmode)
{
nread = recvfrom(fd, retbuf, len, 0, NULL, NULL);
} else {
nread = read_n(fd, retbuf, len);
}
if (nread == 0)
{
debug(1, 1, "nread == 0");
}
return retbuf;
}
int findcksum(int fd)
{
unsigned int ctr = 0;
int fullmatch = 0;
char *c;
int iter = 0;
while (!fullmatch)
{
c = rdata(fd, sizeof(PREAMBLE)-1);
fullmatch = 1;
while (ctr < (sizeof(PREAMBLE)-1))
{
if (c[ctr] != PREAMBLE[ctr])
{
fullmatch = 0;
}
ctr++;
}
if (fullmatch == 1)
{
debug(5, 0, "Preamble matched at iteration %d", iter);
iter = 0;
} else {
debug(5, 0, "Preamble failed at iteration %d", iter);
iter++; ctr = 0;
}
free(c);
}
return fd;
}
#ifdef _COMPRESS
int recvdata_c(int s)
{
//unsigned int _tap_uncompress(char **dst, unsigned int ucompSize, char *comp);
char *tmp_pkt = NULL;
char *recv_pkt = NULL;
uint32_t plength = 0, p = 0;
uint32_t clength = 0, c = 0;
if (cksum)
{
findcksum(s);
}
recv_pkt = rdata(s, sizeof(clength));
memcpy((char *)&clength, recv_pkt, sizeof(clength));
c = ntohl(clength);
free(recv_pkt);
debug(4, 0, "compressed recvdata() packet of %d size", c);
if ( (c < 1) || (c > MAX_PCAP_SIZ) )
{
debug(5, 0, "Broken size. Frame lost");
//bad len
//drop
} else {
tmp_pkt = rdata(s, c);
//tmp_pkt now contains compressed data
_tap_uncompress(&recv_pkt, MAX_PCAP_SIZ-1, tmp_pkt, c);
memcpy((char *)&plength, recv_pkt, sizeof(uint32_t));
p = ntohl(plength);
if ( (p < 1) || (p > MAX_PCAP_SIZ) )
{
debug(5, 0, "Broken size. Frame lost");
//bad len
//drop
} else {
injection_process(p, (const u_char *)recv_pkt+sizeof(uint32_t));
}
free(recv_pkt);
free(tmp_pkt);
}
return 0;
}
#endif
int recvdata_udp(int s)
{
size_t nread = recvfrom(s, recvbuf, sizeof recvbuf, 0, NULL, NULL);
if (nread == 0)
{
debug(1, 1, "nread == 0");
}
if (nread) {
injection_process(nread, (const u_char *)recvbuf);
}
return 0;
}
int recvdata(int s)
{
char *recv_pkt = NULL;
uint32_t plength = 0, p = 0;
if (cksum)
{
findcksum(s);
}
if (udpmode) return recvdata_udp(s);
recv_pkt = rdata(s, sizeof(plength));
memcpy((char *)&plength, recv_pkt, sizeof(plength));
free(recv_pkt);
p = ntohl(plength);
debug(4, 0, "recvdata() packet of %d size", p);
if ( (p < 1) || (p > MAX_PCAP_SIZ) )
{
debug(5, 0, "Broken size. Frame lost");
//something's gone wrong
//ignore the packet
} else {
recv_pkt = rdata(s, p);
injection_process(p, (const u_char *)recv_pkt);
free(recv_pkt);
}
return 0;
}