From 9a22635aa9f0b2fa4cef304fa1d9ea763bf1a90b Mon Sep 17 00:00:00 2001
From: Phil <phil@secdev.org>
Date: Mon, 28 Jul 2008 16:13:00 +0200
Subject: [PATCH] Adapted M. Saitoh patch to use correct loopback interface
 name on *BSD

---
 scapy/arch.py  | 21 ++++++++++++++-------
 scapy/route.py |  8 ++++----
 2 files changed, 18 insertions(+), 11 deletions(-)

diff --git a/scapy/arch.py b/scapy/arch.py
index 8730534e..f3a1bcc0 100644
--- a/scapy/arch.py
+++ b/scapy/arch.py
@@ -23,6 +23,7 @@ except ImportError:
 LINUX=sys.platform.startswith("linux")
 OPENBSD=sys.platform.startswith("openbsd")
 FREEBSD=sys.platform.startswith("freebsd")
+NETBSD = sys.platform.startswith("netbsd")
 DARWIN=sys.platform.startswith("darwin")
 BIG_ENDIAN= struct.pack("H",1) == "\x00\x01"
 X86_64 = (os.uname()[4] == 'x86_64')
@@ -32,6 +33,12 @@ if LINUX:
     DNET=PCAP=0
 else:
     DNET=PCAP=1
+
+if OPENBSD or FREEBSD or NETBSD or DARWIN:
+    LOOPBACK_NAME="lo0"
+else:
+    LOOPBACK_NAME="lo"
+
     
 
 if PCAP:
@@ -118,7 +125,7 @@ def str2mac(s):
 
 if DNET:
     def get_if_raw_hwaddr(iff):
-        if iff[:2] == "lo":
+        if iff[:2] == LOOPBACK_NAME:
             return (772, '\x00'*6)
         try:
             l = dnet.intf().get(iff)
@@ -148,7 +155,7 @@ if PCAP:
         try:
             return pcap.lookupdev()
         except Exception:
-            return 'lo'
+            return LOOPBACK_NAME
 
     def attach_filter(s, filter):
         warning("attach_filter() should not be called in PCAP mode")
@@ -166,12 +173,12 @@ else:
         return lst
     def get_working_if():
         for i in get_if_list():
-            if i == 'lo':                
+            if i == LOOPBACK_NAME:                
                 continue
             ifflags = struct.unpack("16xH14x",get_if(i,SIOCGIFFLAGS))[0]
             if ifflags & IFF_UP:
                 return i
-        return "lo"
+        return LOOPBACK_NAME
     def attach_filter(s, filter):
         # XXX We generate the filter on the interface conf.iface 
         # because tcpdump open the "any" interface and ppp interfaces
@@ -306,14 +313,14 @@ else:
         f=open("/proc/net/route","r")
         routes = []
         s=socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
-        ifreq = ioctl(s, SIOCGIFADDR,struct.pack("16s16x","lo"))
+        ifreq = ioctl(s, SIOCGIFADDR,struct.pack("16s16x",LOOPBACK_NAME))
         addrfamily = struct.unpack("h",ifreq[16:18])[0]
         if addrfamily == socket.AF_INET:
-            ifreq2 = ioctl(s, SIOCGIFNETMASK,struct.pack("16s16x","lo"))
+            ifreq2 = ioctl(s, SIOCGIFNETMASK,struct.pack("16s16x",LOOPBACK_NAME))
             msk = socket.ntohl(struct.unpack("I",ifreq2[20:24])[0])
             dst = socket.ntohl(struct.unpack("I",ifreq[20:24])[0]) & msk
             ifaddr = inet_ntoa(ifreq[20:24])
-            routes.append((dst, msk, "0.0.0.0", "lo", ifaddr))
+            routes.append((dst, msk, "0.0.0.0", LOOPBACK_NAME, ifaddr))
         else:
             warning("Interface lo: unkown address family (%i)"% addrfamily)
     
diff --git a/scapy/route.py b/scapy/route.py
index 1bf0b10a..6fb52018 100644
--- a/scapy/route.py
+++ b/scapy/route.py
@@ -1,5 +1,5 @@
 import socket
-from arch import read_routes,get_if_addr
+from arch import read_routes,get_if_addr,LOOPBACK_NAME
 from utils import atol,ltoa,itom
 from config import conf
 from error import Scapy_Exception,warning
@@ -127,13 +127,13 @@ class Route:
         for d,m,gw,i,a in self.routes:
             aa = atol(a)
             if aa == dst:
-                pathes.append((0xffffffffL,("lo",a,"0.0.0.0")))
+                pathes.append((0xffffffffL,(LOOPBACK_NAME,a,"0.0.0.0")))
             if (dst & m) == (d & m):
                 pathes.append((m,(i,a,gw)))
         if not pathes:
             if verbose:
                 warning("No route found (no default route?)")
-            return "lo","0.0.0.0","0.0.0.0" #XXX linux specific!
+            return LOOPBACK_NAME,"0.0.0.0","0.0.0.0" #XXX linux specific!
         # Choose the more specific route (greatest netmask).
         # XXX: we don't care about metrics
         pathes.sort()
@@ -152,6 +152,6 @@ conf.route=Route()
 
 #XXX use "with"
 _betteriface = conf.route.route("0.0.0.0", verbose=0)[0]
-if _betteriface != "lo": #XXX linux specific...
+if _betteriface != LOOPBACK_NAME:
     conf.iface = _betteriface
 del(_betteriface)
-- 
GitLab