diff options
| author | Anton <anton@picapica.im> | 2026-11-15 21:25:50 +0400 |
|---|---|---|
| committer | Anton <anton@picapica.im> | 2026-11-15 21:25:50 +0400 |
| commit | 0f9e20bdd1159b65013dcc27bf1a6fb30c5cb115 (patch) | |
| tree | 97f0e973ac7dd58b84f52af4a079568b1900d06f /PICA_node.c | |
| parent | bd0bc3949a8fed3a2bd87b39cbeecd0b5cfcafd1 (diff) | |
| download | pica-pica-0f9e20bdd1159b65013dcc27bf1a6fb30c5cb115.tar.gz pica-pica-0f9e20bdd1159b65013dcc27bf1a6fb30c5cb115.zip | |
proto update: add get my addr and get my UDP addr messages
Diffstat (limited to 'PICA_node.c')
| -rw-r--r-- | PICA_node.c | 272 |
1 files changed, 270 insertions, 2 deletions
diff --git a/PICA_node.c b/PICA_node.c index f04df00..2eb97d6 100644 --- a/PICA_node.c +++ b/PICA_node.c @@ -60,6 +60,17 @@ typedef u_short uint16_t; #define NOREPLY_TIMEOUT 15 #define KEEPALIVE_TIMEOUT 60 +//"Get my UDP address": how long an issued token is accepted from the UDP socket +#define UDPADDR_TOKEN_TIMEOUT 10 //sec +//A client is served this many MYUDPADDRREQ messages in a row, and one more +//for every UDPADDR_REQ_PERIOD seconds after that +#define UDPADDR_REQ_BURST 4 +#define UDPADDR_REQ_PERIOD 5 //sec +#define UDPADDR_TOKEN_BUCKETS 1024 +//datagrams taken from the UDP socket per event, so that a flood of them does +//not keep the event loop away from the connections +#define UDP_READ_BATCH 64 + time_t skynet_refresh_tmst; time_t TMO_CCLINK_WAITACTIVE = 15; //CONF @@ -68,6 +79,12 @@ char *my_addr;//TEMP CONF FIXME static struct event_base *pica_event_base; SOCKET listen_comm_sck;//* +//UDP socket on the port with the same number as listen_comm_sck, -1 if it +//could not be opened. Nothing is ever sent from it. +SOCKET listen_udp_sck = -1; + +//clients with an issued "get my UDP address" token, hashed by the token +static struct client *udpaddr_tokens[UDPADDR_TOKEN_BUCKETS]; static int stop_node_loop = 0; @@ -119,6 +136,8 @@ unsigned int procmsg_PINGREP_node(unsigned char* buf, unsigned int size, void* p unsigned int procmsg_dummy_multi_c2n(unsigned char* buf, unsigned int size, void* ptr); unsigned int procmsg_MULTILOGIN_client(unsigned char* buf, unsigned int size, void* ptr); unsigned int procmsg_MULTILOGIN_node(unsigned char* buf, unsigned int size, void* ptr); +unsigned int procmsg_MYTCPADDRREQ(unsigned char* buf, unsigned int size, void* ptr); +unsigned int procmsg_MYUDPADDRREQ(unsigned char* buf, unsigned int size, void* ptr); struct cclink* cclink_list_add(struct client *clr, struct client *cle); struct cclink* cclink_list_search(const unsigned char *caller_id, const unsigned char *callee_id); @@ -164,6 +183,7 @@ void process_n2n_read(evutil_socket_t s, short event, void *arg); void process_n2n_write(evutil_socket_t s, short event, void *arg); void process_c2c_read(evutil_socket_t s, short event, void *arg); void process_c2c_write(evutil_socket_t s, short event, void *arg); +void process_udp_read(evutil_socket_t s, short event, void *arg); void process_nodewait(); #define MSGINFO_MSGSNUM(arr) (sizeof(arr)/sizeof(struct PICA_msginfo)) @@ -176,7 +196,9 @@ struct PICA_msginfo _msginfo_comm[] = {PICA_PROTO_CLNODELISTREQ, PICA_MSG_FIXED_SIZE, PICA_PROTO_CLNODELISTREQ_SIZE, procmsg_NODELISTREQ}, {PICA_PROTO_PINGREQ, PICA_MSG_FIXED_SIZE, PICA_PROTO_PINGREQ_SIZE, procmsg_PINGREQ_client}, {PICA_PROTO_PINGREP, PICA_MSG_FIXED_SIZE, PICA_PROTO_PINGREP_SIZE, procmsg_PINGREP_client}, - {PICA_PROTO_MULTILOGIN, PICA_MSG_VAR_SIZE, PICA_MSG_VARSIZE_INT16, procmsg_MULTILOGIN_client} + {PICA_PROTO_MULTILOGIN, PICA_MSG_VAR_SIZE, PICA_MSG_VARSIZE_INT16, procmsg_MULTILOGIN_client}, + {PICA_PROTO_MYTCPADDRREQ, PICA_MSG_FIXED_SIZE, PICA_PROTO_MYTCPADDRREQ_SIZE, procmsg_MYTCPADDRREQ}, + {PICA_PROTO_MYUDPADDRREQ, PICA_MSG_FIXED_SIZE, PICA_PROTO_MYUDPADDRREQ_SIZE, procmsg_MYUDPADDRREQ} }; struct PICA_msginfo _msginfo_node[] = @@ -214,7 +236,9 @@ struct PICA_msginfo _msginfo_multi[] = {PICA_PROTO_CLNODELISTREQ, PICA_MSG_FIXED_SIZE, PICA_PROTO_CLNODELISTREQ_SIZE, procmsg_NODELISTREQ}, {PICA_PROTO_PINGREQ, PICA_MSG_FIXED_SIZE, PICA_PROTO_PINGREQ_SIZE, procmsg_PINGREQ_client}, {PICA_PROTO_PINGREP, PICA_MSG_FIXED_SIZE, PICA_PROTO_PINGREP_SIZE, procmsg_PINGREP_client}, - {PICA_PROTO_MULTILOGIN, PICA_MSG_VAR_SIZE, PICA_MSG_VARSIZE_INT16, procmsg_MULTILOGIN_client} + {PICA_PROTO_MULTILOGIN, PICA_MSG_VAR_SIZE, PICA_MSG_VARSIZE_INT16, procmsg_MULTILOGIN_client}, + {PICA_PROTO_MYTCPADDRREQ, PICA_MSG_FIXED_SIZE, PICA_PROTO_MYTCPADDRREQ_SIZE, procmsg_MYTCPADDRREQ}, + {PICA_PROTO_MYUDPADDRREQ, PICA_MSG_FIXED_SIZE, PICA_PROTO_MYUDPADDRREQ_SIZE, procmsg_MYUDPADDRREQ} }; static int process_async_ssl_readwrite(SSL *ssl, int ret) @@ -659,6 +683,136 @@ unsigned int procmsg_PINGREP_client(unsigned char* buf, unsigned int size, void* return 1; } +unsigned int procmsg_MYTCPADDRREQ(unsigned char* buf, unsigned int size, void* ptr) +{ + struct client *c = (struct client*)ptr; + struct PICA_proto_msg *mp; + + PICA_debug3("MYTCPADDRREQ"); + + //IPv6 + if ((mp = client_wbuf_push(c, PICA_PROTO_MYTCPADDR_IPV4, PICA_PROTO_MYTCPADDR_IPV4_SIZE))) + { + memcpy(mp->tail, &c->addr.sin_addr.s_addr, 4); + memcpy(mp->tail + 4, &c->addr.sin_port, 2); + } + else + return 0; + + return 1; +} + +static unsigned int udpaddr_token_bucket(const unsigned char *token) +{ + //tokens are random and are generated by the node itself, so any part of + //one is as good a hash as any other, and a client has no say in which + //bucket it ends up in + return (token[0] | (token[1] << 8)) % UDPADDR_TOKEN_BUCKETS; +} + +static void client_udpaddr_token_drop(struct client *c) +{ + struct client **pp; + + if (!c->udpaddr_token_issued) + return; + + pp = &udpaddr_tokens[udpaddr_token_bucket(c->udpaddr_token)]; + + while(*pp && *pp != c) + pp = &(*pp)->udpaddr_token_next; + + if (*pp) + *pp = c->udpaddr_token_next; + + c->udpaddr_token_next = NULL; + c->udpaddr_token_issued = 0; +} + +static struct client *udpaddr_token_search(const unsigned char *token) +{ + struct client *c = udpaddr_tokens[udpaddr_token_bucket(token)]; + + while(c && CRYPTO_memcmp(c->udpaddr_token, token, PICA_PROTO_MYUDPADDR_TOKENSIZE) != 0) + c = c->udpaddr_token_next; + + return c; +} + +static int client_udpaddr_req_allowed(struct client *c) +{ + time_t now = time(0); + time_t periods; + + if (now < c->udpaddr_req_tmst)//the clock was set back + c->udpaddr_req_tmst = now; + + periods = (now - c->udpaddr_req_tmst) / UDPADDR_REQ_PERIOD; + + if (periods >= (time_t)(UDPADDR_REQ_BURST - c->udpaddr_req_allowance)) + { + c->udpaddr_req_allowance = UDPADDR_REQ_BURST; + c->udpaddr_req_tmst = now; + } + else + { + c->udpaddr_req_allowance += periods; + c->udpaddr_req_tmst += periods * UDPADDR_REQ_PERIOD; + } + + if (!c->udpaddr_req_allowance) + return 0; + + c->udpaddr_req_allowance--; + + return 1; +} + +//The reply to this message is a token, which the client then sends to the UDP +//socket - see process_udp_read() for what makes that exchange safe to offer. +unsigned int procmsg_MYUDPADDRREQ(unsigned char* buf, unsigned int size, void* ptr) +{ + struct client *c = (struct client*)ptr; + struct PICA_proto_msg *mp; + unsigned char token[PICA_PROTO_MYUDPADDR_TOKENSIZE]; + + PICA_debug3("MYUDPADDRREQ"); + + //A request that is not served is ignored rather than answered: the client + //has to cope with its datagrams being lost anyway, and gives up by timeout. + if (listen_udp_sck == -1) + return 1; + + if (!client_udpaddr_req_allowed(c)) + { + PICA_debug2("too many MYUDPADDRREQ from client %p, ignoring", (void*)c); + return 1; + } + + if (RAND_bytes(token, sizeof token) != 1) + { + PICA_error("RAND_bytes() failed, unable to issue a token for MYUDPADDRREQ"); + return 1; + } + + if (!(mp = client_wbuf_push(c, PICA_PROTO_MYUDPADDRTOKEN, PICA_PROTO_MYUDPADDRTOKEN_SIZE))) + return 0; + + memcpy(mp->tail, token, sizeof token); + + //one outstanding token per connection, a new request replaces the old one + client_udpaddr_token_drop(c); + + memcpy(c->udpaddr_token, token, sizeof token); + c->udpaddr_token_tmst = time(0); + c->udpaddr_token_issued = 1; + + c->udpaddr_token_next = udpaddr_tokens[udpaddr_token_bucket(token)]; + udpaddr_tokens[udpaddr_token_bucket(token)] = c; + + return 1; +} + unsigned int procmsg_MULTILOGIN_client(unsigned char* buf, unsigned int size, void* ptr) { struct client *other, *me = (struct client*)ptr; @@ -1802,6 +1956,33 @@ int PICA_node_init() event_add(listener_event, /*&tv*/ NULL); + //The UDP socket clients use to learn the address their NAT gives them, on + //the port with the same number as the TCP one. A node works without it. + listen_udp_sck = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); + + if (listen_udp_sck != -1 && bind(listen_udp_sck, (struct sockaddr*)&sd, sizeof(sd)) == -1) + { + CLOSE(listen_udp_sck); + listen_udp_sck = -1; + } + + if (listen_udp_sck != -1) + { + struct event *udp_event; + + IOCTLSETNONBLOCKINGSOCKET(listen_udp_sck, 1); + + udp_event = event_new(pica_event_base, listen_udp_sck, EV_READ|EV_PERSIST, process_udp_read, NULL); + + if (!udp_event) + PICA_fatal("failed to allocate UDP socket event"); + + event_add(udp_event, NULL); + } + else + PICA_error("unable to open UDP port %u, clients will not be able to learn their UDP addresses from this node - %s", + (unsigned int)ntohs(sd.sin_port), strerror(errno)); + timeout_event = event_new(pica_event_base, -1, EV_TIMEOUT|EV_PERSIST, process_timeout_event, NULL); event_add(timeout_event, &tv); @@ -2169,6 +2350,9 @@ struct client *client_list_addnew(struct newconn *nc) ci->tmst = time(0); + ci->udpaddr_req_allowance = UDPADDR_REQ_BURST; + ci->udpaddr_req_tmst = ci->tmst; + ci->ev_read = event_new(pica_event_base, ci->sck_comm, EV_READ | EV_PERSIST, process_c2n_read, ci); ci->ev_write = event_new(pica_event_base, ci->sck_comm, EV_WRITE, process_c2n_write, ci); @@ -2253,6 +2437,8 @@ void client_list_delete(struct client* ci) cclink_list_delete_by_client(ci); + client_udpaddr_token_drop(ci); + if (ci->r_buf) free(ci->r_buf); @@ -2785,6 +2971,88 @@ void process_listen_read(evutil_socket_t listener, short event, void *arg) //IPv6 } +//Tells a client the address and port its datagram came from - the address its +//NAT gave to the socket the datagram was sent from. +// +//Anyone can send anything to this socket, with any source address, so: +// - nothing is ever sent from it. The answer goes over the c2n connection of +// the client that asked, which leaves nothing to reflect or amplify; +// - a datagram is acted upon only if it carries a token this node issued over +// an authenticated c2n connection moments ago. The token is random, so it +// cannot be guessed, and is good for one datagram, so a client is sent one +// answer per request it made itself and duplicates are dropped; +// - a datagram that is not acted upon costs a table lookup and leaves no +// state behind, and it is not logged. +//The source address itself cannot be verified, and does not need to be: it +//is only reported back to the client the token belongs to. +void process_udp_read(evutil_socket_t s, short event, void *arg) +{ + //one octet more than a valid datagram has, so that a longer one does not + //pass for a valid one after being truncated + unsigned char buf[PICA_PROTO_MYUDPADDRPROBE_SIZE + 1]; + int i; + + for (i = 0; i < UDP_READ_BATCH; i++) + { + struct sockaddr_in from; +#ifdef WIN32 + int fromlen = sizeof(from); +#else + socklen_t fromlen = sizeof(from); +#endif + struct client *c; + struct PICA_proto_msg *mp; + time_t now; + int ret; + + ret = recvfrom(s, (char*)buf, sizeof(buf), 0, (struct sockaddr*)&from, &fromlen); + + if (ret == -1) + { +#ifdef WIN32 + if (WSAGetLastError() == WSAEWOULDBLOCK) + break; +#else + if (errno == EAGAIN || errno == EWOULDBLOCK) + break; +#endif + //anything else is about the one datagram, e.g. it did not fit + //into the buffer on Windows + continue; + } + + if (ret != PICA_PROTO_MYUDPADDRPROBE_SIZE || buf[0] != PICA_PROTO_MYUDPADDRPROBE || buf[1] != PICA_PROTO_MYUDPADDRPROBE) + continue; + + if (fromlen < sizeof(from) || from.sin_family != AF_INET) + continue; + + c = udpaddr_token_search(buf + 2); + + if (!c) + continue; + + now = time(0); + ret = (now >= c->udpaddr_token_tmst && now - c->udpaddr_token_tmst <= UDPADDR_TOKEN_TIMEOUT); + + client_udpaddr_token_drop(c); + + if (!ret) + continue; + + PICA_debug3("MYUDPADDRPROBE for client %p from %.16s:%hu", (void*)c, inet_ntoa(from.sin_addr), ntohs(from.sin_port)); + + //IPv6 + if ((mp = client_wbuf_push(c, PICA_PROTO_MYUDPADDR, 2 + PICA_MSG_VARSIZE_INT8 + PICA_PROTO_MYUDPADDR_IPV4_SIZE))) + { + mp->tail[0] = PICA_PROTO_MYUDPADDR_IPV4_SIZE; + mp->tail[1] = PICA_PROTO_MYUDPADDR_IPV4; + memcpy(mp->tail + 2, &from.sin_addr.s_addr, 4); + memcpy(mp->tail + 6, &from.sin_port, 2); + } + } +} + void process_newconn_write(struct newconn *nc) { int ret; |
