From 7ea81ca23758656396fdc50e5efc0c17d028891f Mon Sep 17 00:00:00 2001 From: Keith Winstein Date: Wed, 3 Aug 2011 21:20:44 -0400 Subject: [PATCH] Path MTU --- dos_assert.hpp | 2 +- network.cpp | 30 +++++++++++++++++++++++++++++- network.hpp | 7 +++++-- 3 files changed, 35 insertions(+), 4 deletions(-) diff --git a/dos_assert.hpp b/dos_assert.hpp index 3b1acf2..f28ecf9 100644 --- a/dos_assert.hpp +++ b/dos_assert.hpp @@ -6,7 +6,7 @@ static void dos_detected( const char *expression, const char *file, int line, const char *function ) { - fprintf( stderr, "Illegal counterparty input (possible DOS) in function %s at %s:%d, failed test: %s\n", + fprintf( stderr, "Illegal counterparty input (possible denial of service) in function %s at %s:%d, failed test: %s\n", function, file, line, expression ); exit( 1 ); } diff --git a/network.cpp b/network.cpp index 3667995..b5c71ab 100644 --- a/network.cpp +++ b/network.cpp @@ -60,7 +60,8 @@ Connection::Connection( bool s_server ) sock( -1 ), remote_addr(), server( s_server ), - attached( false ) + attached( false ), + MTU( RECEIVE_MTU ) { /* create socket */ @@ -80,6 +81,31 @@ Connection::Connection( bool s_server ) perror( "bind" ); exit( 1 ); } + + /* Enable path MTU discovery */ + char flag = IP_PMTUDISC_DO; + socklen_t optlen = sizeof( flag ); + if ( setsockopt( sock, IPPROTO_IP, IP_MTU_DISCOVER, &flag, optlen ) < 0 ) { + perror( "setsockopt" ); + exit( 1 ); + } +} + +template +void Connection::update_MTU( void ) +{ + socklen_t optlen = sizeof( MTU ); + if ( getsockopt( sock, IPPROTO_IP, IP_MTU, &MTU, &optlen ) < 0 ) { + perror( "getsockopt" ); + exit( 1 ); + } + + if ( optlen != sizeof( MTU ) ) { + fprintf( stderr, "Error getting path MTU.\n" ); + exit( 1 ); + } + + fprintf( stderr, "Path MTU: %d\n", MTU ); } template @@ -115,6 +141,8 @@ void Connection::send( Outgoing &s ) perror( "sendto" ); exit( 1 ); } + + update_MTU(); } template diff --git a/network.hpp b/network.hpp index f74403d..26b7e78 100644 --- a/network.hpp +++ b/network.hpp @@ -49,10 +49,9 @@ namespace Network { uint64_t next_seq; const Direction direction; - int MTU; Flow( Direction s_direction ) - : next_seq( 0 ), direction( s_direction ), MTU( 2048 ) + : next_seq( 0 ), direction( s_direction ) {} Packet new_packet( Payload &s_payload ); @@ -71,6 +70,10 @@ namespace Network { bool server; bool attached; + int MTU; + + void update_MTU( void ); + public: Connection( bool s_server );