File: displayrouter.php

package info (click to toggle)
ipplan 4.92a-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 7,256 kB
  • ctags: 11,121
  • sloc: php: 44,277; sh: 387; perl: 257; xml: 97; makefile: 57; sql: 32
file content (261 lines) | stat: -rwxr-xr-x 8,806 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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
<?php

// IPplan v4.92a
// Aug 24, 2001
//
// 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.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//


// return array containing routing table obtained via SNMP
// array looks like this:
//      a["network"][$i]
//      a["mask"][$i]
function GetRoutingTable($host, $community, $rtrtype) {

   global $w,$p;

   $OID=array("generic"=>".1.3.6.1.2.1.4.21.1.11",
              "riverstone"=>".1.3.6.1.2.1.4.24.4",
              "juniper"=>".1.3.6.1.2.1.4.24.4.1");

   if (!extension_loaded("snmp")) {
       myError($w,$p, "no snmp!!! - compile php with --with-snmp --enable-ucd-snmp-hack");
       exit;
   }

   if (strpos(strtoupper(PHP_OS),'WIN') !== false) {
      // Windows snmp different
   } else {
      // Unix snmp different - need to set quickprint to be compatible
      // with Windows format. Windows does not have long print format
      // must test for os version as undefined function generates error
      // even with @
      snmp_set_quick_print(1);
   }
   // protect against bad users!
   if (!array_key_exists($rtrtype, $OID)) {
       $rtrtype="generic";
   }
   $routes = @snmpwalkoid($host, $community, $OID[$rtrtype]);

   if (!$routes) {
      return 0;
   }

   for (reset($routes); $network = key($routes); next($routes)) {
       //here is the way to do it with RFC 2096 using ipCidrRouteMask
       //this is what we get back from the riverstone
       //meaning:  subnet IP, subnet mask, destination = ip destination ip
       if ($rtrtype=="riverstone") {
           //kill the destination
           list($oc1, $oc2, $oc3, $oc4, $oc5, $rest) = explode(".", strrev($network), 6);
           //take the subnetmask
           list($oc1, $oc2, $oc3, $oc4, $rest) = explode(".", $rest, 5);
           $mask=strrev(sprintf("%s.%s.%s.%s", $oc1, $oc2, $oc3, $oc4));
           //take the subnet addr
           list($oc1, $oc2, $oc3, $oc4, $rest) = explode(".", $rest, 5);
           $netaddr=strrev(sprintf("%s.%s.%s.%s", $oc1, $oc2, $oc3, $oc4));
       }
       else {
           // The Old way to do it with RFC 1213 MIBv2 (which is deprecated)
           // do some magic to obtain a unique, sortable array index to force the results
           // into ip address order. index will be x0000000000 where the digits are the
           // integer representation of the ip address padded with zeros.
           $mask=$routes[$network];
           // strip out last 4 octets from mib value - lots of .'s
           // complicate matters
           list($oc1, $oc2, $oc3, $oc4, $rest) = explode(".", strrev($network), 5);
           $netaddr=strrev(sprintf("%s.%s.%s.%s", $oc1, $oc2, $oc3, $oc4));
       }

       // $ind='x'.str_pad(inet_aton(substr($netaddr, strpos($netaddr, '.')+1)), 10, "0", STR_PAD_LEFT);
       $ind='x'.str_pad(inet_aton($netaddr), 10, "0", STR_PAD_LEFT);
       $result["$ind"]=array(
               "rtrbase"=>$netaddr,
               "rtrmask"=>$mask);
   }

                        //"rtrmask"=>substr($mask, strpos($mask, ' ')+1));
   return $result;
}

require_once("../ipplanlib.php");
require_once("../adodb/adodb.inc.php");
require_once("../class.dbflib.php");
require_once("../layout/class.layout");

// set language
isset($_COOKIE["ipplanLanguage"]) && myLanguage($_COOKIE['ipplanLanguage']);

//setdefault("window",array("bgcolor"=>"white"));
//setdefault("table",array("cellpadding"=>"0"));
//setdefault("text",array("size"=>"2"));

$title=my_("Display routing table results");
newhtml($p);
$w=myheading($p, $title, true);

// explicitly cast variables as security measure against SQL injection
list($cust, $ipaddr, $community, $rtrtype) = myRegister("I:cust S:ipaddr S:community S:rtrtype");

if (!$_POST) {
   myError($w,$p, my_("You cannot reload or bookmark this page!"));
}

$startnum=inet_aton(DEFAULTROUTE);
$endnum=inet_aton(ALLNETS);

// basic sequence is connect, search, interpret search
// result, close connection

$ds=new IPplanDbf() or myError($w,$p, my_("Could not connect to database"));

