diff options
| author | 2008-09-01 15:04:31 -0700 | |
|---|---|---|
| committer | 2008-09-01 15:04:31 -0700 | |
| commit | 2b05e48cb4a90dfc94ff584124f08e431398bb1a (patch) | |
| tree | e0c8255e2cd5592a31295ac8ce89d8846feb7043 /src | |
| parent | 7ac3d681889a6a8f9987837ace5465f2967cfff9 (diff) | |
| download | libimobiledevice-2b05e48cb4a90dfc94ff584124f08e431398bb1a.tar.gz libimobiledevice-2b05e48cb4a90dfc94ff584124f08e431398bb1a.tar.bz2 | |
Enforce a modified kr style.
Use "make indent" from now on before committing.
Diffstat (limited to 'src')
| -rw-r--r-- | src/AFC.c | 597 | ||||
| -rw-r--r-- | src/AFC.h | 1 | ||||
| -rw-r--r-- | src/ifuse.c | 187 | ||||
| -rw-r--r-- | src/initconf.c | 72 | ||||
| -rw-r--r-- | src/iphone.c | 109 | ||||
| -rw-r--r-- | src/iphone.h | 2 | ||||
| -rw-r--r-- | src/lockdown.c | 586 | ||||
| -rw-r--r-- | src/lockdown.h | 3 | ||||
| -rw-r--r-- | src/main.c | 78 | ||||
| -rw-r--r-- | src/plist.c | 76 | ||||
| -rw-r--r-- | src/plist.h | 10 | ||||
| -rw-r--r-- | src/usbmux.c | 188 | ||||
| -rw-r--r-- | src/userpref.c | 115 | ||||
| -rw-r--r-- | src/userpref.h | 18 |
14 files changed, 1164 insertions, 878 deletions
| @@ -33,10 +33,12 @@ extern int debug; | |||
| 33 | * | 33 | * |
| 34 | * @param client The AFC client connection to lock | 34 | * @param client The AFC client connection to lock |
| 35 | */ | 35 | */ |
| 36 | static void afc_lock(iphone_afc_client_t client) { | 36 | static void afc_lock(iphone_afc_client_t client) |
| 37 | if (debug) fprintf(stderr, "Locked\n"); | 37 | { |
| 38 | if (debug) | ||
| 39 | fprintf(stderr, "Locked\n"); | ||
| 38 | while (client->lock) { | 40 | while (client->lock) { |
| 39 | usleep(500); // they say it's obsolete, but whatever | 41 | usleep(500); // they say it's obsolete, but whatever |
| 40 | } | 42 | } |
| 41 | client->lock = 1; | 43 | client->lock = 1; |
| 42 | } | 44 | } |
| @@ -45,9 +47,11 @@ static void afc_lock(iphone_afc_client_t client) { | |||
| 45 | * | 47 | * |
| 46 | * @param client The AFC | 48 | * @param client The AFC |
| 47 | */ | 49 | */ |
| 48 | static void afc_unlock(iphone_afc_client_t client) { // just to be pretty | 50 | static void afc_unlock(iphone_afc_client_t client) |
| 49 | if (debug) fprintf(stderr, "Unlocked\n"); | 51 | { // just to be pretty |
| 50 | client->lock = 0; | 52 | if (debug) |
| 53 | fprintf(stderr, "Unlocked\n"); | ||
| 54 | client->lock = 0; | ||
| 51 | } | 55 | } |
| 52 | 56 | ||
| 53 | /** Makes a connection to the AFC service on the phone. | 57 | /** Makes a connection to the AFC service on the phone. |
| @@ -58,22 +62,23 @@ static void afc_unlock(iphone_afc_client_t client) { // just to be pretty | |||
| 58 | * | 62 | * |
| 59 | * @return A handle to the newly-connected client or NULL upon error. | 63 | * @return A handle to the newly-connected client or NULL upon error. |
| 60 | */ | 64 | */ |
| 61 | iphone_error_t iphone_afc_new_client ( iphone_device_t device, int src_port, int dst_port, iphone_afc_client_t *client ) { | 65 | iphone_error_t iphone_afc_new_client(iphone_device_t device, int src_port, int dst_port, iphone_afc_client_t * client) |
| 66 | { | ||
| 62 | int ret = IPHONE_E_SUCCESS; | 67 | int ret = IPHONE_E_SUCCESS; |
| 63 | iphone_afc_client_t client_loc = (iphone_afc_client_t)malloc(sizeof(struct iphone_afc_client_int)); | 68 | iphone_afc_client_t client_loc = (iphone_afc_client_t) malloc(sizeof(struct iphone_afc_client_int)); |
| 64 | 69 | ||
| 65 | if (!device) return IPHONE_E_INVALID_ARG; | 70 | if (!device) |
| 66 | 71 | return IPHONE_E_INVALID_ARG; | |
| 72 | |||
| 67 | // Attempt connection | 73 | // Attempt connection |
| 68 | client_loc->connection = NULL; | 74 | client_loc->connection = NULL; |
| 69 | ret = iphone_mux_new_client(device, src_port, dst_port,&client_loc->connection); | 75 | ret = iphone_mux_new_client(device, src_port, dst_port, &client_loc->connection); |
| 70 | if (IPHONE_E_SUCCESS != ret || !client_loc->connection) { | 76 | if (IPHONE_E_SUCCESS != ret || !client_loc->connection) { |
| 71 | free(client_loc); | 77 | free(client_loc); |
| 72 | return ret; | 78 | return ret; |
| 73 | } | 79 | } |
| 74 | |||
| 75 | // Allocate a packet | 80 | // Allocate a packet |
| 76 | client_loc->afc_packet = (AFCPacket*)malloc(sizeof(AFCPacket)); | 81 | client_loc->afc_packet = (AFCPacket *) malloc(sizeof(AFCPacket)); |
| 77 | if (!client_loc->afc_packet) { | 82 | if (!client_loc->afc_packet) { |
| 78 | iphone_mux_free_client(client_loc->connection); | 83 | iphone_mux_free_client(client_loc->connection); |
| 79 | free(client_loc); | 84 | free(client_loc); |
| @@ -100,10 +105,11 @@ iphone_error_t iphone_afc_new_client ( iphone_device_t device, int src_port, int | |||
| 100 | * | 105 | * |
| 101 | * @param client The client to disconnect. | 106 | * @param client The client to disconnect. |
| 102 | */ | 107 | */ |
| 103 | iphone_error_t iphone_afc_free_client ( iphone_afc_client_t client ) { | 108 | iphone_error_t iphone_afc_free_client(iphone_afc_client_t client) |
| 109 | { | ||
| 104 | if (!client || !client->connection || !client->afc_packet) | 110 | if (!client || !client->connection || !client->afc_packet) |
| 105 | return IPHONE_E_INVALID_ARG; | 111 | return IPHONE_E_INVALID_ARG; |
| 106 | 112 | ||
| 107 | iphone_mux_free_client(client->connection); | 113 | iphone_mux_free_client(client->connection); |
| 108 | free(client->afc_packet); | 114 | free(client->afc_packet); |
| 109 | free(client); | 115 | free(client); |
| @@ -124,65 +130,78 @@ iphone_error_t iphone_afc_free_client ( iphone_afc_client_t client ) { | |||
| 124 | * reason is that if you set them to different values, it indicates | 130 | * reason is that if you set them to different values, it indicates |
| 125 | * you want to send the data as two packets. | 131 | * you want to send the data as two packets. |
| 126 | */ | 132 | */ |
| 127 | static int dispatch_AFC_packet(iphone_afc_client_t client, const char *data, int length) { | 133 | static int dispatch_AFC_packet(iphone_afc_client_t client, const char *data, int length) |
| 134 | { | ||
| 128 | int bytes = 0, offset = 0; | 135 | int bytes = 0, offset = 0; |
| 129 | char *buffer; | 136 | char *buffer; |
| 137 | |||
| 138 | if (!client || !client->connection || !client->afc_packet) | ||
| 139 | return 0; | ||
| 140 | if (!data || !length) | ||
| 141 | length = 0; | ||
| 130 | 142 | ||
| 131 | if (!client || !client->connection || !client->afc_packet) return 0; | ||
| 132 | if (!data || !length) length = 0; | ||
| 133 | |||
| 134 | client->afc_packet->packet_num++; | 143 | client->afc_packet->packet_num++; |
| 135 | if (!client->afc_packet->entire_length) { | 144 | if (!client->afc_packet->entire_length) { |
| 136 | client->afc_packet->entire_length = (length) ? sizeof(AFCPacket) + length + 1 : sizeof(AFCPacket); | 145 | client->afc_packet->entire_length = (length) ? sizeof(AFCPacket) + length + 1 : sizeof(AFCPacket); |
| 137 | client->afc_packet->this_length = client->afc_packet->entire_length; | 146 | client->afc_packet->this_length = client->afc_packet->entire_length; |
| 138 | } | 147 | } |
| 139 | if (!client->afc_packet->this_length){ | 148 | if (!client->afc_packet->this_length) { |
| 140 | client->afc_packet->this_length = sizeof(AFCPacket); | 149 | client->afc_packet->this_length = sizeof(AFCPacket); |
| 141 | } | 150 | } |
| 142 | 151 | // We want to send two segments; buffer+sizeof(AFCPacket) to | |
| 143 | // We want to send two segments; buffer+sizeof(AFCPacket) to this_length is the parameters | 152 | // this_length is the parameters |
| 144 | // And everything beyond that is the next packet. (for writing) | 153 | // And everything beyond that is the next packet. (for writing) |
| 145 | if (client->afc_packet->this_length != client->afc_packet->entire_length) { | 154 | if (client->afc_packet->this_length != client->afc_packet->entire_length) { |
| 146 | buffer = (char*)malloc(client->afc_packet->this_length); | 155 | buffer = (char *) malloc(client->afc_packet->this_length); |
| 147 | memcpy(buffer, (char*)client->afc_packet, sizeof(AFCPacket)); | 156 | memcpy(buffer, (char *) client->afc_packet, sizeof(AFCPacket)); |
| 148 | offset = client->afc_packet->this_length - sizeof(AFCPacket); | 157 | offset = client->afc_packet->this_length - sizeof(AFCPacket); |
| 149 | 158 | ||
| 150 | if (debug) fprintf(stderr, "dispatch_AFC_packet: Offset: %i\n", offset); | 159 | if (debug) |
| 160 | fprintf(stderr, "dispatch_AFC_packet: Offset: %i\n", offset); | ||
| 151 | if ((length) < (client->afc_packet->entire_length - client->afc_packet->this_length)) { | 161 | if ((length) < (client->afc_packet->entire_length - client->afc_packet->this_length)) { |
| 152 | if (debug){ | 162 | if (debug) { |
| 153 | fprintf(stderr, "dispatch_AFC_packet: Length did not resemble what it was supposed"); | 163 | fprintf(stderr, "dispatch_AFC_packet: Length did not resemble what it was supposed"); |
| 154 | fprintf(stderr, "to based on the packet.\n"); | 164 | fprintf(stderr, "to based on the packet.\n"); |
| 155 | fprintf(stderr, "length minus offset: %i\n", length-offset); | 165 | fprintf(stderr, "length minus offset: %i\n", length - offset); |
| 156 | fprintf(stderr, "rest of packet: %i\n", client->afc_packet->entire_length - client->afc_packet->this_length); | 166 | fprintf(stderr, "rest of packet: %i\n", |
| 167 | client->afc_packet->entire_length - client->afc_packet->this_length); | ||
| 157 | } | 168 | } |
| 158 | free(buffer); | 169 | free(buffer); |
| 159 | return -1; | 170 | return -1; |
| 160 | } | 171 | } |
| 161 | memcpy(buffer+sizeof(AFCPacket), data, offset); | 172 | memcpy(buffer + sizeof(AFCPacket), data, offset); |
| 162 | iphone_mux_send(client->connection, buffer, client->afc_packet->this_length, &bytes); | 173 | iphone_mux_send(client->connection, buffer, client->afc_packet->this_length, &bytes); |
| 163 | free(buffer); | 174 | free(buffer); |
| 164 | if (bytes <= 0) { | 175 | if (bytes <= 0) { |
| 165 | return bytes; | 176 | return bytes; |
| 166 | } | 177 | } |
| 167 | 178 | ||
| 168 | if (debug) { | 179 | if (debug) { |
| 169 | fprintf(stderr, "dispatch_AFC_packet: sent the first now go with the second\n"); | 180 | fprintf(stderr, "dispatch_AFC_packet: sent the first now go with the second\n"); |
| 170 | fprintf(stderr, "Length: %i\n", length-offset); | 181 | fprintf(stderr, "Length: %i\n", length - offset); |
| 171 | fprintf(stderr, "Buffer: \n"); | 182 | fprintf(stderr, "Buffer: \n"); |
| 172 | fwrite(data+offset, 1, length-offset, stdout); | 183 | fwrite(data + offset, 1, length - offset, stdout); |
| 173 | } | 184 | } |
| 174 | 185 | ||
| 175 | iphone_mux_send(client->connection, data+offset, length-offset, &bytes); | 186 | iphone_mux_send(client->connection, data + offset, length - offset, &bytes); |
| 176 | return bytes; | 187 | return bytes; |
| 177 | } else { | 188 | } else { |
| 178 | if (debug) fprintf(stderr, "dispatch_AFC_packet doin things the old way\n"); | 189 | if (debug) |
| 179 | char *buffer = (char*)malloc(sizeof(char) * client->afc_packet->this_length); | 190 | fprintf(stderr, "dispatch_AFC_packet doin things the old way\n"); |
| 180 | if (debug) fprintf(stderr, "dispatch_AFC_packet packet length = %i\n", client->afc_packet->this_length); | 191 | char *buffer = (char *) malloc(sizeof(char) * client->afc_packet->this_length); |
| 181 | memcpy(buffer, (char*)client->afc_packet, sizeof(AFCPacket)); | 192 | if (debug) |
| 182 | if (debug) fprintf(stderr, "dispatch_AFC_packet packet data follows\n"); | 193 | fprintf(stderr, "dispatch_AFC_packet packet length = %i\n", client->afc_packet->this_length); |
| 183 | if (length > 0) { memcpy(buffer+sizeof(AFCPacket), data, length); buffer[sizeof(AFCPacket)+length] = '\0'; } | 194 | memcpy(buffer, (char *) client->afc_packet, sizeof(AFCPacket)); |
| 184 | if (debug) fwrite(buffer, 1, client->afc_packet->this_length, stdout); | 195 | if (debug) |
| 185 | if (debug) fprintf(stderr, "\n"); | 196 | fprintf(stderr, "dispatch_AFC_packet packet data follows\n"); |
| 197 | if (length > 0) { | ||
| 198 | memcpy(buffer + sizeof(AFCPacket), data, length); | ||
| 199 | buffer[sizeof(AFCPacket) + length] = '\0'; | ||
| 200 | } | ||
| 201 | if (debug) | ||
| 202 | fwrite(buffer, 1, client->afc_packet->this_length, stdout); | ||
| 203 | if (debug) | ||
| 204 | fprintf(stderr, "\n"); | ||
| 186 | iphone_mux_send(client->connection, buffer, client->afc_packet->this_length, &bytes); | 205 | iphone_mux_send(client->connection, buffer, client->afc_packet->this_length, &bytes); |
| 187 | 206 | ||
| 188 | if (buffer) { | 207 | if (buffer) { |
| @@ -205,13 +224,14 @@ static int dispatch_AFC_packet(iphone_afc_client_t client, const char *data, int | |||
| 205 | * AFC_ERROR operation) | 224 | * AFC_ERROR operation) |
| 206 | */ | 225 | */ |
| 207 | 226 | ||
| 208 | static int receive_AFC_data(iphone_afc_client_t client, char **dump_here) { | 227 | static int receive_AFC_data(iphone_afc_client_t client, char **dump_here) |
| 228 | { | ||
| 209 | AFCPacket *r_packet; | 229 | AFCPacket *r_packet; |
| 210 | char *buffer = (char*)malloc(sizeof(AFCPacket) * 4); | 230 | char *buffer = (char *) malloc(sizeof(AFCPacket) * 4); |
| 211 | char *final_buffer = NULL; | 231 | char *final_buffer = NULL; |
| 212 | int bytes = 0, recv_len = 0, current_count=0; | 232 | int bytes = 0, recv_len = 0, current_count = 0; |
| 213 | int retval = 0; | 233 | int retval = 0; |
| 214 | 234 | ||
| 215 | iphone_mux_recv(client->connection, buffer, sizeof(AFCPacket) * 4, &bytes); | 235 | iphone_mux_recv(client->connection, buffer, sizeof(AFCPacket) * 4, &bytes); |
| 216 | if (bytes <= 0) { | 236 | if (bytes <= 0) { |
| 217 | free(buffer); | 237 | free(buffer); |
| @@ -219,102 +239,119 @@ static int receive_AFC_data(iphone_afc_client_t client, char **dump_here) { | |||
| 219 | *dump_here = NULL; | 239 | *dump_here = NULL; |
| 220 | return -1; | 240 | return -1; |
| 221 | } | 241 | } |
| 222 | 242 | ||
| 223 | r_packet = (AFCPacket*)malloc(sizeof(AFCPacket)); | 243 | r_packet = (AFCPacket *) malloc(sizeof(AFCPacket)); |
| 224 | memcpy(r_packet, buffer, sizeof(AFCPacket)); | 244 | memcpy(r_packet, buffer, sizeof(AFCPacket)); |
| 225 | 245 | ||
| 226 | if (r_packet->entire_length == r_packet->this_length && r_packet->entire_length > sizeof(AFCPacket) && r_packet->operation != AFC_ERROR) { | 246 | if (r_packet->entire_length == r_packet->this_length |
| 227 | *dump_here = (char*)malloc(sizeof(char) * (r_packet->entire_length-sizeof(AFCPacket))); | 247 | && r_packet->entire_length > sizeof(AFCPacket) && r_packet->operation != AFC_ERROR) { |
| 228 | memcpy(*dump_here, buffer+sizeof(AFCPacket), r_packet->entire_length-sizeof(AFCPacket)); | 248 | *dump_here = (char *) malloc(sizeof(char) * (r_packet->entire_length - sizeof(AFCPacket))); |
| 229 | retval = r_packet->entire_length - sizeof(AFCPacket); | 249 | memcpy(*dump_here, buffer + sizeof(AFCPacket), r_packet->entire_length - sizeof(AFCPacket)); |
| 250 | retval = r_packet->entire_length - sizeof(AFCPacket); | ||
| 230 | free(buffer); | 251 | free(buffer); |
| 231 | free(r_packet); | 252 | free(r_packet); |
| 232 | return retval; | 253 | return retval; |
| 233 | } | 254 | } |
| 234 | 255 | ||
| 235 | uint32 param1 = buffer[sizeof(AFCPacket)]; | 256 | uint32 param1 = buffer[sizeof(AFCPacket)]; |
| 236 | free(buffer); | 257 | free(buffer); |
| 237 | 258 | ||
| 238 | if (r_packet->operation == AFC_ERROR && !(client->afc_packet->operation == AFC_DELETE && param1 == 7)) { | 259 | if (r_packet->operation == AFC_ERROR && !(client->afc_packet->operation == AFC_DELETE && param1 == 7)) { |
| 239 | if (debug) fprintf(stderr, "Oops? Bad operation code received: 0x%X, operation=0x%X, param1=%d\n", | 260 | if (debug) |
| 240 | r_packet->operation, client->afc_packet->operation, param1); | 261 | fprintf(stderr, |
| 262 | "Oops? Bad operation code received: 0x%X, operation=0x%X, param1=%d\n", | ||
| 263 | r_packet->operation, client->afc_packet->operation, param1); | ||
| 241 | recv_len = r_packet->entire_length - r_packet->this_length; | 264 | recv_len = r_packet->entire_length - r_packet->this_length; |
| 242 | free(r_packet); | 265 | free(r_packet); |
| 243 | if (debug) fprintf(stderr, "recv_len=%d\n", recv_len); | 266 | if (debug) |
| 244 | if(param1 == 0) { | 267 | fprintf(stderr, "recv_len=%d\n", recv_len); |
| 245 | if (debug) fprintf(stderr, "... false alarm, but still\n"); | 268 | if (param1 == 0) { |
| 269 | if (debug) | ||
| 270 | fprintf(stderr, "... false alarm, but still\n"); | ||
| 246 | *dump_here = NULL; | 271 | *dump_here = NULL; |
| 247 | return 0; | 272 | return 0; |
| 273 | } else { | ||
| 274 | if (debug) | ||
| 275 | fprintf(stderr, "Errno %i\n", param1); | ||
| 248 | } | 276 | } |
| 249 | else { if (debug) fprintf(stderr, "Errno %i\n", param1); } | ||
| 250 | *dump_here = NULL; | 277 | *dump_here = NULL; |
| 251 | return -1; | 278 | return -1; |
| 252 | } else { | 279 | } else { |
| 253 | if (debug) fprintf(stderr, "Operation code %x\nFull length %i and this length %i\n", r_packet->operation, r_packet->entire_length, r_packet->this_length); | 280 | if (debug) |
| 281 | fprintf(stderr, | ||
| 282 | "Operation code %x\nFull length %i and this length %i\n", | ||
| 283 | r_packet->operation, r_packet->entire_length, r_packet->this_length); | ||
| 254 | } | 284 | } |
| 255 | 285 | ||
| 256 | recv_len = r_packet->entire_length - r_packet->this_length; | 286 | recv_len = r_packet->entire_length - r_packet->this_length; |
| 257 | free(r_packet); | 287 | free(r_packet); |
| 258 | if (!recv_len && r_packet->operation == AFC_SUCCESS_RESPONSE) | 288 | if (!recv_len && r_packet->operation == AFC_SUCCESS_RESPONSE) { |
| 259 | { | ||
| 260 | *dump_here = NULL; | 289 | *dump_here = NULL; |
| 261 | return 0; | 290 | return 0; |
| 262 | } | 291 | } |
| 263 | |||
| 264 | // Keep collecting packets until we have received the entire file. | 292 | // Keep collecting packets until we have received the entire file. |
| 265 | buffer = (char*)malloc(sizeof(char) * (recv_len < MAXIMUM_PACKET_SIZE) ? recv_len : MAXIMUM_PACKET_SIZE); | 293 | buffer = (char *) malloc(sizeof(char) * (recv_len < MAXIMUM_PACKET_SIZE) ? recv_len : MAXIMUM_PACKET_SIZE); |
| 266 | final_buffer = (char*)malloc(sizeof(char) * recv_len); | 294 | final_buffer = (char *) malloc(sizeof(char) * recv_len); |
| 267 | while(current_count < recv_len){ | 295 | while (current_count < recv_len) { |
| 268 | iphone_mux_recv(client->connection, buffer, recv_len-current_count, &bytes); | 296 | iphone_mux_recv(client->connection, buffer, recv_len - current_count, &bytes); |
| 269 | if (debug) fprintf(stderr, "receive_AFC_data: still collecting packets\n"); | 297 | if (debug) |
| 270 | if (bytes < 0) | 298 | fprintf(stderr, "receive_AFC_data: still collecting packets\n"); |
| 271 | { | 299 | if (bytes < 0) { |
| 272 | if(debug) fprintf(stderr, "receive_AFC_data: mux_recv failed: %d\n", bytes); | 300 | if (debug) |
| 301 | fprintf(stderr, "receive_AFC_data: mux_recv failed: %d\n", bytes); | ||
| 273 | break; | 302 | break; |
| 274 | } | 303 | } |
| 275 | if (bytes > recv_len-current_count) | 304 | if (bytes > recv_len - current_count) { |
| 276 | { | 305 | if (debug) |
| 277 | if(debug) fprintf(stderr, "receive_AFC_data: mux_recv delivered too much data\n"); | 306 | fprintf(stderr, "receive_AFC_data: mux_recv delivered too much data\n"); |
| 278 | break; | 307 | break; |
| 279 | } | 308 | } |
| 280 | if (bytes > 7 && strstr(buffer, "CFA6LPAA")) { | 309 | if (bytes > 7 && strstr(buffer, "CFA6LPAA")) { |
| 281 | if (debug) fprintf(stderr, "receive_AFC_data: WARNING: there is AFC data in this packet at %ti\n", strstr(buffer, "CFA6LPAA") - buffer); | 310 | if (debug) |
| 282 | if (debug) fprintf(stderr, "receive_AFC_data: the total packet length is %i\n", bytes); | 311 | fprintf(stderr, |
| 312 | "receive_AFC_data: WARNING: there is AFC data in this packet at %ti\n", | ||
| 313 | strstr(buffer, "CFA6LPAA") - buffer); | ||
| 314 | if (debug) | ||
| 315 | fprintf(stderr, "receive_AFC_data: the total packet length is %i\n", bytes); | ||
| 283 | } | 316 | } |
| 284 | 317 | ||
| 285 | memcpy(final_buffer+current_count, buffer, bytes); | 318 | memcpy(final_buffer + current_count, buffer, bytes); |
| 286 | current_count += bytes; | 319 | current_count += bytes; |
| 287 | } | 320 | } |
| 288 | free(buffer); | 321 | free(buffer); |
| 289 | 322 | ||
| 290 | *dump_here = final_buffer; | 323 | *dump_here = final_buffer; |
| 291 | return current_count; | 324 | return current_count; |
| 292 | } | 325 | } |
| 293 | 326 | ||
| 294 | static int count_nullspaces(char *string, int number) { | 327 | static int count_nullspaces(char *string, int number) |
| 328 | { | ||
| 295 | int i = 0, nulls = 0; | 329 | int i = 0, nulls = 0; |
| 296 | 330 | ||
| 297 | for (i = 0; i < number; i++) { | 331 | for (i = 0; i < number; i++) { |
| 298 | if (string[i] == '\0') nulls++; | 332 | if (string[i] == '\0') |
| 333 | nulls++; | ||
| 299 | } | 334 | } |
| 300 | 335 | ||
| 301 | return nulls; | 336 | return nulls; |
| 302 | } | 337 | } |
| 303 | 338 | ||
| 304 | static char **make_strings_list(char *tokens, int true_length) { | 339 | static char **make_strings_list(char *tokens, int true_length) |
| 340 | { | ||
| 305 | int nulls = 0, i = 0, j = 0; | 341 | int nulls = 0, i = 0, j = 0; |
| 306 | char **list = NULL; | 342 | char **list = NULL; |
| 307 | 343 | ||
| 308 | if (!tokens || !true_length) return NULL; | 344 | if (!tokens || !true_length) |
| 309 | 345 | return NULL; | |
| 346 | |||
| 310 | nulls = count_nullspaces(tokens, true_length); | 347 | nulls = count_nullspaces(tokens, true_length); |
| 311 | list = (char**)malloc(sizeof(char*) * (nulls + 1)); | 348 | list = (char **) malloc(sizeof(char *) * (nulls + 1)); |
| 312 | for (i = 0; i < nulls; i++) { | 349 | for (i = 0; i < nulls; i++) { |
| 313 | list[i] = strdup(tokens+j); | 350 | list[i] = strdup(tokens + j); |
| 314 | j += strlen(list[i]) + 1; | 351 | j += strlen(list[i]) + 1; |
| 315 | } | 352 | } |
| 316 | list[i] = NULL; | 353 | list[i] = NULL; |
| 317 | 354 | ||
| 318 | return list; | 355 | return list; |
| 319 | } | 356 | } |
| 320 | 357 | ||
| @@ -326,15 +363,17 @@ static char **make_strings_list(char *tokens, int true_length) { | |||
| 326 | * @return A char ** list of files in that directory, terminated by an empty | 363 | * @return A char ** list of files in that directory, terminated by an empty |
| 327 | * string for now or NULL if there was an error. | 364 | * string for now or NULL if there was an error. |
| 328 | */ | 365 | */ |
| 329 | iphone_error_t iphone_afc_get_dir_list ( iphone_afc_client_t client, const char *dir, char ***list) { | 366 | iphone_error_t iphone_afc_get_dir_list(iphone_afc_client_t client, const char *dir, char ***list) |
| 367 | { | ||
| 330 | int bytes = 0; | 368 | int bytes = 0; |
| 331 | char *data = NULL, **list_loc = NULL; | 369 | char *data = NULL, **list_loc = NULL; |
| 332 | iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; | 370 | iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; |
| 333 | 371 | ||
| 334 | if (!client || !dir || !list || (list && *list)) return IPHONE_E_INVALID_ARG; | 372 | if (!client || !dir || !list || (list && *list)) |
| 373 | return IPHONE_E_INVALID_ARG; | ||
| 335 | 374 | ||
| 336 | afc_lock(client); | 375 | afc_lock(client); |
| 337 | 376 | ||
| 338 | // Send the command | 377 | // Send the command |
| 339 | client->afc_packet->operation = AFC_LIST_DIR; | 378 | client->afc_packet->operation = AFC_LIST_DIR; |
| 340 | client->afc_packet->entire_length = 0; | 379 | client->afc_packet->entire_length = 0; |
| @@ -344,22 +383,22 @@ iphone_error_t iphone_afc_get_dir_list ( iphone_afc_client_t client, const char | |||
| 344 | afc_unlock(client); | 383 | afc_unlock(client); |
| 345 | return IPHONE_E_NOT_ENOUGH_DATA; | 384 | return IPHONE_E_NOT_ENOUGH_DATA; |
| 346 | } | 385 | } |
| 347 | |||
| 348 | // Receive the data | 386 | // Receive the data |
| 349 | bytes = receive_AFC_data(client, &data); | 387 | bytes = receive_AFC_data(client, &data); |
| 350 | if (bytes < 0 && !data) { | 388 | if (bytes < 0 && !data) { |
| 351 | afc_unlock(client); | 389 | afc_unlock(client); |
| 352 | return IPHONE_E_NOT_ENOUGH_DATA; | 390 | return IPHONE_E_NOT_ENOUGH_DATA; |
| 353 | } | 391 | } |
| 354 | |||
| 355 | // Parse the data | 392 | // Parse the data |
| 356 | list_loc = make_strings_list(data, bytes); | 393 | list_loc = make_strings_list(data, bytes); |
| 357 | if (list_loc) ret = IPHONE_E_SUCCESS; | 394 | if (list_loc) |
| 358 | if (data) free(data); | 395 | ret = IPHONE_E_SUCCESS; |
| 396 | if (data) | ||
| 397 | free(data); | ||
| 359 | 398 | ||
| 360 | afc_unlock(client); | 399 | afc_unlock(client); |
| 361 | *list = list_loc; | 400 | *list = list_loc; |
| 362 | 401 | ||
| 363 | return ret; | 402 | return ret; |
| 364 | } | 403 | } |
| 365 | 404 | ||
| @@ -370,14 +409,16 @@ iphone_error_t iphone_afc_get_dir_list ( iphone_afc_client_t client, const char | |||
| 370 | * @return A char ** list of parameters as given by AFC or NULL if there was an | 409 | * @return A char ** list of parameters as given by AFC or NULL if there was an |
| 371 | * error. | 410 | * error. |
| 372 | */ | 411 | */ |
| 373 | iphone_error_t iphone_afc_get_devinfo ( iphone_afc_client_t client, char ***infos) { | 412 | iphone_error_t iphone_afc_get_devinfo(iphone_afc_client_t client, char ***infos) |
| 413 | { | ||
| 374 | int bytes = 0; | 414 | int bytes = 0; |
| 375 | char *data = NULL, **list = NULL; | 415 | char *data = NULL, **list = NULL; |
| 376 | 416 | ||
| 377 | if (!client || !infos) return IPHONE_E_INVALID_ARG; | 417 | if (!client || !infos) |
| 418 | return IPHONE_E_INVALID_ARG; | ||
| 378 | 419 | ||
| 379 | afc_lock(client); | 420 | afc_lock(client); |
| 380 | 421 | ||
| 381 | // Send the command | 422 | // Send the command |
| 382 | client->afc_packet->operation = AFC_GET_DEVINFO; | 423 | client->afc_packet->operation = AFC_GET_DEVINFO; |
| 383 | client->afc_packet->entire_length = client->afc_packet->this_length = 0; | 424 | client->afc_packet->entire_length = client->afc_packet->this_length = 0; |
| @@ -386,18 +427,17 @@ iphone_error_t iphone_afc_get_devinfo ( iphone_afc_client_t client, char ***info | |||
| 386 | afc_unlock(client); | 427 | afc_unlock(client); |
| 387 | return IPHONE_E_NOT_ENOUGH_DATA; | 428 | return IPHONE_E_NOT_ENOUGH_DATA; |
| 388 | } | 429 | } |
| 389 | |||
| 390 | // Receive the data | 430 | // Receive the data |
| 391 | bytes = receive_AFC_data(client, &data); | 431 | bytes = receive_AFC_data(client, &data); |
| 392 | if (bytes < 0 && !data) { | 432 | if (bytes < 0 && !data) { |
| 393 | afc_unlock(client); | 433 | afc_unlock(client); |
| 394 | return IPHONE_E_NOT_ENOUGH_DATA; | 434 | return IPHONE_E_NOT_ENOUGH_DATA; |
| 395 | } | 435 | } |
| 396 | |||
| 397 | // Parse the data | 436 | // Parse the data |
| 398 | list = make_strings_list(data, bytes); | 437 | list = make_strings_list(data, bytes); |
| 399 | if (data) free(data); | 438 | if (data) |
| 400 | 439 | free(data); | |
| 440 | |||
| 401 | afc_unlock(client); | 441 | afc_unlock(client); |
| 402 | *infos = list; | 442 | *infos = list; |
| 403 | return IPHONE_E_SUCCESS; | 443 | return IPHONE_E_SUCCESS; |
| @@ -411,14 +451,16 @@ iphone_error_t iphone_afc_get_devinfo ( iphone_afc_client_t client, char ***info | |||
| 411 | * @return IPHONE_E_SUCCESS if everythong went well, IPHONE_E_INVALID_ARG | 451 | * @return IPHONE_E_SUCCESS if everythong went well, IPHONE_E_INVALID_ARG |
| 412 | * if arguments are NULL or invalid, IPHONE_E_NOT_ENOUGH_DATA otherwise. | 452 | * if arguments are NULL or invalid, IPHONE_E_NOT_ENOUGH_DATA otherwise. |
| 413 | */ | 453 | */ |
| 414 | iphone_error_t iphone_afc_delete_file ( iphone_afc_client_t client, const char *path) { | 454 | iphone_error_t iphone_afc_delete_file(iphone_afc_client_t client, const char *path) |
| 455 | { | ||
| 415 | char *response = NULL; | 456 | char *response = NULL; |
| 416 | int bytes; | 457 | int bytes; |
| 417 | 458 | ||
| 418 | if (!client || !path || !client->afc_packet || !client->connection) return IPHONE_E_INVALID_ARG; | 459 | if (!client || !path || !client->afc_packet || !client->connection) |
| 419 | 460 | return IPHONE_E_INVALID_ARG; | |
| 461 | |||
| 420 | afc_lock(client); | 462 | afc_lock(client); |
| 421 | 463 | ||
| 422 | // Send command | 464 | // Send command |
| 423 | client->afc_packet->this_length = client->afc_packet->entire_length = 0; | 465 | client->afc_packet->this_length = client->afc_packet->entire_length = 0; |
| 424 | client->afc_packet->operation = AFC_DELETE; | 466 | client->afc_packet->operation = AFC_DELETE; |
| @@ -427,13 +469,13 @@ iphone_error_t iphone_afc_delete_file ( iphone_afc_client_t client, const char * | |||
| 427 | afc_unlock(client); | 469 | afc_unlock(client); |
| 428 | return IPHONE_E_NOT_ENOUGH_DATA; | 470 | return IPHONE_E_NOT_ENOUGH_DATA; |
| 429 | } | 471 | } |
| 430 | |||
| 431 | // Receive response | 472 | // Receive response |
| 432 | bytes = receive_AFC_data(client, &response); | 473 | bytes = receive_AFC_data(client, &response); |
| 433 | if (response) free(response); | 474 | if (response) |
| 434 | 475 | free(response); | |
| 476 | |||
| 435 | afc_unlock(client); | 477 | afc_unlock(client); |
| 436 | 478 | ||
| 437 | if (bytes < 0) { | 479 | if (bytes < 0) { |
| 438 | return IPHONE_E_NOT_ENOUGH_DATA; | 480 | return IPHONE_E_NOT_ENOUGH_DATA; |
| 439 | } else { | 481 | } else { |
| @@ -450,18 +492,20 @@ iphone_error_t iphone_afc_delete_file ( iphone_afc_client_t client, const char * | |||
| 450 | * @return IPHONE_E_SUCCESS if everythong went well, IPHONE_E_INVALID_ARG | 492 | * @return IPHONE_E_SUCCESS if everythong went well, IPHONE_E_INVALID_ARG |
| 451 | * if arguments are NULL or invalid, IPHONE_E_NOT_ENOUGH_DATA otherwise. | 493 | * if arguments are NULL or invalid, IPHONE_E_NOT_ENOUGH_DATA otherwise. |
| 452 | */ | 494 | */ |
| 453 | iphone_error_t iphone_afc_rename_file ( iphone_afc_client_t client, const char *from, const char *to) { | 495 | iphone_error_t iphone_afc_rename_file(iphone_afc_client_t client, const char *from, const char *to) |
| 496 | { | ||
| 454 | char *response = NULL; | 497 | char *response = NULL; |
| 455 | char *send = (char*)malloc(sizeof(char) * (strlen(from) + strlen(to) + 1 + sizeof(uint32))); | 498 | char *send = (char *) malloc(sizeof(char) * (strlen(from) + strlen(to) + 1 + sizeof(uint32))); |
| 456 | int bytes = 0; | 499 | int bytes = 0; |
| 457 | 500 | ||
| 458 | if (!client || !from || !to || !client->afc_packet || !client->connection) return IPHONE_E_INVALID_ARG; | 501 | if (!client || !from || !to || !client->afc_packet || !client->connection) |
| 459 | 502 | return IPHONE_E_INVALID_ARG; | |
| 503 | |||
| 460 | afc_lock(client); | 504 | afc_lock(client); |
| 461 | 505 | ||
| 462 | // Send command | 506 | // Send command |
| 463 | memcpy(send, from, strlen(from)+1); | 507 | memcpy(send, from, strlen(from) + 1); |
| 464 | memcpy(send+strlen(from)+1, to, strlen(to)+1); | 508 | memcpy(send + strlen(from) + 1, to, strlen(to) + 1); |
| 465 | client->afc_packet->entire_length = client->afc_packet->this_length = 0; | 509 | client->afc_packet->entire_length = client->afc_packet->this_length = 0; |
| 466 | client->afc_packet->operation = AFC_RENAME; | 510 | client->afc_packet->operation = AFC_RENAME; |
| 467 | bytes = dispatch_AFC_packet(client, send, strlen(to) + strlen(from) + 2); | 511 | bytes = dispatch_AFC_packet(client, send, strlen(to) + strlen(from) + 2); |
| @@ -470,13 +514,13 @@ iphone_error_t iphone_afc_rename_file ( iphone_afc_client_t client, const char * | |||
| 470 | afc_unlock(client); | 514 | afc_unlock(client); |
| 471 | return IPHONE_E_NOT_ENOUGH_DATA; | 515 | return IPHONE_E_NOT_ENOUGH_DATA; |
| 472 | } | 516 | } |
| 473 | |||
| 474 | // Receive response | 517 | // Receive response |
| 475 | bytes = receive_AFC_data(client, &response); | 518 | bytes = receive_AFC_data(client, &response); |
| 476 | if (response) free(response); | 519 | if (response) |
| 520 | free(response); | ||
| 477 | 521 | ||
| 478 | afc_unlock(client); | 522 | afc_unlock(client); |
| 479 | 523 | ||
| 480 | if (bytes < 0) { | 524 | if (bytes < 0) { |
| 481 | return IPHONE_E_NOT_ENOUGH_DATA; | 525 | return IPHONE_E_NOT_ENOUGH_DATA; |
| 482 | } else { | 526 | } else { |
| @@ -493,14 +537,16 @@ iphone_error_t iphone_afc_rename_file ( iphone_afc_client_t client, const char * | |||
| 493 | * @return IPHONE_E_SUCCESS if everythong went well, IPHONE_E_INVALID_ARG | 537 | * @return IPHONE_E_SUCCESS if everythong went well, IPHONE_E_INVALID_ARG |
| 494 | * if arguments are NULL or invalid, IPHONE_E_NOT_ENOUGH_DATA otherwise. | 538 | * if arguments are NULL or invalid, IPHONE_E_NOT_ENOUGH_DATA otherwise. |
| 495 | */ | 539 | */ |
| 496 | iphone_error_t iphone_afc_mkdir ( iphone_afc_client_t client, const char *dir) { | 540 | iphone_error_t iphone_afc_mkdir(iphone_afc_client_t client, const char *dir) |
| 541 | { | ||
| 497 | int bytes = 0; | 542 | int bytes = 0; |
| 498 | char *response = NULL; | 543 | char *response = NULL; |
| 499 | 544 | ||
| 500 | if (!client) return IPHONE_E_INVALID_ARG; | 545 | if (!client) |
| 501 | 546 | return IPHONE_E_INVALID_ARG; | |
| 547 | |||
| 502 | afc_lock(client); | 548 | afc_lock(client); |
| 503 | 549 | ||
| 504 | // Send command | 550 | // Send command |
| 505 | client->afc_packet->operation = AFC_MAKE_DIR; | 551 | client->afc_packet->operation = AFC_MAKE_DIR; |
| 506 | client->afc_packet->this_length = client->afc_packet->entire_length = 0; | 552 | client->afc_packet->this_length = client->afc_packet->entire_length = 0; |
| @@ -509,13 +555,13 @@ iphone_error_t iphone_afc_mkdir ( iphone_afc_client_t client, const char *dir) { | |||
| 509 | afc_unlock(client); | 555 | afc_unlock(client); |
| 510 | return IPHONE_E_NOT_ENOUGH_DATA; | 556 | return IPHONE_E_NOT_ENOUGH_DATA; |
| 511 | } | 557 | } |
| 512 | |||
| 513 | // Receive response | 558 | // Receive response |
| 514 | bytes = receive_AFC_data(client, &response); | 559 | bytes = receive_AFC_data(client, &response); |
| 515 | if (response) free(response); | 560 | if (response) |
| 561 | free(response); | ||
| 516 | 562 | ||
| 517 | afc_unlock(client); | 563 | afc_unlock(client); |
| 518 | 564 | ||
| 519 | if (bytes < 0) { | 565 | if (bytes < 0) { |
| 520 | return IPHONE_E_NOT_ENOUGH_DATA; | 566 | return IPHONE_E_NOT_ENOUGH_DATA; |
| 521 | } else { | 567 | } else { |
| @@ -531,18 +577,19 @@ iphone_error_t iphone_afc_mkdir ( iphone_afc_client_t client, const char *dir) { | |||
| 531 | * @return A pointer to an AFCFile struct containing the information received, | 577 | * @return A pointer to an AFCFile struct containing the information received, |
| 532 | * or NULL on failure. | 578 | * or NULL on failure. |
| 533 | */ | 579 | */ |
| 534 | iphone_afc_file_t afc_get_file_info(iphone_afc_client_t client, const char *path) { | 580 | iphone_afc_file_t afc_get_file_info(iphone_afc_client_t client, const char *path) |
| 581 | { | ||
| 535 | char *received, **list; | 582 | char *received, **list; |
| 536 | iphone_afc_file_t my_file; | 583 | iphone_afc_file_t my_file; |
| 537 | int length, i = 0; | 584 | int length, i = 0; |
| 538 | 585 | ||
| 539 | afc_lock(client); | 586 | afc_lock(client); |
| 540 | 587 | ||
| 541 | // Send command | 588 | // Send command |
| 542 | client->afc_packet->operation = AFC_GET_INFO; | 589 | client->afc_packet->operation = AFC_GET_INFO; |
| 543 | client->afc_packet->entire_length = client->afc_packet->this_length = 0; | 590 | client->afc_packet->entire_length = client->afc_packet->this_length = 0; |
| 544 | dispatch_AFC_packet(client, path, strlen(path)); | 591 | dispatch_AFC_packet(client, path, strlen(path)); |
| 545 | 592 | ||
| 546 | // Receive data | 593 | // Receive data |
| 547 | length = receive_AFC_data(client, &received); | 594 | length = receive_AFC_data(client, &received); |
| 548 | if (received) { | 595 | if (received) { |
| @@ -554,23 +601,23 @@ iphone_afc_file_t afc_get_file_info(iphone_afc_client_t client, const char *path | |||
| 554 | } | 601 | } |
| 555 | 602 | ||
| 556 | afc_unlock(client); | 603 | afc_unlock(client); |
| 557 | 604 | ||
| 558 | // Parse the data | 605 | // Parse the data |
| 559 | if (list) { | 606 | if (list) { |
| 560 | my_file = (iphone_afc_file_t)malloc(sizeof(struct iphone_afc_file_int)); | 607 | my_file = (iphone_afc_file_t) malloc(sizeof(struct iphone_afc_file_int)); |
| 561 | for (i = 0; list[i]; i++) { | 608 | for (i = 0; list[i]; i++) { |
| 562 | if (!strcmp(list[i], "st_size")) { | 609 | if (!strcmp(list[i], "st_size")) { |
| 563 | my_file->size = atoi(list[i+1]); | 610 | my_file->size = atoi(list[i + 1]); |
| 564 | } | 611 | } |
| 565 | 612 | ||
| 566 | if (!strcmp(list[i], "st_blocks")) { | 613 | if (!strcmp(list[i], "st_blocks")) { |
| 567 | my_file->blocks = atoi(list[i+1]); | 614 | my_file->blocks = atoi(list[i + 1]); |
| 568 | } | 615 | } |
| 569 | 616 | ||
| 570 | if (!strcmp(list[i], "st_ifmt")) { | 617 | if (!strcmp(list[i], "st_ifmt")) { |
| 571 | if (!strcmp(list[i+1], "S_IFREG")) { | 618 | if (!strcmp(list[i + 1], "S_IFREG")) { |
| 572 | my_file->type = S_IFREG; | 619 | my_file->type = S_IFREG; |
| 573 | } else if (!strcmp(list[i+1], "S_IFDIR")) { | 620 | } else if (!strcmp(list[i + 1], "S_IFDIR")) { |
| 574 | my_file->type = S_IFDIR; | 621 | my_file->type = S_IFDIR; |
| 575 | } | 622 | } |
| 576 | } | 623 | } |
| @@ -591,24 +638,27 @@ iphone_afc_file_t afc_get_file_info(iphone_afc_client_t client, const char *path | |||
| 591 | * @return A pointer to an AFCFile struct containing the information received, | 638 | * @return A pointer to an AFCFile struct containing the information received, |
| 592 | * or NULL on failure. | 639 | * or NULL on failure. |
| 593 | */ | 640 | */ |
| 594 | iphone_error_t iphone_afc_get_file_attr ( iphone_afc_client_t client, const char *filename, struct stat *stbuf ) { | 641 | iphone_error_t iphone_afc_get_file_attr(iphone_afc_client_t client, const char *filename, struct stat * stbuf) |
| 642 | { | ||
| 595 | 643 | ||
| 596 | iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; | 644 | iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; |
| 597 | if (!client ||!client->connection || !client->afc_packet || !stbuf) return IPHONE_E_INVALID_ARG; | 645 | if (!client || !client->connection || !client->afc_packet || !stbuf) |
| 646 | return IPHONE_E_INVALID_ARG; | ||
| 598 | 647 | ||
| 599 | memset(stbuf, 0, sizeof(struct stat)); | 648 | memset(stbuf, 0, sizeof(struct stat)); |
| 600 | iphone_afc_file_t file = afc_get_file_info(client, filename); | 649 | iphone_afc_file_t file = afc_get_file_info(client, filename); |
| 601 | if (!file){ | 650 | if (!file) { |
| 602 | ret = IPHONE_E_NO_SUCH_FILE; | 651 | ret = IPHONE_E_NO_SUCH_FILE; |
| 603 | } else { | 652 | } else { |
| 604 | stbuf->st_mode = file->type | (S_ISDIR(file->type) ? 0755 : 0644); | 653 | stbuf->st_mode = file->type | (S_ISDIR(file->type) ? 0755 : 0644); |
| 605 | stbuf->st_size = file->size; | 654 | stbuf->st_size = file->size; |
| 606 | stbuf->st_blksize = 2048; // FIXME: Is this the actual block size used on the iPhone? | 655 | stbuf->st_blksize = 2048; // FIXME: Is this the actual block |
| 656 | // size used on the iPhone? | ||
| 607 | stbuf->st_blocks = file->blocks; | 657 | stbuf->st_blocks = file->blocks; |
| 608 | stbuf->st_uid = getuid(); | 658 | stbuf->st_uid = getuid(); |
| 609 | stbuf->st_gid = getgid(); | 659 | stbuf->st_gid = getgid(); |
| 610 | 660 | ||
| 611 | ret = iphone_afc_close_file(client,file); | 661 | ret = iphone_afc_close_file(client, file); |
| 612 | } | 662 | } |
| 613 | return ret; | 663 | return ret; |
| 614 | } | 664 | } |
| @@ -626,32 +676,36 @@ iphone_error_t iphone_afc_get_file_attr ( iphone_afc_client_t client, const char | |||
| 626 | * received by afc_get_file_info) as well as the handle to the file or | 676 | * received by afc_get_file_info) as well as the handle to the file or |
| 627 | * NULL in the case of failure. | 677 | * NULL in the case of failure. |
| 628 | */ | 678 | */ |
| 629 | iphone_error_t iphone_afc_open_file ( iphone_afc_client_t client, const char *filename, iphone_afc_file_mode_t file_mode, iphone_afc_file_t *file ) { | 679 | iphone_error_t |
| 680 | iphone_afc_open_file(iphone_afc_client_t client, const char *filename, | ||
| 681 | iphone_afc_file_mode_t file_mode, iphone_afc_file_t * file) | ||
| 682 | { | ||
| 630 | iphone_afc_file_t file_loc = NULL; | 683 | iphone_afc_file_t file_loc = NULL; |
| 631 | uint32 ag = 0; | 684 | uint32 ag = 0; |
| 632 | int bytes = 0, length = 0; | 685 | int bytes = 0, length = 0; |
| 633 | char *data = (char*)malloc(sizeof(char) * (8 + strlen(filename) + 1)); | 686 | char *data = (char *) malloc(sizeof(char) * (8 + strlen(filename) + 1)); |
| 634 | 687 | ||
| 635 | if (!client ||!client->connection || !client->afc_packet) return IPHONE_E_INVALID_ARG; | 688 | if (!client || !client->connection || !client->afc_packet) |
| 636 | 689 | return IPHONE_E_INVALID_ARG; | |
| 690 | |||
| 637 | afc_lock(client); | 691 | afc_lock(client); |
| 638 | 692 | ||
| 639 | // Send command | 693 | // Send command |
| 640 | memcpy(data, &file_mode, 4); | 694 | memcpy(data, &file_mode, 4); |
| 641 | memcpy(data+4, &ag, 4); | 695 | memcpy(data + 4, &ag, 4); |
| 642 | memcpy(data+8, filename, strlen(filename)); | 696 | memcpy(data + 8, filename, strlen(filename)); |
| 643 | data[8+strlen(filename)] = '\0'; | 697 | data[8 + strlen(filename)] = '\0'; |
| 644 | client->afc_packet->operation = AFC_FILE_OPEN; | 698 | client->afc_packet->operation = AFC_FILE_OPEN; |
| 645 | client->afc_packet->entire_length = client->afc_packet->this_length = 0; | 699 | client->afc_packet->entire_length = client->afc_packet->this_length = 0; |
| 646 | bytes = dispatch_AFC_packet(client, data, 8+strlen(filename)); | 700 | bytes = dispatch_AFC_packet(client, data, 8 + strlen(filename)); |
| 647 | free(data); | 701 | free(data); |
| 648 | 702 | ||
| 649 | if (bytes <= 0) { | 703 | if (bytes <= 0) { |
| 650 | if (debug) fprintf(stderr, "afc_open_file: Didn't receive a response to the command\n"); | 704 | if (debug) |
| 705 | fprintf(stderr, "afc_open_file: Didn't receive a response to the command\n"); | ||
| 651 | afc_unlock(client); | 706 | afc_unlock(client); |
| 652 | return IPHONE_E_NOT_ENOUGH_DATA; | 707 | return IPHONE_E_NOT_ENOUGH_DATA; |
| 653 | } | 708 | } |
| 654 | |||
| 655 | // Receive the data | 709 | // Receive the data |
| 656 | length = receive_AFC_data(client, &data); | 710 | length = receive_AFC_data(client, &data); |
| 657 | if (length > 0 && data) { | 711 | if (length > 0 && data) { |
| @@ -664,13 +718,14 @@ iphone_error_t iphone_afc_open_file ( iphone_afc_client_t client, const char *fi | |||
| 664 | *file = file_loc; | 718 | *file = file_loc; |
| 665 | return IPHONE_E_SUCCESS; | 719 | return IPHONE_E_SUCCESS; |
| 666 | } else { | 720 | } else { |
| 667 | if (debug) fprintf(stderr, "afc_open_file: Didn't get any further data\n"); | 721 | if (debug) |
| 722 | fprintf(stderr, "afc_open_file: Didn't get any further data\n"); | ||
| 668 | afc_unlock(client); | 723 | afc_unlock(client); |
| 669 | return IPHONE_E_NOT_ENOUGH_DATA; | 724 | return IPHONE_E_NOT_ENOUGH_DATA; |
| 670 | } | 725 | } |
| 671 | 726 | ||
| 672 | afc_unlock(client); | 727 | afc_unlock(client); |
| 673 | 728 | ||
| 674 | return IPHONE_E_UNKNOWN_ERROR; | 729 | return IPHONE_E_UNKNOWN_ERROR; |
| 675 | } | 730 | } |
| 676 | 731 | ||
| @@ -683,59 +738,70 @@ iphone_error_t iphone_afc_open_file ( iphone_afc_client_t client, const char *fi | |||
| 683 | * | 738 | * |
| 684 | * @return The number of bytes read if successful. If there was an error -1. | 739 | * @return The number of bytes read if successful. If there was an error -1. |
| 685 | */ | 740 | */ |
| 686 | iphone_error_t iphone_afc_read_file ( iphone_afc_client_t client, iphone_afc_file_t file, char *data, int length, uint32_t *bytes) { | 741 | iphone_error_t |
| 742 | iphone_afc_read_file(iphone_afc_client_t client, iphone_afc_file_t file, char *data, int length, uint32_t * bytes) | ||
| 743 | { | ||
| 687 | char *input = NULL; | 744 | char *input = NULL; |
| 688 | int current_count = 0, bytes_loc = 0; | 745 | int current_count = 0, bytes_loc = 0; |
| 689 | const int MAXIMUM_READ_SIZE = 1 << 16; | 746 | const int MAXIMUM_READ_SIZE = 1 << 16; |
| 690 | 747 | ||
| 691 | if (!client || !client->afc_packet || !client->connection || !file) return IPHONE_E_INVALID_ARG; | 748 | if (!client || !client->afc_packet || !client->connection || !file) |
| 692 | if (debug) fprintf(stderr, "afc_read_file called for length %i\n", length); | 749 | return IPHONE_E_INVALID_ARG; |
| 750 | if (debug) | ||
| 751 | fprintf(stderr, "afc_read_file called for length %i\n", length); | ||
| 693 | 752 | ||
| 694 | afc_lock(client); | 753 | afc_lock(client); |
| 695 | 754 | ||
| 696 | // Looping here to get around the maximum amount of data that recieve_AFC_data can handle | 755 | // Looping here to get around the maximum amount of data that |
| 697 | while (current_count < length){ | 756 | // recieve_AFC_data can handle |
| 698 | if (debug) fprintf(stderr, "afc_read_file: current count is %i but length is %i\n", current_count, length); | 757 | while (current_count < length) { |
| 699 | 758 | if (debug) | |
| 759 | fprintf(stderr, "afc_read_file: current count is %i but length is %i\n", current_count, length); | ||
| 760 | |||
| 700 | // Send the read command | 761 | // Send the read command |
| 701 | AFCFilePacket *packet = (AFCFilePacket*)malloc(sizeof(AFCFilePacket)); | 762 | AFCFilePacket *packet = (AFCFilePacket *) malloc(sizeof(AFCFilePacket)); |
| 702 | packet->unknown1 = packet->unknown2 = 0; | 763 | packet->unknown1 = packet->unknown2 = 0; |
| 703 | packet->filehandle = file->filehandle; | 764 | packet->filehandle = file->filehandle; |
| 704 | packet->size = ((length - current_count) < MAXIMUM_READ_SIZE) ? (length - current_count) : MAXIMUM_READ_SIZE; | 765 | packet->size = ((length - current_count) < MAXIMUM_READ_SIZE) ? (length - current_count) : MAXIMUM_READ_SIZE; |
| 705 | client->afc_packet->operation = AFC_READ; | 766 | client->afc_packet->operation = AFC_READ; |
| 706 | client->afc_packet->entire_length = client->afc_packet->this_length = 0; | 767 | client->afc_packet->entire_length = client->afc_packet->this_length = 0; |
| 707 | bytes_loc = dispatch_AFC_packet(client, (char*)packet, sizeof(AFCFilePacket)); | 768 | bytes_loc = dispatch_AFC_packet(client, (char *) packet, sizeof(AFCFilePacket)); |
| 708 | free(packet); | 769 | free(packet); |
| 709 | 770 | ||
| 710 | if (bytes_loc <= 0) { | 771 | if (bytes_loc <= 0) { |
| 711 | afc_unlock(client); | 772 | afc_unlock(client); |
| 712 | return IPHONE_E_NOT_ENOUGH_DATA; | 773 | return IPHONE_E_NOT_ENOUGH_DATA; |
| 713 | } | 774 | } |
| 714 | |||
| 715 | // Receive the data | 775 | // Receive the data |
| 716 | bytes_loc = receive_AFC_data(client, &input); | 776 | bytes_loc = receive_AFC_data(client, &input); |
| 717 | if (debug) fprintf(stderr, "afc_read_file: bytes returned: %i\n", bytes_loc); | 777 | if (debug) |
| 778 | fprintf(stderr, "afc_read_file: bytes returned: %i\n", bytes_loc); | ||
| 718 | if (bytes_loc < 0) { | 779 | if (bytes_loc < 0) { |
| 719 | if (input) free(input); | 780 | if (input) |
| 781 | free(input); | ||
| 720 | afc_unlock(client); | 782 | afc_unlock(client); |
| 721 | return IPHONE_E_NOT_ENOUGH_DATA; | 783 | return IPHONE_E_NOT_ENOUGH_DATA; |
| 722 | } else if (bytes_loc == 0) { | 784 | } else if (bytes_loc == 0) { |
| 723 | if (input) free(input); | 785 | if (input) |
| 786 | free(input); | ||
| 724 | afc_unlock(client); | 787 | afc_unlock(client); |
| 725 | *bytes = current_count; | 788 | *bytes = current_count; |
| 726 | return IPHONE_E_SUCCESS; //FIXME check that's actually a success | 789 | return IPHONE_E_SUCCESS; // FIXME check that's actually a |
| 790 | // success | ||
| 727 | } else { | 791 | } else { |
| 728 | if (input) { | 792 | if (input) { |
| 729 | if (debug) fprintf(stderr, "afc_read_file: %d\n", bytes_loc); | 793 | if (debug) |
| 730 | memcpy(data+current_count, input, (bytes_loc > length) ? length : bytes_loc); | 794 | fprintf(stderr, "afc_read_file: %d\n", bytes_loc); |
| 795 | memcpy(data + current_count, input, (bytes_loc > length) ? length : bytes_loc); | ||
| 731 | free(input); | 796 | free(input); |
| 732 | input = NULL; | 797 | input = NULL; |
| 733 | current_count += (bytes_loc > length) ? length : bytes_loc; | 798 | current_count += (bytes_loc > length) ? length : bytes_loc; |
| 734 | } | 799 | } |
| 735 | } | 800 | } |
| 736 | } | 801 | } |
| 737 | if (debug) fprintf(stderr, "afc_read_file: returning current_count as %i\n", current_count); | 802 | if (debug) |
| 738 | 803 | fprintf(stderr, "afc_read_file: returning current_count as %i\n", current_count); | |
| 804 | |||
| 739 | afc_unlock(client); | 805 | afc_unlock(client); |
| 740 | *bytes = current_count; | 806 | *bytes = current_count; |
| 741 | return IPHONE_E_SUCCESS; | 807 | return IPHONE_E_SUCCESS; |
| @@ -751,17 +817,22 @@ iphone_error_t iphone_afc_read_file ( iphone_afc_client_t client, iphone_afc_fil | |||
| 751 | * @return The number of bytes written to the file, or a value less than 0 if | 817 | * @return The number of bytes written to the file, or a value less than 0 if |
| 752 | * none were written... | 818 | * none were written... |
| 753 | */ | 819 | */ |
| 754 | iphone_error_t iphone_afc_write_file ( iphone_afc_client_t client, iphone_afc_file_t file, const char *data, int length, uint32_t *bytes) { | 820 | iphone_error_t |
| 821 | iphone_afc_write_file(iphone_afc_client_t client, iphone_afc_file_t file, | ||
| 822 | const char *data, int length, uint32_t * bytes) | ||
| 823 | { | ||
| 755 | char *acknowledgement = NULL; | 824 | char *acknowledgement = NULL; |
| 756 | const int MAXIMUM_WRITE_SIZE = 1 << 16; | 825 | const int MAXIMUM_WRITE_SIZE = 1 << 16; |
| 757 | uint32 zero = 0, bytes_loc = 0, segments = (length / MAXIMUM_WRITE_SIZE), current_count = 0, i = 0; | 826 | uint32 zero = 0, bytes_loc = 0, segments = (length / MAXIMUM_WRITE_SIZE), current_count = 0, i = 0; |
| 758 | char *out_buffer = NULL; | 827 | char *out_buffer = NULL; |
| 759 | 828 | ||
| 760 | if (!client ||!client->afc_packet || !client->connection || !file || !bytes) return IPHONE_E_INVALID_ARG; | 829 | if (!client || !client->afc_packet || !client->connection || !file || !bytes) |
| 761 | 830 | return IPHONE_E_INVALID_ARG; | |
| 831 | |||
| 762 | afc_lock(client); | 832 | afc_lock(client); |
| 763 | 833 | ||
| 764 | if (debug) fprintf(stderr, "afc_write_file: Write length: %i\n", length); | 834 | if (debug) |
| 835 | fprintf(stderr, "afc_write_file: Write length: %i\n", length); | ||
| 765 | 836 | ||
| 766 | // Divide the file into segments. | 837 | // Divide the file into segments. |
| 767 | for (i = 0; i < segments; i++) { | 838 | for (i = 0; i < segments; i++) { |
| @@ -769,10 +840,10 @@ iphone_error_t iphone_afc_write_file ( iphone_afc_client_t client, iphone_afc_fi | |||
| 769 | client->afc_packet->this_length = sizeof(AFCPacket) + 8; | 840 | client->afc_packet->this_length = sizeof(AFCPacket) + 8; |
| 770 | client->afc_packet->entire_length = client->afc_packet->this_length + MAXIMUM_WRITE_SIZE; | 841 | client->afc_packet->entire_length = client->afc_packet->this_length + MAXIMUM_WRITE_SIZE; |
| 771 | client->afc_packet->operation = AFC_WRITE; | 842 | client->afc_packet->operation = AFC_WRITE; |
| 772 | out_buffer = (char*)malloc(sizeof(char) * client->afc_packet->entire_length - sizeof(AFCPacket)); | 843 | out_buffer = (char *) malloc(sizeof(char) * client->afc_packet->entire_length - sizeof(AFCPacket)); |
| 773 | memcpy(out_buffer, (char*)&file->filehandle, sizeof(uint32)); | 844 | memcpy(out_buffer, (char *) &file->filehandle, sizeof(uint32)); |
| 774 | memcpy(out_buffer+4, (char*)&zero, sizeof(uint32)); | 845 | memcpy(out_buffer + 4, (char *) &zero, sizeof(uint32)); |
| 775 | memcpy(out_buffer+8, data+current_count, MAXIMUM_WRITE_SIZE); | 846 | memcpy(out_buffer + 8, data + current_count, MAXIMUM_WRITE_SIZE); |
| 776 | bytes_loc = dispatch_AFC_packet(client, out_buffer, MAXIMUM_WRITE_SIZE + 8); | 847 | bytes_loc = dispatch_AFC_packet(client, out_buffer, MAXIMUM_WRITE_SIZE + 8); |
| 777 | if (bytes_loc < 0) { | 848 | if (bytes_loc < 0) { |
| 778 | afc_unlock(client); | 849 | afc_unlock(client); |
| @@ -788,39 +859,42 @@ iphone_error_t iphone_afc_write_file ( iphone_afc_client_t client, iphone_afc_fi | |||
| 788 | return IPHONE_E_NOT_ENOUGH_DATA; | 859 | return IPHONE_E_NOT_ENOUGH_DATA; |
| 789 | } | 860 | } |
| 790 | } | 861 | } |
| 791 | 862 | ||
| 792 | // By this point, we should be at the end. i.e. the last segment that didn't get sent in the for loop | 863 | // By this point, we should be at the end. i.e. the last segment that |
| 793 | // this length is fine because it's always sizeof(AFCPacket) + 8, but to be sure we do it again | 864 | // didn't get sent in the for loop |
| 865 | // this length is fine because it's always sizeof(AFCPacket) + 8, but | ||
| 866 | // to be sure we do it again | ||
| 794 | if (current_count == length) { | 867 | if (current_count == length) { |
| 795 | afc_unlock(client); | 868 | afc_unlock(client); |
| 796 | *bytes = current_count; | 869 | *bytes = current_count; |
| 797 | return IPHONE_E_SUCCESS; | 870 | return IPHONE_E_SUCCESS; |
| 798 | } | 871 | } |
| 799 | 872 | ||
| 800 | client->afc_packet->this_length = sizeof(AFCPacket) + 8; | 873 | client->afc_packet->this_length = sizeof(AFCPacket) + 8; |
| 801 | client->afc_packet->entire_length = client->afc_packet->this_length + (length - current_count); | 874 | client->afc_packet->entire_length = client->afc_packet->this_length + (length - current_count); |
| 802 | client->afc_packet->operation = AFC_WRITE; | 875 | client->afc_packet->operation = AFC_WRITE; |
| 803 | out_buffer = (char*)malloc(sizeof(char) * client->afc_packet->entire_length - sizeof(AFCPacket)); | 876 | out_buffer = (char *) malloc(sizeof(char) * client->afc_packet->entire_length - sizeof(AFCPacket)); |
| 804 | memcpy(out_buffer, (char*)&file->filehandle, sizeof(uint32)); | 877 | memcpy(out_buffer, (char *) &file->filehandle, sizeof(uint32)); |
| 805 | memcpy(out_buffer+4, (char*)&zero, sizeof(uint32)); | 878 | memcpy(out_buffer + 4, (char *) &zero, sizeof(uint32)); |
| 806 | memcpy(out_buffer+8, data+current_count, (length - current_count)); | 879 | memcpy(out_buffer + 8, data + current_count, (length - current_count)); |
| 807 | bytes_loc = dispatch_AFC_packet(client, out_buffer, (length - current_count) + 8); | 880 | bytes_loc = dispatch_AFC_packet(client, out_buffer, (length - current_count) + 8); |
| 808 | free(out_buffer); | 881 | free(out_buffer); |
| 809 | out_buffer = NULL; | 882 | out_buffer = NULL; |
| 810 | 883 | ||
| 811 | current_count += bytes_loc; | 884 | current_count += bytes_loc; |
| 812 | 885 | ||
| 813 | if (bytes_loc <= 0) { | 886 | if (bytes_loc <= 0) { |
| 814 | afc_unlock(client); | 887 | afc_unlock(client); |
| 815 | *bytes = current_count; | 888 | *bytes = current_count; |
| 816 | return IPHONE_E_SUCCESS; | 889 | return IPHONE_E_SUCCESS; |
| 817 | } | 890 | } |
| 818 | 891 | ||
| 819 | zero = bytes_loc; | 892 | zero = bytes_loc; |
| 820 | bytes_loc = receive_AFC_data(client, &acknowledgement); | 893 | bytes_loc = receive_AFC_data(client, &acknowledgement); |
| 821 | afc_unlock(client); | 894 | afc_unlock(client); |
| 822 | if (bytes_loc < 0) { | 895 | if (bytes_loc < 0) { |
| 823 | if (debug) fprintf(stderr, "afc_write_file: uh oh?\n"); | 896 | if (debug) |
| 897 | fprintf(stderr, "afc_write_file: uh oh?\n"); | ||
| 824 | } | 898 | } |
| 825 | *bytes = current_count; | 899 | *bytes = current_count; |
| 826 | return IPHONE_E_SUCCESS; | 900 | return IPHONE_E_SUCCESS; |
| @@ -832,19 +906,22 @@ iphone_error_t iphone_afc_write_file ( iphone_afc_client_t client, iphone_afc_fi | |||
| 832 | * @param file A pointer to an AFCFile struct containing the file handle of the | 906 | * @param file A pointer to an AFCFile struct containing the file handle of the |
| 833 | * file to close. | 907 | * file to close. |
| 834 | */ | 908 | */ |
| 835 | iphone_error_t iphone_afc_close_file ( iphone_afc_client_t client, iphone_afc_file_t file) { | 909 | iphone_error_t iphone_afc_close_file(iphone_afc_client_t client, iphone_afc_file_t file) |
| 836 | if (!client || !file) return IPHONE_E_INVALID_ARG; | 910 | { |
| 911 | if (!client || !file) | ||
| 912 | return IPHONE_E_INVALID_ARG; | ||
| 837 | char *buffer = malloc(sizeof(char) * 8); | 913 | char *buffer = malloc(sizeof(char) * 8); |
| 838 | uint32 zero = 0; | 914 | uint32 zero = 0; |
| 839 | int bytes = 0; | 915 | int bytes = 0; |
| 840 | 916 | ||
| 841 | afc_lock(client); | 917 | afc_lock(client); |
| 842 | 918 | ||
| 843 | if (debug) fprintf(stderr, "afc_close_file: File handle %i\n", file->filehandle); | 919 | if (debug) |
| 844 | 920 | fprintf(stderr, "afc_close_file: File handle %i\n", file->filehandle); | |
| 921 | |||
| 845 | // Send command | 922 | // Send command |
| 846 | memcpy(buffer, &file->filehandle, sizeof(uint32)); | 923 | memcpy(buffer, &file->filehandle, sizeof(uint32)); |
| 847 | memcpy(buffer+sizeof(uint32), &zero, sizeof(zero)); | 924 | memcpy(buffer + sizeof(uint32), &zero, sizeof(zero)); |
| 848 | client->afc_packet->operation = AFC_FILE_CLOSE; | 925 | client->afc_packet->operation = AFC_FILE_CLOSE; |
| 849 | client->afc_packet->entire_length = client->afc_packet->this_length = 0; | 926 | client->afc_packet->entire_length = client->afc_packet->this_length = 0; |
| 850 | bytes = dispatch_AFC_packet(client, buffer, sizeof(char) * 8); | 927 | bytes = dispatch_AFC_packet(client, buffer, sizeof(char) * 8); |
| @@ -852,16 +929,17 @@ iphone_error_t iphone_afc_close_file ( iphone_afc_client_t client, iphone_afc_fi | |||
| 852 | buffer = NULL; | 929 | buffer = NULL; |
| 853 | 930 | ||
| 854 | // FIXME: Is this necesary? | 931 | // FIXME: Is this necesary? |
| 855 | //client->afc_packet->entire_length = client->afc_packet->this_length = 0; | 932 | // client->afc_packet->entire_length = client->afc_packet->this_length |
| 856 | 933 | // = 0; | |
| 857 | if (bytes <= 0) { | 934 | |
| 858 | afc_unlock(client); | 935 | if (bytes <= 0) { |
| 936 | afc_unlock(client); | ||
| 859 | return IPHONE_E_UNKNOWN_ERROR; | 937 | return IPHONE_E_UNKNOWN_ERROR; |
| 860 | } | 938 | } |
| 861 | |||
| 862 | // Receive the response | 939 | // Receive the response |
| 863 | bytes = receive_AFC_data(client, &buffer); | 940 | bytes = receive_AFC_data(client, &buffer); |
| 864 | if (buffer) free(buffer); | 941 | if (buffer) |
| 942 | free(buffer); | ||
| 865 | free(file); | 943 | free(file); |
| 866 | afc_unlock(client); | 944 | afc_unlock(client); |
| 867 | return IPHONE_E_SUCCESS; | 945 | return IPHONE_E_SUCCESS; |
| @@ -876,39 +954,41 @@ iphone_error_t iphone_afc_close_file ( iphone_afc_client_t client, iphone_afc_fi | |||
| 876 | * | 954 | * |
| 877 | * @return IPHONE_E_SUCCESS on success, IPHONE_E_NOT_ENOUGH_DATA on failure. | 955 | * @return IPHONE_E_SUCCESS on success, IPHONE_E_NOT_ENOUGH_DATA on failure. |
| 878 | */ | 956 | */ |
| 879 | iphone_error_t iphone_afc_seek_file ( iphone_afc_client_t client, iphone_afc_file_t file, int seekpos) { | 957 | iphone_error_t iphone_afc_seek_file(iphone_afc_client_t client, iphone_afc_file_t file, int seekpos) |
| 880 | char *buffer = (char*)malloc(sizeof(char) * 24); | 958 | { |
| 959 | char *buffer = (char *) malloc(sizeof(char) * 24); | ||
| 881 | uint32 seekto = 0, bytes = 0, zero = 0; | 960 | uint32 seekto = 0, bytes = 0, zero = 0; |
| 882 | 961 | ||
| 883 | if (seekpos < 0) seekpos = file->size - abs(seekpos); | 962 | if (seekpos < 0) |
| 963 | seekpos = file->size - abs(seekpos); | ||
| 884 | 964 | ||
| 885 | afc_lock(client); | 965 | afc_lock(client); |
| 886 | 966 | ||
| 887 | // Send the command | 967 | // Send the command |
| 888 | seekto = seekpos; | 968 | seekto = seekpos; |
| 889 | memcpy(buffer, &file->filehandle, sizeof(uint32)); // handle | 969 | memcpy(buffer, &file->filehandle, sizeof(uint32)); // handle |
| 890 | memcpy(buffer+4, &zero, sizeof(uint32)); // pad | 970 | memcpy(buffer + 4, &zero, sizeof(uint32)); // pad |
| 891 | memcpy(buffer+8, &zero, sizeof(uint32)); // fromwhere | 971 | memcpy(buffer + 8, &zero, sizeof(uint32)); // fromwhere |
| 892 | memcpy(buffer+12, &zero, sizeof(uint32)); // pad | 972 | memcpy(buffer + 12, &zero, sizeof(uint32)); // pad |
| 893 | memcpy(buffer+16, &seekto, sizeof(uint32)); // offset | 973 | memcpy(buffer + 16, &seekto, sizeof(uint32)); // offset |
| 894 | memcpy(buffer+20, &zero, sizeof(uint32)); // pad | 974 | memcpy(buffer + 20, &zero, sizeof(uint32)); // pad |
| 895 | client->afc_packet->operation = AFC_FILE_SEEK; | 975 | client->afc_packet->operation = AFC_FILE_SEEK; |
| 896 | client->afc_packet->this_length = client->afc_packet->entire_length = 0; | 976 | client->afc_packet->this_length = client->afc_packet->entire_length = 0; |
| 897 | bytes = dispatch_AFC_packet(client, buffer, 23); | 977 | bytes = dispatch_AFC_packet(client, buffer, 23); |
| 898 | free(buffer); | 978 | free(buffer); |
| 899 | buffer = NULL; | 979 | buffer = NULL; |
| 900 | 980 | ||
| 901 | if (bytes <= 0) { | 981 | if (bytes <= 0) { |
| 902 | afc_unlock(client); | 982 | afc_unlock(client); |
| 903 | return IPHONE_E_NOT_ENOUGH_DATA; | 983 | return IPHONE_E_NOT_ENOUGH_DATA; |
| 904 | } | 984 | } |
| 905 | |||
| 906 | // Receive response | 985 | // Receive response |
| 907 | bytes = receive_AFC_data(client, &buffer); | 986 | bytes = receive_AFC_data(client, &buffer); |
| 908 | if (buffer) free(buffer); | 987 | if (buffer) |
| 909 | 988 | free(buffer); | |
| 989 | |||
| 910 | afc_unlock(client); | 990 | afc_unlock(client); |
| 911 | 991 | ||
| 912 | if (bytes >= 0) { | 992 | if (bytes >= 0) { |
| 913 | return IPHONE_E_SUCCESS; | 993 | return IPHONE_E_SUCCESS; |
| 914 | } else { | 994 | } else { |
| @@ -927,17 +1007,18 @@ iphone_error_t iphone_afc_seek_file ( iphone_afc_client_t client, iphone_afc_fil | |||
| 927 | * @note This function is more akin to ftruncate than truncate, and truncate | 1007 | * @note This function is more akin to ftruncate than truncate, and truncate |
| 928 | * calls would have to open the file before calling this, sadly. | 1008 | * calls would have to open the file before calling this, sadly. |
| 929 | */ | 1009 | */ |
| 930 | iphone_error_t iphone_afc_truncate_file ( iphone_afc_client_t client, iphone_afc_file_t file, uint32_t newsize) { | 1010 | iphone_error_t iphone_afc_truncate_file(iphone_afc_client_t client, iphone_afc_file_t file, uint32_t newsize) |
| 931 | char *buffer = (char*)malloc(sizeof(char) * 16); | 1011 | { |
| 1012 | char *buffer = (char *) malloc(sizeof(char) * 16); | ||
| 932 | uint32 bytes = 0, zero = 0; | 1013 | uint32 bytes = 0, zero = 0; |
| 933 | 1014 | ||
| 934 | afc_lock(client); | 1015 | afc_lock(client); |
| 935 | 1016 | ||
| 936 | // Send command | 1017 | // Send command |
| 937 | memcpy(buffer, &file->filehandle, sizeof(uint32)); // handle | 1018 | memcpy(buffer, &file->filehandle, sizeof(uint32)); // handle |
| 938 | memcpy(buffer+4, &zero, sizeof(uint32)); // pad | 1019 | memcpy(buffer + 4, &zero, sizeof(uint32)); // pad |
| 939 | memcpy(buffer+8, &newsize, sizeof(uint32)); // newsize | 1020 | memcpy(buffer + 8, &newsize, sizeof(uint32)); // newsize |
| 940 | memcpy(buffer+12, &zero, 3); // pad | 1021 | memcpy(buffer + 12, &zero, 3); // pad |
| 941 | client->afc_packet->operation = AFC_FILE_TRUNCATE; | 1022 | client->afc_packet->operation = AFC_FILE_TRUNCATE; |
| 942 | client->afc_packet->this_length = client->afc_packet->entire_length = 0; | 1023 | client->afc_packet->this_length = client->afc_packet->entire_length = 0; |
| 943 | bytes = dispatch_AFC_packet(client, buffer, 15); | 1024 | bytes = dispatch_AFC_packet(client, buffer, 15); |
| @@ -948,13 +1029,13 @@ iphone_error_t iphone_afc_truncate_file ( iphone_afc_client_t client, iphone_afc | |||
| 948 | afc_unlock(client); | 1029 | afc_unlock(client); |
| 949 | return IPHONE_E_NOT_ENOUGH_DATA; | 1030 | return IPHONE_E_NOT_ENOUGH_DATA; |
| 950 | } | 1031 | } |
| 951 | |||
| 952 | // Receive response | 1032 | // Receive response |
| 953 | bytes = receive_AFC_data(client, &buffer); | 1033 | bytes = receive_AFC_data(client, &buffer); |
| 954 | if (buffer) free(buffer); | 1034 | if (buffer) |
| 955 | 1035 | free(buffer); | |
| 1036 | |||
| 956 | afc_unlock(client); | 1037 | afc_unlock(client); |
| 957 | 1038 | ||
| 958 | if (bytes >= 0) { | 1039 | if (bytes >= 0) { |
| 959 | return IPHONE_E_SUCCESS; | 1040 | return IPHONE_E_SUCCESS; |
| 960 | } else { | 1041 | } else { |
| @@ -71,4 +71,3 @@ enum { | |||
| 71 | AFC_READ = 0x0000000f, | 71 | AFC_READ = 0x0000000f, |
| 72 | AFC_WRITE = 0x00000010 | 72 | AFC_WRITE = 0x00000010 |
| 73 | }; | 73 | }; |
| 74 | |||
diff --git a/src/ifuse.c b/src/ifuse.c index c266879..ad34eb5 100644 --- a/src/ifuse.c +++ b/src/ifuse.c | |||
| @@ -42,7 +42,8 @@ iphone_lckd_client_t control = NULL; | |||
| 42 | 42 | ||
| 43 | int debug = 0; | 43 | int debug = 0; |
| 44 | 44 | ||
| 45 | static int ifuse_getattr(const char *path, struct stat *stbuf) { | 45 | static int ifuse_getattr(const char *path, struct stat *stbuf) |
| 46 | { | ||
| 46 | int res = 0; | 47 | int res = 0; |
| 47 | 48 | ||
| 48 | iphone_afc_client_t afc = fuse_get_context()->private_data; | 49 | iphone_afc_client_t afc = fuse_get_context()->private_data; |
| @@ -54,31 +55,32 @@ static int ifuse_getattr(const char *path, struct stat *stbuf) { | |||
| 54 | return res; | 55 | return res; |
| 55 | } | 56 | } |
| 56 | 57 | ||
| 57 | static int ifuse_readdir(const char *path, void *buf, fuse_fill_dir_t filler, | 58 | static int ifuse_readdir(const char *path, void *buf, fuse_fill_dir_t filler, off_t offset, struct fuse_file_info *fi) |
| 58 | off_t offset, struct fuse_file_info *fi) { | 59 | { |
| 59 | int i; | 60 | int i; |
| 60 | char **dirs = NULL; | 61 | char **dirs = NULL; |
| 61 | iphone_afc_client_t afc = fuse_get_context()->private_data; | 62 | iphone_afc_client_t afc = fuse_get_context()->private_data; |
| 62 | 63 | ||
| 63 | iphone_afc_get_dir_list(afc, path, &dirs); | 64 | iphone_afc_get_dir_list(afc, path, &dirs); |
| 64 | 65 | ||
| 65 | if(!dirs) | 66 | if (!dirs) |
| 66 | return -ENOENT; | 67 | return -ENOENT; |
| 67 | 68 | ||
| 68 | for (i = 0; dirs[i]; i++) { | 69 | for (i = 0; dirs[i]; i++) { |
| 69 | filler(buf, dirs[i], NULL, 0); | 70 | filler(buf, dirs[i], NULL, 0); |
| 70 | } | 71 | } |
| 71 | 72 | ||
| 72 | free_dictionary(dirs); | 73 | free_dictionary(dirs); |
| 73 | 74 | ||
| 74 | return 0; | 75 | return 0; |
| 75 | } | 76 | } |
| 76 | 77 | ||
| 77 | static int ifuse_create(const char *path, mode_t mode, struct fuse_file_info *fi) { | 78 | static int ifuse_create(const char *path, mode_t mode, struct fuse_file_info *fi) |
| 79 | { | ||
| 78 | // exactly the same as open but using a different mode | 80 | // exactly the same as open but using a different mode |
| 79 | iphone_afc_file_t file = NULL; | 81 | iphone_afc_file_t file = NULL; |
| 80 | iphone_afc_client_t afc = fuse_get_context()->private_data; | 82 | iphone_afc_client_t afc = fuse_get_context()->private_data; |
| 81 | 83 | ||
| 82 | iphone_afc_open_file(afc, path, IPHONE_AFC_FILE_WRITE, &file); | 84 | iphone_afc_open_file(afc, path, IPHONE_AFC_FILE_WRITE, &file); |
| 83 | fh_index++; | 85 | fh_index++; |
| 84 | fi->fh = fh_index; | 86 | fi->fh = fh_index; |
| @@ -86,11 +88,12 @@ static int ifuse_create(const char *path, mode_t mode, struct fuse_file_info *fi | |||
| 86 | return 0; | 88 | return 0; |
| 87 | } | 89 | } |
| 88 | 90 | ||
| 89 | static int ifuse_open(const char *path, struct fuse_file_info *fi) { | 91 | static int ifuse_open(const char *path, struct fuse_file_info *fi) |
| 92 | { | ||
| 90 | iphone_afc_file_t file = NULL; | 93 | iphone_afc_file_t file = NULL; |
| 91 | iphone_afc_client_t afc = fuse_get_context()->private_data; | 94 | iphone_afc_client_t afc = fuse_get_context()->private_data; |
| 92 | uint32_t mode = 0; | 95 | uint32_t mode = 0; |
| 93 | 96 | ||
| 94 | if ((fi->flags & 3) == O_RDWR || (fi->flags & 3) == O_WRONLY) { | 97 | if ((fi->flags & 3) == O_RDWR || (fi->flags & 3) == O_WRONLY) { |
| 95 | mode = IPHONE_AFC_FILE_READ; | 98 | mode = IPHONE_AFC_FILE_READ; |
| 96 | } else if ((fi->flags & 3) == O_RDONLY) { | 99 | } else if ((fi->flags & 3) == O_RDONLY) { |
| @@ -98,9 +101,9 @@ static int ifuse_open(const char *path, struct fuse_file_info *fi) { | |||
| 98 | } else { | 101 | } else { |
| 99 | mode = IPHONE_AFC_FILE_READ; | 102 | mode = IPHONE_AFC_FILE_READ; |
| 100 | } | 103 | } |
| 101 | 104 | ||
| 102 | iphone_afc_open_file(afc, path, mode, &file); | 105 | iphone_afc_open_file(afc, path, mode, &file); |
| 103 | 106 | ||
| 104 | fh_index++; | 107 | fh_index++; |
| 105 | fi->fh = fh_index; | 108 | fi->fh = fh_index; |
| 106 | g_hash_table_insert(file_handles, &fh_index, file); | 109 | g_hash_table_insert(file_handles, &fh_index, file); |
| @@ -108,8 +111,8 @@ static int ifuse_open(const char *path, struct fuse_file_info *fi) { | |||
| 108 | return 0; | 111 | return 0; |
| 109 | } | 112 | } |
| 110 | 113 | ||
| 111 | static int ifuse_read(const char *path, char *buf, size_t size, off_t offset, | 114 | static int ifuse_read(const char *path, char *buf, size_t size, off_t offset, struct fuse_file_info *fi) |
| 112 | struct fuse_file_info *fi) { | 115 | { |
| 113 | int bytes = 0; | 116 | int bytes = 0; |
| 114 | iphone_afc_file_t file; | 117 | iphone_afc_file_t file; |
| 115 | iphone_afc_client_t afc = fuse_get_context()->private_data; | 118 | iphone_afc_client_t afc = fuse_get_context()->private_data; |
| @@ -118,7 +121,7 @@ static int ifuse_read(const char *path, char *buf, size_t size, off_t offset, | |||
| 118 | return 0; | 121 | return 0; |
| 119 | 122 | ||
| 120 | file = g_hash_table_lookup(file_handles, &(fi->fh)); | 123 | file = g_hash_table_lookup(file_handles, &(fi->fh)); |
| 121 | if (!file){ | 124 | if (!file) { |
| 122 | return -ENOENT; | 125 | return -ENOENT; |
| 123 | } | 126 | } |
| 124 | 127 | ||
| @@ -127,171 +130,195 @@ static int ifuse_read(const char *path, char *buf, size_t size, off_t offset, | |||
| 127 | return bytes; | 130 | return bytes; |
| 128 | } | 131 | } |
| 129 | 132 | ||
| 130 | static int ifuse_write(const char *path, const char *buf, size_t size, off_t offset, struct fuse_file_info *fi) { | 133 | static int ifuse_write(const char *path, const char *buf, size_t size, off_t offset, struct fuse_file_info *fi) |
| 134 | { | ||
| 131 | int bytes = 0; | 135 | int bytes = 0; |
| 132 | iphone_afc_file_t file = NULL; | 136 | iphone_afc_file_t file = NULL; |
| 133 | iphone_afc_client_t afc = fuse_get_context()->private_data; | 137 | iphone_afc_client_t afc = fuse_get_context()->private_data; |
| 134 | 138 | ||
| 135 | if (size == 0) return 0; | 139 | if (size == 0) |
| 136 | 140 | return 0; | |
| 141 | |||
| 137 | file = g_hash_table_lookup(file_handles, &(fi->fh)); | 142 | file = g_hash_table_lookup(file_handles, &(fi->fh)); |
| 138 | if (!file) return -ENOENT; | 143 | if (!file) |
| 139 | 144 | return -ENOENT; | |
| 145 | |||
| 140 | if (IPHONE_E_SUCCESS == iphone_afc_seek_file(afc, file, offset)) | 146 | if (IPHONE_E_SUCCESS == iphone_afc_seek_file(afc, file, offset)) |
| 141 | iphone_afc_write_file(afc, file, buf, size, &bytes); | 147 | iphone_afc_write_file(afc, file, buf, size, &bytes); |
| 142 | return bytes; | 148 | return bytes; |
| 143 | } | 149 | } |
| 144 | 150 | ||
| 145 | static int ifuse_fsync(const char *path, int datasync, struct fuse_file_info *fi) { | 151 | static int ifuse_fsync(const char *path, int datasync, struct fuse_file_info *fi) |
| 152 | { | ||
| 146 | return 0; | 153 | return 0; |
| 147 | } | 154 | } |
| 148 | 155 | ||
| 149 | static int ifuse_release(const char *path, struct fuse_file_info *fi){ | 156 | static int ifuse_release(const char *path, struct fuse_file_info *fi) |
| 157 | { | ||
| 150 | iphone_afc_file_t file = NULL; | 158 | iphone_afc_file_t file = NULL; |
| 151 | iphone_afc_client_t afc = fuse_get_context()->private_data; | 159 | iphone_afc_client_t afc = fuse_get_context()->private_data; |
| 152 | 160 | ||
| 153 | file = g_hash_table_lookup(file_handles, &(fi->fh)); | 161 | file = g_hash_table_lookup(file_handles, &(fi->fh)); |
| 154 | if (!file){ | 162 | if (!file) { |
| 155 | return -ENOENT; | 163 | return -ENOENT; |
| 156 | } | 164 | } |
| 157 | iphone_afc_close_file(afc, file); | 165 | iphone_afc_close_file(afc, file); |
| 158 | 166 | ||
| 159 | g_hash_table_remove(file_handles, &(fi->fh)); | 167 | g_hash_table_remove(file_handles, &(fi->fh)); |
| 160 | 168 | ||
| 161 | return 0; | 169 | return 0; |
| 162 | } | 170 | } |
| 163 | 171 | ||
| 164 | void *ifuse_init(struct fuse_conn_info *conn) { | 172 | void *ifuse_init(struct fuse_conn_info *conn) |
| 173 | { | ||
| 165 | int port = 0; | 174 | int port = 0; |
| 166 | iphone_afc_client_t afc = NULL; | 175 | iphone_afc_client_t afc = NULL; |
| 167 | 176 | ||
| 168 | conn->async_read = 0; | 177 | conn->async_read = 0; |
| 169 | 178 | ||
| 170 | file_handles = g_hash_table_new(g_int_hash, g_int_equal); | 179 | file_handles = g_hash_table_new(g_int_hash, g_int_equal); |
| 171 | 180 | ||
| 172 | iphone_get_device(&phone); | 181 | iphone_get_device(&phone); |
| 173 | if (!phone){ | 182 | if (!phone) { |
| 174 | fprintf(stderr, "No iPhone found, is it connected?\n"); | 183 | fprintf(stderr, "No iPhone found, is it connected?\n"); |
| 175 | return NULL; | 184 | return NULL; |
| 176 | } | 185 | } |
| 177 | 186 | ||
| 178 | 187 | ||
| 179 | if (IPHONE_E_SUCCESS != iphone_lckd_new_client(phone, &control)) { | 188 | if (IPHONE_E_SUCCESS != iphone_lckd_new_client(phone, &control)) { |
| 180 | iphone_free_device(phone); | 189 | iphone_free_device(phone); |
| 181 | fprintf(stderr, "Something went wrong in the lockdownd client.\n"); | 190 | fprintf(stderr, "Something went wrong in the lockdownd client.\n"); |
| 182 | return NULL; | 191 | return NULL; |
| 183 | } | 192 | } |
| 184 | 193 | ||
| 185 | if (IPHONE_E_SUCCESS == iphone_lckd_start_service(control, "com.apple.afc", &port) && !port) { | 194 | if (IPHONE_E_SUCCESS == iphone_lckd_start_service(control, "com.apple.afc", &port) && !port) { |
| 186 | iphone_lckd_free_client(control); | 195 | iphone_lckd_free_client(control); |
| 187 | iphone_free_device(phone); | 196 | iphone_free_device(phone); |
| 188 | fprintf(stderr, "Something went wrong when starting AFC."); | 197 | fprintf(stderr, "Something went wrong when starting AFC."); |
| 189 | return NULL; | 198 | return NULL; |
| 190 | } | 199 | } |
| 191 | 200 | ||
| 192 | iphone_afc_new_client(phone, 3432, port, &afc); | 201 | iphone_afc_new_client(phone, 3432, port, &afc); |
| 193 | 202 | ||
| 194 | return afc; | 203 | return afc; |
| 195 | } | 204 | } |
| 196 | 205 | ||
| 197 | void ifuse_cleanup(void *data) { | 206 | void ifuse_cleanup(void *data) |
| 198 | iphone_afc_client_t afc = (iphone_afc_client_t )data; | 207 | { |
| 208 | iphone_afc_client_t afc = (iphone_afc_client_t) data; | ||
| 199 | 209 | ||
| 200 | iphone_afc_free_client(afc); | 210 | iphone_afc_free_client(afc); |
| 201 | iphone_lckd_free_client(control); | 211 | iphone_lckd_free_client(control); |
| 202 | iphone_free_device(phone); | 212 | iphone_free_device(phone); |
| 203 | } | 213 | } |
| 204 | 214 | ||
| 205 | int ifuse_flush(const char *path, struct fuse_file_info *fi) { | 215 | int ifuse_flush(const char *path, struct fuse_file_info *fi) |
| 216 | { | ||
| 206 | return 0; | 217 | return 0; |
| 207 | } | 218 | } |
| 208 | 219 | ||
| 209 | int ifuse_statfs(const char *path, struct statvfs *stats) { | 220 | int ifuse_statfs(const char *path, struct statvfs *stats) |
| 221 | { | ||
| 210 | iphone_afc_client_t afc = fuse_get_context()->private_data; | 222 | iphone_afc_client_t afc = fuse_get_context()->private_data; |
| 211 | char **info_raw = NULL; | 223 | char **info_raw = NULL; |
| 212 | uint32_t totalspace = 0, freespace = 0, blocksize = 0, i = 0; | 224 | uint32_t totalspace = 0, freespace = 0, blocksize = 0, i = 0; |
| 213 | 225 | ||
| 214 | iphone_afc_get_devinfo(afc, &info_raw); | 226 | iphone_afc_get_devinfo(afc, &info_raw); |
| 215 | if (!info_raw) return -ENOENT; | 227 | if (!info_raw) |
| 216 | 228 | return -ENOENT; | |
| 229 | |||
| 217 | for (i = 0; info_raw[i]; i++) { | 230 | for (i = 0; info_raw[i]; i++) { |
| 218 | if (!strcmp(info_raw[i], "FSTotalBytes")) { | 231 | if (!strcmp(info_raw[i], "FSTotalBytes")) { |
| 219 | totalspace = atoi(info_raw[i+1]); | 232 | totalspace = atoi(info_raw[i + 1]); |
| 220 | } else if (!strcmp(info_raw[i], "FSFreeBytes")) { | 233 | } else if (!strcmp(info_raw[i], "FSFreeBytes")) { |
| 221 | freespace = atoi(info_raw[i+1]); | 234 | freespace = atoi(info_raw[i + 1]); |
| 222 | } else if (!strcmp(info_raw[i], "FSBlockSize")) { | 235 | } else if (!strcmp(info_raw[i], "FSBlockSize")) { |
| 223 | blocksize = atoi(info_raw[i+1]); | 236 | blocksize = atoi(info_raw[i + 1]); |
| 224 | } | 237 | } |
| 225 | } | 238 | } |
| 226 | free_dictionary(info_raw); | 239 | free_dictionary(info_raw); |
| 227 | 240 | ||
| 228 | // Now to fill the struct. | 241 | // Now to fill the struct. |
| 229 | stats->f_bsize = stats->f_frsize = blocksize; | 242 | stats->f_bsize = stats->f_frsize = blocksize; |
| 230 | stats->f_blocks = totalspace / blocksize; // gets the blocks by dividing bytes by blocksize | 243 | stats->f_blocks = totalspace / blocksize; // gets the blocks by dividing bytes by blocksize |
| 231 | stats->f_bfree = stats->f_bavail = freespace / blocksize; // all bytes are free to everyone, I guess. | 244 | stats->f_bfree = stats->f_bavail = freespace / blocksize; // all bytes are free to everyone, I guess. |
| 232 | stats->f_namemax = 255; // blah | 245 | stats->f_namemax = 255; // blah |
| 233 | stats->f_files = stats->f_ffree = 1000000000; // make up any old thing, I guess | 246 | stats->f_files = stats->f_ffree = 1000000000; // make up any old thing, I guess |
| 234 | return 0; | 247 | return 0; |
| 235 | } | 248 | } |
| 236 | 249 | ||
| 237 | int ifuse_truncate(const char *path, off_t size) { | 250 | int ifuse_truncate(const char *path, off_t size) |
| 251 | { | ||
| 238 | int result = 0; | 252 | int result = 0; |
| 239 | iphone_afc_client_t afc = fuse_get_context()->private_data; | 253 | iphone_afc_client_t afc = fuse_get_context()->private_data; |
| 240 | iphone_afc_file_t tfile = NULL; | 254 | iphone_afc_file_t tfile = NULL; |
| 241 | iphone_afc_open_file(afc, path, IPHONE_AFC_FILE_READ, &tfile); | 255 | iphone_afc_open_file(afc, path, IPHONE_AFC_FILE_READ, &tfile); |
| 242 | if (!tfile) return -1; | 256 | if (!tfile) |
| 243 | 257 | return -1; | |
| 258 | |||
| 244 | result = iphone_afc_truncate_file(afc, tfile, size); | 259 | result = iphone_afc_truncate_file(afc, tfile, size); |
| 245 | iphone_afc_close_file(afc, tfile); | 260 | iphone_afc_close_file(afc, tfile); |
| 246 | return result; | 261 | return result; |
| 247 | } | 262 | } |
| 248 | 263 | ||
| 249 | int ifuse_ftruncate(const char *path, off_t size, struct fuse_file_info *fi) { | 264 | int ifuse_ftruncate(const char *path, off_t size, struct fuse_file_info *fi) |
| 265 | { | ||
| 250 | iphone_afc_client_t afc = fuse_get_context()->private_data; | 266 | iphone_afc_client_t afc = fuse_get_context()->private_data; |
| 251 | iphone_afc_file_t file = g_hash_table_lookup(file_handles, &fi->fh); | 267 | iphone_afc_file_t file = g_hash_table_lookup(file_handles, &fi->fh); |
| 252 | if (!file) return -ENOENT; | 268 | if (!file) |
| 253 | 269 | return -ENOENT; | |
| 270 | |||
| 254 | return iphone_afc_truncate_file(afc, file, size); | 271 | return iphone_afc_truncate_file(afc, file, size); |
| 255 | } | 272 | } |
| 256 | 273 | ||
| 257 | int ifuse_unlink(const char *path) { | 274 | int ifuse_unlink(const char *path) |
| 275 | { | ||
| 258 | iphone_afc_client_t afc = fuse_get_context()->private_data; | 276 | iphone_afc_client_t afc = fuse_get_context()->private_data; |
| 259 | if (IPHONE_E_SUCCESS == iphone_afc_delete_file(afc, path)) return 0; | 277 | if (IPHONE_E_SUCCESS == iphone_afc_delete_file(afc, path)) |
| 260 | else return -1; | 278 | return 0; |
| 279 | else | ||
| 280 | return -1; | ||
| 261 | } | 281 | } |
| 262 | 282 | ||
| 263 | int ifuse_rename(const char *from, const char *to) { | 283 | int ifuse_rename(const char *from, const char *to) |
| 284 | { | ||
| 264 | iphone_afc_client_t afc = fuse_get_context()->private_data; | 285 | iphone_afc_client_t afc = fuse_get_context()->private_data; |
| 265 | if (IPHONE_E_SUCCESS == iphone_afc_rename_file(afc, from, to)) return 0; | 286 | if (IPHONE_E_SUCCESS == iphone_afc_rename_file(afc, from, to)) |
| 266 | else return -1; | 287 | return 0; |
| 288 | else | ||
| 289 | return -1; | ||
| 267 | } | 290 | } |
| 268 | 291 | ||
| 269 | int ifuse_mkdir(const char *dir, mode_t ignored) { | 292 | int ifuse_mkdir(const char *dir, mode_t ignored) |
| 293 | { | ||
| 270 | iphone_afc_client_t afc = fuse_get_context()->private_data; | 294 | iphone_afc_client_t afc = fuse_get_context()->private_data; |
| 271 | if (IPHONE_E_SUCCESS == iphone_afc_mkdir(afc, dir)) return 0; | 295 | if (IPHONE_E_SUCCESS == iphone_afc_mkdir(afc, dir)) |
| 272 | else return -1; | 296 | return 0; |
| 297 | else | ||
| 298 | return -1; | ||
| 273 | } | 299 | } |
| 274 | 300 | ||
| 275 | static struct fuse_operations ifuse_oper = { | 301 | static struct fuse_operations ifuse_oper = { |
| 276 | .getattr = ifuse_getattr, | 302 | .getattr = ifuse_getattr, |
| 277 | .statfs = ifuse_statfs, | 303 | .statfs = ifuse_statfs, |
| 278 | .readdir = ifuse_readdir, | 304 | .readdir = ifuse_readdir, |
| 279 | .mkdir = ifuse_mkdir, | 305 | .mkdir = ifuse_mkdir, |
| 280 | .rmdir = ifuse_unlink, // AFC uses the same op for both. | 306 | .rmdir = ifuse_unlink, // AFC uses the same op for both. |
| 281 | .create = ifuse_create, | 307 | .create = ifuse_create, |
| 282 | .open = ifuse_open, | 308 | .open = ifuse_open, |
| 283 | .read = ifuse_read, | 309 | .read = ifuse_read, |
| 284 | .write = ifuse_write, | 310 | .write = ifuse_write, |
| 285 | .truncate = ifuse_truncate, | 311 | .truncate = ifuse_truncate, |
| 286 | .ftruncate = ifuse_ftruncate, | 312 | .ftruncate = ifuse_ftruncate, |
| 287 | .unlink = ifuse_unlink, | 313 | .unlink = ifuse_unlink, |
| 288 | .rename = ifuse_rename, | 314 | .rename = ifuse_rename, |
| 289 | .fsync = ifuse_fsync, | 315 | .fsync = ifuse_fsync, |
| 290 | .release = ifuse_release, | 316 | .release = ifuse_release, |
| 291 | .init = ifuse_init, | 317 | .init = ifuse_init, |
| 292 | .destroy = ifuse_cleanup | 318 | .destroy = ifuse_cleanup |
| 293 | }; | 319 | }; |
| 294 | 320 | ||
| 295 | int main(int argc, char *argv[]) { | 321 | int main(int argc, char *argv[]) |
| 322 | { | ||
| 296 | return fuse_main(argc, argv, &ifuse_oper, NULL); | 323 | return fuse_main(argc, argv, &ifuse_oper, NULL); |
| 297 | } | 324 | } |
diff --git a/src/initconf.c b/src/initconf.c index b900f7f..412dd70 100644 --- a/src/initconf.c +++ b/src/initconf.c | |||
| @@ -36,38 +36,43 @@ int debug = 1; | |||
| 36 | * | 36 | * |
| 37 | * @param key The pointer to the desired location of the new key. | 37 | * @param key The pointer to the desired location of the new key. |
| 38 | */ | 38 | */ |
| 39 | void generate_key(gpointer key){ | 39 | void generate_key(gpointer key) |
| 40 | gnutls_x509_privkey_generate(*((gnutls_x509_privkey_t*)key), GNUTLS_PK_RSA, 2048, 0); | 40 | { |
| 41 | gnutls_x509_privkey_generate(*((gnutls_x509_privkey_t *) key), GNUTLS_PK_RSA, 2048, 0); | ||
| 41 | g_thread_exit(0); | 42 | g_thread_exit(0); |
| 42 | } | 43 | } |
| 44 | |||
| 43 | /** Simple function that generates a spinner until the mutex is released. | 45 | /** Simple function that generates a spinner until the mutex is released. |
| 44 | */ | 46 | */ |
| 45 | void progress_bar(gpointer mutex){ | 47 | void progress_bar(gpointer mutex) |
| 48 | { | ||
| 46 | const char *spinner = "|/-\\|/-\\"; | 49 | const char *spinner = "|/-\\|/-\\"; |
| 47 | int i = 0; | 50 | int i = 0; |
| 48 | 51 | ||
| 49 | while (!g_static_mutex_trylock((GStaticMutex*)mutex)){ | 52 | while (!g_static_mutex_trylock((GStaticMutex *) mutex)) { |
| 50 | usleep(500000); | 53 | usleep(500000); |
| 51 | printf("Generating key... %c\r", spinner[i++]); | 54 | printf("Generating key... %c\r", spinner[i++]); |
| 52 | fflush(stdout); | 55 | fflush(stdout); |
| 53 | if (i > 8) i = 0; | 56 | if (i > 8) |
| 57 | i = 0; | ||
| 54 | } | 58 | } |
| 55 | printf("Generating key... done\n"); | 59 | printf("Generating key... done\n"); |
| 56 | g_thread_exit(0); | 60 | g_thread_exit(0); |
| 57 | } | 61 | } |
| 58 | 62 | ||
| 59 | int main(int argc, char *argv[]) { | 63 | int main(int argc, char *argv[]) |
| 64 | { | ||
| 60 | GThread *progress_thread, *key_thread; | 65 | GThread *progress_thread, *key_thread; |
| 61 | GError *err; | 66 | GError *err; |
| 62 | static GStaticMutex mutex = G_STATIC_MUTEX_INIT; | 67 | static GStaticMutex mutex = G_STATIC_MUTEX_INIT; |
| 63 | char* host_id = NULL; | 68 | char *host_id = NULL; |
| 64 | gnutls_x509_privkey_t root_privkey; | 69 | gnutls_x509_privkey_t root_privkey; |
| 65 | gnutls_x509_privkey_t host_privkey; | 70 | gnutls_x509_privkey_t host_privkey; |
| 66 | gnutls_x509_crt_t root_cert; | 71 | gnutls_x509_crt_t root_cert; |
| 67 | gnutls_x509_crt_t host_cert; | 72 | gnutls_x509_crt_t host_cert; |
| 68 | 73 | ||
| 69 | // Create the thread | 74 | // Create the thread |
| 70 | if (!g_thread_supported()){ | 75 | if (!g_thread_supported()) { |
| 71 | g_thread_init(NULL); | 76 | g_thread_init(NULL); |
| 72 | } | 77 | } |
| 73 | gnutls_global_init(); | 78 | gnutls_global_init(); |
| @@ -88,28 +93,28 @@ int main(int argc, char *argv[]) { | |||
| 88 | 93 | ||
| 89 | /* generate root key */ | 94 | /* generate root key */ |
| 90 | g_static_mutex_lock(&mutex); | 95 | g_static_mutex_lock(&mutex); |
| 91 | if((key_thread = g_thread_create((GThreadFunc)generate_key, &root_privkey, TRUE, &err)) == NULL) { | 96 | if ((key_thread = g_thread_create((GThreadFunc) generate_key, &root_privkey, TRUE, &err)) == NULL) { |
| 92 | printf("Thread create failed: %s!!\n", err->message ); | 97 | printf("Thread create failed: %s!!\n", err->message); |
| 93 | g_error_free(err) ; | 98 | g_error_free(err); |
| 94 | } | 99 | } |
| 95 | if((progress_thread = g_thread_create((GThreadFunc)progress_bar, &mutex, TRUE, &err)) == NULL) { | 100 | if ((progress_thread = g_thread_create((GThreadFunc) progress_bar, &mutex, TRUE, &err)) == NULL) { |
| 96 | printf("Thread create failed: %s!!\n", err->message ); | 101 | printf("Thread create failed: %s!!\n", err->message); |
| 97 | g_error_free(err) ; | 102 | g_error_free(err); |
| 98 | } | 103 | } |
| 99 | g_thread_join(key_thread); | 104 | g_thread_join(key_thread); |
| 100 | g_static_mutex_unlock(&mutex); | 105 | g_static_mutex_unlock(&mutex); |
| 101 | g_thread_join(progress_thread); | 106 | g_thread_join(progress_thread); |
| 102 | 107 | ||
| 103 | /* generate host key */ | 108 | /* generate host key */ |
| 104 | g_static_mutex_init(&mutex); | 109 | g_static_mutex_init(&mutex); |
| 105 | g_static_mutex_lock(&mutex); | 110 | g_static_mutex_lock(&mutex); |
| 106 | if((key_thread = g_thread_create((GThreadFunc)generate_key, &host_privkey, TRUE, &err)) == NULL) { | 111 | if ((key_thread = g_thread_create((GThreadFunc) generate_key, &host_privkey, TRUE, &err)) == NULL) { |
| 107 | printf("Thread create failed: %s!!\n", err->message ); | 112 | printf("Thread create failed: %s!!\n", err->message); |
| 108 | g_error_free(err) ; | 113 | g_error_free(err); |
| 109 | } | 114 | } |
| 110 | if((progress_thread = g_thread_create((GThreadFunc)progress_bar, &mutex, TRUE, &err)) == NULL) { | 115 | if ((progress_thread = g_thread_create((GThreadFunc) progress_bar, &mutex, TRUE, &err)) == NULL) { |
| 111 | printf("Thread create failed: %s!!\n", err->message ); | 116 | printf("Thread create failed: %s!!\n", err->message); |
| 112 | g_error_free(err) ; | 117 | g_error_free(err); |
| 113 | } | 118 | } |
| 114 | g_thread_join(key_thread); | 119 | g_thread_join(key_thread); |
| 115 | g_static_mutex_unlock(&mutex); | 120 | g_static_mutex_unlock(&mutex); |
| @@ -136,33 +141,33 @@ int main(int argc, char *argv[]) { | |||
| 136 | 141 | ||
| 137 | 142 | ||
| 138 | /* export to PEM format */ | 143 | /* export to PEM format */ |
| 139 | gnutls_datum_t root_key_pem = {NULL, 0}; | 144 | gnutls_datum_t root_key_pem = { NULL, 0 }; |
| 140 | gnutls_datum_t host_key_pem = {NULL, 0}; | 145 | gnutls_datum_t host_key_pem = { NULL, 0 }; |
| 141 | 146 | ||
| 142 | gnutls_x509_privkey_export (root_privkey, GNUTLS_X509_FMT_PEM, NULL, &root_key_pem.size); | 147 | gnutls_x509_privkey_export(root_privkey, GNUTLS_X509_FMT_PEM, NULL, &root_key_pem.size); |
| 143 | gnutls_x509_privkey_export (host_privkey, GNUTLS_X509_FMT_PEM, NULL, &host_key_pem.size); | 148 | gnutls_x509_privkey_export(host_privkey, GNUTLS_X509_FMT_PEM, NULL, &host_key_pem.size); |
| 144 | 149 | ||
| 145 | root_key_pem.data = gnutls_malloc(root_key_pem.size); | 150 | root_key_pem.data = gnutls_malloc(root_key_pem.size); |
| 146 | host_key_pem.data = gnutls_malloc(host_key_pem.size); | 151 | host_key_pem.data = gnutls_malloc(host_key_pem.size); |
| 147 | 152 | ||
| 148 | gnutls_x509_privkey_export (root_privkey, GNUTLS_X509_FMT_PEM, root_key_pem.data, &root_key_pem.size); | 153 | gnutls_x509_privkey_export(root_privkey, GNUTLS_X509_FMT_PEM, root_key_pem.data, &root_key_pem.size); |
| 149 | gnutls_x509_privkey_export (host_privkey, GNUTLS_X509_FMT_PEM, host_key_pem.data, &host_key_pem.size); | 154 | gnutls_x509_privkey_export(host_privkey, GNUTLS_X509_FMT_PEM, host_key_pem.data, &host_key_pem.size); |
| 150 | 155 | ||
| 151 | gnutls_datum_t root_cert_pem = {NULL, 0}; | 156 | gnutls_datum_t root_cert_pem = { NULL, 0 }; |
| 152 | gnutls_datum_t host_cert_pem = {NULL, 0}; | 157 | gnutls_datum_t host_cert_pem = { NULL, 0 }; |
| 153 | 158 | ||
| 154 | gnutls_x509_crt_export (root_cert, GNUTLS_X509_FMT_PEM, NULL, &root_cert_pem.size); | 159 | gnutls_x509_crt_export(root_cert, GNUTLS_X509_FMT_PEM, NULL, &root_cert_pem.size); |
| 155 | gnutls_x509_crt_export (host_cert, GNUTLS_X509_FMT_PEM, NULL, &host_cert_pem.size); | 160 | gnutls_x509_crt_export(host_cert, GNUTLS_X509_FMT_PEM, NULL, &host_cert_pem.size); |
| 156 | 161 | ||
| 157 | root_cert_pem.data = gnutls_malloc(root_cert_pem.size); | 162 | root_cert_pem.data = gnutls_malloc(root_cert_pem.size); |
| 158 | host_cert_pem.data = gnutls_malloc(host_cert_pem.size); | 163 | host_cert_pem.data = gnutls_malloc(host_cert_pem.size); |
| 159 | 164 | ||
| 160 | printf("Generating root certificate..."); | 165 | printf("Generating root certificate..."); |
| 161 | gnutls_x509_crt_export (root_cert, GNUTLS_X509_FMT_PEM, root_cert_pem.data, &root_cert_pem.size); | 166 | gnutls_x509_crt_export(root_cert, GNUTLS_X509_FMT_PEM, root_cert_pem.data, &root_cert_pem.size); |
| 162 | printf("done\n"); | 167 | printf("done\n"); |
| 163 | 168 | ||
| 164 | printf("Generating host certificate..."); | 169 | printf("Generating host certificate..."); |
| 165 | gnutls_x509_crt_export (host_cert, GNUTLS_X509_FMT_PEM, host_cert_pem.data, &host_cert_pem.size); | 170 | gnutls_x509_crt_export(host_cert, GNUTLS_X509_FMT_PEM, host_cert_pem.data, &host_cert_pem.size); |
| 166 | printf("done\n"); | 171 | printf("done\n"); |
| 167 | 172 | ||
| 168 | 173 | ||
| @@ -176,4 +181,3 @@ int main(int argc, char *argv[]) { | |||
| 176 | 181 | ||
| 177 | return 0; | 182 | return 0; |
| 178 | } | 183 | } |
| 179 | |||
diff --git a/src/iphone.c b/src/iphone.c index 68963fe..2c4c541 100644 --- a/src/iphone.c +++ b/src/iphone.c | |||
| @@ -27,43 +27,42 @@ | |||
| 27 | #include <stdlib.h> | 27 | #include <stdlib.h> |
| 28 | #include <string.h> | 28 | #include <string.h> |
| 29 | 29 | ||
| 30 | extern int debug; | 30 | extern int debug; |
| 31 | 31 | ||
| 32 | /** Gets a handle to an iPhone | 32 | /** Gets a handle to an iPhone |
| 33 | * | 33 | * |
| 34 | * @return A structure with data on the first iPhone it finds. (Or NULL, on | 34 | * @return A structure with data on the first iPhone it finds. (Or NULL, on |
| 35 | * error) | 35 | * error) |
| 36 | */ | 36 | */ |
| 37 | iphone_error_t iphone_get_device ( iphone_device_t *device ){ | 37 | iphone_error_t iphone_get_device(iphone_device_t * device) |
| 38 | { | ||
| 38 | //check we can actually write in device | 39 | //check we can actually write in device |
| 39 | if (!device || (device && *device)) | 40 | if (!device || (device && *device)) |
| 40 | return IPHONE_E_INVALID_ARG; | 41 | return IPHONE_E_INVALID_ARG; |
| 41 | 42 | ||
| 42 | struct usb_bus *bus, *busses; | 43 | struct usb_bus *bus, *busses; |
| 43 | struct usb_device *dev; | 44 | struct usb_device *dev; |
| 44 | iphone_device_t phone = (iphone_device_t)malloc(sizeof(struct iphone_device_int)); | 45 | iphone_device_t phone = (iphone_device_t) malloc(sizeof(struct iphone_device_int)); |
| 45 | 46 | ||
| 46 | // Initialize the struct | 47 | // Initialize the struct |
| 47 | phone->device = NULL; | 48 | phone->device = NULL; |
| 48 | phone->__device = NULL; | 49 | phone->__device = NULL; |
| 49 | phone->buffer = NULL; | 50 | phone->buffer = NULL; |
| 50 | 51 | ||
| 51 | // Initialize libusb | 52 | // Initialize libusb |
| 52 | usb_init(); | 53 | usb_init(); |
| 53 | usb_find_busses(); | 54 | usb_find_busses(); |
| 54 | usb_find_devices(); | 55 | usb_find_devices(); |
| 55 | busses = usb_get_busses(); | 56 | busses = usb_get_busses(); |
| 56 | 57 | ||
| 57 | 58 | ||
| 58 | // Set the device configuration | 59 | // Set the device configuration |
| 59 | for (bus = busses; bus; bus = bus->next) { | 60 | for (bus = busses; bus; bus = bus->next) { |
| 60 | for (dev = bus->devices; dev; dev = dev->next) { | 61 | for (dev = bus->devices; dev; dev = dev->next) { |
| 61 | if (dev->descriptor.idVendor == 0x05ac && | 62 | if (dev->descriptor.idVendor == 0x05ac && |
| 62 | (dev->descriptor.idProduct == 0x1290 || | 63 | (dev->descriptor.idProduct == 0x1290 || |
| 63 | dev->descriptor.idProduct == 0x1291 || | 64 | dev->descriptor.idProduct == 0x1291 || dev->descriptor.idProduct == 0x1292) |
| 64 | dev->descriptor.idProduct == 0x1292 | 65 | ) { |
| 65 | ) | ||
| 66 | ) { | ||
| 67 | phone->__device = dev; | 66 | phone->__device = dev; |
| 68 | phone->device = usb_open(phone->__device); | 67 | phone->device = usb_open(phone->__device); |
| 69 | usb_set_configuration(phone->device, 3); | 68 | usb_set_configuration(phone->device, 3); |
| @@ -71,20 +70,21 @@ iphone_error_t iphone_get_device ( iphone_device_t *device ){ | |||
| 71 | break; | 70 | break; |
| 72 | } | 71 | } |
| 73 | } | 72 | } |
| 74 | if (phone->__device && phone->device) break; | 73 | if (phone->__device && phone->device) |
| 74 | break; | ||
| 75 | } | 75 | } |
| 76 | 76 | ||
| 77 | // Check to see if we are connected | 77 | // Check to see if we are connected |
| 78 | if (!phone->device || !phone->__device) { | 78 | if (!phone->device || !phone->__device) { |
| 79 | iphone_free_device(phone); | 79 | iphone_free_device(phone); |
| 80 | if (debug) fprintf(stderr, "get_iPhone(): iPhone not found\n"); | 80 | if (debug) |
| 81 | fprintf(stderr, "get_iPhone(): iPhone not found\n"); | ||
| 81 | return IPHONE_E_NO_DEVICE; | 82 | return IPHONE_E_NO_DEVICE; |
| 82 | } | 83 | } |
| 83 | |||
| 84 | // Send the version command to the phone | 84 | // Send the version command to the phone |
| 85 | int bytes = 0; | 85 | int bytes = 0; |
| 86 | usbmux_version_header *version = version_header(); | 86 | usbmux_version_header *version = version_header(); |
| 87 | bytes = usb_bulk_write(phone->device, BULKOUT, (char*)version, sizeof(*version), 800); | 87 | bytes = usb_bulk_write(phone->device, BULKOUT, (char *) version, sizeof(*version), 800); |
| 88 | if (bytes < 20 && debug) { | 88 | if (bytes < 20 && debug) { |
| 89 | fprintf(stderr, "get_iPhone(): libusb did NOT send enough!\n"); | 89 | fprintf(stderr, "get_iPhone(): libusb did NOT send enough!\n"); |
| 90 | if (bytes < 0) { | 90 | if (bytes < 0) { |
| @@ -92,20 +92,20 @@ iphone_error_t iphone_get_device ( iphone_device_t *device ){ | |||
| 92 | bytes, usb_strerror(), strerror(-bytes)); | 92 | bytes, usb_strerror(), strerror(-bytes)); |
| 93 | } | 93 | } |
| 94 | } | 94 | } |
| 95 | |||
| 96 | // Read the phone's response | 95 | // Read the phone's response |
| 97 | bytes = usb_bulk_read(phone->device, BULKIN, (char*)version, sizeof(*version), 800); | 96 | bytes = usb_bulk_read(phone->device, BULKIN, (char *) version, sizeof(*version), 800); |
| 98 | 97 | ||
| 99 | // Check for bad response | 98 | // Check for bad response |
| 100 | if (bytes < 20) { | 99 | if (bytes < 20) { |
| 101 | free(version); | 100 | free(version); |
| 102 | iphone_free_device(phone); | 101 | iphone_free_device(phone); |
| 103 | if (debug) fprintf(stderr, "get_iPhone(): Invalid version message -- header too short.\n"); | 102 | if (debug) |
| 104 | if (debug && bytes < 0) fprintf(stderr, "get_iPhone(): libusb error message %d: %s (%s)\n", | 103 | fprintf(stderr, "get_iPhone(): Invalid version message -- header too short.\n"); |
| 105 | bytes, usb_strerror(), strerror(-bytes)); | 104 | if (debug && bytes < 0) |
| 105 | fprintf(stderr, "get_iPhone(): libusb error message %d: %s (%s)\n", | ||
| 106 | bytes, usb_strerror(), strerror(-bytes)); | ||
| 106 | return IPHONE_E_NOT_ENOUGH_DATA; | 107 | return IPHONE_E_NOT_ENOUGH_DATA; |
| 107 | } | 108 | } |
| 108 | |||
| 109 | // Check for correct version | 109 | // Check for correct version |
| 110 | if (ntohl(version->major) == 1 && ntohl(version->minor) == 0) { | 110 | if (ntohl(version->major) == 1 && ntohl(version->minor) == 0) { |
| 111 | // We're all ready to roll. | 111 | // We're all ready to roll. |
| @@ -117,15 +117,17 @@ iphone_error_t iphone_get_device ( iphone_device_t *device ){ | |||
| 117 | // Bad header | 117 | // Bad header |
| 118 | iphone_free_device(phone); | 118 | iphone_free_device(phone); |
| 119 | free(version); | 119 | free(version); |
| 120 | if (debug) fprintf(stderr, "get_iPhone(): Received a bad header/invalid version number."); | 120 | if (debug) |
| 121 | fprintf(stderr, "get_iPhone(): Received a bad header/invalid version number."); | ||
| 121 | return IPHONE_E_BAD_HEADER; | 122 | return IPHONE_E_BAD_HEADER; |
| 122 | } | 123 | } |
| 123 | 124 | ||
| 124 | // If it got to this point it's gotta be bad | 125 | // If it got to this point it's gotta be bad |
| 125 | if (debug) fprintf(stderr, "get_iPhone(): Unknown error.\n"); | 126 | if (debug) |
| 127 | fprintf(stderr, "get_iPhone(): Unknown error.\n"); | ||
| 126 | iphone_free_device(phone); | 128 | iphone_free_device(phone); |
| 127 | free(version); | 129 | free(version); |
| 128 | return IPHONE_E_UNKNOWN_ERROR; // if it got to this point it's gotta be bad | 130 | return IPHONE_E_UNKNOWN_ERROR; // if it got to this point it's gotta be bad |
| 129 | } | 131 | } |
| 130 | 132 | ||
| 131 | /** Cleans up an iPhone structure, then frees the structure itself. | 133 | /** Cleans up an iPhone structure, then frees the structure itself. |
| @@ -134,8 +136,10 @@ iphone_error_t iphone_get_device ( iphone_device_t *device ){ | |||
| 134 | * | 136 | * |
| 135 | * @param phone A pointer to an iPhone structure. | 137 | * @param phone A pointer to an iPhone structure. |
| 136 | */ | 138 | */ |
| 137 | iphone_error_t iphone_free_device ( iphone_device_t device ) { | 139 | iphone_error_t iphone_free_device(iphone_device_t device) |
| 138 | if (!device) return IPHONE_E_INVALID_ARG; | 140 | { |
| 141 | if (!device) | ||
| 142 | return IPHONE_E_INVALID_ARG; | ||
| 139 | iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; | 143 | iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; |
| 140 | 144 | ||
| 141 | if (device->buffer) { | 145 | if (device->buffer) { |
| @@ -150,7 +154,7 @@ iphone_error_t iphone_free_device ( iphone_device_t device ) { | |||
| 150 | free(device); | 154 | free(device); |
| 151 | return ret; | 155 | return ret; |
| 152 | } | 156 | } |
| 153 | 157 | ||
| 154 | /** Sends data to the phone | 158 | /** Sends data to the phone |
| 155 | * This is a low-level (i.e. directly to phone) function. | 159 | * This is a low-level (i.e. directly to phone) function. |
| 156 | * | 160 | * |
| @@ -159,22 +163,27 @@ iphone_error_t iphone_free_device ( iphone_device_t device ) { | |||
| 159 | * @param datalen The length of the data | 163 | * @param datalen The length of the data |
| 160 | * @return The number of bytes sent, or -1 on error or something. | 164 | * @return The number of bytes sent, or -1 on error or something. |
| 161 | */ | 165 | */ |
| 162 | int send_to_phone(iphone_device_t phone, char *data, int datalen) { | 166 | int send_to_phone(iphone_device_t phone, char *data, int datalen) |
| 163 | if (!phone) return -1; | 167 | { |
| 168 | if (!phone) | ||
| 169 | return -1; | ||
| 164 | int bytes = 0; | 170 | int bytes = 0; |
| 165 | 171 | ||
| 166 | if (!phone) return -1; | 172 | if (!phone) |
| 167 | if (debug) fprintf(stderr, "send_to_phone: Attempting to send datalen = %i data = %p\n", datalen, data); | 173 | return -1; |
| 174 | if (debug) | ||
| 175 | fprintf(stderr, "send_to_phone: Attempting to send datalen = %i data = %p\n", datalen, data); | ||
| 168 | 176 | ||
| 169 | bytes = usb_bulk_write(phone->device, BULKOUT, data, datalen, 800); | 177 | bytes = usb_bulk_write(phone->device, BULKOUT, data, datalen, 800); |
| 170 | if (bytes < datalen) { | 178 | if (bytes < datalen) { |
| 171 | if(debug && bytes < 0) | 179 | if (debug && bytes < 0) |
| 172 | fprintf(stderr, "send_to_iphone(): libusb gave me the error %d: %s - %s\n", bytes, usb_strerror(), strerror(-bytes)); | 180 | fprintf(stderr, "send_to_iphone(): libusb gave me the error %d: %s - %s\n", bytes, usb_strerror(), |
| 181 | strerror(-bytes)); | ||
| 173 | return -1; | 182 | return -1; |
| 174 | } else { | 183 | } else { |
| 175 | return bytes; | 184 | return bytes; |
| 176 | } | 185 | } |
| 177 | 186 | ||
| 178 | return -1; | 187 | return -1; |
| 179 | } | 188 | } |
| 180 | 189 | ||
| @@ -186,18 +195,24 @@ int send_to_phone(iphone_device_t phone, char *data, int datalen) { | |||
| 186 | * | 195 | * |
| 187 | * @return How many bytes were read in, or -1 on error. | 196 | * @return How many bytes were read in, or -1 on error. |
| 188 | */ | 197 | */ |
| 189 | int recv_from_phone(iphone_device_t phone, char *data, int datalen) { | 198 | int recv_from_phone(iphone_device_t phone, char *data, int datalen) |
| 190 | if (!phone) return -1; | 199 | { |
| 200 | if (!phone) | ||
| 201 | return -1; | ||
| 191 | int bytes = 0; | 202 | int bytes = 0; |
| 192 | 203 | ||
| 193 | if (!phone) return -1; | 204 | if (!phone) |
| 194 | if (debug) fprintf(stderr, "recv_from_phone(): attempting to receive %i bytes\n", datalen); | 205 | return -1; |
| 195 | 206 | if (debug) | |
| 207 | fprintf(stderr, "recv_from_phone(): attempting to receive %i bytes\n", datalen); | ||
| 208 | |||
| 196 | bytes = usb_bulk_read(phone->device, BULKIN, data, datalen, 3500); | 209 | bytes = usb_bulk_read(phone->device, BULKIN, data, datalen, 3500); |
| 197 | if (bytes < 0) { | 210 | if (bytes < 0) { |
| 198 | if(debug) fprintf(stderr, "recv_from_phone(): libusb gave me the error %d: %s (%s)\n", bytes, usb_strerror(), strerror(-bytes)); | 211 | if (debug) |
| 212 | fprintf(stderr, "recv_from_phone(): libusb gave me the error %d: %s (%s)\n", bytes, usb_strerror(), | ||
| 213 | strerror(-bytes)); | ||
| 199 | return -1; | 214 | return -1; |
| 200 | } | 215 | } |
| 201 | 216 | ||
| 202 | return bytes; | 217 | return bytes; |
| 203 | } | 218 | } |
diff --git a/src/iphone.h b/src/iphone.h index 556a93a..222a1be 100644 --- a/src/iphone.h +++ b/src/iphone.h | |||
| @@ -22,7 +22,7 @@ | |||
| 22 | #ifndef IPHONE_H | 22 | #ifndef IPHONE_H |
| 23 | #define IPHONE_H | 23 | #define IPHONE_H |
| 24 | 24 | ||
| 25 | #ifndef USBMUX_H | 25 | #ifndef USBMUX_H |
| 26 | #include "usbmux.h" | 26 | #include "usbmux.h" |
| 27 | #warning usbmux not included? | 27 | #warning usbmux not included? |
| 28 | #endif | 28 | #endif |
diff --git a/src/lockdown.c b/src/lockdown.c index e5420a3..80974d2 100644 --- a/src/lockdown.c +++ b/src/lockdown.c | |||
| @@ -32,16 +32,17 @@ | |||
| 32 | 32 | ||
| 33 | extern int debug; | 33 | extern int debug; |
| 34 | 34 | ||
| 35 | const ASN1_ARRAY_TYPE pkcs1_asn1_tab[]={ | 35 | const ASN1_ARRAY_TYPE pkcs1_asn1_tab[] = { |
| 36 | {"PKCS1",536872976,0}, | 36 | {"PKCS1", 536872976, 0}, |
| 37 | {0,1073741836,0}, | 37 | {0, 1073741836, 0}, |
| 38 | {"RSAPublicKey",536870917,0}, | 38 | {"RSAPublicKey", 536870917, 0}, |
| 39 | {"modulus",1073741827,0}, | 39 | {"modulus", 1073741827, 0}, |
| 40 | {"publicExponent",3,0}, | 40 | {"publicExponent", 3, 0}, |
| 41 | {0,0,0} | 41 | {0, 0, 0} |
| 42 | }; | 42 | }; |
| 43 | 43 | ||
| 44 | int get_rand(int min, int max) { | 44 | int get_rand(int min, int max) |
| 45 | { | ||
| 45 | int retval = (rand() % (max - min)) + min; | 46 | int retval = (rand() % (max - min)) + min; |
| 46 | return retval; | 47 | return retval; |
| 47 | } | 48 | } |
| @@ -50,21 +51,22 @@ int get_rand(int min, int max) { | |||
| 50 | * | 51 | * |
| 51 | * @param A null terminated string containing a valid HostID. | 52 | * @param A null terminated string containing a valid HostID. |
| 52 | */ | 53 | */ |
| 53 | char *lockdownd_generate_hostid() { | 54 | char *lockdownd_generate_hostid() |
| 54 | char *hostid = (char*)malloc(sizeof(char) * 37); // HostID's are just UUID's, and UUID's are 36 characters long | 55 | { |
| 56 | char *hostid = (char *) malloc(sizeof(char) * 37); // HostID's are just UUID's, and UUID's are 36 characters long | ||
| 55 | const char *chars = "ABCDEF0123456789"; | 57 | const char *chars = "ABCDEF0123456789"; |
| 56 | srand(time(NULL)); | 58 | srand(time(NULL)); |
| 57 | int i = 0; | 59 | int i = 0; |
| 58 | 60 | ||
| 59 | for (i = 0; i < 36; i++) { | 61 | for (i = 0; i < 36; i++) { |
| 60 | if (i == 8 || i == 13 || i == 18 || i == 23) { | 62 | if (i == 8 || i == 13 || i == 18 || i == 23) { |
| 61 | hostid[i] = '-'; | 63 | hostid[i] = '-'; |
| 62 | continue; | 64 | continue; |
| 63 | } else { | 65 | } else { |
| 64 | hostid[i] = chars[get_rand(0,16)]; | 66 | hostid[i] = chars[get_rand(0, 16)]; |
| 65 | } | 67 | } |
| 66 | } | 68 | } |
| 67 | hostid[36] = '\0'; // make it a real string | 69 | hostid[36] = '\0'; // make it a real string |
| 68 | return hostid; | 70 | return hostid; |
| 69 | } | 71 | } |
| 70 | 72 | ||
| @@ -74,16 +76,18 @@ char *lockdownd_generate_hostid() { | |||
| 74 | * | 76 | * |
| 75 | * @return The lockdownd client. | 77 | * @return The lockdownd client. |
| 76 | */ | 78 | */ |
| 77 | iphone_lckd_client_t new_lockdownd_client(iphone_device_t phone) { | 79 | iphone_lckd_client_t new_lockdownd_client(iphone_device_t phone) |
| 78 | if (!phone) return NULL; | 80 | { |
| 79 | iphone_lckd_client_t control = (iphone_lckd_client_t)malloc(sizeof(struct iphone_lckd_client_int)); | 81 | if (!phone) |
| 82 | return NULL; | ||
| 83 | iphone_lckd_client_t control = (iphone_lckd_client_t) malloc(sizeof(struct iphone_lckd_client_int)); | ||
| 80 | 84 | ||
| 81 | if (IPHONE_E_SUCCESS != iphone_mux_new_client ( phone, 0x0a00, 0xf27e, &control->connection)) { | 85 | if (IPHONE_E_SUCCESS != iphone_mux_new_client(phone, 0x0a00, 0xf27e, &control->connection)) { |
| 82 | free(control); | 86 | free(control); |
| 83 | return NULL; | 87 | return NULL; |
| 84 | } | 88 | } |
| 85 | 89 | ||
| 86 | control->ssl_session = (gnutls_session_t*)malloc(sizeof(gnutls_session_t)); | 90 | control->ssl_session = (gnutls_session_t *) malloc(sizeof(gnutls_session_t)); |
| 87 | control->in_SSL = 0; | 91 | control->in_SSL = 0; |
| 88 | control->gtls_buffer_hack_len = 0; | 92 | control->gtls_buffer_hack_len = 0; |
| 89 | return control; | 93 | return control; |
| @@ -94,15 +98,18 @@ iphone_lckd_client_t new_lockdownd_client(iphone_device_t phone) { | |||
| 94 | * | 98 | * |
| 95 | * @param control The lockdown client | 99 | * @param control The lockdown client |
| 96 | */ | 100 | */ |
| 97 | iphone_error_t iphone_lckd_free_client( iphone_lckd_client_t client ) { | 101 | iphone_error_t iphone_lckd_free_client(iphone_lckd_client_t client) |
| 98 | if (!client) return IPHONE_E_INVALID_ARG; | 102 | { |
| 103 | if (!client) | ||
| 104 | return IPHONE_E_INVALID_ARG; | ||
| 99 | iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; | 105 | iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; |
| 100 | 106 | ||
| 101 | if (client->connection) { | 107 | if (client->connection) { |
| 102 | ret = iphone_mux_free_client(client->connection); | 108 | ret = iphone_mux_free_client(client->connection); |
| 103 | } | 109 | } |
| 104 | 110 | ||
| 105 | if (client->ssl_session) gnutls_deinit(*client->ssl_session); | 111 | if (client->ssl_session) |
| 112 | gnutls_deinit(*client->ssl_session); | ||
| 106 | free(client->ssl_session); | 113 | free(client->ssl_session); |
| 107 | free(client); | 114 | free(client); |
| 108 | return ret; | 115 | return ret; |
| @@ -116,24 +123,30 @@ iphone_error_t iphone_lckd_free_client( iphone_lckd_client_t client ) { | |||
| 116 | * | 123 | * |
| 117 | * @return The number of bytes received | 124 | * @return The number of bytes received |
| 118 | */ | 125 | */ |
| 119 | iphone_error_t iphone_lckd_recv ( iphone_lckd_client_t client, char **dump_data, uint32_t *recv_bytes ) { | 126 | iphone_error_t iphone_lckd_recv(iphone_lckd_client_t client, char **dump_data, uint32_t * recv_bytes) |
| 120 | if (!client || !dump_data || !recv_bytes) return IPHONE_E_INVALID_ARG; | 127 | { |
| 128 | if (!client || !dump_data || !recv_bytes) | ||
| 129 | return IPHONE_E_INVALID_ARG; | ||
| 121 | iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; | 130 | iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; |
| 122 | char *receive; | 131 | char *receive; |
| 123 | uint32 datalen = 0, bytes = 0; | 132 | uint32 datalen = 0, bytes = 0; |
| 124 | 133 | ||
| 125 | if (!client->in_SSL) ret = iphone_mux_recv(client->connection, (char *)&datalen, sizeof(datalen), &bytes); | 134 | if (!client->in_SSL) |
| 135 | ret = iphone_mux_recv(client->connection, (char *) &datalen, sizeof(datalen), &bytes); | ||
| 126 | else { | 136 | else { |
| 127 | bytes = gnutls_record_recv(*client->ssl_session, &datalen, sizeof(datalen)); | 137 | bytes = gnutls_record_recv(*client->ssl_session, &datalen, sizeof(datalen)); |
| 128 | if (bytes > 0) ret = IPHONE_E_SUCCESS; | 138 | if (bytes > 0) |
| 139 | ret = IPHONE_E_SUCCESS; | ||
| 129 | } | 140 | } |
| 130 | datalen = ntohl(datalen); | 141 | datalen = ntohl(datalen); |
| 131 | 142 | ||
| 132 | receive = (char*)malloc(sizeof(char) * datalen); | 143 | receive = (char *) malloc(sizeof(char) * datalen); |
| 133 | if (!client->in_SSL) ret = iphone_mux_recv(client->connection, receive, datalen, &bytes); | 144 | if (!client->in_SSL) |
| 145 | ret = iphone_mux_recv(client->connection, receive, datalen, &bytes); | ||
| 134 | else { | 146 | else { |
| 135 | bytes = gnutls_record_recv(*client->ssl_session, receive, datalen); | 147 | bytes = gnutls_record_recv(*client->ssl_session, receive, datalen); |
| 136 | if (bytes > 0) ret = IPHONE_E_SUCCESS; | 148 | if (bytes > 0) |
| 149 | ret = IPHONE_E_SUCCESS; | ||
| 137 | } | 150 | } |
| 138 | *dump_data = receive; | 151 | *dump_data = receive; |
| 139 | *recv_bytes = bytes; | 152 | *recv_bytes = bytes; |
| @@ -151,30 +164,34 @@ iphone_error_t iphone_lckd_recv ( iphone_lckd_client_t client, char **dump_data, | |||
| 151 | * | 164 | * |
| 152 | * @return The number of bytes sent | 165 | * @return The number of bytes sent |
| 153 | */ | 166 | */ |
| 154 | iphone_error_t iphone_lckd_send ( iphone_lckd_client_t client, char *raw_data, uint32_t length, uint32_t *sent_bytes ) { | 167 | iphone_error_t iphone_lckd_send(iphone_lckd_client_t client, char *raw_data, uint32_t length, uint32_t * sent_bytes) |
| 155 | if (!client || !raw_data || length == 0 || !sent_bytes) return IPHONE_E_INVALID_ARG; | 168 | { |
| 169 | if (!client || !raw_data || length == 0 || !sent_bytes) | ||
| 170 | return IPHONE_E_INVALID_ARG; | ||
| 156 | char *real_query; | 171 | char *real_query; |
| 157 | int bytes; | 172 | int bytes; |
| 158 | iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; | 173 | iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; |
| 159 | 174 | ||
| 160 | real_query = (char*)malloc(sizeof(char) * (length+4)); | 175 | real_query = (char *) malloc(sizeof(char) * (length + 4)); |
| 161 | length = htonl(length); | 176 | length = htonl(length); |
| 162 | memcpy(real_query, &length, sizeof(length)); | 177 | memcpy(real_query, &length, sizeof(length)); |
| 163 | memcpy(real_query+4, raw_data, ntohl(length)); | 178 | memcpy(real_query + 4, raw_data, ntohl(length)); |
| 164 | if (debug) { | 179 | if (debug) { |
| 165 | printf("lockdownd_send(): made the query, sending it along\n"); | 180 | printf("lockdownd_send(): made the query, sending it along\n"); |
| 166 | FILE *packet = fopen("grpkt", "w"); | 181 | FILE *packet = fopen("grpkt", "w"); |
| 167 | fwrite(real_query, 1, ntohl(length)+4, packet); | 182 | fwrite(real_query, 1, ntohl(length) + 4, packet); |
| 168 | fclose(packet); | 183 | fclose(packet); |
| 169 | packet = NULL; | 184 | packet = NULL; |
| 170 | } | 185 | } |
| 171 | 186 | ||
| 172 | if (!client->in_SSL) ret = iphone_mux_send(client->connection, real_query, ntohl(length)+sizeof(length), &bytes); | 187 | if (!client->in_SSL) |
| 188 | ret = iphone_mux_send(client->connection, real_query, ntohl(length) + sizeof(length), &bytes); | ||
| 173 | else { | 189 | else { |
| 174 | gnutls_record_send(*client->ssl_session, real_query, ntohl(length)+sizeof(length)); | 190 | gnutls_record_send(*client->ssl_session, real_query, ntohl(length) + sizeof(length)); |
| 175 | ret = IPHONE_E_SUCCESS; | 191 | ret = IPHONE_E_SUCCESS; |
| 176 | } | 192 | } |
| 177 | if (debug) printf("lockdownd_send(): sent it!\n"); | 193 | if (debug) |
| 194 | printf("lockdownd_send(): sent it!\n"); | ||
| 178 | free(real_query); | 195 | free(real_query); |
| 179 | *sent_bytes = bytes; | 196 | *sent_bytes = bytes; |
| 180 | return ret; | 197 | return ret; |
| @@ -188,46 +205,54 @@ iphone_error_t iphone_lckd_send ( iphone_lckd_client_t client, char *raw_data, u | |||
| 188 | * | 205 | * |
| 189 | * @return 1 on success and 0 on failure. | 206 | * @return 1 on success and 0 on failure. |
| 190 | */ | 207 | */ |
| 191 | iphone_error_t lockdownd_hello(iphone_lckd_client_t control) { | 208 | iphone_error_t lockdownd_hello(iphone_lckd_client_t control) |
| 192 | if (!control) return IPHONE_E_INVALID_ARG; | 209 | { |
| 210 | if (!control) | ||
| 211 | return IPHONE_E_INVALID_ARG; | ||
| 193 | xmlDocPtr plist = new_plist(); | 212 | xmlDocPtr plist = new_plist(); |
| 194 | xmlNode *dict, *key; | 213 | xmlNode *dict, *key; |
| 195 | char **dictionary; | 214 | char **dictionary; |
| 196 | int bytes = 0, i = 0; | 215 | int bytes = 0, i = 0; |
| 197 | iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; | 216 | iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; |
| 198 | 217 | ||
| 199 | if (debug) printf("lockdownd_hello() called\n"); | 218 | if (debug) |
| 219 | printf("lockdownd_hello() called\n"); | ||
| 200 | dict = add_child_to_plist(plist, "dict", "\n", NULL, 0); | 220 | dict = add_child_to_plist(plist, "dict", "\n", NULL, 0); |
| 201 | key = add_key_str_dict_element(plist, dict, "Request", "QueryType", 1); | 221 | key = add_key_str_dict_element(plist, dict, "Request", "QueryType", 1); |
| 202 | char *XML_content; | 222 | char *XML_content; |
| 203 | uint32 length; | 223 | uint32 length; |
| 204 | 224 | ||
| 205 | xmlDocDumpMemory(plist, (xmlChar **)&XML_content, &length); | 225 | xmlDocDumpMemory(plist, (xmlChar **) & XML_content, &length); |
| 206 | ret = iphone_lckd_send(control, XML_content, length, &bytes); | 226 | ret = iphone_lckd_send(control, XML_content, length, &bytes); |
| 207 | 227 | ||
| 208 | xmlFree(XML_content); | 228 | xmlFree(XML_content); |
| 209 | xmlFreeDoc(plist); plist = NULL; | 229 | xmlFreeDoc(plist); |
| 230 | plist = NULL; | ||
| 210 | ret = iphone_lckd_recv(control, &XML_content, &bytes); | 231 | ret = iphone_lckd_recv(control, &XML_content, &bytes); |
| 211 | 232 | ||
| 212 | plist = xmlReadMemory(XML_content, bytes, NULL, NULL, 0); | 233 | plist = xmlReadMemory(XML_content, bytes, NULL, NULL, 0); |
| 213 | if (!plist) return IPHONE_E_PLIST_ERROR; | 234 | if (!plist) |
| 235 | return IPHONE_E_PLIST_ERROR; | ||
| 214 | dict = xmlDocGetRootElement(plist); | 236 | dict = xmlDocGetRootElement(plist); |
| 215 | for (dict = dict->children; dict; dict = dict->next) { | 237 | for (dict = dict->children; dict; dict = dict->next) { |
| 216 | if (!xmlStrcmp(dict->name, "dict")) break; | 238 | if (!xmlStrcmp(dict->name, "dict")) |
| 239 | break; | ||
| 217 | } | 240 | } |
| 218 | if (!dict) return IPHONE_E_DICT_ERROR; | 241 | if (!dict) |
| 242 | return IPHONE_E_DICT_ERROR; | ||
| 219 | dictionary = read_dict_element_strings(dict); | 243 | dictionary = read_dict_element_strings(dict); |
| 220 | xmlFreeDoc(plist); | 244 | xmlFreeDoc(plist); |
| 221 | free(XML_content); | 245 | free(XML_content); |
| 222 | 246 | ||
| 223 | for (i = 0; dictionary[i]; i+=2) { | 247 | for (i = 0; dictionary[i]; i += 2) { |
| 224 | if (!strcmp(dictionary[i], "Result") && !strcmp(dictionary[i+1], "Success")) { | 248 | if (!strcmp(dictionary[i], "Result") && !strcmp(dictionary[i + 1], "Success")) { |
| 225 | if (debug) printf("lockdownd_hello(): success\n"); | 249 | if (debug) |
| 250 | printf("lockdownd_hello(): success\n"); | ||
| 226 | ret = IPHONE_E_SUCCESS; | 251 | ret = IPHONE_E_SUCCESS; |
| 227 | break; | 252 | break; |
| 228 | } | 253 | } |
| 229 | } | 254 | } |
| 230 | 255 | ||
| 231 | free_dictionary(dictionary); | 256 | free_dictionary(dictionary); |
| 232 | return ret; | 257 | return ret; |
| 233 | } | 258 | } |
| @@ -242,7 +267,8 @@ iphone_error_t lockdownd_hello(iphone_lckd_client_t control) { | |||
| 242 | */ | 267 | */ |
| 243 | iphone_error_t lockdownd_generic_get_value(iphone_lckd_client_t control, char *req_key, char **value) | 268 | iphone_error_t lockdownd_generic_get_value(iphone_lckd_client_t control, char *req_key, char **value) |
| 244 | { | 269 | { |
| 245 | if (!control || !req_key || !value || (value && *value)) return IPHONE_E_INVALID_ARG; | 270 | if (!control || !req_key || !value || (value && *value)) |
| 271 | return IPHONE_E_INVALID_ARG; | ||
| 246 | xmlDocPtr plist = new_plist(); | 272 | xmlDocPtr plist = new_plist(); |
| 247 | xmlNode *dict = NULL; | 273 | xmlNode *dict = NULL; |
| 248 | xmlNode *key = NULL;; | 274 | xmlNode *key = NULL;; |
| @@ -251,54 +277,61 @@ iphone_error_t lockdownd_generic_get_value(iphone_lckd_client_t control, char *r | |||
| 251 | char *XML_content = NULL; | 277 | char *XML_content = NULL; |
| 252 | uint32 length = 0; | 278 | uint32 length = 0; |
| 253 | iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; | 279 | iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; |
| 254 | 280 | ||
| 255 | /* Setup DevicePublicKey request plist */ | 281 | /* Setup DevicePublicKey request plist */ |
| 256 | dict = add_child_to_plist(plist, "dict", "\n", NULL, 0); | 282 | dict = add_child_to_plist(plist, "dict", "\n", NULL, 0); |
| 257 | key = add_key_str_dict_element(plist, dict, "Key", req_key, 1); | 283 | key = add_key_str_dict_element(plist, dict, "Key", req_key, 1); |
| 258 | key = add_key_str_dict_element(plist, dict, "Request", "GetValue", 1); | 284 | key = add_key_str_dict_element(plist, dict, "Request", "GetValue", 1); |
| 259 | xmlDocDumpMemory(plist, (xmlChar**)&XML_content, &length); | 285 | xmlDocDumpMemory(plist, (xmlChar **) & XML_content, &length); |
| 260 | 286 | ||
| 261 | /* send to iPhone */ | 287 | /* send to iPhone */ |
| 262 | ret = iphone_lckd_send(control, XML_content, length, &bytes); | 288 | ret = iphone_lckd_send(control, XML_content, length, &bytes); |
| 263 | 289 | ||
| 264 | xmlFree(XML_content); | 290 | xmlFree(XML_content); |
| 265 | xmlFreeDoc(plist); plist = NULL; | 291 | xmlFreeDoc(plist); |
| 292 | plist = NULL; | ||
| 266 | 293 | ||
| 267 | if (ret != IPHONE_E_SUCCESS) return ret; | 294 | if (ret != IPHONE_E_SUCCESS) |
| 295 | return ret; | ||
| 268 | 296 | ||
| 269 | /* Now get iPhone's answer */ | 297 | /* Now get iPhone's answer */ |
| 270 | ret = iphone_lckd_recv(control, &XML_content, &bytes); | 298 | ret = iphone_lckd_recv(control, &XML_content, &bytes); |
| 271 | 299 | ||
| 272 | if (ret != IPHONE_E_SUCCESS) return ret; | 300 | if (ret != IPHONE_E_SUCCESS) |
| 301 | return ret; | ||
| 273 | 302 | ||
| 274 | plist = xmlReadMemory(XML_content, bytes, NULL, NULL, 0); | 303 | plist = xmlReadMemory(XML_content, bytes, NULL, NULL, 0); |
| 275 | if (!plist) return IPHONE_E_PLIST_ERROR; | 304 | if (!plist) |
| 305 | return IPHONE_E_PLIST_ERROR; | ||
| 276 | dict = xmlDocGetRootElement(plist); | 306 | dict = xmlDocGetRootElement(plist); |
| 277 | for (dict = dict->children; dict; dict = dict->next) { | 307 | for (dict = dict->children; dict; dict = dict->next) { |
| 278 | if (!xmlStrcmp(dict->name, "dict")) break; | 308 | if (!xmlStrcmp(dict->name, "dict")) |
| 309 | break; | ||
| 279 | } | 310 | } |
| 280 | if (!dict) return IPHONE_E_DICT_ERROR; | 311 | if (!dict) |
| 281 | 312 | return IPHONE_E_DICT_ERROR; | |
| 313 | |||
| 282 | /* Parse xml to check success and to find public key */ | 314 | /* Parse xml to check success and to find public key */ |
| 283 | dictionary = read_dict_element_strings(dict); | 315 | dictionary = read_dict_element_strings(dict); |
| 284 | xmlFreeDoc(plist); | 316 | xmlFreeDoc(plist); |
| 285 | free(XML_content); | 317 | free(XML_content); |
| 286 | 318 | ||
| 287 | int success = 0; | 319 | int success = 0; |
| 288 | for (i = 0; dictionary[i]; i+=2) { | 320 | for (i = 0; dictionary[i]; i += 2) { |
| 289 | if (!strcmp(dictionary[i], "Result") && !strcmp(dictionary[i+1], "Success")) { | 321 | if (!strcmp(dictionary[i], "Result") && !strcmp(dictionary[i + 1], "Success")) { |
| 290 | success = 1; | 322 | success = 1; |
| 291 | } | 323 | } |
| 292 | if (!strcmp(dictionary[i], "Value")) { | 324 | if (!strcmp(dictionary[i], "Value")) { |
| 293 | *value = strdup(dictionary[i+1]); | 325 | *value = strdup(dictionary[i + 1]); |
| 294 | } | 326 | } |
| 295 | } | 327 | } |
| 296 | 328 | ||
| 297 | if (dictionary) { | 329 | if (dictionary) { |
| 298 | free_dictionary(dictionary); | 330 | free_dictionary(dictionary); |
| 299 | dictionary = NULL; | 331 | dictionary = NULL; |
| 300 | } | 332 | } |
| 301 | if (success) ret = IPHONE_E_SUCCESS; | 333 | if (success) |
| 334 | ret = IPHONE_E_SUCCESS; | ||
| 302 | return ret; | 335 | return ret; |
| 303 | } | 336 | } |
| 304 | 337 | ||
| @@ -331,15 +364,15 @@ iphone_error_t lockdownd_get_device_public_key(iphone_lckd_client_t control, cha | |||
| 331 | * | 364 | * |
| 332 | * @return 1 on success and 0 on failure | 365 | * @return 1 on success and 0 on failure |
| 333 | */ | 366 | */ |
| 334 | iphone_error_t iphone_lckd_new_client ( iphone_device_t device, iphone_lckd_client_t *client ) | 367 | iphone_error_t iphone_lckd_new_client(iphone_device_t device, iphone_lckd_client_t * client) |
| 335 | { | 368 | { |
| 336 | if (!device || !client || (client && *client) ) | 369 | if (!device || !client || (client && *client)) |
| 337 | return IPHONE_E_INVALID_ARG; | 370 | return IPHONE_E_INVALID_ARG; |
| 338 | iphone_error_t ret = IPHONE_E_SUCCESS; | 371 | iphone_error_t ret = IPHONE_E_SUCCESS; |
| 339 | char *host_id = NULL; | 372 | char *host_id = NULL; |
| 340 | 373 | ||
| 341 | iphone_lckd_client_t client_loc = new_lockdownd_client( device ); | 374 | iphone_lckd_client_t client_loc = new_lockdownd_client(device); |
| 342 | if (IPHONE_E_SUCCESS != lockdownd_hello(client_loc)){ | 375 | if (IPHONE_E_SUCCESS != lockdownd_hello(client_loc)) { |
| 343 | fprintf(stderr, "Hello failed in the lockdownd client.\n"); | 376 | fprintf(stderr, "Hello failed in the lockdownd client.\n"); |
| 344 | ret = IPHONE_E_NOT_ENOUGH_DATA; | 377 | ret = IPHONE_E_NOT_ENOUGH_DATA; |
| 345 | } | 378 | } |
| @@ -347,12 +380,12 @@ iphone_error_t iphone_lckd_new_client ( iphone_device_t device, iphone_lckd_clie | |||
| 347 | 380 | ||
| 348 | char *uid = NULL; | 381 | char *uid = NULL; |
| 349 | ret = lockdownd_get_device_uid(client_loc, &uid); | 382 | ret = lockdownd_get_device_uid(client_loc, &uid); |
| 350 | if(IPHONE_E_SUCCESS != ret){ | 383 | if (IPHONE_E_SUCCESS != ret) { |
| 351 | fprintf(stderr, "Device refused to send uid.\n"); | 384 | fprintf(stderr, "Device refused to send uid.\n"); |
| 352 | } | 385 | } |
| 353 | 386 | ||
| 354 | host_id = get_host_id(); | 387 | host_id = get_host_id(); |
| 355 | if (IPHONE_E_SUCCESS == ret && !host_id){ | 388 | if (IPHONE_E_SUCCESS == ret && !host_id) { |
| 356 | fprintf(stderr, "No HostID found, run libiphone-initconf.\n"); | 389 | fprintf(stderr, "No HostID found, run libiphone-initconf.\n"); |
| 357 | ret = IPHONE_E_INVALID_CONF; | 390 | ret = IPHONE_E_INVALID_CONF; |
| 358 | } | 391 | } |
| @@ -366,7 +399,7 @@ iphone_error_t iphone_lckd_new_client ( iphone_device_t device, iphone_lckd_clie | |||
| 366 | } | 399 | } |
| 367 | 400 | ||
| 368 | ret = lockdownd_start_SSL_session(client_loc, host_id); | 401 | ret = lockdownd_start_SSL_session(client_loc, host_id); |
| 369 | if (IPHONE_E_SUCCESS != ret ) { | 402 | if (IPHONE_E_SUCCESS != ret) { |
| 370 | ret = IPHONE_E_SSL_ERROR; | 403 | ret = IPHONE_E_SSL_ERROR; |
| 371 | fprintf(stderr, "SSL Session opening failed.\n"); | 404 | fprintf(stderr, "SSL Session opening failed.\n"); |
| 372 | } | 405 | } |
| @@ -399,19 +432,19 @@ iphone_error_t lockdownd_pair_device(iphone_lckd_client_t control, char *uid, ch | |||
| 399 | char *XML_content = NULL; | 432 | char *XML_content = NULL; |
| 400 | uint32 length = 0; | 433 | uint32 length = 0; |
| 401 | 434 | ||
| 402 | char* device_cert_b64 = NULL; | 435 | char *device_cert_b64 = NULL; |
| 403 | char* host_cert_b64 = NULL; | 436 | char *host_cert_b64 = NULL; |
| 404 | char* root_cert_b64 = NULL; | 437 | char *root_cert_b64 = NULL; |
| 405 | char *public_key_b64 = NULL; | 438 | char *public_key_b64 = NULL; |
| 406 | 439 | ||
| 407 | ret = lockdownd_get_device_public_key(control, &public_key_b64); | 440 | ret = lockdownd_get_device_public_key(control, &public_key_b64); |
| 408 | if(ret != IPHONE_E_SUCCESS){ | 441 | if (ret != IPHONE_E_SUCCESS) { |
| 409 | fprintf(stderr, "Device refused to send public key.\n"); | 442 | fprintf(stderr, "Device refused to send public key.\n"); |
| 410 | return ret; | 443 | return ret; |
| 411 | } | 444 | } |
| 412 | 445 | ||
| 413 | ret = lockdownd_gen_pair_cert(public_key_b64, &device_cert_b64, &host_cert_b64, &root_cert_b64); | 446 | ret = lockdownd_gen_pair_cert(public_key_b64, &device_cert_b64, &host_cert_b64, &root_cert_b64); |
| 414 | if(ret != IPHONE_E_SUCCESS){ | 447 | if (ret != IPHONE_E_SUCCESS) { |
| 415 | free(public_key_b64); | 448 | free(public_key_b64); |
| 416 | return ret; | 449 | return ret; |
| 417 | } | 450 | } |
| @@ -426,51 +459,55 @@ iphone_error_t lockdownd_pair_device(iphone_lckd_client_t control, char *uid, ch | |||
| 426 | add_key_data_dict_element(plist, dictRecord, "RootCertificate", root_cert_b64, 2); | 459 | add_key_data_dict_element(plist, dictRecord, "RootCertificate", root_cert_b64, 2); |
| 427 | add_key_str_dict_element(plist, dict, "Request", "Pair", 1); | 460 | add_key_str_dict_element(plist, dict, "Request", "Pair", 1); |
| 428 | 461 | ||
| 429 | xmlDocDumpMemory(plist, (xmlChar**)&XML_content, &length); | 462 | xmlDocDumpMemory(plist, (xmlChar **) & XML_content, &length); |
| 430 | 463 | ||
| 431 | printf("XML Pairing request : %s\n",XML_content); | 464 | printf("XML Pairing request : %s\n", XML_content); |
| 432 | 465 | ||
| 433 | /* send to iPhone */ | 466 | /* send to iPhone */ |
| 434 | ret = iphone_lckd_send(control, XML_content, length, &bytes); | 467 | ret = iphone_lckd_send(control, XML_content, length, &bytes); |
| 435 | 468 | ||
| 436 | xmlFree(XML_content); | 469 | xmlFree(XML_content); |
| 437 | xmlFreeDoc(plist); plist = NULL; | 470 | xmlFreeDoc(plist); |
| 471 | plist = NULL; | ||
| 438 | 472 | ||
| 439 | if (ret != IPHONE_E_SUCCESS) return ret; | 473 | if (ret != IPHONE_E_SUCCESS) |
| 474 | return ret; | ||
| 440 | 475 | ||
| 441 | /* Now get iPhone's answer */ | 476 | /* Now get iPhone's answer */ |
| 442 | ret = iphone_lckd_recv(control, &XML_content, &bytes); | 477 | ret = iphone_lckd_recv(control, &XML_content, &bytes); |
| 443 | 478 | ||
| 444 | if (ret != IPHONE_E_SUCCESS) return ret; | 479 | if (ret != IPHONE_E_SUCCESS) |
| 480 | return ret; | ||
| 445 | 481 | ||
| 446 | if (debug) { | 482 | if (debug) { |
| 447 | printf("lockdown_pair_device: iPhone's response to our pair request:\n"); | 483 | printf("lockdown_pair_device: iPhone's response to our pair request:\n"); |
| 448 | fwrite(XML_content, 1, bytes, stdout); | 484 | fwrite(XML_content, 1, bytes, stdout); |
| 449 | printf("\n\n"); | 485 | printf("\n\n"); |
| 450 | } | 486 | } |
| 451 | 487 | ||
| 452 | plist = xmlReadMemory(XML_content, bytes, NULL, NULL, 0); | 488 | plist = xmlReadMemory(XML_content, bytes, NULL, NULL, 0); |
| 453 | if (!plist) { | 489 | if (!plist) { |
| 454 | free(public_key_b64); | 490 | free(public_key_b64); |
| 455 | return IPHONE_E_PLIST_ERROR; | 491 | return IPHONE_E_PLIST_ERROR; |
| 456 | } | 492 | } |
| 457 | dict = xmlDocGetRootElement(plist); | 493 | dict = xmlDocGetRootElement(plist); |
| 458 | for (dict = dict->children; dict; dict = dict->next) { | 494 | for (dict = dict->children; dict; dict = dict->next) { |
| 459 | if (!xmlStrcmp(dict->name, "dict")) break; | 495 | if (!xmlStrcmp(dict->name, "dict")) |
| 496 | break; | ||
| 460 | } | 497 | } |
| 461 | if (!dict) { | 498 | if (!dict) { |
| 462 | free(public_key_b64); | 499 | free(public_key_b64); |
| 463 | return IPHONE_E_DICT_ERROR; | 500 | return IPHONE_E_DICT_ERROR; |
| 464 | } | 501 | } |
| 465 | 502 | ||
| 466 | /* Parse xml to check success and to find public key */ | 503 | /* Parse xml to check success and to find public key */ |
| 467 | dictionary = read_dict_element_strings(dict); | 504 | dictionary = read_dict_element_strings(dict); |
| 468 | xmlFreeDoc(plist); | 505 | xmlFreeDoc(plist); |
| 469 | free(XML_content); | 506 | free(XML_content); |
| 470 | 507 | ||
| 471 | int success = 0; | 508 | int success = 0; |
| 472 | for (i = 0; dictionary[i]; i+=2) { | 509 | for (i = 0; dictionary[i]; i += 2) { |
| 473 | if (!strcmp(dictionary[i], "Result") && !strcmp(dictionary[i+1], "Success")) { | 510 | if (!strcmp(dictionary[i], "Result") && !strcmp(dictionary[i + 1], "Success")) { |
| 474 | success = 1; | 511 | success = 1; |
| 475 | } | 512 | } |
| 476 | } | 513 | } |
| @@ -482,11 +519,13 @@ iphone_error_t lockdownd_pair_device(iphone_lckd_client_t control, char *uid, ch | |||
| 482 | 519 | ||
| 483 | /* store public key in config if pairing succeeded */ | 520 | /* store public key in config if pairing succeeded */ |
| 484 | if (success) { | 521 | if (success) { |
| 485 | if (debug) printf("lockdownd_pair_device: pair success\n"); | 522 | if (debug) |
| 523 | printf("lockdownd_pair_device: pair success\n"); | ||
| 486 | store_device_public_key(uid, public_key_b64); | 524 | store_device_public_key(uid, public_key_b64); |
| 487 | ret = IPHONE_E_SUCCESS; | 525 | ret = IPHONE_E_SUCCESS; |
| 488 | } else { | 526 | } else { |
| 489 | if (debug) printf("lockdownd_pair_device: pair failure\n"); | 527 | if (debug) |
| 528 | printf("lockdownd_pair_device: pair failure\n"); | ||
| 490 | ret = IPHONE_E_PAIRING_FAILED; | 529 | ret = IPHONE_E_PAIRING_FAILED; |
| 491 | } | 530 | } |
| 492 | free(public_key_b64); | 531 | free(public_key_b64); |
| @@ -498,24 +537,26 @@ iphone_error_t lockdownd_pair_device(iphone_lckd_client_t control, char *uid, ch | |||
| 498 | * | 537 | * |
| 499 | * @return IPHONE_E_SUCCESS on success. | 538 | * @return IPHONE_E_SUCCESS on success. |
| 500 | */ | 539 | */ |
| 501 | iphone_error_t lockdownd_gen_pair_cert(char *public_key_b64, char **device_cert_b64, char **host_cert_b64, char **root_cert_b64) | 540 | iphone_error_t lockdownd_gen_pair_cert(char *public_key_b64, char **device_cert_b64, char **host_cert_b64, |
| 541 | char **root_cert_b64) | ||
| 502 | { | 542 | { |
| 503 | if (!public_key_b64 || !device_cert_b64 || !host_cert_b64 || !root_cert_b64) return IPHONE_E_INVALID_ARG; | 543 | if (!public_key_b64 || !device_cert_b64 || !host_cert_b64 || !root_cert_b64) |
| 544 | return IPHONE_E_INVALID_ARG; | ||
| 504 | iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; | 545 | iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; |
| 505 | 546 | ||
| 506 | gnutls_datum_t modulus = {NULL, 0}; | 547 | gnutls_datum_t modulus = { NULL, 0 }; |
| 507 | gnutls_datum_t exponent = {NULL, 0}; | 548 | gnutls_datum_t exponent = { NULL, 0 }; |
| 508 | 549 | ||
| 509 | /* first decode base64 public_key */ | 550 | /* first decode base64 public_key */ |
| 510 | gnutls_datum_t pem_pub_key; | 551 | gnutls_datum_t pem_pub_key; |
| 511 | gsize decoded_size; | 552 | gsize decoded_size; |
| 512 | pem_pub_key.data = g_base64_decode (public_key_b64, &decoded_size); | 553 | pem_pub_key.data = g_base64_decode(public_key_b64, &decoded_size); |
| 513 | pem_pub_key.size = decoded_size; | 554 | pem_pub_key.size = decoded_size; |
| 514 | 555 | ||
| 515 | /* now decode the PEM encoded key */ | 556 | /* now decode the PEM encoded key */ |
| 516 | gnutls_datum_t der_pub_key; | 557 | gnutls_datum_t der_pub_key; |
| 517 | if( GNUTLS_E_SUCCESS == gnutls_pem_base64_decode_alloc ("RSA PUBLIC KEY", &pem_pub_key, &der_pub_key) ){ | 558 | if (GNUTLS_E_SUCCESS == gnutls_pem_base64_decode_alloc("RSA PUBLIC KEY", &pem_pub_key, &der_pub_key)) { |
| 518 | 559 | ||
| 519 | /* initalize asn.1 parser */ | 560 | /* initalize asn.1 parser */ |
| 520 | ASN1_TYPE pkcs1 = ASN1_TYPE_EMPTY; | 561 | ASN1_TYPE pkcs1 = ASN1_TYPE_EMPTY; |
| 521 | if (ASN1_SUCCESS == asn1_array2tree(pkcs1_asn1_tab, &pkcs1, NULL)) { | 562 | if (ASN1_SUCCESS == asn1_array2tree(pkcs1_asn1_tab, &pkcs1, NULL)) { |
| @@ -526,14 +567,14 @@ iphone_error_t lockdownd_gen_pair_cert(char *public_key_b64, char **device_cert_ | |||
| 526 | if (ASN1_SUCCESS == asn1_der_decoding(&asn1_pub_key, der_pub_key.data, der_pub_key.size, NULL)) { | 567 | if (ASN1_SUCCESS == asn1_der_decoding(&asn1_pub_key, der_pub_key.data, der_pub_key.size, NULL)) { |
| 527 | 568 | ||
| 528 | /* get size to read */ | 569 | /* get size to read */ |
| 529 | int ret1 = asn1_read_value (asn1_pub_key, "modulus", NULL, &modulus.size); | 570 | int ret1 = asn1_read_value(asn1_pub_key, "modulus", NULL, &modulus.size); |
| 530 | int ret2 = asn1_read_value (asn1_pub_key, "publicExponent", NULL, &exponent.size); | 571 | int ret2 = asn1_read_value(asn1_pub_key, "publicExponent", NULL, &exponent.size); |
| 531 | 572 | ||
| 532 | modulus.data = gnutls_malloc(modulus.size); | 573 | modulus.data = gnutls_malloc(modulus.size); |
| 533 | exponent.data = gnutls_malloc(exponent.size); | 574 | exponent.data = gnutls_malloc(exponent.size); |
| 534 | 575 | ||
| 535 | ret1 = asn1_read_value (asn1_pub_key, "modulus", modulus.data, &modulus.size); | 576 | ret1 = asn1_read_value(asn1_pub_key, "modulus", modulus.data, &modulus.size); |
| 536 | ret2 = asn1_read_value (asn1_pub_key, "publicExponent", exponent.data, &exponent.size); | 577 | ret2 = asn1_read_value(asn1_pub_key, "publicExponent", exponent.data, &exponent.size); |
| 537 | if (ASN1_SUCCESS == ret1 && ASN1_SUCCESS == ret2) | 578 | if (ASN1_SUCCESS == ret1 && ASN1_SUCCESS == ret2) |
| 538 | ret = IPHONE_E_SUCCESS; | 579 | ret = IPHONE_E_SUCCESS; |
| 539 | } | 580 | } |
| @@ -548,34 +589,36 @@ iphone_error_t lockdownd_gen_pair_cert(char *public_key_b64, char **device_cert_ | |||
| 548 | if (IPHONE_E_SUCCESS == ret && 0 != modulus.size && 0 != exponent.size) { | 589 | if (IPHONE_E_SUCCESS == ret && 0 != modulus.size && 0 != exponent.size) { |
| 549 | 590 | ||
| 550 | gnutls_global_init(); | 591 | gnutls_global_init(); |
| 551 | gnutls_datum_t essentially_null = {strdup("abababababababab"), strlen("abababababababab")}; | 592 | gnutls_datum_t essentially_null = { strdup("abababababababab"), strlen("abababababababab") }; |
| 552 | 593 | ||
| 553 | gnutls_x509_privkey_t fake_privkey, root_privkey; | 594 | gnutls_x509_privkey_t fake_privkey, root_privkey; |
| 554 | gnutls_x509_crt_t dev_cert, root_cert, host_cert; | 595 | gnutls_x509_crt_t dev_cert, root_cert, host_cert; |
| 555 | 596 | ||
| 556 | gnutls_x509_privkey_init(&fake_privkey); | 597 | gnutls_x509_privkey_init(&fake_privkey); |
| 557 | gnutls_x509_crt_init(&dev_cert); | 598 | gnutls_x509_crt_init(&dev_cert); |
| 558 | gnutls_x509_crt_init(&root_cert); | 599 | gnutls_x509_crt_init(&root_cert); |
| 559 | gnutls_x509_crt_init(&host_cert); | 600 | gnutls_x509_crt_init(&host_cert); |
| 560 | 601 | ||
| 561 | if ( GNUTLS_E_SUCCESS == gnutls_x509_privkey_import_rsa_raw(fake_privkey, &modulus, &exponent, &essentially_null, &essentially_null, &essentially_null, &essentially_null) ) { | 602 | if (GNUTLS_E_SUCCESS == |
| 562 | 603 | gnutls_x509_privkey_import_rsa_raw(fake_privkey, &modulus, &exponent, &essentially_null, &essentially_null, | |
| 604 | &essentially_null, &essentially_null)) { | ||
| 605 | |||
| 563 | gnutls_x509_privkey_init(&root_privkey); | 606 | gnutls_x509_privkey_init(&root_privkey); |
| 564 | 607 | ||
| 565 | /* get root cert */ | 608 | /* get root cert */ |
| 566 | gnutls_datum_t pem_root_cert = {NULL, 0}; | 609 | gnutls_datum_t pem_root_cert = { NULL, 0 }; |
| 567 | get_root_certificate(&pem_root_cert); | 610 | get_root_certificate(&pem_root_cert); |
| 568 | if (GNUTLS_E_SUCCESS != gnutls_x509_crt_import(root_cert, &pem_root_cert, GNUTLS_X509_FMT_PEM)) | 611 | if (GNUTLS_E_SUCCESS != gnutls_x509_crt_import(root_cert, &pem_root_cert, GNUTLS_X509_FMT_PEM)) |
| 569 | ret = IPHONE_E_SSL_ERROR; | 612 | ret = IPHONE_E_SSL_ERROR; |
| 570 | 613 | ||
| 571 | /* get host cert */ | 614 | /* get host cert */ |
| 572 | gnutls_datum_t pem_host_cert = {NULL, 0}; | 615 | gnutls_datum_t pem_host_cert = { NULL, 0 }; |
| 573 | get_host_certificate(&pem_host_cert); | 616 | get_host_certificate(&pem_host_cert); |
| 574 | if (GNUTLS_E_SUCCESS != gnutls_x509_crt_import(host_cert, &pem_host_cert, GNUTLS_X509_FMT_PEM)) | 617 | if (GNUTLS_E_SUCCESS != gnutls_x509_crt_import(host_cert, &pem_host_cert, GNUTLS_X509_FMT_PEM)) |
| 575 | ret = IPHONE_E_SSL_ERROR; | 618 | ret = IPHONE_E_SSL_ERROR; |
| 576 | 619 | ||
| 577 | /* get root private key */ | 620 | /* get root private key */ |
| 578 | gnutls_datum_t pem_root_priv = {NULL, 0}; | 621 | gnutls_datum_t pem_root_priv = { NULL, 0 }; |
| 579 | get_root_private_key(&pem_root_priv); | 622 | get_root_private_key(&pem_root_priv); |
| 580 | if (GNUTLS_E_SUCCESS != gnutls_x509_privkey_import(root_privkey, &pem_root_priv, GNUTLS_X509_FMT_PEM)) | 623 | if (GNUTLS_E_SUCCESS != gnutls_x509_privkey_import(root_privkey, &pem_root_priv, GNUTLS_X509_FMT_PEM)) |
| 581 | ret = IPHONE_E_SSL_ERROR; | 624 | ret = IPHONE_E_SSL_ERROR; |
| @@ -591,7 +634,7 @@ iphone_error_t lockdownd_gen_pair_cert(char *public_key_b64, char **device_cert_ | |||
| 591 | 634 | ||
| 592 | if (IPHONE_E_SUCCESS == ret) { | 635 | if (IPHONE_E_SUCCESS == ret) { |
| 593 | /* if everything went well, export in PEM format */ | 636 | /* if everything went well, export in PEM format */ |
| 594 | gnutls_datum_t dev_pem = {NULL, 0}; | 637 | gnutls_datum_t dev_pem = { NULL, 0 }; |
| 595 | gnutls_x509_crt_export(dev_cert, GNUTLS_X509_FMT_PEM, NULL, &dev_pem.size); | 638 | gnutls_x509_crt_export(dev_cert, GNUTLS_X509_FMT_PEM, NULL, &dev_pem.size); |
| 596 | dev_pem.data = gnutls_malloc(dev_pem.size); | 639 | dev_pem.data = gnutls_malloc(dev_pem.size); |
| 597 | gnutls_x509_crt_export(dev_cert, GNUTLS_X509_FMT_PEM, dev_pem.data, &dev_pem.size); | 640 | gnutls_x509_crt_export(dev_cert, GNUTLS_X509_FMT_PEM, dev_pem.data, &dev_pem.size); |
| @@ -623,7 +666,8 @@ iphone_error_t lockdownd_gen_pair_cert(char *public_key_b64, char **device_cert_ | |||
| 623 | * | 666 | * |
| 624 | * @return 1 on success and 0 on failure | 667 | * @return 1 on success and 0 on failure |
| 625 | */ | 668 | */ |
| 626 | iphone_error_t lockdownd_start_SSL_session(iphone_lckd_client_t control, const char *HostID) { | 669 | iphone_error_t lockdownd_start_SSL_session(iphone_lckd_client_t control, const char *HostID) |
| 670 | { | ||
| 627 | xmlDocPtr plist = new_plist(); | 671 | xmlDocPtr plist = new_plist(); |
| 628 | xmlNode *dict = add_child_to_plist(plist, "dict", "\n", NULL, 0); | 672 | xmlNode *dict = add_child_to_plist(plist, "dict", "\n", NULL, 0); |
| 629 | xmlNode *key; | 673 | xmlNode *key; |
| @@ -631,46 +675,52 @@ iphone_error_t lockdownd_start_SSL_session(iphone_lckd_client_t control, const c | |||
| 631 | uint32 len = 0, bytes = 0, return_me = 0, i = 0; | 675 | uint32 len = 0, bytes = 0, return_me = 0, i = 0; |
| 632 | iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; | 676 | iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; |
| 633 | // end variables | 677 | // end variables |
| 634 | 678 | ||
| 635 | key = add_key_str_dict_element(plist, dict, "HostID", HostID, 1); | 679 | key = add_key_str_dict_element(plist, dict, "HostID", HostID, 1); |
| 636 | if (!key) { | 680 | if (!key) { |
| 637 | if (debug) printf("Couldn't add a key.\n"); | 681 | if (debug) |
| 682 | printf("Couldn't add a key.\n"); | ||
| 638 | xmlFreeDoc(plist); | 683 | xmlFreeDoc(plist); |
| 639 | return IPHONE_E_DICT_ERROR; | 684 | return IPHONE_E_DICT_ERROR; |
| 640 | } | 685 | } |
| 641 | key = add_key_str_dict_element(plist, dict, "Request", "StartSession", 1); | 686 | key = add_key_str_dict_element(plist, dict, "Request", "StartSession", 1); |
| 642 | if (!key) { | 687 | if (!key) { |
| 643 | if (debug) printf("Couldn't add a key.\n"); | 688 | if (debug) |
| 689 | printf("Couldn't add a key.\n"); | ||
| 644 | xmlFreeDoc(plist); | 690 | xmlFreeDoc(plist); |
| 645 | return IPHONE_E_DICT_ERROR; | 691 | return IPHONE_E_DICT_ERROR; |
| 646 | } | 692 | } |
| 647 | 693 | ||
| 648 | xmlDocDumpMemory(plist, (xmlChar **)&what2send, &len); | 694 | xmlDocDumpMemory(plist, (xmlChar **) & what2send, &len); |
| 649 | ret = iphone_lckd_send(control, what2send, len, &bytes); | 695 | ret = iphone_lckd_send(control, what2send, len, &bytes); |
| 650 | 696 | ||
| 651 | xmlFree(what2send); | 697 | xmlFree(what2send); |
| 652 | xmlFreeDoc(plist); | 698 | xmlFreeDoc(plist); |
| 653 | 699 | ||
| 654 | if (ret != IPHONE_E_SUCCESS) return ret; | 700 | if (ret != IPHONE_E_SUCCESS) |
| 655 | 701 | return ret; | |
| 702 | |||
| 656 | if (bytes > 0) { | 703 | if (bytes > 0) { |
| 657 | ret = iphone_lckd_recv(control, &what2send, &len); | 704 | ret = iphone_lckd_recv(control, &what2send, &len); |
| 658 | plist = xmlReadMemory(what2send, len, NULL, NULL, 0); | 705 | plist = xmlReadMemory(what2send, len, NULL, NULL, 0); |
| 659 | dict = xmlDocGetRootElement(plist); | 706 | dict = xmlDocGetRootElement(plist); |
| 660 | if (!dict) return IPHONE_E_DICT_ERROR; | 707 | if (!dict) |
| 708 | return IPHONE_E_DICT_ERROR; | ||
| 661 | for (dict = dict->children; dict; dict = dict->next) { | 709 | for (dict = dict->children; dict; dict = dict->next) { |
| 662 | if (!xmlStrcmp(dict->name, "dict")) break; | 710 | if (!xmlStrcmp(dict->name, "dict")) |
| 711 | break; | ||
| 663 | } | 712 | } |
| 664 | dictionary = read_dict_element_strings(dict); | 713 | dictionary = read_dict_element_strings(dict); |
| 665 | xmlFreeDoc(plist); | 714 | xmlFreeDoc(plist); |
| 666 | free(what2send); | 715 | free(what2send); |
| 667 | for (i = 0; dictionary[i]; i+=2) { | 716 | for (i = 0; dictionary[i]; i += 2) { |
| 668 | if (!strcmp(dictionary[i], "Result") && !strcmp(dictionary[i+1], "Success")) { | 717 | if (!strcmp(dictionary[i], "Result") && !strcmp(dictionary[i + 1], "Success")) { |
| 669 | // Set up GnuTLS... | 718 | // Set up GnuTLS... |
| 670 | //gnutls_anon_client_credentials_t anoncred; | 719 | //gnutls_anon_client_credentials_t anoncred; |
| 671 | gnutls_certificate_credentials_t xcred; | 720 | gnutls_certificate_credentials_t xcred; |
| 672 | 721 | ||
| 673 | if (debug) printf("We started the session OK, now trying GnuTLS\n"); | 722 | if (debug) |
| 723 | printf("We started the session OK, now trying GnuTLS\n"); | ||
| 674 | errno = 0; | 724 | errno = 0; |
| 675 | gnutls_global_init(); | 725 | gnutls_global_init(); |
| 676 | //gnutls_anon_allocate_client_credentials(&anoncred); | 726 | //gnutls_anon_allocate_client_credentials(&anoncred); |
| @@ -678,7 +728,7 @@ iphone_error_t lockdownd_start_SSL_session(iphone_lckd_client_t control, const c | |||
| 678 | gnutls_certificate_set_x509_trust_file(xcred, "hostcert.pem", GNUTLS_X509_FMT_PEM); | 728 | gnutls_certificate_set_x509_trust_file(xcred, "hostcert.pem", GNUTLS_X509_FMT_PEM); |
| 679 | gnutls_init(control->ssl_session, GNUTLS_CLIENT); | 729 | gnutls_init(control->ssl_session, GNUTLS_CLIENT); |
| 680 | { | 730 | { |
| 681 | int protocol_priority[16] = {GNUTLS_SSL3, 0 }; | 731 | int protocol_priority[16] = { GNUTLS_SSL3, 0 }; |
| 682 | int kx_priority[16] = { GNUTLS_KX_ANON_DH, GNUTLS_KX_RSA, 0 }; | 732 | int kx_priority[16] = { GNUTLS_KX_ANON_DH, GNUTLS_KX_RSA, 0 }; |
| 683 | int cipher_priority[16] = { GNUTLS_CIPHER_AES_128_CBC, GNUTLS_CIPHER_AES_256_CBC, 0 }; | 733 | int cipher_priority[16] = { GNUTLS_CIPHER_AES_128_CBC, GNUTLS_CIPHER_AES_256_CBC, 0 }; |
| 684 | int mac_priority[16] = { GNUTLS_MAC_SHA1, GNUTLS_MAC_MD5, 0 }; | 734 | int mac_priority[16] = { GNUTLS_MAC_SHA1, GNUTLS_MAC_MD5, 0 }; |
| @@ -687,30 +737,38 @@ iphone_error_t lockdownd_start_SSL_session(iphone_lckd_client_t control, const c | |||
| 687 | gnutls_cipher_set_priority(*control->ssl_session, cipher_priority); | 737 | gnutls_cipher_set_priority(*control->ssl_session, cipher_priority); |
| 688 | gnutls_compression_set_priority(*control->ssl_session, comp_priority); | 738 | gnutls_compression_set_priority(*control->ssl_session, comp_priority); |
| 689 | gnutls_kx_set_priority(*control->ssl_session, kx_priority); | 739 | gnutls_kx_set_priority(*control->ssl_session, kx_priority); |
| 690 | gnutls_protocol_set_priority( *control->ssl_session, protocol_priority); | 740 | gnutls_protocol_set_priority(*control->ssl_session, protocol_priority); |
| 691 | gnutls_mac_set_priority(*control->ssl_session, mac_priority); | 741 | gnutls_mac_set_priority(*control->ssl_session, mac_priority); |
| 692 | 742 | ||
| 693 | } | 743 | } |
| 694 | gnutls_credentials_set(*control->ssl_session, GNUTLS_CRD_CERTIFICATE, xcred); // this part is killing me. | 744 | gnutls_credentials_set(*control->ssl_session, GNUTLS_CRD_CERTIFICATE, xcred); // this part is killing me. |
| 695 | 745 | ||
| 696 | if (debug) printf("GnuTLS step 1...\n"); | 746 | if (debug) |
| 747 | printf("GnuTLS step 1...\n"); | ||
| 697 | gnutls_transport_set_ptr(*control->ssl_session, (gnutls_transport_ptr_t) control); | 748 | gnutls_transport_set_ptr(*control->ssl_session, (gnutls_transport_ptr_t) control); |
| 698 | if (debug) printf("GnuTLS step 2...\n"); | 749 | if (debug) |
| 699 | gnutls_transport_set_push_function(*control->ssl_session, (gnutls_push_func)&lockdownd_secuwrite); | 750 | printf("GnuTLS step 2...\n"); |
| 700 | if (debug) printf("GnuTLS step 3...\n"); | 751 | gnutls_transport_set_push_function(*control->ssl_session, (gnutls_push_func) & lockdownd_secuwrite); |
| 701 | gnutls_transport_set_pull_function(*control->ssl_session, (gnutls_pull_func)&lockdownd_securead); | 752 | if (debug) |
| 702 | if (debug) printf("GnuTLS step 4 -- now handshaking...\n"); | 753 | printf("GnuTLS step 3...\n"); |
| 703 | 754 | gnutls_transport_set_pull_function(*control->ssl_session, (gnutls_pull_func) & lockdownd_securead); | |
| 704 | if (errno && debug) printf("WARN: errno says %s before handshake!\n", strerror(errno)); | 755 | if (debug) |
| 756 | printf("GnuTLS step 4 -- now handshaking...\n"); | ||
| 757 | |||
| 758 | if (errno && debug) | ||
| 759 | printf("WARN: errno says %s before handshake!\n", strerror(errno)); | ||
| 705 | return_me = gnutls_handshake(*control->ssl_session); | 760 | return_me = gnutls_handshake(*control->ssl_session); |
| 706 | if (debug) printf("GnuTLS handshake done...\n"); | 761 | if (debug) |
| 707 | 762 | printf("GnuTLS handshake done...\n"); | |
| 763 | |||
| 708 | free_dictionary(dictionary); | 764 | free_dictionary(dictionary); |
| 709 | 765 | ||
| 710 | if (return_me != GNUTLS_E_SUCCESS) { | 766 | if (return_me != GNUTLS_E_SUCCESS) { |
| 711 | if (debug) printf("GnuTLS reported something wrong.\n"); | 767 | if (debug) |
| 768 | printf("GnuTLS reported something wrong.\n"); | ||
| 712 | gnutls_perror(return_me); | 769 | gnutls_perror(return_me); |
| 713 | if (debug) printf("oh.. errno says %s\n", strerror(errno)); | 770 | if (debug) |
| 771 | printf("oh.. errno says %s\n", strerror(errno)); | ||
| 714 | return IPHONE_E_SSL_ERROR; | 772 | return IPHONE_E_SSL_ERROR; |
| 715 | } else { | 773 | } else { |
| 716 | control->in_SSL = 1; | 774 | control->in_SSL = 1; |
| @@ -718,19 +776,20 @@ iphone_error_t lockdownd_start_SSL_session(iphone_lckd_client_t control, const c | |||
| 718 | } | 776 | } |
| 719 | } | 777 | } |
| 720 | } | 778 | } |
| 721 | 779 | ||
| 722 | if (debug) { | 780 | if (debug) { |
| 723 | printf("Apparently failed negotiating with lockdownd.\n"); | 781 | printf("Apparently failed negotiating with lockdownd.\n"); |
| 724 | printf("Responding dictionary: \n"); | 782 | printf("Responding dictionary: \n"); |
| 725 | for (i = 0; dictionary[i]; i+=2) { | 783 | for (i = 0; dictionary[i]; i += 2) { |
| 726 | printf("\t%s: %s\n", dictionary[i], dictionary[i+1]); | 784 | printf("\t%s: %s\n", dictionary[i], dictionary[i + 1]); |
| 727 | } | 785 | } |
| 728 | } | 786 | } |
| 729 | 787 | ||
| 730 | free_dictionary(dictionary); | 788 | free_dictionary(dictionary); |
| 731 | return IPHONE_E_SSL_ERROR; | 789 | return IPHONE_E_SSL_ERROR; |
| 732 | } else { | 790 | } else { |
| 733 | if (debug) printf("Didn't get enough bytes.\n"); | 791 | if (debug) |
| 792 | printf("Didn't get enough bytes.\n"); | ||
| 734 | return IPHONE_E_NOT_ENOUGH_DATA; | 793 | return IPHONE_E_NOT_ENOUGH_DATA; |
| 735 | } | 794 | } |
| 736 | } | 795 | } |
| @@ -743,14 +802,18 @@ iphone_error_t lockdownd_start_SSL_session(iphone_lckd_client_t control, const c | |||
| 743 | * | 802 | * |
| 744 | * @return The number of bytes sent | 803 | * @return The number of bytes sent |
| 745 | */ | 804 | */ |
| 746 | ssize_t lockdownd_secuwrite(gnutls_transport_ptr_t transport, char *buffer, size_t length) { | 805 | ssize_t lockdownd_secuwrite(gnutls_transport_ptr_t transport, char *buffer, size_t length) |
| 806 | { | ||
| 747 | int bytes = 0; | 807 | int bytes = 0; |
| 748 | iphone_lckd_client_t control; | 808 | iphone_lckd_client_t control; |
| 749 | control = (iphone_lckd_client_t)transport; | 809 | control = (iphone_lckd_client_t) transport; |
| 750 | if (debug) printf("lockdownd_secuwrite() called\n"); | 810 | if (debug) |
| 751 | if (debug) printf("pre-send\nlength = %zi\n", length); | 811 | printf("lockdownd_secuwrite() called\n"); |
| 812 | if (debug) | ||
| 813 | printf("pre-send\nlength = %zi\n", length); | ||
| 752 | iphone_mux_send(control->connection, buffer, length, &bytes); | 814 | iphone_mux_send(control->connection, buffer, length, &bytes); |
| 753 | if (debug) printf("post-send\nsent %i bytes\n", bytes); | 815 | if (debug) |
| 816 | printf("post-send\nsent %i bytes\n", bytes); | ||
| 754 | if (debug) { | 817 | if (debug) { |
| 755 | FILE *my_ssl_packet = fopen("sslpacketwrite.out", "w+"); | 818 | FILE *my_ssl_packet = fopen("sslpacketwrite.out", "w+"); |
| 756 | fwrite(buffer, 1, length, my_ssl_packet); | 819 | fwrite(buffer, 1, length, my_ssl_packet); |
| @@ -758,7 +821,7 @@ ssize_t lockdownd_secuwrite(gnutls_transport_ptr_t transport, char *buffer, size | |||
| 758 | printf("Wrote SSL packet to drive, too.\n"); | 821 | printf("Wrote SSL packet to drive, too.\n"); |
| 759 | fclose(my_ssl_packet); | 822 | fclose(my_ssl_packet); |
| 760 | } | 823 | } |
| 761 | 824 | ||
| 762 | return bytes; | 825 | return bytes; |
| 763 | } | 826 | } |
| 764 | 827 | ||
| @@ -770,68 +833,86 @@ ssize_t lockdownd_secuwrite(gnutls_transport_ptr_t transport, char *buffer, size | |||
| 770 | * | 833 | * |
| 771 | * @return The number of bytes read | 834 | * @return The number of bytes read |
| 772 | */ | 835 | */ |
| 773 | ssize_t lockdownd_securead(gnutls_transport_ptr_t transport, char *buffer, size_t length) { | 836 | ssize_t lockdownd_securead(gnutls_transport_ptr_t transport, char *buffer, size_t length) |
| 837 | { | ||
| 774 | int bytes = 0, pos_start_fill = 0; | 838 | int bytes = 0, pos_start_fill = 0; |
| 775 | char *hackhackhack = NULL; | 839 | char *hackhackhack = NULL; |
| 776 | iphone_lckd_client_t control; | 840 | iphone_lckd_client_t control; |
| 777 | control = (iphone_lckd_client_t)transport; | 841 | control = (iphone_lckd_client_t) transport; |
| 778 | if (debug) printf("lockdownd_securead() called\nlength = %zi\n", length); | 842 | if (debug) |
| 843 | printf("lockdownd_securead() called\nlength = %zi\n", length); | ||
| 779 | // Buffering hack! Throw what we've got in our "buffer" into the stream first, then get more. | 844 | // Buffering hack! Throw what we've got in our "buffer" into the stream first, then get more. |
| 780 | if (control->gtls_buffer_hack_len > 0) { | 845 | if (control->gtls_buffer_hack_len > 0) { |
| 781 | if (length > control->gtls_buffer_hack_len) { // If it's asking for more than we got | 846 | if (length > control->gtls_buffer_hack_len) { // If it's asking for more than we got |
| 782 | length -= control->gtls_buffer_hack_len; // Subtract what we have from their requested length | 847 | length -= control->gtls_buffer_hack_len; // Subtract what we have from their requested length |
| 783 | pos_start_fill = control->gtls_buffer_hack_len; // set the pos to start filling at | 848 | pos_start_fill = control->gtls_buffer_hack_len; // set the pos to start filling at |
| 784 | memcpy(buffer, control->gtls_buffer_hack, control->gtls_buffer_hack_len); // Fill their buffer partially | 849 | memcpy(buffer, control->gtls_buffer_hack, control->gtls_buffer_hack_len); // Fill their buffer partially |
| 785 | free(control->gtls_buffer_hack); // free our memory, it's not chained anymore | 850 | free(control->gtls_buffer_hack); // free our memory, it's not chained anymore |
| 786 | control->gtls_buffer_hack_len = 0; // we don't have a hack buffer anymore | 851 | control->gtls_buffer_hack_len = 0; // we don't have a hack buffer anymore |
| 787 | if (debug) printf("Did a partial fill to help quench thirst for data\n"); | 852 | if (debug) |
| 788 | } else if (length < control->gtls_buffer_hack_len) { // If it's asking for less... | 853 | printf("Did a partial fill to help quench thirst for data\n"); |
| 789 | control->gtls_buffer_hack_len -= length; // subtract what they're asking for | 854 | } else if (length < control->gtls_buffer_hack_len) { // If it's asking for less... |
| 790 | memcpy(buffer, control->gtls_buffer_hack, length); // fill their buffer | 855 | control->gtls_buffer_hack_len -= length; // subtract what they're asking for |
| 791 | hackhackhack = (char*)malloc(sizeof(char) * control->gtls_buffer_hack_len); // strndup is NOT a good solution -- concatenates \0!!!! Anyway, make a new "hack" buffer. | 856 | memcpy(buffer, control->gtls_buffer_hack, length); // fill their buffer |
| 792 | memcpy(hackhackhack, control->gtls_buffer_hack+length, control->gtls_buffer_hack_len); // Move what's left into the new one | 857 | hackhackhack = (char *) malloc(sizeof(char) * control->gtls_buffer_hack_len); // strndup is NOT a good solution -- concatenates \0!!!! Anyway, make a new "hack" buffer. |
| 793 | free(control->gtls_buffer_hack); // Free the old one | 858 | memcpy(hackhackhack, control->gtls_buffer_hack + length, control->gtls_buffer_hack_len); // Move what's left into the new one |
| 794 | control->gtls_buffer_hack = hackhackhack; // And make it the new one. | 859 | free(control->gtls_buffer_hack); // Free the old one |
| 795 | hackhackhack = NULL; | 860 | control->gtls_buffer_hack = hackhackhack; // And make it the new one. |
| 796 | if (debug) printf("Quenched the thirst for data; new hack length is %i\n", control->gtls_buffer_hack_len); | 861 | hackhackhack = NULL; |
| 797 | return length; // hand it over. | 862 | if (debug) |
| 798 | } else { // length == hack length | 863 | printf("Quenched the thirst for data; new hack length is %i\n", control->gtls_buffer_hack_len); |
| 799 | memcpy(buffer, control->gtls_buffer_hack, length); // copy our buffer into theirs | 864 | return length; // hand it over. |
| 800 | free(control->gtls_buffer_hack); // free our "obligation" | 865 | } else { // length == hack length |
| 801 | control->gtls_buffer_hack_len = 0; // free our "obligation" | 866 | memcpy(buffer, control->gtls_buffer_hack, length); // copy our buffer into theirs |
| 802 | if (debug) printf("Satiated the thirst for data; now we have to eventually receive again.\n"); | 867 | free(control->gtls_buffer_hack); // free our "obligation" |
| 803 | return length; // hand it over | 868 | control->gtls_buffer_hack_len = 0; // free our "obligation" |
| 869 | if (debug) | ||
| 870 | printf("Satiated the thirst for data; now we have to eventually receive again.\n"); | ||
| 871 | return length; // hand it over | ||
| 804 | } | 872 | } |
| 805 | } | 873 | } |
| 806 | // End buffering hack! | 874 | // End buffering hack! |
| 807 | char *recv_buffer = (char*)malloc(sizeof(char) * (length * 1000)); // ensuring nothing stupid happens | 875 | char *recv_buffer = (char *) malloc(sizeof(char) * (length * 1000)); // ensuring nothing stupid happens |
| 808 | 876 | ||
| 809 | if (debug) printf("pre-read\nclient wants %zi bytes\n", length); | 877 | if (debug) |
| 878 | printf("pre-read\nclient wants %zi bytes\n", length); | ||
| 810 | iphone_mux_recv(control->connection, recv_buffer, (length * 1000), &bytes); | 879 | iphone_mux_recv(control->connection, recv_buffer, (length * 1000), &bytes); |
| 811 | if (debug) printf("post-read\nwe got %i bytes\n", bytes); | 880 | if (debug) |
| 881 | printf("post-read\nwe got %i bytes\n", bytes); | ||
| 812 | if (debug && bytes < 0) { | 882 | if (debug && bytes < 0) { |
| 813 | printf("lockdownd_securead(): uh oh\n"); | 883 | printf("lockdownd_securead(): uh oh\n"); |
| 814 | printf("I believe what we have here is a failure to communicate... libusb says %s but strerror says %s\n", usb_strerror(), strerror(errno)); | 884 | printf("I believe what we have here is a failure to communicate... libusb says %s but strerror says %s\n", |
| 815 | return bytes + 28; // an errno | 885 | usb_strerror(), strerror(errno)); |
| 886 | return bytes + 28; // an errno | ||
| 816 | } | 887 | } |
| 817 | if (bytes >= length) { | 888 | if (bytes >= length) { |
| 818 | if (bytes > length) { | 889 | if (bytes > length) { |
| 819 | if (debug) printf("lockdownd_securead: Client deliberately read less data than was there; resorting to GnuTLS buffering hack.\n"); | 890 | if (debug) |
| 820 | if (!control->gtls_buffer_hack_len) { // if there's no hack buffer yet | 891 | printf |
| 892 | ("lockdownd_securead: Client deliberately read less data than was there; resorting to GnuTLS buffering hack.\n"); | ||
| 893 | if (!control->gtls_buffer_hack_len) { // if there's no hack buffer yet | ||
| 821 | //control->gtls_buffer_hack = strndup(recv_buffer+length, bytes-length); // strndup is NOT a good solution! | 894 | //control->gtls_buffer_hack = strndup(recv_buffer+length, bytes-length); // strndup is NOT a good solution! |
| 822 | control->gtls_buffer_hack_len += bytes-length; | 895 | control->gtls_buffer_hack_len += bytes - length; |
| 823 | control->gtls_buffer_hack = (char*)malloc(sizeof(char) * control->gtls_buffer_hack_len); | 896 | control->gtls_buffer_hack = (char *) malloc(sizeof(char) * control->gtls_buffer_hack_len); |
| 824 | memcpy(control->gtls_buffer_hack, recv_buffer+length, control->gtls_buffer_hack_len); | 897 | memcpy(control->gtls_buffer_hack, recv_buffer + length, control->gtls_buffer_hack_len); |
| 825 | } else { // if there is. | 898 | } else { // if there is. |
| 826 | control->gtls_buffer_hack = realloc(control->gtls_buffer_hack, control->gtls_buffer_hack_len + (bytes - length)); | 899 | control->gtls_buffer_hack = |
| 827 | memcpy(control->gtls_buffer_hack+control->gtls_buffer_hack_len, recv_buffer+length, bytes-length); | 900 | realloc(control->gtls_buffer_hack, control->gtls_buffer_hack_len + (bytes - length)); |
| 901 | memcpy(control->gtls_buffer_hack + control->gtls_buffer_hack_len, recv_buffer + length, bytes - length); | ||
| 828 | control->gtls_buffer_hack_len += bytes - length; | 902 | control->gtls_buffer_hack_len += bytes - length; |
| 829 | } | 903 | } |
| 830 | } | 904 | } |
| 831 | memcpy(buffer+pos_start_fill, recv_buffer, length); | 905 | memcpy(buffer + pos_start_fill, recv_buffer, length); |
| 832 | free(recv_buffer); | 906 | free(recv_buffer); |
| 833 | if (bytes == length) { if (debug) printf("Returning how much we received.\n"); return bytes; } | 907 | if (bytes == length) { |
| 834 | else { if (debug) printf("Returning what they want to hear.\nHack length: %i\n", control->gtls_buffer_hack_len); return length; } | 908 | if (debug) |
| 909 | printf("Returning how much we received.\n"); | ||
| 910 | return bytes; | ||
| 911 | } else { | ||
| 912 | if (debug) | ||
| 913 | printf("Returning what they want to hear.\nHack length: %i\n", control->gtls_buffer_hack_len); | ||
| 914 | return length; | ||
| 915 | } | ||
| 835 | } | 916 | } |
| 836 | return bytes; | 917 | return bytes; |
| 837 | } | 918 | } |
| @@ -843,12 +924,16 @@ ssize_t lockdownd_securead(gnutls_transport_ptr_t transport, char *buffer, size_ | |||
| 843 | * | 924 | * |
| 844 | * @return The port number the service was started on or 0 on failure. | 925 | * @return The port number the service was started on or 0 on failure. |
| 845 | */ | 926 | */ |
| 846 | iphone_error_t iphone_lckd_start_service ( iphone_lckd_client_t client, const char *service, int *port ) { | 927 | iphone_error_t iphone_lckd_start_service(iphone_lckd_client_t client, const char *service, int *port) |
| 847 | if (!client || !service || !port) return IPHONE_E_INVALID_ARG; | 928 | { |
| 929 | if (!client || !service || !port) | ||
| 930 | return IPHONE_E_INVALID_ARG; | ||
| 848 | 931 | ||
| 849 | char* host_id = get_host_id(); | 932 | char *host_id = get_host_id(); |
| 850 | if (!host_id) return IPHONE_E_INVALID_CONF; | 933 | if (!host_id) |
| 851 | if (!client->in_SSL && !lockdownd_start_SSL_session(client, host_id)) return IPHONE_E_SSL_ERROR; | 934 | return IPHONE_E_INVALID_CONF; |
| 935 | if (!client->in_SSL && !lockdownd_start_SSL_session(client, host_id)) | ||
| 936 | return IPHONE_E_SSL_ERROR; | ||
| 852 | 937 | ||
| 853 | char *XML_query, **dictionary; | 938 | char *XML_query, **dictionary; |
| 854 | uint32 length, i = 0, port_loc = 0, bytes = 0; | 939 | uint32 length, i = 0, port_loc = 0, bytes = 0; |
| @@ -862,63 +947,78 @@ iphone_error_t iphone_lckd_start_service ( iphone_lckd_client_t client, const ch | |||
| 862 | xmlNode *dict = add_child_to_plist(plist, "dict", "\n", NULL, 0); | 947 | xmlNode *dict = add_child_to_plist(plist, "dict", "\n", NULL, 0); |
| 863 | xmlNode *key; | 948 | xmlNode *key; |
| 864 | key = add_key_str_dict_element(plist, dict, "Request", "StartService", 1); | 949 | key = add_key_str_dict_element(plist, dict, "Request", "StartService", 1); |
| 865 | if (!key) { xmlFreeDoc(plist); return IPHONE_E_UNKNOWN_ERROR; } | 950 | if (!key) { |
| 951 | xmlFreeDoc(plist); | ||
| 952 | return IPHONE_E_UNKNOWN_ERROR; | ||
| 953 | } | ||
| 866 | key = add_key_str_dict_element(plist, dict, "Service", service, 1); | 954 | key = add_key_str_dict_element(plist, dict, "Service", service, 1); |
| 867 | if (!key) { xmlFreeDoc(plist); return IPHONE_E_UNKNOWN_ERROR; } | 955 | if (!key) { |
| 868 | 956 | xmlFreeDoc(plist); | |
| 869 | xmlDocDumpMemory(plist, (xmlChar **)&XML_query, &length); | 957 | return IPHONE_E_UNKNOWN_ERROR; |
| 870 | 958 | } | |
| 959 | |||
| 960 | xmlDocDumpMemory(plist, (xmlChar **) & XML_query, &length); | ||
| 961 | |||
| 871 | ret = iphone_lckd_send(client, XML_query, length, &bytes); | 962 | ret = iphone_lckd_send(client, XML_query, length, &bytes); |
| 872 | free(XML_query); | 963 | free(XML_query); |
| 873 | if (IPHONE_E_SUCCESS != ret) return ret; | 964 | if (IPHONE_E_SUCCESS != ret) |
| 874 | 965 | return ret; | |
| 966 | |||
| 875 | ret = iphone_lckd_recv(client, &XML_query, &bytes); | 967 | ret = iphone_lckd_recv(client, &XML_query, &bytes); |
| 876 | xmlFreeDoc(plist); | 968 | xmlFreeDoc(plist); |
| 877 | if (IPHONE_E_SUCCESS != ret) return ret; | 969 | if (IPHONE_E_SUCCESS != ret) |
| 878 | 970 | return ret; | |
| 879 | if (bytes <= 0) return IPHONE_E_NOT_ENOUGH_DATA; | 971 | |
| 972 | if (bytes <= 0) | ||
| 973 | return IPHONE_E_NOT_ENOUGH_DATA; | ||
| 880 | else { | 974 | else { |
| 881 | plist = xmlReadMemory(XML_query, bytes, NULL, NULL, 0); | 975 | plist = xmlReadMemory(XML_query, bytes, NULL, NULL, 0); |
| 882 | if (!plist) return IPHONE_E_UNKNOWN_ERROR; | 976 | if (!plist) |
| 977 | return IPHONE_E_UNKNOWN_ERROR; | ||
| 883 | dict = xmlDocGetRootElement(plist); | 978 | dict = xmlDocGetRootElement(plist); |
| 884 | if (!dict) return IPHONE_E_UNKNOWN_ERROR; | 979 | if (!dict) |
| 980 | return IPHONE_E_UNKNOWN_ERROR; | ||
| 885 | for (dict = dict->children; dict; dict = dict->next) { | 981 | for (dict = dict->children; dict; dict = dict->next) { |
| 886 | if (!xmlStrcmp(dict->name, "dict")) break; | 982 | if (!xmlStrcmp(dict->name, "dict")) |
| 983 | break; | ||
| 887 | } | 984 | } |
| 888 | 985 | ||
| 889 | if (!dict) return IPHONE_E_UNKNOWN_ERROR; | 986 | if (!dict) |
| 987 | return IPHONE_E_UNKNOWN_ERROR; | ||
| 890 | dictionary = read_dict_element_strings(dict); | 988 | dictionary = read_dict_element_strings(dict); |
| 891 | 989 | ||
| 892 | for (i = 0; dictionary[i]; i+=2) { | 990 | for (i = 0; dictionary[i]; i += 2) { |
| 893 | if (debug) printf("lockdownd_start_service() dictionary %s: %s\n", dictionary[i], dictionary[i+1]); | 991 | if (debug) |
| 894 | 992 | printf("lockdownd_start_service() dictionary %s: %s\n", dictionary[i], dictionary[i + 1]); | |
| 993 | |||
| 895 | if (!xmlStrcmp(dictionary[i], "Port")) { | 994 | if (!xmlStrcmp(dictionary[i], "Port")) { |
| 896 | port_loc = atoi(dictionary[i+1]); | 995 | port_loc = atoi(dictionary[i + 1]); |
| 897 | if (debug) printf("lockdownd_start_service() atoi'd port: %i\n", port); | 996 | if (debug) |
| 997 | printf("lockdownd_start_service() atoi'd port: %i\n", port); | ||
| 898 | } | 998 | } |
| 899 | 999 | ||
| 900 | if (!xmlStrcmp(dictionary[i], "Result")) { | 1000 | if (!xmlStrcmp(dictionary[i], "Result")) { |
| 901 | if (!xmlStrcmp(dictionary[i+1], "Success")) { | 1001 | if (!xmlStrcmp(dictionary[i + 1], "Success")) { |
| 902 | result = 1; | 1002 | result = 1; |
| 903 | } | 1003 | } |
| 904 | } | 1004 | } |
| 905 | } | 1005 | } |
| 906 | 1006 | ||
| 907 | if (debug) { | 1007 | if (debug) { |
| 908 | printf("lockdownd_start_service(): DATA RECEIVED:\n\n"); | 1008 | printf("lockdownd_start_service(): DATA RECEIVED:\n\n"); |
| 909 | fwrite(XML_query, 1, bytes, stdout); | 1009 | fwrite(XML_query, 1, bytes, stdout); |
| 910 | printf("end data received by lockdownd_start_service()\n"); | 1010 | printf("end data received by lockdownd_start_service()\n"); |
| 911 | } | 1011 | } |
| 912 | 1012 | ||
| 913 | free(XML_query); | 1013 | free(XML_query); |
| 914 | xmlFreeDoc(plist); | 1014 | xmlFreeDoc(plist); |
| 915 | free_dictionary(dictionary); | 1015 | free_dictionary(dictionary); |
| 916 | if (port && result) { | 1016 | if (port && result) { |
| 917 | *port = port_loc; | 1017 | *port = port_loc; |
| 918 | return IPHONE_E_SUCCESS; | 1018 | return IPHONE_E_SUCCESS; |
| 919 | } | 1019 | } else |
| 920 | else return IPHONE_E_UNKNOWN_ERROR; | 1020 | return IPHONE_E_UNKNOWN_ERROR; |
| 921 | } | 1021 | } |
| 922 | 1022 | ||
| 923 | return IPHONE_E_UNKNOWN_ERROR; | 1023 | return IPHONE_E_UNKNOWN_ERROR; |
| 924 | } | 1024 | } |
diff --git a/src/lockdown.h b/src/lockdown.h index 62c453f..41402c4 100644 --- a/src/lockdown.h +++ b/src/lockdown.h | |||
| @@ -47,7 +47,8 @@ iphone_error_t lockdownd_hello(iphone_lckd_client_t control); | |||
| 47 | iphone_error_t lockdownd_get_device_uid(iphone_lckd_client_t control, char **uid); | 47 | iphone_error_t lockdownd_get_device_uid(iphone_lckd_client_t control, char **uid); |
| 48 | iphone_error_t lockdownd_get_device_public_key(iphone_lckd_client_t control, char **public_key); | 48 | iphone_error_t lockdownd_get_device_public_key(iphone_lckd_client_t control, char **public_key); |
| 49 | 49 | ||
| 50 | iphone_error_t lockdownd_gen_pair_cert(char *public_key_b64, char **device_cert_b64, char **host_cert_b64, char **root_cert_b64); | 50 | iphone_error_t lockdownd_gen_pair_cert(char *public_key_b64, char **device_cert_b64, char **host_cert_b64, |
| 51 | char **root_cert_b64); | ||
| 51 | iphone_error_t lockdownd_pair_device(iphone_lckd_client_t control, char *public_key, char *host_id); | 52 | iphone_error_t lockdownd_pair_device(iphone_lckd_client_t control, char *public_key, char *host_id); |
| 52 | void lockdownd_close(iphone_lckd_client_t control); | 53 | void lockdownd_close(iphone_lckd_client_t control); |
| 53 | 54 | ||
| @@ -34,23 +34,24 @@ | |||
| 34 | 34 | ||
| 35 | int debug = 1; | 35 | int debug = 1; |
| 36 | 36 | ||
| 37 | int main(int argc, char *argv[]) { | 37 | int main(int argc, char *argv[]) |
| 38 | { | ||
| 38 | int bytes = 0, port = 0, i = 0; | 39 | int bytes = 0, port = 0, i = 0; |
| 39 | iphone_lckd_client_t control = NULL; | 40 | iphone_lckd_client_t control = NULL; |
| 40 | iphone_device_t phone = NULL; | 41 | iphone_device_t phone = NULL; |
| 41 | 42 | ||
| 42 | if (argc > 1 && !strcasecmp(argv[1], "--debug")){ | 43 | if (argc > 1 && !strcasecmp(argv[1], "--debug")) { |
| 43 | debug = 1; | 44 | debug = 1; |
| 44 | } else { | 45 | } else { |
| 45 | debug = 0; | 46 | debug = 0; |
| 46 | } | 47 | } |
| 47 | 48 | ||
| 48 | if (IPHONE_E_SUCCESS != iphone_get_device(&phone)) { | 49 | if (IPHONE_E_SUCCESS != iphone_get_device(&phone)) { |
| 49 | printf("No iPhone found, is it plugged in?\n"); | 50 | printf("No iPhone found, is it plugged in?\n"); |
| 50 | return -1; | 51 | return -1; |
| 51 | } | 52 | } |
| 52 | 53 | ||
| 53 | if (IPHONE_E_SUCCESS != iphone_lckd_new_client(phone, &control)){ | 54 | if (IPHONE_E_SUCCESS != iphone_lckd_new_client(phone, &control)) { |
| 54 | iphone_free_device(phone); | 55 | iphone_free_device(phone); |
| 55 | return -1; | 56 | return -1; |
| 56 | } | 57 | } |
| @@ -62,34 +63,36 @@ int main(int argc, char *argv[]) { | |||
| 62 | } | 63 | } |
| 63 | 64 | ||
| 64 | iphone_lckd_start_service(control, "com.apple.afc", &port); | 65 | iphone_lckd_start_service(control, "com.apple.afc", &port); |
| 65 | 66 | ||
| 66 | if (port) { | 67 | if (port) { |
| 67 | iphone_afc_client_t afc = NULL; | 68 | iphone_afc_client_t afc = NULL; |
| 68 | iphone_afc_new_client(phone, 3432, port, &afc); | 69 | iphone_afc_new_client(phone, 3432, port, &afc); |
| 69 | if (afc) { | 70 | if (afc) { |
| 70 | char **dirs = NULL; | 71 | char **dirs = NULL; |
| 71 | iphone_afc_get_dir_list(afc, "/eafaedf", &dirs); | 72 | iphone_afc_get_dir_list(afc, "/eafaedf", &dirs); |
| 72 | if (!dirs) iphone_afc_get_dir_list(afc, "/", &dirs); | 73 | if (!dirs) |
| 74 | iphone_afc_get_dir_list(afc, "/", &dirs); | ||
| 73 | printf("Directory time.\n"); | 75 | printf("Directory time.\n"); |
| 74 | for (i = 0; dirs[i]; i++) { | 76 | for (i = 0; dirs[i]; i++) { |
| 75 | printf("/%s\n", dirs[i]); | 77 | printf("/%s\n", dirs[i]); |
| 76 | } | 78 | } |
| 77 | 79 | ||
| 78 | g_strfreev(dirs); | 80 | g_strfreev(dirs); |
| 79 | iphone_afc_get_devinfo(afc, &dirs); | 81 | iphone_afc_get_devinfo(afc, &dirs); |
| 80 | if (dirs) { | 82 | if (dirs) { |
| 81 | for (i = 0; dirs[i]; i+=2) { | 83 | for (i = 0; dirs[i]; i += 2) { |
| 82 | printf("%s: %s\n", dirs[i], dirs[i+1]); | 84 | printf("%s: %s\n", dirs[i], dirs[i + 1]); |
| 83 | } | 85 | } |
| 84 | } | 86 | } |
| 85 | g_strfreev(dirs); | 87 | g_strfreev(dirs); |
| 86 | 88 | ||
| 87 | iphone_afc_file_t my_file = NULL; | 89 | iphone_afc_file_t my_file = NULL; |
| 88 | struct stat stbuf; | 90 | struct stat stbuf; |
| 89 | iphone_afc_get_file_attr ( afc, "/iTunesOnTheGoPlaylist.plist", &stbuf ); | 91 | iphone_afc_get_file_attr(afc, "/iTunesOnTheGoPlaylist.plist", &stbuf); |
| 90 | if (IPHONE_E_SUCCESS == iphone_afc_open_file(afc, "/iTunesOnTheGoPlaylist.plist", IPHONE_AFC_FILE_READ, &my_file) && my_file) { | 92 | if (IPHONE_E_SUCCESS == |
| 93 | iphone_afc_open_file(afc, "/iTunesOnTheGoPlaylist.plist", IPHONE_AFC_FILE_READ, &my_file) && my_file) { | ||
| 91 | printf("A file size: %i\n", stbuf.st_size); | 94 | printf("A file size: %i\n", stbuf.st_size); |
| 92 | char *file_data = (char*)malloc(sizeof(char) * stbuf.st_size); | 95 | char *file_data = (char *) malloc(sizeof(char) * stbuf.st_size); |
| 93 | iphone_afc_read_file(afc, my_file, file_data, stbuf.st_size, &bytes); | 96 | iphone_afc_read_file(afc, my_file, file_data, stbuf.st_size, &bytes); |
| 94 | if (bytes >= 0) { | 97 | if (bytes >= 0) { |
| 95 | printf("The file's data:\n"); | 98 | printf("The file's data:\n"); |
| @@ -98,49 +101,58 @@ int main(int argc, char *argv[]) { | |||
| 98 | printf("\nClosing my file.\n"); | 101 | printf("\nClosing my file.\n"); |
| 99 | iphone_afc_close_file(afc, my_file); | 102 | iphone_afc_close_file(afc, my_file); |
| 100 | free(file_data); | 103 | free(file_data); |
| 101 | } else printf("couldn't open a file\n"); | 104 | } else |
| 102 | 105 | printf("couldn't open a file\n"); | |
| 106 | |||
| 103 | iphone_afc_open_file(afc, "/readme.libiphone.fx", IPHONE_AFC_FILE_WRITE, &my_file); | 107 | iphone_afc_open_file(afc, "/readme.libiphone.fx", IPHONE_AFC_FILE_WRITE, &my_file); |
| 104 | if (my_file) { | 108 | if (my_file) { |
| 105 | char *outdatafile = strdup("this is a bitchin text file\n"); | 109 | char *outdatafile = strdup("this is a bitchin text file\n"); |
| 106 | iphone_afc_write_file(afc, my_file, outdatafile, strlen(outdatafile), &bytes); | 110 | iphone_afc_write_file(afc, my_file, outdatafile, strlen(outdatafile), &bytes); |
| 107 | free(outdatafile); | 111 | free(outdatafile); |
| 108 | if (bytes > 0) printf("Wrote a surprise. ;)\n"); | 112 | if (bytes > 0) |
| 109 | else printf("I wanted to write a surprise, but... :(\n"); | 113 | printf("Wrote a surprise. ;)\n"); |
| 114 | else | ||
| 115 | printf("I wanted to write a surprise, but... :(\n"); | ||
| 110 | iphone_afc_close_file(afc, my_file); | 116 | iphone_afc_close_file(afc, my_file); |
| 111 | } | 117 | } |
| 112 | printf("Deleting a file...\n"); | 118 | printf("Deleting a file...\n"); |
| 113 | bytes = iphone_afc_delete_file(afc, "/delme"); | 119 | bytes = iphone_afc_delete_file(afc, "/delme"); |
| 114 | if (bytes) printf("Success.\n"); | 120 | if (bytes) |
| 115 | else printf("Failure. (expected unless you have a /delme file on your phone)\n"); | 121 | printf("Success.\n"); |
| 116 | 122 | else | |
| 123 | printf("Failure. (expected unless you have a /delme file on your phone)\n"); | ||
| 124 | |||
| 117 | printf("Renaming a file...\n"); | 125 | printf("Renaming a file...\n"); |
| 118 | bytes = iphone_afc_rename_file(afc, "/renme", "/renme2"); | 126 | bytes = iphone_afc_rename_file(afc, "/renme", "/renme2"); |
| 119 | if (bytes > 0) printf("Success.\n"); | 127 | if (bytes > 0) |
| 120 | else printf("Failure. (expected unless you have a /renme file on your phone)\n"); | 128 | printf("Success.\n"); |
| 121 | 129 | else | |
| 130 | printf("Failure. (expected unless you have a /renme file on your phone)\n"); | ||
| 131 | |||
| 122 | printf("Seek & read\n"); | 132 | printf("Seek & read\n"); |
| 123 | iphone_afc_open_file(afc, "/readme.libiphone.fx", IPHONE_AFC_FILE_READ, &my_file); | 133 | iphone_afc_open_file(afc, "/readme.libiphone.fx", IPHONE_AFC_FILE_READ, &my_file); |
| 124 | if (IPHONE_E_SUCCESS != iphone_afc_seek_file(afc, my_file, 5)) printf("WARN: SEEK DID NOT WORK\n"); | 134 | if (IPHONE_E_SUCCESS != iphone_afc_seek_file(afc, my_file, 5)) |
| 125 | char *threeletterword = (char*)malloc(sizeof(char) * 5); | 135 | printf("WARN: SEEK DID NOT WORK\n"); |
| 136 | char *threeletterword = (char *) malloc(sizeof(char) * 5); | ||
| 126 | iphone_afc_read_file(afc, my_file, threeletterword, 3, &bytes); | 137 | iphone_afc_read_file(afc, my_file, threeletterword, 3, &bytes); |
| 127 | threeletterword[3] = '\0'; | 138 | threeletterword[3] = '\0'; |
| 128 | if (bytes > 0) printf("Result: %s\n", threeletterword); | 139 | if (bytes > 0) |
| 129 | else printf("Couldn't read!\n"); | 140 | printf("Result: %s\n", threeletterword); |
| 141 | else | ||
| 142 | printf("Couldn't read!\n"); | ||
| 130 | free(threeletterword); | 143 | free(threeletterword); |
| 131 | iphone_afc_close_file(afc, my_file); | 144 | iphone_afc_close_file(afc, my_file); |
| 132 | 145 | ||
| 133 | } | 146 | } |
| 134 | iphone_afc_free_client(afc); | 147 | iphone_afc_free_client(afc); |
| 135 | } else { | 148 | } else { |
| 136 | printf("Start service failure.\n"); | 149 | printf("Start service failure.\n"); |
| 137 | } | 150 | } |
| 138 | 151 | ||
| 139 | printf("All done.\n"); | 152 | printf("All done.\n"); |
| 140 | 153 | ||
| 141 | iphone_lckd_free_client(control); | 154 | iphone_lckd_free_client(control); |
| 142 | iphone_free_device(phone); | 155 | iphone_free_device(phone); |
| 143 | 156 | ||
| 144 | return 0; | 157 | return 0; |
| 145 | } | 158 | } |
| 146 | |||
diff --git a/src/plist.c b/src/plist.c index 31490d0..c4d6bfa 100644 --- a/src/plist.c +++ b/src/plist.c | |||
| @@ -40,12 +40,12 @@ const char *plist_base = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\ | |||
| 40 | * | 40 | * |
| 41 | * @return The formatted string. | 41 | * @return The formatted string. |
| 42 | */ | 42 | */ |
| 43 | char* format_string(const char* buf, int cols, int depth) | 43 | char *format_string(const char *buf, int cols, int depth) |
| 44 | { | 44 | { |
| 45 | int colw = depth + cols + 1; | 45 | int colw = depth + cols + 1; |
| 46 | int len = strlen(buf); | 46 | int len = strlen(buf); |
| 47 | int nlines = len / cols + 1; | 47 | int nlines = len / cols + 1; |
| 48 | char* new_buf = (char*)malloc(nlines * colw + depth + 1); | 48 | char *new_buf = (char *) malloc(nlines * colw + depth + 1); |
| 49 | int i = 0; | 49 | int i = 0; |
| 50 | int j = 0; | 50 | int j = 0; |
| 51 | 51 | ||
| @@ -53,18 +53,18 @@ char* format_string(const char* buf, int cols, int depth) | |||
| 53 | assert(depth >= 0); | 53 | assert(depth >= 0); |
| 54 | 54 | ||
| 55 | // Inserts new lines and tabs at appropriate locations | 55 | // Inserts new lines and tabs at appropriate locations |
| 56 | for (i = 0; i < nlines; i++){ | 56 | for (i = 0; i < nlines; i++) { |
| 57 | new_buf[i * colw] = '\n'; | 57 | new_buf[i * colw] = '\n'; |
| 58 | for (j = 0; j < depth; j++) | 58 | for (j = 0; j < depth; j++) |
| 59 | new_buf[i * colw + 1 + j] = '\t'; | 59 | new_buf[i * colw + 1 + j] = '\t'; |
| 60 | memcpy(new_buf + i * colw + 1 + depth, buf + i * cols, cols); | 60 | memcpy(new_buf + i * colw + 1 + depth, buf + i * cols, cols); |
| 61 | } | 61 | } |
| 62 | new_buf[len+(1+depth)*nlines] = '\n'; | 62 | new_buf[len + (1 + depth) * nlines] = '\n'; |
| 63 | 63 | ||
| 64 | // Inserts final row of indentation and termination character | 64 | // Inserts final row of indentation and termination character |
| 65 | for (j = 0; j < depth; j++) | 65 | for (j = 0; j < depth; j++) |
| 66 | new_buf[len+(1+depth)*nlines + 1 + j] = '\t'; | 66 | new_buf[len + (1 + depth) * nlines + 1 + j] = '\t'; |
| 67 | new_buf[len+(1+depth)*nlines+depth+1] = '\0'; | 67 | new_buf[len + (1 + depth) * nlines + depth + 1] = '\0'; |
| 68 | 68 | ||
| 69 | return new_buf; | 69 | return new_buf; |
| 70 | } | 70 | } |
| @@ -73,12 +73,14 @@ char* format_string(const char* buf, int cols, int depth) | |||
| 73 | * | 73 | * |
| 74 | * @return The plist XML document. | 74 | * @return The plist XML document. |
| 75 | */ | 75 | */ |
| 76 | xmlDocPtr new_plist() { | 76 | xmlDocPtr new_plist() |
| 77 | { | ||
| 77 | char *plist = strdup(plist_base); | 78 | char *plist = strdup(plist_base); |
| 78 | xmlDocPtr plist_xml = xmlReadMemory(plist, strlen(plist), NULL, NULL, 0); | 79 | xmlDocPtr plist_xml = xmlReadMemory(plist, strlen(plist), NULL, NULL, 0); |
| 79 | 80 | ||
| 80 | if (!plist_xml) return NULL; | 81 | if (!plist_xml) |
| 81 | 82 | return NULL; | |
| 83 | |||
| 82 | free(plist); | 84 | free(plist); |
| 83 | 85 | ||
| 84 | return plist_xml; | 86 | return plist_xml; |
| @@ -88,8 +90,10 @@ xmlDocPtr new_plist() { | |||
| 88 | * | 90 | * |
| 89 | * @param plist The XML document to destroy. | 91 | * @param plist The XML document to destroy. |
| 90 | */ | 92 | */ |
| 91 | void free_plist(xmlDocPtr plist) { | 93 | void free_plist(xmlDocPtr plist) |
| 92 | if (!plist) return; | 94 | { |
| 95 | if (!plist) | ||
| 96 | return; | ||
| 93 | 97 | ||
| 94 | xmlFreeDoc(plist); | 98 | xmlFreeDoc(plist); |
| 95 | } | 99 | } |
| @@ -109,14 +113,17 @@ void free_plist(xmlDocPtr plist) { | |||
| 109 | * | 113 | * |
| 110 | * @return The newly created node. | 114 | * @return The newly created node. |
| 111 | */ | 115 | */ |
| 112 | xmlNode *add_child_to_plist(xmlDocPtr plist, const char *name, const char *content, xmlNode *to_node, int depth) { | 116 | xmlNode *add_child_to_plist(xmlDocPtr plist, const char *name, const char *content, xmlNode * to_node, int depth) |
| 117 | { | ||
| 113 | int i = 0; | 118 | int i = 0; |
| 114 | xmlNode *child; | 119 | xmlNode *child; |
| 115 | 120 | ||
| 116 | if (!plist) return NULL; | 121 | if (!plist) |
| 122 | return NULL; | ||
| 117 | assert(depth >= 0); | 123 | assert(depth >= 0); |
| 118 | if (!to_node) to_node = xmlDocGetRootElement(plist); | 124 | if (!to_node) |
| 119 | 125 | to_node = xmlDocGetRootElement(plist); | |
| 126 | |||
| 120 | for (i = 0; i < depth; i++) { | 127 | for (i = 0; i < depth; i++) { |
| 121 | xmlNodeAddContent(to_node, "\t"); | 128 | xmlNodeAddContent(to_node, "\t"); |
| 122 | } | 129 | } |
| @@ -136,12 +143,13 @@ xmlNode *add_child_to_plist(xmlDocPtr plist, const char *name, const char *conte | |||
| 136 | * | 143 | * |
| 137 | * @return The newly created key node. | 144 | * @return The newly created key node. |
| 138 | */ | 145 | */ |
| 139 | xmlNode *add_key_str_dict_element(xmlDocPtr plist, xmlNode *dict, const char *key, const char *value, int depth) { | 146 | xmlNode *add_key_str_dict_element(xmlDocPtr plist, xmlNode * dict, const char *key, const char *value, int depth) |
| 147 | { | ||
| 140 | xmlNode *keyPtr; | 148 | xmlNode *keyPtr; |
| 141 | 149 | ||
| 142 | keyPtr = add_child_to_plist(plist, "key", key, dict, depth); | 150 | keyPtr = add_child_to_plist(plist, "key", key, dict, depth); |
| 143 | add_child_to_plist(plist, "string", value, dict, depth); | 151 | add_child_to_plist(plist, "string", value, dict, depth); |
| 144 | 152 | ||
| 145 | return keyPtr; | 153 | return keyPtr; |
| 146 | } | 154 | } |
| 147 | 155 | ||
| @@ -155,9 +163,10 @@ xmlNode *add_key_str_dict_element(xmlDocPtr plist, xmlNode *dict, const char *ke | |||
| 155 | * | 163 | * |
| 156 | * @return The newly created dict node. | 164 | * @return The newly created dict node. |
| 157 | */ | 165 | */ |
| 158 | xmlNode *add_key_dict_node(xmlDocPtr plist, xmlNode *dict, const char *key, const char *value, int depth) { | 166 | xmlNode *add_key_dict_node(xmlDocPtr plist, xmlNode * dict, const char *key, const char *value, int depth) |
| 167 | { | ||
| 159 | xmlNode *child; | 168 | xmlNode *child; |
| 160 | 169 | ||
| 161 | add_child_to_plist(plist, "key", key, dict, depth); | 170 | add_child_to_plist(plist, "key", key, dict, depth); |
| 162 | child = add_child_to_plist(plist, "dict", value, dict, depth); | 171 | child = add_child_to_plist(plist, "dict", value, dict, depth); |
| 163 | 172 | ||
| @@ -174,12 +183,13 @@ xmlNode *add_key_dict_node(xmlDocPtr plist, xmlNode *dict, const char *key, cons | |||
| 174 | * | 183 | * |
| 175 | * @return The newly created key node. | 184 | * @return The newly created key node. |
| 176 | */ | 185 | */ |
| 177 | xmlNode *add_key_data_dict_element(xmlDocPtr plist, xmlNode *dict, const char *key, const char *value, int depth) { | 186 | xmlNode *add_key_data_dict_element(xmlDocPtr plist, xmlNode * dict, const char *key, const char *value, int depth) |
| 187 | { | ||
| 178 | xmlNode *keyPtr; | 188 | xmlNode *keyPtr; |
| 179 | 189 | ||
| 180 | keyPtr = add_child_to_plist(plist, "key", key, dict, depth); | 190 | keyPtr = add_child_to_plist(plist, "key", key, dict, depth); |
| 181 | add_child_to_plist(plist, "data", format_string(value, 60, depth), dict, depth); | 191 | add_child_to_plist(plist, "data", format_string(value, 60, depth), dict, depth); |
| 182 | 192 | ||
| 183 | return keyPtr; | 193 | return keyPtr; |
| 184 | } | 194 | } |
| 185 | 195 | ||
| @@ -190,17 +200,18 @@ xmlNode *add_key_data_dict_element(xmlDocPtr plist, xmlNode *dict, const char *k | |||
| 190 | * @return An array where each even number is a key and the odd numbers are | 200 | * @return An array where each even number is a key and the odd numbers are |
| 191 | * values. If the odd number is \0, that's the end of the list. | 201 | * values. If the odd number is \0, that's the end of the list. |
| 192 | */ | 202 | */ |
| 193 | char **read_dict_element_strings(xmlNode *dict) { | 203 | char **read_dict_element_strings(xmlNode * dict) |
| 204 | { | ||
| 194 | char **return_me = NULL, **old = NULL; | 205 | char **return_me = NULL, **old = NULL; |
| 195 | int current_length = 0; | 206 | int current_length = 0; |
| 196 | int current_pos = 0; | 207 | int current_pos = 0; |
| 197 | xmlNode *dict_walker; | 208 | xmlNode *dict_walker; |
| 198 | 209 | ||
| 199 | for (dict_walker = dict->children; dict_walker; dict_walker = dict_walker->next) { | 210 | for (dict_walker = dict->children; dict_walker; dict_walker = dict_walker->next) { |
| 200 | if (!xmlStrcmp(dict_walker->name, "key")) { | 211 | if (!xmlStrcmp(dict_walker->name, "key")) { |
| 201 | current_length += 2; | 212 | current_length += 2; |
| 202 | old = return_me; | 213 | old = return_me; |
| 203 | return_me = realloc(return_me, sizeof(char*) * current_length); | 214 | return_me = realloc(return_me, sizeof(char *) * current_length); |
| 204 | if (!return_me) { | 215 | if (!return_me) { |
| 205 | free(old); | 216 | free(old); |
| 206 | return NULL; | 217 | return NULL; |
| @@ -209,25 +220,26 @@ char **read_dict_element_strings(xmlNode *dict) { | |||
| 209 | return_me[current_pos++] = xmlNodeGetContent(dict_walker->next->next); | 220 | return_me[current_pos++] = xmlNodeGetContent(dict_walker->next->next); |
| 210 | } | 221 | } |
| 211 | } | 222 | } |
| 212 | 223 | ||
| 213 | old = return_me; | 224 | old = return_me; |
| 214 | return_me = realloc(return_me, sizeof(char*) * (current_length+1)); | 225 | return_me = realloc(return_me, sizeof(char *) * (current_length + 1)); |
| 215 | return_me[current_pos] = NULL; | 226 | return_me[current_pos] = NULL; |
| 216 | 227 | ||
| 217 | return return_me; | 228 | return return_me; |
| 218 | } | 229 | } |
| 219 | 230 | ||
| 220 | /** Destroys a dictionary as returned by read_dict_element_strings | 231 | /** Destroys a dictionary as returned by read_dict_element_strings |
| 221 | */ | 232 | */ |
| 222 | void free_dictionary(char **dictionary) { | 233 | void free_dictionary(char **dictionary) |
| 234 | { | ||
| 223 | int i = 0; | 235 | int i = 0; |
| 224 | 236 | ||
| 225 | if (!dictionary) return; | 237 | if (!dictionary) |
| 226 | 238 | return; | |
| 239 | |||
| 227 | for (i = 0; dictionary[i]; i++) { | 240 | for (i = 0; dictionary[i]; i++) { |
| 228 | free(dictionary[i]); | 241 | free(dictionary[i]); |
| 229 | } | 242 | } |
| 230 | 243 | ||
| 231 | free(dictionary); | 244 | free(dictionary); |
| 232 | } | 245 | } |
| 233 | |||
diff --git a/src/plist.h b/src/plist.h index ab79386..b27a0c5 100644 --- a/src/plist.h +++ b/src/plist.h | |||
| @@ -25,14 +25,14 @@ | |||
| 25 | #include <libxml/parser.h> | 25 | #include <libxml/parser.h> |
| 26 | #include <libxml/tree.h> | 26 | #include <libxml/tree.h> |
| 27 | 27 | ||
| 28 | xmlNode *add_key_dict_node(xmlDocPtr plist, xmlNode *dict, const char *key, const char *value, int depth); | 28 | xmlNode *add_key_dict_node(xmlDocPtr plist, xmlNode * dict, const char *key, const char *value, int depth); |
| 29 | xmlNode *add_key_str_dict_element(xmlDocPtr plist, xmlNode *dict, const char *key, const char *value, int depth); | 29 | xmlNode *add_key_str_dict_element(xmlDocPtr plist, xmlNode * dict, const char *key, const char *value, int depth); |
| 30 | xmlNode *add_key_data_dict_element(xmlDocPtr plist, xmlNode *dict, const char *key, const char *value, int depth); | 30 | xmlNode *add_key_data_dict_element(xmlDocPtr plist, xmlNode * dict, const char *key, const char *value, int depth); |
| 31 | xmlNode *add_child_to_plist(xmlDocPtr plist, const char *name, const char *content, xmlNode *to_node, int depth); | 31 | xmlNode *add_child_to_plist(xmlDocPtr plist, const char *name, const char *content, xmlNode * to_node, int depth); |
| 32 | 32 | ||
| 33 | void free_plist(xmlDocPtr plist); | 33 | void free_plist(xmlDocPtr plist); |
| 34 | xmlDocPtr new_plist(); | 34 | xmlDocPtr new_plist(); |
| 35 | 35 | ||
| 36 | char **read_dict_element_strings(xmlNode *dict); | 36 | char **read_dict_element_strings(xmlNode * dict); |
| 37 | void free_dictionary(char **dictionary); | 37 | void free_dictionary(char **dictionary); |
| 38 | #endif | 38 | #endif |
diff --git a/src/usbmux.c b/src/usbmux.c index 35f2ef3..cf1a2d9 100644 --- a/src/usbmux.c +++ b/src/usbmux.c | |||
| @@ -39,8 +39,9 @@ static int clients = 0; | |||
| 39 | * | 39 | * |
| 40 | * @return A USBMux packet | 40 | * @return A USBMux packet |
| 41 | */ | 41 | */ |
| 42 | usbmux_tcp_header *new_mux_packet(uint16 s_port, uint16 d_port) { | 42 | usbmux_tcp_header *new_mux_packet(uint16 s_port, uint16 d_port) |
| 43 | usbmux_tcp_header *conn = (usbmux_tcp_header*)malloc(sizeof(usbmux_tcp_header)); | 43 | { |
| 44 | usbmux_tcp_header *conn = (usbmux_tcp_header *) malloc(sizeof(usbmux_tcp_header)); | ||
| 44 | conn->type = htonl(6); | 45 | conn->type = htonl(6); |
| 45 | conn->length = 28; | 46 | conn->length = 28; |
| 46 | conn->sport = htons(s_port); | 47 | conn->sport = htons(s_port); |
| @@ -58,8 +59,9 @@ usbmux_tcp_header *new_mux_packet(uint16 s_port, uint16 d_port) { | |||
| 58 | * | 59 | * |
| 59 | * @return A USBMux header | 60 | * @return A USBMux header |
| 60 | */ | 61 | */ |
| 61 | usbmux_version_header *version_header() { | 62 | usbmux_version_header *version_header() |
| 62 | usbmux_version_header *version = (usbmux_version_header*)malloc(sizeof(usbmux_version_header)); | 63 | { |
| 64 | usbmux_version_header *version = (usbmux_version_header *) malloc(sizeof(usbmux_version_header)); | ||
| 63 | version->type = 0; | 65 | version->type = 0; |
| 64 | version->length = htonl(20); | 66 | version->length = htonl(20); |
| 65 | version->major = htonl(1); | 67 | version->major = htonl(1); |
| @@ -76,11 +78,13 @@ usbmux_version_header *version_header() { | |||
| 76 | * | 78 | * |
| 77 | * @param connection The connection to delete from the tracking list. | 79 | * @param connection The connection to delete from the tracking list. |
| 78 | */ | 80 | */ |
| 79 | void delete_connection(iphone_umux_client_t connection) { | 81 | void delete_connection(iphone_umux_client_t connection) |
| 80 | iphone_umux_client_t *newlist = (iphone_umux_client_t*)malloc(sizeof(iphone_umux_client_t) * (clients - 1)); | 82 | { |
| 83 | iphone_umux_client_t *newlist = (iphone_umux_client_t *) malloc(sizeof(iphone_umux_client_t) * (clients - 1)); | ||
| 81 | int i = 0, j = 0; | 84 | int i = 0, j = 0; |
| 82 | for (i = 0; i < clients; i++) { | 85 | for (i = 0; i < clients; i++) { |
| 83 | if (connlist[i] == connection) continue; | 86 | if (connlist[i] == connection) |
| 87 | continue; | ||
| 84 | else { | 88 | else { |
| 85 | newlist[j] = connlist[i]; | 89 | newlist[j] = connlist[i]; |
| 86 | j++; | 90 | j++; |
| @@ -89,8 +93,10 @@ void delete_connection(iphone_umux_client_t connection) { | |||
| 89 | free(connlist); | 93 | free(connlist); |
| 90 | connlist = newlist; | 94 | connlist = newlist; |
| 91 | clients--; | 95 | clients--; |
| 92 | if (connection->recv_buffer) free(connection->recv_buffer); | 96 | if (connection->recv_buffer) |
| 93 | if (connection->header) free(connection->header); | 97 | free(connection->recv_buffer); |
| 98 | if (connection->header) | ||
| 99 | free(connection->header); | ||
| 94 | connection->r_len = 0; | 100 | connection->r_len = 0; |
| 95 | free(connection); | 101 | free(connection); |
| 96 | } | 102 | } |
| @@ -101,8 +107,10 @@ void delete_connection(iphone_umux_client_t connection) { | |||
| 101 | * @param connection The connection to add to the global list of connections. | 107 | * @param connection The connection to add to the global list of connections. |
| 102 | */ | 108 | */ |
| 103 | 109 | ||
| 104 | void add_connection(iphone_umux_client_t connection) { | 110 | void add_connection(iphone_umux_client_t connection) |
| 105 | iphone_umux_client_t* newlist = (iphone_umux_client_t*)realloc(connlist, sizeof(iphone_umux_client_t) * (clients+1)); | 111 | { |
| 112 | iphone_umux_client_t *newlist = | ||
| 113 | (iphone_umux_client_t *) realloc(connlist, sizeof(iphone_umux_client_t) * (clients + 1)); | ||
| 106 | newlist[clients] = connection; | 114 | newlist[clients] = connection; |
| 107 | connlist = newlist; | 115 | connlist = newlist; |
| 108 | clients++; | 116 | clients++; |
| @@ -116,13 +124,15 @@ void add_connection(iphone_umux_client_t connection) { | |||
| 116 | * @param client A mux TCP header for the connection which is used for tracking and data transfer. | 124 | * @param client A mux TCP header for the connection which is used for tracking and data transfer. |
| 117 | * @return IPHONE_E_SUCCESS on success, an error code otherwise. | 125 | * @return IPHONE_E_SUCCESS on success, an error code otherwise. |
| 118 | */ | 126 | */ |
| 119 | iphone_error_t iphone_mux_new_client ( iphone_device_t device, uint16_t src_port, uint16_t dst_port, iphone_umux_client_t *client ){ | 127 | iphone_error_t iphone_mux_new_client(iphone_device_t device, uint16_t src_port, uint16_t dst_port, |
| 128 | iphone_umux_client_t * client) | ||
| 129 | { | ||
| 120 | if (!device || !src_port || !dst_port) | 130 | if (!device || !src_port || !dst_port) |
| 121 | return IPHONE_E_INVALID_ARG; | 131 | return IPHONE_E_INVALID_ARG; |
| 122 | 132 | ||
| 123 | int bytes = 0; | 133 | int bytes = 0; |
| 124 | // Initialize connection stuff | 134 | // Initialize connection stuff |
| 125 | iphone_umux_client_t new_connection = (iphone_umux_client_t)malloc(sizeof(struct iphone_umux_client_int)); | 135 | iphone_umux_client_t new_connection = (iphone_umux_client_t) malloc(sizeof(struct iphone_umux_client_int)); |
| 126 | new_connection->header = new_mux_packet(src_port, dst_port); | 136 | new_connection->header = new_mux_packet(src_port, dst_port); |
| 127 | 137 | ||
| 128 | // blargg | 138 | // blargg |
| @@ -130,18 +140,19 @@ iphone_error_t iphone_mux_new_client ( iphone_device_t device, uint16_t src_port | |||
| 130 | new_connection->header->tcp_flags = 0x02; | 140 | new_connection->header->tcp_flags = 0x02; |
| 131 | new_connection->header->length = htonl(new_connection->header->length); | 141 | new_connection->header->length = htonl(new_connection->header->length); |
| 132 | new_connection->header->length16 = htons(new_connection->header->length16); | 142 | new_connection->header->length16 = htons(new_connection->header->length16); |
| 133 | 143 | ||
| 134 | if (send_to_phone(device, (char*)new_connection->header, sizeof(usbmux_tcp_header)) >= 0) { | 144 | if (send_to_phone(device, (char *) new_connection->header, sizeof(usbmux_tcp_header)) >= 0) { |
| 135 | usbmux_tcp_header *response; | 145 | usbmux_tcp_header *response; |
| 136 | response = (usbmux_tcp_header*)malloc(sizeof(usbmux_tcp_header)); | 146 | response = (usbmux_tcp_header *) malloc(sizeof(usbmux_tcp_header)); |
| 137 | bytes = recv_from_phone(device, (char*)response, sizeof(*response)); | 147 | bytes = recv_from_phone(device, (char *) response, sizeof(*response)); |
| 138 | if (response->tcp_flags != 0x12) { | 148 | if (response->tcp_flags != 0x12) { |
| 139 | free(response); | 149 | free(response); |
| 140 | return IPHONE_E_UNKNOWN_ERROR; | 150 | return IPHONE_E_UNKNOWN_ERROR; |
| 141 | } else { | 151 | } else { |
| 142 | free(response); | 152 | free(response); |
| 143 | 153 | ||
| 144 | if (debug) printf("mux_connect: connection success\n"); | 154 | if (debug) |
| 155 | printf("mux_connect: connection success\n"); | ||
| 145 | new_connection->header->tcp_flags = 0x10; | 156 | new_connection->header->tcp_flags = 0x10; |
| 146 | new_connection->header->scnt = 1; | 157 | new_connection->header->scnt = 1; |
| 147 | new_connection->header->ocnt = 1; | 158 | new_connection->header->ocnt = 1; |
| @@ -156,7 +167,6 @@ iphone_error_t iphone_mux_new_client ( iphone_device_t device, uint16_t src_port | |||
| 156 | return IPHONE_E_NOT_ENOUGH_DATA; | 167 | return IPHONE_E_NOT_ENOUGH_DATA; |
| 157 | } | 168 | } |
| 158 | } | 169 | } |
| 159 | |||
| 160 | // if we get to this point it's probably bad | 170 | // if we get to this point it's probably bad |
| 161 | return IPHONE_E_UNKNOWN_ERROR; | 171 | return IPHONE_E_UNKNOWN_ERROR; |
| 162 | } | 172 | } |
| @@ -168,22 +178,24 @@ iphone_error_t iphone_mux_new_client ( iphone_device_t device, uint16_t src_port | |||
| 168 | * | 178 | * |
| 169 | * @return IPHONE_E_SUCCESS on success. | 179 | * @return IPHONE_E_SUCCESS on success. |
| 170 | */ | 180 | */ |
| 171 | iphone_error_t iphone_mux_free_client ( iphone_umux_client_t client ) { | 181 | iphone_error_t iphone_mux_free_client(iphone_umux_client_t client) |
| 172 | if (!client || !client->phone) return; | 182 | { |
| 173 | 183 | if (!client || !client->phone) | |
| 184 | return; | ||
| 185 | |||
| 174 | client->header->tcp_flags = 0x04; | 186 | client->header->tcp_flags = 0x04; |
| 175 | client->header->scnt = htonl(client->header->scnt); | 187 | client->header->scnt = htonl(client->header->scnt); |
| 176 | client->header->ocnt = htonl(client->header->ocnt); | 188 | client->header->ocnt = htonl(client->header->ocnt); |
| 177 | int bytes = 0; | 189 | int bytes = 0; |
| 178 | 190 | ||
| 179 | bytes = usb_bulk_write(client->phone->device, BULKOUT, (char*)client->header, sizeof(usbmux_tcp_header), 800); | 191 | bytes = usb_bulk_write(client->phone->device, BULKOUT, (char *) client->header, sizeof(usbmux_tcp_header), 800); |
| 180 | if(debug && bytes < 0) | 192 | if (debug && bytes < 0) |
| 181 | printf("iphone_muxèfree_client(): when writing, libusb gave me the error: %s\n", usb_strerror()); | 193 | printf("iphone_muxèfree_client(): when writing, libusb gave me the error: %s\n", usb_strerror()); |
| 182 | 194 | ||
| 183 | bytes = usb_bulk_read(client->phone->device, BULKIN, (char*)client->header, sizeof(usbmux_tcp_header), 800); | 195 | bytes = usb_bulk_read(client->phone->device, BULKIN, (char *) client->header, sizeof(usbmux_tcp_header), 800); |
| 184 | if(debug && bytes < 0) | 196 | if (debug && bytes < 0) |
| 185 | printf("get_iPhone(): when reading, libusb gave me the error: %s\n", usb_strerror()); | 197 | printf("get_iPhone(): when reading, libusb gave me the error: %s\n", usb_strerror()); |
| 186 | 198 | ||
| 187 | delete_connection(client); | 199 | delete_connection(client); |
| 188 | 200 | ||
| 189 | return IPHONE_E_SUCCESS; | 201 | return IPHONE_E_SUCCESS; |
| @@ -201,30 +213,35 @@ iphone_error_t iphone_mux_free_client ( iphone_umux_client_t client ) { | |||
| 201 | * @return IPHONE_E_SUCCESS on success. | 213 | * @return IPHONE_E_SUCCESS on success. |
| 202 | */ | 214 | */ |
| 203 | 215 | ||
| 204 | iphone_error_t iphone_mux_send ( iphone_umux_client_t client, const char *data, uint32_t datalen, uint32_t *sent_bytes ) { | 216 | iphone_error_t iphone_mux_send(iphone_umux_client_t client, const char *data, uint32_t datalen, uint32_t * sent_bytes) |
| 205 | if (!client->phone || !client || !data || datalen == 0 || !sent_bytes) return IPHONE_E_INVALID_ARG; | 217 | { |
| 218 | if (!client->phone || !client || !data || datalen == 0 || !sent_bytes) | ||
| 219 | return IPHONE_E_INVALID_ARG; | ||
| 206 | // client->scnt and client->ocnt should already be in host notation... | 220 | // client->scnt and client->ocnt should already be in host notation... |
| 207 | // we don't need to change them juuuust yet. | 221 | // we don't need to change them juuuust yet. |
| 208 | *sent_bytes = 0; | 222 | *sent_bytes = 0; |
| 209 | if (debug) printf("mux_send(): client wants to send %i bytes\n", datalen); | 223 | if (debug) |
| 210 | char *buffer = (char*)malloc(sizeof(usbmux_tcp_header) + datalen + 2); // allow 2 bytes of safety padding | 224 | printf("mux_send(): client wants to send %i bytes\n", datalen); |
| 225 | char *buffer = (char *) malloc(sizeof(usbmux_tcp_header) + datalen + 2); // allow 2 bytes of safety padding | ||
| 211 | // Set the length and pre-emptively htonl/htons it | 226 | // Set the length and pre-emptively htonl/htons it |
| 212 | client->header->length = htonl(sizeof(usbmux_tcp_header) + datalen); | 227 | client->header->length = htonl(sizeof(usbmux_tcp_header) + datalen); |
| 213 | client->header->length16 = htons(sizeof(usbmux_tcp_header) + datalen); | 228 | client->header->length16 = htons(sizeof(usbmux_tcp_header) + datalen); |
| 214 | 229 | ||
| 215 | // Put scnt and ocnt into big-endian notation | 230 | // Put scnt and ocnt into big-endian notation |
| 216 | client->header->scnt = htonl(client->header->scnt); | 231 | client->header->scnt = htonl(client->header->scnt); |
| 217 | client->header->ocnt = htonl(client->header->ocnt); | 232 | client->header->ocnt = htonl(client->header->ocnt); |
| 218 | // Concatenation of stuff in the buffer. | 233 | // Concatenation of stuff in the buffer. |
| 219 | memcpy(buffer, client->header, sizeof(usbmux_tcp_header)); | 234 | memcpy(buffer, client->header, sizeof(usbmux_tcp_header)); |
| 220 | memcpy(buffer+sizeof(usbmux_tcp_header), data, datalen); | 235 | memcpy(buffer + sizeof(usbmux_tcp_header), data, datalen); |
| 221 | 236 | ||
| 222 | // We have a buffer full of data, we should now send it to the phone. | 237 | // We have a buffer full of data, we should now send it to the phone. |
| 223 | if (debug) printf("actually sending %zi bytes of data at %p\n", sizeof(usbmux_tcp_header)+datalen, buffer); | 238 | if (debug) |
| 239 | printf("actually sending %zi bytes of data at %p\n", sizeof(usbmux_tcp_header) + datalen, buffer); | ||
| 240 | |||
| 224 | 241 | ||
| 225 | 242 | *sent_bytes = send_to_phone(client->phone, buffer, sizeof(usbmux_tcp_header) + datalen); | |
| 226 | *sent_bytes = send_to_phone(client->phone, buffer, sizeof(usbmux_tcp_header)+datalen); | 243 | if (debug) |
| 227 | if (debug) printf("mux_send: sent %i bytes!\n", *sent_bytes); | 244 | printf("mux_send: sent %i bytes!\n", *sent_bytes); |
| 228 | // Now that we've sent it off, we can clean up after our sloppy selves. | 245 | // Now that we've sent it off, we can clean up after our sloppy selves. |
| 229 | if (debug) { | 246 | if (debug) { |
| 230 | FILE *packet = fopen("packet", "a+"); | 247 | FILE *packet = fopen("packet", "a+"); |
| @@ -232,24 +249,25 @@ iphone_error_t iphone_mux_send ( iphone_umux_client_t client, const char *data, | |||
| 232 | fclose(packet); | 249 | fclose(packet); |
| 233 | printf("\n"); | 250 | printf("\n"); |
| 234 | } | 251 | } |
| 235 | 252 | ||
| 236 | if (buffer) free(buffer); | 253 | if (buffer) |
| 254 | free(buffer); | ||
| 237 | // Re-calculate scnt and ocnt | 255 | // Re-calculate scnt and ocnt |
| 238 | client->header->scnt = ntohl(client->header->scnt) + datalen; | 256 | client->header->scnt = ntohl(client->header->scnt) + datalen; |
| 239 | client->header->ocnt = ntohl(client->header->ocnt); | 257 | client->header->ocnt = ntohl(client->header->ocnt); |
| 240 | 258 | ||
| 241 | // Revert lengths | 259 | // Revert lengths |
| 242 | client->header->length = ntohl(client->header->length); | 260 | client->header->length = ntohl(client->header->length); |
| 243 | client->header->length16 = ntohs(client->header->length16); | 261 | client->header->length16 = ntohs(client->header->length16); |
| 244 | 262 | ||
| 245 | // Now return the bytes. | 263 | // Now return the bytes. |
| 246 | if (*sent_bytes < sizeof(usbmux_tcp_header)+datalen) { | 264 | if (*sent_bytes < sizeof(usbmux_tcp_header) + datalen) { |
| 247 | *sent_bytes = 0; | 265 | *sent_bytes = 0; |
| 248 | return IPHONE_E_NOT_ENOUGH_DATA; | 266 | return IPHONE_E_NOT_ENOUGH_DATA; |
| 249 | } else { | 267 | } else { |
| 250 | *sent_bytes = *sent_bytes - 28; // actual length sent. :/ | 268 | *sent_bytes = *sent_bytes - 28; // actual length sent. :/ |
| 251 | } | 269 | } |
| 252 | 270 | ||
| 253 | return IPHONE_E_SUCCESS; | 271 | return IPHONE_E_SUCCESS; |
| 254 | } | 272 | } |
| 255 | 273 | ||
| @@ -261,7 +279,8 @@ iphone_error_t iphone_mux_send ( iphone_umux_client_t client, const char *data, | |||
| 261 | * | 279 | * |
| 262 | * @return How many bytes were read, or -1 if something bad happens. | 280 | * @return How many bytes were read, or -1 if something bad happens. |
| 263 | */ | 281 | */ |
| 264 | iphone_error_t iphone_mux_recv ( iphone_umux_client_t client, char *data, uint32_t datalen, uint32_t *recv_bytes ) { | 282 | iphone_error_t iphone_mux_recv(iphone_umux_client_t client, char *data, uint32_t datalen, uint32_t * recv_bytes) |
| 283 | { | ||
| 265 | 284 | ||
| 266 | if (!client || !data || datalen == 0 || !recv_bytes) | 285 | if (!client || !data || datalen == 0 || !recv_bytes) |
| 267 | return IPHONE_E_INVALID_ARG; | 286 | return IPHONE_E_INVALID_ARG; |
| @@ -269,18 +288,19 @@ iphone_error_t iphone_mux_recv ( iphone_umux_client_t client, char *data, uint32 | |||
| 269 | * Order of operation: | 288 | * Order of operation: |
| 270 | * 1.) Check if the client has a pre-received buffer. | 289 | * 1.) Check if the client has a pre-received buffer. |
| 271 | * 2.) If so, fill data with the buffer, as much as needed. | 290 | * 2.) If so, fill data with the buffer, as much as needed. |
| 272 | * a.) Return quickly if the buffer has enough | 291 | * a.) Return quickly if the buffer has enough |
| 273 | * b.) If the buffer is only part of the datalen, get the rest of datalen (and if we can't, just return) | 292 | * b.) If the buffer is only part of the datalen, get the rest of datalen (and if we can't, just return) |
| 274 | * 3.) If not, receive directly from the phone. | 293 | * 3.) If not, receive directly from the phone. |
| 275 | * a.) Check incoming packet's ports. If proper, follow proper buffering and receiving operation. | 294 | * a.) Check incoming packet's ports. If proper, follow proper buffering and receiving operation. |
| 276 | * b.) If not, find the client the ports belong to and fill that client's buffer, then return mux_recv with the same args to try again. | 295 | * b.) If not, find the client the ports belong to and fill that client's buffer, then return mux_recv with the same args to try again. |
| 277 | */ | 296 | */ |
| 278 | if (debug) printf("mux_recv: datalen == %i\n", datalen); | 297 | if (debug) |
| 298 | printf("mux_recv: datalen == %i\n", datalen); | ||
| 279 | int bytes = 0, i = 0, complex = 0, offset = 0; | 299 | int bytes = 0, i = 0, complex = 0, offset = 0; |
| 280 | *recv_bytes = 0; | 300 | *recv_bytes = 0; |
| 281 | char *buffer = NULL; | 301 | char *buffer = NULL; |
| 282 | usbmux_tcp_header *header = NULL; | 302 | usbmux_tcp_header *header = NULL; |
| 283 | 303 | ||
| 284 | if (client->recv_buffer) { | 304 | if (client->recv_buffer) { |
| 285 | if (client->r_len >= datalen) { | 305 | if (client->r_len >= datalen) { |
| 286 | memcpy(data, client->recv_buffer, datalen); | 306 | memcpy(data, client->recv_buffer, datalen); |
| @@ -290,41 +310,42 @@ iphone_error_t iphone_mux_recv ( iphone_umux_client_t client, char *data, uint32 | |||
| 290 | client->r_len = 0; | 310 | client->r_len = 0; |
| 291 | client->recv_buffer = NULL; | 311 | client->recv_buffer = NULL; |
| 292 | } else { | 312 | } else { |
| 293 | buffer = (char*)malloc(sizeof(char) * (client->r_len - datalen)); | 313 | buffer = (char *) malloc(sizeof(char) * (client->r_len - datalen)); |
| 294 | memcpy(buffer, client->recv_buffer+datalen, (client->r_len - datalen)); | 314 | memcpy(buffer, client->recv_buffer + datalen, (client->r_len - datalen)); |
| 295 | client->r_len -= datalen; | 315 | client->r_len -= datalen; |
| 296 | free(client->recv_buffer); | 316 | free(client->recv_buffer); |
| 297 | client->recv_buffer = buffer; | 317 | client->recv_buffer = buffer; |
| 298 | } | 318 | } |
| 299 | 319 | ||
| 300 | // Since we were able to fill the data straight from our buffer, we can just return datalen. See 2a above. | 320 | // Since we were able to fill the data straight from our buffer, we can just return datalen. See 2a above. |
| 301 | return datalen; | 321 | return datalen; |
| 302 | } else { | 322 | } else { |
| 303 | memcpy(data, client->recv_buffer, client->r_len); | 323 | memcpy(data, client->recv_buffer, client->r_len); |
| 304 | free(client->recv_buffer); // don't need to deal with anymore, but... | 324 | free(client->recv_buffer); // don't need to deal with anymore, but... |
| 305 | offset = client->r_len; // see #2b, above | 325 | offset = client->r_len; // see #2b, above |
| 306 | client->r_len = 0; | 326 | client->r_len = 0; |
| 307 | } | 327 | } |
| 308 | } // End of what to do if we have a pre-buffer. See #1 and #2 above. | 328 | } // End of what to do if we have a pre-buffer. See #1 and #2 above. |
| 309 | 329 | ||
| 310 | buffer = (char*)malloc(sizeof(char) * 131072); // make sure we get enough ;) | 330 | buffer = (char *) malloc(sizeof(char) * 131072); // make sure we get enough ;) |
| 311 | 331 | ||
| 312 | // See #3. | 332 | // See #3. |
| 313 | bytes = recv_from_phone(client->phone, buffer, 131072); | 333 | bytes = recv_from_phone(client->phone, buffer, 131072); |
| 314 | if (bytes < 28) { | 334 | if (bytes < 28) { |
| 315 | free(buffer); | 335 | free(buffer); |
| 316 | if (debug) printf("mux_recv: Did not even get the header.\n"); | 336 | if (debug) |
| 337 | printf("mux_recv: Did not even get the header.\n"); | ||
| 317 | return IPHONE_E_NOT_ENOUGH_DATA; | 338 | return IPHONE_E_NOT_ENOUGH_DATA; |
| 318 | } | 339 | } |
| 319 | 340 | ||
| 320 | header = (usbmux_tcp_header*)buffer; | 341 | header = (usbmux_tcp_header *) buffer; |
| 321 | if (header->sport != client->header->dport || header->dport != client->header->sport) { | 342 | if (header->sport != client->header->dport || header->dport != client->header->sport) { |
| 322 | // Ooooops -- we got someone else's packet. | 343 | // Ooooops -- we got someone else's packet. |
| 323 | // We gotta stick it in their buffer. (Take that any old way you want ;) ) | 344 | // We gotta stick it in their buffer. (Take that any old way you want ;) ) |
| 324 | for (i = 0; i < clients; i++) { | 345 | for (i = 0; i < clients; i++) { |
| 325 | if (connlist[i]->header->sport == header->dport && connlist[i]->header->dport == header->sport) { | 346 | if (connlist[i]->header->sport == header->dport && connlist[i]->header->dport == header->sport) { |
| 326 | // we have a winner. | 347 | // we have a winner. |
| 327 | char *nfb = (char*)malloc(sizeof(char) * (connlist[i]->r_len + (bytes - 28))); | 348 | char *nfb = (char *) malloc(sizeof(char) * (connlist[i]->r_len + (bytes - 28))); |
| 328 | if (connlist[i]->recv_buffer && connlist[i]->r_len) { | 349 | if (connlist[i]->recv_buffer && connlist[i]->r_len) { |
| 329 | memcpy(nfb, connlist[i]->recv_buffer, connlist[i]->r_len); | 350 | memcpy(nfb, connlist[i]->recv_buffer, connlist[i]->r_len); |
| 330 | free(connlist[i]->recv_buffer); | 351 | free(connlist[i]->recv_buffer); |
| @@ -332,45 +353,44 @@ iphone_error_t iphone_mux_recv ( iphone_umux_client_t client, char *data, uint32 | |||
| 332 | connlist[i]->r_len += bytes - 28; | 353 | connlist[i]->r_len += bytes - 28; |
| 333 | //connlist[i]->recv_buffer = (char*)realloc(connlist[i]->recv_buffer, sizeof(char) * client->r_len); // grow their buffer | 354 | //connlist[i]->recv_buffer = (char*)realloc(connlist[i]->recv_buffer, sizeof(char) * client->r_len); // grow their buffer |
| 334 | connlist[i]->recv_buffer = nfb; | 355 | connlist[i]->recv_buffer = nfb; |
| 335 | nfb = NULL; // A cookie for you if you can guess what "nfb" means. | 356 | nfb = NULL; // A cookie for you if you can guess what "nfb" means. |
| 336 | complex = connlist[i]->r_len - (bytes - 28); | 357 | complex = connlist[i]->r_len - (bytes - 28); |
| 337 | memcpy(connlist[i]->recv_buffer+complex, buffer+28, bytes-28); // paste into their buffer | 358 | memcpy(connlist[i]->recv_buffer + complex, buffer + 28, bytes - 28); // paste into their buffer |
| 338 | connlist[i]->header->ocnt += bytes-28; | 359 | connlist[i]->header->ocnt += bytes - 28; |
| 339 | } | 360 | } |
| 340 | } | 361 | } |
| 341 | // If it wasn't ours, it's been handled by this point... or forgotten. | 362 | // If it wasn't ours, it's been handled by this point... or forgotten. |
| 342 | // Free our buffer and continue. | 363 | // Free our buffer and continue. |
| 343 | free(buffer); | 364 | free(buffer); |
| 344 | buffer = NULL; | 365 | buffer = NULL; |
| 345 | return iphone_mux_recv(client, data, datalen, recv_bytes); // recurse back in to try again | 366 | return iphone_mux_recv(client, data, datalen, recv_bytes); // recurse back in to try again |
| 346 | } | 367 | } |
| 347 | |||
| 348 | // The packet was absolutely meant for us if it hits this point. | 368 | // The packet was absolutely meant for us if it hits this point. |
| 349 | // The pre-buffer has been taken care of, so, again, if we're at this point we have to read from the phone. | 369 | // The pre-buffer has been taken care of, so, again, if we're at this point we have to read from the phone. |
| 350 | 370 | ||
| 351 | if ((bytes-28) > datalen) { | 371 | if ((bytes - 28) > datalen) { |
| 352 | // Copy what we need into the data, buffer the rest because we can. | 372 | // Copy what we need into the data, buffer the rest because we can. |
| 353 | memcpy(data+offset, buffer+28, datalen); // data+offset: see #2b, above | 373 | memcpy(data + offset, buffer + 28, datalen); // data+offset: see #2b, above |
| 354 | complex = client->r_len + (bytes-28) - datalen; | 374 | complex = client->r_len + (bytes - 28) - datalen; |
| 355 | client->recv_buffer = (char*)realloc(client->recv_buffer, (sizeof(char) * complex)); | 375 | client->recv_buffer = (char *) realloc(client->recv_buffer, (sizeof(char) * complex)); |
| 356 | client->r_len = complex; | 376 | client->r_len = complex; |
| 357 | complex = client->r_len - (bytes-28) - datalen; | 377 | complex = client->r_len - (bytes - 28) - datalen; |
| 358 | memcpy(client->recv_buffer+complex, buffer+28+datalen, (bytes-28) - datalen); | 378 | memcpy(client->recv_buffer + complex, buffer + 28 + datalen, (bytes - 28) - datalen); |
| 359 | free(buffer); | 379 | free(buffer); |
| 360 | client->header->ocnt += bytes-28; | 380 | client->header->ocnt += bytes - 28; |
| 361 | *recv_bytes = datalen; | 381 | *recv_bytes = datalen; |
| 362 | return IPHONE_E_SUCCESS; | 382 | return IPHONE_E_SUCCESS; |
| 363 | } else { | 383 | } else { |
| 364 | // Fill the data with what we have, and just return. | 384 | // Fill the data with what we have, and just return. |
| 365 | memcpy(data+offset, buffer+28, bytes-28); // data+offset: see #2b, above | 385 | memcpy(data + offset, buffer + 28, bytes - 28); // data+offset: see #2b, above |
| 366 | client->header->ocnt += bytes-28; | 386 | client->header->ocnt += bytes - 28; |
| 367 | free(buffer); | 387 | free(buffer); |
| 368 | *recv_bytes = bytes - 28; | 388 | *recv_bytes = bytes - 28; |
| 369 | return IPHONE_E_SUCCESS; | 389 | return IPHONE_E_SUCCESS; |
| 370 | } | 390 | } |
| 371 | 391 | ||
| 372 | // If we get to this point, 'tis probably bad. | 392 | // If we get to this point, 'tis probably bad. |
| 373 | if (debug) printf("mux_recv: Heisenbug: bytes and datalen not matching up\n"); | 393 | if (debug) |
| 394 | printf("mux_recv: Heisenbug: bytes and datalen not matching up\n"); | ||
| 374 | return IPHONE_E_UNKNOWN_ERROR; | 395 | return IPHONE_E_UNKNOWN_ERROR; |
| 375 | } | 396 | } |
| 376 | |||
diff --git a/src/userpref.c b/src/userpref.c index 5b53775..57946f7 100644 --- a/src/userpref.c +++ b/src/userpref.c | |||
| @@ -39,10 +39,11 @@ extern int debug; | |||
| 39 | 39 | ||
| 40 | /** Creates a freedesktop compatible configuration directory for libiphone. | 40 | /** Creates a freedesktop compatible configuration directory for libiphone. |
| 41 | */ | 41 | */ |
| 42 | inline void create_config_dir() { | 42 | inline void create_config_dir() |
| 43 | gchar* config_dir = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, NULL); | 43 | { |
| 44 | gchar *config_dir = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, NULL); | ||
| 44 | 45 | ||
| 45 | if (!g_file_test(config_dir, (G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR) )) | 46 | if (!g_file_test(config_dir, (G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR))) |
| 46 | g_mkdir_with_parents(config_dir, 0755); | 47 | g_mkdir_with_parents(config_dir, 0755); |
| 47 | 48 | ||
| 48 | g_free(config_dir); | 49 | g_free(config_dir); |
| @@ -55,26 +56,29 @@ inline void create_config_dir() { | |||
| 55 | * | 56 | * |
| 56 | * @return The string containing the HostID or NULL | 57 | * @return The string containing the HostID or NULL |
| 57 | */ | 58 | */ |
| 58 | char* get_host_id() { | 59 | char *get_host_id() |
| 59 | char* host_id = NULL; | 60 | { |
| 60 | gchar* config_file; | 61 | char *host_id = NULL; |
| 61 | GKeyFile* key_file; | 62 | gchar *config_file; |
| 62 | gchar* loc_host_id; | 63 | GKeyFile *key_file; |
| 64 | gchar *loc_host_id; | ||
| 63 | 65 | ||
| 64 | config_file = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, LIBIPHONE_CONF_FILE, NULL); | 66 | config_file = |
| 67 | g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, LIBIPHONE_CONF_FILE, NULL); | ||
| 65 | 68 | ||
| 66 | /* now parse file to get the HostID */ | 69 | /* now parse file to get the HostID */ |
| 67 | key_file = g_key_file_new(); | 70 | key_file = g_key_file_new(); |
| 68 | if(g_key_file_load_from_file(key_file, config_file, G_KEY_FILE_KEEP_COMMENTS, NULL)) { | 71 | if (g_key_file_load_from_file(key_file, config_file, G_KEY_FILE_KEEP_COMMENTS, NULL)) { |
| 69 | loc_host_id = g_key_file_get_value(key_file, "Global", "HostID", NULL); | 72 | loc_host_id = g_key_file_get_value(key_file, "Global", "HostID", NULL); |
| 70 | if (loc_host_id) | 73 | if (loc_host_id) |
| 71 | host_id = strdup((char*)loc_host_id); | 74 | host_id = strdup((char *) loc_host_id); |
| 72 | g_free(loc_host_id); | 75 | g_free(loc_host_id); |
| 73 | } | 76 | } |
| 74 | g_key_file_free(key_file); | 77 | g_key_file_free(key_file); |
| 75 | g_free(config_file); | 78 | g_free(config_file); |
| 76 | 79 | ||
| 77 | if (debug) printf("get_host_id(): Using %s as HostID\n",host_id); | 80 | if (debug) |
| 81 | printf("get_host_id(): Using %s as HostID\n", host_id); | ||
| 78 | return host_id; | 82 | return host_id; |
| 79 | } | 83 | } |
| 80 | 84 | ||
| @@ -85,16 +89,17 @@ char* get_host_id() { | |||
| 85 | * @return 1 if the iPhone has been connected previously to this configuration | 89 | * @return 1 if the iPhone has been connected previously to this configuration |
| 86 | * or 0 otherwise. | 90 | * or 0 otherwise. |
| 87 | */ | 91 | */ |
| 88 | int is_device_known(char* uid) { | 92 | int is_device_known(char *uid) |
| 93 | { | ||
| 89 | int ret = 0; | 94 | int ret = 0; |
| 90 | gchar *config_file; | 95 | gchar *config_file; |
| 91 | GKeyFile *key_file; | 96 | GKeyFile *key_file; |
| 92 | gchar **devices_list, **pcur, *keyfilepath, *stored_key; | 97 | gchar **devices_list, **pcur, *keyfilepath, *stored_key; |
| 93 | GIOChannel *keyfile; | 98 | GIOChannel *keyfile; |
| 94 | 99 | ||
| 95 | /* first get config file */ | 100 | /* first get config file */ |
| 96 | gchar* device_file = g_strconcat(uid, ".pem", NULL); | 101 | gchar *device_file = g_strconcat(uid, ".pem", NULL); |
| 97 | config_file = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, device_file, NULL); | 102 | config_file = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, device_file, NULL); |
| 98 | if (g_file_test(config_file, (G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR))) | 103 | if (g_file_test(config_file, (G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR))) |
| 99 | ret = 1; | 104 | ret = 1; |
| 100 | g_free(config_file); | 105 | g_free(config_file); |
| @@ -110,7 +115,8 @@ int is_device_known(char* uid) { | |||
| 110 | * @return 1 on success and 0 if no public key is given or if it has already | 115 | * @return 1 on success and 0 if no public key is given or if it has already |
| 111 | * been marked as connected previously. | 116 | * been marked as connected previously. |
| 112 | */ | 117 | */ |
| 113 | int store_device_public_key(char* uid, char* public_key) { | 118 | int store_device_public_key(char *uid, char *public_key) |
| 119 | { | ||
| 114 | 120 | ||
| 115 | if (NULL == public_key || is_device_known(uid)) | 121 | if (NULL == public_key || is_device_known(uid)) |
| 116 | return 0; | 122 | return 0; |
| @@ -119,14 +125,14 @@ int store_device_public_key(char* uid, char* public_key) { | |||
| 119 | create_config_dir(); | 125 | create_config_dir(); |
| 120 | 126 | ||
| 121 | /* build file path */ | 127 | /* build file path */ |
| 122 | gchar* device_file = g_strconcat(uid, ".pem", NULL); | 128 | gchar *device_file = g_strconcat(uid, ".pem", NULL); |
| 123 | gchar* pem = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, device_file, NULL); | 129 | gchar *pem = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, device_file, NULL); |
| 124 | 130 | ||
| 125 | /* decode public key for storing */ | 131 | /* decode public key for storing */ |
| 126 | gsize decoded_size; | 132 | gsize decoded_size; |
| 127 | gchar* data = g_base64_decode (public_key, &decoded_size); | 133 | gchar *data = g_base64_decode(public_key, &decoded_size); |
| 128 | /* store file */ | 134 | /* store file */ |
| 129 | FILE* pFile = fopen(pem , "wb"); | 135 | FILE *pFile = fopen(pem, "wb"); |
| 130 | fwrite(data, 1, decoded_size, pFile); | 136 | fwrite(data, 1, decoded_size, pFile); |
| 131 | fclose(pFile); | 137 | fclose(pFile); |
| 132 | g_free(pem); | 138 | g_free(pem); |
| @@ -142,24 +148,25 @@ int store_device_public_key(char* uid, char* public_key) { | |||
| 142 | * | 148 | * |
| 143 | * @return 1 if the file contents where read successfully and 0 otherwise. | 149 | * @return 1 if the file contents where read successfully and 0 otherwise. |
| 144 | */ | 150 | */ |
| 145 | int read_file_in_confdir(char* file, gnutls_datum_t* data) { | 151 | int read_file_in_confdir(char *file, gnutls_datum_t * data) |
| 152 | { | ||
| 146 | gboolean success; | 153 | gboolean success; |
| 147 | gsize size; | 154 | gsize size; |
| 148 | char *content; | 155 | char *content; |
| 149 | gchar *filepath; | 156 | gchar *filepath; |
| 150 | 157 | ||
| 151 | if (NULL == file || NULL == data) | 158 | if (NULL == file || NULL == data) |
| 152 | return 0; | 159 | return 0; |
| 153 | 160 | ||
| 154 | /* Read file */ | 161 | /* Read file */ |
| 155 | filepath = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, file, NULL); | 162 | filepath = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, file, NULL); |
| 156 | success = g_file_get_contents(filepath, &content, &size, NULL); | 163 | success = g_file_get_contents(filepath, &content, &size, NULL); |
| 157 | g_free(filepath); | 164 | g_free(filepath); |
| 158 | 165 | ||
| 159 | /* Add it to the gnutls_datnum_t structure */ | 166 | /* Add it to the gnutls_datnum_t structure */ |
| 160 | data->data = content; | 167 | data->data = content; |
| 161 | data->size = size; | 168 | data->size = size; |
| 162 | 169 | ||
| 163 | return success; | 170 | return success; |
| 164 | } | 171 | } |
| 165 | 172 | ||
| @@ -169,7 +176,8 @@ int read_file_in_confdir(char* file, gnutls_datum_t* data) { | |||
| 169 | * | 176 | * |
| 170 | * @return 1 if the file was successfully read and 0 otherwise. | 177 | * @return 1 if the file was successfully read and 0 otherwise. |
| 171 | */ | 178 | */ |
| 172 | int get_root_private_key(gnutls_datum_t* root_privkey) { | 179 | int get_root_private_key(gnutls_datum_t * root_privkey) |
| 180 | { | ||
| 173 | return read_file_in_confdir(LIBIPHONE_ROOT_PRIVKEY, root_privkey); | 181 | return read_file_in_confdir(LIBIPHONE_ROOT_PRIVKEY, root_privkey); |
| 174 | } | 182 | } |
| 175 | 183 | ||
| @@ -179,7 +187,8 @@ int get_root_private_key(gnutls_datum_t* root_privkey) { | |||
| 179 | * | 187 | * |
| 180 | * @return 1 if the file was successfully read and 0 otherwise. | 188 | * @return 1 if the file was successfully read and 0 otherwise. |
| 181 | */ | 189 | */ |
| 182 | int get_host_private_key(gnutls_datum_t* host_privkey) { | 190 | int get_host_private_key(gnutls_datum_t * host_privkey) |
| 191 | { | ||
| 183 | return read_file_in_confdir(LIBIPHONE_HOST_PRIVKEY, host_privkey); | 192 | return read_file_in_confdir(LIBIPHONE_HOST_PRIVKEY, host_privkey); |
| 184 | } | 193 | } |
| 185 | 194 | ||
| @@ -189,7 +198,8 @@ int get_host_private_key(gnutls_datum_t* host_privkey) { | |||
| 189 | * | 198 | * |
| 190 | * @return 1 if the file was successfully read and 0 otherwise. | 199 | * @return 1 if the file was successfully read and 0 otherwise. |
| 191 | */ | 200 | */ |
| 192 | int get_root_certificate(gnutls_datum_t* root_cert) { | 201 | int get_root_certificate(gnutls_datum_t * root_cert) |
| 202 | { | ||
| 193 | return read_file_in_confdir(LIBIPHONE_ROOT_CERTIF, root_cert); | 203 | return read_file_in_confdir(LIBIPHONE_ROOT_CERTIF, root_cert); |
| 194 | } | 204 | } |
| 195 | 205 | ||
| @@ -199,7 +209,8 @@ int get_root_certificate(gnutls_datum_t* root_cert) { | |||
| 199 | * | 209 | * |
| 200 | * @return 1 if the file was successfully read and 0 otherwise. | 210 | * @return 1 if the file was successfully read and 0 otherwise. |
| 201 | */ | 211 | */ |
| 202 | int get_host_certificate(gnutls_datum_t* host_cert) { | 212 | int get_host_certificate(gnutls_datum_t * host_cert) |
| 213 | { | ||
| 203 | return read_file_in_confdir(LIBIPHONE_HOST_CERTIF, host_cert); | 214 | return read_file_in_confdir(LIBIPHONE_HOST_CERTIF, host_cert); |
| 204 | } | 215 | } |
| 205 | 216 | ||
| @@ -215,30 +226,34 @@ int get_host_certificate(gnutls_datum_t* host_cert) { | |||
| 215 | * | 226 | * |
| 216 | * @return 1 on success and 0 otherwise. | 227 | * @return 1 on success and 0 otherwise. |
| 217 | */ | 228 | */ |
| 218 | int init_config_file(char* host_id, gnutls_datum_t* root_key, gnutls_datum_t* host_key, gnutls_datum_t* root_cert, gnutls_datum_t* host_cert) { | 229 | int init_config_file(char *host_id, gnutls_datum_t * root_key, gnutls_datum_t * host_key, gnutls_datum_t * root_cert, |
| 219 | FILE * pFile; | 230 | gnutls_datum_t * host_cert) |
| 220 | gchar* pem; | 231 | { |
| 221 | GKeyFile* key_file; | 232 | FILE *pFile; |
| 233 | gchar *pem; | ||
| 234 | GKeyFile *key_file; | ||
| 222 | gsize length; | 235 | gsize length; |
| 223 | gchar *buf, *config_file; | 236 | gchar *buf, *config_file; |
| 224 | GIOChannel* file; | 237 | GIOChannel *file; |
| 225 | 238 | ||
| 226 | if (!host_id || !root_key || !host_key || !root_cert || !host_cert) | 239 | if (!host_id || !root_key || !host_key || !root_cert || !host_cert) |
| 227 | return 0; | 240 | return 0; |
| 228 | 241 | ||
| 229 | /* Make sure config directory exists*/ | 242 | /* Make sure config directory exists */ |
| 230 | create_config_dir(); | 243 | create_config_dir(); |
| 231 | 244 | ||
| 232 | /* Now parse file to get the HostID */ | 245 | /* Now parse file to get the HostID */ |
| 233 | key_file = g_key_file_new(); | 246 | key_file = g_key_file_new(); |
| 234 | 247 | ||
| 235 | /* Store in config file */ | 248 | /* Store in config file */ |
| 236 | if (debug) printf("init_config_file(): setting hostID to %s\n", host_id); | 249 | if (debug) |
| 250 | printf("init_config_file(): setting hostID to %s\n", host_id); | ||
| 237 | g_key_file_set_value(key_file, "Global", "HostID", host_id); | 251 | g_key_file_set_value(key_file, "Global", "HostID", host_id); |
| 238 | 252 | ||
| 239 | /* Write config file on disk */ | 253 | /* Write config file on disk */ |
| 240 | buf = g_key_file_to_data(key_file, &length,NULL); | 254 | buf = g_key_file_to_data(key_file, &length, NULL); |
| 241 | config_file = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, LIBIPHONE_CONF_FILE, NULL); | 255 | config_file = |
| 256 | g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, LIBIPHONE_CONF_FILE, NULL); | ||
| 242 | file = g_io_channel_new_file(config_file, "w", NULL); | 257 | file = g_io_channel_new_file(config_file, "w", NULL); |
| 243 | g_free(config_file); | 258 | g_free(config_file); |
| 244 | g_io_channel_write_chars(file, buf, length, NULL, NULL); | 259 | g_io_channel_write_chars(file, buf, length, NULL, NULL); |
| @@ -248,27 +263,27 @@ int init_config_file(char* host_id, gnutls_datum_t* root_key, gnutls_datum_t* ho | |||
| 248 | g_key_file_free(key_file); | 263 | g_key_file_free(key_file); |
| 249 | 264 | ||
| 250 | /* Now write keys and certificates to disk */ | 265 | /* Now write keys and certificates to disk */ |
| 251 | pem = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, LIBIPHONE_ROOT_PRIVKEY, NULL); | 266 | pem = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, LIBIPHONE_ROOT_PRIVKEY, NULL); |
| 252 | pFile = fopen(pem , "wb"); | 267 | pFile = fopen(pem, "wb"); |
| 253 | fwrite(root_key->data, 1 , root_key->size , pFile ); | 268 | fwrite(root_key->data, 1, root_key->size, pFile); |
| 254 | fclose(pFile); | 269 | fclose(pFile); |
| 255 | g_free(pem); | 270 | g_free(pem); |
| 256 | 271 | ||
| 257 | pem = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, LIBIPHONE_HOST_PRIVKEY, NULL); | 272 | pem = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, LIBIPHONE_HOST_PRIVKEY, NULL); |
| 258 | pFile = fopen(pem , "wb"); | 273 | pFile = fopen(pem, "wb"); |
| 259 | fwrite(host_key->data, 1 , host_key->size , pFile); | 274 | fwrite(host_key->data, 1, host_key->size, pFile); |
| 260 | fclose(pFile); | 275 | fclose(pFile); |
| 261 | g_free(pem); | 276 | g_free(pem); |
| 262 | 277 | ||
| 263 | pem = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, LIBIPHONE_ROOT_CERTIF, NULL); | 278 | pem = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, LIBIPHONE_ROOT_CERTIF, NULL); |
| 264 | pFile = fopen(pem , "wb"); | 279 | pFile = fopen(pem, "wb"); |
| 265 | fwrite(root_cert->data, 1 , root_cert->size , pFile); | 280 | fwrite(root_cert->data, 1, root_cert->size, pFile); |
| 266 | fclose(pFile); | 281 | fclose(pFile); |
| 267 | g_free(pem); | 282 | g_free(pem); |
| 268 | 283 | ||
| 269 | pem = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, LIBIPHONE_HOST_CERTIF, NULL); | 284 | pem = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, LIBIPHONE_HOST_CERTIF, NULL); |
| 270 | pFile = fopen(pem , "wb"); | 285 | pFile = fopen(pem, "wb"); |
| 271 | fwrite(host_cert->data, 1 , host_cert->size , pFile); | 286 | fwrite(host_cert->data, 1, host_cert->size, pFile); |
| 272 | fclose(pFile); | 287 | fclose(pFile); |
| 273 | g_free(pem); | 288 | g_free(pem); |
| 274 | 289 | ||
diff --git a/src/userpref.h b/src/userpref.h index 553c5df..5171929 100644 --- a/src/userpref.h +++ b/src/userpref.h | |||
| @@ -28,44 +28,44 @@ | |||
| 28 | * | 28 | * |
| 29 | * @return the HostID if exist in config file. Returns NULL otherwise. | 29 | * @return the HostID if exist in config file. Returns NULL otherwise. |
| 30 | */ | 30 | */ |
| 31 | char* get_host_id(); | 31 | char *get_host_id(); |
| 32 | 32 | ||
| 33 | /** | 33 | /** |
| 34 | * Determine if we already paired this device. | 34 | * Determine if we already paired this device. |
| 35 | * | 35 | * |
| 36 | * @return 1 if device is already paired. Returns 0 otherwise. | 36 | * @return 1 if device is already paired. Returns 0 otherwise. |
| 37 | */ | 37 | */ |
| 38 | int is_device_known(char* uid); | 38 | int is_device_known(char *uid); |
| 39 | 39 | ||
| 40 | /** | 40 | /** |
| 41 | * @return 1 if everything went well. Returns 0 otherwise. | 41 | * @return 1 if everything went well. Returns 0 otherwise. |
| 42 | */ | 42 | */ |
| 43 | int store_device_public_key(char* uid, char* public_key); | 43 | int store_device_public_key(char *uid, char *public_key); |
| 44 | 44 | ||
| 45 | /** | 45 | /** |
| 46 | * @return 1 if everything went well. Returns 0 otherwise. | 46 | * @return 1 if everything went well. Returns 0 otherwise. |
| 47 | */ | 47 | */ |
| 48 | int get_root_private_key(gnutls_datum_t* root_privkey); | 48 | int get_root_private_key(gnutls_datum_t * root_privkey); |
| 49 | 49 | ||
| 50 | /** | 50 | /** |
| 51 | * @return 1 if everything went well. Returns 0 otherwise. | 51 | * @return 1 if everything went well. Returns 0 otherwise. |
| 52 | */ | 52 | */ |
| 53 | int get_host_private_key(gnutls_datum_t* host_privkey); | 53 | int get_host_private_key(gnutls_datum_t * host_privkey); |
| 54 | 54 | ||
| 55 | /** | 55 | /** |
| 56 | * @return 1 if everything went well. Returns 0 otherwise. | 56 | * @return 1 if everything went well. Returns 0 otherwise. |
| 57 | */ | 57 | */ |
| 58 | int get_root_certificate(gnutls_datum_t* root_cert); | 58 | int get_root_certificate(gnutls_datum_t * root_cert); |
| 59 | 59 | ||
| 60 | /** | 60 | /** |
| 61 | * @return 1 if everything went well. Returns 0 otherwise. | 61 | * @return 1 if everything went well. Returns 0 otherwise. |
| 62 | */ | 62 | */ |
| 63 | int get_host_certificate(gnutls_datum_t* host_cert); | 63 | int get_host_certificate(gnutls_datum_t * host_cert); |
| 64 | 64 | ||
| 65 | /** | 65 | /** |
| 66 | * Setup a brand new config file. | 66 | * Setup a brand new config file. |
| 67 | * @return 1 if everything went well. Returns 0 otherwise. | 67 | * @return 1 if everything went well. Returns 0 otherwise. |
| 68 | */ | 68 | */ |
| 69 | int init_config_file(char* host_id, gnutls_datum_t* root_key, gnutls_datum_t* host_key, gnutls_datum_t* root_cert, gnutls_datum_t* host_cert); | 69 | int init_config_file(char *host_id, gnutls_datum_t * root_key, gnutls_datum_t * host_key, gnutls_datum_t * root_cert, |
| 70 | gnutls_datum_t * host_cert); | ||
| 70 | #endif | 71 | #endif |
| 71 | |||
