Ejemplo n.º 1
0
 /**
 Publish TR data 
 */
 public static function publishTraceroute($data)
 {
     global $dbconn, $ixmaps_debug_mode;
     // FIXME: move this to config.php
     $URI = "https://www.ixmaps.ca/cgi-bin/gather-tr.cgi";
     $trSubString = "";
     $trString = "";
     /*check tr status: does the TR reach its destination?*/
     end($data['ip_analysis']['hops']);
     $lastKey = key($data['ip_analysis']['hops']);
     $lastIp = $data['ip_analysis']['hops'][$lastKey]['winIp'];
     //echo "LastIP: ".$lastIp;
     if ($data['dest_ip'] == $lastIp) {
         $trStatus = "c";
     } else {
         $trStatus = "i";
     }
     // FIXED! collecting the protocol used in the submission data_type = json
     foreach ($data['traceroute_submissions'] as $sub_data) {
         if ($sub_data['data_type'] == "json") {
             // convert to lowercase before comparison
             $sub_data['protocol'] = strtolower($sub_data['protocol']);
             if ($sub_data['protocol'] == "icmp") {
                 $protocol = "i";
             } else {
                 if ($sub_data['protocol'] == "udp") {
                     $protocol = "u";
                 } else {
                     if ($sub_data['protocol'] == "tcp") {
                         $protocol = "t";
                     }
                 }
             }
         }
     }
     // TODO: check for null fields
     // convert timeout to seconds
     $data['timeout'] = round($data['timeout'] / 1000);
     $trString = "dest=" . $data['dest'] . "&dest_ip=" . $data['dest_ip'] . "&submitter=" . urlencode($data['submitter']) . "&zip_code=" . urlencode($data['postal_code']) . "&client=" . urlencode($data['traceroute_submissions'][0]['client']) . "&cl_ver=1.0&privacy=8&timeout=" . $data['timeout'] . "&protocol=" . $protocol . "&maxhops=" . $data['maxhops'] . "&attempts=" . $data['queries'] . "&status=" . $trStatus;
     $hopCount = 0;
     $foundFirstValidIp = false;
     // hops
     foreach ($data['ip_analysis']['hops'] as $key => $hop) {
         // skip local ips
         if (!GatherTr::checkIpIsPrivate($hop['winIp']) || $hop['winIp'] == "") {
             // anonimize first valid ip
             if (!$foundFirstValidIp && $hop['winIp'] != "") {
                 $foundFirstValidIp = true;
                 //echo "\n First Valid IP: ".$hop['winIp'];
                 $ipQuads = explode('.', $hop['winIp']);
                 $ipAmonim = "";
                 for ($i = 0; $i < count($ipQuads); $i++) {
                     if ($i == count($ipQuads) - 1) {
                         $ipAmonim .= ".0";
                     } else {
                         if ($i == 0) {
                             $ipAmonim .= "" . $ipQuads[$i];
                         } else {
                             $ipAmonim .= "." . $ipQuads[$i];
                         }
                     }
                 }
                 $hop['winIp'] = $ipAmonim;
             }
             $hopCount++;
             $latencyCount = 0;
             // latencies
             foreach ($hop['latencies'] as $key1 => $latency) {
                 $latencyCount++;
                 $rtt_ms = 0;
                 if ($hop['winIp'] == "") {
                     $status = "t";
                 } else {
                     $status = "r";
                 }
                 $trString .= "&status_" . $hopCount . "_" . $latencyCount . "=" . $status . "&ip_addr_" . $hopCount . "_" . $latencyCount . "=" . $hop['winIp'] . "&rtt_ms_" . $hopCount . "_" . $latencyCount . "=" . round($latency);
             }
         } else {
             //echo "\n skiping ip: ".$hop['winIp'];
         }
         // end skip ip
     }
     $totItems = $hopCount * $data['queries'];
     $trString .= "&n_items=" . $totItems;
     //echo "\n".$trString."";
     // adding exceptions for SSL certificate
     $arrContextOptions = array("ssl" => array("verify_peer" => false, "verify_peer_name" => false));
     // publish data
     $trResult = file_get_contents($URI . "?" . $trString, false, stream_context_create($arrContextOptions));
     //echo "\n\n".$trResult;
     $search = "new traceroute ID";
     //$lines       = file('example.txt');
     $line_number = false;
     $tr_id_arr = explode("\n", $trResult);
     while (list($key, $line) = each($tr_id_arr) and !$line_number) {
         $line_number = strpos($line, $search) !== FALSE ? $key + 1 : $line_number;
     }
     $tr_id_line = explode("=", $tr_id_arr[$line_number - 1]);
     //echo "\nTR ID: ".$tr_id_line[1];
     if (count($tr_id_line) == 2 && $tr_id_line[1] != 0) {
         return $tr_id_line[1];
     } else {
         return 0;
     }
 }