代碼如下:
void icmp_protocol(unsigned char *icmp_data)
{
struct icmp_header *icmp_protocol;
icmp_protocol = (struct icmp_header *)icmp_data;
printf("--------------------Transport Layer:Internet Control Message Protocol--------------------\n");
switch(icmp_protocol->type){
case 0:
printf("Type: 0 (Echo(ping)reply)\n");
printf("Code: %d\nChecksum: 0x%04x\n",icmp_protocol->code,ntohs(icmp_protocol->checksum));
break;
case 8:
printf("Type: 8 (Echo(ping)request)\n");
printf("Code: %d\nChecksum: 0x%04x\n",icmp_protocol->code,ntohs(icmp_protocol->checksum));
break;
case 3:
printf("Type: 3 (Destination unreachable)\n");
switch(icmp_protocol->code){
case 1:
printf("Code: 1 (Host unreachable)\nChecksum: 0x%04x",ntohs(icmp_protocol->checksum));
break;
case 3:
printf("Code: 3 (Port unreachable)\nChecksum: 0x%04x",ntohs(icmp_protocol->checksum));
break;
case 4:
printf("Code: 4 (Need sliced)\nChecksum: 0x%04x",ntohs(icmp_protocol->checksum));
break;
default:
printf("Unknow code\n");
break;
}
case 11:
printf("Type: 11 (Survival time timeout)");
printf("Code: %d \nChecksum: 0x%04x",icmp_protocol->code,ntohs(icmp_protocol->checksum));
break;
default:
printf("Unknow type\n");
break;
}
printf("Identifier (BE): %d (0x%04x)",ntohs(icmp_protocol->icd_id),ntohs(icmp_protocol->icd_id));
printf("Sequence number (BE): %d (0x%04x)",ntohs(icmp_protocol->icd_sequence),ntohs(icmp_protocol->icd_sequence));
}
這是頭檔案
#ifndef _BASIC_H_
#define _BASIC_H_
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <libnet.h>
#include <pcap.h>
#include <arpa/inet.h>
#include <time.h>
#include <sys/types.h>
struct ether_header
{
unsigned char ether_dhost[6]; //目的mac
unsigned char ether_shost[6]; //源mac
unsigned short ether_type; //以太網型別
};
struct ip_header
{
unsigned int ip_v:4; // version
unsigned int ip_hl:4; // header length
u_int8_t ip_tos; // type of service
u_short ip_len; // total length
u_short ip_id; // identification
u_short ip_off; // fragment offset field
/*#define IP_RF 0x8000 // reserved fragment flag
#define IP_DF 0x4000 // dont fragment flag
#define IP_MF 0x2000 // more fragments flag
#define IP_OFFMASK 0x1fff // mask for fragmenting bits*/
u_int8_t ip_ttl; // time to live
u_int8_t ip_p; // protocol
u_short ip_sum; // checksum
struct in_addr ip_src, ip_dst; // source and dest address
};
struct arp_header
{
unsigned short int ar_hrd; // Format of hardware address.
unsigned short int ar_pro; // Format of protocol address.
unsigned char ar_hln; // Length of hardware address.
unsigned char ar_pln; // Length of protocol address.
unsigned short int ar_op; // ARP opcode (command).
u_int8_t arp_sha[6]; // sender hardware address
u_int8_t arp_spa[4]; // sender protocol address
u_int8_t arp_tha[6]; // target hardware address
u_int8_t arp_tpa[4]; // target protocol address
};
struct rarp_header
{
unsigned short int rar_hrd; // Format of hardware address.
unsigned short int rar_pro; // Format of protocol address.
unsigned char rar_hln; // Length of hardware address.
unsigned char rar_pln; // Length of protocol address.
unsigned short int rar_op; // ARP opcode (command).
u_int8_t rarp_sha[6]; // sender hardware address
u_int8_t rarp_spa[4]; // sender protocol address
u_int8_t rarp_tha[6]; // target hardware address
u_int8_t rarp_tpa[4]; // target protocol address
};
struct tcp_header
{
u_int16_t source;
u_int16_t dest;
u_int32_t seq;
u_int32_t ack_seq;
u_int16_t doff:4;
u_int16_t res1:4;
u_int16_t res2:2;
u_int16_t urg:1;
u_int16_t ack:1;
u_int16_t psh:1;
u_int16_t rst:1;
u_int16_t syn:1;
u_int16_t fin:1;
u_int16_t window;
u_int16_t check;
u_int16_t urg_ptr;
};
struct udp_header
{
u_int16_t sport;
u_int16_t dport;
u_int16_t len; //udp length
u_int16_t check; //checksum
};
struct icmp_header
{
u_int8_t type; //message type
u_int8_t code; //type sub-code
u_int16_t checksum; //ones
u_int16_t icd_id;
u_int16_t icd_sequence;
/*union
{
u_char ih_pptr; //icmp_paramprob
struct in_addr ih_gwaddr; //gateway address
struct ih_idseq //echo datagram
{
u_int16_t icd_id;
u_int16_t icd_sequence;
} ih_idseq;
u_int32_t ih_void;
//icmp_unreach_needfrag
struct ih_pmtu
{
u_int16_t ipm_void;
u_int16_t ipm_nextmtu;
} ih_pmtu;
struct ih_rtradv
{
u_int8_t irt_num_addrs;
u_int8_t irt_wpa;
u_int16_t irt_lifetime;
} ih_rtradv;
}*/
};
void ethernet_protocol_callback(unsigned char *argument,const struct pcap_pkthdr *packet_heaher,const unsigned char *packet_content);
void ip_protocol(unsigned char *ip_data);
void arp_protocol(unsigned char *arp_data);
void rarp_protocol(unsigned char *rarp_data);
void icmp_protocol(unsigned char *icmp_data);
void tcp_protocol(unsigned char *tcp_data);
void udp_protocol(unsigned char *udp_data);
int protocol_type(u_int16_t port,int judge);
#define BUFSIZE 1514
#endif
請大神解惑編譯時為何會出現如下錯誤:
wireshark.c:121:35: error: expected ‘;’, ‘,’ or ‘)’ before ‘.’ token
void icmp_protocol(unsigned char *icmp_data)
basic.h:133:35: error: expected ‘;’, ‘,’ or ‘)’ before ‘.’ token
void icmp_protocol(unsigned char *icmp_data);
wireshark.c:124:43: error: ‘dun’ undeclared (first use in this function); did you mean ‘dup’?
icmp_protocol = (struct icmp_header *)icmp_data;
uj5u.com熱心網友回復:
是不是變數名和函式名重名了?轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/41195.html
標籤:新手樂園
上一篇:求助,C語言編程,萬分感謝
下一篇:順序查找和折半查找
