Skip to content

Commit 25b2b7f

Browse files
committed
[server] add mDNS support
1 parent 00b55e1 commit 25b2b7f

File tree

1 file changed

+52
-1
lines changed

1 file changed

+52
-1
lines changed

libraries/dnssd_loader/dnssd_loader.cpp

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
#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+
28

39
DNSServiceErrorType DNSSD_API DNSServiceRegister
410
(
@@ -15,8 +21,53 @@ DNSServiceErrorType DNSSD_API DNSServiceRegister
1521
DNSServiceRegisterReply callBack, /* may be NULL */
1622
void *context /* may be NULL */
1723
) {
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());
1852

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+
}
2071
return 0;
2172
}
2273

0 commit comments

Comments
 (0)