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

Support usbmuxd on Windows #55

Closed
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 @@ -34,5 +34,6 @@ py-compile
stamp-h1
src/.libs
src/usbmuxd
src/usbmuxd.exe
udev/39-usbmuxd.rules
systemd/usbmuxd.service
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Bastien Nocera
Cerrato Renaud
Christophe Fergeau
David Sansome
Frederik Carlier
Hector Martin
Jacob Myers
John Maguire
Expand Down
29 changes: 26 additions & 3 deletions src/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*
* Copyright (C) 2009 Hector Martin <[email protected]>
* Copyright (C) 2009 Nikias Bassen <[email protected]>
* Copyright (C) 2014 Frederik Carlier <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -22,16 +23,27 @@
#include <config.h>
#endif

#ifdef _MSC_VER
#include "config_msc.h"
#endif

#define _GNU_SOURCE 1

#ifdef WIN32
#include <ws2tcpip.h>
#include <winsock2.h>
#include "winsock2-ext.h"
#else
#include <sys/socket.h>
#include <sys/un.h>
#include <arpa/inet.h>
#endif

#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <fcntl.h>

Expand Down Expand Up @@ -162,6 +174,10 @@ int client_accept(int listenfd)
return cfd;
}

#ifdef WIN32
u_long iMode = 1;
ioctlsocket(cfd, FIONBIO, &iMode);
#else
int flags = fcntl(cfd, F_GETFL, 0);
if (flags < 0) {
usbmuxd_log(LL_ERROR, "ERROR: Could not get socket flags!");
Expand All @@ -170,6 +186,7 @@ int client_accept(int listenfd)
usbmuxd_log(LL_ERROR, "ERROR: Could not set socket to non-blocking mode");
}
}
#endif

struct mux_client *client;
client = malloc(sizeof(struct mux_client));
Expand Down Expand Up @@ -215,7 +232,13 @@ void client_close(struct mux_client *client)
client->state = CLIENT_DEAD;
device_abort_connect(client->connect_device, client);
}

#ifdef WIN32
closesocket(client->fd);
#else
close(client->fd);
#endif

if(client->ob_buf)
free(client->ob_buf);
if(client->ib_buf)
Expand Down
1 change: 1 addition & 0 deletions src/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*
* Copyright (C) 2009 Hector Martin <[email protected]>
* Copyright (C) 2009 Nikias Bassen <[email protected]>
* Copyright (C) 2014 Frederik Carlier <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down
9 changes: 9 additions & 0 deletions src/conf.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* conf.c
*
* Copyright (C) 2013 Nikias Bassen <[email protected]>
* Copyright (C) 2016 Frederik Carlier <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -21,6 +22,11 @@
#include <config.h>
#endif

#include <Windows.h>
#ifdef _MSC_VER
#include "config_msc.h"
#endif

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
Expand All @@ -31,12 +37,15 @@
#endif

#include <dirent.h>
#ifndef _MSC_VER
#include <libgen.h>
#endif
#include <sys/stat.h>
#include <errno.h>

#ifdef WIN32
#include <shlobj.h>
#include <direct.h>
#endif

#include "conf.h"
Expand Down
8 changes: 8 additions & 0 deletions src/config_msc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include "config_msc.h"

char *dirname(char const *file)
{
char* dir[256];
_splitpath(file, 0, dir, 0, 0);
return dir;
}
8 changes: 8 additions & 0 deletions src/config_msc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once
#define PACKAGE_NAME "usbmuxd"
#define PACKAGE_STRING "usbmuxd 1.1.1"
#define PACKAGE_VERSION "1.1.1"
#define HAVE_STRUCT_TIMESPEC 1
#include <stdlib.h>

char *dirname(char const *file);
17 changes: 16 additions & 1 deletion src/device.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*
* Copyright (C) 2009 Hector Martin "marcan" <[email protected]>
* Copyright (C) 2014 Mikkel Kamstrup Erlandsen <[email protected]>
* Copyright (C) 2016 Frederik Carlier <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -24,9 +25,21 @@
#include <config.h>
#endif

#include <sys/time.h>
#ifdef _MSC_VER
#include "config_msc.h"
#endif

