summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorYAMAMOTO Takashi <yamamoto@valinux.co.jp>2013-07-23 12:54:07 +0900
committerFUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>2013-07-24 04:12:02 +0900
commit80d4202c9161d903afdc22ad4f3d75aa5b7fd81b (patch)
treed4ff8fad88729155791f95a05ca3c928ac5863d2
parenta1b6d39f9dfe297c402767e980d1b5642bff57b7 (diff)
addrconv: binary <-> text conversion of mac/ipv4/ipv6 addresses
implemented as a simple wrapper of netaddr. background: the goal is to use the consistent representations of addresses throughout the tree, instead of the current ad-hoc representations like a list of int16 for ipv6 address. - netaddr style text (eg. '10.0.0.1', 'ff02::1') for ryu-app level api. - on-wire binary for internal use. this is the first step to the direction. ie. have a single implementation of address representation conversion. Signed-off-by: YAMAMOTO Takashi <yamamoto@valinux.co.jp> Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
-rw-r--r--ryu/lib/addrconv.py69
1 files changed, 69 insertions, 0 deletions
diff --git a/ryu/lib/addrconv.py b/ryu/lib/addrconv.py
new file mode 100644
index 00000000..512c1e59
--- /dev/null
+++ b/ryu/lib/addrconv.py
@@ -0,0 +1,69 @@
+#!/usr/bin/env python
+#
+# Copyright (C) 2013 Nippon Telegraph and Telephone Corporation.
+# Copyright (C) 2013 YAMAMOTO Takashi <yamamoto at valinux co jp>
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+# implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import netaddr
+
+
+class AddressConverter(object):
+ def __init__(self, addr, strat, **kwargs):
+ self._addr = addr
+ self._strat = strat
+ self._addr_kwargs = kwargs
+
+ def text_to_bin(self, text):
+ return self._addr(text, **self._addr_kwargs).packed
+
+ def bin_to_text(self, bin):
+ return str(self._addr(self._strat.packed_to_int(bin),
+ **self._addr_kwargs))
+
+ipv4 = AddressConverter(netaddr.IPAddress, netaddr.strategy.ipv4, version=4)
+ipv6 = AddressConverter(netaddr.IPAddress, netaddr.strategy.ipv6, version=6)
+
+
+class mac_mydialect(netaddr.mac_unix):
+ word_fmt = '%.2x'
+mac = AddressConverter(netaddr.EUI, netaddr.strategy.eui48, version=48,
+ dialect=mac_mydialect)
+
+if __name__ == '__main__':
+ def eq_(a, b):
+ if a != b:
+ print a
+ print b
+
+ l = ['127.0.0.1', '255.255.0.0']
+ conv = ipv4
+ b2t = conv.bin_to_text
+ t2b = conv.text_to_bin
+ for a in l:
+ eq_(b2t(t2b(a)), a)
+
+ l = ['ff02::1', 'fe80::f00b:a4ff:fe7d:f8ea', '::']
+ conv = ipv6
+ b2t = conv.bin_to_text
+ t2b = conv.text_to_bin
+ for a in l:
+ eq_(b2t(t2b(a)), a)
+
+ l = ['f2:0b:a4:01:0a:23']
+ conv = mac
+ b2t = conv.bin_to_text
+ t2b = conv.text_to_bin
+ for a in l:
+ eq_(b2t(t2b(a)), a)