diff options
| author | 2009-04-27 01:37:05 +0200 | |
|---|---|---|
| committer | 2009-04-27 01:37:05 +0200 | |
| commit | d982007a7350df35c5aeba820a520779694514a7 (patch) | |
| tree | 62d16ab56c7f92b12999bbcdbe7ce0f9293fe069 | |
| parent | 677fa91e376b98d871bb238ecb84685dd4f33ebd (diff) | |
| download | usbmuxd-d982007a7350df35c5aeba820a520779694514a7.tar.gz usbmuxd-d982007a7350df35c5aeba820a520779694514a7.tar.bz2 | |
general framework, unix socket listen, utils
| -rw-r--r-- | CMakeLists.txt | 2 | ||||
| -rw-r--r-- | log.c | 50 | ||||
| -rw-r--r-- | log.h | 38 | ||||
| -rw-r--r-- | main.c | 92 | ||||
| -rw-r--r-- | mux.c (renamed from usbmuxd.c) | 10 | ||||
| -rw-r--r-- | mux.h | 24 | ||||
| -rw-r--r-- | usb-linux.c | 4 | ||||
| -rw-r--r-- | usb.h | 29 | ||||
| -rw-r--r-- | utils.c | 57 | ||||
| -rw-r--r-- | utils.h | 47 |
10 files changed, 344 insertions, 9 deletions
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}) | |||
| 11 | #set(CMAKE_VERBOSE_MAKEFILE ON) | 11 | #set(CMAKE_VERBOSE_MAKEFILE ON) |
| 12 | 12 | ||
| 13 | add_definitions(-Wall -O2) | 13 | add_definitions(-Wall -O2) |
| 14 | add_executable(usbmuxd usbmuxd.c usb-linux.c) | 14 | add_executable(usbmuxd main.c usb-linux.c log.c utils.c) |
| 15 | target_link_libraries(usbmuxd ${LIBS}) | 15 | target_link_libraries(usbmuxd ${LIBS}) |
| 16 | 16 | ||
| @@ -0,0 +1,50 @@ | |||
| 1 | /* | ||
| 2 | usbmuxd - iPhone/iPod Touch USB multiplex server daemon | ||
| 3 | |||
| 4 | Copyright (C) 2009 Hector Martin "marcan" <hector@marcansoft.com> | ||
| 5 | |||
| 6 | This program is free software; you can redistribute it and/or modify | ||
| 7 | it under the terms of the GNU General Public License as published by | ||
| 8 | the Free Software Foundation, either version 2 or version 3. | ||
| 9 | |||
| 10 | This program is distributed in the hope that it will be useful, | ||
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 13 | GNU General Public License for more details. | ||
| 14 | |||
| 15 | You should have received a copy of the GNU General Public License | ||
| 16 | along with this program; if not, write to the Free Software | ||
| 17 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
| 18 | |||
| 19 | */ | ||
| 20 | |||
| 21 | #ifdef HAVE_CONFIG_H | ||
| 22 | #include <config.h> | ||
| 23 | #endif | ||
| 24 | |||
| 25 | #include <stdio.h> | ||
| 26 | #include <stdlib.h> | ||
| 27 | #include <string.h> | ||
| 28 | #include <stdarg.h> | ||
| 29 | |||
| 30 | #include "log.h" | ||
| 31 | |||
| 32 | int log_level = LOG_SPEW; | ||
| 33 | |||
| 34 | void usbmuxd_log(enum loglevel level, const char *fmt, ...) | ||
| 35 | { | ||
| 36 | va_list ap; | ||
| 37 | char *fs; | ||
| 38 | |||
| 39 | if(level < log_level) | ||
| 40 | return; | ||
| 41 | |||
| 42 | fs = malloc(10 + strlen(fmt)); | ||
| 43 | sprintf(fs, "[%d] %s\n", level, fmt); | ||
| 44 | |||
| 45 | va_start(ap, fmt); | ||
| 46 | vfprintf(stderr, fs, ap); | ||
| 47 | va_end(ap); | ||
| 48 | |||
| 49 | free(fs); | ||
| 50 | } \ No newline at end of file | ||
| @@ -0,0 +1,38 @@ | |||
| 1 | /* | ||
| 2 | usbmuxd - iPhone/iPod Touch USB multiplex server daemon | ||
| 3 | |||
| 4 | Copyright (C) 2009 Hector Martin "marcan" <hector@marcansoft.com> | ||
| 5 | |||
| 6 | This program is free software; you can redistribute it and/or modify | ||
| 7 | it under the terms of the GNU General Public License as published by | ||
| 8 | the Free Software Foundation, either version 2 or version 3. | ||
| 9 | |||
| 10 | This program is distributed in the hope that it will be useful, | ||
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 13 | GNU General Public License for more details. | ||
| 14 | |||
| 15 | You should have received a copy of the GNU General Public License | ||
| 16 | along with this program; if not, write to the Free Software | ||
| 17 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
| 18 | |||
| 19 | */ | ||
| 20 | |||
| 21 | #ifndef __LOG_H__ | ||
| 22 | #define __LOG_H__ | ||
| 23 | |||
| 24 | enum loglevel { | ||
| 25 | LOG_SPEW = 0, | ||
| 26 | LOG_DEBUG, | ||
| 27 | LOG_INFO, | ||
| 28 | LOG_NOTICE, | ||
| 29 | LOG_WARNING, | ||
| 30 | LOG_ERROR, | ||
| 31 | LOG_FATAL, | ||
| 32 | }; | ||
| 33 | |||
| 34 | extern int log_level; | ||
| 35 | |||
| 36 | void usbmuxd_log(enum loglevel level, const char *fmt, ...); | ||
| 37 | |||
| 38 | #endif | ||
| @@ -0,0 +1,92 @@ | |||
| 1 | /* | ||
| 2 | usbmuxd - iPhone/iPod Touch USB multiplex server daemon | ||
| 3 | |||
| 4 | Copyright (C) 2009 Hector Martin "marcan" <hector@marcansoft.com> | ||
| 5 | |||
| 6 | This program is free software; you can redistribute it and/or modify | ||
| 7 | it under the terms of the GNU General Public License as published by | ||
| 8 | the Free Software Foundation, either version 2 or version 3. | ||
| 9 | |||
| 10 | This program is distributed in the hope that it will be useful, | ||
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 13 | GNU General Public License for more details. | ||
| 14 | |||
| 15 | You should have received a copy of the GNU General Public License | ||
| 16 | along with this program; if not, write to the Free Software | ||
| 17 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
| 18 | |||
| 19 | */ | ||
| 20 | |||
| 21 | #define _BSD_SOURCE | ||
| 22 | |||
| 23 | #ifdef HAVE_CONFIG_H | ||
| 24 | #include <config.h> | ||
| 25 | #endif | ||
| 26 | |||
| 27 | #include <stdio.h> | ||
| 28 | #include <errno.h> | ||
| 29 | #include <string.h> | ||
| 30 | #include <stdlib.h> | ||
| 31 | #include <unistd.h> | ||
| 32 | #include <sys/socket.h> | ||
| 33 | #include <sys/un.h> | ||
| 34 | |||
| 35 | #include "log.h" | ||
| 36 | #include "usb.h" | ||
| 37 | |||
| 38 | const char *socket_path = "/tmp/usbmuxd"; //TODO: CHANGEME | ||
| 39 | |||
| 40 | int create_socket(void) { | ||
| 41 | struct sockaddr_un bind_addr; | ||
| 42 | int listenfd; | ||
| 43 | |||
| 44 | if(unlink(socket_path) == -1 && errno != ENOENT) { | ||
| 45 | usbmuxd_log(LOG_FATAL, "unlink(%s) failed: %s", socket_path, strerror(errno)); | ||
| 46 | return -1; | ||
| 47 | } | ||
| 48 | |||
| 49 | listenfd = socket(AF_UNIX, SOCK_STREAM, 0); | ||
| 50 | if (listenfd == -1) { | ||
| 51 | usbmuxd_log(LOG_FATAL, "socket() failed: %s", strerror(errno)); | ||
| 52 | return -1; | ||
| 53 | } | ||
| 54 | |||
| 55 | bzero(&bind_addr, sizeof(bind_addr)); | ||
| 56 | bind_addr.sun_family = AF_UNIX; | ||
| 57 | strcpy(bind_addr.sun_path, socket_path); | ||
| 58 | if (bind(listenfd, (struct sockaddr*)&bind_addr, sizeof(bind_addr)) != 0) { | ||
| 59 | usbmuxd_log(LOG_FATAL, "bind() failed: %s", strerror(errno)); | ||
| 60 | return -1; | ||
| 61 | } | ||
| 62 | |||
| 63 | // Start listening | ||
| 64 | if (listen(listenfd, 5) != 0) { | ||
| 65 | usbmuxd_log(LOG_FATAL, "listen() failed: %s", strerror(errno)); | ||
| 66 | return -1; | ||
| 67 | } | ||
| 68 | |||
| 69 | return listenfd; | ||
| 70 | } | ||
| 71 | |||
| 72 | int main(int argc, char *argv[]) | ||
| 73 | { | ||
| 74 | int listenfd; | ||
| 75 | int res; | ||
| 76 | |||
| 77 | usbmuxd_log(LOG_NOTICE, "usbmux v0.1 starting up"); | ||
| 78 | |||
| 79 | usbmuxd_log(LOG_INFO, "Creating socket"); | ||
| 80 | listenfd = create_socket(); | ||
| 81 | if(listenfd < 0) | ||
| 82 | return 1; | ||
| 83 | |||
| 84 | usbmuxd_log(LOG_INFO, "Initializing USB"); | ||
| 85 | if((res = usb_init()) < 0) | ||
| 86 | return 2; | ||
| 87 | usbmuxd_log(LOG_INFO, "%d device%s detected", res, (res==1)?"":"s"); | ||
| 88 | |||
| 89 | usbmuxd_log(LOG_NOTICE, "initialization complete"); | ||
| 90 | |||
| 91 | return 0; | ||
| 92 | } | ||
| @@ -22,12 +22,6 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |||
| 22 | #include <config.h> | 22 | #include <config.h> |
| 23 | #endif | 23 | #endif |
| 24 | 24 | ||
| 25 | #include <stdio.h> | 25 | #include "log.h" |
| 26 | #include <stdlib.h> | 26 | #include "usb.h" |
| 27 | 27 | ||
| 28 | int main(int argc, char *argv[]) | ||
| 29 | { | ||
| 30 | printf("Hello, world!\n"); | ||
| 31 | |||
| 32 | return 0; | ||
| 33 | } | ||
| @@ -0,0 +1,24 @@ | |||
| 1 | /* | ||
| 2 | usbmuxd - iPhone/iPod Touch USB multiplex server daemon | ||
| 3 | |||
| 4 | Copyright (C) 2009 Hector Martin "marcan" <hector@marcansoft.com> | ||
| 5 | |||
| 6 | This program is free software; you can redistribute it and/or modify | ||
| 7 | it under the terms of the GNU General Public License as published by | ||
| 8 | the Free Software Foundation, either version 2 or version 3. | ||
| 9 | |||
| 10 | This program is distributed in the hope that it will be useful, | ||
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 13 | GNU General Public License for more details. | ||
| 14 | |||
| 15 | You should have received a copy of the GNU General Public License | ||
| 16 | along with this program; if not, write to the Free Software | ||
| 17 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
| 18 | |||
| 19 | */ | ||
| 20 | |||
| 21 | #ifndef __MUX_H__ | ||
| 22 | #define __MUX_H__ | ||
| 23 | |||
| 24 | #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 | |||
| 28 | 28 | ||
| 29 | #include <libusb.h> | 29 | #include <libusb.h> |
| 30 | 30 | ||
| 31 | int usb_init(void) | ||
| 32 | { | ||
| 33 | return 0; | ||
| 34 | } | ||
| @@ -0,0 +1,29 @@ | |||
| 1 | /* | ||
| 2 | usbmuxd - iPhone/iPod Touch USB multiplex server daemon | ||
| 3 | |||
| 4 | Copyright (C) 2009 Hector Martin "marcan" <hector@marcansoft.com> | ||
| 5 | |||
| 6 | This program is free software; you can redistribute it and/or modify | ||
| 7 | it under the terms of the GNU General Public License as published by | ||
| 8 | the Free Software Foundation, either version 2 or version 3. | ||
| 9 | |||
| 10 | This program is distributed in the hope that it will be useful, | ||
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 13 | GNU General Public License for more details. | ||
| 14 | |||
| 15 | You should have received a copy of the GNU General Public License | ||
| 16 | along with this program; if not, write to the Free Software | ||
| 17 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
| 18 | |||
| 19 | */ | ||
| 20 | |||
| 21 | #ifndef __USB_H__ | ||
| 22 | #define __USB_H__ | ||
| 23 | |||
| 24 | #define BULK_IN 0x85 | ||
| 25 | #define BULK_OUT 0x04 | ||
| 26 | |||
| 27 | int usb_init(void); | ||
| 28 | |||
| 29 | #endif | ||
| @@ -0,0 +1,57 @@ | |||
| 1 | /* | ||
| 2 | usbmuxd - iPhone/iPod Touch USB multiplex server daemon | ||
| 3 | |||
| 4 | Copyright (C) 2009 Hector Martin "marcan" <hector@marcansoft.com> | ||
| 5 | |||
| 6 | This program is free software; you can redistribute it and/or modify | ||
| 7 | it under the terms of the GNU General Public License as published by | ||
| 8 | the Free Software Foundation, either version 2 or version 3. | ||
| 9 | |||
| 10 | This program is distributed in the hope that it will be useful, | ||
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 13 | GNU General Public License for more details. | ||
| 14 | |||
| 15 | You should have received a copy of the GNU General Public License | ||
| 16 | along with this program; if not, write to the Free Software | ||
| 17 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
| 18 | |||
| 19 | */ | ||
| 20 | |||
| 21 | #ifdef HAVE_CONFIG_H | ||
| 22 | #include <config.h> | ||
| 23 | #endif | ||
| 24 | |||
| 25 | #include <stdlib.h> | ||
| 26 | #include "utils.h" | ||
| 27 | |||
| 28 | void fdlist_create(fdlist *list) | ||
| 29 | { | ||
| 30 | list->count = 0; | ||
| 31 | list->capacity = 4; | ||
| 32 | list->owners = malloc(sizeof(*list->owners) * list->capacity); | ||
| 33 | list->fds = malloc(sizeof(*list->fds) * list->capacity); | ||
| 34 | } | ||
| 35 | void fdlist_add(fdlist *list, enum fdowner owner, int fd, short events) | ||
| 36 | { | ||
| 37 | if(list->count == list->capacity) { | ||
| 38 | list->capacity *= 2; | ||
| 39 | list->owners = realloc(list->owners, sizeof(*list->owners) * list->capacity); | ||
| 40 | list->fds = realloc(list->fds, sizeof(*list->fds) * list->capacity); | ||
| 41 | } | ||
| 42 | list->owners[list->count] = owner; | ||
| 43 | list->fds[list->count].fd = fd; | ||
| 44 | list->fds[list->count].events = events; | ||
| 45 | list->fds[list->count].revents = 0; | ||
| 46 | list->count++; | ||
| 47 | } | ||
| 48 | |||
| 49 | void fdlist_free(fdlist *list) | ||
| 50 | { | ||
| 51 | list->count = 0; | ||
| 52 | list->capacity = 0; | ||
| 53 | free(list->owners); | ||
| 54 | list->owners = NULL; | ||
| 55 | free(list->fds); | ||
| 56 | list->fds = NULL; | ||
| 57 | } | ||
| @@ -0,0 +1,47 @@ | |||
| 1 | /* | ||
| 2 | usbmuxd - iPhone/iPod Touch USB multiplex server daemon | ||
| 3 | |||
| 4 | Copyright (C) 2009 Hector Martin "marcan" <hector@marcansoft.com> | ||
| 5 | |||
| 6 | This program is free software; you can redistribute it and/or modify | ||
| 7 | it under the terms of the GNU General Public License as published by | ||
| 8 | the Free Software Foundation, either version 2 or version 3. | ||
| 9 | |||
| 10 | This program is distributed in the hope that it will be useful, | ||
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 13 | GNU General Public License for more details. | ||
| 14 | |||
| 15 | You should have received a copy of the GNU General Public License | ||
| 16 | along with this program; if not, write to the Free Software | ||
| 17 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
| 18 | |||
| 19 | */ | ||
| 20 | |||
| 21 | #ifndef __LOG_H__ | ||
| 22 | #define __LOG_H__ | ||
| 23 | |||
| 24 | #include <poll.h> | ||
| 25 | |||
| 26 | enum fdowner { | ||
| 27 | FD_LISTEN, | ||
| 28 | FD_CLIENT, | ||
| 29 | FD_USB | ||
| 30 | }; | ||
| 31 | |||
| 32 | typedef struct { | ||
| 33 | int count; | ||
| 34 | int capacity; | ||
| 35 | enum fdowner *owners; | ||
| 36 | struct pollfd *fds; | ||
| 37 | } fdlist; | ||
| 38 | |||
| 39 | void fdlist_create(fdlist *list); | ||
| 40 | void fdlist_add(fdlist *list, enum fdowner owner, int fd, short events); | ||
| 41 | void fdlist_free(fdlist *list); | ||
| 42 | |||
| 43 | #define MIN(X, Y) ((X) < (Y) ? (X) : (Y)) | ||
| 44 | |||
| 45 | |||
| 46 | |||
| 47 | #endif | ||
