Esempio n. 1
0
 def add_ip(self, direct, ip, mask=None,
            brd=None, broadcast=None):
     '''
     Add IP address to an interface
     '''
     # split mask
     if mask is None:
         ip, mask = ip.split('/')
         if mask.find('.') > -1:
             mask = dqn2int(mask)
         else:
             mask = int(mask, 0)
     elif isinstance(mask, basestring):
         mask = dqn2int(mask)
     brd = brd or broadcast
     # FIXME: make it more generic
     # skip IPv6 link-local addresses
     if ip[:4] == 'fe80' and mask == 64:
         return self
     if not direct:
         transaction = self.last()
         transaction.add_ip(ip, mask, brd)
     else:
         self['ipaddr'].unlink((ip, mask))
         if brd is not None:
             raw = {'IFA_BROADCAST': brd}
             self['ipaddr'].add((ip, mask), raw=raw)
         else:
             self['ipaddr'].add((ip, mask))
     return self
Esempio n. 2
0
 def add_ip(self, direct, ip, mask=None, brd=None, broadcast=None):
     '''
     Add IP address to an interface
     '''
     # split mask
     if mask is None:
         ip, mask = ip.split('/')
         if mask.find('.') > -1:
             mask = dqn2int(mask)
         else:
             mask = int(mask, 0)
     elif isinstance(mask, basestring):
         mask = dqn2int(mask)
     brd = brd or broadcast
     # FIXME: make it more generic
     # skip IPv6 link-local addresses
     if ip[:4] == 'fe80' and mask == 64:
         return self
     if not direct:
         transaction = self.last()
         transaction.add_ip(ip, mask, brd)
     else:
         self['ipaddr'].unlink((ip, mask))
         if brd is not None:
             raw = {'IFA_BROADCAST': brd}
             self['ipaddr'].add((ip, mask), raw=raw)
         else:
             self['ipaddr'].add((ip, mask))
     return self
Esempio n. 3
0
    def add_ip(self, ip, mask=None, broadcast=None, anycast=None, scope=None):
        '''
        Add IP address to an interface

        Keyword arguments:

        * mask
        * broadcast
        * anycast
        * scope
        '''
        # split mask
        if mask is None:
            ip, mask = ip.split('/')
            if mask.find('.') > -1:
                mask = dqn2int(mask)
            else:
                mask = int(mask, 0)
        elif isinstance(mask, basestring):
            mask = dqn2int(mask)

        # if it is a transaction or an interface update, apply the change
        self['ipaddr'].unlink((ip, mask))
        request = {}
        if broadcast is not None:
            request['broadcast'] = broadcast
        if anycast is not None:
            request['anycast'] = anycast
        if scope is not None:
            request['scope'] = scope
        self['ipaddr'].add((ip, mask), raw=request)
Esempio n. 4
0
    def add_ip(self, ip, mask=None, broadcast=None, anycast=None, scope=None):
        '''
        Add IP address to an interface

        Keyword arguments:

        * mask
        * broadcast
        * anycast
        * scope
        '''
        # split mask
        if mask is None:
            ip, mask = ip.split('/')
            if mask.find('.') > -1:
                mask = dqn2int(mask)
            else:
                mask = int(mask, 0)
        elif isinstance(mask, basestring):
            mask = dqn2int(mask)

        # if it is a transaction or an interface update, apply the change
        self['ipaddr'].unlink((ip, mask))
        request = {}
        if broadcast is not None:
            request['broadcast'] = broadcast
        if anycast is not None:
            request['anycast'] = anycast
        if scope is not None:
            request['scope'] = scope
        self['ipaddr'].add((ip, mask), raw=request)
Esempio n. 5
0
    def add_ip(self,
               direct,
               ip,
               mask=None,
               broadcast=None,
               anycast=None,
               scope=None):
        '''
        Add IP address to an interface

        Keyword arguments:

        * mask
        * broadcast
        * anycast
        * scope
        '''
        # split mask
        if mask is None:
            ip, mask = ip.split('/')
            if mask.find('.') > -1:
                mask = dqn2int(mask)
            else:
                mask = int(mask, 0)
        elif isinstance(mask, basestring):
            mask = dqn2int(mask)

        # FIXME: make it more generic
        # skip IPv6 link-local addresses
        if ip[:4] == 'fe80' and mask == 64:
            return self

        if not direct:
            # if it is an interface object, route any change
            # to the last transaction
            transaction = self.last()
            transaction.add_ip(ip, mask, broadcast, anycast, scope)
        else:
            # if it is a transaction or an interface update, apply the change
            self['ipaddr'].unlink((ip, mask))
            request = {}
            if broadcast is not None:
                request['broadcast'] = broadcast
            if anycast is not None:
                request['anycast'] = anycast
            if scope is not None:
                request['scope'] = scope
            self['ipaddr'].add((ip, mask), raw=request)
        return self
