int main ( int argc, char *argv [] ) { if ( argc < 1 || argc < 4 ) return print_help(argv[0]); char *_convertable; const char *_ip = argv[1]; char *_key = argv[3]; unsigned short _port = strtol(argv[2], &_convertable, 10); if ( *_convertable ) { printf("Invalid port: cannot convert string to long: %s", _convertable); return 1; } av_session_t *_phone = av_init_session(); tox_callback_friend_request(_phone->_messenger, av_friend_requ, _phone); tox_callback_status_message(_phone->_messenger, av_friend_active, _phone); INFO("\r================================================================================\n" "[!] Trying dht@%s:%d" , _ip, _port); /* Start tox protocol */ event.rise( tox_poll, &_phone->_messenger ); /* Just clean one line */ printf("\r \r"); fflush(stdout); int _r; int _wait_seconds = 5; for ( _r = 0; _r == 0; _r = av_wait_dht(_phone, _wait_seconds, _ip, _key, _port) ) _wait_seconds --; if ( -1 == _r ) { INFO("Error while connecting to dht: %s:%d", _ip, _port); av_terminate_session(_phone); return 1; } INFO("CONNECTED!\n" "================================================================================\n" "%s\n" "================================================================================" , _phone->_my_public_id ); do_phone (_phone); av_terminate_session(_phone); return 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; }