summaryrefslogtreecommitdiffstats
path: root/src/usbmux.c
diff options
context:
space:
mode:
authorGravatar Matt Colyer2008-09-01 15:04:31 -0700
committerGravatar Matt Colyer2008-09-01 15:04:31 -0700
commit2b05e48cb4a90dfc94ff584124f08e431398bb1a (patch)
treee0c8255e2cd5592a31295ac8ce89d8846feb7043 /src/usbmux.c
parent7ac3d681889a6a8f9987837ace5465f2967cfff9 (diff)
downloadlibimobiledevice-2b05e48cb4a90dfc94ff584124f08e431398bb1a.tar.gz
libimobiledevice-2b05e48cb4a90dfc94ff584124f08e431398bb1a.tar.bz2
Enforce a modified kr style.
Use "make indent" from now on before committing.
Diffstat (limited to 'src/usbmux.c')
-rw-r--r--src/usbmux.c188
1 files changed, 104 insertions, 84 deletions
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 */
42usbmux_tcp_header *new_mux_packet(uint16 s_port, uint16 d_port) { 42usbmux_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 */
61usbmux_version_header *version_header() { 62usbmux_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 */
79void delete_connection(iphone_umux_client_t connection) { 81void 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
104void add_connection(iphone_umux_client_t connection) { 110void 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 */
119iphone_error_t iphone_mux_new_client ( iphone_device_t device, uint16_t src_port, uint16_t dst_port, iphone_umux_client_t *client ){ 127iphone_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 */
171iphone_error_t iphone_mux_free_client ( iphone_umux_client_t client ) { 181iphone_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
204iphone_error_t iphone_mux_send ( iphone_umux_client_t client, const char *data, uint32_t datalen, uint32_t *sent_bytes ) { 216iphone_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 */
264iphone_error_t iphone_mux_recv ( iphone_umux_client_t client, char *data, uint32_t datalen, uint32_t *recv_bytes ) { 282iphone_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