diff options
Diffstat (limited to 'testclient.c')
| -rw-r--r-- | testclient.c | 148 |
1 files changed, 148 insertions, 0 deletions
diff --git a/testclient.c b/testclient.c new file mode 100644 index 0000000..fafbf23 --- /dev/null +++ b/testclient.c | |||
| @@ -0,0 +1,148 @@ | |||
| 1 | #include <stdio.h> | ||
| 2 | #include <stdlib.h> | ||
| 3 | #include <fcntl.h> | ||
| 4 | #include <stddef.h> | ||
| 5 | #include <sys/socket.h> | ||
| 6 | #include <sys/un.h> | ||
| 7 | #include <unistd.h> | ||
| 8 | #include <errno.h> | ||
| 9 | #include <arpa/inet.h> | ||
| 10 | #include "usbmuxd.h" | ||
| 11 | #include "sock_stuff.h" | ||
| 12 | |||
| 13 | #define SOCKET_FILE "/var/run/usbmuxd" | ||
| 14 | |||
| 15 | int usbmuxd_get_result(int sfd, uint32_t tag, uint32_t *result) | ||
| 16 | { | ||
| 17 | struct usbmux_result res; | ||
| 18 | int recv_len; | ||
| 19 | |||
| 20 | if (!result) { | ||
| 21 | return -EINVAL; | ||
| 22 | } | ||
| 23 | |||
| 24 | if ((recv_len = recv_buf(sfd, &res, sizeof(res))) <= 0) { | ||
| 25 | perror("recv"); | ||
| 26 | return -errno; | ||
| 27 | } else { | ||
| 28 | if ((recv_len == sizeof(res)) | ||
| 29 | && (res.header.length == recv_len) | ||
| 30 | && (res.header.reserved == 0) | ||
| 31 | && (res.header.type == usbmux_result) | ||
| 32 | ) { | ||
| 33 | *result = res.result; | ||
| 34 | if (res.header.tag == tag) { | ||
| 35 | return 1; | ||
| 36 | } else { | ||
| 37 | return 0; | ||
| 38 | } | ||
| 39 | } | ||
| 40 | } | ||
| 41 | |||
| 42 | return -1; | ||
| 43 | } | ||
| 44 | |||
| 45 | int main(int argc, char **argv) | ||
| 46 | { | ||
| 47 | int sfd; | ||
| 48 | int recv_len = 0; | ||
| 49 | int hello_done; | ||
| 50 | int connected; | ||
| 51 | uint32_t pktlen; | ||
| 52 | unsigned char *buf; | ||
| 53 | struct usbmux_header hello; | ||
| 54 | struct usbmux_dev_info device_info; | ||
| 55 | |||
| 56 | sfd = connect_unix_socket(SOCKET_FILE); | ||
| 57 | if (sfd < 0) { | ||
| 58 | printf("error opening socket, terminating.\n"); | ||
| 59 | return -1; | ||
| 60 | } | ||
| 61 | |||
| 62 | // send hello | ||
| 63 | hello.length = sizeof(struct usbmux_header); | ||
| 64 | hello.reserved = 0; | ||
| 65 | hello.type = usbmux_hello; | ||
| 66 | hello.tag = 2; | ||
| 67 | |||
| 68 | hello_done = 0; | ||
| 69 | connected = 0; | ||
| 70 | |||
| 71 | fprintf(stdout, "sending Hello packet\n"); | ||
| 72 | if (send(sfd, &hello, hello.length, 0) == hello.length) { | ||
| 73 | uint32_t res = -1; | ||
| 74 | // get response | ||
| 75 | if (usbmuxd_get_result(sfd, hello.tag, &res) && (res==0)) { | ||
| 76 | fprintf(stdout, "Got Hello Response!\n"); | ||
| 77 | hello_done = 1; | ||
| 78 | } else { | ||
| 79 | fprintf(stderr, "Did not get Hello response (with result=0)...\n"); | ||
| 80 | close(sfd); | ||
| 81 | return -1; | ||
| 82 | } | ||
| 83 | |||
| 84 | device_info.device_id = 0; | ||
| 85 | |||
| 86 | if (hello_done) { | ||
| 87 | // get all devices | ||
| 88 | while (1) { | ||
| 89 | if (recv_buf_timeout(sfd, &pktlen, 4, MSG_PEEK, 1000) == 4) { | ||
| 90 | buf = (unsigned char*)malloc(pktlen); | ||
| 91 | if (!buf) { | ||
| 92 | exit(-ENOMEM); | ||
| 93 | } | ||
| 94 | recv_len = recv_buf(sfd, buf, pktlen); | ||
| 95 | if (recv_len < pktlen) { | ||
| 96 | fprintf(stdout, "received less data than specified in header!\n"); | ||
| 97 | } | ||
| 98 | fprintf(stdout, "got device data:\n"); | ||
| 99 | //log_debug_buffer(stdout, (char*)buf, pktlen); | ||
| 100 | memcpy(&device_info, buf + sizeof(struct usbmux_header), sizeof(device_info)); | ||
| 101 | free(buf); | ||
| 102 | } else { | ||
| 103 | // we _should_ have all of them now. | ||
| 104 | // or perhaps an error occured. | ||
| 105 | break; | ||
| 106 | } | ||
| 107 | } | ||
| 108 | } | ||
| 109 | |||
| 110 | if (device_info.device_id > 0) { | ||
| 111 | struct usbmux_connect_request c_req; | ||
| 112 | |||
| 113 | // try to connect to last device found | ||
| 114 | c_req.header.length = sizeof(c_req); | ||
| 115 | c_req.header.reserved = 0; | ||
| 116 | c_req.header.type = usbmux_connect; | ||
| 117 | c_req.header.tag = 3; | ||
| 118 | c_req.device_id = device_info.device_id; | ||
| 119 | c_req.port = htons(22); | ||
| 120 | c_req.reserved = 0; | ||
| 121 | |||
| 122 | if (send_buf(sfd, &c_req, sizeof(c_req)) < 0) { | ||
| 123 | perror("send"); | ||
| 124 | } else { | ||
| 125 | // read ACK | ||
| 126 | res = -1; | ||
| 127 | if (usbmuxd_get_result(sfd, c_req.header.tag, &res)) { | ||
| 128 | if (res == 0) { | ||
| 129 | fprintf(stdout, "Connect success!\n"); | ||
| 130 | connected = 1; | ||
| 131 | } else { | ||
| 132 | fprintf(stderr, "Connect failed, Error code=%d\n", res); | ||
| 133 | } | ||
| 134 | } | ||
| 135 | } | ||
| 136 | } | ||
| 137 | |||
| 138 | if (connected) { | ||
| 139 | |||
| 140 | |||
| 141 | // do communication now. | ||
| 142 | sleep(10); | ||
| 143 | } | ||
| 144 | } | ||
| 145 | close(sfd); | ||
| 146 | |||
| 147 | return 0; | ||
| 148 | } | ||
