-
Notifications
You must be signed in to change notification settings - Fork 0
/
source.cpp
213 lines (149 loc) · 3.56 KB
/
source.cpp
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int count = 0;
FILE *fp;
int frame_long[1];
int frame_true_long;
unsigned char ip_ver_long;
int ip_ver;
int ip_headlength;
unsigned char ip_total_long[2];
int ip_total_true_long;
unsigned char ip_src_ip[4];
unsigned char ip_dst_ip[4];
unsigned char mf_df;
int mf;
int df;
unsigned char proto;
unsigned char ttl;
char file[20];
printf("请输入读入文件名:");
scanf("%s",file);
char filew[20];
printf("请输入输出文件名:");
scanf("%s",filew);
if ((fp=fopen(file,"rb"))==NULL)
{
printf("cannot open this file\n");
return 0;
}
FILE *fpw;
fpw=fopen(filew,"a+");
fprintf(fpw,"序号 帧长 IP版本 首部长 DF MF 协议类型 TTL 数据报长 源地址 目标地址\n");
int size ;
fseek(fp,0,SEEK_END);
size = ftell(fp);
//跳过文件头
fseek(fp,24L,0);
while(size!=ftell(fp))
{
count++;
//得到帧长度
fseek(fp,12L,1);
fread (frame_long,4,1,fp);
frame_true_long =frame_long[0];
//跳过MAC
fseek(fp,12L,1);
//08 00 判断
if (fgetc(fp)!=8)
{
fseek(fp,frame_true_long-13,1);
continue;
}
if (fgetc(fp)!=0)
{
fseek(fp,frame_true_long-14,1);
continue;
}
//IP版本 头大小
ip_ver_long=fgetc(fp);
ip_ver=ip_ver_long>>4;
ip_headlength=(ip_ver_long&(15))*4;
//区分服务
fseek(fp,1L,1);
//总长度
char str1[256];
fread (ip_total_long,2,1,fp);
int temp;
temp=ip_total_long[1];
ip_total_long[1]=ip_total_long[0];
ip_total_long[0]=temp;
sprintf(str1, "%d",*ip_total_long);
//ip_total_true_long=;
// fseek(fp,2L,1);
//标识
fseek(fp,2L,1);
/*
标志
*/
//标志位
mf_df=fgetc(fp);
df=(mf_df>>7)&1;
mf=(mf_df>>6)&1;
//跳过片偏移量
fseek(fp,1L,1);
//生存时间
ttl=fgetc(fp);
//协议
proto=fgetc(fp);
//源地址
fseek(fp,2L,1);
fread (ip_src_ip,4,1,fp);
//目的地址
fread (ip_dst_ip,4,1,fp);
//跳过选择字段+填充位+数据
fseek(fp,-34,1);
fseek(fp,frame_true_long,1);
//输出
/*
printf("%d\n",count);
printf("帧长:%d\n",frame_true_long);
printf("IP版本:%d 首部长:%d\n",ip_ver,ip_headlength);
printf("DF:%d\n",df);
printf("MF:%d\n",mf);
switch (proto)
{
case 1: printf("协议:ICMP\n"); break;
case 2: printf("协议:IGMP\n");break;
case 6: printf("协议:TCP\n");break;
case 17:printf("协议:UDP\n");break;
case 88:printf("协议:IGRP\n"); break;
case 89:printf("协议:OSPF\n"); break;
default: printf("协议:未知\n");break;
}
printf("TTL:%d\n",ttl);
printf("数据报长度:%s\n",str1);
printf("源地址:%d.%d.%d.%d\n",ip_src_ip[0],ip_src_ip[1],ip_src_ip[2],ip_src_ip[3]);
printf("目标地址:%d.%d.%d.%d\n",ip_dst_ip[0],ip_dst_ip[1],ip_dst_ip[2],ip_dst_ip[3]);
printf("\n");
*/
fprintf(fpw,"%d ",count);
fprintf(fpw,"%d ",frame_true_long);
fprintf(fpw,"%d %d ",ip_ver,ip_headlength);
fprintf(fpw,"%d ",df);
fprintf(fpw,"%d ",mf);
switch (proto)
{
case 1: fprintf(fpw,"ICMP "); break;
case 2: fprintf(fpw,"IGMP ");break;
case 6: fprintf(fpw,"TCP ");break;
case 17:fprintf(fpw,"UDP ");break;
case 88:fprintf(fpw,"IGRP "); break;
case 89:fprintf(fpw,"OSPF "); break;
default:fprintf(fpw,"未知 ");break;
}
fprintf(fpw,"%d ",ttl);
fprintf(fpw,"%s ",str1);
fprintf(fpw,"%d.%d.%d.%d ",ip_src_ip[0],ip_src_ip[1],ip_src_ip[2],ip_src_ip[3]);
fprintf(fpw,"%d.%d.%d.%d\n",ip_dst_ip[0],ip_dst_ip[1],ip_dst_ip[2],ip_dst_ip[3]);
};
//结束
printf("输出成功\n");
system("pause");
fclose(fp);
fclose(fpw);
return 0;
}