class Scriptroute::Tulip::AliasResolution

alias resolution: ally + ttl-testing + result cache #######

Public Class Methods

aliases(hop1, hop2) click to toggle source
# File lib/scriptroute/tulip/helper.rb, line 198
def AliasResolution.aliases (hop1, hop2) 
  key = cacheKey(hop1.ip, hop2.ip);
  return @@aliasCache[key] if (@@aliasCache[key] != -1) 
    
  output = `#{@@ally} #{hop1.ip} #{hop2.ip}`
  output.gsub!(/\n/, " ");
  Kernel::system("echo '#{hop1.ip} #{hop2.ip} #{output}' >> ally.stats") if (@@logAllyResults);

  if (/ALIAS/.match(output) && !/NOT/.match(output)) then
    @@aliasCache[key] = true;
    return true;
  elsif (@@doTTLTesting and /UNKNOWN/.match(output)) then
    ttlSays = ttlBasedTest(hop1, hop2);
    Kernel::system("echo 'ttlTest: #{hop1} #{hop2} #{ttlSays}' >> ally.stats") if (@@logAllyResults);
    return ttlSays[0];
  else
    @@aliasCache[key] = false;
    return false;
  end
end
before(seq1,seq2) click to toggle source
# File lib/scriptroute/tulip/helper.rb, line 266
def AliasResolution.before(seq1,seq2) 
  diff = seq1-seq2 
  # emulate signed short arithmetic.
  if (diff > 32767) then
    diff-=65535;
  elsif (diff < -32768) then
    diff+=65535;
  end
  # puts "#{seq1} #{diff < 0 ? "" : "not"} before #{seq2}\n"
  (diff < 0)
end
cacheKey(ip1, ip2) click to toggle source
# File lib/scriptroute/tulip/helper.rb, line 194
def AliasResolution.cacheKey (ip1, ip2) 
  (ip1 < ip2)? "#{ip1}:#{ip2}" : "#{ip2}:#{ip1}";
end
seqIPID(thop) click to toggle source
# File lib/scriptroute/tulip/helper.rb, line 219
def AliasResolution.seqIPID (thop) 
  if (@@seqIPIDCache["#{thop.dst} #{thop.hop}"] == -1) 
    @@seqIPIDCache["#{thop.dst} #{thop.hop}"] = LangChecker.increasingIPIDs(thop.dst, thop.hop, "udp");
    puts "seqIPID results for #{thop.dst} #{thop.hop} is %s\n" % [@@seqIPIDCache["#{thop.dst} #{thop.hop}"]];
  end

  return @@seqIPIDCache["#{thop.dst} #{thop.hop}"];
end
ttlBasedTest(hop1, hop2) click to toggle source
# File lib/scriptroute/tulip/helper.rb, line 229
def AliasResolution.ttlBasedTest (hop1, hop2) 
  return [false, "non-seq-ipid"] if (!seqIPID(hop1) or !seqIPID(hop2));
  
  a = Scriptroute::UDP.new(12) 
  b = Scriptroute::UDP.new(12) 
  a.ip_dst, a.ip_ttl = hop1.dst, hop1.hop;
  b.ip_dst, b.ip_ttl = hop2.dst, hop2.hop;
  
  packets = Scriptroute::send_train([ Struct::DelayedPacket.new(0,a), 
                                      Struct::DelayedPacket.new(0.001,b) ])
  
  return [false, "insufficient info"] if (!packets[0] || !packets[1] || 
                                          !packets[0].response || !packets[1].response ||
                                          packets[0].response.packet.ip_src != hop1.ip ||
                                          packets[1].response.packet.ip_src != hop2.ip);
  
  id0 = packets[0].response.packet.ip_id
  id1 = packets[1].response.packet.ip_id
  return [false, "ids dont match"] if (!before(id0-10, id1) || !before(id1, id0+200) || id0 == id1);
  
  packetz = Scriptroute::send_train([ Struct::DelayedPacket.new(0,b), 
                                      Struct::DelayedPacket.new(0.001,a) ])
  
  return [false, "insufficient info2"] if (!packetz[0] || !packetz[1] || 
                                           !packetz[0].response || !packetz[1].response ||
                                           packetz[0].response.packet.ip_src != hop2.ip ||
                                           packetz[1].response.packet.ip_src != hop1.ip);
  
  id2 = packets[0].response.packet.ip_id
  id3 = packets[1].response.packet.ip_id
  return [true, "excellent"] if(before(id2-10, id3) && before(id3, id2+200) && id3 != id2 &&
                                before(id0, id2) && before(id1, id3) && 
                                id2 != id1 && id2 != id0 && id3 != id1 && id3 != id0 );
  
  return [false, "final failure"];
end