diff options
author | Nikias Bassen | 2018-10-14 03:11:38 +0200 |
---|---|---|
committer | Nikias Bassen | 2018-10-14 03:11:38 +0200 |
commit | 80fe6e859302cb771bb6b6f10feb1766d765778b (patch) | |
tree | d8a0c4408dc312f0b137d60fed9ff0b77fdca684 /src | |
parent | f5a7387a54ae08c9cd1d83a415393e0e909dc6e6 (diff) | |
download | libusbmuxd-80fe6e859302cb771bb6b6f10feb1766d765778b.tar.gz libusbmuxd-80fe6e859302cb771bb6b6f10feb1766d765778b.tar.bz2 |
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.
Diffstat (limited to 'src')
-rw-r--r-- | src/libusbmuxd.c | 44 |
1 files changed, 44 insertions, 0 deletions
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 |