diff options
| -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})  #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}) @@ -0,0 +1,50 @@ +/* +	usbmuxd - iPhone/iPod Touch USB multiplex server daemon + +Copyright (C) 2009	Hector Martin "marcan" <hector@marcansoft.com> + +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 <config.h> +#endif + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <stdarg.h> + +#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 @@ -0,0 +1,38 @@ +/* +	usbmuxd - iPhone/iPod Touch USB multiplex server daemon + +Copyright (C) 2009	Hector Martin "marcan" <hector@marcansoft.com> + +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 @@ -0,0 +1,92 @@ +/* +	usbmuxd - iPhone/iPod Touch USB multiplex server daemon + +Copyright (C) 2009	Hector Martin "marcan" <hector@marcansoft.com> + +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 <config.h> +#endif + +#include <stdio.h> +#include <errno.h> +#include <string.h> +#include <stdlib.h> +#include <unistd.h> +#include <sys/socket.h> +#include <sys/un.h> + +#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; +} @@ -22,12 +22,6 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA  #include <config.h>  #endif -#include <stdio.h> -#include <stdlib.h> +#include "log.h" +#include "usb.h" -int main(int argc, char *argv[]) -{ -  printf("Hello, world!\n"); - -  return 0; -} @@ -0,0 +1,24 @@ +/* +	usbmuxd - iPhone/iPod Touch USB multiplex server daemon + +Copyright (C) 2009	Hector Martin "marcan" <hector@marcansoft.com> + +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 <libusb.h> +int usb_init(void) +{ +	return 0; +} @@ -0,0 +1,29 @@ +/* +	usbmuxd - iPhone/iPod Touch USB multiplex server daemon + +Copyright (C) 2009	Hector Martin "marcan" <hector@marcansoft.com> + +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 @@ -0,0 +1,57 @@ +/* +	usbmuxd - iPhone/iPod Touch USB multiplex server daemon + +Copyright (C) 2009	Hector Martin "marcan" <hector@marcansoft.com> + +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 <config.h> +#endif + +#include <stdlib.h> +#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; +} @@ -0,0 +1,47 @@ +/* +	usbmuxd - iPhone/iPod Touch USB multiplex server daemon + +Copyright (C) 2009	Hector Martin "marcan" <hector@marcansoft.com> + +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 <poll.h> + +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 | 
