chiark / gitweb /
site: dynamically create and destroy transform instances
[secnet.git] / udp.c
diff --git a/udp.c b/udp.c
index bbf8c64..483ed37 100644 (file)
--- a/udp.c
+++ b/udp.c
@@ -21,6 +21,7 @@
 #include "util.h"
 #include "unaligned.h"
 #include "ipaddr.h"
+#include "magic.h"
 
 static beforepoll_fn udp_beforepoll;
 static afterpoll_fn udp_afterpoll;
@@ -120,29 +121,28 @@ static void udp_afterpoll(void *state, struct pollfd *fds, int nfds)
                    buf_unprepend(st->rbuf,2);
                    memcpy(&from.sin_port,buf_unprepend(st->rbuf,2),2);
                }
+               struct comm_addr ca;
+               FILLZERO(ca);
+               ca.comm=&st->ops;
+               ca.sin=from;
                done=False;
                for (n=st->notify; n; n=n->next) {
-                   struct comm_addr ca;
-                   FILLZERO(ca);
-                   ca.comm=&st->ops;
-                   ca.sin=from;
                    if (n->fn(n->state, st->rbuf, &ca)) {
                        done=True;
                        break;
                    }
                }
                if (!done) {
-                   uint32_t source,dest;
-                   /* Manufacture and send NAK packet */
-                   source=get_uint32(st->rbuf->start); /* Us */
-                   dest=get_uint32(st->rbuf->start+4); /* Them */
-                   Message(M_INFO,"udp (port %d): sending NAK\n",st->port);
-                   buffer_init(st->rbuf,0);
-                   buf_append_uint32(st->rbuf,dest);
-                   buf_append_uint32(st->rbuf,source);
-                   buf_append_uint32(st->rbuf,0); /* NAK is msg type 0 */
-                   sendto(st->fd, st->rbuf->start, st->rbuf->size, 0,
-                          (struct sockaddr *)&from, sizeof(from));
+                   uint32_t msgtype;
+                   if (st->rbuf->size>12 /* prevents traffic amplification */
+                       && ((msgtype=get_uint32(st->rbuf->start+8))
+                           != LABEL_NAK)) {
+                       uint32_t source,dest;
+                       /* Manufacture and send NAK packet */
+                       source=get_uint32(st->rbuf->start); /* Us */
+                       dest=get_uint32(st->rbuf->start+4); /* Them */
+                       send_nak(&ca,source,dest,msgtype,st->rbuf,"unwanted");
+                   }
                    BUF_FREE(st->rbuf);
                }
                BUF_ASSERT_FREE(st->rbuf);