void Core::makeTox(QByteArray savedata) { // IPv6 needed for LAN discovery, but can crash some weird routers. On by default, can be disabled in options. bool enableIPv6 = Settings::getInstance().getEnableIPv6(); bool forceTCP = Settings::getInstance().getForceTCP(); Settings::ProxyType proxyType = Settings::getInstance().getProxyType(); quint16 proxyPort = Settings::getInstance().getProxyPort(); QString proxyAddr = Settings::getInstance().getProxyAddr(); QByteArray proxyAddrData = proxyAddr.toUtf8(); if (enableIPv6) qDebug() << "Core starting with IPv6 enabled"; else qWarning() << "Core starting with IPv6 disabled. LAN discovery may not work properly."; Tox_Options toxOptions; tox_options_default(&toxOptions); toxOptions.ipv6_enabled = enableIPv6; toxOptions.udp_enabled = !forceTCP; toxOptions.start_port = toxOptions.end_port = 0; // No proxy by default toxOptions.proxy_type = TOX_PROXY_TYPE_NONE; toxOptions.proxy_host = nullptr; toxOptions.proxy_port = 0; toxOptions.savedata_type = (!savedata.isNull() ? TOX_SAVEDATA_TYPE_TOX_SAVE : TOX_SAVEDATA_TYPE_NONE); toxOptions.savedata_data = (uint8_t*)savedata.data(); toxOptions.savedata_length = savedata.size(); if (proxyType != Settings::ProxyType::ptNone) { if (proxyAddr.length() > 255) { qWarning() << "proxy address" << proxyAddr << "is too long"; } else if (proxyAddr != "" && proxyPort > 0) { qDebug() << "using proxy" << proxyAddr << ":" << proxyPort; // protection against changings in TOX_PROXY_TYPE enum if (proxyType == Settings::ProxyType::ptSOCKS5) toxOptions.proxy_type = TOX_PROXY_TYPE_SOCKS5; else if (proxyType == Settings::ProxyType::ptHTTP) toxOptions.proxy_type = TOX_PROXY_TYPE_HTTP; toxOptions.proxy_host = proxyAddrData.data(); toxOptions.proxy_port = proxyPort; } } TOX_ERR_NEW tox_err; tox = tox_new(&toxOptions, &tox_err); switch (tox_err) { case TOX_ERR_NEW_OK: break; case TOX_ERR_NEW_LOAD_BAD_FORMAT: qWarning() << "failed to parse Tox save data"; break; case TOX_ERR_NEW_PORT_ALLOC: if (enableIPv6) { toxOptions.ipv6_enabled = false; tox = tox_new(&toxOptions, &tox_err); if (tox_err == TOX_ERR_NEW_OK) { qWarning() << "Core failed to start with IPv6, falling back to IPv4. LAN discovery may not work properly."; break; } } qCritical() << "can't to bind the port"; emit failedToStart(); return; case TOX_ERR_NEW_PROXY_BAD_HOST: case TOX_ERR_NEW_PROXY_BAD_PORT: case TOX_ERR_NEW_PROXY_BAD_TYPE: qCritical() << "bad proxy, error code:" << tox_err; emit badProxy(); return; case TOX_ERR_NEW_PROXY_NOT_FOUND: qCritical() << "proxy not found"; emit badProxy(); return; case TOX_ERR_NEW_LOAD_ENCRYPTED: qCritical() << "attempted to load encrypted Tox save data"; emit failedToStart(); return; case TOX_ERR_NEW_MALLOC: qCritical() << "memory allocation failed"; emit failedToStart(); return; case TOX_ERR_NEW_NULL: qCritical() << "a parameter was null"; emit failedToStart(); return; default: qCritical() << "Tox core failed to start, unknown error code:" << tox_err; emit failedToStart(); return; } av = new CoreAV(tox); if (av->getToxAv() == nullptr) { qCritical() << "Toxav core failed to start"; emit failedToStart(); return; } }
void Core::start() { // IPv6 needed for LAN discovery, but can crash some weird routers. On by default, can be disabled in options. bool enableIPv6 = Settings::getInstance().getEnableIPv6(); bool forceTCP = Settings::getInstance().getForceTCP(); bool useProxy = Settings::getInstance().getUseProxy(); if (enableIPv6) qDebug() << "Core starting with IPv6 enabled"; else qWarning() << "Core starting with IPv6 disabled. LAN discovery may not work properly."; Tox_Options toxOptions; toxOptions.ipv6enabled = enableIPv6; toxOptions.udp_disabled = forceTCP; // No proxy by default toxOptions.proxy_enabled = false; toxOptions.proxy_address[0] = 0; toxOptions.proxy_port = 0; if (useProxy) { QString proxyAddr = Settings::getInstance().getProxyAddr(); int proxyPort = Settings::getInstance().getProxyPort(); if (proxyAddr.length() > 255) { qWarning() << "Core: proxy address" << proxyAddr << "is too long"; } else if (proxyAddr != "" && proxyPort > 0) { qDebug() << "Core: using proxy" << proxyAddr << ":" << proxyPort; toxOptions.proxy_enabled = true; uint16_t sz = CString::fromString(proxyAddr, (unsigned char*)toxOptions.proxy_address); toxOptions.proxy_address[sz] = 0; toxOptions.proxy_port = proxyPort; } } tox = tox_new(&toxOptions); if (tox == nullptr) { if (enableIPv6) // Fallback to IPv4 { toxOptions.ipv6enabled = false; tox = tox_new(&toxOptions); if (tox == nullptr) { if (toxOptions.proxy_enabled) { //QMessageBox::critical(Widget::getInstance(), tr("Proxy failure", "popup title"), //tr("toxcore failed to start with your proxy settings. qTox cannot run; please modify your " //"settings and restart.", "popup text")); qCritical() << "Core: bad proxy! no toxcore!"; emit badProxy(); } else { qCritical() << "Tox core failed to start"; emit failedToStart(); } return; } else qWarning() << "Core failed to start with IPv6, falling back to IPv4. LAN discovery may not work properly."; } else if (toxOptions.proxy_enabled) { emit badProxy(); return; } else { qCritical() << "Tox core failed to start"; emit failedToStart(); return; } } toxav = toxav_new(tox, TOXAV_MAX_CALLS); if (toxav == nullptr) { qCritical() << "Toxav core failed to start"; emit failedToStart(); return; } qsrand(time(nullptr)); if (!loadConfiguration()) { emit failedToStart(); tox_kill(tox); tox = nullptr; return; } tox_callback_friend_request(tox, onFriendRequest, this); tox_callback_friend_message(tox, onFriendMessage, this); tox_callback_friend_action(tox, onAction, this); tox_callback_name_change(tox, onFriendNameChange, this); tox_callback_typing_change(tox, onFriendTypingChange, this); tox_callback_status_message(tox, onStatusMessageChanged, this); tox_callback_user_status(tox, onUserStatusChanged, this); tox_callback_connection_status(tox, onConnectionStatusChanged, this); tox_callback_group_invite(tox, onGroupInvite, this); tox_callback_group_message(tox, onGroupMessage, this); tox_callback_group_namelist_change(tox, onGroupNamelistChange, this); tox_callback_file_send_request(tox, onFileSendRequestCallback, this); tox_callback_file_control(tox, onFileControlCallback, this); tox_callback_file_data(tox, onFileDataCallback, this); tox_callback_avatar_info(tox, onAvatarInfoCallback, this); tox_callback_avatar_data(tox, onAvatarDataCallback, this); toxav_register_callstate_callback(toxav, onAvInvite, av_OnInvite, this); toxav_register_callstate_callback(toxav, onAvStart, av_OnStart, this); toxav_register_callstate_callback(toxav, onAvCancel, av_OnCancel, this); toxav_register_callstate_callback(toxav, onAvReject, av_OnReject, this); toxav_register_callstate_callback(toxav, onAvEnd, av_OnEnd, this); toxav_register_callstate_callback(toxav, onAvRinging, av_OnRinging, this); toxav_register_callstate_callback(toxav, onAvStarting, av_OnStarting, this); toxav_register_callstate_callback(toxav, onAvEnding, av_OnEnding, this); toxav_register_callstate_callback(toxav, onAvMediaChange, av_OnMediaChange, this); toxav_register_callstate_callback(toxav, onAvRequestTimeout, av_OnRequestTimeout, this); toxav_register_callstate_callback(toxav, onAvPeerTimeout, av_OnPeerTimeout, this); toxav_register_audio_recv_callback(toxav, playCallAudio, this); toxav_register_video_recv_callback(toxav, playCallVideo, this); uint8_t friendAddress[TOX_FRIEND_ADDRESS_SIZE]; tox_get_address(tox, friendAddress); emit friendAddressGenerated(CFriendAddress::toString(friendAddress)); QPixmap pic = Settings::getInstance().getSavedAvatar(getSelfId().toString()); if (!pic.isNull() && !pic.size().isEmpty()) { QByteArray data; QBuffer buffer(&data); buffer.open(QIODevice::WriteOnly); pic.save(&buffer, "PNG"); buffer.close(); setAvatar(TOX_AVATAR_FORMAT_PNG, data); } else qDebug() << "Core: Error loading self avatar"; process(); // starts its own timer }
/** * @brief Creates Tox instance from previously saved data * @param savedata Previously saved Tox data - null, if new profile was created */ void Core::makeTox(QByteArray savedata) { Tox_Options toxOptions = initToxOptions(savedata); TOX_ERR_NEW tox_err; tox = tox_new(&toxOptions, &tox_err); switch (tox_err) { case TOX_ERR_NEW_OK: break; case TOX_ERR_NEW_LOAD_BAD_FORMAT: qCritical() << "failed to parse Tox save data"; emit failedToStart(); return; case TOX_ERR_NEW_PORT_ALLOC: if (Settings::getInstance().getEnableIPv6()) { toxOptions.ipv6_enabled = false; tox = tox_new(&toxOptions, &tox_err); if (tox_err == TOX_ERR_NEW_OK) { qWarning() << "Core failed to start with IPv6, falling back to IPv4. LAN discovery " "may not work properly."; break; } } qCritical() << "can't to bind the port"; emit failedToStart(); return; case TOX_ERR_NEW_PROXY_BAD_HOST: case TOX_ERR_NEW_PROXY_BAD_PORT: case TOX_ERR_NEW_PROXY_BAD_TYPE: qCritical() << "bad proxy, error code:" << tox_err; emit badProxy(); return; case TOX_ERR_NEW_PROXY_NOT_FOUND: qCritical() << "proxy not found"; emit badProxy(); return; case TOX_ERR_NEW_LOAD_ENCRYPTED: qCritical() << "attempted to load encrypted Tox save data"; emit failedToStart(); return; case TOX_ERR_NEW_MALLOC: qCritical() << "memory allocation failed"; emit failedToStart(); return; case TOX_ERR_NEW_NULL: qCritical() << "a parameter was null"; emit failedToStart(); return; default: qCritical() << "Tox core failed to start, unknown error code:" << tox_err; emit failedToStart(); return; } }
void Core::make_tox(QByteArray savedata) { // IPv6 needed for LAN discovery, but can crash some weird routers. On by default, can be disabled in options. bool enableIPv6 = Settings::getInstance().getEnableIPv6(); bool forceTCP = Settings::getInstance().getForceTCP(); ProxyType proxyType = Settings::getInstance().getProxyType(); if (enableIPv6) qDebug() << "Core starting with IPv6 enabled"; else qWarning() << "Core starting with IPv6 disabled. LAN discovery may not work properly."; Tox_Options toxOptions; tox_options_default(&toxOptions); toxOptions.ipv6_enabled = enableIPv6; toxOptions.udp_enabled = !forceTCP; toxOptions.start_port = toxOptions.end_port = 0; // No proxy by default toxOptions.proxy_type = TOX_PROXY_TYPE_NONE; toxOptions.proxy_host = nullptr; toxOptions.proxy_port = 0; if (proxyType != ProxyType::ptNone) { QString proxyAddr = Settings::getInstance().getProxyAddr(); int proxyPort = Settings::getInstance().getProxyPort(); if (proxyAddr.length() > 255) { qWarning() << "Core: proxy address" << proxyAddr << "is too long"; } else if (proxyAddr != "" && proxyPort > 0) { qDebug() << "Core: using proxy" << proxyAddr << ":" << proxyPort; // protection against changings in TOX_PROXY_TYPE enum if (proxyType == ProxyType::ptSOCKS5) toxOptions.proxy_type = TOX_PROXY_TYPE_SOCKS5; else if (proxyType == ProxyType::ptHTTP) toxOptions.proxy_type = TOX_PROXY_TYPE_HTTP; QByteArray proxyAddrData = proxyAddr.toUtf8(); /// TODO: We're leaking a tiny amount of memory there, go fix that later char* proxyAddrCopy = new char[proxyAddrData.size()+1]; memcpy(proxyAddrCopy, proxyAddrData.data(), proxyAddrData.size()+1); toxOptions.proxy_host = proxyAddrCopy; toxOptions.proxy_port = proxyPort; } } tox = tox_new(&toxOptions, (uint8_t*)savedata.data(), savedata.size(), nullptr); if (tox == nullptr) { if (enableIPv6) // Fallback to IPv4 { toxOptions.ipv6_enabled = false; tox = tox_new(&toxOptions, (uint8_t*)savedata.data(), savedata.size(), nullptr); if (tox == nullptr) { if (toxOptions.proxy_type != TOX_PROXY_TYPE_NONE) { qCritical() << "Core: bad proxy! no toxcore!"; emit badProxy(); } else { qCritical() << "Tox core failed to start"; emit failedToStart(); } return; } else { qWarning() << "Core failed to start with IPv6, falling back to IPv4. LAN discovery may not work properly."; } } else if (toxOptions.proxy_type != TOX_PROXY_TYPE_NONE) { emit badProxy(); return; } else { qCritical() << "Tox core failed to start"; emit failedToStart(); return; } } toxav = toxav_new(tox, TOXAV_MAX_CALLS); if (toxav == nullptr) { qCritical() << "Toxav core failed to start"; emit failedToStart(); return; } }