Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

android supported #13

Open
wants to merge 21 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ src/.libs
src/usbmuxd
udev/39-usbmuxd.rules
systemd/usbmuxd.service
.DS_Store
4 changes: 4 additions & 0 deletions src/device.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@

#include <sys/time.h>
#include <netinet/in.h>
#ifndef __ANDROID__
#include <netinet/tcp.h>
#else
#include "tcp.h"
#endif
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
Expand Down
6 changes: 6 additions & 0 deletions src/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@
#include "log.h"
#include "utils.h"

#ifdef __ANDROID__
#include <android/log.h>
#define DEBUG(...) __android_log_print(ANDROID_LOG_DEBUG, "usbmuxd", __VA_ARGS__)
#endif

unsigned int log_level = LL_WARNING;

int log_syslog = 0;
Expand Down Expand Up @@ -89,6 +94,7 @@ void usbmuxd_log(enum loglevel level, const char *fmt, ...)
} else {
vfprintf(stderr, fs, ap);
}
DEBUG(fs, ap);
va_end(ap);

free(fs);
Expand Down
1 change: 1 addition & 0 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ static void set_signal_handlers(void)
sigaction(SIGUSR2, &sa, NULL);
}


#ifndef HAVE_PPOLL
static int ppoll(struct pollfd *fds, nfds_t nfds, const struct timespec *timeout, const sigset_t *sigmask)
{
Expand Down
36 changes: 36 additions & 0 deletions src/tcp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include <endian.h>

typedef u_int32_t tcp_seq;

/*
* TCP header.
* Per RFC 793, September, 1981.
*/
struct tcphdr {
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 */
};
274 changes: 137 additions & 137 deletions src/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -170,137 +170,137 @@ char *stpcpy(char * s1, const char * s2)
* @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;
}

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) {
usbmuxd_log(LL_ERROR, "%s: ERROR: couldn't read %d bytes from %s", __func__, (int)size, filename);
}
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;
}
//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;
//}
//
//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) {
// usbmuxd_log(LL_ERROR, "%s: ERROR: couldn't read %d bytes from %s", __func__, (int)size, filename);
// }
// 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;
//}

#ifndef HAVE_CLOCK_GETTIME
typedef int clockid_t;
Expand Down Expand Up @@ -334,13 +334,13 @@ static int clock_gettime(clockid_t clk_id, struct timespec *ts)

void get_tick_count(struct timeval * tv)
{
struct timespec ts;
if(0 == clock_gettime(CLOCK_MONOTONIC, &ts)) {
tv->tv_sec = ts.tv_sec;
tv->tv_usec = ts.tv_nsec / 1000;
} else {
// struct timespec ts;
// if(0 == clock_gettime(CLOCK_MONOTONIC, &ts)) {
// tv->tv_sec = ts.tv_sec;
// tv->tv_usec = ts.tv_nsec / 1000;
// } else {
gettimeofday(tv, NULL);
}
// }
}

/**
Expand Down
Loading