File: iprange.cpp

package info (click to toggle)
guidedog 1.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 372 kB
  • ctags: 161
  • sloc: cpp: 2,118; sh: 84; makefile: 14
file content (195 lines) | stat: -rw-r--r-- 5,476 bytes parent folder | download
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
/***************************************************************************
                          iprange.cpp  -  description
                             -------------------
    begin                : Thu May 10 08:08:00 EST 2001
    copyright            : (C) 2000-2001 by Simon Edwards
    email                : simon@simonzone.com
 ***************************************************************************/
/***************************************************************************
    Converted to a pure Qt5 application
    begin                : December 2014
    by                   : Antonio Martins
    email                : digiplan.pt@gmail.com
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "iprange.h"

#include <QRegExp>


/*!
 * \brief IPRange::IPRange
 */
IPRange::IPRange() {
    gottype = false;
}


/*!
 * \brief IPRange::IPRange
 * \param a
 */
IPRange::IPRange(const QString &a) {
    setAddress(a);
}


/*!
 * \brief IPRange::~IPRange
 */
IPRange::~IPRange() {
}


/*!
 * \brief IPRange::setAddress
 * \param a
 */
void IPRange::setAddress(const QString &a) {
    address = a;
    gottype = false;
}


/*!
 * \brief IPRange::getAddress
 * \return
 */
QString IPRange::getAddress() const {
    return address;
}


/*!
 * \brief IPRange::getType
 * \return
 */
IPRangeType IPRange::getType() {
    if (!gottype) {
        type = guessType();
        gottype = true;
    }
    return type;
}


/*!
 * \brief IPRange::guessType
 * \return
 */
IPRangeType IPRange::guessType() {
    QRegExp sanity("^[0-9a-zA-Z./-]*$");
    QRegExp domainnametest("^([a-zA-Z0-9-]+\\.)+[a-zA-Z0-9-]+$");
    QRegExp iptest("^([0-9]+)\\.([0-9]+)\\.([0-9]+)\\.([0-9]+)$");
    QRegExp ipmaskedtest("^([0-9]+)\\.([0-9]+)\\.([0-9]+)\\.([0-9]+)/([0-9]+)$");
    QRegExp ipmasked2test("^([0-9]+)\\.([0-9]+)\\.([0-9]+)\\.([0-9]+)/([0-9]+)\\.([0-9]+)\\.([0-9]+)\\.([0-9]+)$");

    bool ok;
    long ipbyte;
        
    // Smoke text
    if (!sanity.exactMatch(address)) {
        return invalid;
    }

    if (address.length() == 0) {
        return invalid;
    }

    // Test against the domainname regexp.
    if (domainnametest.exactMatch(address)) {
        return domainname;
    }
    
    // Ok, now lets try the IP address regexp.
    if (iptest.exactMatch(address)) {
        ipbyte = iptest.cap(1).toLong(&ok);    // Yep, it returns char *.
        if (ipbyte < 0 || ipbyte > 255) {
            return invalid;
        }
        ipbyte = iptest.cap(2).toLong(&ok);
        if (ipbyte < 0 || ipbyte > 255) {
            return invalid;
        }
        ipbyte = iptest.cap(3).toLong(&ok);
        if (ipbyte < 0 || ipbyte > 255) {
            return invalid;
        }
        ipbyte = iptest.cap(4).toLong(&ok);
        if (ipbyte < 0 || ipbyte > 255) {
            return invalid;
        }
        return ip;
    }

    // Ok, now lets try the IP address regexp.
    if (ipmaskedtest.exactMatch(address)) {
        ipbyte = ipmaskedtest.cap(1).toLong(&ok);    // Yep, it returns char *.
        if (ipbyte < 0 || ipbyte > 255) {
            return invalid;
        }
        ipbyte = ipmaskedtest.cap(2).toLong(&ok);
        if (ipbyte < 0 || ipbyte > 255) {
            return invalid;
        }
        ipbyte = ipmaskedtest.cap(3).toLong(&ok);
        if (ipbyte < 0 || ipbyte > 255) {
            return invalid;
        }
        ipbyte = ipmaskedtest.cap(4).toLong(&ok);
        if (ipbyte < 0 || ipbyte > 255) {
            return invalid;
        }
        ipbyte = ipmaskedtest.cap(5).toLong(&ok);
        if (ipbyte < 0 || ipbyte > 32) {
            return invalid;
        }
        return iprange;
    }
    
    if (ipmasked2test.exactMatch(address)) {
        ipbyte = ipmasked2test.cap(1).toLong(&ok);    // Yep, it returns char *.
        if (ipbyte < 0 || ipbyte > 255) {
            return invalid;
        }
        ipbyte = ipmasked2test.cap(2).toLong(&ok);
        if (ipbyte < 0 || ipbyte > 255) {
            return invalid;
        }
        ipbyte = ipmasked2test.cap(3).toLong(&ok);
        if (ipbyte < 0 || ipbyte > 255) {
            return invalid;
        }
        ipbyte = ipmasked2test.cap(4).toLong(&ok);
        if (ipbyte < 0 || ipbyte > 255) {
            return invalid;
        }
        ipbyte = ipmasked2test.cap(5).toLong(&ok);
        if (ipbyte < 0 || ipbyte > 255) {
            return invalid;
        }
        ipbyte = ipmasked2test.cap(6).toLong(&ok);
        if (ipbyte < 0 || ipbyte > 255) {
            return invalid;
        }
        ipbyte = ipmasked2test.cap(7).toLong(&ok);
        if (ipbyte < 0 || ipbyte > 255) {
            return invalid;
        }
        ipbyte = ipmasked2test.cap(8).toLong(&ok);
        if (ipbyte < 0 || ipbyte > 255) {
            return invalid;
        }
        return iprange;
    }
    return invalid;
}