Esempio n. 1
0
    int initializeAndStartProxyServer (DSPro *pDSPro, ConfigManager *pCfgMgr, DSProProxyServer &proxySrv)
    {
        if (pDSPro == NULL || pCfgMgr == NULL) {
            checkAndLogMsg ("DSProMain::initializeAndStartProxyServer (1)", Logger::L_SevereError,
                            "DSProProxyServer has failed to initialize\n");
            return -1;
        }
        const char *pszProxyServerInterface = NULL;
        uint16 ui16ProxyServerPort = 56487; // DISPRO_SVC_PROXY_SERVER_PORT_NUMBER;
        if (pCfgMgr->hasValue ("aci.disservice.proxy.interface")) {
            pszProxyServerInterface = pCfgMgr->getValue ("aci.disservice.proxy.interface");
        }
        if (pCfgMgr->hasValue ("aci.disservice.proxy.port")) {
            ui16ProxyServerPort = (uint16) pCfgMgr->getValueAsInt ("aci.disservice.proxy.port", 56487);
        }

        if (proxySrv.init (pDSPro, pszProxyServerInterface, ui16ProxyServerPort) != 0) {
            checkAndLogMsg ("DSProMain::initializeAndStartProxyServer (2)", Logger::L_SevereError,
                            "DSProProxyServer has failed to initialize\n");
            return -2;
        }
        proxySrv.start();
        checkAndLogMsg ("DSProMain::initializeAndStartProxyServer", Logger::L_Info,
                        "DSProProxyServer is running on port %d\n", ui16ProxyServerPort);
        return 0;
    }
    int LzmaConnectorWriter::writeData (const unsigned char *pSrc, unsigned int uiSrcLen, unsigned char **pDest, unsigned int &uiDestLen, bool bLocalFlush)
    {
        int rc;
        unsigned int uiOldAvailableSpace = 0;
        bool bNeedFlush = false;
        _lzmaCompStream.next_in = pSrc;
        _lzmaCompStream.avail_in = uiSrcLen;
        uiDestLen = 0;
        *pDest = NULL;
        _bFlushed = false;

        while ((_lzmaCompStream.avail_in > 0) || bNeedFlush) {
            bNeedFlush = false;
            uiOldAvailableSpace = _lzmaCompStream.avail_out;
            if (bLocalFlush) {
                rc = lzma_code (&_lzmaCompStream, LZMA_SYNC_FLUSH);
                if (rc > 1) {
                    checkAndLogMsg ("LzmaConnectorWriter::writeData", Logger::L_MildError,
                                    "lzma_code() called with flag LZMA_SYNC_FLUSH returned with error code %d\n", rc);
                    uiDestLen = 0;
                    *pDest = NULL;
                    return -1;
                }
            }
            else {
                rc = lzma_code (&_lzmaCompStream, LZMA_RUN);
                if (rc > 1) {
                    checkAndLogMsg ("LzmaConnectorWriter::writeData", Logger::L_MildError,
                                    "lzma_code() called with flag LZMA_RUN returned with error code %d\n", rc);
                    uiDestLen = 0;
                    *pDest = NULL;
                    return -1;
                }
            }
            uiDestLen += uiOldAvailableSpace - _lzmaCompStream.avail_out;

            if (_lzmaCompStream.avail_out == 0) {
                bNeedFlush = true;
                _ulOutBufSize *= 2;
                _pOutputBuffer = (unsigned char *) realloc (_pOutputBuffer, _ulOutBufSize);
            }
            _lzmaCompStream.avail_out = _ulOutBufSize - uiDestLen;
            _lzmaCompStream.next_out = _pOutputBuffer + uiDestLen;
        }

        if (uiDestLen > 0) {
            *pDest = _pOutputBuffer;
        }
        else {
            *pDest = NULL;
        }
        _lzmaCompStream.avail_out = _ulOutBufSize;
        _lzmaCompStream.next_out = _pOutputBuffer;

        return 0;
    }
