From 5edf478f9f729427bb4d979d15b21db049222d68 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Mon, 18 Jul 2011 00:59:50 +0100 Subject: [PATCH] comm, udp: Provide an addr_to_string method 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 --- secnet.h | 4 ++++ udp.c | 27 +++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/secnet.h b/secnet.h index 809b3a7..fbb7660 100644 --- 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 8ec8581..f420664 100644 --- a/udp.c +++ b/udp.c @@ -16,6 +16,8 @@ #include #include #include +#include +#include #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; -- 2.30.2