File: utils.hh

package info (click to toggle)
pdns 3.4.1-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 9,084 kB
  • sloc: cpp: 56,420; ansic: 37,838; sh: 12,762; sql: 1,081; makefile: 734; ruby: 560; yacc: 222; lex: 120; perl: 52; python: 15
file content (156 lines) | stat: -rw-r--r-- 2,830 bytes parent folder | download | duplicates (7)
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
#include <string>
#include <vector>
#include <time.h>
#include <stdlib.h>
#include <pdns/misc.hh>
#include <pdns/utility.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;
}

/*
 *  Convert a LDAP time string to a time_t. Return 0 if unable to convert
 */

inline time_t str2tstamp( const string& str )
{
        char* tmp;
        struct tm tm;

        tmp =  strptime( str.c_str(), "%Y%m%d%H%M%SZ", &tm );

        if( tmp != NULL && *tmp == 0 )
        {
        	return Utility::timegm( &tm );
        }

        return 0;
}

#endif