- docstring
- add sanity check as haddr_to_bin() is used to parse user-giving string

Signed-off-by: Isaku Yamahata <[email protected]>
---
 ryu/lib/mac.py |   16 +++++++++++++---
 1 files changed, 13 insertions(+), 3 deletions(-)

diff --git a/ryu/lib/mac.py b/ryu/lib/mac.py
index 4b548d5..14218d3 100644
--- a/ryu/lib/mac.py
+++ b/ryu/lib/mac.py
@@ -14,6 +14,9 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
+# Internal representation of mac address is string[6]
+_HADDR_LEN = 6
+
 DONTCARE = '\x00' * 6
 BROADCAST = '\xff' * 6
 MULTICAST = '\xfe' + '\xff' * 5
@@ -25,9 +28,16 @@ def is_multicast(addr):
 
 
 def haddr_to_str(addr):
-    return ''.join(['%02x:' % ord(char) for char in addr[0:6]])[:-1]
+    """Format mac address in internal representation into human readable
+    form"""
+    assert len(addr) == _HADDR_LEN
+    return ':'.join(['%02x' % ord(char) for char in addr])
 
 
 def haddr_to_bin(string):
-    return ''.join(['%c' % chr(int(i, 16)) for i in
-                    string.split(':')])
+    """Parse mac address string in human readable format into
+    internal representation"""
+    hexes = string.split(':')
+    if len(hexes) != _HADDR_LEN:
+        ValueError('Invalid format for mac address: %s' % string)
+    return ''.join([chr(int(h, 16)) for h in hexes])
-- 
1.7.1.1


------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
Ryu-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ryu-devel

Reply via email to