Portál AbcLinuxu, 10. května 2025 22:34
FILE* soubor; /* * dissect/print packet */ void got_packet(u_char *args, const struct pcap_pkthdr *header, const u_char *packet) { cout << "Zachycen packet o velikosti:" << fwrite(packet, 1, header->len, soubor) << endl; return; } int main(int argc, char **argv) { argc = 2; argv[1] = "ath0"; soubor = fopen("muj.cap", "w"); char *dev = NULL; /* capture device name */ char errbuf[PCAP_ERRBUF_SIZE]; /* error buffer */ pcap_t *handle; /* packet capture handle */ bpf_u_int32 mask; /* subnet mask */ bpf_u_int32 net; /* ip */ int num_packets = 1; /* number of packets to capture */ switch ( argc ) { case 1 : cout << "Musis zvolit agrumenty." << endl; exit(EXIT_FAILURE); break; case 2 : if(argc == 2) dev = argv[1]; break; default : cout << "Spatne zadane argumenty." << endl; exit(EXIT_FAILURE); break; } /* get network number and mask associated with capture device */ if (pcap_lookupnet(dev, &net, &mask, errbuf) == -1) { cout << "ERROR: Nemuzu ziskat masku pro. " << dev << endl; cout << "ERROR: " << errbuf << endl; net = 0; mask = 0; } /* Tisk infomace o zachytavani */ cout << "Adapter: " << dev << endl; cout << "Pocet packetu: " << num_packets << endl; /* Otevri zachytavani na adapteru */ handle = pcap_open_live(dev, SNAP_LEN, 1, 1000, errbuf); if (handle == NULL) { cout << "ERROR: Nemuzu otevrit adapter " << dev << endl; cout << "ERROR: " << errbuf << endl; exit(EXIT_FAILURE); } /* Volani funkce po zachyceni jednoho packetu */ pcap_loop(handle, num_packets, got_packet, NULL); /* Cisteni */ pcap_close(handle); cout << endl << "Zachytavani kompletni." << endl; fclose(soubor); return 0; }
Tiskni
Sdílej:
ISSN 1214-1267, (c) 1999-2007 Stickfish s.r.o.