"""Exercise real SDK DHCP against an isolated loopback peer; installs no OS routes.""" import ctypes import json import socket import sys import time import uuid from pathlib import Path def main(): root = Path(__file__).resolve().parents[2] library = Path(sys.argv[1]) if len(sys.argv) > 1 else root / 'build/meshport-runtime/release/libmetin2_meshport.dylib' ffi = ctypes.CDLL(str(library)) ffi.meshport_context_new.restype = ctypes.c_uint64 ffi.meshport_call.argtypes = [ctypes.c_uint64, ctypes.c_char_p] ffi.meshport_call.restype = ctypes.c_void_p ffi.meshport_string_free.argtypes = [ctypes.c_void_p] ffi.meshport_context_free.argtypes = [ctypes.c_uint64] def call(context, **request): pointer = ffi.meshport_call(context, json.dumps(request).encode()) assert pointer, 'missing SDK response' try: response = json.loads(ctypes.string_at(pointer)) finally: ffi.meshport_string_free(pointer) assert response['ok'], 'SDK request failed: ' + request['op'] return response['data'] with socket.socket() as listener: listener.bind(('127.0.0.1', 0)) port = listener.getsockname()[1] identity = 'metin2-isolated-test-' + uuid.uuid4().hex base = dict(network_name=identity, network_secret=uuid.uuid4().hex, runtime_mode='application', stun_servers=[], stun_servers_v6=[], listeners=[], enable_ipv6=False) contexts = [ffi.meshport_context_new(), ffi.meshport_context_new()] try: info = call(contexts[0], op='abi_info') assert info['abi'] == 1 and info['revision'] >= 2 server = dict(base, hostname='dhcp-seed', ipv4='10.253.85.1/24', dhcp=False, listeners=['tcp://127.0.0.1:%d' % port]) client = dict(base, hostname='dhcp-client', ipv4='', dhcp=True, peers=['tcp://127.0.0.1:%d' % port]) for context, profile in zip(contexts, (server, client)): config = call(context, op='build_config', profile=profile)['toml'] call(context, op='start_bridged', toml=config) deadline = time.monotonic() + 40 while time.monotonic() < deadline: data = call(contexts[1], op='status') overlay = data.get('platform_network', {}).get('overlay') or {} address = overlay.get('address') if address and address != '0.0.0.0' and data.get('health', {}).get('mesh_routable'): assert address.startswith('10.253.85.') and address != '10.253.85.1' assert data.get('nodes'), 'DHCP node must have joined seed network' print('Real SDK DHCP passed: assigned %s/%s; application bridge healthy' % (address, overlay['prefix'])) break time.sleep(.25) else: raise AssertionError('DHCP did not assign an address before deadline') finally: for context in reversed(contexts): ffi.meshport_context_free(context) print('SDK contexts and loopback listeners stopped') if __name__ == '__main__': main()