summaryrefslogtreecommitdiffstats
path: root/PICA_node.c
diff options
context:
space:
mode:
Diffstat (limited to 'PICA_node.c')
-rw-r--r--PICA_node.c848
1 files changed, 660 insertions, 188 deletions
diff --git a/PICA_node.c b/PICA_node.c
index 4a59102..e2ccd16 100644
--- a/PICA_node.c
+++ b/PICA_node.c
@@ -60,14 +60,31 @@ 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;
-clock_t TMO_CCLINK_WAITACTIVE = 15; //CONF
+time_t TMO_CCLINK_WAITACTIVE = 15; //CONF
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)
@@ -268,6 +292,13 @@ unsigned int procmsg_INITREQ(unsigned char* buf, unsigned int size, void* ptr)
PICA_debug1("received INITREQ");
+ /* Exactly one request message per newconn. A second (e.g. two INITREQs in
+ * one record) would create a second client on the same socket and orphan
+ * the first, which later closes a socket it no longer owns.
+ */
+ if (nc->type != NEWCONN_UNKNOWN)
+ return 0;
+
nc->type = NEWCONN_C2N;
if (buf[2] == PICA_PROTO_VER_HIGH && buf[3] == PICA_PROTO_VER_LOW)
@@ -297,11 +328,11 @@ unsigned int procmsg_INITREQ(unsigned char* buf, unsigned int size, void* ptr)
if (buf[2] > PICA_PROTO_VER_HIGH || (buf[2] == PICA_PROTO_VER_HIGH && buf[3] > PICA_PROTO_VER_LOW))
{
- PICA_warn("Client has newer protocol version than node. Please check if update for pica-node is available");
+ PICA_warn("The client has a newer protocol version than the node. Please check if an update for pica-node is available");
}
else
{
- PICA_info("Client has older protocol version. Disconnecting ...");
+ PICA_info("The client has an older protocol version. Disconnecting...");
}
}
@@ -350,6 +381,10 @@ unsigned int procmsg_CONNID(unsigned char* buf, unsigned int size, void* ptr)
PICA_debug1("received CONNID");
+ /* One request message per newconn - see procmsg_INITREQ(). */
+ if (nc->type != NEWCONN_UNKNOWN)
+ return 0;
+
nc->type = NEWCONN_C2C;
caller_id = buf + 2;
@@ -377,8 +412,8 @@ unsigned int procmsg_CONNID(unsigned char* buf, unsigned int size, void* ptr)
{
struct client *c;
-//проверка IP адреса.
-//IP адрес должен совпадать с адресом клиента в управляющем соединении
+//IP address check.
+//IP address must match the client's address in the control connection
if (link->state == PICA_CCLINK_LOCAL_WAITCONNCLE || link->state == PICA_CCLINK_N2NCLE_WAITCONNCLE)
c = link->p2;
@@ -405,12 +440,16 @@ unsigned int procmsg_NODECONNREQ(unsigned char* buf, unsigned int size, void* pt
PICA_debug1("received NODECONNREQ");
+ /* One request message per newconn - see procmsg_INITREQ(). */
+ if (nc->type != NEWCONN_UNKNOWN)
+ return 0;
+
nc->type = NEWCONN_N2N;
if (buf[2] == PICA_PROTO_VER_HIGH &&
buf[3] == PICA_PROTO_VER_LOW)
{
- ////-прием входящего подключения
+ ////-accepting incoming connection
//--------------------------
nc->state = PICA_NEWCONN_SENDINGOK;
/*struct nodelink *nl;
@@ -434,12 +473,12 @@ unsigned int procmsg_NODECONNREQ(unsigned char* buf, unsigned int size, void* pt
if (buf[2] > PICA_PROTO_VER_HIGH || (buf[2] == PICA_PROTO_VER_HIGH && buf[3] > PICA_PROTO_VER_LOW))
{
- PICA_warn("Node %s has newer protocol version [%u, %u]. Please check if update for pica-node is available",
+ PICA_warn("Node %s has a newer protocol version [%u, %u]. Please check if an update for pica-node is available",
inet_ntoa(nc->addr.sin_addr), buf[2], buf[3]);
}
else
{
- PICA_info("Node %s has older protocol version. Disconnecting ...", inet_ntoa(nc->addr.sin_addr));
+ PICA_info("Node %s has an older protocol version. Disconnecting...", inet_ntoa(nc->addr.sin_addr));
}
return 0;*/
@@ -481,7 +520,7 @@ int nodelink_send_searchreq(const unsigned char *id)
unsigned int procmsg_CONNREQOUTG(unsigned char* buf, unsigned int size, void* ptr)
{
- struct client *i, *o; //i - указатель на вызывающего клиента, o - вызываемого(если найден)
+ struct client *i, *o; //i - pointer to the calling client, o - to the callee (if found)
unsigned char *callee_id;
struct PICA_proto_msg *mp;
@@ -506,7 +545,7 @@ unsigned int procmsg_CONNREQOUTG(unsigned char* buf, unsigned int size, void* pt
return 1;
}
- //отправить o _CONNREQINC
+ //send _CONNREQINC to o
if ((mp = client_wbuf_push(o, PICA_PROTO_CONNREQINC, PICA_PROTO_CONNREQINC_SIZE)))
{
memcpy(mp->tail, i->id, PICA_ID_SIZE);
@@ -517,7 +556,7 @@ unsigned int procmsg_CONNREQOUTG(unsigned char* buf, unsigned int size, void* pt
return 1;
}
- return 1;//сообщение отправить не удалось, но все равно возвращаем 1
+ return 1;//failed to send the message, but return 1 anyway
}
if (cclink_list_findwaitsearch(callee_id)/*search request for this id is already in progress*/
@@ -527,7 +566,7 @@ unsigned int procmsg_CONNREQOUTG(unsigned char* buf, unsigned int size, void* pt
return 1;
}
-//отправить i _NOTFOUND
+//send _NOTFOUND to i
if ((mp = client_wbuf_push(i, PICA_PROTO_NOTFOUND, PICA_PROTO_NOTFOUND_SIZE)))
{
memcpy(mp->tail, callee_id, PICA_ID_SIZE);
@@ -595,7 +634,7 @@ unsigned int procmsg_CONNDENY(unsigned char* buf, unsigned int size, void* ptr)
if (link->state == PICA_CCLINK_LOCAL_WAITREP)
{
- //отправить caller _NOTFOUND
+ //send _NOTFOUND to caller
if ((mp = client_wbuf_push(caller, PICA_PROTO_NOTFOUND, PICA_PROTO_NOTFOUND_SIZE)))
{
memcpy(mp->tail, callee->id, PICA_ID_SIZE);
@@ -603,7 +642,7 @@ unsigned int procmsg_CONNDENY(unsigned char* buf, unsigned int size, void* ptr)
}
else if (link->state == PICA_CCLINK_N2NCLE_WAITREP)
{
- //отправить caller N2NNOTFOUND
+ //send N2NNOTFOUND to caller
if ((mp = nodelink_wbuf_push(link->caller_node, PICA_PROTO_N2NNOTFOUND, PICA_PROTO_N2NNOTFOUND_SIZE)))
{
memcpy(mp->tail, caller_id, PICA_ID_SIZE);
@@ -645,6 +684,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;
@@ -757,7 +926,7 @@ unsigned int procmsg_MULTILOGIN_node(unsigned char* buf, unsigned int size, void
if (!c)
{
- PICA_debug3("no matching client found for receved n2n MULTILOGIN");
+ PICA_debug3("no matching client found for received n2n MULTILOGIN");
return 1;
}
@@ -769,7 +938,7 @@ unsigned int procmsg_MULTILOGIN_node(unsigned char* buf, unsigned int size, void
if (mp)
{
- memcpy(mp, buf, size);
+ memcpy(mp->tail, buf + 2, size - 2);
}
c = c->next_multi;
@@ -849,7 +1018,7 @@ unsigned int procmsg_NODERESP(unsigned char* buf, unsigned int size, void* ptr)
case PICA_PROTO_VERDIFFER:
if (buf[2] > PICA_PROTO_VER_HIGH || (buf[3] > PICA_PROTO_VER_LOW && buf[2] == PICA_PROTO_VER_HIGH))
{
- PICA_warn("make update of node software - peer node has newer protocol version");
+ PICA_warn("update the node software - peer node has newer protocol version");
}
else
{
@@ -944,7 +1113,7 @@ unsigned int procmsg_NODELISTREQ(unsigned char* buf, unsigned int size, void* pt
{
//TEMP FIXME CONF
-//собственный адрес узла
+//node's own address
nl_buf[nl_size] = PICA_PROTO_NEWNODE_IPV4;
*((in_addr_t*)(nl_buf + nl_size + 1)) = inet_addr(my_addr);
*((in_port_t*)(nl_buf + nl_size + 5)) = (nodecfg.listen_port ? htons(atoi(nodecfg.listen_port)) : htons(PICA_COMM_PORT)); //CONF
@@ -957,7 +1126,7 @@ unsigned int procmsg_NODELISTREQ(unsigned char* buf, unsigned int size, void* pt
switch(nlp->addr_type)
{
case PICA_PROTO_NEWNODE_IPV4:
- if ( PICA_PROTO_NODELISTREQ == buf[0] &&
+ if ( PICA_PROTO_NODELISTREQ == buf[0] && n->node_addr &&
((struct PICA_nodeaddr_ipv4*)nlp->node_addr)->addr == ((struct PICA_nodeaddr_ipv4*)n->node_addr)->addr &&
((struct PICA_nodeaddr_ipv4*)nlp->node_addr)->port == ((struct PICA_nodeaddr_ipv4*)n->node_addr)->port )
break;//exclude requester's address
@@ -1004,68 +1173,72 @@ unsigned int procmsg_NODELIST(unsigned char* buf, unsigned int size, void* ptr)
while(listleft)
{
-#warning "strict error checking!!! -- listleft overflow, unknown list items, check listleft >= possible item size"
- if (listleft == listsize) //first entry, sender node's address
+ unsigned char item_type = buf[size - listleft];
+
+ /* The IPv6 and DNS list item types are defined by the protocol but
+ * are not produced or parsed yet (see procmsg_NODELISTREQ, which only
+ * emits IPv4 items). Their on-wire length is therefore not handled
+ * here, so the parse cannot advance past one and has to stop.
+ */
+ if (item_type == PICA_PROTO_NEWNODE_IPV6 || item_type == PICA_PROTO_NEWNODE_DNS)
{
- switch(buf[size - listleft])
- {
- case PICA_PROTO_NEWNODE_IPV4:
- na_ipv4.magick = PICA_PROTO_NEWNODE_IPV4;
- na_ipv4.addr = *(in_addr_t*)(buf + size - listleft + 1);
- na_ipv4.port = *(in_port_t*)(buf + size - listleft + 5);
- nodelink_attach_nodeaddr(n, na_ipv4.magick, &na_ipv4, sizeof(struct PICA_nodeaddr_ipv4));
+ PICA_warn("NODELIST contains a %s address item, which is not implemented yet, aborting parse",
+ item_type == PICA_PROTO_NEWNODE_IPV6 ? "IPv6" : "DNS");
+ return 0;
+ }
- listleft -= PICA_PROTO_NODELIST_ITEM_IPV4_SIZE;
- break;
- }
- continue;
+ /* Any other type byte is not a list item type at all: its length is
+ * unknown, so parsing cannot safely continue. Without bailing here
+ * the loop would spin forever on a byte it never decrements past.
+ */
+ if (item_type != PICA_PROTO_NEWNODE_IPV4)
+ {
+ PICA_warn("NODELIST contains unknown address type 0x%02X, aborting parse", item_type);
+ return 0;
+ }
+
+ /* Bounds check before reading the 4-byte address and 2-byte port,
+ * so a short trailing item cannot read past the packet or underflow
+ * listleft on the subtraction below.
+ */
+ if (listleft < PICA_PROTO_NODELIST_ITEM_IPV4_SIZE)
+ {
+ PICA_warn("Unexpected end of nodelist packet");
+ return 0;
+ }
+
+ na_ipv4.magick = PICA_PROTO_NEWNODE_IPV4;
+ na_ipv4.addr = *(in_addr_t*)(buf + size - listleft + 1);
+ na_ipv4.port = *(in_port_t*)(buf + size - listleft + 5);
+
+ if (listleft == listsize) //first entry, sender node's address
+ {
+ nodelink_attach_nodeaddr(n, na_ipv4.magick, &na_ipv4, sizeof(struct PICA_nodeaddr_ipv4));
}
else
{
struct PICA_nodeaddr na;
- //char addr[256];
- struct newconn nc;
- struct nodelink *nlp;
- int skip_save = 0;
memset(na.addr, 0, 256);
- memset(&nc, 0, sizeof(struct newconn));
- switch(buf[size - listleft])
+ sprintf(na.addr, "%.16s", inet_ntoa(*(struct in_addr*)&na_ipv4.addr));
+ na.port = ntohs(na_ipv4.port);
+
+ PICA_debug3("NODELIST item %.16s:%hu", inet_ntoa(*(struct in_addr*)&na_ipv4.addr), ntohs(na_ipv4.port));
+
+ if (nodecfg.disable_reserved_addrs && PICA_is_reserved_addr_ipv4(na_ipv4.addr))
{
- case PICA_PROTO_NEWNODE_IPV4:
- na_ipv4.magick = PICA_PROTO_NEWNODE_IPV4;
- na_ipv4.addr = *(in_addr_t*)(buf + size - listleft + 1);
- na_ipv4.port = *(in_port_t*)(buf + size - listleft + 5);
-
- sprintf(na.addr, "%.16s", inet_ntoa(*(struct in_addr*)&na_ipv4.addr));
- na.port = ntohs(na_ipv4.port);
-
- //nodewait_start_connection(&na);
-// if (try_connect_to_node(na.addr,ntohs(na_ipv4.port),&nc))
-// if (try_get_reply(&nc))
-// {
-// nlp=nodelink_list_addnew(&nc);
-// send_newnode(nlp,my_addr);
-// nodelink_attach_nodeaddr(nlp,na_ipv4.magick,&na_ipv4,sizeof(struct PICA_nodeaddr_ipv4));
-// PICA_debug1("LINKED NODE:%s\n",inet_ntoa(*(struct in_addr*)&na_ipv4.addr));
-// }
-
- PICA_debug3("NODELIST item %.16s:%hu", inet_ntoa(*(struct in_addr*)&na_ipv4.addr), ntohs(na_ipv4.port));
-
- if (nodecfg.disable_reserved_addrs && PICA_is_reserved_addr_ipv4(na_ipv4.addr))
- {
- skip_save = 1;
- PICA_debug1("Received NODELIST item %.16s:%hu is in private range, skipping",
- inet_ntoa(*(struct in_addr*)&na_ipv4.addr), ntohs(na_ipv4.port)
- );
- }
- listleft -= PICA_PROTO_NODELIST_ITEM_IPV4_SIZE;
- break;
+ PICA_debug1("Received NODELIST item %.16s:%hu is in private range, skipping",
+ inet_ntoa(*(struct in_addr*)&na_ipv4.addr), ntohs(na_ipv4.port)
+ );
}
- if (!skip_save)
+ else
+ {
PICA_nodeaddr_save(nodecfg.nodes_db_file, &na);//CONF filename
+ }
}
+
+ listleft -= PICA_PROTO_NODELIST_ITEM_IPV4_SIZE;
}
return 1;
@@ -1157,7 +1330,14 @@ unsigned int procmsg_N2NALLOW(unsigned char* buf, unsigned int size, void* ptr)
cc = cclink_list_search(caller_id, callee_id);
- if (cc)
+ /* N2NALLOW is the reply this (caller's) node awaits in N2NCLR_WAITREP.
+ * That state only exists on caller-local links, which always have a
+ * non-NULL p1 - gating on it both prevents a client_wbuf_push(NULL) on a
+ * callee-local (p1 == NULL) link an attacker can create with
+ * N2NCONNREQOUTG, and stops a stray N2NALLOW from disturbing other links.
+ * callee_node == n ties the reply to the node that answered the search.
+ */
+ if (cc && cc->state == PICA_CCLINK_N2NCLR_WAITREP && cc->callee_node == n)
{
if ((mp = client_wbuf_push(cc->p1, PICA_PROTO_FOUND, PICA_PROTO_FOUND_SIZE)))
{
@@ -1182,7 +1362,12 @@ unsigned int procmsg_N2NNOTFOUND(unsigned char* buf, unsigned int size, void* pt
cc = cclink_list_search(caller_id, callee_id);
- if (cc)
+ /* Same gate as N2NALLOW: act only on the caller-local link awaiting this
+ * reply, so a callee-local link (p1 == NULL) cannot crash client_wbuf_push
+ * and a stray N2NNOTFOUND cannot delete an unrelated link. callee_node == n
+ * ties the reply to the node that answered the search.
+ */
+ if (cc && cc->state == PICA_CCLINK_N2NCLR_WAITREP && cc->callee_node == n)
{
if ((mp = client_wbuf_push(cc->p1, PICA_PROTO_NOTFOUND, PICA_PROTO_NOTFOUND_SIZE)))
{
@@ -1215,11 +1400,15 @@ unsigned int procmsg_N2NMSG(unsigned char* buf, unsigned int size, void* ptr)
cc = cclink_list_search(sender_id, receiver_id);
- if (cc && cc->state == PICA_CCLINK_N2NCLE_ACTIVE)
+ /* Accept relayed data only from the node that owns the remote (caller)
+ * half of this link, so a third node cannot inject into or overflow a
+ * relay between two other users' clients.
+ */
+ if (cc && cc->state == PICA_CCLINK_N2NCLE_ACTIVE && cc->caller_node == n)
{
if (datalen + cc->bufpos_p1p2 > cc->buflen_p1p2)
{
- cclink_list_delete(cc);//CLOSING CCLINK DUE TO BUFER OVERRUN --------- FIXME FIX FIX FIX
+ cclink_list_delete(cc);//CLOSING CCLINK DUE TO BUFFER OVERRUN --------- FIXME FIX FIX FIX
return 1;
}
@@ -1230,11 +1419,12 @@ unsigned int procmsg_N2NMSG(unsigned char* buf, unsigned int size, void* ptr)
///////////////
cc = cclink_list_search(receiver_id, sender_id);
- if (cc && cc->state == PICA_CCLINK_N2NCLR_ACTIVE)
+ /* Likewise, only from the node that owns the remote (callee) half. */
+ if (cc && cc->state == PICA_CCLINK_N2NCLR_ACTIVE && cc->callee_node == n)
{
if (datalen + cc->bufpos_p2p1 > cc->buflen_p2p1)
{
- cclink_list_delete(cc);//CLOSING CCLINK DUE TO BUFER OVERRUN --------- FIXME FIX FIX FIX
+ cclink_list_delete(cc);//CLOSING CCLINK DUE TO BUFFER OVERRUN --------- FIXME FIX FIX FIX
return 1;
}
@@ -1285,7 +1475,42 @@ struct cclink* cclink_list_search(const unsigned char *caller_id, const unsigned
void cclink_list_delete(struct cclink *l)
{
PICA_debug1("cclink_list_delete(%p)", (void*)l);
-//удаление из глобального списка
+
+ /* A CONNID connection may still be mid anon-TLS-shutdown with its socket
+ * handed to this link (sck_p1/sck_p2) and nc->iconn.cc pointing here. If
+ * the link is torn down first (WAITACTIVE timeout, or a client
+ * disconnecting), that newconn would later double-close the socket and
+ * re-delete this freed link from newconn_close(). Retire it now: drop its
+ * event and anon SSL and relinquish the fd, which is closed below.
+ */
+ {
+ int i;
+ for (i = 0; i < MAX_NEWCONNS; i++)
+ {
+ struct newconn *nc = &newconns[i];
+
+ if (nc->type == NEWCONN_C2C && nc->iconn.cc == l)
+ {
+ if (nc->ev)
+ {
+ event_del(nc->ev);
+ event_free(nc->ev);
+ nc->ev = NULL;
+ }
+ if (nc->anonssl)
+ {
+ SSL_free(nc->anonssl);
+ nc->anonssl = NULL;
+ }
+ nc->iconn.cc = NULL;
+ nc->sck = -1; /* owned and closed by the link below */
+ nc->pos = 0;
+ nc->type = NEWCONN_UNKNOWN;
+ }
+ }
+ }
+
+//removal from the global list
if (l->prev)
l->prev->next = l->next;
else
@@ -1400,7 +1625,7 @@ void cclink_list_delete_by_nodelink(struct nodelink *node)
//clr - caller
//cle - callee
-// null value of clr or cle means that corresponding client is on remote node, is not local client
+// a NULL value of clr or cle means that the corresponding client is on a remote node, not a local one
struct cclink* cclink_list_add(struct client *clr, struct client *cle)
{
struct cclink *l;
@@ -1415,7 +1640,7 @@ struct cclink* cclink_list_add(struct client *clr, struct client *cle)
l->p1 = clr;
l->p2 = cle;
-//добавление в глобальный список
+//adding to the global list
if (cclink_list_head)
{
cclink_list_end->next = l;
@@ -1525,18 +1750,21 @@ void cclink_setwaitconn(struct cclink *ccl)
int cclink_allocbufs(struct cclink *ccl)
{
- ccl->buf_p1p2 = malloc(DEFAULT_BUF_SIZE);
- ccl->buf_p2p1 = malloc(DEFAULT_BUF_SIZE);
+ ccl->buf_p1p2 = malloc(C2C_BUF_SIZE);
+ ccl->buf_p2p1 = malloc(C2C_BUF_SIZE);
if (!ccl->buf_p1p2 || !ccl->buf_p2p1)
{
if (ccl->buf_p1p2)
free(ccl->buf_p1p2);
if (ccl->buf_p2p1)
free(ccl->buf_p2p1);
+ //cclink_list_delete() frees these again on the way out
+ ccl->buf_p1p2 = NULL;
+ ccl->buf_p2p1 = NULL;
return 0;
}
- ccl->buflen_p1p2 = DEFAULT_BUF_SIZE;
- ccl->buflen_p2p1 = DEFAULT_BUF_SIZE;
+ ccl->buflen_p1p2 = C2C_BUF_SIZE;
+ ccl->buflen_p2p1 = C2C_BUF_SIZE;
return 1;
}
@@ -1697,7 +1925,7 @@ int PICA_node_init()
ret = listen(listen_comm_sck, 20);
if (ret == -1)
- PICA_fatal("unable to listen socket - %s", strerror(errno));
+ PICA_fatal("unable to listen on socket - %s", strerror(errno));
{
int ret_comm;
@@ -1729,17 +1957,62 @@ 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);
+#if OPENSSL_VERSION_NUMBER >= 0x10100000L
+ ctx = SSL_CTX_new(TLS_method());
+ anon_ctx = SSL_CTX_new(TLS_method());
+#else
ctx = SSL_CTX_new(TLSv1_2_method());
anon_ctx = SSL_CTX_new(TLSv1_2_method());
+#endif
if (!ctx || !anon_ctx)
PICA_fatal("unable to create SSL_CTX object");
+#if OPENSSL_VERSION_NUMBER >= 0x10100000L
+ /* TLS_method() on its own would negotiate the highest version both ends
+ * know. Narrowing it back to 1.2 is what keeps this the same protocol
+ * TLSv1_2_method() used to give - see PICA_TLS_MINVERSION for why the
+ * ceiling matters as much as the floor.
+ */
+ if (!SSL_CTX_set_min_proto_version(ctx, PICA_TLS_MINVERSION) ||
+ !SSL_CTX_set_max_proto_version(ctx, PICA_TLS_MAXVERSION) ||
+ !SSL_CTX_set_min_proto_version(anon_ctx, PICA_TLS_MINVERSION) ||
+ !SSL_CTX_set_max_proto_version(anon_ctx, PICA_TLS_MAXVERSION))
+ PICA_fatal("failed to restrict the TLS version range to 1.2");
+#endif
+
ret = SSL_CTX_set_cipher_list(ctx, PICA_TLS_CIPHERLIST);
if (!ret)
@@ -1755,7 +2028,7 @@ int PICA_node_init()
fclose(dh_file);
}
else
- PICA_fatal("Unable to open DH parameters file %", nodecfg.dh_param_file);
+ PICA_fatal("Unable to open DH parameters file %s", nodecfg.dh_param_file);
ret = SSL_CTX_set_tmp_dh(anon_ctx, dh);
@@ -1822,9 +2095,9 @@ void newconns_init()
}
}
-//функция ищет свободный элемент в массиве структур newconn и возвращает на него указатель
-//В случае, если свободный элемент не найден, возвращается элемент с наиболее поздней отметкой времени,
-//при этом соединение закрывается
+//finds a free element in the newconn structure array and returns a pointer to it
+//If no free element is found, the element with the latest timestamp is returned,
+//and its connection is closed
struct newconn* newconn_add(struct newconn *ncs, int *pos)
{
time_t min;
@@ -1859,6 +2132,11 @@ ret:
ncs[*pos].sck = -1;
ncs[*pos].pos = 0;
ncs[*pos].tmst = time(0);
+ //a slot handed over by newconn_free() still carries the type of its
+ //previous connection, which the request handlers would take for a second
+ //request on this one
+ ncs[*pos].type = NEWCONN_UNKNOWN;
+ ncs[*pos].iconn.cl = NULL;
return &ncs[(*pos)++];
}
@@ -1977,7 +2255,7 @@ void client_attach_multi_secondary(struct client *primary, struct client *second
void client_tree_remove(struct client* ci)
{
- struct client** p_link;//указывает на left или right в родительском узле
+ struct client** p_link;//points to left or right in the parent node
if (!ci->up)
{
@@ -2016,14 +2294,19 @@ void client_tree_remove(struct client* ci)
{
struct client *lm;
lm = ci->right;
- while(lm->left) lm = lm->left; //поиск самого левого узла правого поддерева
+ while(lm->left) lm = lm->left; //find the leftmost node of the right subtree
if (lm != ci->right)
{
lm->up->left = lm->right;
+ if (lm->right)
+ lm->right->up = lm->up; // FIX 1
lm->right = ci->right;
+ ci->right->up = lm; // FIX 2
}
lm->left = ci->left;
+ if (ci->left)
+ ci->left->up = lm; // FIX 3
lm->up = ci->up;
*p_link = lm;
}
@@ -2037,6 +2320,14 @@ struct client *client_list_addnew(struct newconn *nc)
if (!ci) return 0;
+ ci->r_buf = calloc(1, DEFAULT_BUF_SIZE);
+ if (!ci->r_buf)
+ {
+ free(ci);
+ return 0;
+ }
+ ci->buflen_r = DEFAULT_BUF_SIZE;
+
ci->ssl_comm = SSL_new (ctx);
if (!ci->ssl_comm)
@@ -2063,16 +2354,11 @@ struct client *client_list_addnew(struct newconn *nc)
SSL_set_fd(ci->ssl_comm, ci->sck_comm);
- ci->r_buf = calloc(1, DEFAULT_BUF_SIZE);
- if (!ci->r_buf)
- {
- free(ci);
- return 0;
- }
- ci->buflen_r = DEFAULT_BUF_SIZE;
-
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);
@@ -2119,7 +2405,7 @@ void client_list_delete(struct client* ci)
PICA_debug1("client_list_delete(%p)", (void*)ci);
SSL_free(ci->ssl_comm);
-//удаление из списка
+//removal from the list
if (ci->prev)
ci->prev->next = ci->next;
else
@@ -2130,7 +2416,7 @@ void client_list_delete(struct client* ci)
else
client_list_end = ci->prev;
-//удаление из дерева
+//removal from the tree
s = client_tree_search(ci->id);
if (s == ci)
@@ -2157,6 +2443,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);
@@ -2242,6 +2530,14 @@ struct nodelink *nodelink_list_addnew(struct newconn *nc)
if (!nl)
return 0;
+ nl->r_buf = calloc(1, DEFAULT_BUF_SIZE);
+ if (!nl->r_buf)
+ {
+ free(nl);
+ return 0;
+ }
+ nl->buflen_r = DEFAULT_BUF_SIZE;
+
if (nodelink_list_end)
{
nodelink_list_end->next = nl;
@@ -2259,14 +2555,6 @@ struct nodelink *nodelink_list_addnew(struct newconn *nc)
nl->anonssl = nc->anonssl;
nc->anonssl = NULL;
- nl->r_buf = calloc(1, DEFAULT_BUF_SIZE);
- if (!nl->r_buf)
- {
- free(nl);
- return 0;
- }
- nl->buflen_r = DEFAULT_BUF_SIZE;
-
nl->tmst = time(0);
nl->ev_read = event_new(pica_event_base, nl->sck, EV_READ | EV_PERSIST, process_n2n_read, nl);
@@ -2490,7 +2778,7 @@ void process_timeouts_c2n()
}
else
{
- //2038 year has come, and this code is still working somewhere with 32bit Unix timestamps :)
+ //the year 2038 has come, and this code is still working somewhere with 32bit Unix timestamps :)
if ((i_ptr->tmst - cur_time ) > NOREPLY_TIMEOUT)
kill_ptr = i_ptr;
}
@@ -2521,7 +2809,7 @@ void process_timeouts_c2n()
if (kill_ptr)
{
- PICA_info("Disconnecting user %s due to noreply timeout", PICA_id_to_base64(kill_ptr -> id, NULL));
+ PICA_info("Disconnecting user %s due to noreply timeout", PICA_id_to_base64(kill_ptr->id, NULL));
client_list_delete(kill_ptr);
kill_ptr = 0;
}
@@ -2549,7 +2837,7 @@ void process_timeouts_n2n()
}
else
{
- //2038 year has come, and this code is still working somewhere with 32bit Unix timestamps :)
+ //the year 2038 has come, and this code is still working somewhere with 32bit Unix timestamps :)
if ((i_ptr->tmst - cur_time ) > NOREPLY_TIMEOUT)
kill_ptr = i_ptr;
}
@@ -2596,17 +2884,17 @@ int process_timeouts()
if (time(0) - skynet_refresh_tmst > SKYNET_REFRESH_TIMEOUT && !nodewait_list)
{
- int clean_treshold = 100 - nodelink_list_count - cclink_list_count;
+ int clean_threshold = 100 - nodelink_list_count - cclink_list_count;
- if (clean_treshold <= 0)
- clean_treshold = 3;
+ if (clean_threshold <= 0)
+ clean_threshold = 3;
PICA_debug3("refreshing nodelist database");
if (nodelink_list_count > 0)
{
- PICA_debug3("trying to clean nodes unreachable after more than %i attempts", clean_treshold);
- PICA_nodeaddr_cleanold(nodecfg.nodes_db_file, clean_treshold);
+ PICA_debug3("trying to clean nodes unreachable after more than %i attempts", clean_threshold);
+ PICA_nodeaddr_cleanold(nodecfg.nodes_db_file, clean_threshold);
}
PICA_node_joinskynet(nodecfg.nodes_db_file, nodecfg.announced_addr);
@@ -2659,6 +2947,10 @@ void process_listen_read(evutil_socket_t listener, short event, void *arg)
nc->state = PICA_NEWCONN_WAITANONTLSACCEPT;
IOCTLSETNONBLOCKINGSOCKET(s, 1);
+ /* the node relays c2c data, so Nagle delays here are added to
+ * the delay of every message between the two clients
+ */
+ SETTCPNODELAY(s);
PICA_debug1("accepted connection from %.16s, new socket %i", inet_ntoa(addr.sin_addr), s);//IPv6
@@ -2685,6 +2977,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;
@@ -3025,7 +3399,7 @@ void process_c2n_write(evutil_socket_t s, short event, void *arg)
if (ret == i_ptr->btw_ssl)
{
if (ret < i_ptr->w_pos)
- memmove(i_ptr->w_buf, i_ptr->w_buf + ret, i_ptr->w_pos - ret); //may be it's better to implement ring buffer here
+ memmove(i_ptr->w_buf, i_ptr->w_buf + ret, i_ptr->w_pos - ret); //maybe it's better to implement a ring buffer here
i_ptr->w_pos -= i_ptr->btw_ssl;
i_ptr->btw_ssl = 0;
@@ -3074,7 +3448,7 @@ void process_c2n_write(evutil_socket_t s, short event, void *arg)
}
}
if (ret == 1)
- //проверить сертификат пользователя
+ //check the user's certificate
{
X509* client_cert;
struct client *primary;
@@ -3084,7 +3458,7 @@ void process_c2n_write(evutil_socket_t s, short event, void *arg)
client_cert = SSL_get_peer_certificate (i_ptr->ssl_comm);
if (!client_cert)
{
- //ERR_CHECK //нет сертификата
+ //ERR_CHECK //no certificate
kill_ptr = i_ptr;
}
@@ -3125,7 +3499,7 @@ void process_c2n_write(evutil_socket_t s, short event, void *arg)
}
if (!ret)
{
- //ERR_CHECK //не уникальный id
+ //ERR_CHECK //id is not unique
kill_ptr = i_ptr;
}
}
@@ -3153,6 +3527,8 @@ void process_c2c_write(evutil_socket_t s, short event, void *arg)
if (cc->bufpos_p1p2)
{
+ int attempted = 1;
+
if (cc->state == PICA_CCLINK_N2NCLR_ACTIVE) //receiver is on remote node
{
int sendlen;
@@ -3171,53 +3547,64 @@ void process_c2c_write(evutil_socket_t s, short event, void *arg)
else
ret = 0;
}
- else if (s == cc->sck_p2)
+ else if (cc->sck_p2 != -1)
ret = send(cc->sck_p2, cc->buf_p1p2, cc->bufpos_p1p2, MSG_NOSIGNAL);
+ else
+ attempted = 0; //no local p2 socket to flush into, leave the buffer untouched
- PICA_debug3("process_c2c_write: send p1p2: ret=%i", ret);
- if (ret > 0)
+ //ret is only meaningful when one of the branches above ran - this
+ //callback is shared by ev_write_p1/ev_write_p2 and is also invoked
+ //directly with s == -1, so it is routinely entered for a direction
+ //that cannot be flushed on this pass
+ if (attempted)
{
- if (ret < cc->bufpos_p1p2)
- memmove(cc->buf_p1p2, cc->buf_p1p2 + ret, cc->bufpos_p1p2 - ret); //may be it's better to implement ring buffer here
- cc->bufpos_p1p2 -= ret;
+ PICA_debug3("process_c2c_write: send p1p2: ret=%i", ret);
+ if (ret > 0)
+ {
+ if (ret < cc->bufpos_p1p2)
+ memmove(cc->buf_p1p2, cc->buf_p1p2 + ret, cc->bufpos_p1p2 - ret); //maybe it's better to implement a ring buffer here
+ cc->bufpos_p1p2 -= ret;
- if (cc->bufpos_p1p2 == 0 && cc->sck_p1 != -1)
- event_add(cc->ev_read_p1, NULL);
+ if (cc->bufpos_p1p2 == 0 && cc->sck_p1 != -1)
+ event_add(cc->ev_read_p1, NULL);
- cc->jam_p1p2 = 0;
- }
+ cc->jam_p1p2 = 0;
+ }
- if (ret == 0)
- {
- kill_ptr = cc;
- }
- if (ret == -1)
- {
-#ifdef WIN32
- ret = WSAGetLastError();
- if (ret == WSAEWOULDBLOCK || ret == WSAENOBUFS)
- cc->jam_p1p2 = 1;
- else
+ if (ret == 0)
{
kill_ptr = cc;
}
-#else
- ret = errno;
- if (ret == EAGAIN || ret == ENOBUFS)
- cc->jam_p1p2 = 1;
- else
+ if (ret == -1)
{
- kill_ptr = cc;
- }
+#ifdef WIN32
+ ret = WSAGetLastError();
+ if (ret == WSAEWOULDBLOCK || ret == WSAENOBUFS)
+ cc->jam_p1p2 = 1;
+ else
+ {
+ kill_ptr = cc;
+ }
+#else
+ ret = errno;
+ if (ret == EAGAIN || ret == ENOBUFS)
+ cc->jam_p1p2 = 1;
+ else
+ {
+ kill_ptr = cc;
+ }
#endif
+ }
}
- if (!kill_ptr && cc->bufpos_p1p2 && s != -1)
+ if (!kill_ptr && cc->bufpos_p1p2 && cc->ev_write_p2)
event_add(cc->ev_write_p2, NULL);
}
if (cc->bufpos_p2p1 /*&& (cc->state==PICA_CCLINK_LOCAL_ACTIVE || cc->state==PICA_CCLINK_N2NCLR_ACTIVE)*/)
{
+ int attempted = 1;
+
if (cc->state == PICA_CCLINK_N2NCLE_ACTIVE) //receiver is on remote node
{
int sendlen;
@@ -3236,49 +3623,54 @@ void process_c2c_write(evutil_socket_t s, short event, void *arg)
else
ret = 0;
}
- else if (s == cc->sck_p1)
+ else if (cc->sck_p1 != -1)
ret = send(cc->sck_p1, cc->buf_p2p1, cc->bufpos_p2p1, MSG_NOSIGNAL);
+ else
+ attempted = 0; //no local p1 socket to flush into, leave the buffer untouched
- PICA_debug3("process_c2c_write: send p2p1: ret=%i", ret);
- if (ret > 0)
+ if (attempted)
{
- if (ret < cc->bufpos_p2p1)
- memmove(cc->buf_p2p1, cc->buf_p2p1 + ret, cc->bufpos_p2p1 - ret); //may be it's better to implement ring buffer here
- cc->bufpos_p2p1 -= ret;
+ PICA_debug3("process_c2c_write: send p2p1: ret=%i", ret);
+ if (ret > 0)
+ {
+ if (ret < cc->bufpos_p2p1)
+ memmove(cc->buf_p2p1, cc->buf_p2p1 + ret, cc->bufpos_p2p1 - ret); //maybe it's better to implement a ring buffer here
+ cc->bufpos_p2p1 -= ret;
- if (cc->bufpos_p2p1 == 0 && cc->sck_p2 != -1)
- event_add(cc->ev_read_p2, NULL);
+ if (cc->bufpos_p2p1 == 0 && cc->sck_p2 != -1)
+ event_add(cc->ev_read_p2, NULL);
- cc->jam_p2p1 = 0;
- }
+ cc->jam_p2p1 = 0;
+ }
- if (ret == 0)
- {
- kill_ptr = cc;
- }
- if (ret == -1)
- {
-#ifdef WIN32
- ret = WSAGetLastError();
- if (ret == WSAEWOULDBLOCK || ret == WSAENOBUFS)
- cc->jam_p2p1 = 1;
- else
+ if (ret == 0)
{
kill_ptr = cc;
}
-#else
- ret = errno;
- if (ret == EAGAIN || ret == ENOBUFS)
- cc->jam_p2p1 = 1;
- else
+ if (ret == -1)
{
- kill_ptr = cc;
- }
+#ifdef WIN32
+ ret = WSAGetLastError();
+ if (ret == WSAEWOULDBLOCK || ret == WSAENOBUFS)
+ cc->jam_p2p1 = 1;
+ else
+ {
+ kill_ptr = cc;
+ }
+#else
+ ret = errno;
+ if (ret == EAGAIN || ret == ENOBUFS)
+ cc->jam_p2p1 = 1;
+ else
+ {
+ kill_ptr = cc;
+ }
#endif
+ }
}
- if (!kill_ptr && cc->bufpos_p2p1 && s != -1)
+ if (!kill_ptr && cc->bufpos_p2p1 && cc->ev_write_p1)
event_add(cc->ev_write_p1, NULL);
}
@@ -3307,7 +3699,7 @@ void process_n2n_write(evutil_socket_t s, short event, void *arg)
if (ret == nl->btw_ssl)
{
if (ret < nl->w_pos)
- memmove(nl->w_buf, nl->w_buf + ret, nl->w_pos - ret); //may be it's better to implement ring buffer here
+ memmove(nl->w_buf, nl->w_buf + ret, nl->w_pos - ret); //maybe it's better to implement a ring buffer here
nl->w_pos -= nl->btw_ssl;
nl->btw_ssl = 0;
}
@@ -3369,7 +3761,7 @@ void process_nodewait()
nodewait_start_connect(nw);
else
{
- PICA_debug3("skipping node %.255s %u , connection already exist", nw->addr.addr, nw->addr.port);
+ PICA_debug3("skipping node %.255s %u, connection already exists", nw->addr.addr, nw->addr.port);
kill_ptr = nw;
}
}
@@ -3451,7 +3843,7 @@ Options:\n\
void print_version()
{
- puts("Pica Pica Node v"PACKAGE_VERSION" \nCopyright (c) 2012 - 2018 Anton Sviridenko\n");
+ puts("Pica Pica Node v"PACKAGE_VERSION" \nCopyright (c) 2012 - 2026 Anton Sviridenko\n");
puts("protocol version "PICA_PROTO_VER_STRING"\n");
puts("License GPLv3: GNU GPL version 3 <http://gnu.org/licenses/gpl.html>\n\
This is free software: you are free to change and redistribute it.\n\
@@ -3612,6 +4004,88 @@ void ReportSvcStatus( DWORD dwCurrentState,
}
#endif
+#ifdef HAVE_LIBMINIUPNPC
+/* The port mapping made for the node's listening port, kept for as long as
+ * the node runs so that its lease can be renewed.
+ */
+static struct PICA_upnp_ctx *upnp_ctx;
+static struct event *upnp_refresh_event;
+
+static void upnp_schedule_refresh(void);
+
+static void upnp_refresh_cb(evutil_socket_t fd, short event, void *arg)
+{
+ int ret;
+
+ /* Blocks the event loop for as long as the IGD takes to answer, normally
+ * a few milliseconds, once in half a lease.
+ */
+ ret = PICA_upnp_refresh(upnp_ctx);
+
+ if (ret != PICA_UPNP_OK)
+ {
+ PICA_error("UPnP: failed to renew the port mapping, the node may become unreachable from the Internet - %s",
+ PICA_upnp_strerror(ret));
+ return;
+ }
+
+ upnp_schedule_refresh();
+}
+
+static void upnp_schedule_refresh(void)
+{
+ struct timeval tv;
+ unsigned int lease = PICA_upnp_lease(upnp_ctx);
+
+ /* a permanent mapping still disappears when the IGD reboots */
+ if (lease == 0)
+ lease = PICA_UPNP_LEASE_DEFAULT;
+
+ tv.tv_sec = lease / 2;
+ tv.tv_usec = 0;
+
+ if (!upnp_refresh_event)
+ upnp_refresh_event = evtimer_new(pica_event_base, upnp_refresh_cb, NULL);
+
+ if (upnp_refresh_event)
+ evtimer_add(upnp_refresh_event, &tv);
+}
+
+/* Forwards the listening port, TCP and UDP - the latter carries the "get my
+ * UDP address" datagrams - and reports the IGD's external address.
+ * \return 1 on success
+ */
+static int upnp_forward_listen_port(int port, char *public_ip, size_t public_ip_size)
+{
+ int ret;
+
+ ret = PICA_upnp_discover(PICA_UPNP_DISCOVER_TIMEOUT, &upnp_ctx);
+
+ if (ret != PICA_UPNP_OK)
+ {
+ PICA_info("UPnP: %s", PICA_upnp_strerror(ret));
+ return 0;
+ }
+
+ ret = PICA_upnp_external_addr(upnp_ctx, public_ip, public_ip_size);
+
+ if (ret == PICA_UPNP_OK)
+ ret = PICA_upnp_map(upnp_ctx, port, PICA_UPNP_LEASE_DEFAULT, "Pica Pica node");
+
+ if (ret != PICA_UPNP_OK)
+ {
+ PICA_info("UPnP: failed to forward port %i - %s", port, PICA_upnp_strerror(ret));
+ PICA_upnp_free(upnp_ctx);
+ upnp_ctx = NULL;
+ return 0;
+ }
+
+ upnp_schedule_refresh();
+
+ return 1;
+}
+#endif
+
int pica_node_main(int argc, char** argv)
{
uint32_t pid;
@@ -3629,7 +4103,7 @@ int pica_node_main(int argc, char** argv)
if (!PICA_nodeconfig_load(nodecfg.config_file))
{
- PICA_fatal("Unable to load configuration. Please check if config file %s exists and has correct permissions", nodecfg.config_file);
+ PICA_fatal("Unable to load configuration. Please check if the config file %s exists and has the correct permissions", nodecfg.config_file);
}
PICA_debug1("nodecfg.config_file = %s", nodecfg.config_file);
@@ -3671,12 +4145,10 @@ int pica_node_main(int argc, char** argv)
if (PICA_is_reserved_addr_ipv4(guess) && nodecfg.disable_reserved_addrs)
{
char public_ip[64];
- int ret;
PICA_info("trying to get global IP with UPnP...");
- ret = PICA_upnp_autoconfigure_ipv4(atoi(nodecfg.listen_port), atoi(nodecfg.listen_port), public_ip);
- if (ret)
+ if (upnp_forward_listen_port(atoi(nodecfg.listen_port), public_ip, sizeof(public_ip)))
{
free(nodecfg.announced_addr);
nodecfg.announced_addr = strdup(public_ip);
@@ -3688,19 +4160,19 @@ int pica_node_main(int argc, char** argv)
if (INADDR_NONE == inet_addr(nodecfg.announced_addr) || INADDR_ANY == inet_addr(nodecfg.announced_addr))
{
- PICA_fatal("announced_addr (%.16s) is invalid or not configured. Please set correct public IP address of your pica-node instance in config file.",
+ PICA_fatal("announced_addr (%.16s) is invalid or not configured. Please set the correct public IP address of your pica-node instance in the config file.",
nodecfg.announced_addr
);
}
if (nodecfg.disable_reserved_addrs && PICA_is_reserved_addr_ipv4(inet_addr(nodecfg.announced_addr)))
{
- PICA_fatal("announced_addr (%.16s) is in private range and is unacceptable for global Internet. Private ranges are disabled by node configuration.",
+ PICA_fatal("announced_addr (%.16s) is in a private range and is unacceptable for the global Internet. Private ranges are disabled by node configuration.",
nodecfg.announced_addr
);
}
- PICA_node_joinskynet(nodecfg.nodes_db_file, nodecfg.announced_addr);//CONF-CONF имя файла с адресами узлов, свой адрес
+ PICA_node_joinskynet(nodecfg.nodes_db_file, nodecfg.announced_addr);//CONF-CONF node addresses file name, own address
my_addr = nodecfg.announced_addr;//TEMP FIXME