brute-force changed access to $_GET['key'] to use get_array instead
[plewww.git] / planetlab / nodes / comon.php
1 <?php
2
3 // Require login
4 require_once 'plc_login.php';
5
6 // Get session and API handles
7 require_once 'plc_session.php';
8 global $plc, $api;
9
10 // Common functions
11 require_once 'plc_functions.php';
12
13 // arguments, e.g. :
14 // http://summer.cs.princeton.edu/
15 // "138.96.250.12"
16 function plc_ip_to_int ($ip) {
17   $bytes=array_map("intval",explode(".",$ip));
18   $res=0;
19   foreach (range(0,3) as $i) {
20     $res=256*$res+$bytes[$i];
21   }
22   return $res;
23   }
24
25 function plc_comon_address ($ip) {
26   return "address==".plc_ip_to_int($ip);
27 }
28
29 // builds the url to comon for the set of ips
30 function plc_comon_url_from_ips($comon_server_url, $ips) {
31   
32   $select="select='" . join("||",array_map("plc_comon_address",$ips)) . "'";
33
34   $url=$comon_server_url . 
35     "/status/tabulator.cgi?table=table_nodeviewshort&" . $select;
36   return $url;
37
38 }
39
40 // we expect to GET either
41 // node_id
42 // site_id
43 // slice_id
44 // peer_id
45 // from that we get a set of nodes and compute a comon URL to gather them all
46
47 $fields=array("hostname","node_id","peer_id", "interface_ids");
48
49 if (get_array($_GET, 'node_id')) {
50   $node_id=intval($_GET['node_id']);
51   $nodes=$api->GetNodes(array("node_id"=>array($node_id)),$fields);
52  } else if (get_array($_GET, 'site_id')) {
53   $site_id=intval($_GET['site_id']);
54   $nodes=$api->GetNodes(array("node_type"=>"regular","site_id"=>array($site_id)),$fields);
55  } else if (get_array($_GET, 'slice_id')) {
56   $slice_id=intval($_GET['slice_id']);
57   $return=$api->GetSlices(array("slice_id"=>array($slice_id)),array("node_ids"));
58   $node_ids=$return[0]['node_ids'];
59   $nodes=$api->GetNodes(array("node_type"=>"regular","node_id"=>$node_ids),$fields);
60  } else if (get_array($_GET, 'peer_id'))) {
61   $peer_id=intval($_GET['peer_id']);
62   if ( ($peer_id == 0) || ($peer_id == "") )
63     $peer_id=NULL;
64   $nodes=$api->GetNodes(array("node_type"=>"regular","peer_id"=>$peer_id),$fields);
65  } else {
66   echo "<div class='plc-warning'> Unexpected args in comon.php </div>\n";
67   exit();
68  }
69
70 // first pass 
71 // * gather interface_ids for local nodes
72 // * gather hostnames for foreign nodes
73
74 $interface_ids=array();
75 $hostnames = array();
76
77 foreach ($nodes as $node) {
78   if (empty($node['peer_id'])) {
79     foreach ($node['interface_ids'] as $id=>$nnid) {
80       $interface_ids [] = $nnid;
81     }
82   } else {
83     $hostnames[] = $node['hostname'];
84   }
85 }
86   
87 // Gather local ips from primary interfaces
88 // fetch primary interfaces
89 $local_ips=array();
90 $nns = $api->GetInterfaces(array("is_primary"=>TRUE,"interface_id"=>$interface_ids),
91                              array("ip"));
92 foreach ($nns as $nn) {
93   $local_ips[] = $nn['ip'];
94 }
95
96 // for foreign hosts we're left with dns resolving them
97 $remote_ips=array();
98 foreach ($hostnames as $hostname) {
99   $resolved=gethostbyname($hostname);
100   // no way to notify this
101   if ($resolved == $hostname) {
102   } else {
103     $remote_ips[] = $resolved;
104   }
105 }
106
107 // add both lists
108 $all_ips=$local_ips+$remote_ips;
109 // compute comon URL
110 $url = plc_comon_url_from_ips("http://comon.cs.princeton.edu",$all_ips);
111
112
113 // redirect to comon
114 plc_redirect($url);
115
116 ?>