-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprettypacket.c
151 lines (122 loc) · 3.06 KB
/
prettypacket.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
/*
* prettypacket.c
*
* Created on: 03/dec/2012
* Author: Acri Emanuele <crossbower@gmail.com>
*
* Disassemble network packet and print their fields.
* Uses the stdin to receive raw packet data. Prints on stdout.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "prettypacket.h"
#define VERSION "1.5"
#define BUFFER_SIZE 8192
/**
* Packet type arguments
*/
enum packet_type {
no_type = 0,
tcp,
udp,
icmp,
igmp,
arp,
stp
};
/**
* Packets disassembling loop
*/
int dis_packet_loop()
{
char buffer[BUFFER_SIZE];
ssize_t size;
int input = fileno(stdin);
while (!feof(stdin)) {
/* Read raw */
size = read(input, buffer, BUFFER_SIZE);
/* Print sisassembled packet */
layer_2_dispatcher(buffer, size, 0);
/* Print end of packet */
puts("\n ----------- ");
fflush(stdout);
}
puts("");
return 0;
}
/**
* Print disassembled example packet
*/
void print_dis_example_packet (enum packet_type type) {
switch (type) {
case tcp: layer_2_dispatcher(tcp_packet, sizeof(tcp_packet)-1, 0); break;
case udp: layer_2_dispatcher(udp_packet, sizeof(udp_packet)-1, 0); break;
case icmp: layer_2_dispatcher(icmp_packet, sizeof(icmp_packet)-1, 0); break;
case igmp: layer_2_dispatcher(igmp_packet, sizeof(igmp_packet)-1, 0); break;
case arp: layer_2_dispatcher(arp_packet, sizeof(arp_packet)-1, 0); break;
case stp: layer_2_dispatcher(stp_packet, sizeof(stp_packet)-1, 0); break;
}
puts("");
}
/**
* Convert string to packet_type
*/
enum packet_type str_to_packet_type(const char *str)
{
if (strcmp(str, "tcp") == 0)
return tcp;
if (strcmp(str, "udp") == 0)
return udp;
if (strcmp(str, "icmp") == 0)
return icmp;
if (strcmp(str, "igmp") == 0)
return igmp;
if (strcmp(str, "arp") == 0)
return arp;
if (strcmp(str, "stp") == 0)
return stp;
return no_type;
}
/**
* Usage
*/
void usage(char *progname) {
printf("PrettyPacket " VERSION " [disassembler for raw network packets]\n"
"written by: Emanuele Acri <crossbower@gmail.com>\n\n"
"Usage:\n"
"\t%s [-x|-h]\n"
"\nOptions:\n"
"\t-x type\tprint example packet, to see its structure\n"
"\t \t(available types: tcp, udp, icmp, igmp, arp, stp)\n"
"\t-h\tthis help screen\n", progname);
exit(0);
}
/**
* Main function
*/
int main(int argc, char **argv) {
enum packet_type type = no_type;
// check arguments
if(argc > 1) {
// print example packet
if(!strcmp(argv[1], "-x")) {
if(argc < 3) usage(argv[0]);
type = str_to_packet_type(argv[2]);
if(type == no_type) usage(argv[0]);
}
// unknown options
else {
usage(argv[0]);
}
}
// example packet
if(type != no_type) {
print_dis_example_packet(type);
}
// disassemble packets
else {
dis_packet_loop();
}
return 0;
}