tox.*, DHT.*:

- return to the caller if the string could be resolved into an IP

other/DHT_bootstrap.c, testing/*_test.c, testing/nTox.c:
- parse cmdline for --ipv4/--ipv6 switch to allow user a choice

util.h:
- proper old-style C-comment
This commit is contained in:
Coren[m] 2013-09-10 22:59:33 +02:00
parent e89dda5cea
commit 64ca4b5db2
11 changed files with 270 additions and 68 deletions

View File

@ -81,11 +81,44 @@ void manage_keys(DHT *dht)
int main(int argc, char *argv[])
{
/* let use decide by cmdline: TODO */
uint8_t ipv6enabled = TOX_ENABLE_IPV6_DEFAULT;
if (argc == 2 && !strncasecmp(argv[1], "-h", 3)) {
printf("Usage (connected) : %s [--ipv4|--ipv6] IP PORT KEY\n", argv[0]);
printf("Usage (unconnected): %s [--ipv4|--ipv6]\n", argv[0]);
exit(0);
}
/* let user override default by cmdline */
uint8_t ipv6enabled = TOX_ENABLE_IPV6_DEFAULT; /* x */
int argvoffset = 0, argi;
for(argi = 1; argi < argc; argi++)
if (!strncasecmp(argv[argi], "--ipv", 5)) {
if (argv[argi][5] && !argv[argi][6]) {
char c = argv[argi][5];
if (c == '4')
ipv6enabled = 0;
else if (c == '6')
ipv6enabled = 1;
else {
printf("Invalid argument: %s. Try --ipv4 or --ipv6!\n", argv[argi]);
exit(1);
}
}
else {
printf("Invalid argument: %s. Try --ipv4 or --ipv6!\n", argv[argi]);
exit(1);
}
if (argvoffset != argi - 1) {
printf("Argument must come first: %s.\n", argv[argi]);
exit(1);
}
argvoffset++;
}
/* Initialize networking -
Bind to ip 0.0.0.0:PORT */
Bind to ip 0.0.0.0 / [::] : PORT */
IP ip;
ip_init(&ip, ipv6enabled);
@ -112,11 +145,17 @@ int main(int argc, char *argv[])
perror("Initialization.");
if (argc > 3) {
if (argc > argvoffset + 3) {
printf("Trying to bootstrap into the network...\n");
uint8_t *bootstrap_key = hex_string_to_bin(argv[3]);
DHT_bootstrap_ex(dht, argv[1], ipv6enabled, htons(atoi(argv[2])), bootstrap_key);
uint16_t port = htons(atoi(argv[argvoffset + 2]));
uint8_t *bootstrap_key = hex_string_to_bin(argv[argvoffset + 3]);
int res = DHT_bootstrap_ex(dht, argv[argvoffset + 1], ipv6enabled, port, bootstrap_key);
free(bootstrap_key);
if (!res) {
printf("Failed to convert \"%s\" into an IP address. Exiting...\n", argv[argvoffset + 1]);
exit(1);
}
}
int is_waiting_for_dht_connection = 1;

View File

@ -133,8 +133,40 @@ void printpacket(uint8_t *data, uint32_t length, IP_Port ip_port)
int main(int argc, char *argv[])
{
/* let use decide by cmdline: TODO */
uint8_t ipv6enabled = TOX_ENABLE_IPV6_DEFAULT;
if (argc < 4) {
printf("Usage: %s [--ipv4|--ipv6] ip port public_key\n", argv[0]);
exit(0);
}
/* let user override default by cmdline */
uint8_t ipv6enabled = TOX_ENABLE_IPV6_DEFAULT; /* x */
int argvoffset = 0, argi;
for(argi = 1; argi < argc; argi++)
if (!strncasecmp(argv[argi], "--ipv", 5)) {
if (argv[argi][5] && !argv[argi][6]) {
char c = argv[argi][5];
if (c == '4')
ipv6enabled = 0;
else if (c == '6')
ipv6enabled = 1;
else {
printf("Invalid argument: %s. Try --ipv4 or --ipv6!\n", argv[argi]);
exit(1);
}
}
else {
printf("Invalid argument: %s. Try --ipv4 or --ipv6!\n", argv[argi]);
exit(1);
}
if (argvoffset != argi - 1) {
printf("Argument must come first: %s.\n", argv[argi]);
exit(1);
}
argvoffset++;
}
//memcpy(self_client_id, "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq", 32);
/* initialize networking */
@ -144,11 +176,6 @@ int main(int argc, char *argv[])
DHT *dht = new_DHT(new_net_crypto(new_networking(ip, PORT)));
if (argc < 4) {
printf("usage %s ip port public_key\n", argv[0]);
exit(0);
}
new_keys(dht->c);
printf("OUR ID: ");
uint32_t i;
@ -168,9 +195,16 @@ int main(int argc, char *argv[])
DHT_addfriend(dht, hex_string_to_bin(temp_id));
perror("Initialization");
DHT_bootstrap_ex(dht, argv[1], ipv6enabled, htons(atoi(argv[2])), hex_string_to_bin(argv[3]));
uint16_t port = htons(atoi(argv[argvoffset + 2]));
unsigned char *binary_string = hex_string_to_bin(argv[argvoffset + 3]);
int res = DHT_bootstrap_ex(dht, 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]);
return 1;
}
/*
IP_Port ip_port;

View File

@ -151,21 +151,50 @@ void printconnection(int connection_id)
int main(int argc, char *argv[])
{
/* let use decide by cmdline: TODO */
uint8_t ipv6enabled = TOX_ENABLE_IPV6_DEFAULT;
/* let user override default by cmdline */
uint8_t ipv6enabled = TOX_ENABLE_IPV6_DEFAULT; /* x */
if (argc < 4) {
printf("usage: %s ip port filename\n", argv[0]);
int argvoffset = 0, argi;
for(argi = 1; argi < argc; argi++)
if (!strncasecmp(argv[argi], "--ipv", 5)) {
if (argv[argi][5] && !argv[argi][6]) {
char c = argv[argi][5];
if (c == '4')
ipv6enabled = 0;
else if (c == '6')
ipv6enabled = 1;
else {
printf("Invalid argument: %s. Try --ipv4 or --ipv6!\n", argv[argi]);
exit(1);
}
}
else {
printf("Invalid argument: %s. Try --ipv4 or --ipv6!\n", argv[argi]);
exit(1);
}
if (argvoffset != argi - 1) {
printf("Argument must come first: %s.\n", argv[argi]);
exit(1);
}
argvoffset++;
}
if (argc < argvoffset + 4) {
printf("Usage: %s [--ipv4|--ipv6] ip port filename\n", argv[0]);
exit(0);
}
uint8_t buffer[512];
int read;
FILE *file = fopen(argv[3], "rb");
FILE *file = fopen(argv[argvoffset + 3], "rb");
if (file == NULL)
if (file == NULL) {
printf("Failed to open file \"%s\".\n", argv[argvoffset + 3]);
return 1;
}
/* initialize networking */
@ -178,8 +207,11 @@ int main(int argc, char *argv[])
IP_Port serverip;
ip_init(&serverip.ip, ipv6enabled);
addr_resolve(argv[1], &serverip.ip);
serverip.port = htons(atoi(argv[2]));
if (!addr_resolve(argv[argvoffset + 1], &serverip.ip)) {
printf("Failed to convert \"%s\" into an IP address.\n", argv[argvoffset + 1]);
return 1;
}
serverip.port = htons(atoi(argv[argvoffset + 2]));
printip(serverip);
int connection = new_connection(ludp, serverip);

View File

@ -147,21 +147,50 @@ void printconnection(int connection_id)
int main(int argc, char *argv[])
{
/* let use decide by cmdline: TODO */
uint8_t ipv6enabled = TOX_ENABLE_IPV6_DEFAULT;
/* let user override default by cmdline */
uint8_t ipv6enabled = TOX_ENABLE_IPV6_DEFAULT; /* x */
if (argc < 2) {
printf("usage: %s filename\n", argv[0]);
int argvoffset = 0, argi;
for(argi = 1; argi < argc; argi++)
if (!strncasecmp(argv[argi], "--ipv", 5)) {
if (argv[argi][5] && !argv[argi][6]) {
char c = argv[argi][5];
if (c == '4')
ipv6enabled = 0;
else if (c == '6')
ipv6enabled = 1;
else {
printf("Invalid argument: %s. Try --ipv4 or --ipv6!\n", argv[argi]);
exit(1);
}
}
else {
printf("Invalid argument: %s. Try --ipv4 or --ipv6!\n", argv[argi]);
exit(1);
}
if (argvoffset != argi - 1) {
printf("Argument must come first: %s.\n", argv[argi]);
exit(1);
}
argvoffset++;
}
if (argc < argvoffset + 2) {
printf("Usage: %s [--ipv4|--ipv6] filename\n", argv[0]);
exit(0);
}
uint8_t buffer[512];
int read;
FILE *file = fopen(argv[1], "wb");
FILE *file = fopen(argv[argvoffset + 1], "wb");
if (file == NULL)
if (file == NULL) {
printf("Failed to open file \"%s\".\n", argv[argvoffset + 1]);
return 1;
}
//initialize networking

View File

@ -95,13 +95,44 @@ void print_message(Messenger *m, int friendnumber, uint8_t *string, uint16_t len
int main(int argc, char *argv[])
{
if (argc < 4 && argc != 2) {
printf("usage %s ip port public_key (of the DHT bootstrap node)\n or\n %s Save.bak\n", argv[0], argv[0]);
/* let user override default by cmdline */
uint8_t ipv6enabled = TOX_ENABLE_IPV6_DEFAULT; /* x */
int argvoffset = 0, argi;
for(argi = 1; argi < argc; argi++)
if (!strncasecmp(argv[argi], "--ipv", 5)) {
if (argv[argi][5] && !argv[argi][6]) {
char c = argv[argi][5];
if (c == '4')
ipv6enabled = 0;
else if (c == '6')
ipv6enabled = 1;
else {
printf("Invalid argument: %s. Try --ipv4 or --ipv6!\n", argv[argi]);
exit(1);
}
}
else {
printf("Invalid argument: %s. Try --ipv4 or --ipv6!\n", argv[argi]);
exit(1);
}
if (argvoffset != argi - 1) {
printf("Argument must come first: %s.\n", argv[argi]);
exit(1);
}
argvoffset++;
}
/* with optional --ipvx, now it can be 1-4 arguments... */
if ((argc != argvoffset + 2) && (argc != argvoffset + 4)) {
printf("Usage: %s [--ipv4|--ipv6] ip port public_key (of the DHT bootstrap node)\n", argv[0]);
printf("or\n");
printf(" %s [--ipv4|--ipv6] Save.bak (to read Save.bak as state file)\n", argv[0], argv[0]);
exit(0);
}
/* IPv6: maybe allow from cmdline --ipv6? sticking to IPv4 for now */
uint8_t ipv6enabled = TOX_ENABLE_IPV6_DEFAULT;
m = initMessenger(ipv6enabled);
if ( !m ) {
@ -109,13 +140,19 @@ int main(int argc, char *argv[])
exit(0);
}
if (argc > 3) {
uint16_t port = htons(atoi(argv[2]));
DHT_bootstrap_ex(m->dht, argv[1], ipv6enabled, port, hex_string_to_bin(argv[3]));
if (argc == argvoffset + 4) {
uint16_t port = htons(atoi(argv[argvoffset + 2]));
uint8_t *bootstrap_key = hex_string_to_bin(argv[argvoffset + 3]);
int res = DHT_bootstrap_ex(m->dht, argv[argvoffset + 1], ipv6enabled, port, bootstrap_key);
free(bootstrap_key);
if (!res) {
printf("Failed to convert \"%s\" into an IP address. Exiting...\n", argv[argvoffset + 1]);
exit(1);
}
} else {
FILE *file = fopen(argv[1], "rb");
FILE *file = fopen(argv[argvoffset + 1], "rb");
if ( file == NULL ) {
printf("Failed to open \"%s\" - does it exist?\n", argv[argvoffset + 1]);
return 1;
}

View File

@ -537,8 +537,40 @@ void print_help(void)
int main(int argc, char *argv[])
{
/* let use decide by cmdline: TODO */
uint8_t ipv6enabled = TOX_ENABLE_IPV6_DEFAULT;
if (argc < 4) {
printf("Usage: %s [--ipv4|--ipv6] IP PORT KEY [-f keyfile]\n", argv[0]);
exit(0);
}
/* let user override default by cmdline */
uint8_t ipv6enabled = TOX_ENABLE_IPV6_DEFAULT; /* x */
int argvoffset = 0, argi;
for(argi = 1; argi < argc; argi++)
if (!strncasecmp(argv[argi], "--ipv", 5)) {
if (argv[argi][5] && !argv[argi][6]) {
char c = argv[argi][5];
if (c == '4')
ipv6enabled = 0;
else if (c == '6')
ipv6enabled = 1;
else {
printf("Invalid argument: %s. Try --ipv4 or --ipv6!\n", argv[argi]);
exit(1);
}
}
else {
printf("Invalid argument: %s. Try --ipv4 or --ipv6!\n", argv[argi]);
exit(1);
}
if (argvoffset != argi - 1) {
printf("Argument must come first: %s.\n", argv[argi]);
exit(1);
}
argvoffset++;
}
int on = 0;
int c = 0;
@ -547,27 +579,16 @@ int main(int argc, char *argv[])
char idstring[200] = {0};
Tox *m;
if (argc < 4) {
printf("[!] Usage: %s [IP] [port] [public_key] <keyfile>\n", argv[0]);
if ((argc == 2) && !strcmp(argv[1], "-h")) {
print_help();
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);
}
}
}
}
/* [-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_ex(ipv6enabled);
@ -593,11 +614,17 @@ int main(int argc, char *argv[])
new_lines(idstring);
strcpy(line, "");
uint16_t port = htons(atoi(argv[2]));
unsigned char *binary_string = hex_string_to_bin(argv[3]);
tox_bootstrap_ex(m, argv[1], ipv6enabled, port, binary_string);
uint16_t port = htons(atoi(argv[argvoffset + 2]));
unsigned char *binary_string = hex_string_to_bin(argv[argvoffset + 3]);
int res = tox_bootstrap_ex(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);
while (1) {
if (on == 0 && tox_isconnected(m)) {

View File

@ -978,14 +978,17 @@ void DHT_bootstrap(DHT *dht, IP_Port ip_port, uint8_t *public_key)
getnodes(dht, ip_port, public_key, dht->c->self_public_key);
send_ping_request(dht->ping, dht->c, ip_port, public_key);
}
void DHT_bootstrap_ex(DHT *dht, const char *address, uint8_t ipv6enabled, uint16_t port, uint8_t *public_key)
int DHT_bootstrap_ex(DHT *dht, const char *address, uint8_t ipv6enabled, uint16_t port, uint8_t *public_key)
{
IP_Port ip_port;
ip_init(&ip_port.ip, ipv6enabled);
if (addr_resolve_or_parse_ip(address, &ip_port.ip)) {
ip_port.port = port;
DHT_bootstrap(dht, ip_port, public_key);
return 1;
}
else
return 0;
}
/* Send the given packet to node with client_id

View File

@ -157,9 +157,10 @@ void do_DHT(DHT *dht);
/* Use this function to bootstrap the client.
* Sends a get nodes request to the given node with ip port and public_key.
* DHT_bootstrap_ex() returns 1 if the address could be converted, 0 otherwise
*/
void DHT_bootstrap(DHT *dht, IP_Port ip_port, uint8_t *public_key);
void DHT_bootstrap_ex(DHT *dht, const char *address, uint8_t ipv6enabled, uint16_t port, uint8_t *public_key);
int DHT_bootstrap_ex(DHT *dht, const char *address, uint8_t ipv6enabled, uint16_t port, uint8_t *public_key);
/* Add nodes to the toping list.
* All nodes in this list are pinged every TIME_TOPING seconds

View File

@ -374,11 +374,11 @@ void tox_bootstrap(void *tox, IP_Port ip_port, uint8_t *public_key)
Messenger *m = tox;
DHT_bootstrap(m->dht, ip_port, public_key);
}
void tox_bootstrap_ex(void *tox, const char *address, uint8_t ipv6enabled,
int tox_bootstrap_ex(void *tox, const char *address, uint8_t ipv6enabled,
uint16_t port, uint8_t *public_key)
{
Messenger *m = tox;
DHT_bootstrap_ex(m->dht, address, ipv6enabled, port, public_key);
return DHT_bootstrap_ex(m->dht, address, ipv6enabled, port, public_key);
};
/* return 0 if we are not connected to the DHT.

View File

@ -340,10 +340,10 @@ void tox_callback_connectionstatus(Tox *tox, void (*function)(Tox *tox, int, uin
/* Use this function to bootstrap the client.
* Sends a get nodes request to the given node with ip port and public_key.
* tox_bootstrap_ex converts the address into an IP_Port structure internally
* tox_bootstrap_ex() returns 1 if the address could be converted, 0 otherwise
*/
void tox_bootstrap(Tox *tox, tox_IP_Port ip_port, uint8_t *public_key);
void tox_bootstrap_ex(Tox *tox, const char *address, uint8_t ipv6enabled,
int tox_bootstrap_ex(Tox *tox, const char *address, uint8_t ipv6enabled,
uint16_t port, uint8_t *public_key);
/* return 0 if we are not connected to the DHT.

View File

@ -17,7 +17,7 @@ bool id_eq(uint8_t *dest, uint8_t *src);
void id_cpy(uint8_t *dest, uint8_t *src);
#undef LOGGING
// #define LOGGING
/* #define LOGGING */
#ifdef LOGGING
extern char logbuffer[512];
void loginit(uint16_t port);