Beispiel #1
0
    def to_python(self, value):
        if value is None:
            return None

        if isinstance(value, str):
            return value

        if isinstance(value, int):
            try:
                return IpToStr(value)
            except ValueError:
                raise ValidationError('"%(value)s" is not valid',
                                      params={'value': value})

        if isinstance(value, bytes):
            try:
                return IpToStr(int.from_bytes(value, 'big'))
            except ValueError:
                raise ValidationError('"%(value)s" is not valid',
                                      params={'value': value})

        raise ValidationError('"%(value)s" type is unexpected type "%(type)s"',
                              params={
                                  'value': value,
                                  'type': type
                              })
Beispiel #2
0
    def clean(self, *args, **kwargs):
        super().clean(*args, **kwargs)
        errors = {}
        try:
            subnet_ip = StrToIp(self.subnet)
            ipv4 = IpIsV4(subnet_ip)
        except ValueError:
            ipv4 = None
            errors['subnet'] = 'Invalid Ip Address'

        if self.prefix is None or self.prefix < 1:
            errors['prefix'] = 'Min Prefix is 1'

        if errors:  # no point in continuing
            raise ValidationError(errors)

        if ipv4 is not None:
            if ipv4:
                if self.prefix > 32:
                    errors['prefix'] = 'Max Prefix for ipv4 is 32'
            else:
                if self.prefix > 128:
                    errors['prefix'] = 'Max Prefix for ipv6 is 128'

            if self.gateway_offset is not None:
                (low, high) = CIDRNetworkBounds(subnet_ip, self.prefix, False,
                                                True)
                if low == high:
                    errors[
                        'gateway_offset'] = 'Gateway not possible in single host subnet'

                if self.gateway_offset < low or self.gateway_offset > high:
                    errors[
                        'gateway_offset'] = 'Must be greater than {0} and less than {1}'.format(
                            low, high)

        if errors:  # no point in continuing
            raise ValidationError(errors)

        (subnet_ip, last_ip) = CIDRNetworkBounds(subnet_ip, self.prefix, True)
        self.subnet = IpToStr(subnet_ip)
        self._max_address = IpToStr(last_ip)
        block_count = AddressBlock.objects.filter(
            subnet__gte=self.subnet, _max_address__lte=self.subnet).count()
        block_count += AddressBlock.objects.filter(
            subnet__gte=self._max_address,
            _max_address__lte=self._max_address).count()
        block_count += AddressBlock.objects.filter(
            _max_address__gte=self.subnet,
            _max_address__lte=self._max_address).count()
        block_count += AddressBlock.objects.filter(
            subnet__gte=self.subnet, subnet__lte=self._max_address).count()
        if block_count > 0:
            errors[
                'subnet'] = 'This subnet/prefix overlaps with an existing Address Block'

        if errors:
            raise ValidationError(errors)
Beispiel #3
0
    def from_db_value(self, value, expression, connection, context):
        if value is None:
            return None

        try:
            return IpToStr(value)
        except ValueError:
            raise ValidationError('"%(value)s" is not valid',
                                  params={'value': value})
Beispiel #4
0
    def to_python(self, value):
        if value is None:
            return None

        if isinstance(value, str):
            return value

        try:
            return IpToStr(value)
        except ValueError:
            raise ValidationError('"%(value)s" is not valid',
                                  params={'value': value})
Beispiel #5
0
    def lookup(ip_address):
        try:
            ip_address_ip = StrToIp(ip_address)
        except ValueError:
            return None

        ip_address = IpToStr(ip_address_ip)  # so it is in a consistant format
        try:
            address_block = AddressBlock.objects.get(
                subnet__lte=ip_address, _max_address__gte=ip_address)
        except AddressBlock.DoesNotExist:
            return None

        offset = ip_address_ip - StrToIp(address_block.subnet)
        try:
            return BaseAddress.objects.get(address_block=address_block,
                                           offset=offset)
        except BaseAddress.DoesNotExist:
            return None

        return None
Beispiel #6
0
    def ip_address(self):
        if self.address_block is None or self.offset is None:
            return None

        return IpToStr(StrToIp(self.address_block.subnet) + self.offset)
Beispiel #7
0
 def netmask(self):
     return IpToStr(CIDRNetmask(self.prefix, not self.isIpV4))
Beispiel #8
0
    def gateway_ip(self):
        if self.gateway_offset is None:
            return None

        return IpToStr(StrToIp(self.subnet) + self.gateway_offset)
