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 214 215 216 217 218
| #include "stdafx.h"
#include "PacketAfdx.h"
pcap_if_t* PacketAfdx::ChooseDev2(){
int nbdev;
pcap_if_t *d;
pcap_if_t *alldevs;
// Retrieve the device list on the local machine
nbdev=0;
char errbuf[PCAP_ERRBUF_SIZE];
if (pcap_findalldevs(&alldevs, errbuf) == -1)
{
cout<<"Error in pcap_findalldevs";
exit(1);
}
for(d=alldevs; d!= NULL;
d= d->next)
{
cout<<++nbdev<<"."<< d->name;
if (d->description)
cout<<" ("<<d->description<<")"<<"\n";
else
cout<<" (No description available)\n";
}
if(nbdev==0)
{
cout<<"\nNo interfaces found! Make sure WinPcap is installed.\n";
exit(1);
}
return alldevs;
}
PacketAfdx::PacketAfdx()
{
}
pcap_t * PacketAfdx::OpenDev(pcap_if_t *alldevs, int inum)
{
pcap_t *adhandle;
pcap_if_t *d;
char errbuf[PCAP_ERRBUF_SIZE];
int i;
for(d=alldevs, i=0; i< inum-1 ;d=d->next, i++);
if ( (adhandle= pcap_open(d->name, // name of the device
65536, // portion of the packet to capture.
// 65536 guarantees that the whole packet will be captured on all the link layers
0, // promiscuous mode
1000, // read timeout
NULL, // authentication on the remote machine
errbuf // error buffer
) ) == NULL)
{
/* Free the device list */
cout <<"Le bloccage est ici";
pcap_freealldevs(alldevs);
}
cout <<"\nlistening on ...\n"<< d->description;
if(pcap_datalink(adhandle) != DLT_EN10MB)
{
fprintf(stderr,"\nThis program works only on Ethernet networks.\n");
}
long netmask;
if (d->addresses != NULL)
//Retrieve the mask of the first address of the interface
netmask=((struct sockaddr_in *)(d->addresses->netmask))->sin_addr.S_un.S_addr;
else
// If the interface is without an address we suppose to be in a C class network
netmask=0xffffff;
struct bpf_program* fcode= (bpf_program *) malloc(sizeof(struct bpf_program));
//compile the filter
if (pcap_compile(adhandle, &fcode, "udp src port 57344", 0, netmask) < 0)
{
fprintf(stderr,"\nUnable to compile the packet filter. Check the syntax.\n");
// Free the device list
}
//set the filter
if (pcap_setfilter(adhandle, &fcode) < 0)
{
fprintf(stderr,"\nError setting the filter.\n");
// Free the device list
}
// At this point, we don't need any more the device list. Free it
pcap_freealldevs(alldevs);
return adhandle;
}
void PacketAfdx::print_Eth_header(const struct ether_header * ethernet){
fprintf(stdout,"-------------------[ETH HEADER]-----------\r\n");
fprintf(stdout,"| %.2X:%.2X:%.2X:%.2X:%.2X:%.2X -> ",ethernet->ether_shost[0],ethernet->ether_shost[1], ethernet->ether_shost[2],ethernet->ether_shost[3],ethernet->ether_shost[4],ethernet->ether_shost[5]);
fprintf(stdout,"%.2X:%.2X:%.2X:%.2X:%.2X:%.2X |\r\n",ethernet->ether_dhost[0],ethernet->ether_dhost[1], ethernet->ether_dhost[2],ethernet->ether_dhost[3],ethernet->ether_dhost[4],ethernet->ether_dhost[5]);
fprintf(stdout,"------------------------------------------\r\n");
fprintf(stdout,"Protocol: 0x%.4X\r\n", htons(ethernet->ether_type));
}
void PacketAfdx::print_ip_header(const struct ip_header * ip){
fprintf(stdout,"The Network Id is: %.5d | ",ip->saddr.NetId);
fprintf(stdout,"The Equipment Id is: %.5d \r\n ",ip->saddr.EquiId);
fprintf(stdout,"The Partition Id is: %.5d | ",ip->saddr.PartitionId);
fprintf(stdout,"The Virtual Link Id is: %.5d \r\n ",ip->daddr.VL_Identifier);
fprintf(stdout,"--------------------[IP HEADER]----------------------\r\n");
fprintf(stdout,"| IP header length : %d * 32 bits = %d bytes |\r\n",ip->ihl, ip->ihl * 4);
//fprintf(stdout,"| IP version : %.5d | ",ip->version);
//fprintf(stdout,"Type of service : %.5d |\r\n",ip->tos);
fprintf(stdout,"| Total length : %.5d | ",htons(ip->tot_len));
fprintf(stdout,"Identification : %.5d |\r\n",htons(ip->Frag_id));
fprintf(stdout,"| Time to live : %.5d | ",ip->ttl);
fprintf(stdout,"Protocol : %.5d |\r\n",ip->protocol);
fprintf(stdout,"| Checksum : %.5d |\r\n",ip->check);
fprintf(stdout,"------------------------------------------------------\r\n");
}
void PacketAfdx::print_udp_header(const struct udp_header * udp){
fprintf(stdout,"[ Port : %.5d -> %.5d ]\n", ntohs(udp->source), ntohs(udp->dest) );
fprintf(stdout,"--------------------[UDP HEADER]---------------------\r\n");
fprintf(stdout,"| Checksum : %.5d |\r\n",udp->udp_cheksum);
fprintf(stdout,"| Length : %.5d |\r\n",udp->udp_length);
fprintf(stdout,"-----------------------------------------------------\r\n");
}
void PacketAfdx::ParseDataPacket(pcap_t *adhandle){
const u_char *packet;
struct pcap_pkthdr *header;
int res;
res=3;
while(( res = pcap_next_ex( adhandle, &header, &packet)) >= 0){
if(res == 0){
//Timeout elapsed
cout <<"\nLe packet a été bien récupéré\n";
cout<<"\r\n\r\nPacket length:"<<header->len<<"\r\n";
cout<< "Received at ....."<<ctime((const time_t*)&header->ts.tv_sec)<<"\r\n";
const struct ether_header *ethernet; // The ethernet header
const struct ip_header *ip; // The IP header
const struct udp_header *udp; // The TCP header
char *payload; // Packet payload
u_int size_ip;
u_int size_udp;
ethernet = (struct ether_header*)(packet);
//Console::WriteLine("Dir Sourc: %d\n", ethernet->ether_type);
print_Eth_header(ethernet);
ip = (struct ip_header*)(packet + SIZE_ETHERNET);
//size_ip = IP_HL(ip)*4;
size_ip= ip->ihl * 4;
if (size_ip < 20)
cout<<" * Invalid IP header length: "<< size_ip<<"bytes\n";
print_ip_header(ip);
udp = (struct udp_header*)(packet + SIZE_ETHERNET + size_ip);
size_udp = (int) (udp->udp_length);
if (size_udp < 8)
cout<<" * Invalid UDP header length: "<< size_udp<<"bytes\n";
print_udp_header(udp);
payload = (char *)(packet + SIZE_ETHERNET + size_ip + size_udp);
//Traitement des données
}
if(res == -1){
cerr <<"Error reading the packets:"<<pcap_geterr(adhandle) <<"\n" ;
}
}
} |
Partager