void StartClient( char const *host_name, char const *service, char const *msg ) { addrinfo hints; addrinfo *addr; memset( &hints, 0, sizeof(hints) ); // Which network layer it's using - usually want to UNSPEC, since it doesn't matter. But since we're hard coding // the client sides connection, we will likely want to use AF_INET when we want to bind an address hints.ai_family = AF_INET; hints.ai_socktype = SOCK_STREAM; // STREAM based, determines transport layer (TCP) int status = getaddrinfo( host_name, service, &hints, &addr ); if (status != 0) { printf( "Failed to create socket address: %s\n", gai_strerror(status) ); return; } addrinfo *iter; char addr_name[INET6_ADDRSTRLEN]; SOCKET host_socket = INVALID_SOCKET; for (iter = addr; iter != nullptr; iter = iter->ai_next) { inet_ntop( iter->ai_family, GetInAddr(iter->ai_addr), addr_name, INET6_ADDRSTRLEN ); printf( "Trying to bind addr: family[%i] type[%i] %s : %s\n", iter->ai_family, iter->ai_socktype, addr_name, service ); host_socket = socket( iter->ai_family, iter->ai_socktype, iter->ai_protocol ); if (host_socket == INVALID_SOCKET) { int error = WSAGetLastError(); printf( "Failed to create socket: Socket Error[%i]\n", error ); continue; } int result = connect( host_socket, iter->ai_addr, iter->ai_addrlen ); if (result == SOCKET_ERROR) { printf( "Failed to connect.\n" ); closesocket( host_socket ); host_socket = INVALID_SOCKET; } else { printf( "Connected!\n" ); break; } } freeaddrinfo(addr); if (host_socket != INVALID_SOCKET) { ClientLoop( host_socket, msg ); closesocket( host_socket ); } }
int SSL_SOCKET :: s_recv(char* b,int sz) { SecPkgContext_StreamSizes Sizes; SECURITY_STATUS ss = 0; ss = QueryContextAttributes(&hCtx,SECPKG_ATTR_STREAM_SIZES,&Sizes); if (FAILED(ss)) return -1; int TotalR = 0; int pI = 0; SecBuffer Buffers[5] = {0}; SecBuffer * pDataBuffer; SecBuffer * pExtraBuffer; Z<char> mmsg(Sizes.cbMaximumMessage*10); if (PendingRecvDataSize) { if (sz <= PendingRecvDataSize) { memcpy(b,PendingRecvData,sz); // Z<char> dj(PendingRecvDataSize); memcpy(dj,PendingRecvData,PendingRecvDataSize); memcpy(PendingRecvData,dj + sz,PendingRecvDataSize - sz); PendingRecvDataSize -= sz; return sz; } // else , occupied already memcpy(b,PendingRecvData,PendingRecvDataSize); sz = PendingRecvDataSize; PendingRecvDataSize = 0; return sz; } for(;;) { unsigned int dwMessage = Sizes.cbMaximumMessage; if (dwMessage > Sizes.cbMaximumMessage) dwMessage = Sizes.cbMaximumMessage; int rval = 0; if (ExtraDataSize) { memcpy(mmsg + pI,ExtraData,ExtraDataSize); pI += ExtraDataSize; ExtraDataSize = 0; } else { rval = recv_p(mmsg + pI,dwMessage); if (rval == 0 || rval == -1) return rval; pI += rval; } Buffers[0].pvBuffer = mmsg; Buffers[0].cbBuffer = pI; Buffers[0].BufferType = SECBUFFER_DATA; Buffers[1].BufferType = SECBUFFER_EMPTY; Buffers[2].BufferType = SECBUFFER_EMPTY; Buffers[3].BufferType = SECBUFFER_EMPTY; sbin.ulVersion = SECBUFFER_VERSION; sbin.pBuffers = Buffers; sbin.cBuffers = 4; ss = DecryptMessage(&hCtx,&sbin,0,NULL); if (ss == SEC_E_INCOMPLETE_MESSAGE) continue; if (ss != SEC_E_OK && ss != SEC_I_RENEGOTIATE && ss != SEC_I_CONTEXT_EXPIRED) return -1; pDataBuffer = NULL; pExtraBuffer = NULL; for (int i = 0; i < 4; i++) { if (pDataBuffer == NULL && Buffers[i].BufferType == SECBUFFER_DATA) { pDataBuffer = &Buffers[i]; } if (pExtraBuffer == NULL && Buffers[i].BufferType == SECBUFFER_EXTRA) { pExtraBuffer = &Buffers[i]; } } if (pExtraBuffer) { ExtraDataSize = pExtraBuffer->cbBuffer; ExtraData.Resize(ExtraDataSize + 10); memcpy(ExtraData,pExtraBuffer->pvBuffer,ExtraDataSize); pI = 0; } if (ss == SEC_I_RENEGOTIATE) { ss = ClientLoop(); if (FAILED(ss)) return -1; } if (pDataBuffer == 0) break; TotalR = pDataBuffer->cbBuffer; if (TotalR <= sz) { memcpy(b,pDataBuffer->pvBuffer,TotalR); } else { TotalR = sz; memcpy(b,pDataBuffer->pvBuffer,TotalR); PendingRecvDataSize = pDataBuffer->cbBuffer - TotalR; PendingRecvData.Resize(PendingRecvDataSize + 100); PendingRecvData.clear(); memcpy(PendingRecvData,(char*)pDataBuffer->pvBuffer + TotalR,PendingRecvDataSize); } break; } return TotalR; }
int main(int argc, char *argv[]) { int sts; int nport = 0; char *envstr; umask(022); __pmGetUsername(&username); __pmSetInternalState(PM_STATE_PMCS); __pmServerSetFeature(PM_SERVER_FEATURE_DISCOVERY); if ((envstr = getenv("PMPROXY_PORT")) != NULL) nport = __pmServerAddPorts(envstr); ParseOptions(argc, argv, &nport); if (nport == 0) __pmServerAddPorts(TO_STRING(PROXY_PORT)); GetProxyHostname(); __pmServerSetServiceSpec(PM_SERVER_PROXY_SPEC); if (run_daemon) { fflush(stderr); StartDaemon(argc, argv); __pmServerCreatePIDFile(PM_SERVER_PROXY_SPEC, 0); } __pmSetSignalHandler(SIGHUP, SIG_IGN); __pmSetSignalHandler(SIGINT, SigIntProc); __pmSetSignalHandler(SIGTERM, SigIntProc); __pmSetSignalHandler(SIGBUS, SigBad); __pmSetSignalHandler(SIGSEGV, SigBad); /* Open request ports for client connections */ if ((sts = __pmServerOpenRequestPorts(&sockFds, MAXPENDING)) < 0) DontStart(); maxReqPortFd = maxSockFd = sts; __pmOpenLog(pmProgname, logfile, stderr, &sts); /* close old stdout, and force stdout into same stream as stderr */ fflush(stdout); close(fileno(stdout)); if (dup(fileno(stderr)) == -1) { fprintf(stderr, "Warning: dup() failed: %s\n", pmErrStr(-oserror())); } fprintf(stderr, "pmproxy: PID = %" FMT_PID, getpid()); fprintf(stderr, ", PDU version = %u\n", PDU_VERSION); __pmServerDumpRequestPorts(stderr); fflush(stderr); /* lose root privileges if we have them */ __pmSetProcessIdentity(username); if (__pmSecureServerSetup(certdb, dbpassfile) < 0) DontStart(); /* all the work is done here */ ClientLoop(); Shutdown(); exit(0); }
int main(int argc, char *argv[]) { int sts; int nport = 0; char *envstr; #ifdef HAVE_SA_SIGINFO static struct sigaction act; #endif umask(022); __pmProcessDataSize(NULL); __pmGetUsername(&username); __pmSetInternalState(PM_STATE_PMCS); __pmServerSetFeature(PM_SERVER_FEATURE_DISCOVERY); __pmServerSetFeature(PM_SERVER_FEATURE_CONTAINERS); if ((envstr = getenv("PMCD_PORT")) != NULL) nport = __pmServerAddPorts(envstr); ParseOptions(argc, argv, &nport); if (nport == 0) __pmServerAddPorts(TO_STRING(SERVER_PORT)); /* Set the local socket path. A message will be generated into the log * if this fails, but it is not fatal, since other connection options * may exist. */ __pmServerSetLocalSocket(sockpath); /* Set the service spec. This will cause our service to be advertised on * the network if that is supported. */ __pmServerSetServiceSpec(PM_SERVER_SERVICE_SPEC); if (run_daemon) { fflush(stderr); StartDaemon(argc, argv); } #ifdef HAVE_SA_SIGINFO act.sa_sigaction = SigIntProc; act.sa_flags = SA_SIGINFO; sigaction(SIGINT, &act, NULL); sigaction(SIGTERM, &act, NULL); #else __pmSetSignalHandler(SIGINT, SigIntProc); __pmSetSignalHandler(SIGTERM, SigIntProc); #endif __pmSetSignalHandler(SIGHUP, SigHupProc); __pmSetSignalHandler(SIGBUS, SigBad); __pmSetSignalHandler(SIGSEGV, SigBad); if ((sts = __pmServerOpenRequestPorts(&clientFds, MAXPENDING)) < 0) DontStart(); maxReqPortFd = maxClientFd = sts; __pmOpenLog(pmProgname, logfile, stderr, &sts); /* close old stdout, and force stdout into same stream as stderr */ fflush(stdout); close(fileno(stdout)); sts = dup(fileno(stderr)); /* if this fails beware of the sky falling in */ assert(sts >= 0); sts = pmLoadASCIINameSpace(pmnsfile, dupok); if (sts < 0) { fprintf(stderr, "Error: pmLoadASCIINameSpace(%s, %d): %s\n", (pmnsfile == PM_NS_DEFAULT) ? "DEFAULT" : pmnsfile, dupok, pmErrStr(sts)); DontStart(); } if (ParseInitAgents(configFileName) < 0) { /* error already reported in ParseInitAgents() */ DontStart(); } if (nAgents <= 0) { fprintf(stderr, "Error: No PMDAs found in the configuration file \"%s\"\n", configFileName); DontStart(); } if (run_daemon) { if (__pmServerCreatePIDFile(PM_SERVER_SERVICE_SPEC, PM_FATAL_ERR) < 0) DontStart(); if (__pmSetProcessIdentity(username) < 0) DontStart(); } if (__pmSecureServerSetup(certdb, dbpassfile) < 0) DontStart(); PrintAgentInfo(stderr); __pmAccDumpLists(stderr); fprintf(stderr, "\npmcd: PID = %" FMT_PID, getpid()); fprintf(stderr, ", PDU version = %u\n", PDU_VERSION); __pmServerDumpRequestPorts(stderr); fflush(stderr); /* all the work is done here */ ClientLoop(); Shutdown(); exit(0); }