Debian Codegraph @

ptrace/syscall/socketcall_struct.py

raw
1 from ctypes import Structure, Union, c_char, c_ushort, c_ubyte, c_uint16, c_uint32
2 from ptrace.os_tools import RUNNING_BSD, RUNNING_LINUX
3 from socket import inet_ntoa
4 from struct import pack
5 from ptrace.ctypes_tools import ntoh_uint, ntoh_ushort
6
7
8 def ip_int2str(ip):
9 """
10 Convert an IP address (as an integer) to a string.
11
12 >>> ip_int2str(0x7f000001)
13 '127.0.0.1'
14 """
15 ip_bytes = pack("!I", ip)
16 return inet_ntoa(ip_bytes)
17
18
19 if RUNNING_BSD:
20 sa_family_t = c_ubyte
21 else:
22 sa_family_t = c_ushort
23
24
25 class sockaddr(Structure):
26 if RUNNING_BSD:
27 _fields_ = (
28 ("len", c_ubyte),
29 ("family", sa_family_t),
30 )
31 else:
32 _fields_ = (
33 ("family", sa_family_t),
34 )
35
36
37 class in_addr(Structure):
38 _fields_ = (
39 ("s_addr", c_uint32),
40 )
41
42 def __repr__(self):
43 ip = ntoh_uint(self.s_addr)
44 return ip_int2str(ip)
45
46
47 class in6_addr(Union):
48 _fields_ = (
49 ("addr8", c_ubyte * 16),
50 ("addr16", c_uint16 * 8),
51 ("addr32", c_uint32 * 4),
52 )
53
54 def __repr__(self):
55 text = ':'.join(("%04x" % ntoh_ushort(part)) for part in self.addr16)
56 return "<in6_addr %s>" % text
57
58 # INET socket
59
60
61 class sockaddr_in(Structure):
62 if RUNNING_BSD:
63 _fields_ = (
64 ("sin_len", c_ubyte),
65 ("sin_family", sa_family_t),
66 ("sin_port", c_uint16),
67 ("sin_addr", in_addr),
68 )
69 else:
70 _fields_ = (
71 ("sin_family", sa_family_t),
72 ("sin_port", c_uint16),
73 ("sin_addr", in_addr),
74 )
75
76
77 class sockaddr_in6(Structure):
78 if RUNNING_BSD:
79 _fields_ = (
80 ("sin6_len", c_ubyte),
81 ("sin6_family", sa_family_t),
82 ("sin6_port", c_uint16),
83 ("sin6_flowinfo", c_uint32),
84 ("sin6_addr", in6_addr),
85 )
86 else:
87 _fields_ = (
88 ("sin6_family", sa_family_t),
89 ("sin6_port", c_uint16),
90 ("sin6_flowinfo", c_uint32),
91 ("sin6_addr", in6_addr),
92 ("sin6_scope_ip", c_uint32),
93 )
94
95 # UNIX socket
96
97
98 class sockaddr_un(Structure):
99 _fields_ = (
100 ("sun_family", sa_family_t),
101 ("sun_path", c_char * 108),
102 )
103
104
105 # Netlink socket
106 if RUNNING_LINUX:
107 class sockaddr_nl(Structure):
108 _fields_ = (
109 ("nl_family", sa_family_t),
110 ("nl_pad", c_ushort),
111 ("nl_pid", c_uint32),
112 ("nl_groups", c_uint32),
113 )