-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.c
220 lines (181 loc) · 5.71 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <arpa/inet.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <errno.h>
#include <ctype.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netdb.h>
#include <ifaddrs.h>
#include <linux/if_link.h>
#include "utils.h"
// s_sleep using select instead of sleep
// s: second, u: usec 10^6usec = 1s
//使用select来搞定sleep,精确度高
void s_sleep(unsigned int s, unsigned int u)
{
struct timeval timeout;
timeout.tv_sec = s;
timeout.tv_usec = u;
select(0, NULL, NULL, NULL, &timeout);
}
// is_valid_ip_address:
// return 0:ipaddress unlegal
//验证ip地址字符串是否有效,inet_pton()
int is_valid_ip_address(const char *ip_address)
{
struct sockaddr_in sa;
int result = inet_pton(AF_INET, ip_address, &(sa.sin_addr));
return result;
}
// net_if_name: name of network interface, e.g. br-lan
// return: 1: error 0:get succeed
//直接调用SIOCGETIFHWADDR来获取mac地址
int get_net_mac(char *net_if_name, char *mac, int mac_len)
{
int ret = 1;
int i = 0;
int sock = 0;
if (mac_len < 12 || net_if_name == NULL) {
return 1;
}
struct ifreq ifreq;
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) {
perror("error sock");
goto OUT;
}
strncpy(ifreq.ifr_name, net_if_name, IFNAMSIZ);
if (ioctl(sock, SIOCGIFHWADDR, &ifreq) < 0) {
perror("error ioctl");
goto OUT;
}
for (i = 0; i < 6; i++) {
snprintf(mac + 2 * i, mac_len - 2 * i, "%02X", (unsigned char) ifreq.ifr_hwaddr.sa_data[i]);
}
mac[strlen(mac)] = 0;
ret = 0;
OUT:
close(sock);
return ret;
}
// return: -1: network interface check failed; other: ifname numbers
// ifconfig的实现???
int show_net_ifname()
{
struct ifaddrs *ifaddr, *ifa;
int family, s, n;
char host[NI_MAXHOST];
if (getifaddrs(&ifaddr) == -1) {
perror("getifaddrs");
exit(EXIT_FAILURE);
}
/* Walk through linked list, maintaining head pointer so we
can free list later */
for (ifa = ifaddr, n = 0; ifa != NULL; ifa = ifa->ifa_next, n++) {
if (ifa->ifa_addr == NULL)
continue;
family = ifa->ifa_addr->sa_family;
/* Display interface name and family (including symbolic
form of the latter for the common families) */
printf("%-8s %s (%d)\n", ifa->ifa_name,
(family == AF_PACKET)
? "AF_PACKET"
: (family == AF_INET) ? "AF_INET" : (family == AF_INET6) ? "AF_INET6" : "???",
family);
/* For an AF_INET* interface address, display the address */
if (family == AF_INET || family == AF_INET6) {
s = getnameinfo(
ifa->ifa_addr,
(family == AF_INET) ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6),
host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
if (s != 0) {
printf("getnameinfo() failed: %s\n", gai_strerror(s));
exit(EXIT_FAILURE);
}
printf("\t\taddress: <%s>\n", host);
} else if (family == AF_PACKET && ifa->ifa_data != NULL) {
struct rtnl_link_stats *stats = (struct rtnl_link_stats *) ifa->ifa_data;
printf(
"\t\ttx_packets = %10u; rx_packets = %10u\n"
"\t\ttx_bytes = %10u; rx_bytes = %10u\n",
stats->tx_packets, stats->rx_packets, stats->tx_bytes, stats->rx_bytes);
}
}
freeifaddrs(ifaddr);
return 0;
}
// return: 0: network interface get succeed
int get_net_ifname(char *if_buf, int blen)
{
if (NULL == if_buf || blen < 8)
return -1;
struct ifaddrs *ifaddr, *ifa;
int family, n;
int ret = 1;
//读取ifaddr链首
if (getifaddrs(&ifaddr) == -1) {
perror("getifaddrs");
exit(EXIT_FAILURE);
}
int found = 0;
char tmp_if_buf[16];
memset(tmp_if_buf, 0, sizeof(tmp_if_buf));
/* Walk through linked list, maintaining head pointer so we
can free list later */
for (ifa = ifaddr, n = 0; ifa != NULL; ifa = ifa->ifa_next, n++) {
if (ifa->ifa_addr == NULL)
continue;
family = ifa->ifa_addr->sa_family;
if (family == AF_INET) {
// for LEDE/OpenWRT embedded router os
//取br-lan接口
if (strcmp(ifa->ifa_name, "br-lan") == 0) {
found = 1;
break;
}
} else if (family == AF_PACKET && ifa->ifa_data != NULL &&
strcmp(ifa->ifa_name, "lo") != 0) { // skip local loop interface
//取最后一个非lo接口
strncpy(tmp_if_buf, ifa->ifa_name, 16);
}
}
if (found) {
strncpy(if_buf, ifa->ifa_name, blen);
ret = 0;
} else if (tmp_if_buf[0] != 0) {
strncpy(if_buf, tmp_if_buf, blen);
ret = 0;
}
//释放
freeifaddrs(ifaddr);
return ret;
}
// e.g. wWw.Baidu.com/China will be trans into www.baidu.com/China
// return: 0:check and trant succeed, 1:failed or domain name is invalid
// dns格式化
int dns_unified(const char *dname, char *udname_buf, int udname_buf_len)
{
if (!dname || !udname_buf || udname_buf_len < strlen(dname) + 1)
return 1;
int has_dot = 0;
int dlen = strlen(dname);
int i = 0;
for (i = 0; i < dlen; i++) {
if (dname[i] == '/')
break;
if (dname[i] == '.' && i != dlen - 1)
has_dot = 1;
udname_buf[i] = tolower(dname[i]);
}
if (!has_dot) // domain name should have 1 dot leastly
return 1;
return 0;
}