From 63a6f32d5f6806b8000b79119ec094f4bfe9552e Mon Sep 17 00:00:00 2001
From: Mehdi Abaakouk <sileht@sileht.net>
Date: Wed, 18 Jul 2012 23:46:44 +0200
Subject: [PATCH] Use service cymru.com service instead of whois

---
 lg.py      | 17 ++++++++++++++++-
 toolbox.py |  4 ++++
 2 files changed, 20 insertions(+), 1 deletion(-)

diff --git a/lg.py b/lg.py
index 7ccca13..728503c 100755
--- a/lg.py
+++ b/lg.py
@@ -27,7 +27,7 @@ from urllib import quote, unquote
 import json
 import random
 
-from toolbox import mask_is_valid, ipv6_is_valid, ipv4_is_valid, resolve, save_cache_pickle, load_cache_pickle
+from toolbox import mask_is_valid, ipv6_is_valid, ipv4_is_valid, resolve, save_cache_pickle, load_cache_pickle, get_asn_from_as
 
 import pydot
 from flask import Flask, render_template, jsonify, redirect, session, request, abort, Response
@@ -315,10 +315,15 @@ def get_as_name(_as):
     It's the use whois database informations
     # Warning, the server can be blacklisted from ripe is too many requests are done
     """
+    if not _as:
+        return "AS?????"
 
     if not _as.isdigit():
         return _as.strip()
 
+    name = get_asn_from_as(_as)[-1].replace(" ","\r",1)
+    return "AS%s | %s" % (_as, name)
+
     if _as not in ASNAME_CACHE:
         whois_answer = whois_command("as%s" % _as)
         as_name = re.search('(as-name|ASName): (.*)', whois_answer)
@@ -467,6 +472,16 @@ def build_as_tree_from_raw_bird_ouput(host, proto, text):
                 # ugly hack for good printing
                 path = [ peer_protocol_name ]
 #                path = ["%s\r%s" % (peer_protocol_name, get_as_name(get_as_number_from_protocol_name(host, proto, peer_protocol_name)))]
+        
+        expr2 = re.search(r'(.*)unreachable\s+\[(\w+)\s+', line)
+        if expr2:
+            if path:
+                path.append(net_dest)
+                paths.append(path)
+                path = None
+
+            if expr2.group(1).strip():
+                net_dest = expr2.group(1).strip()
 
         if line.startswith("BGP.as_path:"):
             path.extend(line.replace("BGP.as_path:", "").strip().split(" "))
diff --git a/toolbox.py b/toolbox.py
index 6b5a253..7c82f4c 100644
--- a/toolbox.py
+++ b/toolbox.py
@@ -27,6 +27,10 @@ import pickle
 def resolve(n, q):
 	return str(resolver.query(n,q)[0])
 
+def get_asn_from_as(n):
+    data = resolve("AS%s.asn.cymru.com" % n ,"TXT").replace("'","").replace('"','')
+    return [ field.strip() for field in data.split("|") ]
+
 def mask_is_valid(n):
 	if not n: 
 		return True