-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterface.c
More file actions
74 lines (63 loc) · 2.16 KB
/
Copy pathinterface.c
File metadata and controls
74 lines (63 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
* This file is part of eISCP Proxy, which is licensed under the
* GNU General Public License v3.0. You can find the full license text
* in the LICENSE file at the root of the source tree or at
* https://www.gnu.org/licenses/gpl-3.0.txt.
*/
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <net/if.h>
#include "types.h"
#include "interface.h"
void setInterfaceIPAddress(InterfaceNode* node) {
int fd;
struct ifreq ifr;
char ip[INET_ADDRSTRLEN];
// Open a dummy socket to perform the ioctl
if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
perror("socket");
return;
}
// Prepare the interface request structure
memset(&ifr, 0, sizeof(ifr));
ifr.ifr_addr.sa_family = AF_INET; // IPv4 address
strncpy(ifr.ifr_name, node->name, IFNAMSIZ-1);
// Retrieve the interface address
if (ioctl(fd, SIOCGIFADDR, &ifr) < 0) {
perror(node->name);
close(fd);
fprintf(stderr, "Error: Interface %s does not exist on this system or does not have an IP address assigned.\n", node->name);
exit(EXIT_FAILURE);
}
close(fd);
// Copy the address to the node's sockaddr_in structure
memcpy(&node->address, (struct sockaddr_in *)&ifr.ifr_addr, sizeof(struct sockaddr_in));
// Convert the IP address to string and store it in the node
inet_ntop(AF_INET, &((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr, ip, INET_ADDRSTRLEN);
node->ipAddress = strdup(ip);
}
void enrichInterfaceNodes(InterfaceNode* node) {
for (InterfaceNode* current = node; current != NULL; current = current->next) {
setInterfaceIPAddress(current);
}
}
void freeInterfaceList(InterfaceNode* node) {
InterfaceNode* current = node;
while (current) {
InterfaceNode* next = current->next;
free(current->name);
free(current);
current = next;
}
}
void dumpInterfaceList(InterfaceNode* node) {
for (InterfaceNode* current = node; current != NULL; current = current->next) {
fprintf(stderr, "Interface %s (%s)\n", current->name, current->ipAddress);
}
}