Esempio n. 6
0
def convert_rt_msg(msg):
    ret = rtmsg()
    ret['header']['type'] = RTNL_NEWROUTE if \
        msg['header']['type'] == RTM_ADD else \
        RTNL_DELROUTE
    ret['family'] = msg['DST']['header']['family']
    ret['attrs'] = []
    if 'address' in msg['DST']:
        ret['attrs'].append(['RTA_DST', msg['DST']['address']])
    if 'NETMASK' in msg and \
            msg['NETMASK']['header']['family'] == ret['family']:
        ret['dst_len'] = dqn2int(msg['NETMASK']['address'], ret['family'])
    if 'GATEWAY' in msg:
        if msg['GATEWAY']['header']['family'] not in (AF_INET, AF_INET6):
            # interface routes, table 255
            # discard for now
            return None
        ret['attrs'].append(['RTA_GATEWAY', msg['GATEWAY']['address']])
    if 'IFA' in msg:
        ret['attrs'].append(['RTA_SRC', msg['IFA']['address']])
    if 'IFP' in msg:
        ret['attrs'].append(['RTA_OIF', msg['IFP']['index']])
    elif msg['rtm_index'] != 0:
        ret['attrs'].append(['RTA_OIF', msg['rtm_index']])
    del ret['value']
    return ret
Esempio n. 7
0
def convert_rt_msg(msg):
    ret = rtmsg()
    ret['header']['type'] = RTNL_NEWROUTE if \
        msg['header']['type'] == RTM_ADD else \
        RTNL_DELROUTE
    ret['family'] = msg['DST']['header']['family']
    ret['attrs'] = []
    if 'address' in msg['DST']:
        ret['attrs'].append(['RTA_DST', msg['DST']['address']])
    if 'NETMASK' in msg and \
            msg['NETMASK']['header']['family'] == ret['family']:
        ret['dst_len'] = dqn2int(msg['NETMASK']['address'], ret['family'])
    if 'GATEWAY' in msg:
        if msg['GATEWAY']['header']['family'] not in (AF_INET, AF_INET6):
            # interface routes, table 255
            # discard for now
            return None
        ret['attrs'].append(['RTA_GATEWAY', msg['GATEWAY']['address']])
    if 'IFA' in msg:
        ret['attrs'].append(['RTA_SRC', msg['IFA']['address']])
    if 'IFP' in msg:
        ret['attrs'].append(['RTA_OIF', msg['IFP']['index']])
    elif msg['rtm_index'] != 0:
        ret['attrs'].append(['RTA_OIF', msg['rtm_index']])
    del ret['value']
    return ret
Esempio n. 8
0
    def add_ip(self, direct, ip,
               mask=None,
               broadcast=None,
               anycast=None,
               scope=None):
        '''
        Add IP address to an interface

        Keyword arguments:

        * mask
        * broadcast
        * anycast
        * scope
        '''
        # split mask
        if mask is None:
            ip, mask = ip.split('/')
            if mask.find('.') > -1:
                mask = dqn2int(mask)
            else:
                mask = int(mask, 0)
        elif isinstance(mask, basestring):
            mask = dqn2int(mask)

        # FIXME: make it more generic
        # skip IPv6 link-local addresses
        if ip[:4] == 'fe80' and mask == 64:
            return self

        if not direct:
            # if it is an interface object, route any change
            # to the last transaction
            transaction = self.last()
            transaction.add_ip(ip, mask, broadcast, anycast, scope)
        else:
            # if it is a transaction or an interface update, apply the change
            self['ipaddr'].unlink((ip, mask))
            request = {}
            if broadcast is not None:
                request['broadcast'] = broadcast
            if anycast is not None:
                request['anycast'] = anycast
            if scope is not None:
                request['scope'] = scope
            self['ipaddr'].add((ip, mask), raw=request)
        return self
