Specify interface on the command line

This commit is contained in:
Juhani Krekelä 2019-07-03 23:09:14 +03:00
parent 4ce7f9c309
commit 3f571bdb27
1 changed files with 8 additions and 2 deletions

View File

@ -7,6 +7,7 @@
#include <net/if.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
@ -28,7 +29,12 @@ void format_mac(const unsigned char binary_address[6], char formatted[18]) {
formatted[17] = '\0';
}
int main(void) {
int main(int argc, char **argv) {
if (argc != 2) {
fprintf(stderr, "Usage: %s interface\n", argv[0]);
exit(1);
}
// Create a packet socket
errno = 0;
int packet_socket = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
@ -38,7 +44,7 @@ int main(void) {
// Find the index of the network interface
struct ifreq ifr;
strncpy(ifr.ifr_name, "wlan0", IFNAMSIZ);
strncpy(ifr.ifr_name, argv[1], IFNAMSIZ);
errno = 0;
if (ioctl(packet_socket, SIOCGIFINDEX, &ifr) == -1) {
err(1, "ioctl");