1. Victor Stinner
  2. python-ptrace

Commits

Victor Stinner  committed 0e73695

Support sockaddr_in6 (IPv6 address)

  • Participants
  • Parent commits d295179
  • Branches default

Comments (0)

Files changed (4)

File ChangeLog

View file
  • Ignore whitespace
 Changelog
 =========
 
+python-ptrace 0.6.4
+-------------------
+
+ * Support sockaddr_in6 (IPv6 address)
+
 python-ptrace 0.6.3 (2011-02-16)
 --------------------------------
 

File ptrace/syscall/socketcall.py

View file
  • Ignore whitespace
 from ptrace.ctypes_tools import ntoh_ushort, ntoh_uint
 from ptrace.syscall import SYSCALL_PROTOTYPES
 from ptrace.syscall.socketcall_constants import SOCKETCALL, SOCKET_FAMILY
-from ptrace.syscall.socketcall_struct import sockaddr, sockaddr_in, sockaddr_un
+from ptrace.syscall.socketcall_struct import sockaddr, sockaddr_in, sockaddr_in6, sockaddr_un
 from ctypes import c_int
 from ptrace.os_tools import RUNNING_LINUX
-from socket import AF_INET, inet_ntoa
+from socket import AF_INET, AF_INET6, inet_ntoa
 from struct import pack
 if RUNNING_LINUX:
     from socket import AF_NETLINK
     from ptrace.syscall.socketcall_struct import sockaddr_nl
 
-def ip_int2str(ip):
-    """
-    Convert an IP address (as an interger) to a string.
-
-    >>> ip_int2str(0x7f000001)
-    '127.0.0.1'
-    """
-    ip_bytes = pack("!I", ip)
-    return inet_ntoa(ip_bytes)
-
 AF_FILE = 1
 
 def formatOptVal(argument):
     family = value.family
     if family == AF_INET:
         return argument.readStruct(address, sockaddr_in)
+    if family == AF_INET6:
+        return argument.readStruct(address, sockaddr_in6)
     if family == AF_FILE:
         return argument.readStruct(address, sockaddr_un)
     if RUNNING_LINUX:
 def formatSockaddrInStruct(argument, name, value):
     if name == "sin_port":
         return ntoh_ushort(value)
-    if name == "sin_addr":
-        ip = ntoh_uint(value.s_addr)
-        return ip_int2str(ip)
     return None
 
+def formatSockaddrIn6Struct(argument, name, value):
+    if name == "sin6_port":
+        return ntoh_ushort(value)
+    #if name == "sin6_addr":
+        # FIXME: ...
+    return None
+

File ptrace/syscall/socketcall_struct.py

View file
  • Ignore whitespace
-from ctypes import Structure, c_char, c_ushort, c_ubyte, c_uint16, c_uint32
+from ctypes import Structure, Union, c_char, c_ushort, c_ubyte, c_uint16, c_uint32
 from ptrace.os_tools import RUNNING_BSD, RUNNING_LINUX
+from socket import inet_ntoa
+from struct import pack
+from ptrace.ctypes_tools import ntoh_uint, ntoh_ushort
+
+def ip_int2str(ip):
+    """
+    Convert an IP address (as an interger) to a string.
+
+    >>> ip_int2str(0x7f000001)
+    '127.0.0.1'
+    """
+    ip_bytes = pack("!I", ip)
+    return inet_ntoa(ip_bytes)
 
 if RUNNING_BSD:
     sa_family_t = c_ubyte
         ("s_addr", c_uint32),
     )
 
+    def __repr__(self):
+        ip = ntoh_uint(self.s_addr)
+        return ip_int2str(ip)
+
+class in6_addr(Union):
+    _fields_ = (
+        ("addr8", c_ubyte * 16),
+        ("addr16", c_uint16 * 8),
+        ("addr32", c_uint32 * 4),
+    )
+
+    def __repr__(self):
+        text = ':'.join(("%04x" % ntoh_ushort(part)) for part in self.addr16)
+        return "<in6_addrr %s>" % text
+
 # INET socket
 class sockaddr_in(Structure):
     if RUNNING_BSD:
             ("sin_addr", in_addr),
         )
 
+class sockaddr_in6(Structure):
+    if RUNNING_BSD:
+        _fields_ = (
+            ("sin6_len", c_ubyte),
+            ("sin6_family", sa_family_t),
+            ("sin6_port", c_uint16),
+            ("sin6_flowinfo", c_uint32),
+            ("sin6_addr", in6_addr),
+        )
+    else:
+        _fields_ = (
+            ("sin6_family", sa_family_t),
+            ("sin6_port", c_uint16),
+            ("sin6_flowinfo", c_uint32),
+            ("sin6_addr", in6_addr),
+            ("sin6_scope_ip", c_uint32),
+        )
+
 # UNIX socket
 class sockaddr_un(Structure):
     _fields_ = (

File ptrace/syscall/syscall_argument.py

View file
  • Ignore whitespace
     formatMmapProt, formatAccessMode, formatOpenMode, formatCloneFlags)
 from ptrace.func_call import FunctionCall
 from ptrace.syscall.socketcall import (setupSocketCall,
-    formatOptVal, formatSockaddr, formatSockaddrInStruct)
+    formatOptVal, formatSockaddr, formatSockaddrInStruct, formatSockaddrIn6Struct)
 from ptrace.syscall.socketcall_constants import SOCKETCALL
 import re
 from ptrace.os_tools import RUNNING_LINUX, RUNNING_FREEBSD
 STRUCT_CALLBACK = {
     # Prototype: callback(argument, attr_name, attr_value) -> str
     "sockaddr_in": formatSockaddrInStruct,
+    "sockaddr_in6": formatSockaddrIn6Struct,
 }
 
 INTEGER_TYPES = set((