~libra-core/libra/master

« back to all changes in this revision

Viewing changes to libra/api/library/ip_filter.py

  • Committer: Monty Taylor
  • Date: 2015-10-17 20:03:27 UTC
  • Revision ID: git-v1:c7082fa72ac73b23b48ce63fc82aa7da2d3e5d6a
Retire stackforge/libra

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright 2013 Hewlett-Packard Development Company, L.P.
2
 
#
3
 
# Licensed under the Apache License, Version 2.0 (the "License"); you may
4
 
# not use this file except in compliance with the License. You may obtain
5
 
# a copy of the License at
6
 
#
7
 
#      http://www.apache.org/licenses/LICENSE-2.0
8
 
#
9
 
# Unless required by applicable law or agreed to in writing, software
10
 
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11
 
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12
 
# License for the specific language governing permissions and limitations
13
 
# under the License.
14
 
 
15
 
import ipaddress
16
 
from libra.api.library.exp import IPOutOfRange
17
 
 
18
 
 
19
 
def ipfilter(address, masks):
20
 
    address = ipaddress.IPv4Address(address)
21
 
    if masks and len(masks) > 0:
22
 
        in_mask = False
23
 
        for mask in masks:
24
 
            if address in ipaddress.IPv4Network(unicode(mask), True):
25
 
                in_mask = True
26
 
                break
27
 
        if not in_mask:
28
 
            raise IPOutOfRange('IP Address not in mask')
29
 
    return str(address)