summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/client.c121
1 files changed, 117 insertions, 4 deletions
diff --git a/src/client.c b/src/client.c
index bbdac84..a24233c 100644
--- a/src/client.c
+++ b/src/client.c
@@ -26,6 +26,7 @@
26 26
27#include <stdlib.h> 27#include <stdlib.h>
28#include <string.h> 28#include <string.h>
29#include <stdio.h>
29#include <errno.h> 30#include <errno.h>
30#include <unistd.h> 31#include <unistd.h>
31#include <sys/types.h> 32#include <sys/types.h>
@@ -69,10 +70,13 @@ struct mux_client {
69 int connect_device; 70 int connect_device;
70 enum client_state state; 71 enum client_state state;
71 uint32_t proto_version; 72 uint32_t proto_version;
73 uint32_t number;
74 plist_t info;
72}; 75};
73 76
74static struct collection client_list; 77static struct collection client_list;
75pthread_mutex_t client_list_mutex; 78pthread_mutex_t client_list_mutex;
79static uint32_t client_number = 0;
76 80
77/** 81/**
78 * Receive raw data from the client socket. 82 * Receive raw data from the client socket.
@@ -197,8 +201,10 @@ int client_accept(int listenfd)
197 client->ib_capacity = CMD_BUF_SIZE; 201 client->ib_capacity = CMD_BUF_SIZE;
198 client->state = CLIENT_COMMAND; 202 client->state = CLIENT_COMMAND;
199 client->events = POLLIN; 203 client->events = POLLIN;
204 client->info = NULL;
200 205
201 pthread_mutex_lock(&client_list_mutex); 206 pthread_mutex_lock(&client_list_mutex);
207 client->number = client_number++;
202 collection_add(&client_list, client); 208 collection_add(&client_list, client);
203 pthread_mutex_unlock(&client_list_mutex); 209 pthread_mutex_unlock(&client_list_mutex);
204 210
@@ -229,10 +235,10 @@ void client_close(struct mux_client *client)
229 device_abort_connect(client->connect_device, client); 235 device_abort_connect(client->connect_device, client);
230 } 236 }
231 close(client->fd); 237 close(client->fd);
232 if(client->ob_buf) 238 free(client->ob_buf);
233 free(client->ob_buf); 239 free(client->ib_buf);
234 if(client->ib_buf) 240 plist_free(client->info);
235 free(client->ib_buf); 241
236 pthread_mutex_lock(&client_list_mutex); 242 pthread_mutex_lock(&client_list_mutex);
237 collection_remove(&client_list, client); 243 collection_remove(&client_list, client);
238 pthread_mutex_unlock(&client_list_mutex); 244 pthread_mutex_unlock(&client_list_mutex);
@@ -377,6 +383,69 @@ static int send_device_list(struct mux_client *client, uint32_t tag)
377 return res; 383 return res;
378} 384}
379 385
386static int send_listener_list(struct mux_client *client, uint32_t tag)
387{
388 int res = -1;
389
390 plist_t dict = plist_new_dict();
391 plist_t listeners = plist_new_array();
392
393 pthread_mutex_lock(&client_list_mutex);
394 FOREACH(struct mux_client *lc, &client_list) {
395 if (lc->state == CLIENT_LISTEN) {
396 plist_t n = NULL;
397 plist_t l = plist_new_dict();
398 plist_dict_set_item(l, "Blacklisted", plist_new_bool(0));
399 n = NULL;
400 if (lc->info) {
401 n = plist_dict_get_item(lc->info, "BundleID");
402 }
403 if (n) {
404 plist_dict_set_item(l, "BundleID", plist_copy(n));
405 }
406 plist_dict_set_item(l, "ConnType", plist_new_uint(0));
407
408 n = NULL;
409 char *progname = NULL;
410 if (lc->info) {
411 n = plist_dict_get_item(lc->info, "ProgName");
412 }
413 if (n) {
414 plist_get_string_val(n, &progname);
415 }
416 if (!progname) {
417 progname = strdup("unknown");
418 }
419 char *idstring = malloc(strlen(progname) + 12);
420 sprintf(idstring, "%u-%s", client->number, progname);
421
422 plist_dict_set_item(l, "ID String", plist_new_string(idstring));
423 free(idstring);
424 plist_dict_set_item(l, "ProgName", plist_new_string(progname));
425 free(progname);
426
427 n = NULL;
428 uint64_t version = 0;
429 if (lc->info) {
430 n = plist_dict_get_item(lc->info, "kLibUSBMuxVersion");
431 }
432 if (n) {
433 plist_get_uint_val(n, &version);
434 }
435 plist_dict_set_item(l, "kLibUSBMuxVersion", plist_new_uint(version));
436
437 plist_array_append_item(listeners, l);
438 }
439 } ENDFOREACH
440 pthread_mutex_unlock(&client_list_mutex);
441
442 plist_dict_set_item(dict, "ListenerList", listeners);
443 res = send_plist_pkt(client, tag, dict);
444 plist_free(dict);
445
446 return res;
447}
448
380static int send_system_buid(struct mux_client *client, uint32_t tag) 449static int send_system_buid(struct mux_client *client, uint32_t tag)
381{ 450{
382 int res = -1; 451 int res = -1;
@@ -489,6 +558,43 @@ static char* plist_dict_get_string_val(plist_t dict, const char* key)
489 return str; 558 return str;
490} 559}
491 560
561static void update_client_info(struct mux_client *client, plist_t dict)
562{
563 plist_t node = NULL;
564 char *strval = NULL;
565 uint64_t u64val = 0;
566 plist_t info = plist_new_dict();
567
568 node = plist_dict_get_item(dict, "BundleID");
569 if (node && (plist_get_node_type(node) == PLIST_STRING)) {
570 plist_get_string_val(node, &strval);
571 plist_dict_set_item(info, "BundleID", plist_new_string(strval));
572 }
573
574 strval = NULL;
575 node = plist_dict_get_item(dict, "ClientVersionString");
576 if (node && (plist_get_node_type(node) == PLIST_STRING)) {
577 plist_get_string_val(node, &strval);
578 plist_dict_set_item(info, "ClientVersionString", plist_new_string(strval));
579 }
580
581 strval = NULL;
582 node = plist_dict_get_item(dict, "ProgName");
583 if (node && (plist_get_node_type(node) == PLIST_STRING)) {
584 plist_get_string_val(node, &strval);
585 plist_dict_set_item(info, "ProgName", plist_new_string(strval));
586 }
587
588 u64val = 0;
589 node = plist_dict_get_item(dict, "kLibUSBMuxVersion");
590 if (node && (plist_get_node_type(node) == PLIST_UINT)) {
591 plist_get_uint_val(node, &u64val);
592 plist_dict_set_item(info, "kLibUSBMuxVersion", plist_new_uint(u64val));
593 }
594 plist_free(client->info);
595 client->info = info;
596}
597
492static int client_command(struct mux_client *client, struct usbmuxd_header *hdr) 598static int client_command(struct mux_client *client, struct usbmuxd_header *hdr)
493{ 599{
494 int res; 600 int res;
@@ -536,6 +642,7 @@ static int client_command(struct mux_client *client, struct usbmuxd_header *hdr)
536 plist_free(dict); 642 plist_free(dict);
537 return -1; 643 return -1;
538 } 644 }
645 update_client_info(client, dict);
539 if (!strcmp(message, "Listen")) { 646 if (!strcmp(message, "Listen")) {
540 free(message); 647 free(message);
541 plist_free(dict); 648 plist_free(dict);
@@ -592,6 +699,12 @@ static int client_command(struct mux_client *client, struct usbmuxd_header *hdr)
592 if (send_device_list(client, hdr->tag) < 0) 699 if (send_device_list(client, hdr->tag) < 0)
593 return -1; 700 return -1;
594 return 0; 701 return 0;
702 } else if (!strcmp(message, "ListListeners")) {
703 free(message);
704 plist_free(dict);
705 if (send_listener_list(client, hdr->tag) < 0)
706 return -1;
707 return 0;
595 } else if (!strcmp(message, "ReadBUID")) { 708 } else if (!strcmp(message, "ReadBUID")) {
596 free(message); 709 free(message);
597 plist_free(dict); 710 plist_free(dict);