summaryrefslogtreecommitdiff
path: root/orkbasecxx/Utils.cpp
blob: f06d6733c4540c5330bc17b68ac939014b0cfeb4 (plain)
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#include "Utils.h"
#include "ace/OS_NS_stdio.h"
#include "ace/OS_NS_arpa_inet.h"
#include "ace/OS_NS_sys_stat.h"

#ifndef WIN32
#include <pwd.h>
#include <grp.h>
#endif

//========================================================
// String related stuff

bool StringIsDigit(CStdString& string)
{
	int size = string.size();
	for(int i=0; i<size; i++)
	{
		if(isdigit(string.GetAt(i)) == false)
		{
			return false;
		}
	}
	return true;
}

bool MatchesStringList(CStdString& string, std::list<CStdString>& stringList)
{
	if(string.size() == 0)
	{
		return false;
	}
	for(std::list<CStdString>::iterator it = stringList.begin(); it != stringList.end(); it++)
	{
		CStdString element = *it;

		if(element.CompareNoCase(string) == 0)
		{
			return true;
		}
	}
	return false;
}

//========================================================
// file related stuff

CStdString FileBaseName(CStdString& path)
{
	CStdString result;
	int lastSeparatorPosition = path.ReverseFind('/');
	if(lastSeparatorPosition == -1)
	{
		lastSeparatorPosition = path.ReverseFind('\\');
	}
	if(lastSeparatorPosition != -1 && path.GetLength()>3)
	{
		result = path.Right(path.GetLength() - lastSeparatorPosition - 1);
	}
	else
	{
		result = path;
	}
	return result;
}

CStdString FilePath(CStdString& path)
{
	CStdString result;
	int lastSeparatorPosition = path.ReverseFind('/');
	if(lastSeparatorPosition == -1)
	{
		lastSeparatorPosition = path.ReverseFind('\\');
	}
	if(lastSeparatorPosition != -1 && path.GetLength()>3)
	{
		result = path.Left(lastSeparatorPosition + 1);
	}
	return result;
}

CStdString FileStripExtension(CStdString& filename)
{
	CStdString result;
	int extensionPosition = filename.ReverseFind('.');
	if (extensionPosition != -1)
	{
		result = filename.Left(extensionPosition);
	}
	else
	{
		result = filename;
	}
	return result;
}

bool FileCanOpen(CStdString& path)
{
	FILE* file = ACE_OS::fopen((PCSTR)path, "r");
	if(file)
	{
		ACE_OS::fclose(file);
		return true;
	}
	return false;
}

void FileRecursiveMkdir(CStdString& path, int permissions, CStdString owner, CStdString group, CStdString rootDirectory)
{
	int position = 0, newPermissions = permissions;
	bool done = false;

	/*
	 * Create the directories first. We have separated this because
	 * we do not want the introduction of rootDirectory to break
	 * any old functionality.
	 */
	while (!done)
	{
		position = path.Find('/', position+1);
		if (position == -1)
		{
			done = true;
		}
		else
		{
			CStdString level = path.Left(position);
			ACE_OS::mkdir((PCSTR)level);
		}
	}

	done = false;
	position = 0;
	if(rootDirectory.size())
	{
	        if(path.Find(rootDirectory) >= 0)
		{
			position = 1 + rootDirectory.size();
	        }
	}

	if(newPermissions & S_IRUSR)
	{
		newPermissions |= S_IXUSR;
	}

	if(newPermissions & S_IRGRP)
	{
		newPermissions |= S_IXGRP;
	}

	if(newPermissions & S_IROTH)
	{
		newPermissions |= S_IXOTH;
	}

	while (!done)
	{
		position = path.Find('/', position+1);
		if (position == -1)
		{
			done = true;
		}
		else
		{
			CStdString level = path.Left(position);

			if(owner.size() && group.size())
			{
				FileSetOwnership(level, owner, group);
			}

			if(newPermissions)
			{
				FileSetPermissions(level, newPermissions);
			}
		}
	}
}

int FileSetPermissions(CStdString filename, int permissions)
{
	int res = 0;

#ifndef WIN32
	res = chmod(filename.c_str(), permissions);
#endif

	return res;
}

int FileSetOwnership(CStdString filename, CStdString owner, CStdString group)
{
	int res = 0;

#ifndef WIN32
	struct group fileGroup, *fgP = NULL;
	struct passwd fileUser, *fuP = NULL;
	char infoGroupBuf[4096], infoUserBuf[4096];

	memset(infoGroupBuf, 0, sizeof(infoGroupBuf));
	memset(infoUserBuf, 0, sizeof(infoUserBuf));
	memset(&fileGroup, 0, sizeof(fileGroup));
	memset(&fileUser, 0, sizeof(fileUser));

	if(!getgrnam_r(group.c_str(), &fileGroup, infoGroupBuf, sizeof(infoGroupBuf), &fgP))
	{
		if(!getpwnam_r(owner.c_str(), &fileUser, infoUserBuf, sizeof(infoUserBuf), &fuP))
		{
			if(chown(filename.c_str(), fileUser.pw_uid, fileGroup.gr_gid))
			{
				res = -1;
			}
		}
		else
		{
			res = -1;
		}
	}
	else
	{
		res = -1;
	}
#endif

	return res;
}

