first check-in of feisty meow codebase. many things broken still due to recent
[feisty_meow.git] / octopi / library / sockets / subnet_calculator.h
1 #ifndef SUBNET_CALCULATOR_CLASS
2 #define SUBNET_CALCULATOR_CLASS
3
4 /*****************************************************************************\
5 *                                                                             *
6 *  Name   : subnet_calculator                                                 *
7 *  Author : Chris Koeritz                                                     *
8 *                                                                             *
9 *******************************************************************************
10 * Copyright (c) 1997-$now By Author.  This program is free software; you can  *
11 * redistribute it and/or modify it under the terms of the GNU General Public  *
12 * License as published by the Free Software Foundation; either version 2 of   *
13 * the License or (at your option) any later version.  This is online at:      *
14 *     http://www.fsf.org/copyleft/gpl.html                                    *
15 * Please send any updates to: fred@gruntose.com                               *
16 \*****************************************************************************/
17
18 #include <basis/astring.h>
19
20 namespace sockets {
21
22 //! Provides an easy way to determine the range of a subnet given the subnet mask and a sample IP address.
23
24 class subnet_calculator : public basis::root_object
25 {
26 public:
27   subnet_calculator(const basis::astring &subnet_mask, const basis::astring &ip_address);
28   ~subnet_calculator();
29
30   basis::astring convert(basis::un_int num_format);
31   basis::un_int convert(const basis::astring &ip_format);
32     // converts between the numerical and string forms of the ip address.
33
34   // these two functions return the computed low / high range of the subnet.
35   // they ensure that the subnet calculator is valid by calling the calculate
36   // method if it hasn't already been called.
37   const basis::astring &low_end();
38   const basis::astring &high_end();
39
40   // these allow observation and modification of the parameters that are needed
41   // to calculate the subnet range.
42   const basis::astring &subnet_mask() const;
43   void subnet_mask(const basis::astring &new_mask);
44   const basis::astring &ip_address() const;
45   void ip_address(const basis::astring &new_address);
46
47   bool valid() const { return _valid; }
48     // returns whether the object has recalculated its mask information yet
49     // or not.  the object should always be valid until the mask or address
50     // are changed.  once the two range methods are called, the object should 
51     // be valid afterwards.
52
53 private:
54   bool _valid;  // is this object valid yet (has calculate been called)?
55   basis::astring *_subnet_mask;  // the mask used for the subnets in question.
56   basis::astring *_ip_address;  // internet address of an example host on the subnet.
57   basis::astring *_low_end;  // lower bound and
58   basis::astring *_high_end;   // upper bound of the subnet.
59
60   void calculate();
61     // performs the main action of determining the lower and upper bounds
62     // of the subnet's range.
63 };
64
65 } //namespace.
66
67 #endif
68