Esempio n. 9
0
    def _GetAdaptersInfo(self):
        ret = {'interfaces': [],
               'addresses': []}

        # prepare buffer
        buf = ctypes.create_string_buffer(15000)
        buf_len = ctypes.c_ulong(15000)
        (ctypes
         .windll
         .iphlpapi
         .GetAdaptersInfo(ctypes.byref(buf),
                          ctypes.byref(buf_len)))
        adapter = IP_ADAPTER_INFO.from_address(ctypes.addressof(buf))
        while True:
            mac = ':'.join(['%02x' % x for x in adapter.Address][:6])
            ifname = (ctypes
                      .string_at(ctypes.addressof(adapter.AdapterName))
                      .decode('utf-8'))
            spec = {'index': adapter.Index,
                    'attrs': (['IFLA_ADDRESS', mac],
                              ['IFLA_IFNAME', ifname])}

            msg = ifinfmsg().load(spec)
            del msg['value']
            ret['interfaces'].append(msg)

            ipaddr = adapter.IpAddressList
            while True:
                addr = (ctypes
                        .string_at(ctypes.addressof(ipaddr.IpAddress))
                        .decode('utf-8'))
                mask = (ctypes
                        .string_at(ctypes.addressof(ipaddr.IpMask))
                        .decode('utf-8'))
                spec = {'index': adapter.Index,
                        'family': AF_INET,
                        'prefixlen': dqn2int(mask),
                        'attrs': (['IFA_ADDRESS', addr],
                                  ['IFA_LOCAL', addr],
                                  ['IFA_LABEL', ifname])}
                msg = ifaddrmsg().load(spec)
                del msg['value']
                ret['addresses'].append(msg)
                if ipaddr.Next:
                    ipaddr = ipaddr.Next.contents
                else:
                    break

            if adapter.Next:
                adapter = adapter.Next.contents
            else:
                break
        return ret
Esempio n. 10
0
def convert_ifa_msg(msg):
    ret = ifaddrmsg()
    ret['header']['type'] = RTNL_NEWADDR if \
        msg['header']['type'] == RTM_NEWADDR else \
        RTNL_DELADDR
    ret['index'] = msg['IFP']['index']
    ret['family'] = msg['IFA']['header']['family']
    ret['prefixlen'] = dqn2int(msg['NETMASK']['address'], ret['family'])
    ret['attrs'] = [['IFA_ADDRESS', msg['IFA']['address']],
                    ['IFA_BROADCAST', msg['BRD']['address']],
                    ['IFA_LABEL', msg['IFP']['ifname']]]
    del ret['value']
    return ret
Esempio n. 11
0
def convert_ifa_msg(msg):
    ret = ifaddrmsg()
    ret['header']['type'] = RTNL_NEWADDR if \
        msg['header']['type'] == RTM_NEWADDR else \
        RTNL_DELADDR
    ret['index'] = msg['IFP']['index']
    ret['family'] = msg['IFA']['header']['family']
    ret['prefixlen'] = dqn2int(msg['NETMASK']['address'], ret['family'])
    ret['attrs'] = [['IFA_ADDRESS', msg['IFA']['address']],
                    ['IFA_BROADCAST', msg['BRD']['address']],
                    ['IFA_LABEL', msg['IFP']['ifname']]]
    del ret['value']
    return ret
Esempio n. 12
0
 def del_ip(self, ip, mask=None):
     '''
     Delete IP address from an interface
     '''
     if mask is None:
         ip, mask = ip.split('/')
         if mask.find('.') > -1:
             mask = dqn2int(mask)
         else:
             mask = int(mask, 0)
     if (ip, mask) in self['ipaddr']:
         self['ipaddr'].unlink((ip, mask))
         self['ipaddr'].remove((ip, mask))
Esempio n. 13
0
 def del_ip(self, ip, mask=None):
     '''
     Delete IP address from an interface
     '''
     if mask is None:
         ip, mask = ip.split('/')
         if mask.find('.') > -1:
             mask = dqn2int(mask)
         else:
             mask = int(mask, 0)
     if (ip, mask) in self['ipaddr']:
         self['ipaddr'].unlink((ip, mask))
         self['ipaddr'].remove((ip, mask))
