Skip to content

Commit

Permalink
Support binding to a specific ip address. Closes #256 (#438)
Browse files Browse the repository at this point in the history
* Support binding to a specific ip address.  Closes #256

* fix ini

* comment out bind ip by default

* Try mac 11 for vcpkg

* try bumping hash
  • Loading branch information
MisterTea authored May 24, 2021
1 parent 111aebb commit 48fbdd1
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/vcpkg_build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ jobs:
# The key includes: hash of the vcpkg.json file, the hash of the vcpkg Git commit id, and the used vcpkg's triplet. The vcpkg's commit id would suffice, but computing an hash out it does not harm.
# Note: given a key, the cache content is immutable. If a cache entry has been created improperly, in order the recreate the right content the key must be changed as well, and it must be brand new (i.e. not existing already).
key: |
et-vcpkg-${{ hashFiles( 'vcpkg.json' ) }}-${{ hashFiles( '.git/modules/external/vcpkg/HEAD' )}}-${{ matrix.os }}
et-2-vcpkg-${{ hashFiles( 'vcpkg.json' ) }}-${{ hashFiles( '.git/modules/external/vcpkg/HEAD' )}}-${{ matrix.os }}
- name: Show content of workspace after cache has been restored
run: find $RUNNER_WORKSPACE
Expand Down
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@ endif()
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# Enable compile commands export for vscode
add_definitions(-DCMAKE_EXPORT_COMPILE_COMMANDS=ON)

if(WIN32)
# Enable unicode
add_definitions(-DUNICODE)
Expand Down
1 change: 1 addition & 0 deletions etc/et.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

[Networking]
port = 2022
# bind_ip = 0.0.0.0

[Debug]
verbose = 0
Expand Down
11 changes: 7 additions & 4 deletions src/base/TcpSocketHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ int TcpSocketHandler::connect(const SocketEndpoint &endpoint) {
setBlocking(sockFd, false);

if (::connect(sockFd, p->ai_addr, p->ai_addrlen) == -1 &&
GetErrno() != EINPROGRESS && GetErrno() != EWOULDBLOCK
) {
GetErrno() != EINPROGRESS && GetErrno() != EWOULDBLOCK) {
auto localErrno = GetErrno();
if (p->ai_canonname) {
LOG(INFO) << "Error connecting with " << p->ai_canonname << ": "
Expand Down Expand Up @@ -163,11 +162,15 @@ set<int> TcpSocketHandler::listen(const SocketEndpoint &endpoint) {
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE; // use my IP address
hints.ai_flags = AI_PASSIVE; // use any IP address
const char *bindIp = NULL;
if (endpoint.has_name()) {
bindIp = endpoint.name().c_str();
}

std::string portname = std::to_string(port);

if ((rc = getaddrinfo(NULL, portname.c_str(), &hints, &servinfo)) != 0) {
if ((rc = getaddrinfo(bindIp, portname.c_str(), &hints, &servinfo)) != 0) {
STERROR << "Error getting address info for " << port << ": " << rc << " ("
<< gai_strerror(rc) << ")";
exit(1);
Expand Down
27 changes: 22 additions & 5 deletions src/terminal/TerminalServerMain.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include "TelemetryService.hpp"
#include "TerminalServer.hpp"
#include <cxxopts.hpp>

#include "SimpleIni.h"
#include <cxxopts.hpp>
#include "TelemetryService.hpp"
#include "TerminalServer.hpp"

using namespace et;
namespace google {}
Expand Down Expand Up @@ -31,7 +31,9 @@ int main(int argc, char **argv) {
("version", "Print version") //
("port", "Port to listen on",
cxxopts::value<int>()->default_value("0")) //
("daemon", "Daemonize the server") //
("bindip", "IP to listen on",
cxxopts::value<string>()->default_value("")) //
("daemon", "Daemonize the server") //
("cfgfile", "Location of the config file",
cxxopts::value<std::string>()->default_value("")) //
("logtostdout", "log to stdout") //
Expand Down Expand Up @@ -79,6 +81,7 @@ int main(int argc, char **argv) {
string maxlogsize = "20971520";

int port = 0;
string bindIp = "";
bool telemetry = true;
if (result.count("cfgfile")) {
// Load the config file
Expand All @@ -87,12 +90,19 @@ int main(int argc, char **argv) {
SI_Error rc = ini.LoadFile(cfgfilename.c_str());
if (rc == 0) {
if (!result.count("port")) {
const char *portString = ini.GetValue("Networking", "Port", NULL);
const char *portString = ini.GetValue("Networking", "port", NULL);
if (portString) {
port = stoi(portString);
}
}

if (!result.count("bindip")) {
const char *bindIpPtr = ini.GetValue("Networking", "bind_ip", NULL);
if (bindIpPtr) {
bindIp = string(bindIpPtr);
}
}

telemetry = bool(stoi(ini.GetValue("Debug", "Telemetry", "1")));
// read verbose level (prioritize command line option over cfgfile)
const char *vlevel = ini.GetValue("Debug", "verbose", NULL);
Expand Down Expand Up @@ -136,6 +146,10 @@ int main(int argc, char **argv) {
port = result["port"].as<int>();
}

if (result.count("bindip")) {
bindIp = result["bindip"].as<string>();
}

if (result.count("telemetry")) {
telemetry = result["telemetry"].as<bool>();
}
Expand Down Expand Up @@ -169,6 +183,9 @@ int main(int argc, char **argv) {

SocketEndpoint serverEndpoint;
serverEndpoint.set_port(port);
if (bindIp.length()) {
serverEndpoint.set_name(bindIp);
}
SocketEndpoint routerFifo;
routerFifo.set_name(serverFifo);
TerminalServer terminalServer(tcpSocketHandler, serverEndpoint,
Expand Down

0 comments on commit 48fbdd1

Please sign in to comment.