aboutsummaryrefslogtreecommitdiff
path: root/muswd.c
diff options
context:
space:
mode:
authorrodri <rgl@antares-labs.eu>2023-02-16 13:35:24 +0000
committerrodri <rgl@antares-labs.eu>2023-02-16 13:35:24 +0000
commitfd5dc301e4a69d7b7c1293aafe5b069b4ff400a4 (patch)
treef9d51305517fe6a5bd0167522be82a0dcc28b38c /muswd.c
parent62e75d8830eb56ab03bd4689d51ffd6d4150f461 (diff)
downloadmusw-fd5dc301e4a69d7b7c1293aafe5b069b4ff400a4.tar.gz
musw-fd5dc301e4a69d7b7c1293aafe5b069b4ff400a4.tar.bz2
musw-fd5dc301e4a69d7b7c1293aafe5b069b4ff400a4.zip
implemented a keep alive mechanism.
also changed the newframe fn to take a Udphdr* instead of a Frame*. the verifyframe fn now returns 1 if correct 0 otherwise.
Diffstat (limited to 'muswd.c')
-rw-r--r--muswd.c35
1 files changed, 32 insertions, 3 deletions
diff --git a/muswd.c b/muswd.c
index d2f1f01..c85b5a0 100644
--- a/muswd.c
+++ b/muswd.c
@@ -58,6 +58,32 @@ popconn(NetConn *nc)
}
void
+nudgeconns(ulong curts)
+{
+ NetConn **ncp, **ncpe;
+ Frame *f;
+ ulong elapsed, elapsednudge;
+
+ ncpe = conns+nconns;
+
+ for(ncp = conns; ncp < ncpe; ncp++){
+ elapsed = curts - (*ncp)->lastrecvts;
+ elapsednudge = curts - (*ncp)->lastnudgets;
+
+ if((*ncp)->state == NCSConnected && elapsed > ConnTimeout){
+ popconn(*ncp);
+ delnetconn(*ncp);
+ }else if((*ncp)->state == NCSConnected && elapsednudge > 1000){ /* every second */
+ f = newframe(&(*ncp)->udp, NSnudge, 0, 0, 0, nil);
+ signframe(f, (*ncp)->dh.priv);
+ sendp(egress, f);
+
+ (*ncp)->lastnudgets = curts;
+ }
+ }
+}
+
+void
threadnetrecv(void *arg)
{
uchar buf[MTU];
@@ -105,7 +131,7 @@ threadnetppu(void *)
nc = newnetconn(NCSConnecting, &frame->udp);
putconn(nc);
- newf = newframe(frame, NShi, 0, 0, 2*sizeof(ulong), nil);
+ newf = newframe(&frame->udp, NShi, frame->seq+1, frame->seq, 2*sizeof(ulong), nil);
dhgenpg(&nc->dh.p, &nc->dh.g);
pack(newf->data, newf->len, "kk", nc->dh.p, nc->dh.g);
@@ -117,6 +143,8 @@ threadnetppu(void *)
goto discard;
}
+ nc->lastrecvts = nanosec()/1e6;
+
switch(nc->state){
case NCSConnecting:
switch(frame->type){
@@ -127,7 +155,7 @@ threadnetppu(void *)
if(debug)
fprint(2, "\trcvd pubkey %ld\n", nc->dh.pub);
- newf = newframe(frame, NSdhx, 0, 0, sizeof(ulong), nil);
+ newf = newframe(&frame->udp, NSdhx, frame->seq+1, frame->seq, sizeof(ulong), nil);
nc->dh.sec = truerand();
nc->dh.priv = dhgenkey(nc->dh.pub, nc->dh.sec, nc->dh.p);
@@ -141,7 +169,7 @@ threadnetppu(void *)
}
break;
case NCSConnected:
- if(verifyframe(frame, nc->dh.priv) != 0){
+ if(!verifyframe(frame, nc->dh.priv)){
if(debug)
fprint(2, "\tbad signature\n");
goto discard;
@@ -250,6 +278,7 @@ threadsim(void *)
}
broadcaststate();
+ nudgeconns(now/1e6);
iosleep(io, HZ2MS(70));
}