diff --git a/vsock-bridge/include/config.h b/vsock-bridge/include/config.h index a8cd0e3..f060d43 100644 --- a/vsock-bridge/include/config.h +++ b/vsock-bridge/include/config.h @@ -32,6 +32,8 @@ namespace vsockproxy ServiceType _type = ServiceType::UNKNOWN; EndpointConfig _listenEndpoint; EndpointConfig _connectEndpoint; + int _sndbuf; + int _rcvbuf; }; std::vector loadConfig(const std::string& filepath); diff --git a/vsock-bridge/include/listener.h b/vsock-bridge/include/listener.h index 52383df..3ab953a 100644 --- a/vsock-bridge/include/listener.h +++ b/vsock-bridge/include/listener.h @@ -70,8 +70,11 @@ namespace vsockio const int MAX_POLLER_EVENTS = 256; const int SO_BACKLOG = 64; - Listener(std::unique_ptr&& listenEndpoint, std::unique_ptr&& connectEndpoint, Dispatcher& dispatcher) + Listener(std::unique_ptr&& listenEndpoint, std::unique_ptr&& connectEndpoint, Dispatcher& dispatcher, + int SNDBUF, int RCVBUF) : _fd(-1) + , _sndbuf(SNDBUF) + , _rcvbuf(RCVBUF) , _listenEp(std::move(listenEndpoint)) , _connectEp(std::move(connectEndpoint)) , _events(new VsbEvent[MAX_POLLER_EVENTS]) @@ -174,6 +177,18 @@ namespace vsockio return; } + if (setsockopt(clientFd, SOL_SOCKET, SO_RCVBUF, &_rcvbuf, sizeof(int)) < 0) + { + close(clientFd); + throw std::runtime_error("error setting SO_RCVBUF"); + } + + if (setsockopt(clientFd, SOL_SOCKET, SO_SNDBUF, &_sndbuf, sizeof(int)) < 0) + { + close(clientFd); + throw std::runtime_error("error setting SO_SNDBUF"); + } + auto outPeer = connectToPeer(); if (!outPeer) { @@ -209,6 +224,18 @@ namespace vsockio return nullptr; } + if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &_rcvbuf, sizeof(int)) < 0) + { + close(fd); + throw std::runtime_error("error setting SO_RCVBUF"); + } + + if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &_sndbuf, sizeof(int)) < 0) + { + close(fd); + throw std::runtime_error("error setting SO_SNDBUF"); + } + auto addrAndLen = _connectEp->getAddress(); int status = connect(fd, addrAndLen.first, addrAndLen.second); if (status == 0) @@ -232,6 +259,8 @@ namespace vsockio inline bool listening() const { return _fd >= 0; } int _fd; + int _sndbuf; + int _rcvbuf; std::unique_ptr _listenEp; std::unique_ptr _listenEpClone; std::unique_ptr _connectEp; diff --git a/vsock-bridge/src/vsock-bridge.cpp b/vsock-bridge/src/vsock-bridge.cpp index ed7cd6d..f5c2a4c 100644 --- a/vsock-bridge/src/vsock-bridge.cpp +++ b/vsock-bridge/src/vsock-bridge.cpp @@ -27,7 +27,8 @@ static std::unique_ptr createEndpoint(EndpointScheme scheme, const std } } -static std::unique_ptr createListener(Dispatcher& dispatcher, EndpointScheme inScheme, const std::string& inAddress, uint16_t inPort, EndpointScheme outScheme, const std::string& outAddress, uint16_t outPort) +static std::unique_ptr createListener(Dispatcher& dispatcher, EndpointScheme inScheme, const std::string& inAddress, uint16_t inPort, EndpointScheme outScheme, + const std::string& outAddress, uint16_t outPort, int SNDBUF, int RCVBUF) { auto listenEp { createEndpoint(inScheme, inAddress, inPort) }; auto connectEp{ createEndpoint(outScheme, outAddress, outPort) }; @@ -44,7 +45,7 @@ static std::unique_ptr createListener(Dispatcher& dispatcher, Endpoint } else { - return std::make_unique(std::move(listenEp), std::move(connectEp), dispatcher); + return std::make_unique(std::move(listenEp), std::move(connectEp), dispatcher, SNDBUF, RCVBUF); } } @@ -68,7 +69,9 @@ static void startServices(const std::vector& services, int n /*inPort:*/ sd._listenEndpoint._port, /*outScheme:*/ sd._connectEndpoint._scheme, /*outAddress:*/ sd._connectEndpoint._address, - /*outPort:*/ sd._connectEndpoint._port + /*outPort:*/ sd._connectEndpoint._port, + sd._sndbuf, + sd._rcvbuf ); if (!listener)