-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathifnetdevs_manip.c
136 lines (106 loc) · 3.76 KB
/
ifnetdevs_manip.c
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <pcap/pcap.h>
#include <arpa/inet.h>
#include "ifnetdevs_manip.h"
bpf_u_int32 netip, subnetip ;
char error_buffer[PCAP_ERRBUF_SIZE] ={0} ;
struct __active_inet_devices *
fetch_active_netdev_interface(pcap_if_t * raw_netdevs , active_inet_devices * indevs)
{
int incoming_if_status_check = 0;
while (raw_netdevs != NULL){
incoming_if_status_check = raw_netdevs->flags & PCAP_IF_CONNECTION_STATUS ;
if ( incoming_if_status_check == PCAP_IF_CONNECTION_STATUS_CONNECTED) {
struct __active_inet_devices * new_indev = (struct __active_inet_devices *)\
malloc(sizeof(*new_indev)) ;
if (!new_indev)
return NULL ;
memcpy(new_indev->idevname , raw_netdevs->name , strlen(raw_netdevs->name));
char *ipv4addr = get_interface_ipv4(raw_netdevs->addresses) ;
if (ipv4addr!= nullable)
memcpy(new_indev->ipv4_address , ipv4addr , strlen(ipv4addr)) ;
printf("ip address %s\n" , new_indev->ipv4_address) ;
indevs = register_inetdev(indevs , new_indev) ;
}
raw_netdevs = raw_netdevs->next ;
}
return indevs ;
}
static char * get_interface_ipv4 ( struct pcap_addr * addr_resolution )
{
while (addr_resolution != nullable) {
struct sockaddr *sa = addr_resolution->addr ;
if (sa->sa_family == PF_INET) {
return inet_ntoa( ((struct sockaddr_in *)sa)->sin_addr);
}
addr_resolution = addr_resolution->next ;
}
return nullable;
}
static struct __active_inet_devices *
register_inetdev(active_inet_devices * idevs_nodelist , active_inet_devices * new_idev_node)
{
/* *[idevs]-> NULL
* [new_idev_node]
* |
* v
* [new_idev_node] -> [*idevs] -> NULL
* | |
* ----------v |
* -----------)--------
* | ------|
* v v
* *[new_idev_node] -> [idevs] -> NULL
*
*/
struct __active_inet_devices * hold_head = idevs_nodelist ;
idevs_nodelist = new_idev_node ;
idevs_nodelist->next = hold_head ;
return idevs_nodelist ;
}
int list_active_interface(const active_inet_devices *idevs_nodelist , show_info_t show)
{
struct __active_inet_devices * node = (struct __active_inet_devices*) idevs_nodelist ;
int if_items = 0 ;
while(node != NULL ) {
if (show)
show(node) ;
node = node->next ;
if_items++ ;
}
return if_items ;
}
char * get_interface(const active_inet_devices * idevs_nodelist , int index)
{
int n_interfaces = list_active_interface(idevs_nodelist , (void * ) 0) ;
int i = abs(n_interfaces - index) ;
int j = 1 ;
struct __active_inet_devices * node = (struct __active_inet_devices *) idevs_nodelist ;
while (node != NULL) {
if (j == i ) break ;
node= node->next ;
j++ ;
}
return strdup(node->idevname) ;
}
struct __idev_info_t *
get_interface_info(const char * interface_name)
{
inet_device_info * devinfo = (inet_device_info *) malloc(sizeof(*devinfo)) ;
if (!devinfo) return NULL ;
if (PCAP_ERROR == pcap_lookupnet(interface_name , &netip , &subnetip , error_buffer)){
free(devinfo) ;
return NULL;
}
int subnetclass = SUBN_MASK & subnetip ;
struct in_addr address[2] ;
address[0].s_addr = netip;
address[1].s_addr = subnetip ;
memcpy(devinfo->idevname , interface_name , strlen(interface_name ));
memcpy(devinfo->ipv4netnum , inet_ntoa(*(address+0)) , NET_IPV4_LENGHT) ;
memcpy(devinfo->subnet_mask , inet_ntoa(* (address +1)) , SUBNETMASK_BUFFER) ;
devinfo->class_type = _GSTRCLS(subnetclass) ;
return devinfo ;
}