|
|
|
|
@ -9,10 +9,10 @@
|
|
|
|
|
#include "../lib/u_async.h" |
|
|
|
|
#include "../lib/debug_config.h" |
|
|
|
|
#include "../lib/mem.h" |
|
|
|
|
#include "test_net.h" |
|
|
|
|
#include <stdio.h> |
|
|
|
|
#include <string.h> |
|
|
|
|
#include <stdlib.h> |
|
|
|
|
#include <time.h> |
|
|
|
|
|
|
|
|
|
static int ping_ok = -1, ping_rtt = 0; |
|
|
|
|
static void ping_cb(int success, uint16_t rtt, void *arg, uint64_t nonce, |
|
|
|
|
@ -28,7 +28,7 @@ static void unexpected_ready(struct stcp_conn *c, void *arg) {
|
|
|
|
|
ping_ok = 0; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static char *build_config(const char *node_id, const char *priv, const char *pub, int port) { |
|
|
|
|
static char *build_config(const char *node_id, const char *priv, const char *pub) { |
|
|
|
|
static char buf[512]; |
|
|
|
|
snprintf(buf, sizeof(buf), |
|
|
|
|
"[global]\n" |
|
|
|
|
@ -37,10 +37,10 @@ static char *build_config(const char *node_id, const char *priv, const char *pub
|
|
|
|
|
"my_public_key=%s\n" |
|
|
|
|
"\n" |
|
|
|
|
"[server: stcp]\n" |
|
|
|
|
"addr=127.0.0.1:%d\n" |
|
|
|
|
"addr=127.0.0.1:0\n" |
|
|
|
|
"type=public\n" |
|
|
|
|
"transport=tcp\n", |
|
|
|
|
node_id, priv, pub, port); |
|
|
|
|
node_id, priv, pub); |
|
|
|
|
return buf; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -50,22 +50,20 @@ int main(void) {
|
|
|
|
|
debug_set_categories(DEBUG_CATEGORY_GENERAL | DEBUG_CATEGORY_ETCP | DEBUG_CATEGORY_CONNECTION); |
|
|
|
|
DEBUG_INFO(DEBUG_CATEGORY_GENERAL, "=== TCP ping (etcp_send_tcp_ping) ==="); |
|
|
|
|
|
|
|
|
|
srand((unsigned)time(NULL)); |
|
|
|
|
struct UASYNC *ua = uasync_create(); |
|
|
|
|
if (!ua) return 1; |
|
|
|
|
utun_instance_set_tun_init_enabled(0); |
|
|
|
|
int port = 27000 + rand() % 1000; |
|
|
|
|
|
|
|
|
|
struct UTUN_INSTANCE *srv = utun_instance_create_from_str(ua, build_config( |
|
|
|
|
"AAAAAAAAAAAAAAAA", |
|
|
|
|
"38240cb82199e504686507f11f6eaa4f740fde6f0c425c495e49a523019a5d68", |
|
|
|
|
"ce8871f07fa056c636d297115f231b08c29cdf94e0d440fce83a07c34416d36a", port)); |
|
|
|
|
"ce8871f07fa056c636d297115f231b08c29cdf94e0d440fce83a07c34416d36a")); |
|
|
|
|
if (!srv || utun_instance_init(srv) < 0) { DEBUG_ERROR(DEBUG_CATEGORY_GENERAL, "srv init failed"); return 1; } |
|
|
|
|
|
|
|
|
|
struct UTUN_INSTANCE *cli = utun_instance_create_from_str(ua, build_config( |
|
|
|
|
"BBBBBBBBBBBBBBBB", |
|
|
|
|
"704f2e012c8fa8768130cb0f988a997dccb628372bc5ceccacc78dcbfec5916f", |
|
|
|
|
"b3193173def895bd0fcea6f86af077c7d77216f10395275f627ac18242ec0f01", port + 1)); |
|
|
|
|
"b3193173def895bd0fcea6f86af077c7d77216f10395275f627ac18242ec0f01")); |
|
|
|
|
if (!cli || utun_instance_init(cli) < 0) { DEBUG_ERROR(DEBUG_CATEGORY_GENERAL, "cli init failed"); return 1; } |
|
|
|
|
|
|
|
|
|
uint8_t srv_pub[SC_PUBKEY_SIZE]; |
|
|
|
|
@ -74,9 +72,7 @@ int main(void) {
|
|
|
|
|
srv->padding.min = srv->padding.max = ETCP_PADDING_MAX; |
|
|
|
|
|
|
|
|
|
/* success path: ping listening STCP server */ |
|
|
|
|
struct sockaddr_storage addr; memset(&addr, 0, sizeof(addr)); |
|
|
|
|
struct sockaddr_in *sin = (struct sockaddr_in *)&addr; |
|
|
|
|
sin->sin_family = AF_INET; sin->sin_addr.s_addr = htonl(0x7f000001); sin->sin_port = htons(port); |
|
|
|
|
struct sockaddr_storage addr = test_etcp_bound_socket(srv, "stcp")->local_addr; |
|
|
|
|
|
|
|
|
|
int rc = etcp_send_tcp_ping(cli, srv_pub, &addr, 2000, ping_cb, NULL, NULL, NULL); |
|
|
|
|
if (rc != 0) { DEBUG_ERROR(DEBUG_CATEGORY_GENERAL, "etcp_send_tcp_ping failed rc=%d", rc); return 1; } |
|
|
|
|
@ -97,14 +93,23 @@ int main(void) {
|
|
|
|
|
if (ping_ok != 1) { DEBUG_ERROR(DEBUG_CATEGORY_GENERAL, "TCP ping zero server padding failed"); return 1; } |
|
|
|
|
DEBUG_INFO(DEBUG_CATEGORY_GENERAL, "=== reversed padding boundary PASSED ==="); |
|
|
|
|
|
|
|
|
|
/* failure path: port with no listener */ |
|
|
|
|
/* Отрицательный endpoint удерживаем привязанным сокетом без listen(). */ |
|
|
|
|
ping_ok = -1; ping_rtt = 0; |
|
|
|
|
struct sockaddr_storage bad_addr; memset(&bad_addr, 0, sizeof(bad_addr)); |
|
|
|
|
struct sockaddr_in *bsin = (struct sockaddr_in *)&bad_addr; |
|
|
|
|
bsin->sin_family = AF_INET; bsin->sin_addr.s_addr = htonl(0x7f000001); bsin->sin_port = htons(port + 5000); |
|
|
|
|
bsin->sin_family = AF_INET; bsin->sin_addr.s_addr = htonl(0x7f000001); |
|
|
|
|
socket_t refused = socket(AF_INET, SOCK_STREAM, 0); |
|
|
|
|
if (refused == SOCKET_INVALID || bind(refused, (struct sockaddr*)bsin, sizeof(*bsin)) != 0) { |
|
|
|
|
DEBUG_ERROR(DEBUG_CATEGORY_SOCKET, "TCP ping: cannot reserve refused endpoint: %s", socket_strerror(socket_get_error())); |
|
|
|
|
if (refused != SOCKET_INVALID) socket_close_wrapper(refused); |
|
|
|
|
return 1; |
|
|
|
|
} |
|
|
|
|
bsin->sin_port = htons(test_bound_port(refused)); |
|
|
|
|
DEBUG_INFO(DEBUG_CATEGORY_SOCKET, "TCP ping: reserved non-listening port=%u", ntohs(bsin->sin_port)); |
|
|
|
|
rc = etcp_send_tcp_ping(cli, srv_pub, &bad_addr, 1000, ping_cb, NULL, NULL, NULL); |
|
|
|
|
ticks = 0; |
|
|
|
|
while (ping_ok < 0 && ticks < 3000) { uasync_poll(ua, 10); ticks++; } |
|
|
|
|
socket_close_wrapper(refused); |
|
|
|
|
if (ping_ok != 0) { DEBUG_ERROR(DEBUG_CATEGORY_GENERAL, "=== FAILED (failure path): success=%d rtt=%u rc=%d ===", ping_ok, ping_rtt, rc); return 1; } |
|
|
|
|
DEBUG_INFO(DEBUG_CATEGORY_GENERAL, "=== failure path PASSED: success=%d ===", ping_ok); |
|
|
|
|
|
|
|
|
|
@ -113,11 +118,11 @@ int main(void) {
|
|
|
|
|
reality.enabled = 1; reality.short_id_count = 1; |
|
|
|
|
if (reality_generate_keypair(reality.private_key, cc.server_static_pubkey)) return 1; |
|
|
|
|
strcpy(cc.server_name, "www.microsoft.com"); |
|
|
|
|
struct stcp_server *rs = stcp_server_create(ua, port + 2, &srv->my_keys, srv->my_ed25519_pubkey, |
|
|
|
|
struct stcp_server *rs = stcp_server_create(ua, 0, &srv->my_keys, srv->my_ed25519_pubkey, |
|
|
|
|
srv, unexpected_ready, NULL, NULL, NULL, AF_INET); |
|
|
|
|
if (!rs) return 1; |
|
|
|
|
stcp_server_set_reality(rs, &reality); |
|
|
|
|
sin->sin_port = htons(port + 2); |
|
|
|
|
((struct sockaddr_in*)&addr)->sin_port = htons(stcp_server_get_port(rs)); |
|
|
|
|
|
|
|
|
|
/* Одноразовый TCP-пинг не удерживает instance: длина выбирается до асинхронного connect. */ |
|
|
|
|
ping_ok = -1; |
|
|
|
|
|