static char file_ok_chars[] = "-.0123456789@ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz";
static char hex_digits[17] = "0123456789ABCDEF";

void FileEscapeName(CStdString& in, CStdString& out)
{
	// Translates all the characters that are not in file_ok_chars string into %xx sequences
	// %xx specifies the character ascii code in hexadecimal
	out = "";
	for (int i = 0 ; i<in.size() ; i++)
	{
		if (strchr(file_ok_chars, in.GetAt(i)))
		{
			out += in.GetAt(i);
		}
		else
		{
			out += '%';
			out += hex_digits[((unsigned char) in.GetAt(i)) >> 4];
			out += hex_digits[in.GetAt(i) & 0x0F];
		}
	}
}

//=====================================================
// TcpAddress

void TcpAddress::ToString(CStdString& string)
{
	char szIp[16];
	ACE_OS::inet_ntop(AF_INET, (void*)&ip, szIp, sizeof(szIp));

	string.Format("%s,%u", szIp, port);
}


void TcpAddressList::AddAddress(struct in_addr ip, unsigned short port)
{
	TcpAddress addr;
	addr.ip = ip;
	addr.port = port;
	m_addresses.push_back(addr);
}

bool TcpAddressList::HasAddress(struct in_addr ip, unsigned short port)
{
	for(std::list<TcpAddress>::iterator it = m_addresses.begin(); it != m_addresses.end(); it++)
	{
		if ((unsigned int)((*it).ip.s_addr) == (unsigned int)ip.s_addr  && (*it).port == port)
		{
			return true;
		}
	}
	return false;
}

bool TcpAddressList::HasAddressOrAdd(struct in_addr ip, unsigned short port)
{
	if(HasAddress(ip, port) == false)
	{
		AddAddress(ip, port);
		return false;
	}
	return true;
}

//=========================
// IpRanges

void IpRanges::Compute()
{
	m_ipRangePrefixes.clear();
	m_ipRangeBitWidths.clear();
	std::list<CStdString>::iterator it;

	for(it = m_asciiIpRanges.begin(); it != m_asciiIpRanges.end(); it++)
	{
		CStdString cidrPrefixLengthString;
		unsigned int cidrPrefixLength = 32;		// by default, x.x.x.x/32
		CStdString cidrIpAddressString;
		struct in_addr cidrIpAddress;
		
		CStdString entry = *it;
		int slashPosition = entry.Find('/');
		if(slashPosition > 0)
		{
			cidrIpAddressString = entry.Left(slashPosition);
			cidrPrefixLengthString = entry.Mid(slashPosition+1);

			bool notAnInt = false;
			try
			{
				cidrPrefixLength = StringToInt(cidrPrefixLengthString);
			}
			catch (...) {notAnInt = true;}
			if(cidrPrefixLength < 1 || cidrPrefixLength > 32 || notAnInt)
			{
				throw (CStdString("IpRanges: invalid CIDR prefix length" + entry));
			}
		}
		else
		{
			cidrIpAddressString = entry;
		}

		if(ACE_OS::inet_aton((PCSTR)cidrIpAddressString, &cidrIpAddress))
		{
			unsigned int rangeBitWidth = 32-cidrPrefixLength;
			unsigned int prefix = ntohl((unsigned int)cidrIpAddress.s_addr) >> (rangeBitWidth);
			m_ipRangePrefixes.push_back(prefix);
			m_ipRangeBitWidths.push_back(rangeBitWidth);
		}
		else
		{
			throw (CStdString("invalid IP range:" + entry));
		}
	}
}

bool IpRanges::Matches(struct in_addr ip)
{
	bool matches = false;
	std::list<unsigned int>::iterator bitWidthIt = m_ipRangeBitWidths.begin();
	std::list<unsigned int>::iterator prefixIt = m_ipRangePrefixes.begin();

	while(prefixIt != m_ipRangePrefixes.end())
	{
		unsigned int bitWidth = *bitWidthIt;
		unsigned int prefix = *prefixIt;
		unsigned int packetSourcePrefix = ntohl((unsigned int)ip.s_addr) >> bitWidth;
		if(packetSourcePrefix == prefix)
		{
			matches = true;
			break;
		}
		prefixIt++; 
		bitWidthIt++;
	}
	return matches;
}