Esempio n. 3
0
    void SocketConnector::run (void)
    {
        started();

        while (!terminationRequested()) {
            TCPSocket * const pSocket = dynamic_cast<TCPSocket * const> (_pServerSocket->accept());
            if (!pSocket) {
                if (!terminationRequested()) {
                    checkAndLogMsg ("SocketConnector::run", Logger::L_MildError,
                                    "accept() on ServerSocket failed with error %d\n",
                                    _pServerSocket->error());
                    setTerminatingResultCode (-1);
                }
                break;
            }
            pSocket->bufferingMode (0);

            ConnectorAdapter * const pConnectorAdapter = ConnectorAdapter::ConnectorAdapterFactory (pSocket);
            Connection * const pConnection = new Connection (pConnectorAdapter, this);

            _mConnectionsTable.lock();
            pConnection->lock();
            Connection * const pOldConnection = _connectionsTable.put (generateUInt64Key (InetAddr (pSocket->getRemoteHostAddr(), pSocket->getRemotePort())), pConnection);
            _mConnectionsTable.unlock();
            if (pOldConnection) {
                // There was already a connection from this node to the remote node - close that one
                checkAndLogMsg ("SocketConnector::run", Logger::L_Info,
                                "replaced an old SocketConnection to <%s:%hu> in status %hu with a new one\n",
                                pConnection->getRemoteProxyInetAddr()->getIPAsString(), pConnection->getRemoteProxyInetAddr()->getPort(),
                                pOldConnection->getStatus());
                delete pOldConnection;
            }
            else {
                checkAndLogMsg ("SocketConnector::run", Logger::L_Info,
                                "accepted a new SocketConnection from <%s:%hu>\n",
                                pConnection->getRemoteProxyInetAddr()->getIPAsString(),
                                pConnection->getRemoteProxyInetAddr()->getPort());
            }

            pConnection->startMessageHandler();
            pConnection->unlock();
        }
        checkAndLogMsg ("SocketConnector::run", Logger::L_Info,
                        "SocketConnector terminated; termination code is %d\n", getTerminatingResultCode());

        Connector::_pConnectionManager->deregisterConnector (_connectorType);

        terminating();
        delete this;
    }
Esempio n. 4
0
 int getHomeDir (const char *pszProgDir, String &homeDir)
 {
     #ifndef ANDROID
         if (pszProgDir == NULL) {
             return -1;
         }
         // Compute the Home Directory
         // Assume that the executable is in <home>\\bin (or <home/bin>)
         char szHomeDir[PATH_MAX];
         strcpy (szHomeDir, pszProgDir);
         // Strip off last directory level in path
         char *pszTemp = strrchr (szHomeDir, getPathSepChar());
         if (pszTemp == NULL) {
             checkAndLogMsg ("main", Logger::L_SevereError,
                             "executable not installed in expected directory structure - could not parse directory "
                             "<%s> to compute the config file directory\n", pszProgDir);
             #if defined (WIN32)
                 printf ("\n\n");
                 system ("pause");
             #endif
             return -2;
         }
         *pszTemp = '\0';
         homeDir = szHomeDir;
     #endif
     return 0;
 }
Esempio n. 5
0
    int SocketConnector::init (uint16 ui16SocketPort)
    {
        int rc;

        _pServerSocket = new TCPSocket();
        if ((rc = _pServerSocket->setupToReceive (ui16SocketPort)) < 0) {
            checkAndLogMsg ("SocketConnector::init", Logger::L_MildError,
                            "listen() on ServerSocket failed - could not initialize to use port %d; rc = %d\n",
                            (int) ui16SocketPort, rc);
            return -1;
        }

        return 0;
    }
Esempio n. 6
0
    char * const PacketBufferManager::getAndLockWriteBuf (void)
    {
        for (int i = 0; i < NetProxyApplicationParameters::WRITE_PACKET_BUFFERS; i++) {
            if (_amtxTAPBuf[i].try_lock()) {
                return _cTAPBuf[i];
            }
        }

        checkAndLogMsg ("PacketBufferManager::getWriteBuf", NOMADSUtil::Logger::L_Warning,
                        "could not find a free buffer; waiting for one to be freed\n");

        _amtxTAPBuf[0].lock();
        return _cTAPBuf[0];
    }
Esempio n. 7
0
 void getDefaultConfigFile (const char *pszHomeDir, const char *pszDefaultConfigFile, String &configFile, bool bPermissive=true)
 {
     if (configFile.length() <= 0) {
         char *pszConfigFilePath = ConfigManager::getDefaultConfigFilePath (pszHomeDir, pszDefaultConfigFile);
         if ((pszConfigFilePath == NULL) && (!bPermissive)) {
             printUsageAndExitWithError();
         }
         else if (pszConfigFilePath != NULL) {
             configFile = pszConfigFilePath;
             free (pszConfigFilePath);
             checkAndLogMsg ("DSProMain::getDefaultConfigFile", Logger::L_Info, "attempting to run "
                             "DSPro with default config file: <%s>\n", configFile.c_str());
         }
     }
 }
Esempio n. 8
0
 int addPeer (DSPro &dspro, AdaptorType adaptorType, StringHashset &addresses, uint16 ui16Port)
 {
     int allRc = 0;
     const char *pszMethodName = "DSProUtils::addPeer";
     StringHashset::Iterator iter = addresses.getAllElements();
     for (; !iter.end(); iter.nextElement()) {
         int rc = dspro.addPeer (adaptorType, NULL, iter.getKey(), ui16Port);
         if (rc < 0) {
             checkAndLogMsg (pszMethodName, Logger::L_Warning, "could not connect "
                 "to %s:%u via %s. Returned %d.\n", iter.getKey(), ui16Port,
                 getAdaptorTypeAsString (adaptorType), rc);
             allRc = -1;
         }
     }
     return allRc;
 }
Esempio n. 9
0
    void initializeLoggers (const char *pszLogDir)
    {
        if (!FileUtils::directoryExists (pszLogDir)) {
            if (!FileUtils::createDirectory (pszLogDir)) {
                return;
            }
        }
        time_t now = time (NULL);
        struct tm *ptm = localtime (&now);
        static char timestamp[17];
        sprintf (timestamp, "%d%02d%02d-%02d_%02d_%02d",
                 (ptm->tm_year+1900), (ptm->tm_mon+1), ptm->tm_mday,
                 ptm->tm_hour, ptm->tm_min, ptm->tm_sec);

        printf ("Running DSPro (Version built on %s).\n", BUILD_TIME);
        initializeLogger (&pLogger, pszLogDir, "dspro.log", timestamp, false);
        checkAndLogMsg ("main", Logger::L_Info, "Running DSPro version built on %s.\n", BUILD_TIME);
        initializeLogger (&pNetLog, pszLogDir, "dspro-matchmaking.log", timestamp, false);
        initializeLogger (&pTopoLog, pszLogDir, "dspro-topology.log", timestamp, false);
        initializeLogger (&pCmdProcLog, pszLogDir, "dspro-notifications.log", timestamp, false);
    }
Esempio n. 10
0
    int LzmaConnectorWriter::flush (unsigned char **pDest, unsigned int &uiDestLen)
    {
        if (_bFlushed) {
            *pDest = NULL;
            uiDestLen = 0;
            return 0;
        }

        bool bDone = false;
        _lzmaCompStream.next_in = NULL;
        _lzmaCompStream.avail_in = 0;
        uiDestLen = 0;
        while (!bDone) {
            int rc;
            unsigned int uiOldAvail_out = _lzmaCompStream.avail_out;
            if (0 != (rc = lzma_code ( &_lzmaCompStream, LZMA_FINISH))) {
                if (rc == LZMA_STREAM_END) {
                    bDone = true;
                }
                else if (rc != LZMA_OK) {
                    checkAndLogMsg ("LzmaConnectorWriter::flush", Logger::L_MildError,
                                    "deflate with flag Z_FINISH returned with error code %d\n", rc);
                    uiDestLen = 0;
                    *pDest = NULL;
                    return -1;
                }
            }
            if (_lzmaCompStream.avail_out < _ulOutBufSize) {
                uiDestLen += (uiOldAvail_out - _lzmaCompStream.avail_out);
                if (_lzmaCompStream.avail_out == 0) {
                    _ulOutBufSize *= 2;
                    _pOutputBuffer = (unsigned char*) realloc (_pOutputBuffer, _ulOutBufSize);
                    if (!_pOutputBuffer) {
                        checkAndLogMsg ("LzmaConnectorWriter::flush", Logger::L_MildError,
                                        "error trying to realloc %u (previously %u) bytes\n",
                                        _ulOutBufSize, _ulOutBufSize/2);
                        uiDestLen = 0;
                        *pDest = NULL;
                        return -2;
                    }
                }
                _lzmaCompStream.avail_out = _ulOutBufSize - uiDestLen;
                _lzmaCompStream.next_out = _pOutputBuffer + uiDestLen;
            }
            else if (!bDone) {
                // deflate was not done but did not put anything into the output buffer
                checkAndLogMsg ("LzmaConnectorWriter::flush", Logger::L_MildError,
                                "lzma_code() with flag LZMA_FINISH didn't produce new output but returned code is not LZMA_STREAM_END (code: %d)\n", rc);
                uiDestLen = 0;
                *pDest = NULL;
                return -3;
            }
        }

        if (uiDestLen > 0) {
            *pDest = _pOutputBuffer;
        }
        else {
            *pDest = NULL;
        }
        _lzmaCompStream.avail_out = _ulOutBufSize;
        _lzmaCompStream.next_out = _pOutputBuffer;
        _bFlushed = true;

        return 0;
    }
Esempio n. 11
0
    int LzmaConnectorWriter::writeDataAndResetWriter (const unsigned char *pSrc, unsigned int uiSrcLen, unsigned char **pDest, unsigned int &uiDestLen)
    {
        int rc;
        unsigned int uiOldAvail_out = 0;
        bool bDone = false;
        *pDest = NULL;
        uiDestLen = 0;

        if (!pSrc || (uiSrcLen == 0)) {
            lzma_end (&_lzmaCompStream);
            resetCompStream();
            if (LZMA_OK != lzma_easy_encoder (&_lzmaCompStream, getCompressionLevel(), COMPRESSION_CHECK)) {
                return -1;
            }
            return 0;
        }
        _lzmaCompStream.next_in = pSrc;
        _lzmaCompStream.avail_in = uiSrcLen;

        while (!bDone) {
            uiOldAvail_out = _lzmaCompStream.avail_out;
            if (0 != (rc = lzma_code ( &_lzmaCompStream, LZMA_FINISH))) {
                if (rc == LZMA_STREAM_END) {
                    bDone = true;
                }
                else if (rc != LZMA_OK) {
                    checkAndLogMsg ("LzmaConnectorWriter::flush", Logger::L_MildError,
                                    "deflate with flag Z_FINISH returned with error code %d\n", rc);
                    uiDestLen = 0;
                    *pDest = NULL;
                    return -2;
                }
            }
            if (_lzmaCompStream.avail_out < _ulOutBufSize) {
                uiDestLen += (uiOldAvail_out - _lzmaCompStream.avail_out);
                if (_lzmaCompStream.avail_out == 0) {
                    _ulOutBufSize *= 2;
                    _pOutputBuffer = (unsigned char*) realloc (_pOutputBuffer, _ulOutBufSize);
                    if (!_pOutputBuffer) {
                        checkAndLogMsg ("LzmaConnectorWriter::flush", Logger::L_MildError,
                                        "error trying to realloc %u (previously %u) bytes\n",
                                        _ulOutBufSize, _ulOutBufSize/2);
                        uiDestLen = 0;
                        *pDest = NULL;
                        return -3;
                    }
                }
                _lzmaCompStream.avail_out = _ulOutBufSize - uiDestLen;
                _lzmaCompStream.next_out = _pOutputBuffer + uiDestLen;
            }
            else if (!bDone) {
                // deflate was not done but did not put anything into the output buffer
                checkAndLogMsg ("LzmaConnectorWriter::flush", Logger::L_MildError,
                                "lzma_code() with flag LZMA_FINISH didn't produce new output but returned code is not LZMA_STREAM_END (code: %d)\n", rc);
                uiDestLen = 0;
                *pDest = NULL;
                return -4;
            }
        }

        if (uiDestLen > 0) {
            *pDest = _pOutputBuffer;
        }
        else {
            *pDest = NULL;
        }
        _lzmaCompStream.avail_out = _ulOutBufSize;
        _lzmaCompStream.next_out = _pOutputBuffer;
        _bFlushed = true;

        lzma_end (&_lzmaCompStream);
        resetCompStream();
        if (LZMA_OK != lzma_easy_encoder (&_lzmaCompStream, getCompressionLevel(), COMPRESSION_CHECK)) {
            return -5;
        }

        return 0;
    }
Esempio n. 12
0
    int TapInterface::init (void)
    {
        std::lock_guard<std::mutex> lg (_mtxWrite);

        if (_bInitDone) {
            return 0;
        }
        _bInitDone = true;

        #if defined (WIN32)
            const char * WIN32_NETWORK_ADAPTERS_REG_KEY = "SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E972-E325-11CE-BFC1-08002BE10318}";
            const char * WIN32_NETWORK_ADAPTERS_NAMES_REG_KEY = "SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}";
            const char * WIN32_TCPIP_INTERFACE_CONFIGURATION_REG_KEY = "SYSTEM\\CurrentControlSet\\services\\Tcpip\\Parameters\\Interfaces";
            const char * TAP_INTERFACE_DESCRIPTION1 = "TAP-Win32";
            const char * TAP_INTERFACE_DESCRIPTION2 = "TAP-Windows Adapter V9";
            const char * USER_MODE_DEVICE_DIR_PREFIX = "\\\\.\\Global\\";       // From OpenVPN - tap-win32/common.h
            const char * SYS_DEVICE_DIR = "\\Device\\";                         // From OpenVPN - tap-win32/common.h
            const char * USER_DEVICE_DIR = "\\DosDevices\\Global\\";            // From OpenVPN - tap-win32/common.h
            const char * TAP_DEVICE_SUFFIX = ".tap";                            // From OpenVPN - tap-win32/common.h

            int iCount = 0;
            std::string sInstanceID, sMACAddr, sIPAddr, sSubnetMask, sDefaultGateway, sMTU;
            NOMADSUtil::RegKeys *pRK = NOMADSUtil::getRegistrySubKeys (WIN32_NETWORK_ADAPTERS_REG_KEY);
            for (int i = 0; i <= pRK->getHighestIndex(); i++) {
                const char *pszSubKey = (*pRK)[i];
                char szKey [MAX_PATH];
                szKey[MAX_PATH-1] = '\0';
                snprintf (szKey, MAX_PATH, "%s\\%s", WIN32_NETWORK_ADAPTERS_REG_KEY, pszSubKey);
                NOMADSUtil::RegEntries *pREs = NOMADSUtil::getRegistryEntries (szKey);
                if (pREs != nullptr) {
                    NOMADSUtil::RegEntry *pEntry = pREs->get ("DriverDesc");
                    if ((pEntry != nullptr) && (pEntry->type == NOMADSUtil::RegEntry::RET_String)) {
                        checkAndLogMsg ("TapInterface::init", NOMADSUtil::Logger::L_Info,
                                        "found network adaptor <%s>\n", pEntry->value);
                        if ((strstr (pEntry->value, TAP_INTERFACE_DESCRIPTION1)) || (strstr (pEntry->value, TAP_INTERFACE_DESCRIPTION2))) {
                            auto * const pREInstanceId = pREs->get ("NetCfgInstanceId");
                            if ((pREInstanceId != nullptr) && (pREInstanceId->type == NOMADSUtil::RegEntry::RET_String)) {
                                iCount++;
                                sInstanceID = pREInstanceId->value;
                                auto * const pREMACAddr = pREs->get ("MAC");
                                if ((pREMACAddr != nullptr) && (pREMACAddr->type == NOMADSUtil::RegEntry::RET_String)) {
                                    sMACAddr = pREMACAddr->value;
                                }
                                auto * const pREMTU = pREs->get ("MTU");
                                if ((pREMTU != nullptr) && (pREMTU->type == NOMADSUtil::RegEntry::RET_String)) {
                                    sMTU = pREMTU->value;
                                }

                                snprintf (szKey, MAX_PATH, "%s\\%s\\Connection", WIN32_NETWORK_ADAPTERS_NAMES_REG_KEY, sInstanceID.c_str());
                                auto * const pNetworkConnEntries = NOMADSUtil::getRegistryEntries (szKey);
                                if (pNetworkConnEntries != nullptr) {
                                    auto * const pREName = pNetworkConnEntries->get ("Name");
                                    if (pREName != nullptr) {
                                        _sUserFriendlyInterfaceName = _sInterfaceName = pREName->value;
                                        checkAndLogMsg ("TapInterface::init", NOMADSUtil::Logger::L_Info,
                                                        "TUN/TAP Network Adaptor name is %s\n", _sInterfaceName.c_str());
                                    }
                                    else {
                                        checkAndLogMsg ("TapInterface::init", NOMADSUtil::Logger::L_Warning,
                                                        "unable to find the TUN/TAP interface name\n");
                                    }
                                }

                                snprintf (szKey, MAX_PATH, "%s\\%s", WIN32_TCPIP_INTERFACE_CONFIGURATION_REG_KEY, sInstanceID.c_str());
                                auto * const pTCPIPEntries = NOMADSUtil::getRegistryEntries (szKey);
                                if (pTCPIPEntries != nullptr) {
                                    // IP address
                                    auto * const pREIPAddr = pTCPIPEntries->get ("IPAddress");
                                    if ((pREIPAddr != nullptr) && (pREIPAddr->type == NOMADSUtil::RegEntry::RET_MultiString)) {
                                        if (pREIPAddr->values.getHighestIndex() < 0) {
                                            checkAndLogMsg ("TapInterface::init", NOMADSUtil::Logger::L_Warning,
                                                            "TUN/TAP Network Adaptor does not have an IP Address\n");
                                        }
                                        else {
                                            sIPAddr = pREIPAddr->values[0];
                                            if (pREIPAddr->values.getHighestIndex() > 0) {
                                                checkAndLogMsg ("TapInterface::init", NOMADSUtil::Logger::L_Warning,
                                                                "TUN/TAP Network Adaptor has more than one IP address - using <%s>\n",
                                                                sIPAddr.c_str());
                                            }
                                        }
                                    }

                                    // Netmask
                                    auto * const pRENetmask = pTCPIPEntries->get ("SubnetMask");
                                    if ((pRENetmask != nullptr) && (pRENetmask->type == NOMADSUtil::RegEntry::RET_MultiString)) {
                                        if (pRENetmask->values.getHighestIndex() < 0) {
                                            checkAndLogMsg ("TapInterface::init", NOMADSUtil::Logger::L_Warning,
                                                            "TUN/TAP Network Adaptor does not have a specified SubnetMask\n");
                                        }
                                        else {
                                            sSubnetMask = pRENetmask->values[0];
                                            if (pRENetmask->values.getHighestIndex() > 0) {
                                                checkAndLogMsg ("TapInterface::init", NOMADSUtil::Logger::L_Warning,
                                                                "TUN/TAP Network Adaptor has more than one SubnetMask - using <%s>\n",
                                                                sSubnetMask.c_str());
                                            }
                                        }
                                    }

                                    // Netmask
                                    auto * const pREGateway = pTCPIPEntries->get ("DefaultGateway");
                                    if ((pREGateway != nullptr) && (pREGateway->type == NOMADSUtil::RegEntry::RET_MultiString)) {
                                        if (pREGateway->values.getHighestIndex() < 0) {
                                            checkAndLogMsg ("TapInterface::init", NOMADSUtil::Logger::L_Warning,
                                                            "TUN/TAP Network Adaptor does not have a specified Default Gateway\n");
                                        }
                                        else {
                                            sDefaultGateway = pRENetmask->values[0];
                                            if (pRENetmask->values.getHighestIndex() > 0) {
                                                checkAndLogMsg ("TapInterface::init", NOMADSUtil::Logger::L_Warning,
                                                                "TUN/TAP Network Adaptor has more than one Default Gateway - using <%s>\n",
                                                                sDefaultGateway.c_str());
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            delete pRK;

            if (iCount == 0) {
                checkAndLogMsg ("TapInterface::init", NOMADSUtil::Logger::L_SevereError,
                                "did not find the TAP-Win32 interface\n");
                return -1;
            }
            else if (iCount > 1) {
                checkAndLogMsg ("TapInterface::init", NOMADSUtil::Logger::L_SevereError,
                                "found %d TAP-Win32 interfaces - do not know which one to use\n", iCount);
                return -2;
            }

            char szDeviceFilePath[MAX_PATH];
            szDeviceFilePath[MAX_PATH-1] = '\0';
            snprintf (szDeviceFilePath, MAX_PATH, "%s%s%s", USER_MODE_DEVICE_DIR_PREFIX, sInstanceID.c_str(), TAP_DEVICE_SUFFIX);

            checkAndLogMsg ("TapInterface::init", NOMADSUtil::Logger::L_Info,
                            "opening device %s\n", szDeviceFilePath);

            _hInterface = CreateFile (szDeviceFilePath, GENERIC_READ | GENERIC_WRITE, 0, nullptr, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, nullptr);
            if (_hInterface == INVALID_HANDLE_VALUE) {
                checkAndLogMsg ("TapInterface::init", NOMADSUtil::Logger::L_SevereError,
                                "CreateFile failed with error %d while attempting to open <%s>\n",
                                GetLastError(), szDeviceFilePath);
                return -3;
            }

            memset (&_oRead, 0, sizeof(_oRead));
            memset (&_oWrite, 0, sizeof(_oWrite));
            _oRead.hEvent = CreateEvent (nullptr, TRUE, FALSE, nullptr);
            if (_oRead.hEvent == INVALID_HANDLE_VALUE) {
                checkAndLogMsg ("TapInterface::init", NOMADSUtil::Logger::L_SevereError,
                                "CreateEvent 1 failed with error %d\n", GetLastError());
                return -4;
            }
            _oWrite.hEvent = CreateEvent (nullptr, TRUE, FALSE, nullptr);
            if (_oWrite.hEvent == INVALID_HANDLE_VALUE) {
                checkAndLogMsg ("TapInterface::init", NOMADSUtil::Logger::L_SevereError,
                                "CreateEvent 2 failed with error %d\n", GetLastError());
                return -5;
            }

            // Process MAC address
            if (sMACAddr.length() > 0) {
                // MAC Address was configured - parse it
                _bMACAddrFound = true;
                auto svMACAddressBytes = splitStringToVector (sMACAddr, ':');
                if (svMACAddressBytes.size() != 6) {
                    _bMACAddrFound = false;
                    checkAndLogMsg("TapInterface::init", NOMADSUtil::Logger::L_Info,
                                    "Error parsing the retrieved MAC address <%s>\n", sMACAddr.c_str());
                }

                for (int i = 0; (i < 6) && _bMACAddrFound; i++) {
                    char pszByte[5];
                    strcpy (pszByte, "0x");
                    strcat (pszByte, svMACAddressBytes[i].c_str());
                    strcat (pszByte, "\0");

                    unsigned int tmp;
                    std::stringstream ss;
                    ss << std::hex << pszByte;
                    ss >> tmp;
                    _aui8MACAddr[i] = static_cast<uint8> (tmp);
                }
            }
Esempio n. 13
0
    int ZLibConnectorReader::receiveTCPDataProxyMessage (const uint8 *const ui8SrcData, uint16 ui16SrcLen, uint8 **pDest, uint32 &ui32DestLen)
    {
        int rc = 0;
        ui32DestLen = 0;
        // Check if the buffer is large enough; if not, reallocate it
        if (_ulInBufSize < (_zsDecompStream.avail_in + ui16SrcLen)) {
            unsigned int uiIncreaseRate = 2;
            while ((uiIncreaseRate * _ulInBufSize) < (_zsDecompStream.avail_in + ui16SrcLen)) {
                uiIncreaseRate *= 2;
            }
            if (NULL == (_pInputBuffer = (unsigned char*) realloc (_pInputBuffer, uiIncreaseRate*_ulInBufSize))) {
                checkAndLogMsg ("ZLibConnectorReader::receiveTCPDataProxyMessage", Logger::L_MildError,
                                "error reallocating memory for _pInputBuffer; impossible to increase size from %u to %u bytes\n",
                                _ulInBufSize, uiIncreaseRate * _ulInBufSize);
                return -1;
            }
            _zsDecompStream.next_in = _pInputBuffer;
            _ulInBufSize = uiIncreaseRate * _ulInBufSize;
        }
        memcpy (_pInputBuffer + _zsDecompStream.avail_in, ui8SrcData, ui16SrcLen);
        _zsDecompStream.avail_in += ui16SrcLen;

        bool bIterate;
        do {
            bIterate = false;
            unsigned int uiOldAvailableBytes = _zsDecompStream.avail_out;
            rc = inflate (&_zsDecompStream, Z_SYNC_FLUSH);
            if ((rc != Z_OK) && (rc != Z_STREAM_END)) {
                checkAndLogMsg ("ZLibConnectorReader::receiveTCPDataProxyMessage", Logger::L_MildError,
                                "error calling inflate() with flag Z_SYNC_FLUSH; returned error code is %d\n", rc);
                return -2;
            }
            ui32DestLen += uiOldAvailableBytes - _zsDecompStream.avail_out;

            if (_zsDecompStream.avail_out == 0) {
                bIterate = true;
                _ulOutBufSize *= 2;
                _pOutputBuffer = (unsigned char *) realloc (_pOutputBuffer, _ulOutBufSize);
            }
            _zsDecompStream.avail_out = _ulOutBufSize - ui32DestLen;
            _zsDecompStream.next_out = _pOutputBuffer + ui32DestLen;
        } while (bIterate);

        if (_zsDecompStream.avail_out < _ulOutBufSize) {
            *pDest = _pOutputBuffer;
        }
        else {
            *pDest = NULL;
            ui32DestLen = 0;
        }

        _zsDecompStream.avail_out = _ulOutBufSize;
        _zsDecompStream.next_out = _pOutputBuffer;
        if (_zsDecompStream.avail_in > 0) {
            // Any useful byte is always at the beginning of the buffer
            memmove (_pInputBuffer, _zsDecompStream.next_in, _zsDecompStream.avail_in);
        }
        _zsDecompStream.next_in = _pInputBuffer;

        return 0;
    }
Esempio n. 14
0
    int LzmaConnectorReader::receiveTCPDataProxyMessage (const uint8 * const ui8SrcData, uint16 ui16SrcLen, uint8 ** pDest, uint32 & ui32DestLen)
    {
        int rc = 0;
        ui32DestLen = 0;
        // Check if the buffer is large enough; if not, reallocate it
        if (_ulInBufSize < (_lzmaDecompStream.avail_in + ui16SrcLen)) {
            unsigned int uiIncreaseRate = 2;
            while ((uiIncreaseRate * _ulInBufSize) < (_lzmaDecompStream.avail_in + ui16SrcLen)) {
                uiIncreaseRate *= 2;
            }
            if (nullptr == (_pInputBuffer = (unsigned char*) realloc (_pInputBuffer, uiIncreaseRate * _ulInBufSize))) {
                checkAndLogMsg ("LzmaConnectorReader::receiveTCPDataProxyMessage", NOMADSUtil::Logger::L_MildError,
                                "error reallocating memory for _pInputBuffer; impossible to increase size from %u to %u bytes\n",
                                _ulInBufSize, uiIncreaseRate*_ulInBufSize);
                return -1;
            }
            _lzmaDecompStream.next_in = _pInputBuffer;
            _ulInBufSize = uiIncreaseRate * _ulInBufSize;
        }
        memcpy (_pInputBuffer + _lzmaDecompStream.avail_in, ui8SrcData, ui16SrcLen);
        _lzmaDecompStream.avail_in += ui16SrcLen;

        bool bIterate;
        do {
            bIterate = false;
            unsigned int uiOldAvailableBytes = _lzmaDecompStream.avail_out;
            rc = lzma_code (&_lzmaDecompStream, LZMA_RUN);
            if (rc > LZMA_STREAM_END) {
                if (rc == LZMA_MEMLIMIT_ERROR) {
                    // doubling memory limit
                    if (LZMA_OK != (rc = lzma_memlimit_set (&_lzmaDecompStream, 2*lzma_memlimit_get (&_lzmaDecompStream)))) {
                        checkAndLogMsg ("LzmaConnectorReader::receiveTCPDataProxyMessage", NOMADSUtil::Logger::L_MildError,
                                        "error calling lzma_memlimit_set() with new memory limit equal to %llu bytes; returned error code is %d\n",
                                        2*lzma_memlimit_get (&_lzmaDecompStream), rc);
                        return -2;
                    }
                    bIterate = true;

                    ui32DestLen += uiOldAvailableBytes - _lzmaDecompStream.avail_out;
                    if ((_lzmaDecompStream.avail_out == 0) && (rc == LZMA_OK)) {
                        _ulOutBufSize *= 2;
                        _pOutputBuffer = (unsigned char *) realloc (_pOutputBuffer, _ulOutBufSize);
                    }
                    _lzmaDecompStream.avail_out = _ulOutBufSize - ui32DestLen;
                    _lzmaDecompStream.next_out = _pOutputBuffer + ui32DestLen;

                    checkAndLogMsg ("LzmaConnectorReader::receiveTCPDataProxyMessage", NOMADSUtil::Logger::L_Warning,
                                    "set a new memory limit of %llu bytes in the LZMA decompressor", lzma_memlimit_get (&_lzmaDecompStream));
                    continue;
                }
                else if ((rc >= LZMA_NO_CHECK) && (rc <= LZMA_GET_CHECK)) {
                    checkAndLogMsg ("LzmaConnectorReader::receiveTCPDataProxyMessage", NOMADSUtil::Logger::L_HighDetailDebug,
                                    "calling to lzma_code() with flag LZMA_RUN returned check code %d; ignoring\n", rc);
                    bIterate = true;
                    continue;
                }
                checkAndLogMsg ("LzmaConnectorReader::receiveTCPDataProxyMessage", NOMADSUtil::Logger::L_MildError,
                                "error calling lzma_code() with flag LZMA_RUN; returned error code is %d\n", rc);
                return -3;
            }
            ui32DestLen += uiOldAvailableBytes - _lzmaDecompStream.avail_out;

            if ((_lzmaDecompStream.avail_out == 0) && (rc == LZMA_OK)) {
                bIterate = true;
                _ulOutBufSize *= 2;
                _pOutputBuffer = (unsigned char *) realloc (_pOutputBuffer, _ulOutBufSize);
            }
            _lzmaDecompStream.avail_out = _ulOutBufSize - ui32DestLen;
            _lzmaDecompStream.next_out = _pOutputBuffer + ui32DestLen;
        } while (bIterate);

        if (_lzmaDecompStream.avail_out < _ulOutBufSize) {
            *pDest = _pOutputBuffer;
        }
        else {
            *pDest = nullptr;
            ui32DestLen = 0;
        }

        _lzmaDecompStream.avail_out = _ulOutBufSize;
        _lzmaDecompStream.next_out = _pOutputBuffer;
        if (_lzmaDecompStream.avail_in > 0) {
            // Any useful byte is always at the beginning of the buffer
            memmove (_pInputBuffer, _lzmaDecompStream.next_in, _lzmaDecompStream.avail_in);
        }
        _lzmaDecompStream.next_in = _pInputBuffer;

        return 0;
    }