summaryrefslogtreecommitdiffstats
path: root/usbmuxd
diff options
context:
space:
mode:
authorGravatar Nikias Bassen2009-08-20 01:19:09 +0200
committerGravatar Hector Martin2009-08-21 03:08:18 +0200
commitc46062aca98f2f077b3bab5c5f72ff2cb57b9dc2 (patch)
tree0934caaa277436a42c515c9ccc86acb004620c7a /usbmuxd
parent886d4014509d64023ecf99b57d0fd39818e85bd4 (diff)
downloadusbmuxd-c46062aca98f2f077b3bab5c5f72ff2cb57b9dc2.tar.gz
usbmuxd-c46062aca98f2f077b3bab5c5f72ff2cb57b9dc2.tar.bz2
Updated usbmuxd protocol definition and public header.
[Hector] Merged by putting utils.c into a common dir, avoiding log.c dependency for libusbmuxd, adding CMake magic to tie things up.
Diffstat (limited to 'usbmuxd')
-rw-r--r--usbmuxd/CMakeLists.txt8
-rw-r--r--usbmuxd/client.c36
-rw-r--r--usbmuxd/client.h50
-rw-r--r--usbmuxd/utils.c110
-rw-r--r--usbmuxd/utils.h65
5 files changed, 26 insertions, 243 deletions
diff --git a/usbmuxd/CMakeLists.txt b/usbmuxd/CMakeLists.txt
index b982dc0..7d0d3d8 100644
--- a/usbmuxd/CMakeLists.txt
+++ b/usbmuxd/CMakeLists.txt
@@ -2,8 +2,12 @@ find_package(USB REQUIRED)
2include_directories(${USB_INCLUDE_DIRS}) 2include_directories(${USB_INCLUDE_DIRS})
3set(LIBS ${LIBS} ${USB_LIBRARIES}) 3set(LIBS ${LIBS} ${USB_LIBRARIES})
4 4
5add_definitions(-Wall -O2 -g) 5include_directories (${CMAKE_SOURCE_DIR}/common)
6add_executable(usbmuxd main.c usb-linux.c log.c utils.c device.c client.c) 6include_directories (${CMAKE_SOURCE_DIR}/usbmuxd)
7include_directories (${CMAKE_SOURCE_DIR}/libusbmuxd)
8
9add_definitions(-Wall -O2 -g -DUSBMUXD_DAEMON)
10add_executable(usbmuxd main.c usb-linux.c log.c ../common/utils.c device.c client.c)
7target_link_libraries(usbmuxd ${LIBS}) 11target_link_libraries(usbmuxd ${LIBS})
8 12
9install(TARGETS usbmuxd RUNTIME DESTINATION sbin) \ No newline at end of file 13install(TARGETS usbmuxd RUNTIME DESTINATION sbin) \ No newline at end of file
diff --git a/usbmuxd/client.c b/usbmuxd/client.c
index 7a3160f..0e47e84 100644
--- a/usbmuxd/client.c
+++ b/usbmuxd/client.c
@@ -150,10 +150,10 @@ void client_get_fds(struct fdlist *list)
150 } ENDFOREACH 150 } ENDFOREACH
151} 151}
152 152
153static int send_pkt(struct mux_client *client, uint32_t tag, enum client_msgtype msg, void *payload, int payload_length) 153static int send_pkt(struct mux_client *client, uint32_t tag, enum usbmuxd_msgtype msg, void *payload, int payload_length)
154{ 154{
155 struct client_header hdr; 155 struct usbmuxd_header hdr;
156 hdr.version = CLIENT_PROTOCOL_VERSION; 156 hdr.version = USBMUXD_PROTOCOL_VERSION;
157 hdr.length = sizeof(hdr) + payload_length; 157 hdr.length = sizeof(hdr) + payload_length;
158 hdr.message = msg; 158 hdr.message = msg;
159 hdr.tag = tag; 159 hdr.tag = tag;
@@ -176,7 +176,7 @@ static int send_result(struct mux_client *client, uint32_t tag, uint32_t result)
176 return send_pkt(client, tag, MESSAGE_RESULT, &result, sizeof(uint32_t)); 176 return send_pkt(client, tag, MESSAGE_RESULT, &result, sizeof(uint32_t));
177} 177}
178 178
179int client_notify_connect(struct mux_client *client, enum client_result result) 179int client_notify_connect(struct mux_client *client, enum usbmuxd_result result)
180{ 180{
181 usbmuxd_log(LL_SPEW, "client_notify_connect fd %d result %d", client->fd, result); 181 usbmuxd_log(LL_SPEW, "client_notify_connect fd %d result %d", client->fd, result);
182 if(client->state == CLIENT_DEAD) 182 if(client->state == CLIENT_DEAD)
@@ -201,13 +201,13 @@ int client_notify_connect(struct mux_client *client, enum client_result result)
201 201
202static int notify_device(struct mux_client *client, struct device_info *dev) 202static int notify_device(struct mux_client *client, struct device_info *dev)
203{ 203{
204 struct client_msg_dev dmsg; 204 struct usbmuxd_device_record dmsg;
205 memset(&dmsg, 0, sizeof(dmsg)); 205 memset(&dmsg, 0, sizeof(dmsg));
206 dmsg.device_id = dev->id; 206 dmsg.device_id = dev->id;
207 strncpy(dmsg.device_serial, dev->serial, 256); 207 strncpy(dmsg.serial_number, dev->serial, 256);
208 dmsg.device_serial[255] = 0; 208 dmsg.serial_number[255] = 0;
209 dmsg.location = dev->location; 209 dmsg.location = dev->location;
210 dmsg.device_pid = dev->pid; 210 dmsg.product_id = dev->pid;
211 return send_pkt(client, 0, MESSAGE_DEVICE_ADD, &dmsg, sizeof(dmsg)); 211 return send_pkt(client, 0, MESSAGE_DEVICE_ADD, &dmsg, sizeof(dmsg));
212} 212}
213 213
@@ -225,7 +225,7 @@ static int start_listen(struct mux_client *client)
225 count = device_get_list(devs); 225 count = device_get_list(devs);
226 226
227 // going to need a larger buffer for many devices 227 // going to need a larger buffer for many devices
228 int needed_buffer = count * (sizeof(struct client_msg_dev) + sizeof(struct client_header)) + REPLY_BUF_SIZE; 228 int needed_buffer = count * (sizeof(struct usbmuxd_device_record) + sizeof(struct usbmuxd_header)) + REPLY_BUF_SIZE;
229 if(client->ob_capacity < needed_buffer) { 229 if(client->ob_capacity < needed_buffer) {
230 usbmuxd_log(LL_DEBUG, "Enlarging client %d reply buffer %d -> %d to make space for device notifications", client->fd, client->ob_capacity, needed_buffer); 230 usbmuxd_log(LL_DEBUG, "Enlarging client %d reply buffer %d -> %d to make space for device notifications", client->fd, client->ob_capacity, needed_buffer);
231 client->ob_buf = realloc(client->ob_buf, needed_buffer); 231 client->ob_buf = realloc(client->ob_buf, needed_buffer);
@@ -242,7 +242,7 @@ static int start_listen(struct mux_client *client)
242 return count; 242 return count;
243} 243}
244 244
245static int client_command(struct mux_client *client, struct client_header *hdr, const char *payload) 245static int client_command(struct mux_client *client, struct usbmuxd_header *hdr, const char *payload)
246{ 246{
247 int res; 247 int res;
248 usbmuxd_log(LL_DEBUG, "Client command in fd %d len %d ver %d msg %d tag %d", client->fd, hdr->length, hdr->version, hdr->message, hdr->tag); 248 usbmuxd_log(LL_DEBUG, "Client command in fd %d len %d ver %d msg %d tag %d", client->fd, hdr->length, hdr->version, hdr->message, hdr->tag);
@@ -255,7 +255,7 @@ static int client_command(struct mux_client *client, struct client_header *hdr,
255 return -1; 255 return -1;
256 } 256 }
257 257
258 struct client_msg_connect *ch; 258 struct usbmuxd_connect_request *ch;
259 switch(hdr->message) { 259 switch(hdr->message) {
260 case MESSAGE_LISTEN: 260 case MESSAGE_LISTEN:
261 if(send_result(client, hdr->tag, 0) < 0) 261 if(send_result(client, hdr->tag, 0) < 0)
@@ -318,8 +318,8 @@ static void process_recv(struct mux_client *client)
318{ 318{
319 int res; 319 int res;
320 int did_read = 0; 320 int did_read = 0;
321 if(client->ib_size < sizeof(struct client_header)) { 321 if(client->ib_size < sizeof(struct usbmuxd_header)) {
322 res = recv(client->fd, client->ib_buf + client->ib_size, sizeof(struct client_header) - client->ib_size, 0); 322 res = recv(client->fd, client->ib_buf + client->ib_size, sizeof(struct usbmuxd_header) - client->ib_size, 0);
323 if(res <= 0) { 323 if(res <= 0) {
324 if(res < 0) 324 if(res < 0)
325 usbmuxd_log(LL_ERROR, "Receive from client fd %d failed: %s", client->fd, strerror(errno)); 325 usbmuxd_log(LL_ERROR, "Receive from client fd %d failed: %s", client->fd, strerror(errno));
@@ -329,20 +329,20 @@ static void process_recv(struct mux_client *client)
329 return; 329 return;
330 } 330 }
331 client->ib_size += res; 331 client->ib_size += res;
332 if(client->ib_size < sizeof(struct client_header)) 332 if(client->ib_size < sizeof(struct usbmuxd_header))
333 return; 333 return;
334 did_read = 1; 334 did_read = 1;
335 } 335 }
336 struct client_header *hdr = (void*)client->ib_buf; 336 struct usbmuxd_header *hdr = (void*)client->ib_buf;
337 if(hdr->version != CLIENT_PROTOCOL_VERSION) { 337 if(hdr->version != USBMUXD_PROTOCOL_VERSION) {
338 usbmuxd_log(LL_INFO, "Client %d version mismatch: expected %d, got %d", client->fd, CLIENT_PROTOCOL_VERSION, hdr->version); 338 usbmuxd_log(LL_INFO, "Client %d version mismatch: expected %d, got %d", client->fd, USBMUXD_PROTOCOL_VERSION, hdr->version);
339 client_close(client); 339 client_close(client);
340 } 340 }
341 if(hdr->length > client->ib_capacity) { 341 if(hdr->length > client->ib_capacity) {
342 usbmuxd_log(LL_INFO, "Client %d message is too long (%d bytes)", client->fd, hdr->length); 342 usbmuxd_log(LL_INFO, "Client %d message is too long (%d bytes)", client->fd, hdr->length);
343 client_close(client); 343 client_close(client);
344 } 344 }
345 if(hdr->length < sizeof(struct client_header)) { 345 if(hdr->length < sizeof(struct usbmuxd_header)) {
346 usbmuxd_log(LL_ERROR, "Client %d message is too short (%d bytes)", client->fd, hdr->length); 346 usbmuxd_log(LL_ERROR, "Client %d message is too short (%d bytes)", client->fd, hdr->length);
347 client_close(client); 347 client_close(client);
348 } 348 }
diff --git a/usbmuxd/client.h b/usbmuxd/client.h
index 0cda676..4fc1ab4 100644
--- a/usbmuxd/client.h
+++ b/usbmuxd/client.h
@@ -22,62 +22,16 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22#define __CLIENT_H__ 22#define __CLIENT_H__
23 23
24#include <stdint.h> 24#include <stdint.h>
25#include "usbmuxd-proto.h"
25 26
26struct device_info; 27struct device_info;
27struct mux_client; 28struct mux_client;
28 29
29enum client_result {
30 RESULT_OK = 0,
31 RESULT_BADCOMMAND = 1,
32 RESULT_BADDEV = 2,
33 RESULT_CONNREFUSED = 3,
34 // ???
35 // ???
36 RESULT_BADVERSION = 6,
37};
38
39enum client_msgtype {
40 MESSAGE_RESULT = 1,
41 MESSAGE_CONNECT = 2,
42 MESSAGE_LISTEN = 3,
43 MESSAGE_DEVICE_ADD = 4,
44 MESSAGE_DEVICE_REMOVE = 5,
45 //???
46 //???
47 //MESSAGE_PLIST = 8,
48};
49
50#define CLIENT_PROTOCOL_VERSION 0
51
52struct client_header {
53 uint32_t length;
54 uint32_t version;
55 uint32_t message;
56 uint32_t tag;
57};
58
59struct client_msg_result {
60 uint32_t result;
61};
62
63struct client_msg_connect {
64 uint32_t device_id;
65 uint16_t port;
66};
67
68struct client_msg_dev {
69 uint32_t device_id;
70 uint16_t device_pid;
71 char device_serial[256];
72 uint16_t padding;
73 uint32_t location;
74};
75
76int client_read(struct mux_client *client, void *buffer, int len); 30int client_read(struct mux_client *client, void *buffer, int len);
77int client_write(struct mux_client *client, void *buffer, int len); 31int client_write(struct mux_client *client, void *buffer, int len);
78int client_set_events(struct mux_client *client, short events); 32int client_set_events(struct mux_client *client, short events);
79void client_close(struct mux_client *client); 33void client_close(struct mux_client *client);
80int client_notify_connect(struct mux_client *client, enum client_result result); 34int client_notify_connect(struct mux_client *client, enum usbmuxd_result result);
81 35
82void client_device_add(struct device_info *dev); 36void client_device_add(struct device_info *dev);
83void client_device_remove(int device_id); 37void client_device_remove(int device_id);
diff --git a/usbmuxd/utils.c b/usbmuxd/utils.c
deleted file mode 100644
index 1ffa04a..0000000
--- a/usbmuxd/utils.c
+++ /dev/null
@@ -1,110 +0,0 @@
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 <string.h>
27#include "utils.h"
28#include "log.h"
29
30void fdlist_create(struct fdlist *list)
31{
32 list->count = 0;
33 list->capacity = 4;
34 list->owners = malloc(sizeof(*list->owners) * list->capacity);
35 list->fds = malloc(sizeof(*list->fds) * list->capacity);
36}
37void fdlist_add(struct fdlist *list, enum fdowner owner, int fd, short events)
38{
39 if(list->count == list->capacity) {
40 list->capacity *= 2;
41 list->owners = realloc(list->owners, sizeof(*list->owners) * list->capacity);
42 list->fds = realloc(list->fds, sizeof(*list->fds) * list->capacity);
43 }
44 list->owners[list->count] = owner;
45 list->fds[list->count].fd = fd;
46 list->fds[list->count].events = events;
47 list->fds[list->count].revents = 0;
48 list->count++;
49}
50
51void fdlist_free(struct fdlist *list)
52{
53 list->count = 0;
54 list->capacity = 0;
55 free(list->owners);
56 list->owners = NULL;
57 free(list->fds);
58 list->fds = NULL;
59}
60
61void collection_init(struct collection *col)
62{
63 col->list = malloc(sizeof(void *));
64 memset(col->list, 0, sizeof(void *));
65 col->capacity = 1;
66}
67
68void collection_free(struct collection *col)
69{
70 free(col->list);
71 col->list = NULL;
72 col->capacity = 0;
73}
74
75void collection_add(struct collection *col, void *element)
76{
77 int i;
78 for(i=0; i<col->capacity; i++) {
79 if(!col->list[i]) {
80 col->list[i] = element;
81 return;
82 }
83 }
84 col->list = realloc(col->list, sizeof(void*) * col->capacity * 2);
85 memset(&col->list[col->capacity], 0, sizeof(void *) * col->capacity);
86 col->list[col->capacity] = element;
87 col->capacity *= 2;
88}
89
90void collection_remove(struct collection *col, void *element)
91{
92 int i;
93 for(i=0; i<col->capacity; i++) {
94 if(col->list[i] == element) {
95 col->list[i] = NULL;
96 return;
97 }
98 }
99 usbmuxd_log(LL_ERROR, "collection_remove: element %p not present in collection %p (cap %d)", element, col, col->capacity);
100}
101
102int collection_count(struct collection *col)
103{
104 int i, cnt = 0;
105 for(i=0; i<col->capacity; i++) {
106 if(col->list[i])
107 cnt++;
108 }
109 return cnt;
110}
diff --git a/usbmuxd/utils.h b/usbmuxd/utils.h
deleted file mode 100644
index ad4ac9d..0000000
--- a/usbmuxd/utils.h
+++ /dev/null
@@ -1,65 +0,0 @@
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 __UTILS_H__
22#define __UTILS_H__
23
24#include <poll.h>
25
26enum fdowner {
27 FD_LISTEN,
28 FD_CLIENT,
29 FD_USB
30};
31
32struct fdlist {
33 int count;
34 int capacity;
35 enum fdowner *owners;
36 struct pollfd *fds;
37};
38
39void fdlist_create(struct fdlist *list);
40void fdlist_add(struct fdlist *list, enum fdowner owner, int fd, short events);
41void fdlist_free(struct fdlist *list);
42
43struct collection {
44 void **list;
45 int capacity;
46};
47
48void collection_init(struct collection *col);
49void collection_add(struct collection *col, void *element);
50void collection_remove(struct collection *col, void *element);
51int collection_count(struct collection *col);
52void collection_free(struct collection *col);
53
54#define FOREACH(var, col) \
55 do { \
56 int _iter; \
57 for(_iter=0; _iter<(col)->capacity; _iter++) { \
58 if(!(col)->list[_iter]) continue; \
59 var = (col)->list[_iter];
60
61#define ENDFOREACH \
62 } \
63 } while(0);
64
65#endif