static SynceInfo* synce_info_from_midasyncd(const char* path) { bool success = false; SynceInfo* result = calloc(1, sizeof(SynceInfo)); DBusConnection* connection = NULL; DBusError error; char** device_names = NULL; int device_count = 0; DBusMessage* properties = NULL; dbus_error_init(&error); /* Connect to D-BUS */ connection = dbus_bus_get(DBUS_BUS_SYSTEM, &error); if (dbus_error_is_set (&error)) { synce_error("%s error '%s'", error.name, error.message); dbus_error_free(&error); goto exit; } if (!connection) { synce_error("Failed to connect to the D-BUS daemon"); goto exit; } if (!path) { if (!get_all_devices(connection, &device_names, &device_count)) goto exit; if (device_count < 1) goto exit; path = device_names[0]; } if (!get_all_properties(connection, path, &properties)) goto exit; if (!properties_to_info(properties, result)) goto exit; success = true; exit: /* TODO: clean up D-BUS connection */ if (device_names) dbus_free_string_array(device_names); if (success) return result; else { synce_info_destroy(result); return NULL; } }
static SynceInfo* synce_info_from_file(const char* filename) { SynceInfo* result = calloc(1, sizeof(SynceInfo)); bool success = false; char* connection_filename; struct configFile* config = NULL; if (filename) connection_filename = strdup(filename); else synce_get_connection_filename(&connection_filename); config = readConfigFile(connection_filename); if (!config) { synce_error("unable to open file: %s", connection_filename); goto exit; } result->dccm_pid = getConfigInt(config, "dccm", "pid"); result->key = getConfigInt(config, "device", "key"); result->os_version = getConfigInt(config, "device", "os_version"); result->build_number = getConfigInt(config, "device", "build_number"); result->processor_type = getConfigInt(config, "device", "processor_type"); result->partner_id_1 = getConfigInt(config, "device", "partner_id_1"); result->partner_id_2 = getConfigInt(config, "device", "partner_id_2"); result->ip = STRDUP(getConfigString(config, "device", "ip")); result->password = STRDUP(getConfigString(config, "device", "password")); result->name = STRDUP(getConfigString(config, "device", "name")); result->os_name = STRDUP(getConfigString(config, "device", "os_name")); result->model = STRDUP(getConfigString(config, "device", "model")); result->transport = STRDUP(getConfigString(config, "connection", "transport")); success = true; exit: FREE(connection_filename); if (config) unloadConfigFile(config); if (success) return result; else { synce_info_destroy(result); return NULL; } }
STDAPI rapi_context_disconnect(RapiContext* context) { if (!context->is_initialized) return E_FAIL; context->rapi_ops = NULL; if (context->own_info) { synce_info_destroy(context->info); context->info = NULL; context->own_info = false; } synce_socket_close(context->socket); context->is_initialized = false; return S_OK; }
static void rapi_context_free(RapiContext* context)/*{{{*/ { if (!context) return; /* check it against the current context, * this should never happen */ RapiContext* check_context = get_current_context(); if (check_context == context) set_current_context(NULL); rapi_buffer_free(context->send_buffer); rapi_buffer_free(context->recv_buffer); synce_socket_free(context->socket); if (context->own_info && context->info) synce_info_destroy(context->info); free(context); }/*}}}*/
HRESULT rapi_context_connect(RapiContext* context) { HRESULT result = E_FAIL; SynceInfo* info = NULL; if (context->is_initialized) { /* Fail immediately */ return CERAPI_E_ALREADYINITIALIZED; } if (context->info) info = context->info; else info = synce_info_new(NULL); if (!info) { synce_error("Failed to get connection info"); goto fail; } const char *transport = synce_info_get_transport(info); /* * original dccm or vdccm, sanity checking */ if (transport == NULL || ( strcmp(transport, "odccm") != 0 && strcmp(transport, "udev") != 0 ) ) { pid_t dccm_pid = 0; if (!(dccm_pid = synce_info_get_dccm_pid(info))) { synce_error("DCCM PID entry not found for current connection"); goto fail; } if (kill(dccm_pid, 0) < 0) { if (errno != EPERM) { synce_error("DCCM not running with pid %i", synce_info_get_dccm_pid(info)); goto fail; } } if (!synce_info_get_device_ip(info)) { synce_error("IP entry not found for current connection"); goto fail; } } /* * original dccm or vdccm */ if (transport == NULL || strncmp(transport, "ppp", 3) == 0) { /* * original dccm or vdccm */ if ( !synce_socket_connect(context->socket, synce_info_get_device_ip(info), RAPI_PORT) ) { synce_error("failed to connect to %s", synce_info_get_device_ip(info)); goto fail; } const char *password = synce_info_get_password(info); if (password && strlen(password)) { bool password_correct = false; if (!synce_password_send(context->socket, password, (unsigned char)synce_info_get_key(info))) { synce_error("failed to send password"); result = E_ACCESSDENIED; goto fail; } if (!synce_password_recv_reply(context->socket, 1, &password_correct)) { synce_error("failed to get password reply"); result = E_ACCESSDENIED; goto fail; } if (!password_correct) { synce_error("invalid password"); result = E_ACCESSDENIED; goto fail; } } context->rapi_ops = &rapi_ops; } else { /* * odccm, udev, or proxy ? */ #if ENABLE_ODCCM_SUPPORT if (strcmp(transport, "odccm") == 0) { int fd = -1; HRESULT fd_result = get_connection_from_udev_or_odccm(info, &fd); if (fd_result != S_OK) { synce_error("failed to get context fd from odccm: %08x: %s", fd_result, synce_strerror(HRESULT_CODE(fd_result))); result = fd_result; goto fail; } synce_socket_take_descriptor(context->socket, fd); } else #endif #if ENABLE_UDEV_SUPPORT if (strcmp(transport, "udev") == 0) { int fd = -1; HRESULT fd_result = get_connection_from_udev_or_odccm(info, &fd); if (fd_result != S_OK) { synce_error("failed to get context fd from udev: %08x: %s", fd_result, synce_strerror(HRESULT_CODE(fd_result))); result = fd_result; goto fail; } synce_socket_take_descriptor(context->socket, fd); } else #endif if ( !synce_socket_connect_proxy(context->socket, synce_info_get_device_ip(info)) ) { synce_error("failed to connect to proxy for %s", synce_info_get_device_ip(info)); goto fail; } /* rapi 2 seems to be used on devices with OS version of 5.1 or greater */ unsigned int os_major = 0, os_minor = 0; synce_info_get_os_version(info, &os_major, &os_minor); if ((os_major > 4) && (os_minor > 0)) context->rapi_ops = &rapi2_ops; else context->rapi_ops = &rapi_ops; } if (!context->info) { context->info = info; context->own_info = true; } context->is_initialized = true; result = S_OK; fail: if (!context->info) synce_info_destroy(info); return result; }
HRESULT rapi_context_connect(RapiContext* context) { HRESULT result = E_FAIL; SynceInfo* info = NULL; if (context->is_initialized) { /* Fail immediately */ return CERAPI_E_ALREADYINITIALIZED; } if (context->info) info = context->info; else info = synce_info_new(NULL); if (!info) { synce_error("Failed to get connection info"); goto fail; } if (!info->dccm_pid) { synce_error("DCCM PID entry not found for current connection"); goto fail; } if (kill(info->dccm_pid, 0) < 0) { if (errno != EPERM) { synce_error("DCCM not running with pid %i", info->dccm_pid); goto fail; } } if (!info->ip) { synce_error("IP entry not found for current connection"); goto fail; } if (info->transport == NULL || strncmp(info->transport, "ppp", 3) == 0) { if ( !synce_socket_connect(context->socket, info->ip, RAPI_PORT) ) { synce_error("failed to connect to %s", info->ip); goto fail; } if (info->password && strlen(info->password)) { bool password_correct = false; if (!synce_password_send(context->socket, info->password, info->key)) { synce_error("failed to send password"); result = E_ACCESSDENIED; goto fail; } if (!synce_password_recv_reply(context->socket, 1, &password_correct)) { synce_error("failed to get password reply"); result = E_ACCESSDENIED; goto fail; } if (!password_correct) { synce_error("invalid password"); result = E_ACCESSDENIED; goto fail; } } context->rapi_ops = &rapi_ops; } else { if ( !synce_socket_connect_proxy(context->socket, info->ip) ) { synce_error("failed to connect to proxy for %s", info->ip); goto fail; } context->rapi_ops = &rapi2_ops; } context->is_initialized = true; result = S_OK; fail: if (!context->info) synce_info_destroy(info); return result; }