From 0833499c76f78da21fc33874a485946189a33dad Mon Sep 17 00:00:00 2001 From: Nikias Bassen Date: Wed, 26 May 2010 20:30:02 +0200 Subject: libusbmuxd: use winsock API for win32 --- libusbmuxd/CMakeLists.txt | 3 +++ libusbmuxd/libusbmuxd.c | 6 ++++++ libusbmuxd/sock_stuff.c | 26 ++++++++++++++++++++++++++ tools/CMakeLists.txt | 6 +++++- tools/iproxy.c | 12 +++++++++--- 5 files changed, 49 insertions(+), 4 deletions(-) diff --git a/libusbmuxd/CMakeLists.txt b/libusbmuxd/CMakeLists.txt index 236cca3..0d7d897 100644 --- a/libusbmuxd/CMakeLists.txt +++ b/libusbmuxd/CMakeLists.txt @@ -7,6 +7,9 @@ find_library (PTHREAD pthread) if (HAVE_PLIST) message("-- libusbmuxd will be built with protocol version 1 support") endif() +if(WIN32) + set(OPT_LIBS ${OPT_LIBS} ws2_32) +endif() target_link_libraries (libusbmuxd ${CMAKE_THREAD_LIBS_INIT} ${OPT_LIBS}) # 'lib' is a UNIXism, the proper CMake target is usbmuxd diff --git a/libusbmuxd/libusbmuxd.c b/libusbmuxd/libusbmuxd.c index f465deb..80ffdd7 100644 --- a/libusbmuxd/libusbmuxd.c +++ b/libusbmuxd/libusbmuxd.c @@ -26,8 +26,14 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA #include #include #include +#ifdef WIN32 +#include +#include +#define sleep(x) Sleep(x*1000) +#else #include #include +#endif #include #include #include diff --git a/libusbmuxd/sock_stuff.c b/libusbmuxd/sock_stuff.c index 33ac3e6..0592f6b 100644 --- a/libusbmuxd/sock_stuff.c +++ b/libusbmuxd/sock_stuff.c @@ -29,11 +29,17 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA #include #include #include +#ifdef WIN32 +#include +#include +static int wsa_init = 0; +#else #include #include #include #include #include +#endif #include "sock_stuff.h" #define RECV_TIMEOUT 20000 @@ -143,6 +149,16 @@ int create_socket(uint16_t port) { int sfd = -1; int yes = 1; +#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 struct sockaddr_in saddr; if (0 > (sfd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP))) { @@ -182,6 +198,16 @@ int connect_socket(const char *addr, uint16_t port) int yes = 1; struct hostent *hp; struct sockaddr_in saddr; +#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 if (!addr) { errno = EINVAL; diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt index 08ea714..64d0d0e 100644 --- a/tools/CMakeLists.txt +++ b/tools/CMakeLists.txt @@ -2,6 +2,10 @@ include_directories (${CMAKE_SOURCE_DIR}/libusbmuxd) link_directories (${CMAKE_BINARY_DIR}/libusbmuxd) add_executable(iproxy iproxy.c) -target_link_libraries(iproxy libusbmuxd pthread) +if(WIN32) + target_link_libraries(iproxy libusbmuxd pthread ws2_32) +else() + target_link_libraries(iproxy libusbmuxd pthread) +endif() install(TARGETS iproxy RUNTIME DESTINATION bin) diff --git a/tools/iproxy.c b/tools/iproxy.c index 707724a..1855c67 100644 --- a/tools/iproxy.c +++ b/tools/iproxy.c @@ -30,11 +30,17 @@ TODO: improve code... #include #include #include -#include -#include #include #include +#ifdef WIN32 +#include +#include +typedef unsigned int socklen_t; +#else +#include +#include #include +#endif #include #include "sock_stuff.h" #include "usbmuxd.h" @@ -98,7 +104,7 @@ void *run_ctos_loop(void *arg) int recv_len; int sent; char buffer[131072]; - pthread_t stoc = 0; + pthread_t stoc; printf("%s: fd = %d\n", __func__, cdata->fd); -- cgit v1.1-32-gdbae