From 5745de43d0435a6024e2f79f5559b56dbed3d100 Mon Sep 17 00:00:00 2001 From: lunzii Date: Fri, 25 Oct 2013 12:03:35 +0800 Subject: [PATCH] Port to android, build succeed with android-ndk-r8e on mac osx 10.9. --- daemon/device.c | 112 +++++++++++++++++++++++++++---------- daemon/device.h | 41 ++++++++++++++ daemon/main.c | 11 +++- libusbmuxd/libusbmuxd.c | 5 +- libusbmuxd/usbmuxd-proto.h | 4 ++ 5 files changed, 139 insertions(+), 34 deletions(-) diff --git a/daemon/device.c b/daemon/device.c index 8c786a72..399bb0c8 100644 --- a/daemon/device.c +++ b/daemon/device.c @@ -148,7 +148,11 @@ static int send_packet(struct mux_device *dev, enum mux_protocol proto, void *he hdrlen = sizeof(struct version_header); break; case MUX_PROTO_TCP: - hdrlen = sizeof(struct tcphdr); +#ifdef ANDROID + hdrlen = sizeof(struct tcphdr_bsd); +#else + hdrlen = sizeof(struct tcphdr); +#endif break; default: usbmuxd_log(LL_ERROR, "Invalid protocol %d for outgoing packet (dev %d hdr %p data %p len %d)", proto, dev->id, header, data, length); @@ -200,7 +204,11 @@ static uint16_t find_sport(struct mux_device *dev) static int send_anon_rst(struct mux_device *dev, uint16_t sport, uint16_t dport, uint32_t ack) { - struct tcphdr th; +#ifdef ANDROID + struct tcphdr_bsd th; +#else + struct tcphdr th; +#endif memset(&th, 0, sizeof(th)); th.th_sport = htons(sport); th.th_dport = htons(dport); @@ -216,7 +224,11 @@ static int send_anon_rst(struct mux_device *dev, uint16_t sport, uint16_t dport, static int send_tcp(struct mux_connection *conn, uint8_t flags, const unsigned char *data, int length) { - struct tcphdr th; +#ifdef ANDROID + struct tcphdr_bsd th; +#else + struct tcphdr th; +#endif memset(&th, 0, sizeof(th)); th.th_sport = htons(conn->sport); th.th_dport = htons(conn->dport); @@ -300,7 +312,12 @@ int device_start_connect(int device_id, uint16_t dport, struct mux_client *clien conn->tx_win = 131072; conn->rx_recvd = 0; conn->flags = 0; - conn->max_payload = USB_MTU - sizeof(struct mux_header) - sizeof(struct tcphdr); + +#ifdef ANDROID + conn->max_payload = USB_MTU - sizeof(struct mux_header) - sizeof(struct tcphdr_bsd); +#else + conn->max_payload = USB_MTU - sizeof(struct mux_header) - sizeof(struct tcphdr); +#endif conn->ob_buf = malloc(CONN_OUTBUF_SIZE); conn->ob_capacity = CONN_OUTBUF_SIZE; @@ -464,7 +481,12 @@ static void device_version_input(struct mux_device *dev, struct version_header * client_device_add(&info); } -static void device_tcp_input(struct mux_device *dev, struct tcphdr *th, unsigned char *payload, uint32_t payload_length) + +#ifdef ANDROID + static void device_tcp_input(struct mux_device *dev, struct tcphdr_bsd *th, unsigned char *payload, uint32_t payload_length) +#else + static void device_tcp_input(struct mux_device *dev, struct tcphdr *th, unsigned char *payload, uint32_t payload_length) +#endif { uint16_t sport = ntohs(th->th_dport); uint16_t dport = ntohs(th->th_sport); @@ -603,32 +625,62 @@ void device_data_input(struct usb_device *usbdev, unsigned char *buffer, uint32_ return; } - struct tcphdr *th; - unsigned char *payload; - uint32_t payload_length; +#ifdef ANDROID + struct tcphdr_bsd *th; + unsigned char *payload; + uint32_t payload_length; - switch(ntohl(mhdr->protocol)) { - case MUX_PROTO_VERSION: - if(length < (sizeof(struct mux_header) + sizeof(struct version_header))) { - usbmuxd_log(LL_ERROR, "Incoming version packet is too small (%d)", length); - return; - } - device_version_input(dev, (struct version_header *)(mhdr+1)); - break; - case MUX_PROTO_TCP: - if(length < (sizeof(struct mux_header) + sizeof(struct tcphdr))) { - usbmuxd_log(LL_ERROR, "Incoming TCP packet is too small (%d)", length); - return; - } - th = (struct tcphdr *)(mhdr+1); - payload = (unsigned char *)(th+1); - payload_length = length - sizeof(struct tcphdr) - sizeof(struct mux_header); - device_tcp_input(dev, (struct tcphdr *)(mhdr+1), payload, payload_length); - break; - default: - usbmuxd_log(LL_ERROR, "Incoming packet for device %d has unknown protocol 0x%x)", dev->id, ntohl(mhdr->protocol)); - break; - } + switch(ntohl(mhdr->protocol)) { + case MUX_PROTO_VERSION: + if(length < (sizeof(struct mux_header) + sizeof(struct version_header))) { + usbmuxd_log(LL_ERROR, "Incoming version packet is too small (%d)", length); + return; + } + device_version_input(dev, (struct version_header *)(mhdr+1)); + break; + case MUX_PROTO_TCP: + if(length < (sizeof(struct mux_header) + sizeof(struct tcphdr))) { + usbmuxd_log(LL_ERROR, "Incoming TCP packet is too small (%d)", length); + return; + } + th = (struct tcphdr_bsd *)(mhdr+1); + payload = (unsigned char *)(th+1); + payload_length = length - sizeof(struct tcphdr_bsd) - sizeof(struct mux_header); + device_tcp_input(dev, (struct tcphdr_bsd *)(mhdr+1), payload, payload_length); + break; + default: + usbmuxd_log(LL_ERROR, "Incoming packet for device %d has unknown protocol 0x%x)", dev->id, ntohl(mhdr->protocol)); + break; + } +#else + struct tcphdr *th; + unsigned char *payload; + uint32_t payload_length; + + switch(ntohl(mhdr->protocol)) { + case MUX_PROTO_VERSION: + if(length < (sizeof(struct mux_header) + sizeof(struct version_header))) { + usbmuxd_log(LL_ERROR, "Incoming version packet is too small (%d)", length); + return; + } + device_version_input(dev, (struct version_header *)(mhdr+1)); + break; + case MUX_PROTO_TCP: + if(length < (sizeof(struct mux_header) + sizeof(struct tcphdr))) { + usbmuxd_log(LL_ERROR, "Incoming TCP packet is too small (%d)", length); + return; + } + th = (struct tcphdr *)(mhdr+1); + payload = (unsigned char *)(th+1); + payload_length = length - sizeof(struct tcphdr) - sizeof(struct mux_header); + device_tcp_input(dev, (struct tcphdr *)(mhdr+1), payload, payload_length); + break; + default: + usbmuxd_log(LL_ERROR, "Incoming packet for device %d has unknown protocol 0x%x)", dev->id, ntohl(mhdr->protocol)); + break; + } +#endif + } diff --git a/daemon/device.h b/daemon/device.h index ea770697..a06cd071 100644 --- a/daemon/device.h +++ b/daemon/device.h @@ -49,4 +49,45 @@ void device_check_timeouts(void); void device_init(void); void device_kill_connections(void); void device_shutdown(void); + +#ifdef ANDROID +#include + +typedef u_int32_t tcp_seq; + +/* + * TCP header. + * Per RFC 793, September, 1981. + */ +struct tcphdr_bsd { + u_short th_sport; /* source port */ + u_short th_dport; /* destination port */ + tcp_seq th_seq; /* sequence number */ + tcp_seq th_ack; /* acknowledgement number */ + #if __BYTE_ORDER == __LITTLE_ENDIAN + u_int th_x2:4, /* (unused) */ + th_off:4; /* data offset */ + #endif + #if __BYTE_ORDER == __BIG_ENDIAN + u_int th_off:4, /* data offset */ + th_x2:4; /* (unused) */ + #endif + u_char th_flags; + #define TH_FIN 0x01 + #define TH_SYN 0x02 + #define TH_RST 0x04 + #define TH_PUSH 0x08 + #define TH_ACK 0x10 + #define TH_URG 0x20 + #define TH_ECE 0x40 + #define TH_CWR 0x80 + #define TH_FLAGS (TH_FIN|TH_SYN|TH_RST|TH_ACK|TH_URG|TH_ECE|TH_CWR) + + u_short th_win; /* window */ + u_short th_sum; /* checksum */ + u_short th_urp; /* urgent pointer */ +}; + +#endif + #endif diff --git a/daemon/main.c b/daemon/main.c index 140bee15..0eaae8bf 100644 --- a/daemon/main.c +++ b/daemon/main.c @@ -47,8 +47,13 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA #include "device.h" #include "client.h" -static const char *socket_path = "/var/run/usbmuxd"; -static const char *lockfile = "/var/run/usbmuxd.pid"; +#ifdef ANDROID + static const char *socket_path = "/data/local/tmp/usbmuxd"; + static const char *lockfile = "/data/local/tmp/usbmuxd.pid"; +#else + static const char *socket_path = "/var/run/usbmuxd"; + static const char *lockfile = "/var/run/usbmuxd.pid"; +#endif int should_exit; int should_discover; @@ -147,7 +152,7 @@ void set_signal_handlers(void) sigaction(SIGUSR2, &sa, NULL); } -#if defined(__FreeBSD__) || defined(__APPLE__) +#if defined(__FreeBSD__) || defined(__APPLE__) || defined(ANDROID) static int ppoll(struct pollfd *fds, nfds_t nfds, const struct timespec *timeout, const sigset_t *sigmask) { int ready; diff --git a/libusbmuxd/libusbmuxd.c b/libusbmuxd/libusbmuxd.c index 20ac8ab2..64d1a489 100644 --- a/libusbmuxd/libusbmuxd.c +++ b/libusbmuxd/libusbmuxd.c @@ -690,7 +690,10 @@ int usbmuxd_unsubscribe() } #else if (pthread_kill(devmon, 0) == 0) { - pthread_cancel(devmon); +#ifdef ANDROID +#else + pthread_cancel(devmon); +#endif pthread_join(devmon, NULL); } #endif diff --git a/libusbmuxd/usbmuxd-proto.h b/libusbmuxd/usbmuxd-proto.h index be9e7092..33e58df6 100644 --- a/libusbmuxd/usbmuxd-proto.h +++ b/libusbmuxd/usbmuxd-proto.h @@ -31,8 +31,12 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA #if defined(WIN32) || defined(__CYGWIN__) #define USBMUXD_SOCKET_PORT 27015 #else +#ifdef ANDROID +#define USBMUXD_SOCKET_FILE "/data/local/tmp/usbmuxd" +#else #define USBMUXD_SOCKET_FILE "/var/run/usbmuxd" #endif +#endif #ifdef __cplusplus extern "C" {