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
|
/*
* tools.cpp
*
* Created on: 05.08.2012
* Author: savop
*/
#include "../include/tools.h"
#include <sstream>
#include <net/if.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <boost/uuid/uuid_generators.hpp>
#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_io.hpp>
using namespace std;
namespace upnp {
namespace tools {
string GetAddressByInterface(string Interface){
string address;
if(!Interface.empty()){
int fd;
struct ifreq ifr;
fd = socket(AF_INET, SOCK_DGRAM, 0);
/* I want to get an IPv4 IP address */
ifr.ifr_addr.sa_family = AF_INET;
/* I want IP address attached to "eth0" */
strncpy(ifr.ifr_name, Interface.c_str(), IFNAMSIZ-1);
int ret = ioctl(fd, SIOCGIFADDR, &ifr);
close(fd);
if(ret==0){
sockaddr_in* inAddr = (sockaddr_in*)&ifr.ifr_addr;
address = inet_ntoa(inAddr->sin_addr);
}
}
return address;
}
string GetNetworkInterfaceByIndex(int Index, bool skipLoop){
return GetNetworkInterfaces(skipLoop)[Index];
}
StringVector GetNetworkInterfaces(bool skipLoop){
StringVector interfaces;
int fd;
struct ifconf ifc;
struct ifreq ifr[10];
int nifaces, i;
memset(&ifc,0,sizeof(ifc));
ifc.ifc_buf = (char*) (ifr);
ifc.ifc_len = sizeof(ifr);
fd = socket(AF_INET, SOCK_DGRAM, 0);
int ret = ioctl(fd, SIOCGIFCONF, &ifc);
close(fd);
if(ret==0){
nifaces = ifc.ifc_len/sizeof(struct ifreq);
for(i = 0; i < nifaces; i++){
if(skipLoop && strcmp("lo", ifr[i].ifr_name)==0)
continue;
else
interfaces.push_back(ifr[i].ifr_name);
}
}
return interfaces;
}
string ToString(long number){
stringstream ss;
ss << number;
return ss.str();
}
string StringListToCSV(StringList list){
stringstream ss;
if(list.empty()) return string();
StringList::iterator it = list.begin();
ss << (*it);
for(++it; it != list.end(); ++it){
ss << "," << (*it);
}
return ss.str();
}
string IdListToCSV(IdList list){
stringstream ss;
if(list.empty()) return string();
IdList::iterator it = list.begin();
ss << (*it).first << "," << (*it).second;
for(++it; it != list.end(); ++it){
ss << "," << (*it).first << "," << (*it).second;
}
return ss.str();
}
string GenerateUUIDFromURL(string url){
boost::uuids::string_generator gen;
boost::uuids::uuid urlNamespace = gen(L"6ba7b811-9dad-11d1-80b4-00c04fd430c8");
stringstream uuid;
uuid << boost::uuids::name_generator(urlNamespace)(url);
return uuid.str();
}
string GenerateUUIDRandomly(){
stringstream uuid;
uuid << boost::uuids::random_generator()();
return uuid.str();
}
void StringExplode(string str, string separator, StringVector& results) {
string::size_type found;
found = str.find_first_of(separator);
while(found != string::npos){
if(found > 0){
results.push_back(str.substr(0,found));
}
str = str.substr(found+1);
found = str.find_first_of(separator);
}
if(str.length() > 0){
results.push_back(str);
}
}
} // namespace tools
} // namespace upnp
|