summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Hector Martin2009-04-27 01:37:05 +0200
committerGravatar Hector Martin2009-04-27 01:37:05 +0200
commitd982007a7350df35c5aeba820a520779694514a7 (patch)
tree62d16ab56c7f92b12999bbcdbe7ce0f9293fe069
parent677fa91e376b98d871bb238ecb84685dd4f33ebd (diff)
downloadusbmuxd-d982007a7350df35c5aeba820a520779694514a7.tar.gz
usbmuxd-d982007a7350df35c5aeba820a520779694514a7.tar.bz2
general framework, unix socket listen, utils
-rw-r--r--CMakeLists.txt2
-rw-r--r--log.c50
-rw-r--r--log.h38
-rw-r--r--main.c92
-rw-r--r--mux.c (renamed from usbmuxd.c)10
-rw-r--r--mux.h24
-rw-r--r--usb-linux.c4
-rw-r--r--usb.h29
-rw-r--r--utils.c57
-rw-r--r--utils.h47
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
13add_definitions(-Wall -O2) 13add_definitions(-Wall -O2)
14add_executable(usbmuxd usbmuxd.c usb-linux.c) 14add_executable(usbmuxd main.c usb-linux.c log.c utils.c)
15target_link_libraries(usbmuxd ${LIBS}) 15target_link_libraries(usbmuxd ${LIBS})
16 16
diff --git a/log.c b/log.c
new file mode 100644
index 0000000..d7f57df
--- /dev/null
+++ b/log.c
@@ -0,0 +1,50 @@
1/*
2 usbmuxd - iPhone/iPod Touch USB multiplex server daemon
3
4Copyright (C) 2009 Hector Martin "marcan" <hector@marcansoft.com>
5
6This program is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation, either version 2 or version 3.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with this program; if not, write to the Free Software
17Foundation, 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
32int log_level = LOG_SPEW;
33
34void 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
diff --git a/log.h b/log.h
new file mode 100644
index 0000000..fda2e1a
--- /dev/null
+++ b/log.h
@@ -0,0 +1,38 @@
1/*
2 usbmuxd - iPhone/iPod Touch USB multiplex server daemon
3
4Copyright (C) 2009 Hector Martin "marcan" <hector@marcansoft.com>
5
6This program is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation, either version 2 or version 3.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with this program; if not, write to the Free Software
17Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
19*/
20
21#ifndef __LOG_H__
22#define __LOG_H__
23
24enum 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
34extern int log_level;
35
36void usbmuxd_log(enum loglevel level, const char *fmt, ...);
37
38#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 @@
1/*
2 usbmuxd - iPhone/iPod Touch USB multiplex server daemon
3
4Copyright (C) 2009 Hector Martin "marcan" <hector@marcansoft.com>
5
6This program is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation, either version 2 or version 3.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with this program; if not, write to the Free Software
17Foundation, 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
38const char *socket_path = "/tmp/usbmuxd"; //TODO: CHANGEME
39
40int 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
72int 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}
diff --git a/usbmuxd.c b/mux.c
index c52dcf9..c56f8f4 100644
--- a/usbmuxd.c
+++ b/mux.c
@@ -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
28int main(int argc, char *argv[])
29{
30 printf("Hello, world!\n");
31
32 return 0;
33}
diff --git a/mux.h b/mux.h
new file mode 100644
index 0000000..d6f61af
--- /dev/null
+++ b/mux.h
@@ -0,0 +1,24 @@
1/*
2 usbmuxd - iPhone/iPod Touch USB multiplex server daemon
3
4Copyright (C) 2009 Hector Martin "marcan" <hector@marcansoft.com>
5
6This program is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation, either version 2 or version 3.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with this program; if not, write to the Free Software
17Foundation, 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
31int usb_init(void)
32{
33 return 0;
34}
diff --git a/usb.h b/usb.h
new file mode 100644
index 0000000..cf25c3a
--- /dev/null
+++ b/usb.h
@@ -0,0 +1,29 @@
1/*
2 usbmuxd - iPhone/iPod Touch USB multiplex server daemon
3
4Copyright (C) 2009 Hector Martin "marcan" <hector@marcansoft.com>
5
6This program is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation, either version 2 or version 3.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with this program; if not, write to the Free Software
17Foundation, 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
27int usb_init(void);
28
29#endif
diff --git a/utils.c b/utils.c
new file mode 100644
index 0000000..c3d5c50
--- /dev/null
+++ b/utils.c
@@ -0,0 +1,57 @@
1/*
2 usbmuxd - iPhone/iPod Touch USB multiplex server daemon
3
4Copyright (C) 2009 Hector Martin "marcan" <hector@marcansoft.com>
5
6This program is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation, either version 2 or version 3.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with this program; if not, write to the Free Software
17Foundation, 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
28void 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}
35void 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
49void 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}
diff --git a/utils.h b/utils.h
new file mode 100644
index 0000000..465487d
--- /dev/null
+++ b/utils.h
@@ -0,0 +1,47 @@
1/*
2 usbmuxd - iPhone/iPod Touch USB multiplex server daemon
3
4Copyright (C) 2009 Hector Martin "marcan" <hector@marcansoft.com>
5
6This program is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation, either version 2 or version 3.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with this program; if not, write to the Free Software
17Foundation, 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
26enum fdowner {
27 FD_LISTEN,
28 FD_CLIENT,
29 FD_USB
30};
31
32typedef struct {
33 int count;
34 int capacity;
35 enum fdowner *owners;
36 struct pollfd *fds;
37} fdlist;
38
39void fdlist_create(fdlist *list);
40void fdlist_add(fdlist *list, enum fdowner owner, int fd, short events);
41void fdlist_free(fdlist *list);
42
43#define MIN(X, Y) ((X) < (Y) ? (X) : (Y))
44
45
46
47#endif