[dts] [PATCH 1/3] framework utils: add function to convert ipaddress

Marvin Liu yong.liu at intel.com
Wed Oct 26 09:00:39 CEST 2016


Add new function to convert ip address to integer and covert integer to
ip.

Signed-off-by: Marvin Liu <yong.liu at intel.com>

diff --git a/framework/utils.py b/framework/utils.py
index edfeeca..1ecef09 100644
--- a/framework/utils.py
+++ b/framework/utils.py
@@ -33,6 +33,8 @@ import json         # json format
 import re
 import os
 import inspect
+import socket
+import struct
 
 DTS_ENV_PAT = r"DTS_*"
 
@@ -138,3 +140,23 @@ def create_mask(indexes):
         val |= 1 << int(index)
 
     return hex(val).rstrip("L")
+
+def convert_int2ip(value, ip_type):
+    if ip_type == 4:
+        ip_str = socket.inet_ntop(socket.AF_INET, struct.pack('!I', value))
+    else:
+        h = value >> 64
+        l = value & ((1 << 64) - 1)
+        ip_str = socket.inet_ntop(socket.AF_INET6, struct.pack('!QQ', h, l))
+
+    return ip_str
+
+def convert_ip2int(ip_str, ip_type):
+    if ip_type == 4:
+        ip_val = struct.unpack("!I", socket.inet_aton(ip_str))[0]
+    else:
+        _hex = socket.inet_pton(socket.AF_INET6, ip_str)
+        h, l = struct.unpack('!QQ', _hex)
+        ip_val = (h << 64) | l
+
+    return ip_val
-- 
1.9.3



More information about the dts mailing list