File: utils.hh

package info (click to toggle)
pdns 2.9.20-8
  • links: PTS
  • area: main
  • in suites: etch-m68k
  • size: 4,640 kB
  • ctags: 3,511
  • sloc: cpp: 24,567; sh: 17,754; makefile: 360; yacc: 189; lex: 131; sql: 69; perl: 39
file content (134 lines) | stat: -rw-r--r-- 1,877 bytes parent folder | download | duplicates (2)
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
#include <string>
#include <vector>
#include <pdns/misc.hh>


#ifndef LDAPBACKEND_UTILS_HH
#define LDAPBACKEND_UTILS_HH

using std::string;
using std::vector;


inline string ptr2ip4( vector<string>& parts )
{
	string ip;
	parts.pop_back();
	parts.pop_back();


	ip = parts.back();
	parts.pop_back();

	while( !parts.empty() )
	{
		ip += "." + parts.back();
		parts.pop_back();
	}

	return ip;
}


inline string ptr2ip6( vector<string>& parts )
{
	int i = 0;
	string ip;


	parts.pop_back();
	parts.pop_back();

	while( i < 3 && parts.size() > 1 && parts.back() == "0" )
	{
		parts.pop_back();
		i++;
	}

	while( i++ < 4 && !parts.empty() )
	{
		ip += parts.back();
		parts.pop_back();
	}

	while( !parts.empty() )
	{
		i = 0;
		ip += ":";

		while( i < 3 && parts.size() > 1 && parts.back() == "0" )
		{
			parts.pop_back();
			i++;
		}

		while( i++ < 4 && !parts.empty() )
		{
			ip += parts.back();
			parts.pop_back();
		}
	}

	return ip;
}


inline string ip2ptr4( const string& ip )
{
	string ptr;
	vector<string> parts;

	stringtok( parts, ip, "." );
	while( !parts.empty() )
	{
		ptr += parts.back() +  ".";
		parts.pop_back();
	}

	return ptr + "in-addr.arpa";
}


inline string ip2ptr6( const string& ip )
{
	string ptr, part, defstr;
	vector<string> parts;

	stringtok( parts, ip, ":" );
	while( !parts.empty() )
	{
		defstr = "0.0.0.0.";
		part = parts.back();

		while( part.length() < 4 )
		{
			part = "0" + part;
		}

		defstr[0] = part[3];
		defstr[2] = part[2];
		defstr[4] = part[1];
		defstr[6] = part[0];
		ptr += defstr;
		parts.pop_back();
	}

	return ptr + "ip6.arpa";
}


inline string strbind( const string& search, const string& replace, string subject )
{
	size_t pos = 0;


	while( ( pos = subject.find( search, pos ) ) != string::npos )
	{
		subject.replace( pos, search.size(), replace );
		pos += replace.size();
	}

	return subject;
}

#endif