summaryrefslogtreecommitdiffstats
path: root/src/usbmux.c
diff options
context:
space:
mode:
authorGravatar Jonathan Beck2008-08-31 11:47:18 +0200
committerGravatar Jonathan Beck2008-08-31 19:33:19 +0200
commit86f61988990daa30c8564e2b26666dd442bd6e65 (patch)
tree985400ee8848585dabf8efbe2acbfc37d9b3f2f8 /src/usbmux.c
parentdc89741f00bd6919c0eedbd882c05f66d72fdbee (diff)
downloadlibimobiledevice-86f61988990daa30c8564e2b26666dd442bd6e65.tar.gz
libimobiledevice-86f61988990daa30c8564e2b26666dd442bd6e65.tar.bz2
make all functions return an error code.
Diffstat (limited to 'src/usbmux.c')
-rw-r--r--src/usbmux.c48
1 files changed, 30 insertions, 18 deletions
diff --git a/src/usbmux.c b/src/usbmux.c
index 5e3f441..8d85245 100644
--- a/src/usbmux.c
+++ b/src/usbmux.c
@@ -116,7 +116,7 @@ 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. 116 * @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. 117 * @return IPHONE_E_SUCCESS on success, an error code otherwise.
118 */ 118 */
119int iphone_mux_new_client ( iphone_device_t device, uint16_t src_port, uint16_t dst_port, iphone_umux_client_t *client ){ 119iphone_error_t iphone_mux_new_client ( iphone_device_t device, uint16_t src_port, uint16_t dst_port, iphone_umux_client_t *client ){
120 if (!device || !src_port || !dst_port) 120 if (!device || !src_port || !dst_port)
121 return IPHONE_E_INVALID_ARG; 121 return IPHONE_E_INVALID_ARG;
122 122
@@ -165,8 +165,10 @@ int iphone_mux_new_client ( iphone_device_t device, uint16_t src_port, uint16_t
165 * @note Once a connection is closed it may not be used again. 165 * @note Once a connection is closed it may not be used again.
166 * 166 *
167 * @param connection The connection to close. 167 * @param connection The connection to close.
168 *
169 * @return IPHONE_E_SUCCESS on success.
168 */ 170 */
169void iphone_mux_free_client ( iphone_umux_client_t client ) { 171iphone_error_t iphone_mux_free_client ( iphone_umux_client_t client ) {
170 if (!client || !client->phone) return; 172 if (!client || !client->phone) return;
171 173
172 client->header->tcp_flags = 0x04; 174 client->header->tcp_flags = 0x04;
@@ -183,6 +185,8 @@ void iphone_mux_free_client ( iphone_umux_client_t client ) {
183 printf("get_iPhone(): when reading, libusb gave me the error: %s\n", usb_strerror()); 185 printf("get_iPhone(): when reading, libusb gave me the error: %s\n", usb_strerror());
184 186
185 delete_connection(client); 187 delete_connection(client);
188
189 return IPHONE_E_SUCCESS;
186} 190}
187 191
188 192
@@ -192,15 +196,16 @@ void iphone_mux_free_client ( iphone_umux_client_t client ) {
192 * @param client The client we're sending data on. 196 * @param client The client we're sending data on.
193 * @param data A pointer to the data to send. 197 * @param data A pointer to the data to send.
194 * @param datalen How much data we're sending. 198 * @param datalen How much data we're sending.
199 * @param sent_bytes The number of bytes sent, minus the header (28)
195 * 200 *
196 * @return The number of bytes sent, minus the header (28), or -1 on error. 201 * @return IPHONE_E_SUCCESS on success.
197 */ 202 */
198 203
199int iphone_mux_send ( iphone_umux_client_t client, const char *data, uint32_t datalen ) { 204iphone_error_t iphone_mux_send ( iphone_umux_client_t client, const char *data, uint32_t datalen, uint32_t *sent_bytes ) {
200 if (!client->phone || !client || !data || datalen == 0) return -1; 205 if (!client->phone || !client || !data || datalen == 0 || !sent_bytes) return IPHONE_E_INVALID_ARG;
201 // client->scnt and client->ocnt should already be in host notation... 206 // client->scnt and client->ocnt should already be in host notation...
202 // we don't need to change them juuuust yet. 207 // we don't need to change them juuuust yet.
203 int bytes = 0; 208 *sent_bytes = 0;
204 if (debug) printf("mux_send(): client wants to send %i bytes\n", datalen); 209 if (debug) printf("mux_send(): client wants to send %i bytes\n", datalen);
205 char *buffer = (char*)malloc(sizeof(usbmux_tcp_header) + datalen + 2); // allow 2 bytes of safety padding 210 char *buffer = (char*)malloc(sizeof(usbmux_tcp_header) + datalen + 2); // allow 2 bytes of safety padding
206 // Set the length and pre-emptively htonl/htons it 211 // Set the length and pre-emptively htonl/htons it
@@ -218,12 +223,12 @@ int iphone_mux_send ( iphone_umux_client_t client, const char *data, uint32_t da
218 if (debug) printf("actually sending %zi bytes of data at %p\n", sizeof(usbmux_tcp_header)+datalen, buffer); 223 if (debug) printf("actually sending %zi bytes of data at %p\n", sizeof(usbmux_tcp_header)+datalen, buffer);
219 224
220 225
221 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);
222 if (debug) printf("mux_send: sent %i bytes!\n", bytes); 227 if (debug) printf("mux_send: sent %i bytes!\n", *sent_bytes);
223 // Now that we've sent it off, we can clean up after our sloppy selves. 228 // Now that we've sent it off, we can clean up after our sloppy selves.
224 if (debug) { 229 if (debug) {
225 FILE *packet = fopen("packet", "a+"); 230 FILE *packet = fopen("packet", "a+");
226 fwrite(buffer, 1, bytes, packet); 231 fwrite(buffer, 1, *sent_bytes, packet);
227 fclose(packet); 232 fclose(packet);
228 printf("\n"); 233 printf("\n");
229 } 234 }
@@ -238,13 +243,14 @@ int iphone_mux_send ( iphone_umux_client_t client, const char *data, uint32_t da
238 client->header->length16 = ntohs(client->header->length16); 243 client->header->length16 = ntohs(client->header->length16);
239 244
240 // Now return the bytes. 245 // Now return the bytes.
241 if (bytes < sizeof(usbmux_tcp_header)+datalen) { 246 if (*sent_bytes < sizeof(usbmux_tcp_header)+datalen) {
242 return -1; // blah 247 *sent_bytes = 0;
248 return IPHONE_E_NOT_ENOUGH_DATA;
243 } else { 249 } else {
244 return bytes - 28; // actual length sent. :/ 250 *sent_bytes = *sent_bytes - 28; // actual length sent. :/
245 } 251 }
246 252
247 return bytes; // or something 253 return IPHONE_E_UNKNOWN_ERROR;
248} 254}
249 255
250/** This is a higher-level USBMuxTCP-like function 256/** This is a higher-level USBMuxTCP-like function
@@ -255,7 +261,10 @@ int iphone_mux_send ( iphone_umux_client_t client, const char *data, uint32_t da
255 * 261 *
256 * @return How many bytes were read, or -1 if something bad happens. 262 * @return How many bytes were read, or -1 if something bad happens.
257 */ 263 */
258int iphone_mux_recv ( iphone_umux_client_t client, char *data, uint32_t datalen ) { 264iphone_error_t iphone_mux_recv ( iphone_umux_client_t client, char *data, uint32_t datalen, uint32_t *recv_bytes ) {
265
266 if (!client || !data || datalen == 0 || !recv_bytes)
267 return IPHONE_E_INVALID_ARG;
259 /* 268 /*
260 * Order of operation: 269 * Order of operation:
261 * 1.) Check if the client has a pre-received buffer. 270 * 1.) Check if the client has a pre-received buffer.
@@ -268,6 +277,7 @@ int iphone_mux_recv ( iphone_umux_client_t client, char *data, uint32_t datalen
268 */ 277 */
269 if (debug) printf("mux_recv: datalen == %i\n", datalen); 278 if (debug) printf("mux_recv: datalen == %i\n", datalen);
270 int bytes = 0, i = 0, complex = 0, offset = 0; 279 int bytes = 0, i = 0, complex = 0, offset = 0;
280 *recv_bytes = 0;
271 char *buffer = NULL; 281 char *buffer = NULL;
272 usbmux_tcp_header *header = NULL; 282 usbmux_tcp_header *header = NULL;
273 283
@@ -304,7 +314,7 @@ int iphone_mux_recv ( iphone_umux_client_t client, char *data, uint32_t datalen
304 if (bytes < 28) { 314 if (bytes < 28) {
305 free(buffer); 315 free(buffer);
306 if (debug) printf("mux_recv: Did not even get the header.\n"); 316 if (debug) printf("mux_recv: Did not even get the header.\n");
307 return -1; 317 return IPHONE_E_NOT_ENOUGH_DATA;
308 } 318 }
309 319
310 header = (usbmux_tcp_header*)buffer; 320 header = (usbmux_tcp_header*)buffer;
@@ -332,7 +342,7 @@ int iphone_mux_recv ( iphone_umux_client_t client, char *data, uint32_t datalen
332 // Free our buffer and continue. 342 // Free our buffer and continue.
333 free(buffer); 343 free(buffer);
334 buffer = NULL; 344 buffer = NULL;
335 return iphone_mux_recv(client, data, datalen); // recurse back in to try again 345 return iphone_mux_recv(client, data, datalen, recv_bytes); // recurse back in to try again
336 } 346 }
337 347
338 // The packet was absolutely meant for us if it hits this point. 348 // The packet was absolutely meant for us if it hits this point.
@@ -348,13 +358,15 @@ int iphone_mux_recv ( iphone_umux_client_t client, char *data, uint32_t datalen
348 memcpy(client->recv_buffer+complex, buffer+28+datalen, (bytes-28) - datalen); 358 memcpy(client->recv_buffer+complex, buffer+28+datalen, (bytes-28) - datalen);
349 free(buffer); 359 free(buffer);
350 client->header->ocnt += bytes-28; 360 client->header->ocnt += bytes-28;
351 return datalen; 361 *recv_bytes = datalen;
362 return IPHONE_E_SUCCESS;
352 } else { 363 } else {
353 // Fill the data with what we have, and just return. 364 // Fill the data with what we have, and just return.
354 memcpy(data+offset, buffer+28, bytes-28); // data+offset: see #2b, above 365 memcpy(data+offset, buffer+28, bytes-28); // data+offset: see #2b, above
355 client->header->ocnt += bytes-28; 366 client->header->ocnt += bytes-28;
356 free(buffer); 367 free(buffer);
357 return (bytes-28); 368 *recv_bytes = bytes - 28;
369 return IPHONE_E_SUCCESS;
358 } 370 }
359 371
360 // If we get to this point, 'tis probably bad. 372 // If we get to this point, 'tis probably bad.