From 3bac586a043ca1ba576229541a0a6dde91f3652a Mon Sep 17 00:00:00 2001 From: Keith Winstein Date: Sat, 13 Aug 2011 19:33:56 -0400 Subject: [PATCH] Use temporary socket for path MTU discovery --- Makefile | 2 +- network.cpp | 25 +++++++++++++++---------- stm.cpp | 3 ++- 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/Makefile b/Makefile index e6f5f06..2ec10b8 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ repos = templates.rpo executables = parse termemu ntester encrypt decrypt stm-server stm CXX = g++ -CXXFLAGS = -g -O2 --std=c++0x -pedantic -Werror -Wall -Wextra -Weffc++ -fno-implicit-templates -fno-default-inline -pipe -D_FILE_OFFSET_BITS=64 -D_XOPEN_SOURCE=500 -D_GNU_SOURCE -D_BSD_SOURCE +CXXFLAGS = -g --std=c++0x -pedantic -Werror -Wall -Wextra -Weffc++ -fno-implicit-templates -fno-default-inline -pipe -D_FILE_OFFSET_BITS=64 -D_XOPEN_SOURCE=500 -D_GNU_SOURCE -D_BSD_SOURCE LIBS = -lutil -lssl -lrt -lm -lprotobuf-lite PROTOC = protoc diff --git a/network.cpp b/network.cpp index d7f4afd..045370a 100644 --- a/network.cpp +++ b/network.cpp @@ -136,13 +136,23 @@ Connection::Connection( const char *key_str, const char *ip, int port ) /* clien void Connection::update_MTU( void ) { - /* Temporarily connect socket so we can retrieve path MTU */ - if ( connect( sock, (sockaddr *)&remote_addr, sizeof( remote_addr ) ) < 0 ) { + if ( !attached ) { + return; + } + + /* We don't want to use our main socket because we don't want to have to connect it */ + int path_MTU_socket = socket( AF_INET, SOCK_DGRAM, 0 ); + if ( path_MTU_socket < 0 ) { + throw NetworkException( "socket", errno ); + } + + /* Connect socket so we can retrieve path MTU */ + if ( connect( path_MTU_socket, (sockaddr *)&remote_addr, sizeof( remote_addr ) ) < 0 ) { throw NetworkException( "connect", errno ); } socklen_t optlen = sizeof( MTU ); - if ( getsockopt( sock, IPPROTO_IP, IP_MTU, &MTU, &optlen ) < 0 ) { + if ( getsockopt( path_MTU_socket, IPPROTO_IP, IP_MTU, &MTU, &optlen ) < 0 ) { throw NetworkException( "getsockopt", errno ); } @@ -150,14 +160,9 @@ void Connection::update_MTU( void ) throw NetworkException( "Error getting path MTU", errno ); } - /* Disconnect socket */ - struct sockaddr disconnect; - disconnect.sa_family = AF_UNSPEC; - - if ( connect( sock, (sockaddr *)&disconnect, sizeof( disconnect ) ) < 0 ) { - throw NetworkException( "connect", errno ); + if ( close( path_MTU_socket ) < 0 ) { + throw NetworkException( "close", errno ); } - } void Connection::send( string &s, bool send_timestamp ) diff --git a/stm.cpp b/stm.cpp index 14e73d8..6bf8d0a 100644 --- a/stm.cpp +++ b/stm.cpp @@ -180,7 +180,8 @@ void client( const char *ip, int port, const char *key ) if ( (pollfds[ 0 ].revents | pollfds[ 1 ].revents) & (POLLERR | POLLHUP | POLLNVAL) ) { - break; + perror( "poll" ); + // break; } } }