-
Notifications
You must be signed in to change notification settings - Fork 4
/
utils.c
135 lines (107 loc) · 2.35 KB
/
utils.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
// Copyright (c) 2020 Microchip Technology Inc. and its subsidiaries.
// SPDX-License-Identifier: (GPL-2.0)
#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
#include <getopt.h>
#include <netlink/genl/genl.h>
#include <netlink/genl/ctrl.h>
#include <linux/types.h>
#include <net/if.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <stdbool.h>
#include <linux/if_ether.h>
#include "utils.h"
static int netsock = 0;
int if_get_mac(int ifindex, unsigned char *mac)
{
char name[IF_NAMESIZE];
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
if (!if_indextoname(ifindex, name))
return 0;
strncpy(ifr.ifr_name, name, IFNAMSIZ - 1);
if(ioctl(netsock, SIOCGIFHWADDR, &ifr))
return 0;
memcpy(mac, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
return 0;
}
int if_get_link(int ifindex)
{
char name[IF_NAMESIZE];
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
if (!if_indextoname(ifindex, name))
return 0;
strncpy(ifr.ifr_name, name, IFNAMSIZ - 1);
if(ioctl(netsock, SIOCGIFFLAGS, &ifr))
return 0;
return ifr.ifr_flags & IFF_RUNNING;
}
int if_init(void)
{
netsock = socket(AF_INET, SOCK_DGRAM, 0);
if (!netsock) {
printf("netsock was not open!\n");
return -1;
}
return 0;
}
struct frame_buf *fb_alloc(uint32_t size)
{
struct frame_buf *fb;
fb = malloc(sizeof(*fb));
if (!fb)
return NULL;
fb->start = malloc(size);
if (!fb->start) {
free(fb);
return NULL;
}
memset(fb->start, 0x0, size);
fb->data = fb->start;
fb->size = 0;
return fb;
};
void *fb_put(struct frame_buf *fb, uint32_t size) {
void *res = fb->data;
fb->data += size;
fb->size += size;
return res;
}
void ether_addr_copy(uint8_t *dst, const uint8_t *src)
{
uint16_t *a = (uint16_t *)dst;
const uint16_t *b = (const uint16_t *)src;
a[0] = b[0];
a[1] = b[1];
a[2] = b[2];
}
bool ether_addr_equal(const uint8_t *addr1, const uint8_t *addr2)
{
const uint16_t *a = (const uint16_t *)addr1;
const uint16_t *b = (const uint16_t *)addr2;
return ((a[0] ^ b[0]) | (a[1] ^ b[1]) | (a[2] ^ b[2])) == 0;
}
uint64_t ether_addr_to_u64(const uint8_t *addr)
{
uint64_t u = 0;
int i;
for (i = 0; i < ETH_ALEN; i++)
u = u << 8 | addr[i];
return u;
}
uint32_t get_ms(void)
{
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec * 1000 + tv.tv_usec / 1000;
}
void if_cleanup(void)
{
close(netsock);
}