1 # Copyright (C) 2011 Nippon Telegraph and Telephone Corporation.
2 # Copyright (C) 2011 Isaku Yamahata <yamahata at valinux co jp>
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
19 from ryu.lib import addrconv
26 # string representation
27 HADDR_PATTERN = r'([0-9a-f]{2}:){5}[0-9a-f]{2}'
29 DONTCARE = b'\x00' * 6
30 BROADCAST = b'\xff' * 6
31 DONTCARE_STR = '00:00:00:00:00:00'
32 BROADCAST_STR = 'ff:ff:ff:ff:ff:ff'
33 MULTICAST = 'fe:ff:ff:ff:ff:ff'
34 UNICAST = '01:00:00:00:00:00'
37 def is_multicast(addr):
38 return bool(_ord(addr[0]) & 0x01)
41 def haddr_to_str(addr):
42 """Format mac address in internal representation into human readable
47 return addrconv.mac.bin_to_text(addr)
52 def haddr_to_int(addr):
53 """Convert mac address string in human readable format into
56 return int(addr.replace(':', ''), 16)
61 def haddr_to_bin(string):
62 """Parse mac address string in human readable format into
63 internal representation"""
65 return addrconv.mac.text_to_bin(string)
70 def haddr_bitand(addr, mask):
71 return b''.join(six.int2byte(_ord(a) & _ord(m)) for (a, m)