Hi,

What do you mean "arbitrary bitmask"?

I guess netaddr.ip.IPNetwork() can resolve IPv4/v6 address with mask.
 (http://pythonhosted.org//netaddr/tutorial_01.html)

e.g.) IPv4 address
$ python 
Python 2.7.6 (default, Mar 22 2014, 22:59:56) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from netaddr import *
>>> ip = IPNetwork("192.168.0.1/255.255.255.0")
>>> print ip
192.168.0.1/24
>>> print ip.ip
192.168.0.1
>>> print ip.network
192.168.0.0
>>> print ip.netmask
255.255.255.0
>>> ip = IPNetwork("192.168.0.1/24")
>>> print ip
192.168.0.1/24
>>> print ip.ip
192.168.0.1
>>> print ip.network
192.168.0.0
>>> print ip.netmask
255.255.255.0

What do you want to realize with your patch?

Regards,
Iwase

On 2014年12月24日 19:38, Wei-Li Tang wrote:
> Signed-off-by: Wei-Li Tang <[email protected]>
> ---
>  ryu/lib/ofctl_v1_2.py | 27 +++++++++++++++++++++++----
>  ryu/lib/ofctl_v1_3.py | 27 +++++++++++++++++++++++----
>  2 files changed, 46 insertions(+), 8 deletions(-)
> 
> diff --git a/ryu/lib/ofctl_v1_2.py b/ryu/lib/ofctl_v1_2.py
> index c8b9174..5e056d1 100644
> --- a/ryu/lib/ofctl_v1_2.py
> +++ b/ryu/lib/ofctl_v1_2.py
> @@ -17,7 +17,6 @@ import base64
>  import struct
>  import socket
>  import logging
> -import netaddr
>  
>  from ryu.ofproto import ether
>  from ryu.ofproto import inet
> @@ -284,10 +283,30 @@ def to_match_eth(value):
>  
>  
>  def to_match_ip(value):
> +    # NOTE: We don't use netaddr.ip.IPNetwork() to resolve address here
> +    #       because it doesn't support arbitrary bitmask.
>      if '/' in value:
> -        ip = netaddr.ip.IPNetwork(value)
> -        ip_addr = str(ip.ip)
> -        ip_mask = str(ip.netmask)
> +        (ip_addr, ip_mask) = value.split('/')
> +
> +        # Convert ip_mask into dotted-decimal or colon-hexadecimal format
> +        # if value is represented in CIDR notation.
> +        if ip_mask.isdigit():
> +            # Identify the family of the address.
> +            if ':' in ip_addr:
> +                # IPv6 CIDR
> +                UINT128_MAX = (1 << 128) - 1
> +                ip_mask_int = UINT128_MAX & UINT128_MAX << 128 - int(ip_mask)
> +                ip_mask_h = ip_mask_int >> 64
> +                ip_mask_l = ip_mask_int & ofproto_v1_2_parser.UINT64_MAX
> +                ip_mask = socket.inet_ntop(socket.AF_INET6,
> +                                           struct.pack('!2Q',
> +                                                       ip_mask_h, ip_mask_l))
> +            else:
> +                # IPv4 CIDR
> +                ip_mask_int = ofproto_v1_2_parser.UINT32_MAX & \
> +                    ofproto_v1_2_parser.UINT32_MAX << 32 - int(ip_mask)
> +                ip_mask = socket.inet_ntoa(struct.pack('!I', ip_mask_int))
> +
>          return ip_addr, ip_mask
>      else:
>          return value
> diff --git a/ryu/lib/ofctl_v1_3.py b/ryu/lib/ofctl_v1_3.py
> index 485a3b4..698d7b1 100644
> --- a/ryu/lib/ofctl_v1_3.py
> +++ b/ryu/lib/ofctl_v1_3.py
> @@ -17,7 +17,6 @@ import base64
>  import struct
>  import socket
>  import logging
> -import netaddr
>  
>  from ryu.ofproto import ether
>  from ryu.ofproto import inet
> @@ -305,10 +304,30 @@ def to_match_eth(value):
>  
>  
>  def to_match_ip(value):
> +    # NOTE: We don't use netaddr.ip.IPNetwork() to resolve address here
> +    #       because it doesn't support arbitrary bitmask.
>      if '/' in value:
> -        ip = netaddr.ip.IPNetwork(value)
> -        ip_addr = str(ip.ip)
> -        ip_mask = str(ip.netmask)
> +        (ip_addr, ip_mask) = value.split('/')
> +
> +        # Convert ip_mask into dotted-decimal or colon-hexadecimal format
> +        # if value is represented in CIDR notation.
> +        if ip_mask.isdigit():
> +            # Identify the family of the address.
> +            if ':' in ip_addr:
> +                # IPv6 CIDR
> +                UINT128_MAX = (1 << 128) - 1
> +                ip_mask_int = UINT128_MAX & UINT128_MAX << 128 - int(ip_mask)
> +                ip_mask_h = ip_mask_int >> 64
> +                ip_mask_l = ip_mask_int & ofproto_v1_3_parser.UINT64_MAX
> +                ip_mask = socket.inet_ntop(socket.AF_INET6,
> +                                           struct.pack('!2Q',
> +                                                       ip_mask_h, ip_mask_l))
> +            else:
> +                # IPv4 CIDR
> +                ip_mask_int = ofproto_v1_3_parser.UINT32_MAX & \
> +                    ofproto_v1_3_parser.UINT32_MAX << 32 - int(ip_mask)
> +                ip_mask = socket.inet_ntoa(struct.pack('!I', ip_mask_int))
> +
>          return ip_addr, ip_mask
>      else:
>          return value
> 

------------------------------------------------------------------------------
Dive into the World of Parallel Programming! The Go Parallel Website,
sponsored by Intel and developed in partnership with Slashdot Media, is your
hub for all things parallel software development, from weekly thought
leadership blogs to news, videos, case studies, tutorials and more. Take a
look and join the conversation now. http://goparallel.sourceforge.net
_______________________________________________
Ryu-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ryu-devel

Reply via email to