From 80fe6e859302cb771bb6b6f10feb1766d765778b Mon Sep 17 00:00:00 2001 From: Nikias Bassen Date: Sun, 14 Oct 2018 03:11:38 +0200 Subject: Allow using non-standard usbmuxd socket address via environment variable By using USBMUXD_SOCKET_ADDRESS environment variable, it is possible to make libusbmuxd connect to the specified address. The value needs to be in format ADDRESS:PORT (or UNIX:PATH on unix systems). If no port number is specified or parsing fails, the standard socket address (or unix domain socket file path) will be used silently. --- src/libusbmuxd.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) (limited to 'src') diff --git a/src/libusbmuxd.c b/src/libusbmuxd.c index 0aaae24..30fa207 100644 --- a/src/libusbmuxd.c +++ b/src/libusbmuxd.c @@ -143,6 +143,50 @@ static usbmuxd_device_info_t *devices_find(uint32_t handle) */ static int connect_usbmuxd_socket() { + char *usbmuxd_socket_addr = getenv("USBMUXD_SOCKET_ADDRESS"); + if (usbmuxd_socket_addr) { + if (strncmp(usbmuxd_socket_addr, "UNIX:", 5) == 0) { +#if defined(WIN32) || defined(__CYGWIN__) + /* not supported, ignore */ +#else + if (usbmuxd_socket_addr[5] != '\0') { + return socket_connect_unix(usbmuxd_socket_addr+5); + } +#endif + } else { + uint16_t port = 0; + char *p = strrchr(usbmuxd_socket_addr, ':'); + if (p) { + char *endp = NULL; + long l_port = strtol(p+1, &endp, 10); + if (endp && *endp == '\0') { + if (l_port > 0 && l_port < 65536) { + port = (uint16_t)l_port; + } + } + } + if (p && port > 0) { + char *connect_addr = NULL; + if (usbmuxd_socket_addr[0] == '[') { + connect_addr = strdup(usbmuxd_socket_addr+1); + connect_addr[p - usbmuxd_socket_addr - 1] = '\0'; + p = strrchr(connect_addr, ']'); + if (p) { + *p = '\0'; + } + } else { + connect_addr = strdup(usbmuxd_socket_addr); + connect_addr[p - usbmuxd_socket_addr] = '\0'; + } + if (connect_addr && *connect_addr != '\0') { + int res = socket_connect(connect_addr, port); + free(connect_addr); + return res; + } + free(connect_addr); + } + } + } #if defined(WIN32) || defined(__CYGWIN__) return socket_connect("127.0.0.1", USBMUXD_SOCKET_PORT); #else -- cgit v1.1-32-gdbae