From d09e25b69d2744ba7a6a88cc58d9cc8fc4810d2a Mon Sep 17 00:00:00 2001 From: Martin Szulecki Date: Tue, 4 Sep 2012 00:36:40 +0200 Subject: Add new idevicedebugserverproxy tool --- tools/Makefile.am | 7 +- tools/idevicedebugserverproxy.c | 374 ++++++++++++++++++++++++++++++++++++++ tools/socket.c | 392 ++++++++++++++++++++++++++++++++++++++++ tools/socket.h | 65 +++++++ tools/thread.c | 47 +++++ tools/thread.h | 38 ++++ 6 files changed, 922 insertions(+), 1 deletion(-) create mode 100644 tools/idevicedebugserverproxy.c create mode 100644 tools/socket.c create mode 100644 tools/socket.h create mode 100644 tools/thread.c create mode 100644 tools/thread.h (limited to 'tools') diff --git a/tools/Makefile.am b/tools/Makefile.am index 6a4822b..904159b 100644 --- a/tools/Makefile.am +++ b/tools/Makefile.am @@ -3,7 +3,7 @@ AM_CPPFLAGS = -I$(top_srcdir)/include AM_CFLAGS = $(GLOBAL_CFLAGS) $(libgnutls_CFLAGS) $(libtasn1_CFLAGS) $(openssl_CFLAGS) $(libplist_CFLAGS) $(LFS_CFLAGS) AM_LDFLAGS = $(libgnutls_LIBS) $(libtasn1_LIBS) $(openssl_LIBS) $(libplist_LIBS) -bin_PROGRAMS = idevice_id ideviceinfo idevicepair idevicesyslog ideviceimagemounter idevicescreenshot ideviceenterrecovery idevicedate idevicebackup idevicebackup2 ideviceprovision +bin_PROGRAMS = idevice_id ideviceinfo idevicepair idevicesyslog ideviceimagemounter idevicescreenshot ideviceenterrecovery idevicedate idevicebackup idevicebackup2 ideviceprovision idevicedebugserverproxy ideviceinfo_SOURCES = ideviceinfo.c ideviceinfo_CFLAGS = $(AM_CFLAGS) @@ -59,3 +59,8 @@ ideviceprovision_SOURCES = ideviceprovision.c ideviceprovision_CFLAGS = $(AM_CFLAGS) ideviceprovision_LDFLAGS = $(AM_LDFLAGS) ideviceprovision_LDADD = ../src/libimobiledevice.la + +idevicedebugserverproxy_SOURCES = idevicedebugserverproxy.c socket.c thread.c +idevicedebugserverproxy_CFLAGS = $(AM_CFLAGS) +idevicedebugserverproxy_LDFLAGS = $(AM_LDFLAGS) +idevicedebugserverproxy_LDADD = ../src/libimobiledevice.la diff --git a/tools/idevicedebugserverproxy.c b/tools/idevicedebugserverproxy.c new file mode 100644 index 0000000..4487647 --- /dev/null +++ b/tools/idevicedebugserverproxy.c @@ -0,0 +1,374 @@ +/* + * idevicedebugserverproxy.c + * Proxy a debugserver connection from device for remote debugging + * + * Copyright (c) 2012 Martin Szulecki All Rights Reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include +#include +#include +#include +#include + +#include +#include + +#include "socket.h" +#include "thread.h" + +#define info(...) fprintf(stdout, __VA_ARGS__); fflush(stdout) +#define debug(...) if(debug_mode) fprintf(stdout, __VA_ARGS__) + +static int debug_mode = 0; +static int quit_flag = 0; + +typedef struct { + int server_fd; + int client_fd; + uint16_t local_port; + uint16_t remote_port; + idevice_connection_t device_connection; + volatile int stop_ctod; + volatile int stop_dtoc; +} socket_info_t; + +static void clean_exit(int sig) +{ + fprintf(stderr, "Exiting...\n"); + quit_flag++; +} + +static void print_usage(int argc, char **argv) +{ + char *name = NULL; + + name = strrchr(argv[0], '/'); + printf("Usage: %s [OPTIONS] \n", (name ? name + 1: argv[0])); + printf("Proxy debugserver connection from device to a local socket at PORT.\n\n"); + printf(" -d, --debug\t\tenable communication debugging\n"); + printf(" -u, --udid UDID\ttarget specific device by its 40-digit device UDID\n"); + printf(" -h, --help\t\tprints usage information\n"); + printf("\n"); +} + +static void *thread_device_to_client(void *data) +{ + socket_info_t* socket_info = (socket_info_t*)data; + idevice_error_t res = IDEVICE_E_UNKNOWN_ERROR; + + int recv_len; + int sent; + char buffer[131072]; + + debug("%s: started thread...\n", __func__); + + debug("%s: client_fd = %d\n", __func__, socket_info->client_fd); + debug("%s: server fd = %d\n", __func__, socket_info->server_fd); + + while (!quit_flag && !socket_info->stop_dtoc && socket_info->client_fd > 0 && socket_info->server_fd > 0) { + debug("%s: receiving data from device...\n", __func__); + + res = idevice_connection_receive_timeout(socket_info->device_connection, buffer, sizeof(buffer), (uint32_t*)&recv_len, 5000); + + if (recv_len <= 0) { + if (recv_len == 0 && res == IDEVICE_E_SUCCESS) { + // try again + continue; + } else { + fprintf(stderr, "recv failed: %s\n", strerror(errno)); + break; + } + } else { + /* send to device */ + debug("%s: sending data to device...\n", __func__); + sent = socket_send(socket_info->client_fd, buffer, recv_len); + if (sent < recv_len) { + if (sent <= 0) { + fprintf(stderr, "send failed: %s\n", strerror(errno)); + break; + } else { + fprintf(stderr, "only sent %d from %d bytes\n", sent, recv_len); + } + } else { + // sending succeeded, receive from device + debug("%s: pushed %d bytes to client\n", __func__, sent); + } + } + } + + debug("%s: shutting down...\n", __func__); + + socket_shutdown(socket_info->client_fd, SHUT_RDWR); + socket_close(socket_info->client_fd); + + socket_info->client_fd = -1; + socket_info->stop_ctod = 1; + + return NULL; +} + +static void *thread_client_to_device(void *data) +{ + socket_info_t* socket_info = (socket_info_t*)data; + idevice_error_t res = IDEVICE_E_UNKNOWN_ERROR; + + int recv_len; + int sent; + char buffer[131072]; + thread_t dtoc; + + debug("%s: started thread...\n", __func__); + + debug("%s: client_fd = %d\n", __func__, socket_info->client_fd); + debug("%s: server_fd = %d\n", __func__, socket_info->server_fd); + + /* spawn server to client thread */ + socket_info->stop_dtoc = 0; + if (thread_create(&dtoc, thread_device_to_client, data) != 0) { + fprintf(stderr, "Failed to start device to client thread...\n"); + } + + while (!quit_flag && !socket_info->stop_ctod && socket_info->client_fd > 0 && socket_info->server_fd > 0) { + debug("%s: receiving data from client...\n", __func__); + + /* attempt to read incoming data from client */ + recv_len = socket_receive_timeout(socket_info->client_fd, buffer, sizeof(buffer), 0, 5000); + + /* any data received? */ + if (recv_len <= 0) { + if (recv_len == 0) { + /* try again */ + continue; + } else { + fprintf(stderr, "Receive failed: %s\n", strerror(errno)); + break; + } + } else { + /* forward data to device */ + debug("%s: sending data to device...\n", __func__); + res = idevice_connection_send(socket_info->device_connection, buffer, recv_len, (uint32_t*)&sent); + + if (sent < recv_len || res != IDEVICE_E_SUCCESS) { + if (sent <= 0) { + fprintf(stderr, "send failed: %s\n", strerror(errno)); + break; + } else { + fprintf(stderr, "only sent %d from %d bytes\n", sent, recv_len); + } + } else { + // sending succeeded, receive from device + debug("%s: sent %d bytes to device\n", __func__, sent); + } + } + } + + debug("%s: shutting down...\n", __func__); + + socket_shutdown(socket_info->client_fd, SHUT_RDWR); + socket_close(socket_info->client_fd); + + socket_info->client_fd = -1; + socket_info->stop_dtoc = 1; + + /* join other thread to allow it to stop */ + thread_join(dtoc); + + return NULL; +} + +static void* connection_handler(void* data) +{ + socket_info_t* socket_info = (socket_info_t*)data; + thread_t ctod; + + debug("%s: client_fd = %d\n", __func__, socket_info->client_fd); + + /* spawn client to device thread */ + socket_info->stop_ctod = 0; + if (thread_create(&ctod, thread_client_to_device, data) != 0) { + fprintf(stderr, "Failed to start client to device thread...\n"); + } + + /* join the fun */ + thread_join(ctod); + + /* shutdown client socket */ + socket_shutdown(socket_info->client_fd, SHUT_RDWR); + socket_close(socket_info->client_fd); + + /* shutdown server socket if we have to terminate to unblock the server loop */ + if (quit_flag) { + socket_shutdown(socket_info->server_fd, SHUT_RDWR); + socket_close(socket_info->server_fd); + } + + return NULL; +} + +int main(int argc, char *argv[]) +{ + lockdownd_client_t lockdown = NULL; + idevice_t device = NULL; + idevice_connection_t connection = NULL; + idevice_error_t ret = IDEVICE_E_UNKNOWN_ERROR; + thread_t th; + char udid[41]; + udid[0] = 0; + uint16_t port = 0; + uint16_t local_port = 0; + int result = EXIT_SUCCESS; + int i; + + /* bind signals */ + signal(SIGINT, clean_exit); + signal(SIGTERM, clean_exit); +#ifndef WIN32 + signal(SIGQUIT, clean_exit); + signal(SIGPIPE, SIG_IGN); +#endif + + /* parse cmdline arguments */ + for (i = 1; i < argc; i++) { + if (!strcmp(argv[i], "-d") || !strcmp(argv[i], "--debug")) { + debug_mode = 1; + idevice_set_debug_level(1); + socket_set_verbose(3); + continue; + } + else if (!strcmp(argv[i], "-u") || !strcmp(argv[i], "--udid")) { + i++; + if (!argv[i] || (strlen(argv[i]) != 40)) { + print_usage(argc, argv); + return 0; + } + strcpy(udid, argv[i]); + continue; + } + else if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) { + print_usage(argc, argv); + return EXIT_SUCCESS; + } + else if (atoi(argv[i]) > 0) { + local_port = atoi(argv[i]); + continue; + } + else { + print_usage(argc, argv); + return EXIT_SUCCESS; + } + } + + /* a PORT is mandatory */ + if (!local_port) { + fprintf(stderr, "Please specify a PORT.\n"); + print_usage(argc, argv); + goto leave_cleanup; + } + + /* start services and connect to device */ + if (udid[0] != 0) { + ret = idevice_new(&device, udid); + if (ret != IDEVICE_E_SUCCESS) { + fprintf(stderr, "No device found with udid %s, is it plugged in?\n", udid); + result = EXIT_FAILURE; + goto leave_cleanup; + } + } + else + { + ret = idevice_new(&device, NULL); + if (ret != IDEVICE_E_SUCCESS) { + fprintf(stderr, "No device found, is it plugged in?\n"); + result = EXIT_FAILURE; + goto leave_cleanup; + } + } + + if (LOCKDOWN_E_SUCCESS != lockdownd_client_new_with_handshake(device, &lockdown, "idevicedebugserverproxy")) { + fprintf(stderr, "Could not connect to lockdownd. Exiting.\n"); + result = EXIT_FAILURE; + goto leave_cleanup; + } + + if ((lockdownd_start_service(lockdown, "com.apple.debugserver", &port) != LOCKDOWN_E_SUCCESS) || !port) { + fprintf(stderr, "Could not start com.apple.debugserver!\nPlease make sure to mount the developer disk image first.\n"); + result = EXIT_FAILURE; + goto leave_cleanup; + } + + if (idevice_connect(device, port, &connection) != IDEVICE_E_SUCCESS) { + fprintf(stderr, "Connection to debugserver port %d failed!\n", (int)&port); + result = EXIT_FAILURE; + goto leave_cleanup; + } + + /* free lockdown connection if running as it is not needed anymore */ + if (lockdown) { + lockdownd_client_free(lockdown); + lockdown = NULL; + } + + /* setup and create socket endpoint */ + socket_info_t socket_info; + + socket_info.device_connection = connection; + socket_info.local_port = local_port; + socket_info.remote_port = port; + + /* create local socket */ + socket_info.server_fd = socket_create(socket_info.local_port); + if (socket_info.server_fd < 0) { + fprintf(stderr, "Could not create socket\n"); + result = EXIT_FAILURE; + goto leave_cleanup; + } + + while (!quit_flag) { + debug("%s: Waiting for connection on local port %d\n", __func__, socket_info.local_port); + + /* wait for client */ + socket_info.client_fd = socket_accept(socket_info.server_fd, socket_info.local_port); + if (socket_info.client_fd < 0) { + debug("%s: Continuing...\n", __func__); + continue; + } + + debug("%s: Handling new client connection...\n", __func__); + + if (thread_create(&th, connection_handler, (void*)&socket_info) != 0) { + fprintf(stderr, "Could not start connection handler.\n"); + socket_shutdown(socket_info.server_fd, SHUT_RDWR); + socket_close(socket_info.server_fd); + } + } + + debug("%s: Shutting down debugserver proxy...\n", __func__); + +leave_cleanup: + if (connection) { + idevice_disconnect(connection); + } + if (lockdown) { + lockdownd_client_free(lockdown); + } + if (device) { + idevice_free(device); + } + + return result; +} diff --git a/tools/socket.c b/tools/socket.c new file mode 100644 index 0000000..c35de33 --- /dev/null +++ b/tools/socket.c @@ -0,0 +1,392 @@ +/* + * socket.c + * + * Copyright (c) 2012 Martin Szulecki All Rights Reserved. + * Copyright (c) 2012 Nikias Bassen All Rights Reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#ifdef WIN32 +#include +#include +static int wsa_init = 0; +#else +#include +#include +#include +#include +#include +#endif +#include "socket.h" + +#define RECV_TIMEOUT 20000 + +static int verbose = 0; + +void socket_set_verbose(int level) +{ + verbose = level; +} + +#ifndef WIN32 +int socket_create_unix(const char *filename) +{ + struct sockaddr_un name; + int sock; + size_t size; + + // remove if still present + unlink(filename); + + /* Create the socket. */ + sock = socket(PF_LOCAL, SOCK_STREAM, 0); + if (sock < 0) { + perror("socket"); + return -1; + } + + /* Bind a name to the socket. */ + name.sun_family = AF_LOCAL; + strncpy(name.sun_path, filename, sizeof(name.sun_path)); + name.sun_path[sizeof(name.sun_path) - 1] = '\0'; + + /* The size of the address is + the offset of the start of the filename, + plus its length, + plus one for the terminating null byte. + Alternatively you can just do: + size = SUN_LEN (&name); + */ + size = (offsetof(struct sockaddr_un, sun_path) + + strlen(name.sun_path) + 1); + + if (bind(sock, (struct sockaddr *) &name, size) < 0) { + perror("bind"); + socket_close(sock); + return -1; + } + + if (listen(sock, 10) < 0) { + perror("listen"); + socket_close(sock); + return -1; + } + + return sock; +} + +int socket_connect_unix(const char *filename) +{ + struct sockaddr_un name; + int sfd = -1; + size_t size; + struct stat fst; + + // check if socket file exists... + if (stat(filename, &fst) != 0) { + if (verbose >= 2) + fprintf(stderr, "%s: stat '%s': %s\n", __func__, filename, + strerror(errno)); + return -1; + } + // ... and if it is a unix domain socket + if (!S_ISSOCK(fst.st_mode)) { + if (verbose >= 2) + fprintf(stderr, "%s: File '%s' is not a socket!\n", __func__, + filename); + return -1; + } + // make a new socket + if ((sfd = socket(PF_LOCAL, SOCK_STREAM, 0)) < 0) { + if (verbose >= 2) + fprintf(stderr, "%s: socket: %s\n", __func__, strerror(errno)); + return -1; + } + // and connect to 'filename' + name.sun_family = AF_LOCAL; + strncpy(name.sun_path, filename, sizeof(name.sun_path)); + name.sun_path[sizeof(name.sun_path) - 1] = 0; + + size = (offsetof(struct sockaddr_un, sun_path) + + strlen(name.sun_path) + 1); + + if (connect(sfd, (struct sockaddr *) &name, size) < 0) { + socket_close(sfd); + if (verbose >= 2) + fprintf(stderr, "%s: connect: %s\n", __func__, + strerror(errno)); + return -1; + } + + return sfd; +} +#endif + +int socket_create(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))) { + perror("socket()"); + return -1; + } + + if (setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, (void*)&yes, sizeof(int)) == -1) { + perror("setsockopt()"); + socket_close(sfd); + return -1; + } + + memset((void *) &saddr, 0, sizeof(saddr)); + saddr.sin_family = AF_INET; + saddr.sin_addr.s_addr = htonl(INADDR_ANY); + saddr.sin_port = htons(port); + + if (0 > bind(sfd, (struct sockaddr *) &saddr, sizeof(saddr))) { + perror("bind()"); + socket_close(sfd); + return -1; + } + + if (listen(sfd, 1) == -1) { + perror("listen()"); + socket_close(sfd); + return -1; + } + + return sfd; +} + +int socket_connect(const char *addr, uint16_t port) +{ + int sfd = -1; + 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; + return -1; + } + + if ((hp = gethostbyname(addr)) == NULL) { + if (verbose >= 2) + fprintf(stderr, "%s: unknown host '%s'\n", __func__, addr); + return -1; + } + + if (!hp->h_addr) { + if (verbose >= 2) + fprintf(stderr, "%s: gethostbyname returned NULL address!\n", + __func__); + return -1; + } + + if (0 > (sfd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP))) { + perror("socket()"); + return -1; + } + + if (setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, (void*)&yes, sizeof(int)) == -1) { + perror("setsockopt()"); + socket_close(sfd); + return -1; + } + + memset((void *) &saddr, 0, sizeof(saddr)); + saddr.sin_family = AF_INET; + saddr.sin_addr.s_addr = *(uint32_t *) hp->h_addr; + saddr.sin_port = htons(port); + + if (connect(sfd, (struct sockaddr *) &saddr, sizeof(saddr)) < 0) { + perror("connect"); + socket_close(sfd); + return -2; + } + + return sfd; +} + +int socket_check_fd(int fd, fd_mode fdm, unsigned int timeout) +{ + fd_set fds; + int sret; + int eagain; + struct timeval to; + struct timeval *pto; + + if (fd <= 0) { + if (verbose >= 2) + fprintf(stderr, "ERROR: invalid fd in check_fd %d\n", fd); + return -1; + } + + FD_ZERO(&fds); + FD_SET(fd, &fds); + + if (timeout > 0) { + to.tv_sec = (time_t) (timeout / 1000); + to.tv_usec = (time_t) ((timeout - (to.tv_sec * 1000)) * 1000); + pto = &to; + } else { + pto = NULL; + } + + sret = -1; + + do { + eagain = 0; + switch (fdm) { + case FDM_READ: + sret = select(fd + 1, &fds, NULL, NULL, pto); + break; + case FDM_WRITE: + sret = select(fd + 1, NULL, &fds, NULL, pto); + break; + case FDM_EXCEPT: + sret = select(fd + 1, NULL, NULL, &fds, pto); + break; + default: + return -1; + } + + if (sret < 0) { + switch (errno) { + case EINTR: + // interrupt signal in select + if (verbose >= 2) + fprintf(stderr, "%s: EINTR\n", __func__); + eagain = 1; + break; + case EAGAIN: + if (verbose >= 2) + fprintf(stderr, "%s: EAGAIN\n", __func__); + break; + default: + if (verbose >= 2) + fprintf(stderr, "%s: select failed: %s\n", __func__, + strerror(errno)); + return -1; + } + } + } while (eagain); + + return sret; +} + +int socket_accept(int fd, uint16_t port) +{ +#ifdef WIN32 + int addr_len; +#else + socklen_t addr_len; +#endif + int result; + struct sockaddr_in addr; + + memset(&addr, 0, sizeof(addr)); + addr.sin_family = AF_INET; + addr.sin_addr.s_addr = htonl(INADDR_ANY); + addr.sin_port = htons(port); + + addr_len = sizeof(addr); + result = accept(fd, (struct sockaddr*)&addr, &addr_len); + + return result; +} + +int socket_shutdown(int fd, int how) +{ + return shutdown(fd, how); +} + +int socket_close(int fd) { +#ifdef WIN32 + return closesocket(fd); +#else + return close(fd); +#endif +} + +int socket_receive(int fd, void *data, size_t length) +{ + return socket_receive_timeout(fd, data, length, 0, RECV_TIMEOUT); +} + +int socket_peek(int fd, void *data, size_t length) +{ + return socket_receive_timeout(fd, data, length, MSG_PEEK, RECV_TIMEOUT); +} + +int socket_receive_timeout(int fd, void *data, size_t length, int flags, + unsigned int timeout) +{ + int res; + int result; + + // check if data is available + res = socket_check_fd(fd, FDM_READ, timeout); + if (res <= 0) { + return res; + } + // if we get here, there _is_ data available + result = recv(fd, data, length, flags); + if (res > 0 && result == 0) { + // but this is an error condition + if (verbose >= 3) + fprintf(stderr, "%s: fd=%d recv returned 0\n", __func__, fd); + return -EAGAIN; + } + if (result < 0) { + return -errno; + } + return result; +} + +int socket_send(int fd, void *data, size_t length) +{ + return send(fd, data, length, 0); +} diff --git a/tools/socket.h b/tools/socket.h new file mode 100644 index 0000000..c2b2599 --- /dev/null +++ b/tools/socket.h @@ -0,0 +1,65 @@ +/* + * socket.h + * + * Copyright (c) 2012 Martin Szulecki All Rights Reserved. + * Copyright (c) 2012 Nikias Bassen All Rights Reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef __SOCKET_SOCKET_H +#define __SOCKET_SOCKET_H + +#include +#include + +enum fd_mode { + FDM_READ, + FDM_WRITE, + FDM_EXCEPT +}; +typedef enum fd_mode fd_mode; + +#ifdef WIN32 +#include +#define SHUT_RD SD_READ +#define SHUT_WR SD_WRITE +#define SHUT_RDWR SD_BOTH +#else +#include +#endif + +#ifndef WIN32 +int socket_create_unix(const char *filename); +int socket_connect_unix(const char *filename); +#endif +int socket_create(uint16_t port); +int socket_connect(const char *addr, uint16_t port); +int socket_check_fd(int fd, fd_mode fdm, unsigned int timeout); +int socket_accept(int fd, uint16_t port); + +int socket_shutdown(int fd, int how); +int socket_close(int fd); + +int socket_receive(int fd, void *data, size_t size); +int socket_peek(int fd, void *data, size_t size); +int socket_receive_timeout(int fd, void *data, size_t size, int flags, + unsigned int timeout); + +int socket_send(int fd, void *data, size_t size); + +void socket_set_verbose(int level); + +#endif /* __SOCKET_SOCKET_H */ diff --git a/tools/thread.c b/tools/thread.c new file mode 100644 index 0000000..f077243 --- /dev/null +++ b/tools/thread.c @@ -0,0 +1,47 @@ +/* + * thread.c + * + * Copyright (c) 2012 Martin Szulecki All Rights Reserved. + * Copyright (c) 2012 Nikias Bassen All Rights Reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "thread.h" + +int thread_create(thread_t *thread, thread_func_t thread_func, void* data) +{ +#ifdef WIN32 + HANDLE th = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)thread_func, data, 0, NULL); + if (th == NULL) { + return -1; + } + *thread = th; + return 0; +#else + int res = pthread_create(thread, NULL, thread_func, data); + return res; +#endif +} + +void thread_join(thread_t thread) +{ + /* wait for thread to complete */ +#ifdef WIN32 + WaitForSingleObject(thread, INFINITE); +#else + pthread_join(thread, NULL); +#endif +} diff --git a/tools/thread.h b/tools/thread.h new file mode 100644 index 0000000..983eeb5 --- /dev/null +++ b/tools/thread.h @@ -0,0 +1,38 @@ +/* + * thread.h + * + * Copyright (c) 2012 Martin Szulecki All Rights Reserved. + * Copyright (c) 2012 Nikias Bassen All Rights Reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef __THREAD_H +#define __THREAD_H + +#ifdef WIN32 +#include +typedef HANDLE thread_t; +#else +#include +typedef pthread_t thread_t; +#endif + +typedef void* (*thread_func_t)(void* data); + +int thread_create(thread_t* thread, thread_func_t thread_func, void* data); +void thread_join(thread_t thread); + +#endif -- cgit v1.1-32-gdbae