#ifdef WIN32
#include <ws2tcpip.h>
#include <winsock2.h>
#include "winsock2-ext.h"
#include "tcp.h"
#else
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <sys/time.h>
#endif

#include <stdlib.h>
#include <string.h>
#include <stdint.h>
Expand Down Expand Up @@ -937,6 +950,7 @@ int device_get_list(int include_hidden, struct device_info **devices)
return count;
}

#ifndef WIN32
int device_get_timeout(void)
{
uint64_t oldest = (uint64_t)-1LL;
Expand Down Expand Up @@ -976,6 +990,7 @@ void device_check_timeouts(void)
} ENDFOREACH
pthread_mutex_unlock(&device_list_mutex);
}
#endif

void device_init(void)
{
Expand Down
3 changes: 3 additions & 0 deletions src/device.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* device.h
*
* Copyright (C) 2009 Hector Martin <[email protected]>
* Copyright (C) 2016 Frederik Carlier <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -46,8 +47,10 @@ void device_set_preflight_cb_data(int device_id, void* data);
int device_get_count(int include_hidden);
int device_get_list(int include_hidden, struct device_info **devices);

#ifndef WIN32
int device_get_timeout(void);
void device_check_timeouts(void);
#endif

void device_init(void);
void device_kill_connections(void);
Expand Down
31 changes: 30 additions & 1 deletion src/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*
* Copyright (C) 2009 Hector Martin <[email protected]>
* Copyright (C) 2009 Nikias Bassen <[email protected]>
* Copyright (C) 2014 Frederik Carlier <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -27,14 +28,25 @@
#include <string.h>
#include <stdarg.h>
#include <time.h>
#ifdef _MSC_VER
// Includes a definition for 'timeval'
#include "winsock2.h"
#else
#include <sys/time.h>
#endif

#ifdef WIN32
#else
#include <syslog.h>
#endif

#include "log.h"
#include "utils.h"

unsigned int log_level = LL_WARNING;

#ifdef WIN32
#else
int log_syslog = 0;

void log_enable_syslog()
Expand All @@ -60,6 +72,7 @@ static int level_to_syslog_level(int level)
}
return result;
}
#endif

void usbmuxd_log(enum loglevel level, const char *fmt, ...)
{
Expand All @@ -72,23 +85,39 @@ void usbmuxd_log(enum loglevel level, const char *fmt, ...)
return;

get_tick_count(&ts);
tp = localtime(&ts.tv_sec);

fs = malloc(20 + strlen(fmt));

#ifdef WIN32
time_t ltime;
time(&ltime);
tp = localtime(&ltime);

strftime(fs, 10, "[%H:%M:%S", tp);
sprintf(fs + 9, ".%03d][%d] %s\n", (int)(ts.tv_usec / 1000), level, fmt);
#else
tp = localtime(&ts.tv_sec);

if(log_syslog) {
sprintf(fs, "[%d] %s\n", level, fmt);
} else {
strftime(fs, 10, "[%H:%M:%S", tp);
sprintf(fs+9, ".%03d][%d] %s\n", (int)(ts.tv_usec / 1000), level, fmt);
}
#endif

va_start(ap, fmt);

#ifdef WIN32
vfprintf(stderr, fs, ap);
#else
if (log_syslog) {
vsyslog(level_to_syslog_level(level), fs, ap);
} else {
vfprintf(stderr, fs, ap);
}
#endif

va_end(ap);

free(fs);
Expand Down
10 changes: 9 additions & 1 deletion src/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*
* Copyright (C) 2009 Hector Martin "marcan" <[email protected]>
* Copyright (C) 2009 Nikias Bassen <[email protected]>
* Copyright (C) 2016 Frederik Carlier <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -34,9 +35,16 @@ enum loglevel {

extern unsigned int log_level;

#ifdef WIN32
#else
void log_enable_syslog();
void log_disable_syslog();
#endif

void usbmuxd_log(enum loglevel level, const char *fmt, ...) __attribute__ ((format (printf, 2, 3)));
void usbmuxd_log(enum loglevel level, const char *fmt, ...)
#ifndef _MSC_VER
__attribute__ ((format (printf, 2, 3)))
#endif
;

#endif
Loading