-
Notifications
You must be signed in to change notification settings - Fork 120
/
mkfuzz.c
66 lines (54 loc) · 1.03 KB
/
mkfuzz.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
#include <arpa/inet.h>
#include <unistd.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
enum tftp_opcode {
RRQ = 1,
WRQ = 2,
DATA = 3,
ACK = 4,
ERR = 5,
OACK = 6
};
static inline char *pkt_mknum(char *pkt, uint16_t n)
{
*(uint16_t*)pkt = htons(n);
return pkt + 2;
}
static char *pkt_mkopt(char *pkt, const char *opt, const char* val)
{
strcpy(pkt, opt);
pkt += strlen(opt) + 1;
strcpy(pkt, val);
pkt += strlen(val) + 1;
return pkt;
}
int main(int argc, char** argv)
{
if (argc != 2) {
return 1;
}
const size_t fsize = 4096;
char pkt[1024 + 4];
char* p;
size_t len = 512;
memset(pkt, 0, sizeof(pkt));
if (argc == 2 && argv[1][0] == 'k') {
len = 1024;
p = pkt_mknum(pkt, OACK);
pkt_mkopt(p, "blksize", "1024");
} else {
p = pkt_mknum(pkt, ACK);
pkt_mknum(p, 0);
}
write(STDOUT_FILENO, pkt, len + 4);
size_t i = 0;
for (; i < (fsize/len + 1); ++i) {
memset(pkt, 0, sizeof(pkt));
p = pkt_mknum(pkt, ACK);
pkt_mknum(p, i + 1);
write(STDOUT_FILENO, pkt, len + 4);
}
}