Repository navigation
Expand file tree
/
Copy pathnetwork.cpp
More file actions
62 lines (53 loc) · 1.43 KB
/
Copy pathnetwork.cpp
File metadata and controls
62 lines (53 loc) · 1.43 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
#include <cstdlib>
#include <cstring>
#include <map>
#include <string>
#include <arpa/inet.h>
#include <ifaddrs.h>
#include <netinet/in.h>
#include <string>
#include <string_view>
#include <unistd.h>
#include "network.h"
#include "usb.h"
void usb_rndis_start() {
usb_gadget_add_rndis();
usb_gadget_start();
usleep(500 * 1000);
system("ifconfig usb0 192.168.42.1");
system("/etc/init.d/S80dnsmasq restart");
}
void usb_ncm_start() {
usb_gadget_add_ncm();
usb_gadget_start();
usleep(500 * 1000);
system("ifconfig usb0 192.168.42.1");
system("/etc/init.d/S80dnsmasq restart");
}
void usb_net_stop() {
usb_gadget_stop();
system("/etc/init.d/S80dnsmasq stop");
usleep(500 * 1000);
}
std::map<std::string, std::string> get_ip_addr() {
struct ifaddrs *interfaces = nullptr;
struct ifaddrs *addr = nullptr;
std::map<std::string, std::string> ip_map;
int result = getifaddrs(&interfaces);
if (result == 0) {
struct ifaddrs *curr = interfaces;
while (curr) {
if (curr->ifa_addr && curr->ifa_addr->sa_family == AF_INET &&
std::strcmp(curr->ifa_name, "lo")) {
auto if_name = std::string(curr->ifa_name);
auto ip_addr =
std::string(inet_ntoa(((sockaddr_in *)curr->ifa_addr)->sin_addr));
ip_map.insert(
std::map<std::string, std::string>::value_type(if_name, ip_addr));
}
curr = curr->ifa_next;
}
freeifaddrs(interfaces);
}
return ip_map;
}