backing up
[vsorcdistro/.git] / ryu / build / lib.linux-armv7l-2.7 / ryu / lib / mac.py
1 # Copyright (C) 2011 Nippon Telegraph and Telephone Corporation.
2 # Copyright (C) 2011 Isaku Yamahata <yamahata at valinux co jp>
3 #
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
7 #
8 #    http://www.apache.org/licenses/LICENSE-2.0
9 #
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
13 # implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 import six
18
19 from ryu.lib import addrconv
20
21 if six.PY3:
22     _ord = int
23 else:
24     _ord = ord
25
26 # string representation
27 HADDR_PATTERN = r'([0-9a-f]{2}:){5}[0-9a-f]{2}'
28
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'
35
36
37 def is_multicast(addr):
38     return bool(_ord(addr[0]) & 0x01)
39
40
41 def haddr_to_str(addr):
42     """Format mac address in internal representation into human readable
43     form"""
44     if addr is None:
45         return 'None'
46     try:
47         return addrconv.mac.bin_to_text(addr)
48     except:
49         raise AssertionError
50
51
52 def haddr_to_int(addr):
53     """Convert mac address string in human readable format into
54     integer value"""
55     try:
56         return int(addr.replace(':', ''), 16)
57     except:
58         raise ValueError
59
60
61 def haddr_to_bin(string):
62     """Parse mac address string in human readable format into
63     internal representation"""
64     try:
65         return addrconv.mac.text_to_bin(string)
66     except:
67         raise ValueError
68
69
70 def haddr_bitand(addr, mask):
71     return b''.join(six.int2byte(_ord(a) & _ord(m)) for (a, m)
72                     in zip(addr, mask))