Skip to content

Commit

Permalink
Port to android, build succeed with android-ndk-r8e on mac osx 10.9.
Browse files Browse the repository at this point in the history
  • Loading branch information
lunzii committed Oct 25, 2013
1 parent 10939f3 commit 5745de4
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 34 deletions.
112 changes: 82 additions & 30 deletions daemon/device.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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


}

Expand Down
41 changes: 41 additions & 0 deletions daemon/device.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <endian.h>

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
11 changes: 8 additions & 3 deletions daemon/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
5 changes: 4 additions & 1 deletion libusbmuxd/libusbmuxd.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions libusbmuxd/usbmuxd-proto.h
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down

0 comments on commit 5745de4

Please sign in to comment.