Пример #1
0
static void do_tox(Tox *m, ToxWindow *prompt)
{
    static int conn_try = 0;
    static int conn_err = 0;
    static bool dht_on = false;

    if (!dht_on && !tox_isconnected(m) && !(conn_try++ % 100)) {
        if (!conn_err) {
            conn_err = init_connection(m);
            wprintw(prompt->window, "\nEstablishing connection...\n");

            if (conn_err)
                wprintw(prompt->window, "\nAuto-connect failed with error code %d\n", conn_err);
        }
    } else if (!dht_on && tox_isconnected(m)) {
        dht_on = true;
        prompt_update_connectionstatus(prompt, dht_on);
        wprintw(prompt->window, "\nDHT connected.\n");
    } else if (dht_on && !tox_isconnected(m)) {
        dht_on = false;
        prompt_update_connectionstatus(prompt, dht_on);
        wprintw(prompt->window, "\nDHT disconnected. Attempting to reconnect.\n");
    }

    tox_do(m);
}
Пример #2
0
void Core::checkConnection()
{
    static bool isConnected = false;

    if (tox_isconnected(tox) && !isConnected) {
        emit connected();
        isConnected = true;
    } else if (!tox_isconnected(tox) && isConnected) {
        emit disconnected();
        isConnected = false;
    }
}
Пример #3
0
void Core::checkConnection()
{
    static bool isConnected = false;

    if (!isConnected && tox_isconnected(handle)) {
        emit connected();
        isConnected = true;
    } else if (isConnected && !tox_isconnected(handle)) {
        emit disconnected();
        isConnected = false;
    }
}
Пример #4
0
void Core::checkConnection()
{
    static bool isConnected = false;

    if (tox_isconnected(tox) && !isConnected) {
        qDebug() << "Core: Connected to DHT";
        emit connected();
        isConnected = true;
    } else if (!tox_isconnected(tox) && isConnected) {
        qDebug() << "Core: Disconnected to DHT";
        emit disconnected();
        isConnected = false;
    }
}
Пример #5
0
int av_wait_dht(av_session_t *_phone, int _wait_seconds, const char *_ip, char *_key, unsigned short _port)
{
    if ( !_wait_seconds )
        return -1;

    int _waited = 0;

    while ( !tox_isconnected(_phone->_messenger) ) {

        if ( -1 == av_connect_to_dht(_phone, _key, _ip, _port) ) {
            INFO("Could not connect to: %s", _ip);
            av_terminate_session(_phone);
            return -1;
        }

        if ( _waited >= _wait_seconds ) return 0;

        printf(".");
        fflush(stdout);

        _waited ++;
        usleep(1000000);
    }

    int _r = _wait_seconds - _waited;
    return _r ? _r : 1;
}
Пример #6
0
static void do_connection(Tox *m, ToxWindow *prompt)
{
    uint8_t msg[MAX_STR_SIZE] = {0};

    static int conn_try = 0;
    static int conn_err = 0;
    static bool dht_on = false;

    bool is_connected = tox_isconnected(m);

    if (!dht_on && !is_connected && !(conn_try++ % 100)) {
        if (!conn_err) {
            if ((conn_err = init_connection(m))) {
                snprintf(msg, sizeof(msg), "\nAuto-connect failed with error code %d", conn_err);
            }
        }
    } else if (!dht_on && is_connected) {
        dht_on = true;
        prompt_update_connectionstatus(prompt, dht_on);
        snprintf(msg, sizeof(msg), "DHT connected.");
    } else if (dht_on && !is_connected) {
        dht_on = false;
        prompt_update_connectionstatus(prompt, dht_on);
        snprintf(msg, sizeof(msg), "\nDHT disconnected. Attempting to reconnect.");
    }

    if (msg[0])
        line_info_add(prompt, NULL, NULL, NULL, msg, SYS_MSG, 0, 0);
}
Пример #7
0
static void do_connection(Tox *m, ToxWindow *prompt)
{
    char msg[MAX_STR_SIZE] = {0};

    static int conn_err = 0;
    static bool was_connected = false;
    static uint64_t last_conn_try = 0;
    uint64_t curtime = get_unix_time();
    bool is_connected = tox_isconnected(m);

    if (was_connected && is_connected)
        return;

    if (!was_connected && is_connected) {
        was_connected = true;
        prompt_update_connectionstatus(prompt, was_connected);
        snprintf(msg, sizeof(msg), "DHT connected.");
    } else if (was_connected && !is_connected) {
        was_connected = false;
        prompt_update_connectionstatus(prompt, was_connected);
        snprintf(msg, sizeof(msg), "DHT disconnected. Attempting to reconnect.");
    } else if (!was_connected && !is_connected && timed_out(last_conn_try, curtime, TRY_CONNECT)) {
        /* if autoconnect has already failed there's no point in trying again */
        if (conn_err == 0) {
            last_conn_try = curtime;

            if ((conn_err = init_connection(m)) != 0)
                snprintf(msg, sizeof(msg), "Auto-connect failed with error code %d", conn_err);
        }
    }

    if (msg[0])
        line_info_add(prompt, NULL, NULL, NULL, SYS_MSG, 0, 0, msg);
}
Пример #8
0
void Core::onBootstrapTimer()
{
    if (!tox)
        return;
    if(!tox_isconnected(tox))
        bootstrapDht();
}
Пример #9
0
void *tox_works(void *a){
    // do tox loop
    time_t timestamp0 = time(NULL);
    int on = 0;
    while (1) {
        if (on == 0) {
            if (tox_isconnected(my_tox)) {
                on = 1;
            } else {
                time_t timestamp1 = time(NULL);
                if (timestamp0 + 10 < timestamp1) {
                    timestamp0 = timestamp1;
                    init_tox_connection(my_tox);
                }
                
            }
        }
        tox_do(my_tox);
        if(msg_task_queue->size != 0){
            send_data_remote();
        }else{
#ifdef _WIN32
			Sleep(4);
#else
			usleep(4000);
#endif
        }
    }
}
Пример #10
0
int
twc_do_timer_cb(void *data,
                int remaining_calls)
{
    struct t_twc_profile *profile = data;

    tox_do(profile->tox);
    struct t_hook *hook = weechat_hook_timer(tox_do_interval(profile->tox), 0, 1,
                                             twc_do_timer_cb, profile);
    profile->tox_do_timer = hook;

    // check connection status
    int connected = tox_isconnected(profile->tox);
    twc_profile_set_online_status(profile, connected);

    return WEECHAT_RC_OK;
}
Пример #11
0
bool Core::checkConnection()
{
    static bool isConnected = false;
    //static int count = 0;
    bool toxConnected = tox_isconnected(tox);

    if (toxConnected && !isConnected) {
        qDebug() << "Core: Connected to DHT";
        emit connected();
        isConnected = true;
        //if (count) qDebug() << "Core: disconnect count:" << count;
        //count = 0;
    } else if (!toxConnected && isConnected) {
        qDebug() << "Core: Disconnected to DHT";
        emit disconnected();
        isConnected = false;
        //count++;
    } //else if (!toxConnected) count++;
    return isConnected;
}
Пример #12
0
Файл: tox.c Проект: Boerde/uTox
void tox_thread(void *UNUSED(args))
{
    Tox *tox;
    ToxAv *av;
    uint8_t id[TOX_FRIEND_ADDRESS_SIZE];

TOP:;
    debug("new tox object ipv6: %u no_udp: %u proxy: %u %s %u\n", options.ipv6enabled, options.udp_disabled, options.proxy_enabled, options.proxy_address, options.proxy_port);
    if((tox = tox_new(&options)) == NULL) {
        debug("trying without proxy\n");
        if(!options.proxy_enabled || (options.proxy_enabled = 0, (tox = tox_new(&options)) == NULL)) {
            debug("trying without ipv6\n");
            if(!options.ipv6enabled || (options.ipv6enabled = 0, (tox = tox_new(&options)) == NULL)) {
                debug("tox_new() failed\n");
                exit(1);
            }
            dropdown_ipv6.selected = dropdown_ipv6.over = 1;
        }
        dropdown_proxy.selected = dropdown_proxy.over = 0;
    }

    if(!load_save(tox)) {
        debug("No save file, using defaults\n");
        load_defaults(tox);
    }

    edit_setstr(&edit_name, self.name, self.name_length);
    edit_setstr(&edit_status, self.statusmsg, self.statusmsg_length);

    tox_get_address(tox, id);
    id_to_string(self.id, id);

    debug("Tox ID: %.*s\n", (int)sizeof(self.id), self.id);

    set_callbacks(tox);

    do_bootstrap(tox);

    av = toxav_new(tox, MAX_CALLS);

    set_av_callbacks(av);


    global_av = av;
    tox_thread_init = 1;

    thread(audio_thread, av);
    thread(video_thread, av);
    thread(toxav_thread, av);

    _Bool connected = 0, reconfig;
    uint64_t last_save = get_time(), time;
    while(1) {
        tox_do(tox);

        if(tox_isconnected(tox) != connected) {
            connected = !connected;
            postmessage(DHT_CONNECTED, connected, 0, NULL);

            debug("Connected to DHT: %u\n", connected);
        }

        time = get_time();

        if(time - last_save >= (uint64_t)10 * 1000 * 1000 * 1000) {
            last_save = time;

            if(!connected) {
                do_bootstrap(tox);
            }

            write_save(tox);
        }

        if(tox_thread_msg) {
            TOX_MSG *msg = &tox_msg;
            if(!msg->msg) {
                reconfig = msg->param1;
                tox_thread_msg = 0;
                break;
            }
            tox_thread_message(tox, av, time, msg->msg, msg->param1, msg->param2, msg->data);
            tox_thread_msg = 0;
        }

        utox_thread_work_for_transfers(tox, time);
        utox_thread_work_for_typing_notifications(tox, time);

        uint32_t interval = tox_do_interval(tox);
        yieldcpu((interval > 20) ? 20 : interval);
    }

    write_save(tox);

    while(audio_thread_init || video_thread_init || toxav_thread_init) {
        yieldcpu(1);
    }

    debug("av_thread exit, tox thread ending\n");

    toxav_kill(av);
    tox_kill(tox);

    if(reconfig) {
        goto TOP;
    }

    tox_thread_init = 0;
}
Пример #13
0
int main(int argc, char *argv[])
{
    int on = 0;
    int c = 0;
    int i = 0;
    char *filename = "data";
    char idstring[200] = {0};
    Tox *m;

    if (argc < 4) {
        printf("[!] Usage: %s [IP] [port] [public_key] <keyfile>\n", argv[0]);
        exit(0);
    }

    for (i = 0; i < argc; i++) {
        if (argv[i] == NULL) {
            break;
        } else if (argv[i][0] == '-') {
            if (argv[i][1] == 'h') {
                print_help();
                exit(0);
            } else if (argv[i][1] == 'f') {
                if (argv[i + 1] != NULL)
                    filename = argv[i + 1];
                else {
                    fputs("[!] you passed '-f' without giving an argument!\n", stderr);
                }
            }
        }
    }

    m = tox_new();

    if ( !m ) {
        fputs("Failed to allocate Messenger datastructure", stderr);
        exit(0);
    }

    load_key(m, filename);

    tox_callback_friendrequest(m, print_request, NULL);
    tox_callback_friendmessage(m, print_message, NULL);
    tox_callback_namechange(m, print_nickchange, NULL);
    tox_callback_statusmessage(m, print_statuschange, NULL);

    initscr();
    noecho();
    raw();
    getmaxyx(stdscr, y, x);

    new_lines("/h for list of commands");
    get_id(m, idstring);
    new_lines(idstring);
    strcpy(line, "");

    tox_IP_Port bootstrap_ip_port;
    bootstrap_ip_port.port = htons(atoi(argv[2]));
    int resolved_address = resolve_addr(argv[1]);

    if (resolved_address != 0)
        bootstrap_ip_port.ip.i = resolved_address;
    else
        exit(1);

    unsigned char *binary_string = hex_string_to_bin(argv[3]);
    tox_bootstrap(m, bootstrap_ip_port, binary_string);
    free(binary_string);
    nodelay(stdscr, TRUE);

    while (true) {
        if (on == 0 && tox_isconnected(m)) {
            new_lines("[i] connected to DHT\n[i] define username with /n");
            on = 1;
        }

        tox_do(m);
        c_sleep(1);
        do_refresh();

        c = getch();

        if (c == ERR || c == 27)
            continue;

        getmaxyx(stdscr, y, x);

        if (c == '\n') {
            line_eval(m, line);
            strcpy(line, "");
        } else if (c == 8 || c == 127) {
            line[strlen(line) - 1] = '\0';
        } else if (isalnum(c) || ispunct(c) || c == ' ') {
            strcpy(line, appender(line, (char) c));
        }
    }

    tox_kill(m);
    endwin();
    return 0;
}
Пример #14
0
int main(int argc, char *argv[])
{
    uint8_t ipv6enabled = TOX_ENABLE_IPV6_DEFAULT; /* x */
    int argvoffset = cmdline_parsefor_ipv46(argc, argv, &ipv6enabled);

    if (argvoffset < 0)
        exit(1);

    /* with optional --ipvx, now it can be 1-4 arguments... */
    if ((argc != argvoffset + 3) && (argc != argvoffset + 5)) {
        printf("Usage: %s [--ipv4|--ipv6] ip port public_key (of the DHT bootstrap node) folder (to sync)\n", argv[0]);
        exit(0);
    }

    Tox *tox = tox_new(ipv6enabled);
    tox_callback_file_data(tox, write_file, NULL);
    tox_callback_file_control(tox, file_print_control, NULL);
    tox_callback_file_send_request(tox, file_request_accept, NULL);
    tox_callback_connection_status(tox, print_online, NULL);

    uint16_t port = htons(atoi(argv[argvoffset + 2]));
    unsigned char *binary_string = hex_string_to_bin(argv[argvoffset + 3]);
    int res = tox_bootstrap_from_address(tox, argv[argvoffset + 1], ipv6enabled, port, binary_string);
    free(binary_string);

    if (!res) {
        printf("Failed to convert \"%s\" into an IP address. Exiting...\n", argv[argvoffset + 1]);
        exit(1);
    }

    uint8_t address[TOX_FRIEND_ADDRESS_SIZE];
    tox_get_address(tox, address);
    uint32_t i;

    for (i = 0; i < TOX_FRIEND_ADDRESS_SIZE; i++) {
        printf("%02X", address[i]);
    }

    char temp_id[128];
    printf("\nEnter the address of the other id you want to sync with (38 bytes HEX format):\n");

    if (scanf("%s", temp_id) != 1) {
        return 1;
    }

    uint8_t *bin_id = hex_string_to_bin(temp_id);
    int num = tox_add_friend(tox, bin_id, (uint8_t *)"Install Gentoo", sizeof("Install Gentoo"));
    free(bin_id);

    if (num < 0) {
        printf("\nSomething went wrong when adding friend.\n");
        return 1;
    }

    memcpy(path, argv[argvoffset + 4], strlen(argv[argvoffset + 4]));
    DIR           *d;
    struct dirent *dir;
    uint8_t notconnected = 1;

    while (1) {
        if (tox_isconnected(tox) && notconnected) {
            printf("\nDHT connected.\n");
            notconnected = 0;
        }

        if (not_sending() && tox_get_friend_connection_status(tox, num)) {
            d = opendir(path);

            if (d) {
                while ((dir = readdir(d)) != NULL) {
                    if (dir->d_type == DT_REG) {
                        char fullpath[1024];

                        if (path[strlen(path) - 1] == '/')
                            sprintf(fullpath, "%s%s", path, dir->d_name);
                        else
                            sprintf(fullpath, "%s/%s", path, dir->d_name);

                        add_filesender(tox, num, fullpath);
                    }
                }

                closedir(d);

            } else {
                printf("\nFailed to open directory.\n");
                return 1;
            }
        }

        send_filesenders(tox);
        tox_do(tox);
        c_sleep(1);
    }

    return 0;
}
Пример #15
0
int main(int argc, char *argv[])
{
    // 添加系统事件监听
    signal(SIGINT, intHandler);
#ifndef _WIN32
    signal(SIGPIPE, SIG_IGN);
#endif
    my_tox = init_tox();
    init_tox_connection(my_tox);
    load_data(my_tox);
    // 处理参数
    if(argc >3){
		local_port = atoi(argv[1]);
        target_id = argv[2];
        target_ip = argv[3];
        target_port = atoi(argv[4]);
        
        uint8_t my_address_bin[TOX_FRIEND_ADDRESS_SIZE+1];
        uint8_t my_address_str[TOX_FRIEND_ADDRESS_SIZE*2+1];
        tox_get_address(my_tox,my_address_bin);
        hex_bin_to_string(my_address_bin,TOX_FRIEND_ADDRESS_SIZE,my_address_str);
        printf("MYID:%s\n",my_address_str);
    }else{
		local_port = 9990 ;
		target_id = "3E567CBCE8DA8A18ED3C30127ADB61D78AFA2D01B5CD12425C110E84F23AC365144CE2807378";
		target_ip = "127.0.0.1";
		target_port = 22;
    }
    // 虛擬參數
    
    // 初始化消息隊列
    msg_task_queue = createQueue(MAX_MSG_CACHE); // 远程操作消息队列
    
    // 開始tox線程
    pthread_t tox_thread;
    int iret1 = pthread_create( &tox_thread, NULL, tox_works,NULL);
    if(iret1){
        exit(EXIT_FAILURE);
    }
    
    // 初始化本地连接
    local_socksfd = init_local_sock_serv(local_port);
    
    // 等待tox成功連接
    while(!tox_isconnected(my_tox)){
#ifdef _WIN32
		Sleep(20);
#else
		usleep(20000);
#endif
        
    }
    printf("TOXCORE:ONLINE\n");
    printf("SERVER:LISTEN ON %d\n",local_port);
    // 進入請求者模式
    int res = init_connect(my_tox,target_id,&msg_listener_list);
    if(res == 402){
        printf("CONNECT:OK\n");
    }
    else{
        printf("CONNECT:ERROR\n");
    }
    

    // client mode
    // create local tcp server
    msocks_list = create_local_socks_list();
    
    while(1){
        struct sockaddr_in cli_addr;
        uint32_t clilen = sizeof(cli_addr);
		int32_t newsockfd = accept(local_socksfd,
			(struct sockaddr *) &cli_addr,
			&clilen);
        
		if (newsockfd < 0){
			printf("socket error\n");
		}
		else{
			printf("accepted:%d\n", newsockfd);
			// 发送创建远程端口指令
            uint32_t sockfd = newsockfd;
            uint8_t *target_addr_bin = (uint8_t *)malloc(sizeof(uint8_t)*TOX_FRIEND_ADDRESS_SIZE);
            hex_string_to_bin(target_addr_bin,target_id);
            add_local_socks(msocks_list,sockfd,target_addr_bin,target_ip,target_port);
            // create remote socket
            uint8_t uuid[UUID_LENGTH+1];
            get_local_socks_uuid(msocks_list,sockfd,uuid);
            create_remote_socket(uuid,target_addr_bin,target_ip,target_port);
            free(target_addr_bin);
		}
    }
    
    printf("EXITED\n");
    return 0; 
}
Пример #16
0
int main(int argc, char *argv[])
{
    /* minimalistic locale support (i.e. when printing dates) */
    setlocale(LC_ALL, "");

    if (argc < 4) {
        if ((argc == 2) && !strcmp(argv[1], "-h")) {
            print_help(argv[0]);
            exit(0);
        }

        printf("Usage: %s [--ipv4|--ipv6] IP PORT KEY [-f keyfile] (or %s -h for help)\n", argv[0], argv[0]);
        exit(0);
    }

    /* let user override default by cmdline */
    uint8_t ipv6enabled = TOX_ENABLE_IPV6_DEFAULT; /* x */
    int argvoffset = cmdline_parsefor_ipv46(argc, argv, &ipv6enabled);

    if (argvoffset < 0)
        exit(1);

    int on = 0;
    char *filename = "data";
    char idstring[200] = {0};
    Tox *m;

    /* [-f keyfile] MUST be last two arguments, no point in walking over the list
     * especially not a good idea to accept it anywhere in the middle */
    if (argc > argvoffset + 3)
        if (!strcmp(argv[argc - 2], "-f"))
            filename = argv[argc - 1];

    m = tox_new(ipv6enabled);

    if ( !m ) {
        fputs("Failed to allocate Messenger datastructure", stderr);
        exit(0);
    }

    load_data_or_init(m, filename);

    tox_callback_friend_request(m, print_request, NULL);
    tox_callback_friend_message(m, print_message, NULL);
    tox_callback_name_change(m, print_nickchange, NULL);
    tox_callback_status_message(m, print_statuschange, NULL);
    tox_callback_group_invite(m, print_invite, NULL);
    tox_callback_group_message(m, print_groupmessage, NULL);
    tox_callback_file_data(m, write_file, NULL);
    tox_callback_file_control(m, file_print_control, NULL);
    tox_callback_file_send_request(m, file_request_accept, NULL);
    tox_callback_group_namelist_change(m, print_groupnamelistchange, NULL);

    initscr();
    noecho();
    raw();
    getmaxyx(stdscr, y, x);

    new_lines("/h for list of commands");
    get_id(m, idstring);
    new_lines(idstring);
    strcpy(input_line, "");

    uint16_t port = htons(atoi(argv[argvoffset + 2]));
    unsigned char *binary_string = hex_string_to_bin(argv[argvoffset + 3]);
    int res = tox_bootstrap_from_address(m, argv[argvoffset + 1], ipv6enabled, port, binary_string);
    free(binary_string);

    if (!res) {
        printf("Failed to convert \"%s\" into an IP address. Exiting...\n", argv[argvoffset + 1]);
        endwin();
        exit(1);
    }

    nodelay(stdscr, TRUE);

    new_lines("[i] change username with /n");
    uint8_t name[TOX_MAX_NAME_LENGTH + 1];
    uint16_t namelen = tox_get_self_name(m, name);
    name[namelen] = 0;

    if (namelen > 0) {
        char whoami[128 + TOX_MAX_NAME_LENGTH];
        snprintf(whoami, sizeof(whoami), "[i] your current username is: %s", name);
        new_lines(whoami);
    }

    time_t timestamp0 = time(NULL);

    while (1) {
        if (on == 0) {
            if (tox_isconnected(m)) {
                new_lines("[i] connected to DHT");
                on = 1;
            } else {
                time_t timestamp1 = time(NULL);

                if (timestamp0 + 10 < timestamp1) {
                    timestamp0 = timestamp1;
                    tox_bootstrap_from_address(m, argv[argvoffset + 1], ipv6enabled, port, binary_string);
                }
            }
        }



        send_filesenders(m);
        tox_do(m);
        do_refresh();

        int c = timeout_getch(m);

        if (c == ERR || c == 27)
            continue;

        getmaxyx(stdscr, y, x);

        if ((c == 0x0d) || (c == 0x0a)) {
            line_eval(m, input_line);
            strcpy(input_line, "");
        } else if (c == 8 || c == 127) {
            input_line[strlen(input_line) - 1] = '\0';
        } else if (isalnum(c) || ispunct(c) || c == ' ') {
            strcpy(input_line, appender(input_line, (char) c));
        }
    }

    tox_kill(m);
    endwin();
    return 0;
}