This repository has been archived on 2025-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
stellar-stellar/test/packet_parser/packet_parser.cpp

57 lines
1.1 KiB
C++
Raw Normal View History

2024-05-24 16:14:20 +08:00
#include <unistd.h>
#include <pcap/pcap.h>
#include "packet_priv.h"
static void packet_handler(u_char *user, const struct pcap_pkthdr *h, const u_char *bytes)
{
struct packet pkt;
packet_parse(&pkt, (const char *)bytes, h->caplen);
packet_print_table(&pkt);
2024-05-24 16:14:20 +08:00
}
static void usage(char *cmd)
{
printf("Usage: %s\n", cmd);
printf("Options:\n");
printf(" -f <pcap file> pcap file\n");
printf(" -h print help\n");
printf("\n");
}
int main(int argc, char **argv)
{
int opt = 0;
char *file = NULL;
while ((opt = getopt(argc, argv, "f:h")) != -1)
{
switch (opt)
{
case 'f':
file = optarg;
break;
case 'h':
usage(argv[0]);
return 0;
default:
usage(argv[0]);
return -1;
}
}
if (file == NULL)
{
usage(argv[0]);
return -1;
}
pcap_t *pcap = pcap_open_offline(file, NULL);
if (pcap == NULL)
{
printf("pcap_open_offline() failed\n");
return -1;
}
pcap_loop(pcap, -1, packet_handler, NULL);
pcap_close(pcap);
return 0;
}