Created
March 25, 2019 00:20
-
-
Save gmelodie/4cba93f2735bb8159375a5a45b9976cf to your computer and use it in GitHub Desktop.
Network interface scanner
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <stdio.h> | |
#include <sys/types.h> | |
#include <sys/socket.h> | |
#include <linux/if_packet.h> | |
#include <net/ethernet.h> | |
#include <netdb.h> | |
#include <ifaddrs.h> | |
#include <arpa/inet.h> | |
#include <errno.h> | |
#include <string.h> | |
void print_addr(struct sockaddr *addr) { | |
char *addr_string = malloc(sizeof(char) * 50); | |
struct sockaddr_in *addr_in = (struct sockaddr_in *) addr; | |
struct sockaddr_in6 *addr_in6 = (struct sockaddr_in6 *) addr; | |
struct sockaddr_ll *addr_ll = (struct sockaddr_ll *) addr; | |
int len = 0; | |
// Casting for each address family (only for most common ones | |
switch(addr->sa_family) { | |
// IPv4 | |
case AF_INET: | |
inet_ntop(addr_in->sin_family, | |
&addr_in->sin_addr, addr_string, 50); | |
break; | |
// IPv6 | |
case AF_INET6: | |
inet_ntop(addr_in6->sin6_family, | |
&addr_in6->sin6_addr, addr_string, 50); | |
break; | |
// MAC address | |
case AF_PACKET: | |
len = 0; | |
for(int i = 0; i < 6; i++) | |
len += sprintf(addr_string + len, "%02X%s", | |
addr_ll->sll_addr[i], i < 5 ? ":":""); | |
break; | |
default: | |
printf("Address family not supported\n"); | |
return; | |
} | |
if(addr_string == NULL) { | |
printf("Couldn't find addresses related\n"); | |
} else { | |
printf("%s\n", addr_string); | |
} | |
free(addr_string); | |
} | |
void print_iface(int niface, struct ifaddrs *nets_head){ | |
struct ifaddrs *next; | |
// Iterate until selected structure | |
next = nets_head; | |
for(int i = 0; i < niface && next != NULL; i++) | |
next = next->ifa_next; | |
if(next == NULL) { | |
printf("Couldn't find interface\n"); | |
return; | |
} | |
printf("=========================================================\n"); | |
printf("Name: %s\n", next->ifa_name); | |
printf("Address: "); | |
print_addr(next->ifa_addr); | |
if(next->ifa_addr->sa_family == AF_INET || | |
next->ifa_addr->sa_family == AF_INET6) { | |
printf("Netmask: "); | |
print_addr(next->ifa_netmask); | |
} | |
printf("=========================================================\n"); | |
} | |
int main(int argc, char *argv[]) { | |
int ifchoice; | |
struct ifaddrs *nets_head = malloc(sizeof(struct ifaddrs)); | |
struct ifaddrs *next; // aux variable to iterate through list | |
getifaddrs(&nets_head); | |
// List all net structures | |
next = nets_head; | |
for(int i = 0; next != NULL; i++) { | |
printf("%d: %s\n", i, next->ifa_name); | |
next = next->ifa_next; | |
} | |
// Choose structure to show details | |
scanf("%d", &ifchoice); | |
print_iface(ifchoice, nets_head); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment