[PATCH 22/41] udp, util: Break out send_nak function
Ian Jackson
ijackson at chiark.greenend.org.uk
Thu Jul 25 18:40:48 BST 2013
Move the code in udp.c which constructs NAKs into its own function
in util.c. This will make it easier to reuse.
Signed-off-by: Ian Jackson <ijackson at chiark.greenend.org.uk>
---
udp.c | 20 +++++---------------
util.c | 17 +++++++++++++++++
util.h | 4 ++++
3 files changed, 26 insertions(+), 15 deletions(-)
diff --git a/udp.c b/udp.c
index c83e618..483ed37 100644
--- a/udp.c
+++ b/udp.c
@@ -121,12 +121,12 @@ 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;
@@ -141,17 +141,7 @@ static void udp_afterpoll(void *state, struct pollfd *fds, int nfds)
/* 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, peer %s):"
- " %08"PRIx32"<-%08"PRIx32": %08"PRIx32":"
- " unwanted/incorrect, sending NAK\n",
- st->port, saddr_to_string(&from),
- dest, source, msgtype);
- buffer_init(st->rbuf,0);
- buf_append_uint32(st->rbuf,dest);
- buf_append_uint32(st->rbuf,source);
- buf_append_uint32(st->rbuf,LABEL_NAK);
- sendto(st->fd, st->rbuf->start, st->rbuf->size, 0,
- (struct sockaddr *)&from, sizeof(from));
+ send_nak(&ca,source,dest,msgtype,st->rbuf,"unwanted");
}
BUF_FREE(st->rbuf);
}
diff --git a/util.c b/util.c
index 3d11987..1b46bc0 100644
--- a/util.c
+++ b/util.c
@@ -38,6 +38,7 @@
#include <sys/wait.h>
#include "util.h"
#include "unaligned.h"
+#include "magic.h"
#define MIN_BUFFER_SIZE 64
#define DEFAULT_BUFFER_SIZE 4096
@@ -381,6 +382,22 @@ static list_t *buffer_apply(closure_t *self, struct cloc loc, dict_t *context,
return new_closure(&st->cl);
}
+void send_nak(const struct comm_addr *dest, uint32_t our_index,
+ uint32_t their_index, uint32_t msgtype,
+ struct buffer_if *buf, const char *logwhy)
+{
+ buffer_init(buf,dest->comm->min_start_pad);
+ buf_append_uint32(buf,their_index);
+ buf_append_uint32(buf,our_index);
+ buf_append_uint32(buf,LABEL_NAK);
+ if (logwhy)
+ Message(M_INFO,"%s: %08"PRIx32"<-%08"PRIx32": %08"PRIx32":"
+ " %s; sending NAK\n",
+ dest->comm->addr_to_string(dest->comm->st,dest),
+ our_index, their_index, msgtype, logwhy);
+ dest->comm->sendmsg(dest->comm->st, buf, dest);
+}
+
int consttime_memeq(const void *s1in, const void *s2in, size_t n)
{
const uint8_t *s1=s1in, *s2=s2in;
diff --git a/util.h b/util.h
index cbe9f52..816c056 100644
--- a/util.h
+++ b/util.h
@@ -45,6 +45,10 @@ extern int32_t write_mpbin(MP_INT *a, uint8_t *buffer, int32_t buflen);
extern struct log_if *init_log(list_t *loglist);
+extern void send_nak(const struct comm_addr *dest, uint32_t our_index,
+ uint32_t their_index, uint32_t msgtype,
+ struct buffer_if *buf, const char *logwhy);
+
extern int consttime_memeq(const void *s1, const void *s2, size_t n);
#endif /* util_h */
--
1.7.2.5
More information about the sgo-software-discuss
mailing list