int asr_receive(asr_client_t asr, plist_t* data) { uint32_t size = 0; char* buffer = NULL; plist_t request = NULL; idevice_error_t device_error = IDEVICE_E_SUCCESS; *data = NULL; buffer = (char*) malloc(ASR_BUFFER_SIZE); if (buffer == NULL) { error("ERROR: Unable to allocate memory for ASR receive buffer\n"); return -1; } memset(buffer, '\0', ASR_BUFFER_SIZE); device_error = idevice_connection_receive(asr->connection, buffer, ASR_BUFFER_SIZE, &size); if (device_error != IDEVICE_E_SUCCESS) { error("ERROR: Unable to receive data from ASR\n"); free(buffer); return -1; } plist_from_xml(buffer, size, &request); *data = request; debug("Received %d bytes:\n", size); if (idevicerestore_debug) debug_plist(request); free(buffer); return 0; }
/** * 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->connection) || !plist) { return PROPERTY_LIST_SERVICE_E_INVALID_ARG; } idevice_connection_receive_timeout(client->connection, (char*)&pktlen, sizeof(pktlen), &bytes, timeout); debug_info("initial read=%i", bytes); if (bytes < 4) { debug_info("initial read failed!"); return PROPERTY_LIST_SERVICE_E_MUX_ERROR; } else { pktlen = be32toh(pktlen); if (pktlen < (1 << 24)) { /* prevent huge buffers */ uint32_t curlen = 0; char *content = NULL; debug_info("%d bytes following", pktlen); content = (char*)malloc(pktlen); while (curlen < pktlen) { idevice_connection_receive(client->connection, 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 (!memcmp(content, "bplist00", 8)) { plist_from_bin(content, pktlen, plist); } else { /* 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); } if (*plist) { debug_plist(*plist); res = PROPERTY_LIST_SERVICE_E_SUCCESS; } else { res = PROPERTY_LIST_SERVICE_E_PLIST_ERROR; } free(content); content = NULL; } else { res = PROPERTY_LIST_SERVICE_E_UNKNOWN_ERROR; } } return res; }
SWIGEXPORT jshort JNICALL Java_org_robovm_libimobiledevice_binding_libimobiledeviceJNI_idevice_1connection_1receive(JNIEnv *jenv, jclass jcls, jlong jarg1, jstring jarg2, jint jarg3, jlong jarg4, jobject jarg4_) { jshort jresult = 0 ; idevice_connection_t arg1 = (idevice_connection_t) 0 ; char *arg2 = (char *) 0 ; uint32_t arg3 ; uint32_t *arg4 = (uint32_t *) 0 ; idevice_error_t result; (void)jenv; (void)jcls; (void)jarg4_; arg1 = *(idevice_connection_t *)&jarg1; arg2 = 0; if (jarg2) { arg2 = (char *)(*jenv)->GetStringUTFChars(jenv, jarg2, 0); if (!arg2) return 0; } arg3 = (uint32_t)jarg3; arg4 = *(uint32_t **)&jarg4; result = (idevice_error_t)idevice_connection_receive(arg1,arg2,arg3,arg4); jresult = (jshort)result; if (arg2) (*jenv)->ReleaseStringUTFChars(jenv, jarg2, (const char *)arg2); return jresult; }
int main(int argc, char **argv) { idevice_t phone = NULL; lockdownd_client_t client = NULL; uint16_t port = 0; char* crashLogFile; if(argc != 2) { print_usage(argc, argv); return -1; } crashLogFile = argv[1]; if (idevice_new(&phone, NULL) != IDEVICE_E_SUCCESS) { printf("No device found, is it plugged in?\n"); return -1; } if (lockdownd_client_new_with_handshake(phone, &client, "idevicecrashlog") != LOCKDOWN_E_SUCCESS) { fprintf(stderr, "Could not connect to lockdownd. Exiting.\n"); return -1; } if ((lockdownd_start_service(client, "com.apple.mobile.file_relay", &port) != LOCKDOWN_E_SUCCESS) || !port) { fprintf(stderr, "Could not start com.apple.mobile.file_relay!\n"); return -1; } const char *sources[] = {"CrashReporter", NULL}; idevice_connection_t dump = NULL; file_relay_client_t frc = NULL; if (file_relay_client_new(phone, port, &frc) != FILE_RELAY_E_SUCCESS) { printf("could not connect to file_relay service!\n"); return -1; } if (file_relay_request_sources(frc, sources, &dump) != FILE_RELAY_E_SUCCESS) { printf("could not get sources\n"); return -1; } if (!dump) { printf("did not get connection!\n"); return -1; } uint32_t cnt = 0; uint32_t len = 0; char buf[4096]; char* dumpTmpFile = tmpnam(NULL); FILE *f = fopen(dumpTmpFile, "w"); //receiving file while (idevice_connection_receive(dump, buf, 4096, &len) == IDEVICE_E_SUCCESS) { fwrite(buf, 1, len, f); cnt += len; len = 0; } fclose(f); extract_file(dumpTmpFile, crashLogFile); lockdownd_client_free(client); }
/** * 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 */ idevice_connection_receive(client->connection, (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)!", __func__, entire_len, MAXIMUM_PACKET_SIZE); } *dump_here = (char*)malloc(entire_len); if (this_len > 0) { idevice_connection_receive(client->connection, *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) { idevice_connection_receive(client->connection, (*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; }