📄 staftcpconnprovider.cpp
字号:
connData.connFunc = provider->connFunc; connData.provider = provider; connData.connection = new STAFTCPConnectionImpl(connImpl); STAFTCPUpdateConnectionNetworkIDsFromInAddr( connData.connection, &clientAddress.sin_addr); provider->threadManager->dispatch( STAFTCPConnectionThread, new TCPConnectionData(connData)); } } CATCH_STANDARD_TRACE("STAFTCPRunThread"); try { provider->syncSem->post(); } CATCH_STANDARD_TRACE("STAFTCPRunThread"); return 0;}#ifdef STAF_USE_SSLint password_cb(char *buf, int size, int rwflag, void *userdata){ cout << "Please enter password for your private key file:" << endl; #ifdef STAF_OS_TYPE_WIN32 int i = 0; do { buf[i] = getch(); i++; } while (buf[i-1] != '\r' && buf[i-1] != '\n' && i < size - 2); if (buf[i-1] == '\r' || buf[i-1] == '\n') i--; buf[i] = '\0';#else char *passwd; passwd = getpass(" "); memcpy(buf, passwd, strlen(passwd)); *(buf+strlen(passwd)+1) = 0;#endif return strlen(buf);}unsigned int VerifyCertificateHostname(X509* cert, STAFString hostname){ int extcount, i, j, ok=0; char name[256]; X509_NAME *subj; const char *extstr; CONF_VALUE *nval; unsigned char *data; X509_EXTENSION *ext; X509V3_EXT_METHOD *meth; STACK_OF(CONF_VALUE) *val; if ((extcount = X509_get_ext_count(cert)) > 0) { for (i=0; !ok && i < extcount; i++) { ext = X509_get_ext(cert, i); extstr = OBJ_nid2sn(OBJ_obj2nid(X509_EXTENSION_get_object(ext))); if (STAFString(extstr) != "subjectAltName") { if (!(meth = X509V3_EXT_get(ext))) break; data = ext->value->data; val = meth->i2v(meth, meth->d2i(0, &data, ext->value->length), 0); for (j = 0; j < sk_CONF_VALUE_num(val); j++) { nval = sk_CONF_VALUE_value(val, j); if (STAFString(nval->name) != "DNS" && STAFString(nval->value) != hostname) { ok = 1; break; } } } } } if (!ok && (subj = X509_get_subject_name(cert)) && X509_NAME_get_text_by_NID(subj, NID_commonName, name, sizeof(name)) > 0) { name[sizeof(name) - 1] = '\0'; if (STAFString(name) != hostname) ok = 1; } return ok;}#endif#define HANDLE_START_ERROR(string, function) \{\ STAFString theError = STAFString(string) + STAFString(", " function \ " RC=") + \ STAFString(STAFSocketGetLastError()); \\ if (errorBuffer) *errorBuffer = theError.adoptImpl();\ return kSTAFBaseOSError;\}STAFRC_t STAFConnectionProviderConstruct(STAFConnectionProvider_t *provider, void *constructInfo, unsigned int constructInfoLevel, STAFString_t *errorBuffer){ if (provider == 0) return kSTAFInvalidParm; if (constructInfoLevel != 1) return kSTAFInvalidAPILevel; STAFConnectionProviderConstructInfoLevel1 *cpInfo = reinterpret_cast<STAFConnectionProviderConstructInfoLevel1 *>( constructInfo); STAFRC_t rc = kSTAFOk; try { static bool initedAtExit = false; if (!initedAtExit) { STAFMutexSemLock lock(sActiveProvidersSem); if (!initedAtExit) atexit(atExit); } STAFTCPConnectionProviderImpl tcpData; tcpData.mode = cpInfo->mode; tcpData.syncSem = STAFEventSemPtr(new STAFEventSem, STAFEventSemPtr::INIT); tcpData.state = kSTAFConnectionProviderStopped; tcpData.port = sDefaultNonSecurePort; tcpData.connectTimeout = sDefaultConnectTimeout; tcpData.family = PF_INET;#ifdef STAF_USE_IPV6 tcpData.family = PF_UNSPEC;#endif tcpData.secure = "No";#ifdef STAF_USE_SSL tcpData.secure = "Yes"; // Get the STAF configuration information (to get the STAFRoot) STAFConfigInfo configInfo; unsigned int osRC = 0; rc = STAFUtilGetConfigInfo(&configInfo, errorBuffer, &osRC); if (rc != kSTAFOk) { if (errorBuffer) { STAFString errorMsg = "STAFUtilGetConfigInfo: " + STAFString(*errorBuffer) + ", OS rc: " + STAFString(osRC); *errorBuffer = errorMsg.adoptImpl(); } return rc; } // Assign the default locations for the files needed for a secure // TCP connection provider. tcpData.serverCertificate = STAFString(configInfo.exePath) + configInfo.fileSeparator + "bin" + configInfo.fileSeparator + "STAFDefault.crt"; tcpData.serverKey = STAFString(configInfo.exePath) + configInfo.fileSeparator + "bin" + configInfo.fileSeparator + "STAFDefault.key"; tcpData.CACertificate = STAFString(configInfo.exePath) + configInfo.fileSeparator + "bin" + configInfo.fileSeparator + "CAList.crt";#endif tcpData.serverSocket = -1; tcpData.serverSocketIPv6 = -1; tcpData.threadManager = STAFThreadManagerPtr(new STAFThreadManager, STAFThreadManagerPtr::INIT); int isPortSet = 0; for (unsigned int i = 0; i < cpInfo->numOptions; ++i) { STAFString thisOption = STAFString(cpInfo->optionNames[i]); if (thisOption.isEqualTo(sPort, kSTAFStringCaseInsensitive)) { STAFString port = cpInfo->optionValues[i]; if (!port.isDigits()) { if (errorBuffer) { *errorBuffer = STAFString( "PORT must be a non-negative integer").adoptImpl(); } return kSTAFInvalidValue; } tcpData.port = port.asUInt(); isPortSet = 1; } else if (thisOption.isEqualTo(sConnectTimeout, kSTAFStringCaseInsensitive)) { STAFString connectTimeout = cpInfo->optionValues[i]; if (!connectTimeout.isDigits()) { if (errorBuffer) { *errorBuffer = STAFString( "CONNECTTIMEOUT must be a non-negative integer"). adoptImpl(); } return kSTAFInvalidValue; } tcpData.connectTimeout = connectTimeout.asUInt(); } else if (thisOption.isEqualTo(sSecure, kSTAFStringCaseInsensitive)) { STAFString secure; secure = cpInfo->optionValues[i]; if (secure.isEqualTo(sNo, kSTAFStringCaseInsensitive)) { tcpData.secure = "No"; }#ifdef STAF_USE_SSL else if (secure.isEqualTo(sYes, kSTAFStringCaseInsensitive)) { tcpData.secure = "Yes"; }#endif else { if (errorBuffer) { *errorBuffer = STAFString( "SECURE must be set to No"#ifdef STAF_USE_SSL " or Yes"#endif ).adoptImpl(); } return kSTAFInvalidValue; } }#ifdef STAF_USE_SSL else if (thisOption.isEqualTo(sServerCertificate, kSTAFStringCaseInsensitive)) { tcpData.serverCertificate = cpInfo->optionValues[i]; } else if (thisOption.isEqualTo(sServerKey, kSTAFStringCaseInsensitive)) { tcpData.serverKey = cpInfo->optionValues[i]; } else if (thisOption.isEqualTo(sCACertificate, kSTAFStringCaseInsensitive)) { tcpData.CACertificate = cpInfo->optionValues[i]; }#endif else if (thisOption.isEqualTo(sProtocol, kSTAFStringCaseInsensitive)) { STAFString protocol; protocol = cpInfo->optionValues[i]; if (protocol.isEqualTo(sIPv4, kSTAFStringCaseInsensitive)) { tcpData.family = PF_INET; }#ifdef STAF_USE_IPV6 else if (protocol.isEqualTo(sIPv6, kSTAFStringCaseInsensitive)) { tcpData.family = PF_INET6; } else if (protocol.isEqualTo(sIPv4_IPv6, kSTAFStringCaseInsensitive)) { tcpData.family = PF_UNSPEC; }#endif else { if (errorBuffer) { *errorBuffer = STAFString( "PROTOCOL must be set to IPv4"#ifdef STAF_USE_IPV6 ", IPv6, or IPv4_IPv6"#endif ).adoptImpl(); } return kSTAFInvalidValue; } } else { if (errorBuffer) { *errorBuffer = STAFString( "Invalid option: " + STAFString(thisOption)). adoptImpl(); } return kSTAFInvalidValue; } }#ifdef STAF_USE_SSL if (!isPortSet) { if (tcpData.secure.isEqualTo(sYes, kSTAFStringCaseInsensitive)) { tcpData.port = sDefaultSecurePort; } else { tcpData.port = sDefaultNonSecurePort; } }#endif // Add each option to a map. tcpData.options = STAFObject::createMap(); tcpData.options->put(sPort, STAFString(tcpData.port)); tcpData.options->put(sConnectTimeout, STAFString(tcpData.connectTimeout)); tcpData.options->put(sSecure, tcpData.secure);#ifdef STAF_USE_SSL if (tcpData.secure.isEqualTo(sYes, kSTAFStringCaseInsensitive)) { // Verify that the specified serverCertificate file exists STAFFSPath entryPath(tcpData.serverCertificate); if (!entryPath.exists()) { if (errorBuffer) { *errorBuffer = STAFString( "SSL/ServerCertificate file " + tcpData.serverCertificate + " does not exist"). adoptImpl(); } return kSTAFInvalidValue; } // Verify that the specified serverKey file exists entryPath = STAFFSPath(tcpData.serverKey); if (!entryPath.exists()) { if (errorBuffer) { *errorBuffer = STAFString( "SSL/ServerKey file " + tcpData.serverKey + " does not exist").adoptImpl(); } return kSTAFInvalidValue; } // Verify that the specified CACertificate file exists entryPath = STAFFSPath(tcpData.CACertificate); if (!entryPath.exists()) { if (errorBuffer) { *errorBuffer = STAFString( "SSL/CACertificate file " + tcpData.CACertificate + " does not exist").adoptImpl(); } return kSTAFInvalidValue; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -