stun

simple point to point tunnel
git clone git://git.2f30.org/stun.git
Log | Files | Refs | README

commit 7bb7eb1cb88b03e12fbedd1e123b329fbc322d2b
parent 9c0bf8b3a3f98553a4a889204d6273cf2e9deb01
Author: sin <sin@2f30.org>
Date:   Wed Apr 13 12:44:22 +0100

add ntop conversions

Diffstat:
stun.c | 6++++--
stun.h | 4++++
util.c | 38++++++++++++++++++++++++++++++++++++++
3 files changed, 46 insertions(+), 2 deletions(-)
diff --git a/stun.c b/stun.c @@ -149,8 +149,9 @@ main(int argc, char *argv[]) netreset(); continue; } - logdbg("new client connected: %d", netfd); + logdbg("client %s is ready", peer_ntop(netfd)); tunnel(netfd, devfd); + logdbg("client %s disconnected", peer_ntop(netfd)); close(netfd); netreset(); } @@ -170,8 +171,9 @@ main(int argc, char *argv[]) sleep(RECONNECTTIMEO); continue; } - logdbg("connected to %s:%s", host, port); + logdbg("connected to %s", peer_ntop(netfd)); tunnel(netfd, devfd); + logdbg("disconnected from %s", peer_ntop(netfd)); close(netfd); netreset(); sleep(RECONNECTTIMEO); diff --git a/stun.h b/stun.h @@ -1,3 +1,5 @@ +#include <sys/socket.h> + #include <stddef.h> #include <stdint.h> @@ -74,3 +76,5 @@ void pack64(unsigned char *, uint64_t); uint64_t unpack64(unsigned char *); void revokeprivs(void); int setnonblock(int, int); +char *saddr_ntop(struct sockaddr *, socklen_t); +char *peer_ntop(int); diff --git a/util.c b/util.c @@ -1,9 +1,12 @@ #include <sys/types.h> +#include <netdb.h> + #include <fcntl.h> #include <grp.h> #include <pwd.h> #include <stdint.h> +#include <stdio.h> #include <unistd.h> #include "stun.h" @@ -72,3 +75,38 @@ setnonblock(int fd, int mode) flags &= ~O_NONBLOCK; return fcntl(fd, F_SETFL, flags); } + +char * +saddr_ntop(struct sockaddr *sa, socklen_t salen) +{ + static char buf[512]; + char host[NI_MAXHOST], port[NI_MAXSERV]; + int ret; + + if ((ret = getnameinfo(sa, salen, host, sizeof(host), + port, sizeof(port), NI_NUMERICHOST | NI_NUMERICSERV))) { + if (ret == EAI_SYSTEM) { + logwarn("getnameinfo failed"); + return NULL; + } else { + logwarn("getnameinfo: %s", gai_strerror(ret)); + return NULL; + } + } + snprintf(buf, sizeof(buf), "[%s]:%s", host, port); + return buf; +} + +char * +peer_ntop(int fd) +{ + struct sockaddr_storage ss; + socklen_t sslen; + + sslen = sizeof(ss); + if (getpeername(fd, (struct sockaddr *)&ss, &sslen) < 0) { + logwarn("getpeername failed"); + return NULL; + } + return saddr_ntop((struct sockaddr *)&ss, sslen); +}