1 | |
---|
2 | #include <lsf-1.0/network.hpp> |
---|
3 | #include <lsf-1.0/common.hpp> |
---|
4 | |
---|
5 | #include <vector> |
---|
6 | #include <iostream> |
---|
7 | #include <iomanip> |
---|
8 | #include <sstream> |
---|
9 | #include <cstring> |
---|
10 | |
---|
11 | #include <sys/ioctl.h> |
---|
12 | #include <net/if.h> |
---|
13 | #include <netinet/in.h> |
---|
14 | #include <arpa/inet.h> |
---|
15 | #include <unistd.h> |
---|
16 | |
---|
17 | |
---|
18 | using namespace std; |
---|
19 | using namespace net::lliurex; |
---|
20 | |
---|
21 | /* |
---|
22 | * Based on the example code of: |
---|
23 | * Adam Pierce <adam@doctort.org> |
---|
24 | */ |
---|
25 | vector<network::DeviceInfo> network::GetDeviceList() |
---|
26 | { |
---|
27 | |
---|
28 | int sck; |
---|
29 | struct ifconf ifc; |
---|
30 | struct ifreq *ifr; |
---|
31 | int nInterfaces; |
---|
32 | char buf[1024]; |
---|
33 | |
---|
34 | vector<network::DeviceInfo> nlist; |
---|
35 | |
---|
36 | sck = socket(AF_INET, SOCK_DGRAM, 0); |
---|
37 | |
---|
38 | if(sck < 0) |
---|
39 | { |
---|
40 | throw Exception("Failed to open the socket"); |
---|
41 | } |
---|
42 | |
---|
43 | |
---|
44 | ifc.ifc_len = sizeof(buf); |
---|
45 | ifc.ifc_buf = buf; |
---|
46 | |
---|
47 | if(ioctl(sck, SIOCGIFCONF, &ifc) < 0) |
---|
48 | { |
---|
49 | throw Exception("Failed on ioctl SIOCGIFCONF"); |
---|
50 | } |
---|
51 | |
---|
52 | |
---|
53 | |
---|
54 | ifr = ifc.ifc_req; |
---|
55 | nInterfaces = ifc.ifc_len / sizeof(struct ifreq); |
---|
56 | for(int i = 0; i < nInterfaces; i++) |
---|
57 | { |
---|
58 | struct ifreq *item = &ifr[i]; |
---|
59 | struct ifreq item_flags; |
---|
60 | struct ifreq item_mac; |
---|
61 | |
---|
62 | network::DeviceInfo dev; |
---|
63 | |
---|
64 | dev.name = string(item->ifr_name); |
---|
65 | dev.address=((struct sockaddr_in *)&item->ifr_addr)->sin_addr.s_addr; |
---|
66 | |
---|
67 | strncpy(item_flags.ifr_name,item->ifr_name,sizeof(item->ifr_name)); |
---|
68 | int rv = ioctl(sck, SIOCGIFFLAGS, &item_flags); |
---|
69 | |
---|
70 | if(rv<0) |
---|
71 | { |
---|
72 | throw Exception("Failed on ioctl SIOCGIFFLAGS"); |
---|
73 | } |
---|
74 | |
---|
75 | strcpy(item_mac.ifr_name,item->ifr_name); |
---|
76 | rv = ioctl(sck, SIOCGIFHWADDR, &item_mac); |
---|
77 | |
---|
78 | if(rv<0) |
---|
79 | { |
---|
80 | throw Exception("Failed on ioctl SIOCGIFHWADDR"); |
---|
81 | } |
---|
82 | |
---|
83 | for(int n=0;n<6;n++) |
---|
84 | { |
---|
85 | dev.mac[n]=item_mac.ifr_addr.sa_data[n]; |
---|
86 | } |
---|
87 | |
---|
88 | dev.link=(item_flags.ifr_flags & IFF_UP) && (item_flags.ifr_flags & IFF_RUNNING); |
---|
89 | dev.promisc=item_flags.ifr_flags & IFF_PROMISC; |
---|
90 | |
---|
91 | nlist.push_back(dev); |
---|
92 | } |
---|
93 | |
---|
94 | |
---|
95 | |
---|
96 | close(sck); |
---|
97 | |
---|
98 | return nlist; |
---|
99 | } |
---|
100 | |
---|
101 | /** |
---|
102 | * Converts IPv4 address to string |
---|
103 | */ |
---|
104 | string network::AddressToString(unsigned long address) |
---|
105 | { |
---|
106 | stringstream ss; |
---|
107 | union |
---|
108 | { |
---|
109 | unsigned long addr; |
---|
110 | unsigned char data[4]; |
---|
111 | }span_addr; |
---|
112 | |
---|
113 | span_addr.addr=address; |
---|
114 | |
---|
115 | ss<<(int)span_addr.data[0]<<"."<<(int)span_addr.data[1]<<"."<<(int)span_addr.data[2]<<"."<<(int)span_addr.data[3]; |
---|
116 | |
---|
117 | return ss.str(); |
---|
118 | } |
---|
119 | |
---|
120 | /** |
---|
121 | * Converts MAC address to string |
---|
122 | **/ |
---|
123 | string network::MACToString(unsigned char * mac) |
---|
124 | { |
---|
125 | stringstream ss; |
---|
126 | |
---|
127 | ss<<hex<<setw(2)<<setfill('0')<<(int)mac[0]<<":"; |
---|
128 | ss<<hex<<setw(2)<<setfill('0')<<(int)mac[1]<<":"; |
---|
129 | ss<<hex<<setw(2)<<setfill('0')<<(int)mac[2]<<":"; |
---|
130 | ss<<hex<<setw(2)<<setfill('0')<<(int)mac[3]<<":"; |
---|
131 | ss<<hex<<setw(2)<<setfill('0')<<(int)mac[4]<<":"; |
---|
132 | ss<<hex<<setw(2)<<setfill('0')<<(int)mac[5]; |
---|
133 | |
---|
134 | return ss.str(); |
---|
135 | } |
---|