Improved integration of ws2_32 API + add demo_ws2_32.py sample

This commit is contained in:
clement rouault
2022-10-31 10:18:23 +01:00
parent 5d9e9a240f
commit c1aad71dd3
4 changed files with 42 additions and 1 deletions
@@ -7,6 +7,7 @@
// INVALID_SOCKET (SOCKET)(~0) // will change in 32 & 64 bits..
// The final INVALID_SOCKET is setuped in __init__.py of generated def
#define INVALID_SOCKET32 (0xffffffff)
#define INVALID_SOCKET64 (0xffffffffffffffff)
+36
View File
@@ -0,0 +1,36 @@
import windows.generated_def as gdef
import windows.winproxy
import ctypes
WEBSITE = b"perdu.com"
MESSAGE = b"""GET / HTTP/1.1\r\nHost: perdu.com\r\n\r\n"""
x = gdef.WSADATA()
windows.winproxy.WSAStartup(0x0202, x)
print("=== WSADATA ===")
windows.utils.sprint(x)
hints = gdef.ADDRINFOA()
hints.ai_family = gdef.AF_UNSPEC
hints.ai_socktype = gdef.SOCK_STREAM
hints.ai_protocol = gdef.IPPROTO_TCP
result = gdef.PADDRINFOA()
windows.winproxy.getaddrinfo(WEBSITE, b"80", hints, result);
print("=== PADDRINFOA ===")
windows.utils.sprint(result)
connect_socket = windows.winproxy.socket(result[0].ai_family, result[0].ai_socktype, result[0].ai_protocol)
res = windows.winproxy.connect(connect_socket, result[0].ai_addr, result[0].ai_addrlen)
windows.winproxy.send(connect_socket, MESSAGE)
buf = ctypes.create_string_buffer(10000)
windows.winproxy.recv(connect_socket, buf)
print("Received:\n{0}".format(buf.value))
windows.winproxy.closesocket(connect_socket)
windows.winproxy.WSACleanup()
+2
View File
@@ -29,6 +29,7 @@ if bitness() == 32:
winstructs.IRQ_RESOURCE = winstructs.IRQ_RESOURCE_32
# Socket
windef.WSADATA = winstructs.WSADATA32
windef.INVALID_SOCKET = windef.INVALID_SOCKET32
@@ -53,6 +54,7 @@ else:
winstructs.IRQ_RESOURCE = winstructs.IRQ_RESOURCE_64
# Socket
windef.WSADATA = winstructs.WSADATA64
windef.INVALID_SOCKET = windef.INVALID_SOCKET64
from . import winfuncs
+3 -1
View File
@@ -18,7 +18,9 @@ def check_invalid_socket(func_name, result, func, args):
@Ws2_32Proxy()
def WSAStartup(wVersionRequested, lpWSAData):
return WSAStartup.ctypes_function(wVersionRequested, lpWSAData)
if isinstance(lpWSAData, gdef.WSADATA):
lpWSAData = ctypes.byref(lpWSAData) # Not naturally done as lpWSAData is defined as a PVOID due to WSADATA32/WSADATA64 types
return WSAStartup.ctypes_function(wVersionRequested, lpWSAData)
@Ws2_32Proxy()