1
1
#include " dns_sd.h"
2
+ #include < iostream>
3
+ #include < string>
4
+ #include < unistd.h>
5
+ #include < sys/prctl.h> // prctl(), PR_SET_PDEATHSIG
6
+ #include < signal.h> // signals
7
+
2
8
3
9
DNSServiceErrorType DNSSD_API DNSServiceRegister
4
10
(
@@ -15,8 +21,53 @@ DNSServiceErrorType DNSSD_API DNSServiceRegister
15
21
DNSServiceRegisterReply callBack, /* may be NULL */
16
22
void *context /* may be NULL */
17
23
) {
24
+ // python3 -c 'from ctypes import *; sdRef = c_int(); CDLL('libdns_sd.so').DNSServiceRegister(byref(sdRef), flags, interfaceIndex, name, regtype, domain, host, txtLen, txtRecord, None, None)'
25
+ std::string pyCommand = " from ctypes import *; dll = CDLL('libdns_sd.so'); " ;
26
+
27
+ pyCommand += " sdRef = c_int(); " ;
28
+ #define INT_ARG (argname ) (std::string(" " ) + #argname " = " + std::to_string(argname) + " ; " )
29
+ #define STR_ARG (argname ) (argname ? std::string(#argname " = br'" ) + argname + " '; " : std::string(#argname " = None; " ))
30
+ pyCommand += INT_ARG (flags);
31
+ pyCommand += INT_ARG (interfaceIndex);
32
+ pyCommand += STR_ARG (name);
33
+ pyCommand += STR_ARG (regtype);
34
+ pyCommand += STR_ARG (domain);
35
+ pyCommand += STR_ARG (host);
36
+ pyCommand += INT_ARG (port);
37
+ pyCommand += INT_ARG (txtLen);
38
+
39
+ std::string txtRecordHex = " " ;
40
+ for (int i = 0 ; i < txtLen; i++) {
41
+ char buf[16 ] = { 0 };
42
+ sprintf (buf, " \\ x%02X" , *((char *)txtRecord + i));
43
+ txtRecordHex += buf;
44
+ }
45
+ pyCommand += " txtRecord = b'" + txtRecordHex + " '; " ;
46
+ pyCommand += " ret = dll.DNSServiceRegister(byref(sdRef), flags, interfaceIndex, name, regtype, domain, host, port, txtLen, txtRecord, None, None); " ;
47
+ pyCommand += " print('DNSServiceRegister result: %d' % ret); " ;
48
+
49
+ pyCommand += " from threading import Event; Event().wait(); " ;
50
+
51
+ printf (" Running python3 command to advertise AltServer: %s\n " , pyCommand.c_str ());
18
52
19
- // XXX TODO: Wait for usbmuxd linux to support network device
53
+ pid_t ppid_before_fork = getpid ();
54
+ int child,status;
55
+ if ((child = fork ()) < 0 ) {
56
+ perror (" fork" );
57
+ return EXIT_FAILURE;
58
+ }
59
+ if (child == 0 ){
60
+ int r = prctl (PR_SET_PDEATHSIG, SIGTERM);
61
+ if (r == -1 ) { perror (0 ); exit (1 ); }
62
+ // test in case the original parent exited just
63
+ // before the prctl() call
64
+ if (getppid () != ppid_before_fork)
65
+ exit (1 );
66
+ execlp (" python3" , " python3" , " -c" , pyCommand.c_str (), NULL );
67
+ exit (1 );
68
+ } else {
69
+ ;
70
+ }
20
71
return 0 ;
21
72
}
22
73
0 commit comments