Skip to content

Commit 1382569

Browse files
committed
net info
1 parent d54f35f commit 1382569

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

main

16.3 KB
Binary file not shown.

src/main.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "cpu/cpu_info.h"
55
#include "rom/rom_info.h"
66
#include "hlp/hlp_info.h"
7+
#include "net/net_info.h"
78

89
int main(int argc, char **argv) {
910
if(argc == 1) {
@@ -27,6 +28,13 @@ int main(int argc, char **argv) {
2728
print_ram_info();
2829
} else if(strcmp(argv[1], "-rom") == 0) {
2930
print_rom_info();
31+
} else if(strcmp(argv[1], "-net") == 0) {
32+
char host_name[100];
33+
printf("Enter a hostname: ");
34+
fgets(host_name, sizeof(host_name), stdin);
35+
host_name[strcspn(host_name, "\n")] = '\0';
36+
37+
print_net_info(host_name);
3038
} else if(strcmp(argv[1], "-v") == 0 || strcmp(argv[1], "--version") == 0) {
3139
print_ver_info();
3240
} else if(strcmp(argv[1], "-b") == 0 || strcmp(argv[1], "--banner") == 0) {

src/net/net_info.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include <ifaddrs.h>
2+
#include <stdio.h>
3+
#include <stdlib.h>
4+
#include <sys/socket.h>
5+
#include <netdb.h>
6+
#include <arpa/inet.h>
7+
8+
void print_net_info(char *hostname){
9+
struct hostent *host = gethostbyname(hostname);
10+
if(host == NULL){
11+
perror("gethostbyname");
12+
exit(1);
13+
} else {
14+
printf("Host Name: %s\n", host->h_name);
15+
printf("IP Address: ");
16+
for(int i = 0; host->h_addr_list[i] != NULL; i++){
17+
printf("%s ", inet_ntoa(*(struct in_addr *)host->h_addr_list[i]));
18+
}
19+
printf("\n");
20+
}
21+
22+
struct ifaddrs *ifaddr, *ifa;
23+
if(getifaddrs(&ifaddr) == -1) {
24+
perror("getifaddrs error");
25+
exit(1);
26+
}
27+
for(ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next){
28+
if(ifa->ifa_addr != NULL && ifa->ifa_addr->sa_family == AF_INET){
29+
printf("Interface: %s\n", ifa->ifa_name);
30+
printf("Address: %s\n", inet_ntoa(((struct sockaddr_in*)ifa->ifa_addr)->sin_addr));
31+
printf("Netmask: %s\n", inet_ntoa(((struct sockaddr_in*)ifa->ifa_netmask)->sin_addr));
32+
}
33+
}
34+
35+
freeifaddrs(ifaddr);
36+
}

0 commit comments

Comments
 (0)