/** * Receive binary from the device. * * @note This function returns MOBILEBACKUP2_E_SUCCESS even if no data * has been received (unless a communication error occured). * The fourth parameter is required and must be checked to know how * many bytes were actually received. * * @param client The MobileBackup client to receive from. * @param data Pointer to a buffer that will be filled with the received data. * @param length Number of bytes to receive. The data buffer needs to be large * enough to store this amount of data. * @paran bytes Number of bytes actually received. * * @return MOBILEBACKUP2_E_SUCCESS if any or no data was received, * MOBILEBACKUP2_E_INVALID_ARG if one of the parameters is invalid, * or MOBILEBACKUP2_E_MUX_ERROR if receiving the data failed. */ mobilebackup2_error_t mobilebackup2_receive_raw(mobilebackup2_client_t client, char *data, uint32_t length, uint32_t *bytes) { if (!client || !client->parent || !data || (length == 0) || !bytes) return MOBILEBACKUP2_E_INVALID_ARG; service_client_t raw = client->parent->parent->parent; *bytes = 0; int bytes_loc = 0; uint32_t received = 0; do { bytes_loc = 0; service_receive(raw, data+received, length-received, (uint32_t*)&bytes_loc); if (bytes_loc <= 0) break; received += bytes_loc; } while (received < length); if (received > 0) { *bytes = received; return MOBILEBACKUP2_E_SUCCESS; } else if (received == 0) { return MOBILEBACKUP2_E_SUCCESS; } else { return MOBILEBACKUP2_E_MUX_ERROR; } }
/** * Receives a plist using the given property list service client. * Internally used generic plist receive function. * * @param client The property list service client to use for receiving * @param plist pointer to a plist_t that will point to the received plist * upon successful return * @param timeout Maximum time in milliseconds to wait for data. * * @return PROPERTY_LIST_SERVICE_E_SUCCESS on success, * PROPERTY_LIST_SERVICE_E_INVALID_ARG when client or *plist is NULL, * PROPERTY_LIST_SERVICE_E_PLIST_ERROR when the received data cannot be * converted to a plist, PROPERTY_LIST_SERVICE_E_MUX_ERROR when a * communication error occurs, or PROPERTY_LIST_SERVICE_E_UNKNOWN_ERROR * when an unspecified error occurs. */ static property_list_service_error_t internal_plist_receive_timeout(property_list_service_client_t client, plist_t *plist, unsigned int timeout) { property_list_service_error_t res = PROPERTY_LIST_SERVICE_E_UNKNOWN_ERROR; uint32_t pktlen = 0; uint32_t bytes = 0; if (!client || (client && !client->parent) || !plist) { return PROPERTY_LIST_SERVICE_E_INVALID_ARG; } *plist = NULL; service_error_t serr = service_receive_with_timeout(client->parent, (char*)&pktlen, sizeof(pktlen), &bytes, timeout); if ((serr == SERVICE_E_SUCCESS) && (bytes == 0)) { return PROPERTY_LIST_SERVICE_E_RECEIVE_TIMEOUT; } debug_info("initial read=%i", bytes); if (bytes < 4) { debug_info("initial read failed!"); return PROPERTY_LIST_SERVICE_E_MUX_ERROR; } else { uint32_t curlen = 0; char *content = NULL; pktlen = be32toh(pktlen); debug_info("%d bytes following", pktlen); content = (char*)malloc(pktlen); if (!content) { debug_info("out of memory when allocating %d bytes", pktlen); return PROPERTY_LIST_SERVICE_E_UNKNOWN_ERROR; } while (curlen < pktlen) { service_receive(client->parent, content+curlen, pktlen-curlen, &bytes); if (bytes <= 0) { res = PROPERTY_LIST_SERVICE_E_MUX_ERROR; break; } debug_info("received %d bytes", bytes); curlen += bytes; } if (curlen < pktlen) { debug_info("received incomplete packet (%d of %d bytes)", curlen, pktlen); if (curlen > 0) { debug_info("incomplete packet following:"); debug_buffer(content, curlen); } free(content); return res; } if ((pktlen > 8) && !memcmp(content, "bplist00", 8)) { plist_from_bin(content, pktlen, plist); } else if ((pktlen > 5) && !memcmp(content, "<?xml", 5)) { /* iOS 4.3+ hack: plist data might contain invalid characters, thus we convert those to spaces */ for (bytes = 0; bytes < pktlen-1; bytes++) { if ((content[bytes] >= 0) && (content[bytes] < 0x20) && (content[bytes] != 0x09) && (content[bytes] != 0x0a) && (content[bytes] != 0x0d)) content[bytes] = 0x20; } plist_from_xml(content, pktlen, plist); } else { debug_info("WARNING: received unexpected non-plist content"); debug_buffer(content, pktlen); } if (*plist) { debug_plist(*plist); res = PROPERTY_LIST_SERVICE_E_SUCCESS; } else { res = PROPERTY_LIST_SERVICE_E_PLIST_ERROR; } free(content); content = NULL; } return res; }
/** * Receives data through an AFC client and sets a variable to the received data. * * @param client The client to receive data on. * @param dump_here The char* to point to the newly-received data. * @param bytes_recv How much data was received. * * @return AFC_E_SUCCESS on success or an AFC_E_* error value. */ static afc_error_t afc_receive_data(afc_client_t client, char **dump_here, uint32_t *bytes_recv) { AFCPacket header; uint32_t entire_len = 0; uint32_t this_len = 0; uint32_t current_count = 0; uint64_t param1 = -1; *bytes_recv = 0; /* first, read the AFC header */ service_receive(client->parent, (char*)&header, sizeof(AFCPacket), bytes_recv); AFCPacket_from_LE(&header); if (*bytes_recv == 0) { debug_info("Just didn't get enough."); *dump_here = NULL; return AFC_E_MUX_ERROR; } else if (*bytes_recv < sizeof(AFCPacket)) { debug_info("Did not even get the AFCPacket header"); *dump_here = NULL; return AFC_E_MUX_ERROR; } /* check if it's a valid AFC header */ if (strncmp(header.magic, AFC_MAGIC, AFC_MAGIC_LEN)) { debug_info("Invalid AFC packet received (magic != " AFC_MAGIC ")!"); } /* check if it has the correct packet number */ if (header.packet_num != client->afc_packet->packet_num) { /* otherwise print a warning but do not abort */ debug_info("ERROR: Unexpected packet number (%lld != %lld) aborting.", header.packet_num, client->afc_packet->packet_num); *dump_here = NULL; return AFC_E_OP_HEADER_INVALID; } /* then, read the attached packet */ if (header.this_length < sizeof(AFCPacket)) { debug_info("Invalid AFCPacket header received!"); *dump_here = NULL; return AFC_E_OP_HEADER_INVALID; } else if ((header.this_length == header.entire_length) && header.entire_length == sizeof(AFCPacket)) { debug_info("Empty AFCPacket received!"); *dump_here = NULL; *bytes_recv = 0; if (header.operation == AFC_OP_DATA) { return AFC_E_SUCCESS; } else { return AFC_E_IO_ERROR; } } debug_info("received AFC packet, full len=%lld, this len=%lld, operation=0x%llx", header.entire_length, header.this_length, header.operation); entire_len = (uint32_t)header.entire_length - sizeof(AFCPacket); this_len = (uint32_t)header.this_length - sizeof(AFCPacket); /* this is here as a check (perhaps a different upper limit is good?) */ if (entire_len > (uint32_t)MAXIMUM_PACKET_SIZE) { fprintf(stderr, "%s: entire_len is larger than MAXIMUM_PACKET_SIZE, (%d > %d)!", __FUNCTION__, entire_len, MAXIMUM_PACKET_SIZE); } *dump_here = (char*)malloc(entire_len); if (this_len > 0) { service_receive(client->parent, *dump_here, this_len, bytes_recv); if (*bytes_recv <= 0) { free(*dump_here); *dump_here = NULL; debug_info("Did not get packet contents!"); return AFC_E_NOT_ENOUGH_DATA; } else if (*bytes_recv < this_len) { free(*dump_here); *dump_here = NULL; debug_info("Could not receive this_len=%d bytes", this_len); return AFC_E_NOT_ENOUGH_DATA; } } current_count = this_len; if (entire_len > this_len) { while (current_count < entire_len) { service_receive(client->parent, (*dump_here)+current_count, entire_len - current_count, bytes_recv); if (*bytes_recv <= 0) { debug_info("Error receiving data (recv returned %d)", *bytes_recv); break; } current_count += *bytes_recv; } if (current_count < entire_len) { debug_info("WARNING: could not receive full packet (read %s, size %d)", current_count, entire_len); } } if (current_count >= sizeof(uint64_t)) { param1 = le64toh(*(uint64_t*)(*dump_here)); } debug_info("packet data size = %i", current_count); debug_info("packet data follows"); debug_buffer(*dump_here, current_count); /* check operation types */ if (header.operation == AFC_OP_STATUS) { /* status response */ debug_info("got a status response, code=%lld", param1); if (param1 != AFC_E_SUCCESS) { /* error status */ /* free buffer */ free(*dump_here); *dump_here = NULL; return (afc_error_t)param1; } } else if (header.operation == AFC_OP_DATA) { /* data response */ debug_info("got a data response"); } else if (header.operation == AFC_OP_FILE_OPEN_RES) { /* file handle response */ debug_info("got a file handle response, handle=%lld", param1); } else if (header.operation == AFC_OP_FILE_TELL_RES) { /* tell response */ debug_info("got a tell response, position=%lld", param1); } else { /* unknown operation code received */ free(*dump_here); *dump_here = NULL; *bytes_recv = 0; debug_info("WARNING: Unknown operation code received 0x%llx param1=%lld", header.operation, param1); #ifndef WIN32 fprintf(stderr, "%s: WARNING: Unknown operation code received 0x%llx param1=%lld", __func__, (long long)header.operation, (long long)param1); #endif return AFC_E_OP_NOT_SUPPORTED; } *bytes_recv = current_count; return AFC_E_SUCCESS; }
int hslink_input(user_data_rec *udta, /* current user data record */ int argc, /* argument count */ char *argv[]) /* argument values */ { /* allocate application workspace if needed */ if (udta->substt == 0) { udta->appws = (workspace_rec*)mem_alloc(sizeof(workspace_rec)); if (udta->appws == 0) { cprintf("Cannot allocate workspace for user %d!\r\n",usrnum); return 0; } } /* current workspace to application workspace for this user */ current_hsws = udta->appws; /* process current substate */ switch (udta->substt) { case 0: if (top_init()) { udta->substt = 0; break; } set_defaults(); if (argc == 1) { usage("No command line given\r\n",""); udta->substt = 0; break; } if (process_options(argc,argv)) { usage("No command line given\r\n",""); udta->substt = 0; break; } ComOpen(); WS.Option.ComSpeed = ComGetSpeed(); if (!WS.Option.EffSpeed) WS.Option.EffSpeed = WS.Option.ComSpeed; /* we're now ready for SlowHandshake to work */ WS.IoLevel = 0; /* allocate up to 10k for file buffers, but no more */ WS.buffer_sizes = mem_avail()-1000>10240: 10240:mem_avail()-1000; /* display opening screen */ prepare_display(); process_filespecs(argc,argv); /* verify hardware handshake status */ if (!ComGetCts() && !WS.Option.ForceCts) { cprintf("CTS signal missing! Please use -HC or -FC option.\r\n"); WS.Option.CtsHandshake = 0; } udta->substt = 1; break; case 1: /* wait for ready handshake with remote */ service_receive(); udta->substt = wait_for_ready()? 1:2; break; case 2: /* select first file in batch and begin transmit */ udta->curnode = WS.first_send; udta->substt = 3; break; case 3: /* end of batch? change to state 4 */ if (udta->curnode == NULL) { udta->substt = 4; PSEND("%d file%s transmitted.\r\n",WS.files_sent, WS.files_sent==1?"":"s"); break; } service_receive(); if (transmit_file(udta->curnode->name)) break; /* remain in state 3 */ /* advance to next file in the batch */ udta->curnode = udta->curnode->next; break; case 4: /* wait for remaining receive activity to terminate */ service_receive(); udta->substt = finish_receive()? 4:5; break; case 5: /* close down link */ udta->substt = terminate_link()? 5:6; break; case 6: /* process exit codes */ if (ComCarrierLost()) set_cancel_link(CANCEL_CARRIER_LOST); if ((WS.files_received+WS.files_sent) ==0) set_cancel_link(CANCEL_NO_FILES); ComClose(); close_display(); cprintf("HS/Link finished! (t:%d r:%d)",WS.files_sent,WS.files_received); cprintf(" Exit code = %d\r\n",(int)WS.cancel_link); udta->substt = 0; break; } /* if substt is 0 we are done and need to free the workspace */ if (udta->substt == 0) { current_hsws = 0; mem_free(udta->appws); udta->appws = 0; } /* otherwise we need more calls- return non 0 */ return udta->substt; }