[PATCH 10/18] comm, udp: Provide an addr_to_string method
Ian Jackson
ijackson at chiark.greenend.org.uk
Wed Aug 17 23:54:28 BST 2011
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 at chiark.greenend.org.uk>
---
secnet.h | 4 ++++
udp.c | 27 +++++++++++++++++++++++++++
2 files changed, 31 insertions(+), 0 deletions(-)
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 <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;
--
1.7.2.5
More information about the sgo-software-discuss
mailing list