added -l to list all interfaces

This commit is contained in:
2025-03-25 06:42:58 +01:00
parent 4356a49271
commit 39a8b7c016
3 changed files with 40 additions and 8 deletions

View File

@@ -18,6 +18,7 @@ pcapmirror [options]
* -p <port> Specify the destination port (default: 37008) * -p <port> Specify the destination port (default: 37008)
* -4 Force IPv4 host lookup * -4 Force IPv4 host lookup
* -6 Force IPv6 host lookup * -6 Force IPv6 host lookup
* -l List available network interfaces.
* -v Enable verbose mode * -v Enable verbose mode
* -h Show this help message * -h Show this help message

36
main.c
View File

@@ -41,6 +41,25 @@ int is_little_endian() {
return (((unsigned char*)&i)[0] == 0x67); return (((unsigned char*)&i)[0] == 0x67);
} }
void list_interfaces() {
pcap_if_t *alldevs;
char errbuf[PCAP_ERRBUF_SIZE];
if (pcap_findalldevs(&alldevs, errbuf) == -1) {
fprintf(stderr, "Error in pcap_findalldevs: %s\n", errbuf);
return;
}
printf("Available network interfaces:\n");
for (pcap_if_t *d = alldevs; d != NULL; d = d->next) {
printf("%s", d->name);
if (d->description) {
printf(" (%s)", d->description);
}
printf("\n");
}
pcap_freealldevs(alldevs);
}
void print_usage(const char *program_name) { void print_usage(const char *program_name) {
printf("Usage: %s [options]\n", program_name); printf("Usage: %s [options]\n", program_name);
printf("Options:\n"); printf("Options:\n");
@@ -50,6 +69,7 @@ void print_usage(const char *program_name) {
printf(" -p <port> Specify the destination port (default: %d)\n", DEFAULT_DEST_PORT); printf(" -p <port> Specify the destination port (default: %d)\n", DEFAULT_DEST_PORT);
printf(" -4 Force IPv4 host lookup\n"); printf(" -4 Force IPv4 host lookup\n");
printf(" -6 Force IPv6 host lookup\n"); printf(" -6 Force IPv6 host lookup\n");
printf(" -l List available network interfaces\n");
printf(" -v Enable verbose mode\n"); printf(" -v Enable verbose mode\n");
printf(" -h Show this help message\n"); printf(" -h Show this help message\n");
printf("Example:\n"); printf("Example:\n");
@@ -67,6 +87,7 @@ int main(int argc, char *argv[]) {
int verbose = 0; // Verbose flag, default is false int verbose = 0; // Verbose flag, default is false
int force_ipv4 = 0; // Flag to force IPv4 lookup int force_ipv4 = 0; // Flag to force IPv4 lookup
int force_ipv6 = 0; // Flag to force IPv6 lookup int force_ipv6 = 0; // Flag to force IPv6 lookup
int list_interfaces_flag = 0; // Flag to list interfaces
// Socket variables // Socket variables
int sockfd; int sockfd;
@@ -102,11 +123,18 @@ int main(int argc, char *argv[]) {
force_ipv4 = 1; // Force IPv4 lookup force_ipv4 = 1; // Force IPv4 lookup
} else if (strcmp(argv[i], "-6") == 0) { } else if (strcmp(argv[i], "-6") == 0) {
force_ipv6 = 1; // Force IPv6 lookup force_ipv6 = 1; // Force IPv6 lookup
} else if (strcmp(argv[i], "-l") == 0) {
list_interfaces_flag = 1; // Set flag to list interfaces
} }
} }
if (list_interfaces_flag) {
list_interfaces();
return 0;
}
// Check if destination IP is provided // Check if destination IP is provided
if (mirror_host == NULL) { if (mirror_host == NULL && !list_interfaces_flag) {
fprintf(stderr, "Error: Destination IP address is required.\n"); fprintf(stderr, "Error: Destination IP address is required.\n");
print_usage(argv[0]); print_usage(argv[0]);
return 1; return 1;
@@ -172,14 +200,14 @@ int main(int argc, char *argv[]) {
return(1); return(1);
} }
// Print the available devices for debugging
/* if (list_interfaces_flag) {
pcap_if_t *device; pcap_if_t *device;
printf("Available devices:\n"); printf("Available devices:\n");
for (device = alldevs; device != NULL; device = device->next) { for (device = alldevs; device != NULL; device = device->next) {
printf("%s - %s\n", device->name, (device->description != NULL) ? device->description : "No description available"); printf("%s - %s\n", device->name, (device->description != NULL) ? device->description : "No description available");
} }
*/ }
// Use the first device if no device is specified // Use the first device if no device is specified
if (alldevs == NULL) { if (alldevs == NULL) {

View File

@@ -30,6 +30,9 @@ Force IPv4 host lookup.
.B \-6 .B \-6
Force IPv6 host lookup. Force IPv6 host lookup.
.TP .TP
.B \-l
List available network interfaces.
.TP
.B \-v .B \-v
Enable verbose mode (prints packet information). Enable verbose mode (prints packet information).
.TP .TP