Esempio n. 14
0
    def _GetAdaptersInfo(self):
        ret = {'interfaces': [],
               'addresses': []}

        # prepare buffer
        buf = ctypes.create_string_buffer(15000)
        buf_len = ctypes.c_ulong(15000)
        (ctypes
         .windll
         .iphlpapi
         .GetAdaptersInfo(ctypes.byref(buf),
                          ctypes.byref(buf_len)))
        adapter = IP_ADAPTER_INFO.from_address(ctypes.addressof(buf))
        while True:
            mac = ':'.join(['%02x' % x for x in adapter.Address][:6])
            ifname = ctypes.string_at(ctypes.addressof(adapter.AdapterName))
            spec = {'index': adapter.Index,
                    'attrs': (['IFLA_ADDRESS', mac],
                              ['IFLA_IFNAME', ifname])}

            msg = ifinfmsg().load(spec)
            del msg['value']
            ret['interfaces'].append(msg)

            ipaddr = adapter.IpAddressList
            while True:
                addr = ctypes.string_at(ctypes.addressof(ipaddr.IpAddress))
                mask = ctypes.string_at(ctypes.addressof(ipaddr.IpMask))
                spec = {'index': adapter.Index,
                        'family': AF_INET,
                        'prefixlen': dqn2int(mask),
                        'attrs': (['IFA_ADDRESS', addr],
                                  ['IFA_LOCAL', addr],
                                  ['IFA_LABEL', ifname])}
                msg = ifaddrmsg().load(spec)
                del msg['value']
                ret['addresses'].append(msg)
                if ipaddr.Next:
                    ipaddr = ipaddr.Next.contents
                else:
                    break

            if adapter.Next:
                adapter = adapter.Next.contents
            else:
                break
        return ret
Esempio n. 15
0
 def del_ip(self, direct, ip, mask=None):
     '''
     Delete IP address from an interface
     '''
     if mask is None:
         ip, mask = ip.split('/')
         if mask.find('.') > -1:
             mask = dqn2int(mask)
         else:
             mask = int(mask, 0)
     if not direct:
         transaction = self.last()
         if (ip, mask) in transaction['ipaddr']:
             transaction.del_ip(ip, mask)
     else:
         self['ipaddr'].unlink((ip, mask))
         self['ipaddr'].remove((ip, mask))
     return self
Esempio n. 16
0
 def del_ip(self, direct, ip, mask=None):
     '''
     Delete IP address from an interface
     '''
     if mask is None:
         ip, mask = ip.split('/')
         if mask.find('.') > -1:
             mask = dqn2int(mask)
         else:
             mask = int(mask, 0)
     if not direct:
         transaction = self.last()
         if (ip, mask) in transaction['ipaddr']:
             transaction.del_ip(ip, mask)
     else:
         self['ipaddr'].unlink((ip, mask))
         self['ipaddr'].remove((ip, mask))
     return self
Esempio n. 17
0
 def add_ip(self, direct, ip, mask=None):
     '''
     Add IP address to an interface
     '''
     # split mask
     if mask is None:
         ip, mask = ip.split('/')
         if mask.find('.') > -1:
             mask = dqn2int(mask)
         else:
             mask = int(mask, 0)
     # FIXME: make it more generic
     # skip IPv6 link-local addresses
     if ip[:4] == 'fe80' and mask == 64:
         return self
     if not direct:
         transaction = self.last()
         transaction.add_ip(ip, mask)
     else:
         self['ipaddr'].unlink((ip, mask))
         self['ipaddr'].add((ip, mask))
     return self
Esempio n. 18
0
 def test_dqn2int(self):
     assert dqn2int('255.255.255.0') == 24
     assert dqn2int('255.240.0.0') == 12
     assert dqn2int('255.0.0.0') == 8
Esempio n. 19
0
 def test_dqn2int(self):
     assert dqn2int('255.255.255.0') == 24
     assert dqn2int('255.240.0.0') == 12
     assert dqn2int('255.0.0.0') == 8
Esempio n. 20
0
def norm_mask(value):
    if isinstance(value, basestring):
        if '.' in value:
            value = dqn2int(value)
        value = int(value)
    return value