if (testIP($ipaddr)) {
   myError($w,$p, my_("Invalid IP address"));
}
// get snmp info
if (!$resultsnmp = GetRoutingTable($ipaddr, $community, $rtrtype)) {
   myError($w,$p, my_("Could not query router - no SNMP on router or firewall blocking access?"));
}

$custdescrip=$ds->GetCustomerDescrip($cust);

insert($w,heading(3, sprintf(my_("Search for IP subnets from a routing table for customer '%s'"), $custdescrip)));

// get database info
$result = $ds->GetBase($startnum, $endnum, "", $cust);

// rewrite arrays for sorting and merging to work - use ip address
// as key in this format "x0000012345"
$dbf=array();
while ($row = $result->FetchRow()) {
   $ind='x'.str_pad($row["baseaddr"], 10, "0", STR_PAD_LEFT);
   $dbf["$ind"]=array("dbfbase"=>inet_ntoa($row["baseaddr"]),
                              "dbfsize"=>$row["subnetsize"],
                              "baseindex"=>$row["baseindex"],
                              "dbfdescrip"=>$row["descrip"]);
}

// do the merge
$arr=array_merge_recursive($resultsnmp, $dbf);
ksort($arr);

// create a table
insert($w,$t = table(array("cols"=>"7",
                           "class"=>"outputtable")));
 
// create a table
setdefault("cell",array("class"=>"heading"));
insert($t,$ct = cell(array("colspan"=>"3")));
insert($ct,block("<center>"));
insert($ct,text(my_("Routing Table")));
insert($ct,block("</center>"));
insert($t,$ct = cell(array("colspan"=>"4")));
insert($ct,block("<center>"));
insert($ct,text(my_("IPplan information")));
insert($ct,block("</center>"));
 
// draw heading
insert($t,$c = cell());
insert($c,text(my_("Routing Table Base address")));
insert($t,$c = cell());
insert($c,text(my_("Routing Table Subnet size")));
insert($t,$c = cell());
insert($c,text(my_("Routing Table Subnet Mask")));
insert($t,$c = cell());
insert($c,text(my_("Base address")));
insert($t,$c = cell());
insert($c,text(my_("Subnet size")));
insert($t,$c = cell());
insert($c,text(my_("Subnet Mask")));
insert($t,$c = cell());
insert($c,text(my_("Description")));

 
$cnt=0;
while (list ($key, $val) = each ($arr)) {

    setdefault("cell",array("class"=>color_flip_flop()));

    if (isset($val["rtrbase"]) and $val["rtrbase"] == DEFAULTROUTE) {
       insert($t,$c = cell(array("colspan"=>"3")));
       insert($c,text("Default route"));
       insert($t,$c = cell(array("colspan"=>"4")));
       continue;
    }
    if (isset($val["rtrbase"])) {
        $hosts=inet_aton(ALLNETS)-inet_aton($val["rtrmask"])+1;
        insert($t,$c = cell());
        insert($c,anchor("createsubnetform.php?ipaddr=".$val["rtrbase"].
                    "&cust=".$cust."&size=".$hosts, $val["rtrbase"]));

        insert($t,$c = cell());
        if ($val["rtrmask"]) {
            if ($hosts == 1)
                insert($c,text(my_("Host")));
            else {
                insert($c,text($hosts));
            }
        }

        insert($t,$c = cell());
        if ($val["rtrmask"]) {
            insert($c,text($val["rtrmask"]."/".inet_bits(inet_aton(ALLNETS)-inet_aton($val["rtrmask"])+1)));
        }
    }
    else {
        insert($t,$c = cell());
        insert($t,$c = cell());
        insert($t,$c = cell());
    }

    if (isset($val["dbfsize"])) {
        insert($t,$c = cell());
        if ($val["dbfsize"] == 1) {
            insert($c,text($val["dbfbase"]));
        }
        else {
            insert($c,anchor("displaysubnet.php?baseindex=".$val["baseindex"], 
                        $val["dbfbase"]));
        }
        insert($t,$c = cell());
        if ($val["dbfsize"] == 1)
            insert($c,text(my_("Host")));
        else
            insert($c,text($val["dbfsize"]));
        insert($t,$c = cell());
        if ($val["dbfsize"])
            insert($c,text(inet_ntoa(inet_aton(ALLNETS)+1-$val["dbfsize"])."/".inet_bits($val["dbfsize"])));
        insert($t,$c = cell());
        insert($c,text($val["dbfdescrip"]));
    }
    else {
        insert($t,$c = cell());
        insert($t,$c = cell());
        insert($t,$c = cell());
        insert($t,$c = cell());
    }

    $cnt++;
}

insert($w,block("<p>"));
insert($w,textb(sprintf(my_("Total records: %u"), $cnt)));

$result->Close();
printhtml($p);

?>