/
printp.c
137 lines (123 loc) · 4.85 KB
/
printp.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
/**
* CS 558L Lab 9
*
* Print packets
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/in.h>
#include "packet.h"
#include "packet_util.h"
#include "printp.h"
void fprintp(FILE* logfile, u_char* packet, int size) {
fprintf(logfile, "============================== packet received, size = %d ==============================\n", size);
struct rthdr* rth = (struct rthdr*) (packet + sizeof(struct ethhdr));
switch(rth->protocol) {
case ROUTE_ON_CONTROL:
print_ctl_packet(logfile, packet, size);
break;
case ROUTE_ON_UNRELIABLE:
print_ur_packet(logfile, packet, size);
break;
case ROUTE_ON_RELIABLE:
print_rl_packet(logfile, packet, size);
break;
default:
fprintf(logfile, "this is a non route-on defined packet\n");
print_data(logfile, packet, size);
break;
}
fprintf(logfile, "============================== end of packet ==============================\n\n\n\n\n\n");
}
void print_rthdr(FILE* logfile, struct rthdr* hdr) {
// TODO: test if we need any ntohs(); or htons();
fprintf(logfile, "Routing Header:\n");
fprintf(logfile, "\t|-source: %04x\n", hdr->saddr);
fprintf(logfile, "\t|-destination: %04x\n", hdr->daddr);
fprintf(logfile, "\t|-ttl: %d\n", (unsigned int)(hdr->ttl));
fprintf(logfile, "\t|-protocol: %d\n", (unsigned int)(hdr->protocol));
fprintf(logfile, "\t|-size: %d\n", (unsigned int)ntohs((hdr->size)));
fprintf(logfile, "\t|-checksum: %04x\n", ntohs(hdr->check));
fprintf(logfile, "checksum test: %04x\n", rthdr_chk_gen(hdr));
fprintf(logfile, "\n");
}
void print_ctl_packet(FILE* logfile, u_char* packet, int size) {
struct rthdr* rth = (struct rthdr*) (packet + sizeof(struct ethhdr));
print_rthdr(logfile, rth);
struct chdr* ch = (struct chdr*)(packet + sizeof(struct ethhdr) + sizeof(struct rthdr));
fprintf(logfile, "Control Protocol Header:\n");
fprintf(logfile, "\t|-checksum: %d\n", ntohs(ch->check));
fprintf(logfile, "checksum test: %04x\n", packet_chk_gen(packet, size));
fprintf(logfile, "\n");
print_data(logfile, packet, size);
}
void print_ur_packet(FILE* logfile, u_char* packet, int size) {
struct rthdr* rth = (struct rthdr*) (packet + sizeof(struct ethhdr));
print_rthdr(logfile, rth);
struct urhdr* urh = (struct urhdr*)(packet + sizeof(struct ethhdr) + sizeof(struct rthdr));
fprintf(logfile, "Unreliable Protocol Header:\n");
fprintf(logfile, "\t|-port: %d\n", (unsigned int)(urh->port));
fprintf(logfile, "\t|-checksum: %04x\n", ntohs(urh->check));
fprintf(logfile, "checksum test: %04x\n", packet_chk_gen(packet, size));
fprintf(logfile, "\n");
print_data(logfile, packet, size);
}
void print_rl_packet(FILE* logfile, u_char* packet, int size) {
struct rthdr* rth = (struct rthdr*) (packet + sizeof(struct ethhdr));
print_rthdr(logfile, rth);
struct rlhdr* rlh = (struct rlhdr*)(packet + sizeof(struct ethhdr) + sizeof(struct rthdr));
fprintf(logfile, "Reliable Protocol Header:\n");
fprintf(logfile, "\t|-port: %d\n", (unsigned int)(rlh->port));
fprintf(logfile, "\t|-sequence number: %d\n", (unsigned int)ntohs((rlh->seq)));
fprintf(logfile, "\t|-checksum: %04x\n", ntohs(rlh->check));
fprintf(logfile, "checksum test: %04x\n", packet_chk_gen(packet, size));
fprintf(logfile, "\n");
print_data(logfile, packet, size);
}
void print_ke_packet(FILE* logfile, u_char* packet, int size) {
struct rthdr* rth = (struct rthdr*) (packet + sizeof(struct ethhdr));
print_rthdr(logfile, rth);
struct kehdr* keh = (struct kehdr*)(packet + sizeof(struct ethhdr) + sizeof(struct rthdr));
fprintf(logfile, "Key Exchange Protocol Header:\n");
fprintf(logfile, "\t|-size: %d\n", (unsigned int)(keh->size));
fprintf(logfile, "\n");
print_data(logfile, packet, size);
}
void print_data(FILE* logfile, const u_char* data, int size) {
fprintf(logfile, "Data:\n");
int i, j;
for(i=0 ; i < size ; i++) {
//if one line of hex printing is complete...
if( i!=0 && i%16==0) {
fprintf(logfile , " ");
for(j=i-16 ; j<i ; j++) {
if(data[j]>=32 && data[j]<=128) {
fprintf(logfile , "%c",(unsigned char)data[j]); //if its a number or alphabet
} else {
fprintf(logfile , "."); //otherwise print a dot
}
}
fprintf(logfile , "\n");
}
if(i%16==0) {
fprintf(logfile , " ");
}
fprintf(logfile , " %02X",(unsigned int)data[i]);
//print the last spaces
if( i==size-1) {
for(j=0;j<15-i%16;j++) {
fprintf(logfile , " "); //extra spaces
}
fprintf(logfile , " ");
for(j=i-i%16 ; j<=i ; j++) {
if(data[j]>=32 && data[j]<=128) {
fprintf(logfile , "%c",(unsigned char)data[j]);
} else {
fprintf(logfile , ".");
}
}
fprintf(logfile , "\n" );
}
}
}