Beispiel #9
0
def test_iptostr():
  assert IpToStr( 0 ) == '::'
  assert IpToStr( 1 ) == '::1'
  assert IpToStr( 10 ) == '::a'
  assert IpToStr( 65535 ) == '::ffff'
  assert IpToStr( 0, False ) == '::'
  assert IpToStr( 1, False ) == '::1'
  assert IpToStr( 10, False ) == '::a'
  assert IpToStr( 65535, False ) == '::ffff'
  assert IpToStr( 0, True ) == '::'
  assert IpToStr( 1, True ) == '::1'
  assert IpToStr( 10, True ) == '::a'
  assert IpToStr( 65535, True ) == '::ffff'
  assert IpToStr( 42540766411282592856903984951653826561 ) == '2001:db8::1'
  assert IpToStr( 42540766411282592856904266426630537217 ) == '2001:db8::1:0:0:1'
  assert IpToStr( 42540488161975842760550637900276957185 ) == '2001::1:0:0:1'
  assert IpToStr( 42540488161977051686370252529451728896 ) == '2001:0:1:0:1:0:1:0'
  assert IpToStr( 42540488161975842760550356425300246528 ) == '2001::'
  assert IpToStr( 42540766411282592856903984951653826561, False ) == '2001:db8::1'
  assert IpToStr( 42540766411282592856904266426630537217, False ) == '2001:db8::1:0:0:1'
  assert IpToStr( 42540488161975842760550637900276957185, False ) == '2001::1:0:0:1'
  assert IpToStr( 42540488161977051686370252529451728896, False ) == '2001:0:1:0:1:0:1:0'
  assert IpToStr( 42540488161975842760550356425300246528, False ) == '2001::'
  assert IpToStr( 42540766411282592856903984951653826561, True ) == '2001:db8::1'
  assert IpToStr( 42540766411282592856904266426630537217, True ) == '2001:db8::1:0:0:1'
  assert IpToStr( 42540488161975842760550637900276957185, True ) == '2001::1:0:0:1'
  assert IpToStr( 42540488161977051686370252529451728896, True ) == '2001:0:1:0:1:0:1:0'
  assert IpToStr( 42540488161975842760550356425300246528, True ) == '2001::'
  assert IpToStr( 281470681743360 ) == '0.0.0.0'
  assert IpToStr( 281470681743361 ) == '0.0.0.1'
  assert IpToStr( 281472812449793 ) == '127.0.0.1'
  assert IpToStr( 281470698652420 ) == '1.2.3.4'
  assert IpToStr( 281470681743360, False ) == '0.0.0.0'
  assert IpToStr( 281470681743361, False ) == '0.0.0.1'
  assert IpToStr( 281472812449793, False ) == '127.0.0.1'
  assert IpToStr( 281470698652420, False ) == '1.2.3.4'
  assert IpToStr( 281470681743360, True ) == ':ffff:0.0.0.0'
  assert IpToStr( 281470681743361, True ) == ':ffff:0.0.0.1'
  assert IpToStr( 281472812449793, True ) == ':ffff:127.0.0.1'
  assert IpToStr( 281470698652420, True ) == ':ffff:1.2.3.4'
  assert IpToStr( StrToIp( '1:1:1:1:2:3:4:5' ) ) == '1:1:1:1:2:3:4:5'
  assert IpToStr( StrToIp( '2:2:2:2:2:3:4:5' ) ) == '2:2:2:2:2:3:4:5'
  assert IpToStr( StrToIp( '0:0:0:0:2:3:4:5' ) ) == '::2:3:4:5'
  assert IpToStr( StrToIp( '1:2:2:2:2:3:4:5' ) ) == '1:2:2:2:2:3:4:5'
  assert IpToStr( StrToIp( '1:0:0:0:0:3:4:5' ) ) == '1::3:4:5'
  assert IpToStr( StrToIp( '1:1:1:1:0:0:4:5' ) ) == '1:1:1:1::4:5'
  assert IpToStr( StrToIp( '1:1:1:1:0:0:0:0' ) ) == '1:1:1:1::'

  with pytest.raises( ValueError ):
    IpToStr( -1 )
  with pytest.raises( ValueError ):
    IpToStr( 0x100000000000000000000000000000000 )
  with pytest.raises( ValueError ):
    IpToStr( '0' )

  assert IpToStr( None ) is None