Changeset 906:0c387ff526c8

Show
Ignore:
Timestamp:
09/12/08 14:02:11 (4 months ago)
Author:
Phil <phil@secdev.org>
Message:

Fixed netstat -rn output parsing on Solaris (ticket #135)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • scapy/arch/unix.py

    r885 r906  
    77import sys,os,struct,socket,time 
    88from fcntl import ioctl 
     9from scapy.error import warning 
    910import scapy.config 
    1011import scapy.utils 
     
    3435    mtu_present = False 
    3536    routes = [] 
     37    pending_if = [] 
    3638    for l in f.readlines(): 
    3739        if not l: 
     
    5052            break 
    5153        if scapy.arch.SOLARIS: 
    52             dest,mask,gw,netif,mxfrg,rtt,ref,flg = l.split()[:8] 
     54            lspl = l.split() 
     55            if len(lspl) == 10: 
     56                dest,mask,gw,netif,mxfrg,rtt,ref,flg = lspl[:8] 
     57            else: # missing interface 
     58                dest,mask,gw,mxfrg,rtt,ref,flg = lspl[:7] 
     59                netif=None 
    5360        else: 
    5461            if mtu_present: 
     
    7380        if not "G" in flg: 
    7481            gw = '0.0.0.0' 
    75         ifaddr = scapy.arch.get_if_addr(netif) 
    76         routes.append((dest,netmask,gw,netif,ifaddr)) 
     82        if netif is not None: 
     83            ifaddr = scapy.arch.get_if_addr(netif) 
     84            routes.append((dest,netmask,gw,netif,ifaddr)) 
     85        else: 
     86            pending_if.append((dest,netmask,gw)) 
    7787    f.close() 
     88 
     89    # On Solaris, netstat does not provide output interfaces for some routes 
     90    # We need to parse completely the routing table to route their gw and 
     91    # know their output interface 
     92    for dest,netmask,gw in pending_if: 
     93        gw_l = scapy.utils.atol(gw) 
     94        max_rtmask,gw_if,gw_if_addr, = 0,None,None 
     95        for rtdst,rtmask,_,rtif,rtaddr in routes[:]: 
     96            if gw_l & rtmask == rtdst: 
     97                if rtmask >= max_rtmask: 
     98                    max_rtmask = rtmask 
     99                    gw_if = rtif 
     100                    gw_if_addr = rtaddr 
     101        if gw_if: 
     102            routes.append((dest,netmask,gw,gw_if,gw_if_addr)) 
     103        else: 
     104            warning("Did not find output interface to reach gateway %s" % gw) 
     105             
    78106    return routes 
    79107