From ce7609375646cfb1e7d490579e172c37c74a0589 Mon Sep 17 00:00:00 2001 From: Nikias Bassen Date: Wed, 1 Sep 2021 15:48:32 +0200 Subject: Remove common code in favor of new libimobiledevice-glue --- README.md | 3 +- common/Makefile.am | 5 +- common/socket.c | 1169 --------------------------------------- common/socket.h | 68 --- common/thread.c | 140 ----- common/thread.h | 76 --- common/userpref.c | 3 +- common/utils.c | 530 ------------------ common/utils.h | 64 --- configure.ac | 3 + src/Makefile.am | 2 + src/afc.h | 2 +- src/companion_proxy.c | 1 - src/companion_proxy.h | 2 +- src/debugserver.c | 3 +- src/idevice.c | 5 +- src/installation_proxy.h | 2 +- src/lockdown.c | 2 +- src/mobile_image_mounter.h | 2 +- src/notification_proxy.h | 2 +- src/preboard.h | 2 +- src/sbservices.h | 2 +- src/syslog_relay.h | 2 +- tools/Makefile.am | 54 +- tools/idevicebackup.c | 2 +- tools/idevicebackup2.c | 2 +- tools/idevicecrashreport.c | 2 +- tools/idevicedebugserverproxy.c | 4 +- tools/ideviceimagemounter.c | 2 +- tools/ideviceinfo.c | 2 +- tools/ideviceprovision.c | 2 +- tools/idevicesyslog.c | 92 +-- 32 files changed, 71 insertions(+), 2181 deletions(-) delete mode 100644 common/socket.c delete mode 100644 common/socket.h delete mode 100644 common/thread.c delete mode 100644 common/thread.h delete mode 100644 common/utils.c delete mode 100644 common/utils.h diff --git a/README.md b/README.md index 2f561aa..d838453 100644 --- a/README.md +++ b/README.md @@ -62,6 +62,7 @@ sudo apt-get install \ libtool-bin \ libplist-dev \ libusbmuxd-dev \ + libimobiledevice-glue-dev \ libssl-dev \ usbmuxd ``` @@ -188,4 +189,4 @@ iPadOS, tvOS, watchOS, and macOS are trademarks of Apple Inc. This project is an independent software and has not been authorized, sponsored, or otherwise approved by Apple Inc. -README Updated on: 2021-07-27 +README Updated on: 2021-08-30 diff --git a/common/Makefile.am b/common/Makefile.am index ab01b83..1a90571 100644 --- a/common/Makefile.am +++ b/common/Makefile.am @@ -25,11 +25,8 @@ noinst_LTLIBRARIES = libinternalcommon.la libinternalcommon_la_LIBADD = libinternalcommon_la_LDFLAGS = $(AM_LDFLAGS) -no-undefined libinternalcommon_la_SOURCES = \ - socket.c socket.h \ - thread.c thread.h \ debug.c debug.h \ - userpref.c userpref.h \ - utils.c utils.h + userpref.c userpref.h if WIN32 libinternalcommon_la_LIBADD += -lole32 -lws2_32 diff --git a/common/socket.c b/common/socket.c deleted file mode 100644 index 99a96b1..0000000 --- a/common/socket.c +++ /dev/null @@ -1,1169 +0,0 @@ -/* - * socket.c - * - * Copyright (C) 2012-2020 Nikias Bassen - * Copyright (C) 2012 Martin Szulecki - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - */ - -#ifdef HAVE_CONFIG_H -#include -#endif -#include -#include -#include -#include -#include -#include -#include -#include -#ifdef WIN32 -#include -#include -#include -#ifndef HAVE_GETIFADDRS -#include -#endif -static int wsa_init = 0; -#ifndef IFF_RUNNING -#define IFF_RUNNING IFF_UP -#endif -#ifndef AI_NUMERICSERV -#define AI_NUMERICSERV 0 -#endif -#else -#include -#include -#include -#include -#include -#include -#include -#ifdef AF_INET6 -#include -#include -#endif -#endif -#include "socket.h" - -#define RECV_TIMEOUT 20000 -#define SEND_TIMEOUT 10000 -#define CONNECT_TIMEOUT 5000 - -#ifndef EAFNOSUPPORT -#define EAFNOSUPPORT 102 -#endif -#ifndef ECONNRESET -#define ECONNRESET 108 -#endif -#ifndef ETIMEDOUT -#define ETIMEDOUT 138 -#endif - -static int verbose = 0; - -void socket_set_verbose(int level) -{ - verbose = level; -} - -const char *socket_addr_to_string(struct sockaddr *addr, char *addr_out, size_t addr_out_size) -{ -#ifdef WIN32 - WSADATA wsa_data; - if (!wsa_init) { - if (WSAStartup(MAKEWORD(2,2), &wsa_data) != ERROR_SUCCESS) { - fprintf(stderr, "WSAStartup failed!\n"); - ExitProcess(-1); - } - wsa_init = 1; - } - DWORD addr_out_len = addr_out_size; - DWORD addrlen = 0; - - if (addr->sa_family == AF_INET) { - addrlen = sizeof(struct sockaddr_in); - } -#ifdef AF_INET6 - else if (addr->sa_family == AF_INET6) { - addrlen = sizeof(struct sockaddr_in6); - } -#endif - else { - errno = EAFNOSUPPORT; - return NULL; - } - - if (WSAAddressToString(addr, addrlen, NULL, addr_out, &addr_out_len) == 0) { - return addr_out; - } -#else - const void *addrdata = NULL; - - if (addr->sa_family == AF_INET) { - addrdata = &((struct sockaddr_in*)addr)->sin_addr; - } -#ifdef AF_INET6 - else if (addr->sa_family == AF_INET6) { - addrdata = &((struct sockaddr_in6*)addr)->sin6_addr; - } -#endif - else { - errno = EAFNOSUPPORT; - return NULL; - } - - if (inet_ntop(addr->sa_family, addrdata, addr_out, addr_out_size)) { - return addr_out; - } -#endif - return NULL; -} - -#ifndef WIN32 -int socket_create_unix(const char *filename) -{ - struct sockaddr_un name; - int sock; -#ifdef SO_NOSIGPIPE - int yes = 1; -#endif - - // remove if still present - unlink(filename); - - /* Create the socket. */ - sock = socket(PF_UNIX, SOCK_STREAM, 0); - if (sock < 0) { - perror("socket"); - return -1; - } - -#ifdef SO_NOSIGPIPE - if (setsockopt(sock, SOL_SOCKET, SO_NOSIGPIPE, (void*)&yes, sizeof(int)) == -1) { - perror("setsockopt()"); - socket_close(sock); - return -1; - } -#endif - - /* Bind a name to the socket. */ - name.sun_family = AF_UNIX; - strncpy(name.sun_path, filename, sizeof(name.sun_path)); - name.sun_path[sizeof(name.sun_path) - 1] = '\0'; - - if (bind(sock, (struct sockaddr*)&name, sizeof(name)) < 0) { - perror("bind"); - socket_close(sock); - return -1; - } - - if (listen(sock, 100) < 0) { - perror("listen"); - socket_close(sock); - return -1; - } - - return sock; -} - -int socket_connect_unix(const char *filename) -{ - struct sockaddr_un name; - int sfd = -1; - struct stat fst; -#ifdef SO_NOSIGPIPE - int yes = 1; -#endif - int bufsize = 0x20000; - - // check if socket file exists... - if (stat(filename, &fst) != 0) { - if (verbose >= 2) - fprintf(stderr, "%s: stat '%s': %s\n", __func__, filename, - strerror(errno)); - return -1; - } - // ... and if it is a unix domain socket - if (!S_ISSOCK(fst.st_mode)) { - if (verbose >= 2) - fprintf(stderr, "%s: File '%s' is not a socket!\n", __func__, - filename); - return -1; - } - // make a new socket - if ((sfd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) { - if (verbose >= 2) - fprintf(stderr, "%s: socket: %s\n", __func__, strerror(errno)); - return -1; - } - - if (setsockopt(sfd, SOL_SOCKET, SO_SNDBUF, (void*)&bufsize, sizeof(int)) == -1) { - perror("Could not set send buffer for socket"); - } - - if (setsockopt(sfd, SOL_SOCKET, SO_RCVBUF, (void*)&bufsize, sizeof(int)) == -1) { - perror("Could not set receive buffer for socket"); - } - -#ifdef SO_NOSIGPIPE - if (setsockopt(sfd, SOL_SOCKET, SO_NOSIGPIPE, (void*)&yes, sizeof(int)) == -1) { - perror("setsockopt()"); - socket_close(sfd); - return -1; - } -#endif - // and connect to 'filename' - name.sun_family = AF_UNIX; - strncpy(name.sun_path, filename, sizeof(name.sun_path)); - name.sun_path[sizeof(name.sun_path) - 1] = 0; - - int flags = fcntl(sfd, F_GETFL, 0); - fcntl(sfd, F_SETFL, flags | O_NONBLOCK); - - do { - if (connect(sfd, (struct sockaddr*)&name, sizeof(name)) != -1) { - break; - } - if (errno == EINPROGRESS) { - fd_set fds; - FD_ZERO(&fds); - FD_SET(sfd, &fds); - - struct timeval timeout; - timeout.tv_sec = CONNECT_TIMEOUT / 1000; - timeout.tv_usec = (CONNECT_TIMEOUT - (timeout.tv_sec * 1000)) * 1000; - if (select(sfd + 1, NULL, &fds, NULL, &timeout) == 1) { - int so_error; - socklen_t len = sizeof(so_error); - getsockopt(sfd, SOL_SOCKET, SO_ERROR, (void*)&so_error, &len); - if (so_error == 0) { - break; - } - } - } - socket_close(sfd); - sfd = -1; - } while (0); - - if (sfd < 0) { - if (verbose >= 2) - fprintf(stderr, "%s: connect: %s\n", __func__, strerror(errno)); - return -1; - } - - return sfd; -} -#endif - -int socket_create(const char* addr, uint16_t port) -{ - int sfd = -1; - int yes = 1; - struct addrinfo hints; - struct addrinfo *result, *rp; - char portstr[8]; - int res; -#ifdef WIN32 - WSADATA wsa_data; - if (!wsa_init) { - if (WSAStartup(MAKEWORD(2,2), &wsa_data) != ERROR_SUCCESS) { - fprintf(stderr, "WSAStartup failed!\n"); - ExitProcess(-1); - } - wsa_init = 1; - } -#endif - - memset(&hints, '\0', sizeof(struct addrinfo)); - hints.ai_family = AF_UNSPEC; - hints.ai_socktype = SOCK_STREAM; - hints.ai_flags = AI_PASSIVE | AI_NUMERICSERV; - hints.ai_protocol = IPPROTO_TCP; - - sprintf(portstr, "%d", port); - - if (!addr) { - addr = "localhost"; - } - res = getaddrinfo(addr, portstr, &hints, &result); - if (res != 0) { - fprintf(stderr, "%s: getaddrinfo: %s\n", __func__, gai_strerror(res)); - return -1; - } - - for (rp = result; rp != NULL; rp = rp->ai_next) { - sfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); - if (sfd == -1) { - continue; - } - - if (setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, (void*)&yes, sizeof(int)) == -1) { - perror("setsockopt()"); - socket_close(sfd); - continue; - } - -#ifdef SO_NOSIGPIPE - if (setsockopt(sfd, SOL_SOCKET, SO_NOSIGPIPE, (void*)&yes, sizeof(int)) == -1) { - perror("setsockopt()"); - socket_close(sfd); - continue; - } -#endif - -#if defined(AF_INET6) && defined(IPV6_V6ONLY) - if (rp->ai_family == AF_INET6) { - if (setsockopt(sfd, IPPROTO_IPV6, IPV6_V6ONLY, (void*)&yes, sizeof(int)) == -1) { - perror("setsockopt() IPV6_V6ONLY"); - } - } -#endif - - if (bind(sfd, rp->ai_addr, rp->ai_addrlen) < 0) { - perror("bind()"); - socket_close(sfd); - continue; - } - - if (listen(sfd, 100) < 0) { - perror("listen()"); - socket_close(sfd); - continue; - } - break; - } - - freeaddrinfo(result); - - if (rp == NULL) { - return -1; - } - - return sfd; -} - -#ifdef AF_INET6 -static uint32_t _in6_addr_scope(struct in6_addr* addr) -{ - uint32_t scope = 0; - - if (IN6_IS_ADDR_MULTICAST(addr)) { - if (IN6_IS_ADDR_MC_NODELOCAL(addr)) { - scope = 1; - } else if (IN6_IS_ADDR_MC_LINKLOCAL(addr)) { - scope = 2; - } else if (IN6_IS_ADDR_MC_SITELOCAL(addr)) { - scope = 5; - } - - return scope; - } - - if (IN6_IS_ADDR_LINKLOCAL(addr)) { - scope = 2; - } else if (IN6_IS_ADDR_LOOPBACK(addr)) { - scope = 2; - } else if (IN6_IS_ADDR_SITELOCAL(addr)) { - scope = 5; - } else if (IN6_IS_ADDR_UNSPECIFIED(addr)) { - scope = 0; - } - - return scope; -} - -#ifndef HAVE_GETIFADDRS -#ifdef WIN32 - -struct ifaddrs { - struct ifaddrs *ifa_next; /* Next item in list */ - char *ifa_name; /* Name of interface */ - unsigned int ifa_flags; /* Flags from SIOCGIFFLAGS */ - struct sockaddr *ifa_addr; /* Address of interface */ - struct sockaddr *ifa_netmask; /* Netmask of interface */ - union { - struct sockaddr *ifu_broadaddr; /* Broadcast address of interface */ - struct sockaddr *ifu_dstaddr; /* Point-to-point destination address */ - } ifa_ifu; -#define ifa_broadaddr ifa_ifu.ifu_broadaddr -#define ifa_dstaddr ifa_ifu.ifu_dstaddr - void *ifa_data; /* Address-specific data */ -}; - -#define WORKING_BUFFER_SIZE 15000 -#define MAX_TRIES 3 - -static void freeifaddrs(struct ifaddrs *ifa) -{ - if (!ifa) { - return; - } - free(ifa->ifa_name); - free(ifa->ifa_addr); - free(ifa->ifa_netmask); - free(ifa->ifa_dstaddr); - freeifaddrs(ifa->ifa_next); - free(ifa); -} - -/* - * getifaddrs() reference implementation for win32. - * Heavily based on openpgm's implementation found here: - * https://github.com/steve-o/openpgm/blob/master/openpgm/pgm/getifaddrs.c - */ -static int getifaddrs(struct ifaddrs** ifap) -{ - struct ifaddrs* ifa = NULL; - - DWORD dwRetVal = 0; - - PIP_ADAPTER_ADDRESSES pAddresses = NULL; - ULONG outBufLen = 0; - ULONG Iterations = 0; - - ULONG flags = GAA_FLAG_INCLUDE_PREFIX | - GAA_FLAG_SKIP_ANYCAST | - GAA_FLAG_SKIP_DNS_SERVER | - GAA_FLAG_SKIP_FRIENDLY_NAME | - GAA_FLAG_SKIP_MULTICAST; - - PIP_ADAPTER_ADDRESSES adapter = NULL; - - if (!ifap) { - errno = EINVAL; - return -1; - } - *ifap = NULL; - - outBufLen = WORKING_BUFFER_SIZE; - do { - pAddresses = (IP_ADAPTER_ADDRESSES*)malloc(outBufLen); - if (pAddresses == NULL) { - printf("Memory allocation failed for IP_ADAPTER_ADDRESSES struct\n"); - return -1; - } - dwRetVal = GetAdaptersAddresses(AF_UNSPEC, flags, NULL, pAddresses, &outBufLen); - if (dwRetVal == ERROR_BUFFER_OVERFLOW) { - free(pAddresses); - pAddresses = NULL; - } else { - break; - } - Iterations++; - } while ((dwRetVal == ERROR_BUFFER_OVERFLOW) && (Iterations < MAX_TRIES)); - - if (dwRetVal != NO_ERROR) { - free(pAddresses); - return -1; - } - - for (adapter = pAddresses; adapter; adapter = adapter->Next) { - int unicastIndex = 0; - for (IP_ADAPTER_UNICAST_ADDRESS *unicast = adapter->FirstUnicastAddress; unicast; unicast = unicast->Next, ++unicastIndex) { - /* ensure IP adapter */ - if (AF_INET != unicast->Address.lpSockaddr->sa_family && AF_INET6 != unicast->Address.lpSockaddr->sa_family) { - continue; - } - - if (!ifa) { - ifa = malloc(sizeof(struct ifaddrs)); - if (!ifa) { - errno = ENOMEM; - free(pAddresses); - return -1; - } - *ifap = ifa; - ifa->ifa_next = NULL; - } else { - struct ifaddrs* ifanew = malloc(sizeof(struct ifaddrs)); - if (!ifanew) { - freeifaddrs(*ifap); - free(pAddresses); - errno = ENOMEM; - return -1; - } - ifa->ifa_next = ifanew; - ifa = ifanew; - ifa->ifa_next = NULL; - } - - /* name */ - ifa->ifa_name = strdup(adapter->AdapterName); - - /* flags */ - ifa->ifa_flags = 0; - if (IfOperStatusUp == adapter->OperStatus) - ifa->ifa_flags |= IFF_UP; - if (IF_TYPE_SOFTWARE_LOOPBACK == adapter->IfType) - ifa->ifa_flags |= IFF_LOOPBACK; - if (!(adapter->Flags & IP_ADAPTER_NO_MULTICAST)) - ifa->ifa_flags |= IFF_MULTICAST; - - /* address */ - ifa->ifa_addr = (struct sockaddr*)malloc(sizeof(struct sockaddr_storage)); - memcpy(ifa->ifa_addr, unicast->Address.lpSockaddr, unicast->Address.iSockaddrLength); - - /* netmask */ - ifa->ifa_netmask = (struct sockaddr*)malloc(sizeof(struct sockaddr_storage)); - memset(ifa->ifa_netmask, 0, sizeof(struct sockaddr_storage)); - -/* pre-Vista must hunt for matching prefix in linked list, otherwise use - * OnLinkPrefixLength from IP_ADAPTER_UNICAST_ADDRESS structure. - * FirstPrefix requires Windows XP SP1, from SP1 to pre-Vista provides a - * single adapter prefix for each IP address. Vista and later provides - * host IP address prefix, subnet IP address, and subnet broadcast IP - * address. In addition there is a multicast and broadcast address prefix. - */ - ULONG prefixLength = 0; - -#if defined( _WIN32 ) && ( _WIN32_WINNT >= 0x0600 ) -/* For a unicast IPv4 address, any value greater than 32 is an illegal - * value. For a unicast IPv6 address, any value greater than 128 is an - * illegal value. A value of 255 is commonly used to represent an illegal - * value. - * - * Windows 7 SP1 returns 64 for Teredo links which is incorrect. - */ - -#define IN6_IS_ADDR_TEREDO(addr) \ - (((const uint32_t *)(addr))[0] == ntohl (0x20010000)) - - if (AF_INET6 == unicast->Address.lpSockaddr->sa_family && -/* TunnelType only applies to one interface on the adapter and no - * convenient method is provided to determine which. - */ - TUNNEL_TYPE_TEREDO == adapter->TunnelType && -/* Test the interface with the known Teredo network prefix. - */ - IN6_IS_ADDR_TEREDO( &((struct sockaddr_in6*)(unicast->Address.lpSockaddr))->sin6_addr) && -/* Test that this version is actually wrong, subsequent releases from Microsoft - * may resolve the issue. - */ - 32 != unicast->OnLinkPrefixLength) - { - prefixLength = 32; - } - else - prefixLength = unicast->OnLinkPrefixLength; -#else -/* The order of linked IP_ADAPTER_UNICAST_ADDRESS structures pointed to by - * the FirstUnicastAddress member does not have any relationship with the - * order of linked IP_ADAPTER_PREFIX structures pointed to by the FirstPrefix - * member. - * - * Example enumeration: - * [ no subnet ] - * ::1/128 - address - * ff00::%1/8 - multicast (no IPv6 broadcast) - * 127.0.0.0/8 - subnet - * 127.0.0.1/32 - address - * 127.255.255.255/32 - subnet broadcast - * 224.0.0.0/4 - multicast - * 255.255.255.255/32 - broadcast - * - * Which differs from most adapters listing three IPv6: - * fe80::%10/64 - subnet - * fe80::51e9:5fe5:4202:325a%10/128 - address - * ff00::%10/8 - multicast - * - * !IfOperStatusUp IPv4 addresses are skipped: - * fe80::%13/64 - subnet - * fe80::d530:946d:e8df:8c91%13/128 - address - * ff00::%13/8 - multicast - * [ no subnet ] - * [ no address ] - * 224.0.0.0/4 - multicast - * 255.255.255.255/32 - broadcast - * - * On PTP links no multicast or broadcast addresses are returned: - * [ no subnet ] - * fe80::5efe:10.203.9.30/128 - address - * [ no multicast ] - * [ no multicast ] - * [ no broadcast ] - * - * Active primary IPv6 interfaces are a bit overloaded: - * ::/0 - default route - * 2001::/32 - global subnet - * 2001:0:4137:9e76:2443:d6:ba87:1a2a/128 - global address - * fe80::/64 - link-local subnet - * fe80::2443:d6:ba87:1a2a/128 - link-local address - * ff00::/8 - multicast - */ - -#define IN_LINKLOCAL(a) ((((uint32_t) (a)) & 0xaffff0000) == 0xa9fe0000) - - for (IP_ADAPTER_PREFIX *prefix = adapter->FirstPrefix; prefix; prefix = prefix->Next) { - LPSOCKADDR lpSockaddr = prefix->Address.lpSockaddr; - if (lpSockaddr->sa_family != unicast->Address.lpSockaddr->sa_family) - continue; -/* special cases */ -/* RFC2863: IPv4 interface not up */ - if (AF_INET == lpSockaddr->sa_family && adapter->OperStatus != IfOperStatusUp) { -/* RFC3927: link-local IPv4 always has 16-bit CIDR */ - if (IN_LINKLOCAL( ntohl (((struct sockaddr_in*)(unicast->Address.lpSockaddr))->sin_addr.s_addr))) { - prefixLength = 16; - } - break; - } -/* default IPv6 route */ - if (AF_INET6 == lpSockaddr->sa_family && 0 == prefix->PrefixLength && IN6_IS_ADDR_UNSPECIFIED( &((struct sockaddr_in6*)(lpSockaddr))->sin6_addr)) { - continue; - } -/* Assume unicast address for first prefix of operational adapter */ - if (AF_INET == lpSockaddr->sa_family) - if (IN_MULTICAST( ntohl (((struct sockaddr_in*)(lpSockaddr))->sin_addr.s_addr))) { - fprintf(stderr, "FATAL: first prefix is non a unicast address\n"); - break; - } - if (AF_INET6 == lpSockaddr->sa_family) - if (IN6_IS_ADDR_MULTICAST( &((struct sockaddr_in6*)(lpSockaddr))->sin6_addr)) { - fprintf(stderr, "FATAL: first prefix is not a unicast address\n"); - break; - } -/* Assume subnet or host IP address for XP backward compatibility */ - - prefixLength = prefix->PrefixLength; - break; - } -#endif /* defined( _WIN32 ) && ( _WIN32_WINNT >= 0x0600 ) */ - -/* map prefix to netmask */ - ifa->ifa_netmask->sa_family = unicast->Address.lpSockaddr->sa_family; - switch (unicast->Address.lpSockaddr->sa_family) { - case AF_INET: - if (0 == prefixLength || prefixLength > 32) { - prefixLength = 32; - } -#if defined( _WIN32) && ( _WIN32_WINNT >= 0x0600 ) -/* Added in Vista, but no IPv6 equivalent. */ - { - ULONG Mask; - ConvertLengthToIpv4Mask (prefixLength, &Mask); - ((struct sockaddr_in*)ifa->ifa_netmask)->sin_addr.s_addr = Mask; /* network order */ - } -#else -/* NB: left-shift of full bit-width is undefined in C standard. */ - ((struct sockaddr_in*)ifa->ifa_netmask)->sin_addr.s_addr = htonl( 0xffffffffU << ( 32 - prefixLength ) ); -#endif - break; - - case AF_INET6: - if (0 == prefixLength || prefixLength > 128) { - prefixLength = 128; - } - for (LONG i = prefixLength, j = 0; i > 0; i -= 8, ++j) { - ((struct sockaddr_in6*)ifa->ifa_netmask)->sin6_addr.s6_addr[ j ] = i >= 8 ? 0xff : (ULONG)(( 0xffU << ( 8 - i ) ) & 0xffU ); - } - break; - default: - break; - } - } - } - free(pAddresses); - - return 0; -} -#else -#error No reference implementation for getifaddrs available for this platform. -#endif -#endif - -static int32_t _sockaddr_in6_scope_id(struct sockaddr_in6* addr) -{ - int32_t res = -1; - struct ifaddrs *ifaddr = NULL, *ifa = NULL; - uint32_t addr_scope; - - /* get scope for requested address */ - addr_scope = _in6_addr_scope(&addr->sin6_addr); - if (addr_scope == 0) { - /* global scope doesn't need a specific scope id */ - return addr_scope; - } - - /* get interfaces */ - if (getifaddrs(&ifaddr) == -1) { - perror("getifaddrs"); - return res; - } - - /* loop over interfaces */ - for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { - /* skip if no address is available */ - if (ifa->ifa_addr == NULL) { - continue; - } - - /* skip if wrong family */ - if (ifa->ifa_addr->sa_family != AF_INET6) { - continue; - } - - /* skip if not up */ - if ((ifa->ifa_flags & IFF_UP) == 0) { - continue; - } - - /* skip if not running */ - if ((ifa->ifa_flags & IFF_RUNNING) == 0) { - continue; - } - - struct sockaddr_in6* addr_in = (struct sockaddr_in6*)ifa->ifa_addr; - - /* skip if scopes do not match */ - if (_in6_addr_scope(&addr_in->sin6_addr) != addr_scope) { - continue; - } - - /* use if address is equal */ - if (memcmp(&addr->sin6_addr.s6_addr, &addr_in->sin6_addr.s6_addr, sizeof(addr_in->sin6_addr.s6_addr)) == 0) { - /* if scope id equals the requested one then assume it was valid */ - if (addr->sin6_scope_id == addr_in->sin6_scope_id) { - res = addr_in->sin6_scope_id; - break; - } - - if ((addr_in->sin6_scope_id > addr->sin6_scope_id) && (res >= 0)) { - // use last valid scope id as we're past the requested scope id - break; - } - res = addr_in->sin6_scope_id; - continue; - } - - /* skip loopback interface if not already matched exactly above */ - if ((ifa->ifa_flags & IFF_LOOPBACK) != 0) { - continue; - } - - if ((addr_in->sin6_scope_id > addr->sin6_scope_id) && (res >= 0)) { - // use last valid scope id as we're past the requested scope id - break; - } - - res = addr_in->sin6_scope_id; - - /* if scope id equals the requested one then assume it was valid */ - if (addr->sin6_scope_id == addr_in->sin6_scope_id) { - /* set the scope id of this interface as most likely candidate */ - break; - } - } - - freeifaddrs(ifaddr); - - return res; -} -#endif - -int socket_connect_addr(struct sockaddr* addr, uint16_t port) -{ - int sfd = -1; - int yes = 1; - int bufsize = 0x20000; - int addrlen = 0; -#ifdef WIN32 - u_long l_yes = 1; - WSADATA wsa_data; - if (!wsa_init) { - if (WSAStartup(MAKEWORD(2,2), &wsa_data) != ERROR_SUCCESS) { - fprintf(stderr, "WSAStartup failed!\n"); - ExitProcess(-1); - } - wsa_init = 1; - } -#endif - - if (addr->sa_family == AF_INET) { - struct sockaddr_in* addr_in = (struct sockaddr_in*)addr; - addr_in->sin_port = htons(port); - addrlen = sizeof(struct sockaddr_in); - } -#ifdef AF_INET6 - else if (addr->sa_family == AF_INET6) { - struct sockaddr_in6* addr_in = (struct sockaddr_in6*)addr; - addr_in->sin6_port = htons(port); - - /* - * IPv6 Routing Magic: - * - * If the scope of the address is a link-local one, IPv6 requires the - * scope id set to an interface number to allow proper routing. However, - * as the provided sockaddr might contain a wrong scope id, we must find - * a scope id from a suitable interface on this system or routing might - * fail. An IPv6 guru should have another look though... - */ - addr_in->sin6_scope_id = _sockaddr_in6_scope_id(addr_in); - - addrlen = sizeof(struct sockaddr_in6); - } -#endif - else { - fprintf(stderr, "ERROR: Unsupported address family"); - return -1; - } - - sfd = socket(addr->sa_family, SOCK_STREAM, IPPROTO_TCP); - if (sfd == -1) { - perror("socket()"); - return -1; - } - -#ifdef SO_NOSIGPIPE - if (setsockopt(sfd, SOL_SOCKET, SO_NOSIGPIPE, (void*)&yes, sizeof(int)) == -1) { - perror("setsockopt()"); - socket_close(sfd); - return -1; - } -#endif - - if (setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, (void*)&yes, sizeof(int)) == -1) { - perror("setsockopt()"); - socket_close(sfd); - return -1; - } - -#ifdef WIN32 - ioctlsocket(sfd, FIONBIO, &l_yes); -#else - int flags = fcntl(sfd, F_GETFL, 0); - fcntl(sfd, F_SETFL, flags | O_NONBLOCK); -#endif - - do { - if (connect(sfd, addr, addrlen) != -1) { - break; - } -#ifdef WIN32 - if (WSAGetLastError() == WSAEWOULDBLOCK) -#else - if (errno == EINPROGRESS) -#endif - { - fd_set fds; - FD_ZERO(&fds); - FD_SET(sfd, &fds); - - struct timeval timeout; - timeout.tv_sec = CONNECT_TIMEOUT / 1000; - timeout.tv_usec = (CONNECT_TIMEOUT - (timeout.tv_sec * 1000)) * 1000; - if (select(sfd + 1, NULL, &fds, NULL, &timeout) == 1) { - int so_error; - socklen_t len = sizeof(so_error); - getsockopt(sfd, SOL_SOCKET, SO_ERROR, (void*)&so_error, &len); - if (so_error == 0) { - errno = 0; - break; - } - errno = so_error; - } - } - socket_close(sfd); - sfd = -1; - } while (0); - - if (sfd < 0) { - if (verbose >= 2) { - char addrtxt[48]; - socket_addr_to_string(addr, addrtxt, sizeof(addrtxt)); - fprintf(stderr, "%s: Could not connect to %s port %d\n", __func__, addrtxt, port); - } - return -1; - } - - if (setsockopt(sfd, IPPROTO_TCP, TCP_NODELAY, (void*)&yes, sizeof(int)) == -1) { - perror("Could not set TCP_NODELAY on socket"); - } - - if (setsockopt(sfd, SOL_SOCKET, SO_SNDBUF, (void*)&bufsize, sizeof(int)) == -1) { - perror("Could not set send buffer for socket"); - } - - if (setsockopt(sfd, SOL_SOCKET, SO_RCVBUF, (void*)&bufsize, sizeof(int)) == -1) { - perror("Could not set receive buffer for socket"); - } - - return sfd; -} - -int socket_connect(const char *addr, uint16_t port) -{ - int sfd = -1; - int yes = 1; - int bufsize = 0x20000; - struct addrinfo hints; - struct addrinfo *result, *rp; - char portstr[8]; - int res; -#ifdef WIN32 - u_long l_yes = 1; - WSADATA wsa_data; - if (!wsa_init) { - if (WSAStartup(MAKEWORD(2,2), &wsa_data) != ERROR_SUCCESS) { - fprintf(stderr, "WSAStartup failed!\n"); - ExitProcess(-1); - } - wsa_init = 1; - } -#else - int flags = 0; -#endif - - if (!addr) { - errno = EINVAL; - return -1; - } - - memset(&hints, '\0', sizeof(struct addrinfo)); - hints.ai_family = AF_UNSPEC; - hints.ai_socktype = SOCK_STREAM; - hints.ai_flags = AI_NUMERICSERV; - hints.ai_protocol = IPPROTO_TCP; - - sprintf(portstr, "%d", port); - - res = getaddrinfo(addr, portstr, &hints, &result); - if (res != 0) { - fprintf(stderr, "%s: getaddrinfo: %s\n", __func__, gai_strerror(res)); - return -1; - } - - for (rp = result; rp != NULL; rp = rp->ai_next) { - sfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); - if (sfd == -1) { - continue; - } - -#ifdef SO_NOSIGPIPE - if (setsockopt(sfd, SOL_SOCKET, SO_NOSIGPIPE, (void*)&yes, sizeof(int)) == -1) { - perror("setsockopt()"); - socket_close(sfd); - return -1; - } -#endif - - if (setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, (void*)&yes, sizeof(int)) == -1) { - perror("setsockopt()"); - socket_close(sfd); - continue; - } - -#ifdef WIN32 - ioctlsocket(sfd, FIONBIO, &l_yes); -#else - flags = fcntl(sfd, F_GETFL, 0); - fcntl(sfd, F_SETFL, flags | O_NONBLOCK); -#endif - - if (connect(sfd, rp->ai_addr, rp->ai_addrlen) != -1) { - break; - } -#ifdef WIN32 - if (WSAGetLastError() == WSAEWOULDBLOCK) -#else - if (errno == EINPROGRESS) -#endif - { - fd_set fds; - FD_ZERO(&fds); - FD_SET(sfd, &fds); - - struct timeval timeout; - timeout.tv_sec = CONNECT_TIMEOUT / 1000; - timeout.tv_usec = (CONNECT_TIMEOUT - (timeout.tv_sec * 1000)) * 1000; - if (select(sfd + 1, NULL, &fds, NULL, &timeout) == 1) { - int so_error; - socklen_t len = sizeof(so_error); - getsockopt(sfd, SOL_SOCKET, SO_ERROR, (void*)&so_error, &len); - if (so_error == 0) { - break; - } - } - } - socket_close(sfd); - } - - freeaddrinfo(result); - - if (rp == NULL) { - if (verbose >= 2) - fprintf(stderr, "%s: Could not connect to %s:%d\n", __func__, addr, port); - return -1; - } - - if (setsockopt(sfd, IPPROTO_TCP, TCP_NODELAY, (void*)&yes, sizeof(int)) == -1) { - perror("Could not set TCP_NODELAY on socket"); - } - - if (setsockopt(sfd, SOL_SOCKET, SO_SNDBUF, (void*)&bufsize, sizeof(int)) == -1) { - perror("Could not set send buffer for socket"); - } - - if (setsockopt(sfd, SOL_SOCKET, SO_RCVBUF, (void*)&bufsize, sizeof(int)) == -1) { - perror("Could not set receive buffer for socket"); - } - - return sfd; -} - -int socket_check_fd(int fd, fd_mode fdm, unsigned int timeout) -{ - fd_set fds; - int sret; - int eagain; - struct timeval to; - struct timeval *pto; - - if (fd < 0) { - if (verbose >= 2) - fprintf(stderr, "ERROR: invalid fd in check_fd %d\n", fd); - return -1; - } - - FD_ZERO(&fds); - FD_SET(fd, &fds); - - sret = -1; - - do { - if (timeout > 0) { - to.tv_sec = (time_t) (timeout / 1000); - to.tv_usec = (time_t) ((timeout - (to.tv_sec * 1000)) * 1000); - pto = &to; - } else { - pto = NULL; - } - eagain = 0; - switch (fdm) { - case FDM_READ: - sret = select(fd + 1, &fds, NULL, NULL, pto); - break; - case FDM_WRITE: - sret = select(fd + 1, NULL, &fds, NULL, pto); - break; - case FDM_EXCEPT: - sret = select(fd + 1, NULL, NULL, &fds, pto); - break; - default: - return -1; - } - - if (sret < 0) { - switch (errno) { - case EINTR: - // interrupt signal in select - if (verbose >= 2) - fprintf(stderr, "%s: EINTR\n", __func__); - eagain = 1; - break; - case EAGAIN: - if (verbose >= 2) - fprintf(stderr, "%s: EAGAIN\n", __func__); - break; - default: - if (verbose >= 2) - fprintf(stderr, "%s: select failed: %s\n", __func__, - strerror(errno)); - return -1; - } - } else if (sret == 0) { - return -ETIMEDOUT; - } - } while (eagain); - - return sret; -} - -int socket_accept(int fd, uint16_t port) -{ -#ifdef WIN32 - int addr_len; -#else - socklen_t addr_len; -#endif - int result; - struct sockaddr_storage addr; - addr_len = sizeof(addr); - - result = accept(fd, (struct sockaddr*)&addr, &addr_len); - - return result; -} - -int socket_shutdown(int fd, int how) -{ - return shutdown(fd, how); -} - -int socket_close(int fd) { -#ifdef WIN32 - return closesocket(fd); -#else - return close(fd); -#endif -} - -int socket_receive(int fd, void *data, size_t length) -{ - return socket_receive_timeout(fd, data, length, 0, RECV_TIMEOUT); -} - -int socket_peek(int fd, void *data, size_t length) -{ - return socket_receive_timeout(fd, data, length, MSG_PEEK, RECV_TIMEOUT); -} - -int socket_receive_timeout(int fd, void *data, size_t length, int flags, - unsigned int timeout) -{ - int res; - int result; - - // check if data is available - res = socket_check_fd(fd, FDM_READ, timeout); - if (res <= 0) { - return res; - } - // if we get here, there _is_ data available - result = recv(fd, data, length, flags); - if (res > 0 && result == 0) { - // but this is an error condition - if (verbose >= 3) - fprintf(stderr, "%s: fd=%d recv returned 0\n", __func__, fd); - return -ECONNRESET; - } - if (result < 0) { - return -errno; - } - return result; -} - -int socket_send(int fd, void *data, size_t length) -{ - int flags = 0; - int res = socket_check_fd(fd, FDM_WRITE, SEND_TIMEOUT); - if (res <= 0) { - return res; - } -#ifdef MSG_NOSIGNAL - flags |= MSG_NOSIGNAL; -#endif - return send(fd, data, length, flags); -} diff --git a/common/socket.h b/common/socket.h deleted file mode 100644 index 9567270..0000000 --- a/common/socket.h +++ /dev/null @@ -1,68 +0,0 @@ -/* - * socket.h - * - * Copyright (C) 2012-2020 Nikias Bassen - * Copyright (C) 2012 Martin Szulecki - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - */ - -#ifndef SOCKET_SOCKET_H -#define SOCKET_SOCKET_H - -#include -#include - -enum fd_mode { - FDM_READ, - FDM_WRITE, - FDM_EXCEPT -}; -typedef enum fd_mode fd_mode; - -#ifdef WIN32 -#include -#define SHUT_RD SD_READ -#define SHUT_WR SD_WRITE -#define SHUT_RDWR SD_BOTH -#else -#include -#endif - -#ifndef WIN32 -int socket_create_unix(const char *filename); -int socket_connect_unix(const char *filename); -#endif -int socket_create(const char *addr, uint16_t port); -int socket_connect_addr(struct sockaddr *addr, uint16_t port); -int socket_connect(const char *addr, uint16_t port); -int socket_check_fd(int fd, fd_mode fdm, unsigned int timeout); -int socket_accept(int fd, uint16_t port); - -int socket_shutdown(int fd, int how); -int socket_close(int fd); - -int socket_receive(int fd, void *data, size_t length); -int socket_peek(int fd, void *data, size_t length); -int socket_receive_timeout(int fd, void *data, size_t length, int flags, - unsigned int timeout); - -int socket_send(int fd, void *data, size_t length); - -void socket_set_verbose(int level); - -const char *socket_addr_to_string(struct sockaddr *addr, char *addr_out, size_t addr_out_size); - -#endif /* SOCKET_SOCKET_H */ diff --git a/common/thread.c b/common/thread.c deleted file mode 100644 index eb535ab..0000000 --- a/common/thread.c +++ /dev/null @@ -1,140 +0,0 @@ -/* - * thread.c - * - * Copyright (c) 2012-2019 Nikias Bassen, All Rights Reserved. - * Copyright (c) 2012 Martin Szulecki, All Rights Reserved. - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - */ - -#ifdef HAVE_CONFIG_H -#include -#endif -#include "thread.h" - -int thread_new(THREAD_T *thread, thread_func_t thread_func, void* data) -{ -#ifdef WIN32 - HANDLE th = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)thread_func, data, 0, NULL); - if (th == NULL) { - return -1; - } - *thread = th; - return 0; -#else - int res = pthread_create(thread, NULL, thread_func, data); - return res; -#endif -} - -void thread_detach(THREAD_T thread) -{ -#ifdef WIN32 - CloseHandle(thread); -#else - pthread_detach(thread); -#endif -} - -void thread_free(THREAD_T thread) -{ -#ifdef WIN32 - CloseHandle(thread); -#endif -} - -int thread_join(THREAD_T thread) -{ - /* wait for thread to complete */ -#ifdef WIN32 - return (int)WaitForSingleObject(thread, INFINITE); -#else - return pthread_join(thread, NULL); -#endif -} - -int thread_alive(THREAD_T thread) -{ -#ifdef WIN32 - return WaitForSingleObject(thread, 0) == WAIT_TIMEOUT; -#else - return pthread_kill(thread, 0) == 0; -#endif -} - -int thread_cancel(THREAD_T thread) -{ -#ifdef WIN32 - return -1; -#else -#ifdef HAVE_PTHREAD_CANCEL - return pthread_cancel(thread); -#else - return -1; -#endif -#endif -} - -void mutex_init(mutex_t* mutex) -{ -#ifdef WIN32 - InitializeCriticalSection(mutex); -#else - pthread_mutex_init(mutex, NULL); -#endif -} - -void mutex_destroy(mutex_t* mutex) -{ -#ifdef WIN32 - DeleteCriticalSection(mutex); -#else - pthread_mutex_destroy(mutex); -#endif -} - -void mutex_lock(mutex_t* mutex) -{ -#ifdef WIN32 - EnterCriticalSection(mutex); -#else - pthread_mutex_lock(mutex); -#endif -} - -void mutex_unlock(mutex_t* mutex) -{ -#ifdef WIN32 - LeaveCriticalSection(mutex); -#else - pthread_mutex_unlock(mutex); -#endif -} - -void thread_once(thread_once_t *once_control, void (*init_routine)(void)) -{ -#ifdef WIN32 - while (InterlockedExchange(&(once_control->lock), 1) != 0) { - Sleep(1); - } - if (!once_control->state) { - once_control->state = 1; - init_routine(); - } - InterlockedExchange(&(once_control->lock), 0); -#else - pthread_once(once_control, init_routine); -#endif -} diff --git a/common/thread.h b/common/thread.h deleted file mode 100644 index 23e4510..0000000 --- a/common/thread.h +++ /dev/null @@ -1,76 +0,0 @@ -/* - * thread.h - * - * Copyright (c) 2012-2019 Nikias Bassen, All Rights Reserved. - * Copyright (c) 2012 Martin Szulecki, All Rights Reserved. - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - */ - -#ifndef __THREAD_H -#define __THREAD_H - -#include - -#ifdef WIN32 -#include -typedef HANDLE THREAD_T; -typedef CRITICAL_SECTION mutex_t; -typedef volatile struct { - LONG lock; - int state; -} thread_once_t; -#define THREAD_ONCE_INIT {0, 0} -#define THREAD_ID GetCurrentThreadId() -#define THREAD_T_NULL (THREAD_T)NULL -#else -#include -#include -typedef pthread_t THREAD_T; -typedef pthread_mutex_t mutex_t; -typedef pthread_once_t thread_once_t; -#define THREAD_ONCE_INIT PTHREAD_ONCE_INIT -#define THREAD_ID pthread_self() -#define THREAD_T_NULL (THREAD_T)NULL -#endif - -typedef void* (*thread_func_t)(void* data); - -int thread_new(THREAD_T* thread, thread_func_t thread_func, void* data); -void thread_detach(THREAD_T thread); -void thread_free(THREAD_T thread); -int thread_join(THREAD_T thread); -int thread_alive(THREAD_T thread); - -int thread_cancel(THREAD_T thread); - -#ifdef WIN32 -#undef HAVE_THREAD_CLEANUP -#else -#ifdef HAVE_PTHREAD_CANCEL -#define HAVE_THREAD_CLEANUP 1 -#define thread_cleanup_push(routine, arg) pthread_cleanup_push(routine, arg) -#define thread_cleanup_pop(execute) pthread_cleanup_pop(execute) -#endif -#endif - -void mutex_init(mutex_t* mutex); -void mutex_destroy(mutex_t* mutex); -void mutex_lock(mutex_t* mutex); -void mutex_unlock(mutex_t* mutex); - -void thread_once(thread_once_t *once_control, void (*init_routine)(void)); - -#endif diff --git a/common/userpref.c b/common/userpref.c index bf7e1bd..32904c7 100644 --- a/common/userpref.c +++ b/common/userpref.c @@ -73,9 +73,10 @@ #include #endif +#include + #include "userpref.h" #include "debug.h" -#include "utils.h" #if defined(HAVE_GNUTLS) const ASN1_ARRAY_TYPE pkcs1_asn1_tab[] = { diff --git a/common/utils.c b/common/utils.c deleted file mode 100644 index 58dac02..0000000 --- a/common/utils.c +++ /dev/null @@ -1,530 +0,0 @@ -/* - * utils.c - * Miscellaneous utilities for string manipulation, - * file I/O and plist helper. - * - * Copyright (c) 2014-2019 Nikias Bassen, All Rights Reserved. - * Copyright (c) 2013-2014 Martin Szulecki, All Rights Reserved. - * Copyright (c) 2013 Federico Mena Quintero - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - */ - -#ifdef HAVE_CONFIG_H -#include -#endif - -#include -#include -#include -#include -#include -#include -#include - -#include "utils.h" - -#ifndef HAVE_STPCPY -/** - * Copy characters from one string into another - * - * @note: The strings should not overlap, as the behavior is undefined. - * - * @s1: The source string. - * @s2: The destination string. - * - * @return a pointer to the terminating `\0' character of @s1, - * or NULL if @s1 or @s2 is NULL. - */ -char *stpcpy(char *s1, const char *s2) -{ - if (s1 == NULL || s2 == NULL) - return NULL; - - strcpy(s1, s2); - - return s1 + strlen(s2); -} -#endif - -/** - * Concatenate strings into a newly allocated string - * - * @note: Specify NULL for the last string in the varargs list - * - * @str: The first string in the list - * @...: Subsequent strings. Use NULL for the last item. - * - * @return a newly allocated string, or NULL if @str is NULL. This will also - * return NULL and set errno to ENOMEM if memory is exhausted. - */ -char *string_concat(const char *str, ...) -{ - size_t len; - va_list args; - char *s; - char *result; - char *dest; - - if (!str) - return NULL; - - /* Compute final length */ - - len = strlen(str) + 1; /* plus 1 for the null terminator */ - - va_start(args, str); - s = va_arg(args, char *); - while (s) { - len += strlen(s); - s = va_arg(args, char*); - } - va_end(args); - - /* Concat each string */ - - result = malloc(len); - if (!result) - return NULL; /* errno remains set */ - - dest = result; - - dest = stpcpy(dest, str); - - va_start(args, str); - s = va_arg(args, char *); - while (s) { - dest = stpcpy(dest, s); - s = va_arg(args, char *); - } - va_end(args); - - return result; -} - -char *string_append(char* str, ...) -{ - size_t len = 0; - size_t slen; - va_list args; - char *s; - char *result; - char *dest; - - /* Compute final length */ - - if (str) { - len = strlen(str); - } - slen = len; - len++; /* plus 1 for the null terminator */ - - va_start(args, str); - s = va_arg(args, char *); - while (s) { - len += strlen(s); - s = va_arg(args, char*); - } - va_end(args); - - result = realloc(str, len); - if (!result) - return NULL; /* errno remains set */ - - dest = result + slen; - - /* Concat additional strings */ - - va_start(args, str); - s = va_arg(args, char *); - while (s) { - dest = stpcpy(dest, s); - s = va_arg(args, char *); - } - va_end(args); - - return result; -} - -char *string_build_path(const char *elem, ...) -{ - if (!elem) - return NULL; - va_list args; - int len = strlen(elem)+1; - va_start(args, elem); - char *arg = va_arg(args, char*); - while (arg) { - len += strlen(arg)+1; - arg = va_arg(args, char*); - } - va_end(args); - - char* out = (char*)malloc(len); - strcpy(out, elem); - - va_start(args, elem); - arg = va_arg(args, char*); - while (arg) { - strcat(out, "/"); - strcat(out, arg); - arg = va_arg(args, char*); - } - va_end(args); - return out; -} - -char *string_format_size(uint64_t size) -{ - char buf[80]; - double sz; - if (size >= 1000000000000LL) { - sz = ((double)size / 1000000000000.0f); - sprintf(buf, "%0.1f TB", sz); - } else if (size >= 1000000000LL) { - sz = ((double)size / 1000000000.0f); - sprintf(buf, "%0.1f GB", sz); - } else if (size >= 1000000LL) { - sz = ((double)size / 1000000.0f); - sprintf(buf, "%0.1f MB", sz); - } else if (size >= 1000LL) { - sz = ((double)size / 1000.0f); - sprintf(buf, "%0.1f KB", sz); - } else { - sprintf(buf, "%d Bytes", (int)size); - } - return strdup(buf); -} - -char *string_toupper(char* str) -{ - char *res = strdup(str); - unsigned int i; - for (i = 0; i < strlen(res); i++) { - res[i] = toupper(res[i]); - } - return res; -} - -static int get_rand(int min, int max) -{ - int retval = (rand() % (max - min)) + min; - return retval; -} - -char *generate_uuid() -{ - const char *chars = "ABCDEF0123456789"; - int i = 0; - char *uuid = (char *) malloc(sizeof(char) * 37); - - srand(time(NULL)); - - for (i = 0; i < 36; i++) { - if (i == 8 || i == 13 || i == 18 || i == 23) { - uuid[i] = '-'; - continue; - } else { - uuid[i] = chars[get_rand(0, 16)]; - } - } - - /* make it a real string */ - uuid[36] = '\0'; - - return uuid; -} - -void buffer_read_from_filename(const char *filename, char **buffer, uint64_t *length) -{ - FILE *f; - uint64_t size; - - *length = 0; - - f = fopen(filename, "rb"); - if (!f) { - return; - } - - fseek(f, 0, SEEK_END); - size = ftell(f); - rewind(f); - - if (size == 0) { - fclose(f); - return; - } - - *buffer = (char*)malloc(sizeof(char)*(size+1)); - if (fread(*buffer, sizeof(char), size, f) != size) { - fclose(f); - return; - } - fclose(f); - - *length = size; -} - -void buffer_write_to_filename(const char *filename, const char *buffer, uint64_t length) -{ - FILE *f; - - f = fopen(filename, "wb"); - if (f) { - fwrite(buffer, sizeof(char), length, f); - fclose(f); - } -} - -int plist_read_from_filename(plist_t *plist, const char *filename) -{ - char *buffer = NULL; - uint64_t length; - - if (!filename) - return 0; - - buffer_read_from_filename(filename, &buffer, &length); - - if (!buffer) { - return 0; - } - - if ((length > 8) && (memcmp(buffer, "bplist00", 8) == 0)) { - plist_from_bin(buffer, length, plist); - } else { - plist_from_xml(buffer, length, plist); - } - - free(buffer); - - return 1; -} - -int plist_write_to_filename(plist_t plist, const char *filename, enum plist_format_t format) -{ - char *buffer = NULL; - uint32_t length; - - if (!plist || !filename) - return 0; - - if (format == PLIST_FORMAT_XML) - plist_to_xml(plist, &buffer, &length); - else if (format == PLIST_FORMAT_BINARY) - plist_to_bin(plist, &buffer, &length); - else - return 0; - - buffer_write_to_filename(filename, buffer, length); - - free(buffer); - - return 1; -} - -static const char base64_str[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; -static const char base64_pad = '='; - -static char *base64encode(const unsigned char *buf, size_t size) -{ - if (!buf || !(size > 0)) return NULL; - int outlen = (size / 3) * 4; - char *outbuf = (char*)malloc(outlen+5); // 4 spare bytes + 1 for '\0' - size_t n = 0; - size_t m = 0; - unsigned char input[3]; - unsigned int output[4]; - while (n < size) { - input[0] = buf[n]; - input[1] = (n+1 < size) ? buf[n+1] : 0; - input[2] = (n+2 < size) ? buf[n+2] : 0; - output[0] = input[0] >> 2; - output[1] = ((input[0] & 3) << 4) + (input[1] >> 4); - output[2] = ((input[1] & 15) << 2) + (input[2] >> 6); - output[3] = input[2] & 63; - outbuf[m++] = base64_str[(int)output[0]]; - outbuf[m++] = base64_str[(int)output[1]]; - outbuf[m++] = (n+1 < size) ? base64_str[(int)output[2]] : base64_pad; - outbuf[m++] = (n+2 < size) ? base64_str[(int)output[3]] : base64_pad; - n+=3; - } - outbuf[m] = 0; // 0-termination! - return outbuf; -} - -static void plist_node_print_to_stream(plist_t node, int* indent_level, FILE* stream); - -static void plist_array_print_to_stream(plist_t node, int* indent_level, FILE* stream) -{ - /* iterate over items */ - int i, count; - plist_t subnode = NULL; - - count = plist_array_get_size(node); - - for (i = 0; i < count; i++) { - subnode = plist_array_get_item(node, i); - fprintf(stream, "%*s", *indent_level, ""); - fprintf(stream, "%d: ", i); - plist_node_print_to_stream(subnode, indent_level, stream); - } -} - -static void plist_dict_print_to_stream(plist_t node, int* indent_level, FILE* stream) -{ - /* iterate over key/value pairs */ - plist_dict_iter it = NULL; - - char* key = NULL; - plist_t subnode = NULL; - plist_dict_new_iter(node, &it); - plist_dict_next_item(node, it, &key, &subnode); - while (subnode) - { - fprintf(stream, "%*s", *indent_level, ""); - fprintf(stream, "%s", key); - if (plist_get_node_type(subnode) == PLIST_ARRAY) - fprintf(stream, "[%d]: ", plist_array_get_size(subnode)); - else - fprintf(stream, ": "); - free(key); - key = NULL; - plist_node_print_to_stream(subnode, indent_level, stream); - plist_dict_next_item(node, it, &key, &subnode); - } - free(it); -} - -static void plist_node_print_to_stream(plist_t node, int* indent_level, FILE* stream) -{ - char *s = NULL; - char *data = NULL; - double d; - uint8_t b; - uint64_t u = 0; - struct timeval tv = { 0, 0 }; - - plist_type t; - - if (!node) - return; - - t = plist_get_node_type(node); - - switch (t) { - case PLIST_BOOLEAN: - plist_get_bool_val(node, &b); - fprintf(stream, "%s\n", (b ? "true" : "false")); - break; - - case PLIST_UINT: - plist_get_uint_val(node, &u); - fprintf(stream, "%"PRIu64"\n", u); - break; - - case PLIST_REAL: - plist_get_real_val(node, &d); - fprintf(stream, "%f\n", d); - break; - - case PLIST_STRING: - plist_get_string_val(node, &s); - fprintf(stream, "%s\n", s); - free(s); - break; - - case PLIST_KEY: - plist_get_key_val(node, &s); - fprintf(stream, "%s: ", s); - free(s); - break; - - case PLIST_DATA: - plist_get_data_val(node, &data, &u); - if (u > 0) { - s = base64encode((unsigned char*)data, u); - free(data); - if (s) { - fprintf(stream, "%s\n", s); - free(s); - } else { - fprintf(stream, "\n"); - } - } else { - fprintf(stream, "\n"); - } - break; - - case PLIST_DATE: - plist_get_date_val(node, (int32_t*)&tv.tv_sec, (int32_t*)&tv.tv_usec); - { - time_t ti = (time_t)tv.tv_sec + MAC_EPOCH; - struct tm *btime = localtime(&ti); - if (btime) { - s = (char*)malloc(24); - memset(s, 0, 24); - if (strftime(s, 24, "%Y-%m-%dT%H:%M:%SZ", btime) <= 0) { - free (s); - s = NULL; - } - } - } - if (s) { - fprintf(stream, "%s\n", s); - free(s); - } else { - fprintf(stream, "\n"); - } - break; - - case PLIST_ARRAY: - fprintf(stream, "\n"); - (*indent_level)++; - plist_array_print_to_stream(node, indent_level, stream); - (*indent_level)--; - break; - - case PLIST_DICT: - fprintf(stream, "\n"); - (*indent_level)++; - plist_dict_print_to_stream(node, indent_level, stream); - (*indent_level)--; - break; - - default: - break; - } -} - -void plist_print_to_stream(plist_t plist, FILE* stream) -{ - int indent = 0; - - if (!plist || !stream) - return; - - switch (plist_get_node_type(plist)) { - case PLIST_DICT: - plist_dict_print_to_stream(plist, &indent, stream); - break; - case PLIST_ARRAY: - plist_array_print_to_stream(plist, &indent, stream); - break; - default: - plist_node_print_to_stream(plist, &indent, stream); - } -} diff --git a/common/utils.h b/common/utils.h deleted file mode 100644 index 2c3acec..0000000 --- a/common/utils.h +++ /dev/null @@ -1,64 +0,0 @@ -/* - * utils.h - * Miscellaneous utilities for string manipulation, - * file I/O and plist helper. - * - * Copyright (c) 2014-2019 Nikias Bassen, All Rights Reserved. - * Copyright (c) 2013-2014 Martin Szulecki, All Rights Reserved. - * Copyright (c) 2013 Federico Mena Quintero - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - */ - -#ifndef __UTILS_H -#define __UTILS_H - -#ifdef HAVE_CONFIG_H -#include -#endif - -#ifdef WIN32 -#include -#endif - -#include -#include - -#define MAC_EPOCH 978307200 - -#ifndef HAVE_STPCPY -char *stpcpy(char *s1, const char *s2); -#endif -char *string_concat(const char *str, ...); -char *string_append(char *str, ...); -char *string_build_path(const char *elem, ...); -char *string_format_size(uint64_t size); -char *string_toupper(char *str); -char *generate_uuid(void); - -void buffer_read_from_filename(const char *filename, char **buffer, uint64_t *length); -void buffer_write_to_filename(const char *filename, const char *buffer, uint64_t length); - -enum plist_format_t { - PLIST_FORMAT_XML, - PLIST_FORMAT_BINARY -}; - -int plist_read_from_filename(plist_t *plist, const char *filename); -int plist_write_to_filename(plist_t plist, const char *filename, enum plist_format_t format); - -void plist_print_to_stream(plist_t plist, FILE* stream); - -#endif diff --git a/configure.ac b/configure.ac index a95bc69..ed9270c 100644 --- a/configure.ac +++ b/configure.ac @@ -20,10 +20,12 @@ LIBIMOBILEDEVICE_SO_VERSION=6:0:0 dnl Minimum package versions LIBUSBMUXD_VERSION=2.0.2 LIBPLIST_VERSION=2.2.0 +LIMD_GLUE_VERSION=1.0.0 AC_SUBST(LIBIMOBILEDEVICE_SO_VERSION) AC_SUBST(LIBUSBMUXD_VERSION) AC_SUBST(LIBPLIST_VERSION) +AC_SUBST(LIMD_GLUE_VERSION) # Checks for programs. AC_PROG_CC @@ -34,6 +36,7 @@ LT_INIT # Checks for libraries. PKG_CHECK_MODULES(libusbmuxd, libusbmuxd-2.0 >= $LIBUSBMUXD_VERSION) PKG_CHECK_MODULES(libplist, libplist-2.0 >= $LIBPLIST_VERSION) +PKG_CHECK_MODULES(limd_glue, libimobiledevice-glue-1.0 >= $LIMD_GLUE_VERSION) # Checks for header files. AC_CHECK_HEADERS([stdint.h stdlib.h string.h gcrypt.h]) diff --git a/src/Makefile.am b/src/Makefile.am index 96fe963..183a745 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -6,6 +6,7 @@ AM_CFLAGS = \ $(GLOBAL_CFLAGS) \ $(libusbmuxd_CFLAGS) \ $(libplist_CFLAGS) \ + $(limd_glue_CFLAGS) \ $(ssl_lib_CFLAGS) \ $(LFS_CFLAGS) \ $(PTHREAD_CFLAGS) @@ -13,6 +14,7 @@ AM_CFLAGS = \ AM_LDFLAGS = \ $(libusbmuxd_LIBS) \ $(libplist_LIBS) \ + $(limd_glue_LIBS) \ $(ssl_lib_LIBS) \ $(PTHREAD_LIBS) diff --git a/src/afc.h b/src/afc.h index 3e4ef67..6bfdf56 100644 --- a/src/afc.h +++ b/src/afc.h @@ -28,7 +28,7 @@ #include "libimobiledevice/afc.h" #include "service.h" #include "endianness.h" -#include "common/thread.h" +#include #define AFC_MAGIC "CFA6LPAA" #define AFC_MAGIC_LEN (8) diff --git a/src/companion_proxy.c b/src/companion_proxy.c index f09b416..92bc7f1 100644 --- a/src/companion_proxy.c +++ b/src/companion_proxy.c @@ -29,7 +29,6 @@ #include "companion_proxy.h" #include "lockdown.h" #include "common/debug.h" -#include "common/thread.h" /** * Convert a property_list_service_error_t value to a companion_proxy_error_t value. diff --git a/src/companion_proxy.h b/src/companion_proxy.h index 0314b67..0226640 100644 --- a/src/companion_proxy.h +++ b/src/companion_proxy.h @@ -24,7 +24,7 @@ #include "libimobiledevice/companion_proxy.h" #include "property_list_service.h" -#include "common/thread.h" +#include struct companion_proxy_client_private { property_list_service_client_t parent; diff --git a/src/debugserver.c b/src/debugserver.c index 1774087..b6a8b62 100644 --- a/src/debugserver.c +++ b/src/debugserver.c @@ -29,10 +29,11 @@ #define __USE_GNU 1 #include +#include + #include "debugserver.h" #include "lockdown.h" #include "common/debug.h" -#include "common/utils.h" #include "asprintf.h" /** diff --git a/src/idevice.c b/src/idevice.c index 869ecac..4545bfa 100644 --- a/src/idevice.c +++ b/src/idevice.c @@ -48,10 +48,11 @@ #error No supported TLS/SSL library enabled #endif +#include +#include + #include "idevice.h" #include "common/userpref.h" -#include "common/socket.h" -#include "common/thread.h" #include "common/debug.h" #ifdef WIN32 diff --git a/src/installation_proxy.h b/src/installation_proxy.h index 66dd5d0..033bdef 100644 --- a/src/installation_proxy.h +++ b/src/installation_proxy.h @@ -25,7 +25,7 @@ #include "libimobiledevice/installation_proxy.h" #include "property_list_service.h" -#include "common/thread.h" +#include struct instproxy_client_private { property_list_service_client_t parent; diff --git a/src/lockdown.c b/src/lockdown.c index 8fc2c49..70db834 100644 --- a/src/lockdown.c +++ b/src/lockdown.c @@ -34,13 +34,13 @@ #include #include #include +#include #include "property_list_service.h" #include "lockdown.h" #include "idevice.h" #include "common/debug.h" #include "common/userpref.h" -#include "common/utils.h" #include "asprintf.h" #ifdef WIN32 diff --git a/src/mobile_image_mounter.h b/src/mobile_image_mounter.h index e9754e4..55c9cf2 100644 --- a/src/mobile_image_mounter.h +++ b/src/mobile_image_mounter.h @@ -24,7 +24,7 @@ #include "libimobiledevice/mobile_image_mounter.h" #include "property_list_service.h" -#include "common/thread.h" +#include struct mobile_image_mounter_client_private { property_list_service_client_t parent; diff --git a/src/notification_proxy.h b/src/notification_proxy.h index f641e25..ea85149 100644 --- a/src/notification_proxy.h +++ b/src/notification_proxy.h @@ -24,7 +24,7 @@ #include "libimobiledevice/notification_proxy.h" #include "property_list_service.h" -#include "common/thread.h" +#include struct np_client_private { property_list_service_client_t parent; diff --git a/src/preboard.h b/src/preboard.h index c5143a9..61263fc 100644 --- a/src/preboard.h +++ b/src/preboard.h @@ -24,7 +24,7 @@ #include "libimobiledevice/preboard.h" #include "property_list_service.h" -#include "common/thread.h" +#include struct preboard_client_private { property_list_service_client_t parent; diff --git a/src/sbservices.h b/src/sbservices.h index 6c047ce..39d822c 100644 --- a/src/sbservices.h +++ b/src/sbservices.h @@ -24,7 +24,7 @@ #include "libimobiledevice/sbservices.h" #include "property_list_service.h" -#include "common/thread.h" +#include struct sbservices_client_private { property_list_service_client_t parent; diff --git a/src/syslog_relay.h b/src/syslog_relay.h index 3e48fa4..86d798e 100644 --- a/src/syslog_relay.h +++ b/src/syslog_relay.h @@ -24,7 +24,7 @@ #include "libimobiledevice/syslog_relay.h" #include "service.h" -#include "common/thread.h" +#include struct syslog_relay_client_private { service_client_t parent; diff --git a/tools/Makefile.am b/tools/Makefile.am index b78f3f2..354cf1c 100644 --- a/tools/Makefile.am +++ b/tools/Makefile.am @@ -31,9 +31,9 @@ bin_PROGRAMS = \ idevicesetlocation ideviceinfo_SOURCES = ideviceinfo.c -ideviceinfo_CFLAGS = $(AM_CFLAGS) -ideviceinfo_LDFLAGS = $(AM_LDFLAGS) -ideviceinfo_LDADD = $(top_builddir)/src/libimobiledevice-1.0.la $(top_builddir)/common/libinternalcommon.la +ideviceinfo_CFLAGS = $(AM_CFLAGS) $(limd_glue_CFLAGS) +ideviceinfo_LDFLAGS = $(AM_LDFLAGS) $(limd_glue_LIBS) +ideviceinfo_LDADD = $(top_builddir)/src/libimobiledevice-1.0.la idevicename_SOURCES = idevicename.c idevicename_CFLAGS = $(AM_CFLAGS) @@ -41,13 +41,13 @@ idevicename_LDFLAGS = $(AM_LDFLAGS) idevicename_LDADD = $(top_builddir)/src/libimobiledevice-1.0.la idevicepair_SOURCES = idevicepair.c -idevicepair_CFLAGS = -I$(top_srcdir) $(AM_CFLAGS) $(ssl_lib_CFLAGS) +idevicepair_CFLAGS = $(AM_CFLAGS) $(ssl_lib_CFLAGS) idevicepair_LDFLAGS = $(AM_LDFLAGS) $(libusbmuxd_LIBS) $(ssl_lib_LIBS) idevicepair_LDADD = $(top_builddir)/src/libimobiledevice-1.0.la $(top_builddir)/common/libinternalcommon.la idevicesyslog_SOURCES = idevicesyslog.c -idevicesyslog_CFLAGS = $(AM_CFLAGS) -idevicesyslog_LDFLAGS = $(AM_LDFLAGS) +idevicesyslog_CFLAGS = $(AM_CFLAGS) $(limd_glue_CFLAGS) +idevicesyslog_LDFLAGS = $(AM_LDFLAGS) $(limd_glue_LIBS) idevicesyslog_LDADD = $(top_builddir)/src/libimobiledevice-1.0.la idevice_id_SOURCES = idevice_id.c @@ -56,19 +56,19 @@ idevice_id_LDFLAGS = $(AM_LDFLAGS) idevice_id_LDADD = $(top_builddir)/src/libimobiledevice-1.0.la idevicebackup_SOURCES = idevicebackup.c -idevicebackup_CFLAGS = $(AM_CFLAGS) $(ssl_lib_CFLAGS) -idevicebackup_LDFLAGS = $(AM_LDFLAGS) $(ssl_lib_LIBS) -idevicebackup_LDADD = $(top_builddir)/src/libimobiledevice-1.0.la $(top_builddir)/common/libinternalcommon.la +idevicebackup_CFLAGS = $(AM_CFLAGS) $(ssl_lib_CFLAGS) $(limd_glue_CFLAGS) +idevicebackup_LDFLAGS = $(AM_LDFLAGS) $(ssl_lib_LIBS) $(limd_glue_LIBS) +idevicebackup_LDADD = $(top_builddir)/src/libimobiledevice-1.0.la idevicebackup2_SOURCES = idevicebackup2.c -idevicebackup2_CFLAGS = $(AM_CFLAGS) -idevicebackup2_LDFLAGS = $(AM_LDFLAGS) -idevicebackup2_LDADD = $(top_builddir)/src/libimobiledevice-1.0.la $(top_builddir)/common/libinternalcommon.la +idevicebackup2_CFLAGS = $(AM_CFLAGS) $(limd_glue_CFLAGS) +idevicebackup2_LDFLAGS = $(AM_LDFLAGS) $(limd_glue_LIBS) +idevicebackup2_LDADD = $(top_builddir)/src/libimobiledevice-1.0.la ideviceimagemounter_SOURCES = ideviceimagemounter.c -ideviceimagemounter_CFLAGS = $(AM_CFLAGS) -ideviceimagemounter_LDFLAGS = $(AM_LDFLAGS) -ideviceimagemounter_LDADD = $(top_builddir)/src/libimobiledevice-1.0.la $(top_builddir)/common/libinternalcommon.la +ideviceimagemounter_CFLAGS = $(AM_CFLAGS) $(limd_glue_CFLAGS) +ideviceimagemounter_LDFLAGS = $(AM_LDFLAGS) $(limd_glue_LIBS) +ideviceimagemounter_LDADD = $(top_builddir)/src/libimobiledevice-1.0.la idevicescreenshot_SOURCES = idevicescreenshot.c idevicescreenshot_CFLAGS = $(AM_CFLAGS) @@ -86,14 +86,14 @@ idevicedate_LDFLAGS = $(AM_LDFLAGS) idevicedate_LDADD = $(top_builddir)/src/libimobiledevice-1.0.la ideviceprovision_SOURCES = ideviceprovision.c -ideviceprovision_CFLAGS = $(AM_CFLAGS) -ideviceprovision_LDFLAGS = $(AM_LDFLAGS) -ideviceprovision_LDADD = $(top_builddir)/src/libimobiledevice-1.0.la $(top_builddir)/common/libinternalcommon.la +ideviceprovision_CFLAGS = $(AM_CFLAGS) $(limd_glue_CFLAGS) +ideviceprovision_LDFLAGS = $(AM_LDFLAGS) $(limd_glue_LIBS) +ideviceprovision_LDADD = $(top_builddir)/src/libimobiledevice-1.0.la idevicedebugserverproxy_SOURCES = idevicedebugserverproxy.c -idevicedebugserverproxy_CFLAGS = -I$(top_srcdir) $(AM_CFLAGS) -idevicedebugserverproxy_LDFLAGS = $(AM_LDFLAGS) -idevicedebugserverproxy_LDADD = $(top_builddir)/src/libimobiledevice-1.0.la $(top_builddir)/common/libinternalcommon.la +idevicedebugserverproxy_CFLAGS = $(AM_CFLAGS) $(limd_glue_CFLAGS) +idevicedebugserverproxy_LDFLAGS = $(AM_LDFLAGS) $(limd_glue_LIBS) +idevicedebugserverproxy_LDADD = $(top_builddir)/src/libimobiledevice-1.0.la idevicediagnostics_SOURCES = idevicediagnostics.c idevicediagnostics_CFLAGS = $(AM_CFLAGS) @@ -101,8 +101,8 @@ idevicediagnostics_LDFLAGS = $(AM_LDFLAGS) idevicediagnostics_LDADD = $(top_builddir)/src/libimobiledevice-1.0.la idevicedebug_SOURCES = idevicedebug.c -idevicedebug_CFLAGS = $(AM_CFLAGS) -idevicedebug_LDFLAGS = $(AM_LDFLAGS) +idevicedebug_CFLAGS = $(AM_CFLAGS) $(limd_glue_CFLAGS) +idevicedebug_LDFLAGS = $(AM_LDFLAGS) $(limd_glue_LIBS) idevicedebug_LDADD = $(top_builddir)/src/libimobiledevice-1.0.la $(top_builddir)/common/libinternalcommon.la idevicenotificationproxy_SOURCES = idevicenotificationproxy.c @@ -111,11 +111,11 @@ idevicenotificationproxy_LDFLAGS = $(AM_LDFLAGS) idevicenotificationproxy_LDADD = $(top_builddir)/src/libimobiledevice-1.0.la idevicecrashreport_SOURCES = idevicecrashreport.c -idevicecrashreport_CFLAGS = -I$(top_srcdir) $(AM_CFLAGS) -idevicecrashreport_LDFLAGS = $(AM_LDFLAGS) -idevicecrashreport_LDADD = $(top_builddir)/src/libimobiledevice-1.0.la $(top_builddir)/common/libinternalcommon.la +idevicecrashreport_CFLAGS = $(AM_CFLAGS) $(limd_glue_CFLAGS) +idevicecrashreport_LDFLAGS = $(AM_LDFLAGS) $(limd_glue_LIBS) +idevicecrashreport_LDADD = $(top_builddir)/src/libimobiledevice-1.0.la idevicesetlocation_SOURCES = idevicesetlocation.c idevicesetlocation_CFLAGS = $(AM_CFLAGS) idevicesetlocation_LDFLAGS = $(AM_LDFLAGS) -idevicesetlocation_LDADD = $(top_builddir)/src/libimobiledevice-1.0.la $(top_builddir)/common/libinternalcommon.la +idevicesetlocation_LDADD = $(top_builddir)/src/libimobiledevice-1.0.la diff --git a/tools/idevicebackup.c b/tools/idevicebackup.c index 8d0f74b..42f020d 100644 --- a/tools/idevicebackup.c +++ b/tools/idevicebackup.c @@ -55,7 +55,7 @@ #include #include #include -#include "common/utils.h" +#include #define MOBILEBACKUP_SERVICE_NAME "com.apple.mobilebackup" #define NP_SERVICE_NAME "com.apple.mobile.notification_proxy" diff --git a/tools/idevicebackup2.c b/tools/idevicebackup2.c index 6ed852f..7c8503e 100644 --- a/tools/idevicebackup2.c +++ b/tools/idevicebackup2.c @@ -45,7 +45,7 @@ #include #include #include -#include "common/utils.h" +#include #include diff --git a/tools/idevicecrashreport.c b/tools/idevicecrashreport.c index 0a03c68..4d3b686 100644 --- a/tools/idevicecrashreport.c +++ b/tools/idevicecrashreport.c @@ -33,7 +33,7 @@ #ifndef WIN32 #include #endif -#include "common/utils.h" +#include #include #include diff --git a/tools/idevicedebugserverproxy.c b/tools/idevicedebugserverproxy.c index 15e8deb..b190f63 100644 --- a/tools/idevicedebugserverproxy.c +++ b/tools/idevicedebugserverproxy.c @@ -41,8 +41,8 @@ #include #include -#include "common/socket.h" -#include "common/thread.h" +#include +#include #define info(...) fprintf(stdout, __VA_ARGS__); fflush(stdout) #define debug(...) if(debug_mode) fprintf(stdout, __VA_ARGS__) diff --git a/tools/ideviceimagemounter.c b/tools/ideviceimagemounter.c index 325a9e2..37c2154 100644 --- a/tools/ideviceimagemounter.c +++ b/tools/ideviceimagemounter.c @@ -46,7 +46,7 @@ #include #include #include -#include "common/utils.h" +#include static int list_mode = 0; static int use_network = 0; diff --git a/tools/ideviceinfo.c b/tools/ideviceinfo.c index 97ae03a..54ae1d2 100644 --- a/tools/ideviceinfo.c +++ b/tools/ideviceinfo.c @@ -37,7 +37,7 @@ #include #include -#include "common/utils.h" +#include #define FORMAT_KEY_VALUE 1 #define FORMAT_XML 2 diff --git a/tools/ideviceprovision.c b/tools/ideviceprovision.c index c93a682..36c69b0 100644 --- a/tools/ideviceprovision.c +++ b/tools/ideviceprovision.c @@ -44,7 +44,7 @@ #include #include #include -#include "common/utils.h" +#include static void print_usage(int argc, char **argv) { diff --git a/tools/idevicesyslog.c b/tools/idevicesyslog.c index d9b627f..4eb1605 100644 --- a/tools/idevicesyslog.c +++ b/tools/idevicesyslog.c @@ -41,10 +41,10 @@ #include #include +#include static int quit_flag = 0; static int exit_on_disconnect = 0; -static int use_colors = 0; static int show_device_name = 0; static char* udid = NULL; @@ -75,58 +75,6 @@ static char *line = NULL; static int line_buffer_size = 0; static int lp = 0; -#ifdef WIN32 -static WORD COLOR_RESET = 0; -static HANDLE h_stdout = INVALID_HANDLE_VALUE; - -#define COLOR_NORMAL COLOR_RESET -#define COLOR_DARK FOREGROUND_INTENSITY -#define COLOR_RED FOREGROUND_RED |FOREGROUND_INTENSITY -#define COLOR_DARK_RED FOREGROUND_RED -#define COLOR_GREEN FOREGROUND_GREEN | FOREGROUND_INTENSITY -#define COLOR_DARK_GREEN FOREGROUND_GREEN -#define COLOR_YELLOW FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY -#define COLOR_DARK_YELLOW FOREGROUND_GREEN | FOREGROUND_RED -#define COLOR_BLUE FOREGROUND_BLUE | FOREGROUND_INTENSITY -#define COLOR_DARK_BLUE FOREGROUND_BLUE -#define COLOR_MAGENTA FOREGROUND_BLUE | FOREGROUND_RED | FOREGROUND_INTENSITY -#define COLOR_DARK_MAGENTA FOREGROUND_BLUE | FOREGROUND_RED -#define COLOR_CYAN FOREGROUND_BLUE | FOREGROUND_GREEN -#define COLOR_BRIGHT_CYAN FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY -#define COLOR_DARK_CYAN FOREGROUND_BLUE | FOREGROUND_GREEN -#define COLOR_WHITE FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY -#define COLOR_DARK_WHITE FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE - -static void TEXT_COLOR(WORD attr) -{ - if (use_colors) { - SetConsoleTextAttribute(h_stdout, attr); - } -} -#else - -#define COLOR_RESET "\e[m" -#define COLOR_NORMAL "\e[0m" -#define COLOR_DARK "\e[2m" -#define COLOR_RED "\e[0;31m" -#define COLOR_DARK_RED "\e[2;31m" -#define COLOR_GREEN "\e[0;32m" -#define COLOR_DARK_GREEN "\e[2;32m" -#define COLOR_YELLOW "\e[0;33m" -#define COLOR_DARK_YELLOW "\e[2;33m" -#define COLOR_BLUE "\e[0;34m" -#define COLOR_DARK_BLUE "\e[2;34m" -#define COLOR_MAGENTA "\e[0;35m" -#define COLOR_DARK_MAGENTA "\e[2;35m" -#define COLOR_CYAN "\e[0;36m" -#define COLOR_BRIGHT_CYAN "\e[1;36m" -#define COLOR_DARK_CYAN "\e[2;36m" -#define COLOR_WHITE "\e[1;37m" -#define COLOR_DARK_WHITE "\e[0;37m" - -#define TEXT_COLOR(x) if (use_colors) { fwrite(x, 1, sizeof(x)-1, stdout); } -#endif - static void add_filter(const char* filterstr) { int filter_len = strlen(filterstr); @@ -201,7 +149,7 @@ static void syslog_callback(char c, void *user_data) do { if (lp < 16) { shall_print = 1; - TEXT_COLOR(COLOR_WHITE); + cprintf(COLOR_WHITE); break; } else if (line[3] == ' ' && line[6] == ' ' && line[15] == ' ') { char* end = &line[lp]; @@ -331,11 +279,7 @@ static void syslog_callback(char c, void *user_data) /* log level */ char* level_start = p; char* level_end = p; -#ifdef WIN32 - WORD level_color = COLOR_NORMAL; -#else const char* level_color = NULL; -#endif if (!strncmp(p, ":", 9)) { level_end += 9; level_color = COLOR_GREEN; @@ -353,24 +297,24 @@ static void syslog_callback(char c, void *user_data) } /* write date and time */ - TEXT_COLOR(COLOR_DARK_WHITE); + cprintf(COLOR_LIGHT_GRAY); fwrite(line, 1, 16, stdout); if (show_device_name) { /* write device name */ - TEXT_COLOR(COLOR_DARK_YELLOW); + cprintf(COLOR_DARK_YELLOW); fwrite(device_name_start, 1, device_name_end-device_name_start+1, stdout); - TEXT_COLOR(COLOR_RESET); + cprintf(COLOR_RESET); } /* write process name */ - TEXT_COLOR(COLOR_BRIGHT_CYAN); + cprintf(COLOR_BRIGHT_CYAN); fwrite(process_name_start, 1, process_name_end-process_name_start, stdout); - TEXT_COLOR(COLOR_CYAN); + cprintf(COLOR_CYAN); fwrite(process_name_end, 1, proc_name_end-process_name_end+1, stdout); /* write log level */ - TEXT_COLOR(level_color); + cprintf(level_color); if (level_end > level_start) { fwrite(level_start, 1, level_end-level_start, stdout); p = level_end; @@ -379,17 +323,17 @@ static void syslog_callback(char c, void *user_data) lp -= p - linep; linep = p; - TEXT_COLOR(COLOR_WHITE); + cprintf(COLOR_WHITE); } else { shall_print = 1; - TEXT_COLOR(COLOR_WHITE); + cprintf(COLOR_WHITE); } } while (0); if ((num_msg_filters == 0 && num_proc_filters == 0 && num_pid_filters == 0 && num_trigger_filters == 0 && num_untrigger_filters == 0) || shall_print) { fwrite(linep, 1, lp, stdout); - TEXT_COLOR(COLOR_RESET); + cprintf(COLOR_RESET); fflush(stdout); if (trigger_off) { triggered = 0; @@ -560,14 +504,6 @@ static void print_usage(int argc, char **argv, int is_error) int main(int argc, char *argv[]) { -#ifdef WIN32 - CONSOLE_SCREEN_BUFFER_INFO csbi; - h_stdout = GetStdHandle(STD_OUTPUT_HANDLE); - if (GetConsoleScreenBufferInfo(h_stdout, &csbi)) { - COLOR_RESET = csbi.wAttributes; - } -#endif - int no_colors = 0; int include_filter = 0; int exclude_filter = 0; int include_kernel = 0; @@ -700,7 +636,7 @@ int main(int argc, char *argv[]) return 0; } case 2: - no_colors = 1; + term_colors_set_enabled(0); break; case 'v': printf("%s %s\n", TOOL_NAME, PACKAGE_VERSION); @@ -756,10 +692,6 @@ int main(int argc, char *argv[]) argc -= optind; argv += optind; - if (!no_colors && isatty(1)) { - use_colors = 1; - } - int num = 0; idevice_info_t *devices = NULL; idevice_get_device_list_extended(&devices, &num); -- cgit v1.1-32-gdbae