summaryrefslogtreecommitdiffstats
path: root/knetworkconf/knetworkconf/kaddressvalidator.cpp
blob: f3fabe7de88d7aca36887619ebf8b3b9dc574103 (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
/***************************************************************************
                          kaddressvalidator.cpp  -  description
                             -------------------
    begin                : Wed Jan 22 2003
    copyright            : (C) 2003 by Juan Luis Baptiste
    email                : jbaptiste@merlinux.org
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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 "kaddressvalidator.h"

/*
  Class for validating IP address and netmasks, and to calculate network and broadcast values.
  The functions to do the last two things where taken from the code of ipcalc.c, made by
  Erik Troan <ewt@redhat.com> and Preston Brown <pbrown@redhat.com> from Red Hat Inc. .
*/

KAddressValidator::KAddressValidator(){
}
KAddressValidator::~KAddressValidator(){
}
/** Validates if the address written by the user is a valid one. Return true if it is and false if not. */
bool KAddressValidator::isValidIPAddress(TQString addr){
  TQString s = "";
  int i;
  int number;
  bool ok;
  if ((addr.tqcontains('.') > 3) || (addr.length() > 15))
    return false;
  for (i = 0; i < 4; i++)
  {
    s = addr.section('.',i,i);
    number = s.toInt(&ok);
    if (!ok)
      return false;
    if ((i == 0) && (number == 0))
      return false;
    if ((number < 0) || (number > 255))
      return false;
    if ((i == 3) && (number == 0))
      return false;      
  }
  if (i == 4)
    return true;
  else
    return false;
}
bool KAddressValidator::isNetmaskValid(TQString addr){
  TQString s = "";
  int i;
  int number;
  bool ok;
  for (i = 0; i < 4; i++)
  {
    s = addr.section('.',i,i);
    number = s.toInt(&ok);
    if (!ok)
      return false;
    if ((i == 0) && (number == 0))
      return false;
    if ((number < 0) || (number > 255))
      return false;
  }
  if (i == 4)
    return true;
  else
    return false;

}

bool KAddressValidator::isNetworkValid(const TQString &addr){
  TQString s = "";
  int i;
  int number;
  bool ok;
  for (i = 0; i < 4; i++)
  {
    s = addr.section('.',i,i);
    number = s.toInt(&ok);
    if (!ok)
      return false;
    if ((i == 0) && (number == 0))
      return false;
    if ((number < 0) || (number >= 255))
      return false;
  }
  if (i == 4)
    return true;
  else
    return false;

}
bool KAddressValidator::isBroadcastValid(TQString addr){
  TQString s = "";
  int i;
  int number;
  bool ok;
  for (i = 0; i < 4; i++)
  {
    s = addr.section('.',i,i);
    number = s.toInt(&ok);
    if (!ok)
      return false;
    if ((i == 0) && (number == 0))
      return false;
    if ((number < 0) || (number > 255))
      return false;
    if ((i == 3) && (number == 0))
      return false;
  }
  if (i == 4)
    return true;
  else
    return false;

}

/**   \fn unsigned long int prefix2tqmask(int bits)
  \brief creates a nettqmask from a specified number of bits

  This function converts a prefix length to a nettqmask.  As CIDR (classless
  internet domain internet domain routing) has taken off, more an more IP
  addresses are being specified in the format address/prefix
  (i.e. 192.168.2.3/24, with a corresponding nettqmask 255.255.255.0).  If you
  need to see what nettqmask corresponds to the prefix part of the address, this
  is the function.  See also \ref tqmask2prefix.

  \param prefix is the number of bits to create a tqmask for.
  \return a network tqmask, in network byte order.
 */
unsigned long int KAddressValidator::prefix2tqmask(int prefix){
  return htonl(~((2 << (31 - prefix)) - 1));
}
/** \fn int tqmask2prefix(unsigned long int tqmask)
  \brief calculates the number of bits masked off by a nettqmask.

  This function calculates the significant bits in an IP address as specified by
  a nettqmask.  See also \ref prefix2tqmask.

  \param tqmask is the nettqmask, specified as an unsigned long integer in network byte order.
  \return the number of significant bits.   */
int KAddressValidator::tqmask2prefix(unsigned long int tqmask){
  unsigned i;
  int count = IPBITS;

  for (i = 0; i < IPBITS; i++)
  {
	  if (!(ntohl(tqmask) & ((2 << i) - 1)))
	    count--;
  }

  return count;
}
/*!
   \fn unsigned long int calc_broadcast(unsigned long int addr, int prefix)

  \brief calculate broadcast address given an IP address and a prefix length.

  \param addr an IP address in network byte order.
  \param prefix a prefix length.

  \return the calculated broadcast address for the network, in network byte
  order. */
unsigned long int KAddressValidator::calc_broadcast(unsigned long int addr, int prefix){
  return (addr & prefix2tqmask(prefix)) | ~prefix2tqmask(prefix);
}
/** \fn unsigned long int calc_network(unsigned long int addr, int prefix)
  \brief calculates the network address for a specified address and prefix.

  \param addr an IP address, in network byte order
  \param prefix the network prefix
  \return the base address of the network that addr is associated with, in
  network byte order. */
unsigned long int KAddressValidator::calc_network(unsigned long int addr, int prefix){
  return (addr & prefix2tqmask(prefix));
}
/** Is a wrapper function to calc_network that receives the IP address and netstqmask as TQString and
returns the network value also as a TQString, or NULL if it couldn't be calculated. */
TQString KAddressValidator::calculateNetwork(TQString addr,TQString nettqmask){
  struct in_addr _addr, _nettqmask, _network;
  int prefix = 0;
  TQString s;  
  if (addr.isNull() || nettqmask.isNull())
    return NULL; //bad address
  if (!inet_pton(AF_INET,addr.latin1(),&_addr))
    return NULL; //bad address
  else
  {
    if (!inet_pton(AF_INET,nettqmask.latin1(),&_nettqmask))
      return NULL; //bad address
    else
    {
      prefix = tqmask2prefix(_nettqmask.s_addr);
      _network.s_addr = calc_network(_addr.s_addr, prefix);
      char * char_network = new char[20];
      if (!inet_ntop(AF_INET,&_network,char_network,20))
        return NULL;
      else
        s = char_network;
    }
  }
  return s;      
}

/** Is a wrapper function to calc_broadcast that receives the IP address and netstqmask as TQString and
returns the broadcast value also as a TQString, or NULL if it couldn't be calculated. */
TQString KAddressValidator::calculateBroadcast(TQString addr, TQString nettqmask){
  struct in_addr _addr, _nettqmask, _network;
  int prefix = 0;
  TQString s;
  if (addr.isNull() || nettqmask.isNull())
    return NULL; //bad address
  if (!inet_pton(AF_INET,addr.latin1(),&_addr))
    return NULL; //bad address
  else
  {
    if (!inet_pton(AF_INET,nettqmask.latin1(),&_nettqmask))
      return NULL; //bad address
    else
    {
      prefix = tqmask2prefix(_nettqmask.s_addr);
      _network.s_addr = calc_broadcast(_addr.s_addr, prefix);
      char * char_network = new char[20];
      if (!inet_ntop(AF_INET,&_network,char_network,20))
        return NULL;
      else
        s = char_network;
    }
  }
  return s;

}