From d982007a7350df35c5aeba820a520779694514a7 Mon Sep 17 00:00:00 2001 From: Hector Martin Date: Mon, 27 Apr 2009 01:37:05 +0200 Subject: general framework, unix socket listen, utils --- CMakeLists.txt | 2 +- log.c | 50 +++++++++++++++++++++++++++++++ log.h | 38 ++++++++++++++++++++++++ main.c | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ mux.c | 27 +++++++++++++++++ mux.h | 24 +++++++++++++++ usb-linux.c | 4 +++ usb.h | 29 ++++++++++++++++++ usbmuxd.c | 33 --------------------- utils.c | 57 ++++++++++++++++++++++++++++++++++++ utils.h | 47 ++++++++++++++++++++++++++++++ 11 files changed, 369 insertions(+), 34 deletions(-) create mode 100644 log.c create mode 100644 log.h create mode 100644 main.c create mode 100644 mux.c create mode 100644 mux.h create mode 100644 usb.h delete mode 100644 usbmuxd.c create mode 100644 utils.c create mode 100644 utils.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 9e4345f..81a6965 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,6 +11,6 @@ set(LIBS ${LIBS} ${USB_LIBRARIES}) #set(CMAKE_VERBOSE_MAKEFILE ON) add_definitions(-Wall -O2) -add_executable(usbmuxd usbmuxd.c usb-linux.c) +add_executable(usbmuxd main.c usb-linux.c log.c utils.c) target_link_libraries(usbmuxd ${LIBS}) diff --git a/log.c b/log.c new file mode 100644 index 0000000..d7f57df --- /dev/null +++ b/log.c @@ -0,0 +1,50 @@ +/* + usbmuxd - iPhone/iPod Touch USB multiplex server daemon + +Copyright (C) 2009 Hector Martin "marcan" + +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 +the Free Software Foundation, either version 2 or version 3. + +This program 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 General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +*/ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include +#include +#include +#include + +#include "log.h" + +int log_level = LOG_SPEW; + +void usbmuxd_log(enum loglevel level, const char *fmt, ...) +{ + va_list ap; + char *fs; + + if(level < log_level) + return; + + fs = malloc(10 + strlen(fmt)); + sprintf(fs, "[%d] %s\n", level, fmt); + + va_start(ap, fmt); + vfprintf(stderr, fs, ap); + va_end(ap); + + free(fs); +} \ No newline at end of file diff --git a/log.h b/log.h new file mode 100644 index 0000000..fda2e1a --- /dev/null +++ b/log.h @@ -0,0 +1,38 @@ +/* + usbmuxd - iPhone/iPod Touch USB multiplex server daemon + +Copyright (C) 2009 Hector Martin "marcan" + +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 +the Free Software Foundation, either version 2 or version 3. + +This program 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 General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +*/ + +#ifndef __LOG_H__ +#define __LOG_H__ + +enum loglevel { + LOG_SPEW = 0, + LOG_DEBUG, + LOG_INFO, + LOG_NOTICE, + LOG_WARNING, + LOG_ERROR, + LOG_FATAL, +}; + +extern int log_level; + +void usbmuxd_log(enum loglevel level, const char *fmt, ...); + +#endif diff --git a/main.c b/main.c new file mode 100644 index 0000000..c2467a2 --- /dev/null +++ b/main.c @@ -0,0 +1,92 @@ +/* + usbmuxd - iPhone/iPod Touch USB multiplex server daemon + +Copyright (C) 2009 Hector Martin "marcan" + +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 +the Free Software Foundation, either version 2 or version 3. + +This program 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 General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +*/ + +#define _BSD_SOURCE + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include +#include +#include +#include +#include +#include +#include + +#include "log.h" +#include "usb.h" + +const char *socket_path = "/tmp/usbmuxd"; //TODO: CHANGEME + +int create_socket(void) { + struct sockaddr_un bind_addr; + int listenfd; + + if(unlink(socket_path) == -1 && errno != ENOENT) { + usbmuxd_log(LOG_FATAL, "unlink(%s) failed: %s", socket_path, strerror(errno)); + return -1; + } + + listenfd = socket(AF_UNIX, SOCK_STREAM, 0); + if (listenfd == -1) { + usbmuxd_log(LOG_FATAL, "socket() failed: %s", strerror(errno)); + return -1; + } + + bzero(&bind_addr, sizeof(bind_addr)); + bind_addr.sun_family = AF_UNIX; + strcpy(bind_addr.sun_path, socket_path); + if (bind(listenfd, (struct sockaddr*)&bind_addr, sizeof(bind_addr)) != 0) { + usbmuxd_log(LOG_FATAL, "bind() failed: %s", strerror(errno)); + return -1; + } + + // Start listening + if (listen(listenfd, 5) != 0) { + usbmuxd_log(LOG_FATAL, "listen() failed: %s", strerror(errno)); + return -1; + } + + return listenfd; +} + +int main(int argc, char *argv[]) +{ + int listenfd; + int res; + + usbmuxd_log(LOG_NOTICE, "usbmux v0.1 starting up"); + + usbmuxd_log(LOG_INFO, "Creating socket"); + listenfd = create_socket(); + if(listenfd < 0) + return 1; + + usbmuxd_log(LOG_INFO, "Initializing USB"); + if((res = usb_init()) < 0) + return 2; + usbmuxd_log(LOG_INFO, "%d device%s detected", res, (res==1)?"":"s"); + + usbmuxd_log(LOG_NOTICE, "initialization complete"); + + return 0; +} diff --git a/mux.c b/mux.c new file mode 100644 index 0000000..c56f8f4 --- /dev/null +++ b/mux.c @@ -0,0 +1,27 @@ +/* + usbmuxd - iPhone/iPod Touch USB multiplex server daemon + +Copyright (C) 2009 Hector Martin "marcan" + +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 +the Free Software Foundation, either version 2 or version 3. + +This program 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 General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +*/ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include "log.h" +#include "usb.h" + diff --git a/mux.h b/mux.h new file mode 100644 index 0000000..d6f61af --- /dev/null +++ b/mux.h @@ -0,0 +1,24 @@ +/* + usbmuxd - iPhone/iPod Touch USB multiplex server daemon + +Copyright (C) 2009 Hector Martin "marcan" + +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 +the Free Software Foundation, either version 2 or version 3. + +This program 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 General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +*/ + +#ifndef __MUX_H__ +#define __MUX_H__ + +#endif diff --git a/usb-linux.c b/usb-linux.c index daca994..221ce33 100644 --- a/usb-linux.c +++ b/usb-linux.c @@ -28,3 +28,7 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA #include +int usb_init(void) +{ + return 0; +} diff --git a/usb.h b/usb.h new file mode 100644 index 0000000..cf25c3a --- /dev/null +++ b/usb.h @@ -0,0 +1,29 @@ +/* + usbmuxd - iPhone/iPod Touch USB multiplex server daemon + +Copyright (C) 2009 Hector Martin "marcan" + +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 +the Free Software Foundation, either version 2 or version 3. + +This program 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 General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +*/ + +#ifndef __USB_H__ +#define __USB_H__ + +#define BULK_IN 0x85 +#define BULK_OUT 0x04 + +int usb_init(void); + +#endif diff --git a/usbmuxd.c b/usbmuxd.c deleted file mode 100644 index c52dcf9..0000000 --- a/usbmuxd.c +++ /dev/null @@ -1,33 +0,0 @@ -/* - usbmuxd - iPhone/iPod Touch USB multiplex server daemon - -Copyright (C) 2009 Hector Martin "marcan" - -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 -the Free Software Foundation, either version 2 or version 3. - -This program 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 General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program; if not, write to the Free Software -Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - -*/ - -#ifdef HAVE_CONFIG_H -#include -#endif - -#include -#include - -int main(int argc, char *argv[]) -{ - printf("Hello, world!\n"); - - return 0; -} diff --git a/utils.c b/utils.c new file mode 100644 index 0000000..c3d5c50 --- /dev/null +++ b/utils.c @@ -0,0 +1,57 @@ +/* + usbmuxd - iPhone/iPod Touch USB multiplex server daemon + +Copyright (C) 2009 Hector Martin "marcan" + +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 +the Free Software Foundation, either version 2 or version 3. + +This program 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 General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +*/ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include +#include "utils.h" + +void fdlist_create(fdlist *list) +{ + list->count = 0; + list->capacity = 4; + list->owners = malloc(sizeof(*list->owners) * list->capacity); + list->fds = malloc(sizeof(*list->fds) * list->capacity); +} +void fdlist_add(fdlist *list, enum fdowner owner, int fd, short events) +{ + if(list->count == list->capacity) { + list->capacity *= 2; + list->owners = realloc(list->owners, sizeof(*list->owners) * list->capacity); + list->fds = realloc(list->fds, sizeof(*list->fds) * list->capacity); + } + list->owners[list->count] = owner; + list->fds[list->count].fd = fd; + list->fds[list->count].events = events; + list->fds[list->count].revents = 0; + list->count++; +} + +void fdlist_free(fdlist *list) +{ + list->count = 0; + list->capacity = 0; + free(list->owners); + list->owners = NULL; + free(list->fds); + list->fds = NULL; +} diff --git a/utils.h b/utils.h new file mode 100644 index 0000000..465487d --- /dev/null +++ b/utils.h @@ -0,0 +1,47 @@ +/* + usbmuxd - iPhone/iPod Touch USB multiplex server daemon + +Copyright (C) 2009 Hector Martin "marcan" + +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 +the Free Software Foundation, either version 2 or version 3. + +This program 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 General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +*/ + +#ifndef __LOG_H__ +#define __LOG_H__ + +#include + +enum fdowner { + FD_LISTEN, + FD_CLIENT, + FD_USB +}; + +typedef struct { + int count; + int capacity; + enum fdowner *owners; + struct pollfd *fds; +} fdlist; + +void fdlist_create(fdlist *list); +void fdlist_add(fdlist *list, enum fdowner owner, int fd, short events); +void fdlist_free(fdlist *list); + +#define MIN(X, Y) ((X) < (Y) ? (X) : (Y)) + + + +#endif -- cgit v1.1-32-gdbae