Both sides previous revisionPrevious revisionNext revision | Previous revision |
developers:client_examples:echo_bot [2020/03/22 20:59] – Update DHT bootstrap nodes as the current ones are offline nurupo | developers:client_examples:echo_bot [2020/03/23 22:08] (current) – Minor code fixes nurupo |
---|
uint16_t port; | uint16_t port; |
const char key_hex[TOX_PUBLIC_KEY_SIZE*2 + 1]; // 1 for null terminator | const char key_hex[TOX_PUBLIC_KEY_SIZE*2 + 1]; // 1 for null terminator |
unsigned char key_bin[TOX_PUBLIC_KEY_SIZE]; | |
} DHT_node; | } DHT_node; |
| |
DHT_node nodes[] = | DHT_node nodes[] = |
{ | { |
{"85.143.221.42", 33445, "DA4E4ED4B697F2E9B000EEFE3A34B554ACD3F45F5C96EAEA2516DD7FF9AF7B43", {0}}, | {"85.143.221.42", 33445, "DA4E4ED4B697F2E9B000EEFE3A34B554ACD3F45F5C96EAEA2516DD7FF9AF7B43"}, |
{"2a04:ac00:1:9f00:5054:ff:fe01:becd", 33445, "DA4E4ED4B697F2E9B000EEFE3A34B554ACD3F45F5C96EAEA2516DD7FF9AF7B43", {0}}, | {"2a04:ac00:1:9f00:5054:ff:fe01:becd", 33445, "DA4E4ED4B697F2E9B000EEFE3A34B554ACD3F45F5C96EAEA2516DD7FF9AF7B43"}, |
{"78.46.73.141", 33445, "02807CF4F8BB8FB390CC3794BDF1E8449E9A8392C5D3F2200019DA9F1E812E46", {0}}, | {"78.46.73.141", 33445, "02807CF4F8BB8FB390CC3794BDF1E8449E9A8392C5D3F2200019DA9F1E812E46"}, |
{"2a01:4f8:120:4091::3", 33445, "02807CF4F8BB8FB390CC3794BDF1E8449E9A8392C5D3F2200019DA9F1E812E46", {0}}, | {"2a01:4f8:120:4091::3", 33445, "02807CF4F8BB8FB390CC3794BDF1E8449E9A8392C5D3F2200019DA9F1E812E46"}, |
{"tox.initramfs.io", 33445, "3F0A45A268367C1BEA652F258C85F4A66DA76BCAA667A49E770BCC4917AB6A25", {0}}, | {"tox.initramfs.io", 33445, "3F0A45A268367C1BEA652F258C85F4A66DA76BCAA667A49E770BCC4917AB6A25"}, |
{"tox2.abilinski.com", 33445, "7A6098B590BDC73F9723FC59F82B3F9085A64D1B213AAF8E610FD351930D052D", {0}}, | {"tox2.abilinski.com", 33445, "7A6098B590BDC73F9723FC59F82B3F9085A64D1B213AAF8E610FD351930D052D"}, |
{"205.185.115.131", 53, "3091C6BEB2A993F1C6300C16549FABA67098FF3D62C6D253828B531470B53D68", {0}}, | {"205.185.115.131", 53, "3091C6BEB2A993F1C6300C16549FABA67098FF3D62C6D253828B531470B53D68"}, |
{"tox.kurnevsky.net", 33445, "82EF82BA33445A1F91A7DB27189ECFC0C013E06E3DA71F588ED692BED625EC23", {0}} | {"tox.kurnevsky.net", 33445, "82EF82BA33445A1F91A7DB27189ECFC0C013E06E3DA71F588ED692BED625EC23"} |
}; | }; |
| |
for (size_t i = 0; i < sizeof(nodes)/sizeof(DHT_node); i ++) { | for (size_t i = 0; i < sizeof(nodes)/sizeof(DHT_node); i ++) { |
sodium_hex2bin(nodes[i].key_bin, sizeof(nodes[i].key_bin), | unsigned char key_bin[TOX_PUBLIC_KEY_SIZE]; |
nodes[i].key_hex, sizeof(nodes[i].key_hex)-1, NULL, NULL, NULL); | sodium_hex2bin(key_bin, sizeof(key_bin), nodes[i].key_hex, sizeof(nodes[i].key_hex)-1, |
tox_bootstrap(tox, nodes[i].ip, nodes[i].port, nodes[i].key_bin, NULL); | NULL, NULL, NULL); |
| tox_bootstrap(tox, nodes[i].ip, nodes[i].port, key_bin, NULL); |
} | } |
</code> | </code> |
uint16_t port; | uint16_t port; |
const char key_hex[TOX_PUBLIC_KEY_SIZE*2 + 1]; | const char key_hex[TOX_PUBLIC_KEY_SIZE*2 + 1]; |
unsigned char key_bin[TOX_PUBLIC_KEY_SIZE]; | |
} DHT_node; | } DHT_node; |
| |
void friend_request_cb(Tox *tox, const uint8_t *public_key, const uint8_t *message, size_t length, | void friend_request_cb(Tox *tox, const uint8_t *public_key, const uint8_t *message, size_t length, |
void *user_data) | void *user_data) |
{ | { |
tox_friend_add_norequest(tox, public_key, NULL); | tox_friend_add_norequest(tox, public_key, NULL); |
| |
void friend_message_cb(Tox *tox, uint32_t friend_number, TOX_MESSAGE_TYPE type, const uint8_t *message, | void friend_message_cb(Tox *tox, uint32_t friend_number, TOX_MESSAGE_TYPE type, const uint8_t *message, |
size_t length, void *user_data) | size_t length, void *user_data) |
{ | { |
tox_friend_send_message(tox, friend_number, type, message, length, NULL); | tox_friend_send_message(tox, friend_number, type, message, length, NULL); |
DHT_node nodes[] = | DHT_node nodes[] = |
{ | { |
{"85.143.221.42", 33445, "DA4E4ED4B697F2E9B000EEFE3A34B554ACD3F45F5C96EAEA2516DD7FF9AF7B43", {0}}, | {"85.143.221.42", 33445, "DA4E4ED4B697F2E9B000EEFE3A34B554ACD3F45F5C96EAEA2516DD7FF9AF7B43"}, |
{"2a04:ac00:1:9f00:5054:ff:fe01:becd", 33445, "DA4E4ED4B697F2E9B000EEFE3A34B554ACD3F45F5C96EAEA2516DD7FF9AF7B43", {0}}, | {"2a04:ac00:1:9f00:5054:ff:fe01:becd", 33445, "DA4E4ED4B697F2E9B000EEFE3A34B554ACD3F45F5C96EAEA2516DD7FF9AF7B43"}, |
{"78.46.73.141", 33445, "02807CF4F8BB8FB390CC3794BDF1E8449E9A8392C5D3F2200019DA9F1E812E46", {0}}, | {"78.46.73.141", 33445, "02807CF4F8BB8FB390CC3794BDF1E8449E9A8392C5D3F2200019DA9F1E812E46"}, |
{"2a01:4f8:120:4091::3", 33445, "02807CF4F8BB8FB390CC3794BDF1E8449E9A8392C5D3F2200019DA9F1E812E46", {0}}, | {"2a01:4f8:120:4091::3", 33445, "02807CF4F8BB8FB390CC3794BDF1E8449E9A8392C5D3F2200019DA9F1E812E46"}, |
{"tox.initramfs.io", 33445, "3F0A45A268367C1BEA652F258C85F4A66DA76BCAA667A49E770BCC4917AB6A25", {0}}, | {"tox.initramfs.io", 33445, "3F0A45A268367C1BEA652F258C85F4A66DA76BCAA667A49E770BCC4917AB6A25"}, |
{"tox2.abilinski.com", 33445, "7A6098B590BDC73F9723FC59F82B3F9085A64D1B213AAF8E610FD351930D052D", {0}}, | {"tox2.abilinski.com", 33445, "7A6098B590BDC73F9723FC59F82B3F9085A64D1B213AAF8E610FD351930D052D"}, |
{"205.185.115.131", 53, "3091C6BEB2A993F1C6300C16549FABA67098FF3D62C6D253828B531470B53D68", {0}}, | {"205.185.115.131", 53, "3091C6BEB2A993F1C6300C16549FABA67098FF3D62C6D253828B531470B53D68"}, |
{"tox.kurnevsky.net", 33445, "82EF82BA33445A1F91A7DB27189ECFC0C013E06E3DA71F588ED692BED625EC23", {0}} | {"tox.kurnevsky.net", 33445, "82EF82BA33445A1F91A7DB27189ECFC0C013E06E3DA71F588ED692BED625EC23"} |
}; | }; |
| |
for (size_t i = 0; i < sizeof(nodes)/sizeof(DHT_node); i ++) { | for (size_t i = 0; i < sizeof(nodes)/sizeof(DHT_node); i ++) { |
sodium_hex2bin(nodes[i].key_bin, sizeof(nodes[i].key_bin), | unsigned char key_bin[TOX_PUBLIC_KEY_SIZE]; |
nodes[i].key_hex, sizeof(nodes[i].key_hex)-1, NULL, NULL, NULL); | sodium_hex2bin(key_bin, sizeof(key_bin), nodes[i].key_hex, sizeof(nodes[i].key_hex)-1, |
tox_bootstrap(tox, nodes[i].ip, nodes[i].port, nodes[i].key_bin, NULL); | NULL, NULL, NULL); |
| tox_bootstrap(tox, nodes[i].ip, nodes[i].port, key_bin, NULL); |
} | } |
| |
| |
tox_callback_self_connection_status(tox, self_connection_status_cb); | tox_callback_self_connection_status(tox, self_connection_status_cb); |
| |
| printf("Connecting...\n"); |
| |
while (1) { | while (1) { |
$ ./echo_bot | $ ./echo_bot |
Tox ID: 4F8E7814B40B22F7DBB8B18B8518EBB369F45DE6B40309F43F39AFECF340FD7624FC706CE668 | Tox ID: 4F8E7814B40B22F7DBB8B18B8518EBB369F45DE6B40309F43F39AFECF340FD7624FC706CE668 |
| Connecting... |
Online, using UDP | Online, using UDP |
</code> | </code> |
uint16_t port; | uint16_t port; |
const char key_hex[TOX_PUBLIC_KEY_SIZE*2 + 1]; | const char key_hex[TOX_PUBLIC_KEY_SIZE*2 + 1]; |
unsigned char key_bin[TOX_PUBLIC_KEY_SIZE]; | |
} DHT_node; | } DHT_node; |
| |
fseek(f, 0, SEEK_SET); | fseek(f, 0, SEEK_SET); |
| |
char *savedata = malloc(fsize); | uint8_t *savedata = malloc(fsize); |
| |
fread(savedata, fsize, 1, f); | fread(savedata, fsize, 1, f); |
{ | { |
size_t size = tox_get_savedata_size(tox); | size_t size = tox_get_savedata_size(tox); |
char *savedata = malloc(size); | uint8_t *savedata = malloc(size); |
tox_get_savedata(tox, savedata); | tox_get_savedata(tox, savedata); |
| |
DHT_node nodes[] = | DHT_node nodes[] = |
{ | { |
{"85.143.221.42", 33445, "DA4E4ED4B697F2E9B000EEFE3A34B554ACD3F45F5C96EAEA2516DD7FF9AF7B43", {0}}, | {"85.143.221.42", 33445, "DA4E4ED4B697F2E9B000EEFE3A34B554ACD3F45F5C96EAEA2516DD7FF9AF7B43"}, |
{"2a04:ac00:1:9f00:5054:ff:fe01:becd", 33445, "DA4E4ED4B697F2E9B000EEFE3A34B554ACD3F45F5C96EAEA2516DD7FF9AF7B43", {0}}, | {"2a04:ac00:1:9f00:5054:ff:fe01:becd", 33445, "DA4E4ED4B697F2E9B000EEFE3A34B554ACD3F45F5C96EAEA2516DD7FF9AF7B43"}, |
{"78.46.73.141", 33445, "02807CF4F8BB8FB390CC3794BDF1E8449E9A8392C5D3F2200019DA9F1E812E46", {0}}, | {"78.46.73.141", 33445, "02807CF4F8BB8FB390CC3794BDF1E8449E9A8392C5D3F2200019DA9F1E812E46"}, |
{"2a01:4f8:120:4091::3", 33445, "02807CF4F8BB8FB390CC3794BDF1E8449E9A8392C5D3F2200019DA9F1E812E46", {0}}, | {"2a01:4f8:120:4091::3", 33445, "02807CF4F8BB8FB390CC3794BDF1E8449E9A8392C5D3F2200019DA9F1E812E46"}, |
{"tox.initramfs.io", 33445, "3F0A45A268367C1BEA652F258C85F4A66DA76BCAA667A49E770BCC4917AB6A25", {0}}, | {"tox.initramfs.io", 33445, "3F0A45A268367C1BEA652F258C85F4A66DA76BCAA667A49E770BCC4917AB6A25"}, |
{"tox2.abilinski.com", 33445, "7A6098B590BDC73F9723FC59F82B3F9085A64D1B213AAF8E610FD351930D052D", {0}}, | {"tox2.abilinski.com", 33445, "7A6098B590BDC73F9723FC59F82B3F9085A64D1B213AAF8E610FD351930D052D"}, |
{"205.185.115.131", 53, "3091C6BEB2A993F1C6300C16549FABA67098FF3D62C6D253828B531470B53D68", {0}}, | {"205.185.115.131", 53, "3091C6BEB2A993F1C6300C16549FABA67098FF3D62C6D253828B531470B53D68"}, |
{"tox.kurnevsky.net", 33445, "82EF82BA33445A1F91A7DB27189ECFC0C013E06E3DA71F588ED692BED625EC23", {0}} | {"tox.kurnevsky.net", 33445, "82EF82BA33445A1F91A7DB27189ECFC0C013E06E3DA71F588ED692BED625EC23"} |
}; | }; |
| |
for (size_t i = 0; i < sizeof(nodes)/sizeof(DHT_node); i ++) { | for (size_t i = 0; i < sizeof(nodes)/sizeof(DHT_node); i ++) { |
sodium_hex2bin(nodes[i].key_bin, sizeof(nodes[i].key_bin), | unsigned char key_bin[TOX_PUBLIC_KEY_SIZE]; |
nodes[i].key_hex, sizeof(nodes[i].key_hex)-1, NULL, NULL, NULL); | sodium_hex2bin(key_bin, sizeof(key_bin), nodes[i].key_hex, sizeof(nodes[i].key_hex)-1, |
tox_bootstrap(tox, nodes[i].ip, nodes[i].port, nodes[i].key_bin, NULL); | NULL, NULL, NULL); |
| tox_bootstrap(tox, nodes[i].ip, nodes[i].port, key_bin, NULL); |
} | } |
} | } |
| |
void friend_request_cb(Tox *tox, const uint8_t *public_key, const uint8_t *message, size_t length, | void friend_request_cb(Tox *tox, const uint8_t *public_key, const uint8_t *message, size_t length, |
void *user_data) | void *user_data) |
{ | { |
tox_friend_add_norequest(tox, public_key, NULL); | tox_friend_add_norequest(tox, public_key, NULL); |
| |
void friend_message_cb(Tox *tox, uint32_t friend_number, TOX_MESSAGE_TYPE type, const uint8_t *message, | void friend_message_cb(Tox *tox, uint32_t friend_number, TOX_MESSAGE_TYPE type, const uint8_t *message, |
size_t length, void *user_data) | size_t length, void *user_data) |
{ | { |
tox_friend_send_message(tox, friend_number, type, message, length, NULL); | tox_friend_send_message(tox, friend_number, type, message, length, NULL); |
| |
update_savedata_file(tox); | update_savedata_file(tox); |
| |
| printf("Connecting...\n"); |
| |
while (1) { | while (1) { |