stun

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

commit 89ff82a312ad917e7405e1684b0c030c0ff4dc96
parent 43f7c2ff10f634225ec3e99cec45ca74beacac30
Author: sin <sin@2f30.org>
Date:   Sat Apr 30 18:23:40 +0100

Factor out socket options configuration to a util function

Diffstat:
client.c | 26++------------------------
server.c | 26++------------------------
stun.h | 1+
util.c | 28++++++++++++++++++++++++++++
4 files changed, 33 insertions(+), 48 deletions(-)
diff --git a/client.c b/client.c @@ -1,9 +1,6 @@ #include <sys/types.h> #include <sys/socket.h> -#include <netinet/in.h> -#include <netinet/tcp.h> -#include <arpa/inet.h> #include <netdb.h> #include <string.h> @@ -15,7 +12,7 @@ int clientconnect(char *host, char *port) { struct addrinfo hints, *ai, *p; - int ret, netfd, val; + int ret, netfd; memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_UNSPEC; @@ -44,26 +41,7 @@ clientconnect(char *host, char *port) } setnonblock(netfd, 1); - - val = !!(netconfig.tcpflags & TCPFLAG_KEEPALIVE); - if (setsockopt(netfd, SOL_SOCKET, SO_KEEPALIVE, &val, sizeof(val)) < 0) - logwarn("setsockopt SO_KEEPALIVE"); - val = !!(netconfig.tcpflags & TCPFLAG_NODELAY); - if (setsockopt(netfd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val)) < 0) - logwarn("setsockopt TCP_NODELAY"); - - if (netconfig.tcpflags & TCPFLAG_RCVBUFSZ) { - val = netconfig.rcvbufsz; - if (setsockopt(netfd, SOL_SOCKET, SO_RCVBUF, &val, - sizeof(val)) < 0) - logwarn("setsockopt SO_RCVBUF"); - } - if (netconfig.tcpflags & TCPFLAG_SNDBUFSZ) { - val = netconfig.sndbufsz; - if (setsockopt(netfd, SOL_SOCKET, SO_SNDBUF, &val, - sizeof(val)) < 0) - logwarn("setsockopt SO_SNDBUF"); - } + setsockopts(netfd); if (response(netfd) < 0 || challenge(netfd) < 0) { close(netfd); diff --git a/server.c b/server.c @@ -1,9 +1,6 @@ #include <sys/types.h> #include <sys/socket.h> -#include <netinet/in.h> -#include <netinet/tcp.h> -#include <arpa/inet.h> #include <netdb.h> #include <errno.h> @@ -55,7 +52,7 @@ int serveraccept(int listenfd) { struct sockaddr_storage remote; - int netfd, val; + int netfd; if (verbose) logdbgx("waiting for a client to connect..."); @@ -68,26 +65,7 @@ serveraccept(int listenfd) } setnonblock(netfd, 1); - - val = !!(netconfig.tcpflags & TCPFLAG_KEEPALIVE); - if (setsockopt(netfd, SOL_SOCKET, SO_KEEPALIVE, &val, sizeof(val)) < 0) - logwarn("setsockopt SO_KEEPALIVE"); - val = !!(netconfig.tcpflags & TCPFLAG_NODELAY); - if (setsockopt(netfd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val)) < 0) - logwarn("setsockopt TCP_NODELAY"); - - if (netconfig.tcpflags & TCPFLAG_RCVBUFSZ) { - val = netconfig.rcvbufsz; - if (setsockopt(netfd, SOL_SOCKET, SO_RCVBUF, &val, - sizeof(val)) < 0) - logwarn("setsockopt SO_RCVBUF"); - } - if (netconfig.tcpflags & TCPFLAG_SNDBUFSZ) { - val = netconfig.sndbufsz; - if (setsockopt(netfd, SOL_SOCKET, SO_SNDBUF, &val, - sizeof(val)) < 0) - logwarn("setsockopt SO_SNDBUF"); - } + setsockopts(netfd); if (challenge(netfd) < 0 || response(netfd) < 0) { close(netfd); diff --git a/stun.h b/stun.h @@ -123,3 +123,4 @@ int setnonblock(int, int); char *saddr_ntop(struct sockaddr *, socklen_t); char *peer_ntop(int); int ipversion(unsigned char *); +void setsockopts(int); diff --git a/util.c b/util.c @@ -1,7 +1,9 @@ #include <sys/types.h> +#include <sys/socket.h> #include <netinet/in.h> #include <netinet/ip.h> +#include <netinet/tcp.h> #include <netdb.h> #include <fcntl.h> @@ -129,3 +131,29 @@ ipversion(unsigned char *pkt) } return -1; } + +void +setsockopts(int fd) +{ + int val; + + val = !!(netconfig.tcpflags & TCPFLAG_KEEPALIVE); + if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &val, sizeof(val)) < 0) + logwarn("setsockopt SO_KEEPALIVE"); + val = !!(netconfig.tcpflags & TCPFLAG_NODELAY); + if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val)) < 0) + logwarn("setsockopt TCP_NODELAY"); + + if (netconfig.tcpflags & TCPFLAG_RCVBUFSZ) { + val = netconfig.rcvbufsz; + if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &val, + sizeof(val)) < 0) + logwarn("setsockopt SO_RCVBUF"); + } + if (netconfig.tcpflags & TCPFLAG_SNDBUFSZ) { + val = netconfig.sndbufsz; + if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &val, + sizeof(val)) < 0) + logwarn("setsockopt SO_SNDBUF"); + } +}