Пример #1
0
 def clean(self):
     cleaned_data = super(NetworkForm_network, self).clean()
     network_str = cleaned_data.get('network', '')
     try:
         ip_type = cleaned_data.get('ip_type')
         if ip_type not in IP_TYPES:
             raise ValidationError("IP type must be either IPv4 or IPv6.")
         elif ip_type == IP_TYPE_4:
             network = ipaddr.IPv4Network(network_str)
             ip_upper, ip_lower = 0, int(network.network)
         elif ip_type == IP_TYPE_4:
             network = ipaddr.IPv6Network(network_str)
             ip_upper, ip_lower = ipv6_to_longs(network.network)
     except ipaddr.AddressValueError, e:
         raise ValidationError("Bad Ip address {0}".format(e))
Пример #2
0
 def clean(self):
     cleaned_data = super(NetworkForm_network, self).clean()
     network_str = cleaned_data.get('network', '')
     try:
         ip_type = cleaned_data.get('ip_type')
         if ip_type not in IP_TYPES:
             raise ValidationError("IP type must be either IPv4 or IPv6.")
         elif ip_type == IP_TYPE_4:
             network = ipaddr.IPv4Network(network_str)
             ip_upper, ip_lower = 0, int(network.network)
         elif ip_type == IP_TYPE_4:
             network = ipaddr.IPv6Network(network_str)
             ip_upper, ip_lower = ipv6_to_longs(network.network)
     except ipaddr.AddressValueError, e:
         raise ValidationError("Bad IP address {0}".format(e))
Пример #3
0
    def clean(self):
        cleaned_data = super(NetworkForm, self).clean()
        network_str = cleaned_data.get('network_str', '')
        try:
            ip_type = cleaned_data.get('ip_type')
            if ip_type not in IP_TYPES:
                raise ValidationError("IP type must be either IPv4 or IPv6.")
            if ip_type == IP_TYPE_4:
                network = ipaddr.IPv4Network(network_str)
                ip_upper, ip_lower = 0, int(network.network)
            elif ip_type == IP_TYPE_6:
                network = ipaddr.IPv6Network(network_str)
                ip_upper, ip_lower = ipv6_to_longs(network.network)

            if cleaned_data["routers"]:
                for key in ["gateway", "subnet_mask"]:
                    value = cleaned_data.get(key)
                    if value:
                        if ip_type == IP_TYPE_4:
                            value = ipaddr.IPv4Network(value)
                            size = 32
                        elif ip_type == IP_TYPE_6:
                            value = ipaddr.IPv6Network(value)
                            size = 128
                        if key == "gateway" and not network.overlaps(value):
                            raise ValidationError("Network does not contain "
                                                  "specified gateway.")
                        if key == "subnet_mask":
                            binstring = bin(int(value.broadcast))
                            binstring = "".join(binstring[2:])
                            if "01" in binstring or len(binstring) != size:
                                raise ValidationError("Invalid subnet mask.")
                            prefixlen = binstring.count('1')
                            if prefixlen > network.prefixlen:
                                raise ValidationError(
                                    "Subnet mask is smaller than network.")
                        value = str(value.broadcast)
                        cleaned_data[key] = value

        except ipaddr.AddressValueError, e:
            raise ValidationError("Bad IP address {0}".format(e))
Пример #4
0
    def clean(self):
        cleaned_data = super(NetworkForm, self).clean()
        network_str = cleaned_data.get('network_str', '')
        try:
            ip_type = cleaned_data.get('ip_type')
            if ip_type not in IP_TYPES:
                raise ValidationError("IP type must be either IPv4 or IPv6.")
            if ip_type == IP_TYPE_4:
                network = ipaddr.IPv4Network(network_str)
                ip_upper, ip_lower = 0, int(network.network)
            elif ip_type == IP_TYPE_6:
                network = ipaddr.IPv6Network(network_str)
                ip_upper, ip_lower = ipv6_to_longs(network.network)

            if cleaned_data["routers"]:
                for key in ["gateway", "subnet_mask"]:
                    value = cleaned_data.get(key)
                    if value:
                        if ip_type == IP_TYPE_4:
                            value = ipaddr.IPv4Network(value)
                            size = 32
                        elif ip_type == IP_TYPE_6:
                            value = ipaddr.IPv6Network(value)
                            size = 128
                        if key == "gateway" and not network.overlaps(value):
                            raise ValidationError("Network does not contain "
                                                  "specified gateway.")
                        if key == "subnet_mask":
                            binstring = bin(int(value.broadcast))
                            binstring = "".join(binstring[2:])
                            if "01" in binstring or len(binstring) != size:
                                raise ValidationError("Invalid subnet mask.")
                            prefixlen = binstring.count('1')
                            if prefixlen > network.prefixlen:
                                raise ValidationError(
                                    "Subnet mask is smaller than network.")
                        value = str(value.broadcast)
                        cleaned_data[key] = value

        except ipaddr.AddressValueError, e:
            raise ValidationError("Bad IP address {0}".format(e))