From a30727fe2b0d1d4c83aae7b00351e529ddef536b Mon Sep 17 00:00:00 2001 From: Martin Willi Date: Thu, 11 Apr 2013 20:19:57 +0200 Subject: socket-default: refactor socket pair opening to a function --- .../plugins/socket_default/socket_default_socket.c | 50 ++++++++++------------ 1 file changed, 23 insertions(+), 27 deletions(-) (limited to 'src/libcharon/plugins/socket_default/socket_default_socket.c') diff --git a/src/libcharon/plugins/socket_default/socket_default_socket.c b/src/libcharon/plugins/socket_default/socket_default_socket.c index 4e6217af3..9c1bce303 100644 --- a/src/libcharon/plugins/socket_default/socket_default_socket.c +++ b/src/libcharon/plugins/socket_default/socket_default_socket.c @@ -614,6 +614,27 @@ static int open_socket(private_socket_default_socket_t *this, return skt; } +/** + * Open a socket pair (normal an NAT traversal) for a given address family + */ +static void open_socketpair(private_socket_default_socket_t *this, int family, + int *skt, int *skt_natt, char *label) +{ + *skt = open_socket(this, family, &this->port); + if (*skt == -1) + { + DBG1(DBG_NET, "could not open %s socket, %s disabled", label, label); + } + else + { + *skt_natt = open_socket(this, family, &this->natt); + if (*skt_natt == -1) + { + DBG1(DBG_NET, "could not open %s NAT-T socket", label); + } + } +} + METHOD(socket_t, destroy, void, private_socket_default_socket_t *this) { @@ -672,33 +693,8 @@ socket_default_socket_t *socket_default_socket_create() /* we allocate IPv6 sockets first as that will reserve randomly allocated * ports also for IPv4 */ - this->ipv6 = open_socket(this, AF_INET6, &this->port); - if (this->ipv6 == -1) - { - DBG1(DBG_NET, "could not open IPv6 socket, IPv6 disabled"); - } - else - { - this->ipv6_natt = open_socket(this, AF_INET6, &this->natt); - if (this->ipv6_natt == -1) - { - DBG1(DBG_NET, "could not open IPv6 NAT-T socket"); - } - } - - this->ipv4 = open_socket(this, AF_INET, &this->port); - if (this->ipv4 == -1) - { - DBG1(DBG_NET, "could not open IPv4 socket, IPv4 disabled"); - } - else - { - this->ipv4_natt = open_socket(this, AF_INET, &this->natt); - if (this->ipv4_natt == -1) - { - DBG1(DBG_NET, "could not open IPv4 NAT-T socket"); - } - } + open_socketpair(this, AF_INET6, &this->ipv6, &this->ipv6_natt, "IPv6"); + open_socketpair(this, AF_INET, &this->ipv4, &this->ipv4_natt, "IPv4"); if (this->ipv4 == -1 && this->ipv6 == -1) { -- cgit v1.2.3