chiark / gitweb /
comm, udp: Provide an addr_to_string method
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 17 Jul 2011 23:59:50 +0000 (00:59 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Wed, 17 Aug 2011 22:14:54 +0000 (23:14 +0100)
This method writes (into a single static buffer) a string describing a
comm_addr.  It describes both the comm instance and the peer address.

No callers yet, but one is about to be introduced.

Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
secnet.h
udp.c

index 809b3a75eb608af6c25cfbe8694aa1cd05fe5051..fbb7660da685e8721aac0a56a4e017845ffd69f5 100644 (file)
--- a/secnet.h
+++ b/secnet.h
@@ -325,6 +325,9 @@ typedef void comm_release_notify_fn(void *commst, void *nst,
                                    comm_notify_fn *fn);
 typedef bool_t comm_sendmsg_fn(void *commst, struct buffer_if *buf,
                               const struct comm_addr *dest);
+typedef const char *comm_addr_to_string_fn(void *commst,
+                                          const struct comm_addr *ca);
+        /* Returned string is in a static buffer. */
 struct comm_if {
     void *st;
     int32_t min_start_pad;
@@ -332,6 +335,7 @@ struct comm_if {
     comm_request_notify_fn *request_notify;
     comm_release_notify_fn *release_notify;
     comm_sendmsg_fn *sendmsg;
+    comm_addr_to_string_fn *addr_to_string;
 };
 
 /* LOG interface */
diff --git a/udp.c b/udp.c
index 8ec8581f6c28aa1eaa89f002f055feb5f7167427..f4206642fc15f44f1de6606bd970c6e2449797f4 100644 (file)
--- a/udp.c
+++ b/udp.c
@@ -16,6 +16,8 @@
 #include <errno.h>
 #include <sys/socket.h>
 #include <sys/wait.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
 #include "util.h"
 #include "unaligned.h"
 #include "ipaddr.h"
@@ -46,6 +48,30 @@ struct udp {
     struct sockaddr_in proxy;
 };
 
+static const char *saddr_to_string(const struct sockaddr_in *sin) {
+    static char bufs[2][100];
+    static int b;
+
+    b ^= 1;
+    snprintf(bufs[b], sizeof(bufs[b]), "[%s]:%d",
+            inet_ntoa(sin->sin_addr),
+            ntohs(sin->sin_port));
+    return bufs[b];
+}
+
+static const char *addr_to_string(void *commst, const struct comm_addr *ca) {
+    struct udp *st=commst;
+    static char sbuf[100];
+
+    struct sockaddr_in la;
+    la.sin_addr.s_addr=htonl(st->addr);
+    la.sin_port=htons(st->port);
+
+    snprintf(sbuf, sizeof(sbuf), "udp:%s-%s",
+            saddr_to_string(&la), saddr_to_string(&ca->sin));
+    return sbuf;
+}
+
 static int udp_beforepoll(void *state, struct pollfd *fds, int *nfds_io,
                          int *timeout_io)
 {
@@ -267,6 +293,7 @@ static list_t *udp_apply(closure_t *self, struct cloc loc, dict_t *context,
     st->ops.request_notify=request_notify;
     st->ops.release_notify=release_notify;
     st->ops.sendmsg=udp_sendmsg;
+    st->ops.addr_to_string=addr_to_string;
     st->port=0;
     st->use_proxy=False;