标签:
//ProtoHead.h
1 #define IP_PROTO 0X0800 2 #define ARP_PROTO 0X0806 3 4 /*以下是以太网协议格式*/ 5 struct ether_header 6 { 7 unsigned char ether_dhost[6]; //目的Mac地址 8 unsigned char ether_shost[6]; //源Mac地址 9 unsigned short ether_type; //协议类型 10 }; 11 12 /*下面是ARP协议格式的定义*/ 13 struct arp_header 14 {unsigned short arp_hardware_type; /*硬件类型*/ 15 unsigned short arp_protocol_type; /*协议类型*/ 16 unsigned char arp_hardware_length; /*硬件地址长度*/ 17 unsigned char arp_protocol_length; /*协议地址长度*/ 18 unsigned short arp_operation_code; /*操作码*/ 19 unsigned char arp_source_ethernet_address[6]; /*源以太网地址*/ 20 unsigned char arp_source_ip_address[4]; /*源IP地址*/ 21 unsigned char arp_destination_ethernet_address[6]; /*目的以太网地址*/ 22 unsigned char arp_destination_ip_address[4]; /*目的IP地址*/ 23 };
// ResProtocol.cpp
1 #include "string.h" 2 #include "memory.h" 3 #include "ProtoHead.h" 4 #include "stdio.h" 5 #include "stdlib.h" 6 #include <math.h> 7 #include <iostream> 8 using namespace std; 9 10 static int m=0; 11 int main() 12 { 13 void detransformation(int temp); 14 int transformation(char macDes[]); 15 char* getDesMac(char prodat[],int m,struct ether_header *eheader,int flag); 16 char* getProtocolType(int start,char prodat[],struct ether_header *eheader,int end); 17 18 char* getHardwareType(int start,char prodat[],struct arp_header *arphead,int end); 19 char* getHardwareLength(int start,char prodat[],struct arp_header *arphead,int end); 20 char* getProtocolLength(int start,char prodat[],struct arp_header *arphead,int end); 21 char* getOperationCode(int start,char prodat[],struct arp_header *arphead,int end); 22 char* getArpSorceMac(int start,char prodat[],struct arp_header *arphead,int end); 23 char* getDesSorceMac(int start,char prodat[],struct arp_header *arphead,int end); 24 char* getArpIp(int start,char prodat[],struct arp_header *arphead,int type); 25 26 char prodat[1600]; 27 strcpy(prodat,"ffffff"); 28 strcat(prodat,"ffffff"); 29 30 strcat(prodat,"00e04c4f1090"); 31 strcat(prodat,"0806"); 32 strcat(prodat,"000108000604"); 33 strcat(prodat,"0001"); 34 strcat(prodat,"00e04c4f1090c0a80014"); 35 strcat(prodat,"000000000000c0a80002"); 36 strcat(prodat,"000000000000"); 37 strcat(prodat,"000000000000"); 38 strcat(prodat,"000000000000"); 39 40 ether_header eheader = {{0},{0},0}; 41 arp_header arphead = {0,0,0,0,0,{0},{0},{0},{0}}; 42 43 printf("以太网头部:\n"); 44 char desAddr[30]={0}; 45 strcpy(desAddr,getDesMac(prodat,m,&eheader,0)); 46 printf(" 目的Mac地址: %s\n",&desAddr); 47 48 char sourAddr[30]={0}; 49 strcpy(sourAddr,getDesMac(prodat,m,&eheader,1)); 50 printf(" 源Mac地址: %s\n",&sourAddr); 51 52 char protocolType[5]={0}; 53 strcpy(protocolType,getProtocolType(24,prodat,&eheader,28)); 54 printf(" 协议类型: %s\n",strcat(protocolType,"<ARP协议>")); 55 56 printf("Arp头部:\n"); 57 char hardwareType[5] = {0}; 58 strcpy(hardwareType,getProtocolType(28,prodat,&eheader,32)); 59 printf(" 硬件类型: %s\n",strcat(hardwareType,"<以太网>")); 60 61 char ipType[5]={0}; 62 strcpy(ipType,getHardwareType(32,prodat,&arphead,36)); 63 printf(" 协议类型: %s\n",strcat(ipType,"<IP>")); 64 65 char hardwareLength[3] = {0}; 66 strcpy(hardwareLength,getHardwareLength(36,prodat,&arphead,38)); 67 printf(" 硬件地址长度:%s\n",hardwareLength); 68 69 char protocolLength[3] = {0}; 70 strcpy(protocolLength,getProtocolLength(38,prodat,&arphead,40)); 71 printf(" 协议地址长度:%s\n",protocolLength); 72 73 char operationCode[5] = {0}; 74 strcpy(operationCode,getOperationCode(40,prodat,&arphead,44)); 75 printf(" 操作码: %s<ARP请求>\n",operationCode); 76 77 char arpSourceMac[30] = {0}; 78 strcpy(arpSourceMac,getArpSorceMac(44,prodat,&arphead,56)); 79 printf(" 源MAC地址: %s\n",arpSourceMac); 80 81 char sourceIp[20] = {0}; 82 strcpy(sourceIp,getArpIp(56,prodat,&arphead,1)); 83 printf(" 源IP地址: %s<",sourceIp); 84 for(int i=0;i<4;i++) 85 { 86 if(i==3) 87 printf("%d>\n",arphead.arp_source_ip_address[i]); 88 else 89 printf("%d.",arphead.arp_source_ip_address[i]); 90 } 91 92 char arpDesMac[30] = {0}; 93 strcpy(arpDesMac,getArpSorceMac(64,prodat,&arphead,76)); 94 printf(" 目的MAC地址: %s\n",arpDesMac); 95 96 char desIp[20] = {0}; 97 strcpy(desIp,getArpIp(76,prodat,&arphead,2)); 98 printf(" 目的IP地址: %s<",desIp); 99 for(int i=0;i<4;i++) 100 { 101 if(i==3) 102 printf("%d>\n",arphead.arp_destination_ip_address[i]); 103 else 104 printf("%d.",arphead.arp_destination_ip_address[i]); 105 } 106 system("pause"); 107 return 0; 108 } 109 110 //将每两个16进制数转为10进制 111 int transformation(char macDes[]) 112 { 113 //转为ascii码值 114 int a[2]={0}; 115 a[0] = (int)macDes[0]; 116 a[1] = (int)macDes[1]; 117 118 //定义一个8位字符数组,用以储存8位2进制数 119 int b[8] = {0}; 120 for(int i=0;i<2;i++) 121 { 122 int temp=0; //用以存储每一个10进制数 123 if(a[i]>=48 && a[i]<=57) 124 temp = a[i]-48; 125 else if(a[i]>=65 && a[i]<=90) 126 temp = a[i] - 55; 127 else if(a[i]>=97 && a[i]<=122) 128 temp = a[i] - 87; 129 130 //两个10进制转二进制 131 int j=0; 132 if(i==0) 133 {/////////////////////////////////////// 134 for(j=4;j<=7&&temp!=0;j++) 135 { 136 b[j] = temp%2; 137 temp /= 2; 138 } 139 } 140 if(i==1) 141 { 142 for(j=0;j<=3&&temp!=0;j++) 143 { 144 b[j] = temp%2; 145 temp /=2; 146 } 147 } 148 } 149 150 int result = 0; 151 for(int index=7;index>=0;index--) 152 result += b[index]*(int)(double)pow(2.0f,index); 153 154 return result; 155 } 156 157 //将10进制数转为两个16进制数 158 char* detransformation(int temp) 159 { 160 char result[3] = {0}; 161 int hex[2]={0}; 162 //将temp转为8位2进制数 163 int b[8] = {0}; 164 int length=0; 165 // 166 for(int j=0;j<8&&temp!=0;j++) 167 { 168 length++; 169 b[j] = temp%2; 170 temp /=2; 171 } 172 173 for(int k=7;k>=4;k--) 174 { 175 hex[0] += b[k]*(int)(double)pow(2.0f,k-4); 176 177 } 178 for(int m=3;m>=0;m--) 179 { 180 hex[1] += b[m]*(int)(double)pow(2.0f,m); 181 } 182 183 for(int n=0;n<2;n++) 184 { 185 if(hex[n]<=9) 186 result[n] = hex[n] + 48; 187 else 188 result[n] = hex[n] + 55; 189 } 190 // printf("result :%s---",&result); 191 return result; 192 } 193 194 //获得各种mac地址,12个字符 195 char* getDesMac(char prodat[],int m,struct ether_header *eheader,int flag) 196 { 197 if(flag==1) 198 m=12; 199 char result[30]={0}; 200 char macDes[6][2] = {0}; 201 202 for (int i=0;i<6;i++) 203 for(int j=0;j<2;j++) 204 macDes[i][j] = prodat[m++]; 205 206 for(int k=0;k<6;k++) 207 { 208 int afterTrans=0; 209 //transformation返回转换后的10进制数 210 afterTrans = transformation(macDes[k]); 211 if(flag==0) //标识是源地址还是目的地址 212 eheader->ether_dhost[k] = afterTrans; 213 else if(flag ==1) 214 eheader->ether_shost[k] = afterTrans; 215 216 if(k==5) 217 strcat(result,detransformation(afterTrans)); 218 else 219 strcat(result,strcat(detransformation(afterTrans),"--")); 220 } 221 return result; 222 } 223 224 //16进制字符串转10进制 225 unsigned short stringToHex(char temp[]) 226 { 227 int result = 0; 228 int a = 0; 229 int tempory = 0; 230 //把4位16进制数转为一个10进制数 231 for(int i=0;i<4;i++) 232 { 233 a = (int)temp[i]; 234 if(a>=48 && a<=57) 235 { 236 tempory = a - 48; 237 } 238 else if(a>=65 && a<=90) 239 { 240 tempory = a - 55; 241 } 242 else if(a>=97 && a<=122) 243 { 244 tempory = a - 87; 245 } 246 result += tempory*(int)(double)pow(16.0f,3-i); 247 } 248 return result; 249 } 250 251 //获得协议类型 252 char* getProtocolType(int start,char prodat[],struct ether_header *eheader,int end) 253 { 254 char temp[5]={0}; 255 for(int i=0;start<end;start++,i++) 256 temp[i] = prodat[start]; 257 eheader->ether_type=stringToHex(temp); 258 return temp; 259 } 260 261 //硬件类型 262 char* getHardwareType(int start,char prodat[],struct arp_header *arphead,int end) 263 { 264 char temp[5]={0}; 265 for(int i=0;start<end;start++,i++) 266 temp[i] = prodat[start]; 267 arphead->arp_hardware_type=stringToHex(temp); 268 return temp; 269 } 270 271 //硬件地址长度 272 char* getHardwareLength(int start,char prodat[],struct arp_header *arphead,int end) 273 { 274 char temp[3]={0}; 275 for(int i=0;start<end;start++,i++) 276 temp[i] = prodat[start]; 277 arphead->arp_hardware_type=stringToHex(temp); 278 return temp; 279 } 280 281 //协议地址长度 282 char* getProtocolLength(int start,char prodat[],struct arp_header *arphead,int end) 283 { 284 char temp[3]={0}; 285 for(int i=0;start<end;start++,i++) 286 temp[i] = prodat[start]; 287 arphead->arp_protocol_length=stringToHex(temp); 288 return temp; 289 } 290 291 //操作码 292 char* getOperationCode(int start,char prodat[],struct arp_header *arphead,int end) 293 { 294 char temp[5]={0}; 295 for(int i=0;start<end;start++,i++) 296 temp[i] = prodat[start]; 297 arphead->arp_operation_code=stringToHex(temp); 298 return temp; 299 } 300 301 // 302 char* getArpSorceMac(int start,char prodat[],struct arp_header *arphead,int end) 303 { 304 char result[30]={0}; 305 char macDes[6][2] = {0}; 306 307 for (int i=0;i<6;i++) 308 for(int j=0;j<2;j++) 309 macDes[i][j] = prodat[start++]; 310 311 for(int k=0;k<6;k++) 312 { 313 int afterTrans=0; 314 //transformation返回转换后的10进制数 315 afterTrans = transformation(macDes[k]); 316 317 arphead->arp_source_ethernet_address[k] = afterTrans; 318 319 if(k==5) 320 strcat(result,detransformation(afterTrans)); 321 else 322 strcat(result,strcat(detransformation(afterTrans),"--")); 323 } 324 return result; 325 } 326 327 // 328 char* getArpIp(int start,char prodat[],struct arp_header *arphead,int type) 329 { 330 char result[30]={0}; 331 char macDes[4][3] = {0}; 332 for (int i=0;i<4;i++) 333 for(int j=0;j<2;j++) 334 macDes[i][j] = prodat[start++]; 335 for(int k=0;k<4;k++) 336 { 337 int afterTrans=0; 338 //transformation返回转换后的10进制数 339 afterTrans = transformation(macDes[k]); 340 if(type == 1) 341 arphead->arp_source_ip_address[k] = afterTrans; 342 else if(type == 2) 343 arphead->arp_destination_ip_address[k] = afterTrans; 344 strcat(result,detransformation(afterTrans)); 345 } 346 return result; 347 }
标签:
原文地址:http://www.cnblogs.com/eleven24/p/4353349.html