c++socketstcphole-punching

Socket programming between different networks


I'm developing a client-server program in c++ to transfer data from one computer to another.

Everything works fine but now I was asked to make it work on computers on different networks. I have searched everywhere but can't find a reliable solution.

I've seen the TCP hole punching solution but can't seem to find anywhere how to do it in c++.

I want it to work like teamviewer but without an intermediary server. Connect my client (on one computer) to the server (on another computer in a different network) all programmatically.

#include "../include/ip_tunnel_ms_windows_20180815.h" 
#include "../include/message_processor_common_20190410.h"


SOCKET clientSocket;
int n = 0;
void IPTunnel::initialize(void)
{

    if (inputSignals.empty()) {
        printf("server%d\n", n++);
        if (!server()) {
            printf("Error opening server\n");
            ::exit(1);
        }
    }
    else {
        printf("client%d\n", n++);
        if (!client()) {
            printf("Error opening client\n");
            ::exit(1);
        }
    }
}


bool IPTunnel::runBlock(void)
{
    .....
    (transmit data)
    .....
    return true;
}

void IPTunnel::terminate(void) {
    closesocket(clientSocket);
    WSACleanup();
}

bool IPTunnel::server() {
    WSADATA wsData;
    WORD ver = MAKEWORD(2, 2);

    int wsOk = WSAStartup(ver, &wsData);
    if (wsOk != 0)
    {
        cerr << "Can't Initialize winsock! Quitting" << endl;
        return false;
    }

    SOCKET listening = socket(AF_INET, SOCK_STREAM, 0);
    if (listening == INVALID_SOCKET)
    {
        cerr << "Can't create a socket! Quitting" << endl;
        return false;
    }

    sockaddr_in hint;
    hint.sin_family = AF_INET;
    hint.sin_port = ntohs(tcpPort);
    //inet_pton(AF_INET, (PCSTR)remoteMachineIpAddress.c_str(), &hint.sin_addr.s_addr); // hint.sin_addr.S_un.S_addr = inet_addr(ipAddressServer.c_str());
    hint.sin_addr.S_un.S_addr = INADDR_ANY;


    if (::bind(listening, (sockaddr*)& hint, sizeof(hint)) < 0) {
        printf("\n ERROR on binding");
        return false;
    }

    if (listen(listening, SOMAXCONN) == -1) {
        printf("\n ERROR on binding");
        return false;
    }

    sockaddr_in client;
    int clientSize = sizeof(client);

    clientSocket = accept(listening, (sockaddr*)& client, &clientSize);

    char host[NI_MAXHOST];
    char service[NI_MAXSERV];

    ZeroMemory(host, NI_MAXHOST);
    ZeroMemory(service, NI_MAXSERV);

    if (getnameinfo((sockaddr*)& client, sizeof(client), host, NI_MAXHOST, service, NI_MAXSERV, 0) == 0)
    {
        cout << host << " connected on port " << service << endl;
    }
    else
    {
        inet_ntop(AF_INET, &client.sin_addr, host, NI_MAXHOST);
        cout << host << " connected on port " <<
            ntohs(client.sin_port) << endl;
    }
    return true;
}

bool IPTunnel::client() {

    WSAData data;
    WORD ver = MAKEWORD(2, 2);
    int wsResult = WSAStartup(ver, &data);
    if (wsResult != 0)
    {
        cerr << "Can't start Winsock, Err #" << wsResult << endl;
        return false;
    }

    clientSocket = socket(AF_INET, SOCK_STREAM, 0);
    if (clientSocket == INVALID_SOCKET)
    {
        cerr << "Can't create socket, Err #" << WSAGetLastError() << endl;
        WSACleanup();
        return false;
    }

    sockaddr_in hint;
    hint.sin_family = AF_INET;
    hint.sin_port = htons(tcpPort);
    inet_pton(AF_INET, remoteMachineIpAddress.c_str(), &hint.sin_addr);

    int connResult = -2;
    while (connResult != 0 || numberOfTrials == 0) {
        connResult = connect(clientSocket, (sockaddr*)& hint, sizeof(hint));
        if (connResult == SOCKET_ERROR)
        {
            cerr << "Can't connect to server, Err #" << WSAGetLastError() << endl;
            cerr << "Waiting " << timeIntervalSeconds << " seconds." << endl;
        }

        Sleep(timeIntervalSeconds * 1000);
        ;
        if (--numberOfTrials == 0) {
            cerr << "Reached maximum number of attempts." << endl;
            ::exit(1);
        }
    }
    cout << "Connected!\n";
    return true;
}

Solution

  • The definition of hole punching includes:

    Both clients initiate a connection to an unrestricted server

    You say:

    I want it to work like teamviewer but without an intermediary server

    That's not possible in general.