commit 8bedd55b73dbdcc7c19f1286aa68b2712aa4c5fe parent b34f9a1a6b36ecc62aabe9c60e22c12a1e628d57 Author: Michael Savage <mikejsavage@gmail.com> Date: Sat Sep 2 19:17:41 +0300 Socket cleanup Diffstat:
platform_network.cc | | | 32 | +++++++++++--------------------- |
diff --git a/platform_network.cc b/platform_network.cc @@ -86,6 +86,14 @@ static struct sockaddr_storage netaddress_to_sockaddr( const NetAddress & addr ) return ss; } +static void setsockoptone( OSSocket fd, int level, int opt ) { + int one = 1; + int ok = setsockopt( fd, level, opt, ( char * ) &one, sizeof( one ) ); + if( ok == -1 ) { + FATAL( "setsockopt" ); + } +} + UDPSocket net_new_udp( NonblockingBool nonblocking, u16 port ) { UDPSocket sock; @@ -104,27 +112,9 @@ UDPSocket net_new_udp( NonblockingBool nonblocking, u16 port ) { } if( port != 0 ) { - int one = 1; - { - int ok = setsockopt( sock.ipv4, SOL_SOCKET, SO_REUSEADDR, ( char * ) &one, sizeof( one ) ); - if( ok == -1 ) { - FATAL( "setsockopt" ); - } - } - - { - int ok = setsockopt( sock.ipv6, SOL_SOCKET, SO_REUSEADDR, ( char * ) &one, sizeof( one ) ); - if( ok == -1 ) { - FATAL( "setsockopt" ); - } - } - - { - int ok = setsockopt( sock.ipv6, IPPROTO_IPV6, IPV6_V6ONLY, ( char * ) &one, sizeof( one ) ); - if( ok == -1 ) { - FATAL( "setsockopt" ); - } - } + setsockoptone( sock.ipv4, SOL_SOCKET, SO_REUSEADDR ); + setsockoptone( sock.ipv6, SOL_SOCKET, SO_REUSEADDR ); + setsockoptone( sock.ipv6, IPPROTO_IPV6, IPV6_V6ONLY ); } {