TcpClient* TcpServer::Accept() { if (m_listenSocket != INVALID_SOCKET) { char sockAddrData[sizeof(sockaddr_in6)] = { 0 }; int sockAddrData_Size = sizeof(sockAddrData); SOCKET remoteSocket = accept(m_listenSocket, reinterpret_cast<sockaddr*>(sockAddrData), &sockAddrData_Size); if (remoteSocket != INVALID_SOCKET) { SocketEndpoint clientEndpoint; switch (m_localEndpoint.GetAddressFamily()) { case AF_INET: clientEndpoint.Set(*reinterpret_cast<sockaddr_in*>(sockAddrData)); break; case AF_INET6: clientEndpoint.Set(*reinterpret_cast<sockaddr_in6*>(sockAddrData)); break; } return new TcpClient(remoteSocket, clientEndpoint); } } return nullptr; }
bool CDfuPlusHelper::runLocalDaFileSvr(SocketEndpoint &listenep,bool requireauthenticate, unsigned timeout) { Owned<CDafsThread> thr = new CDafsThread(listenep,requireauthenticate); if (!thr->ok()) return false; thr->start(); StringBuffer eps; if (listenep.isNull()) progress("Started local Dali file server on port %d\n", listenep.port?listenep.port:DAFILESRV_PORT); else progress("Started local Dali file server on %s\n", listenep.getUrlStr(eps).str()); if (timeout==0) { setDafsTrace(NULL,0); // disable client tracing dafsthread.setown(thr.getClear()); } else { loop { Sleep(500); if (thr->idleTime()>timeout) { thr->stop(); break; } } } return true; }
ISmartSocket *CSmartSocketFactory::connect_timeout( unsigned timeoutms) { SmartSocketEndpoint *ss = nextSmartEndpoint(); if (!ss) throw createSmartSocketException(0, "smartsocket failed to get nextEndpoint"); ISocket *sock = NULL; SocketEndpoint ep; try { { synchronized block(lock); ss->checkHost(dnsInterval); ep = ss->ep; } if (timeoutms) sock = ISocket::connect_timeout(ep, timeoutms); else sock = ISocket::connect(ep); return new CSmartSocket(sock, ep, this); } catch (IException *e) { StringBuffer s("CSmartSocketFactory::connect "); ep.getUrlStr(s); EXCLOG(e,s.str()); ss->status=false; if (sock) sock->Release(); throw; } }
virtual void main() { running = true; loop { INode *senderNode; CMessageBuffer msg; if (!queryWorldCommunicator().recv(msg, NULL, MPTAG_THORREGISTRATION, &senderNode)) return; rank_t sender = queryClusterGroup().rank(senderNode); SocketEndpoint ep = senderNode->endpoint(); ep.port -= THOR_MP_INC; StringBuffer url; ep.getUrlStr(url); if (RANK_NULL == sender) { PROGLOG("Node %s trying to deregister is not part of this cluster", url.str()); continue; } RegistryCode code; msg.read((int &)code); if (!rc_deregister == code) throwUnexpected(); registry.deregisterNode(sender); } running = false; }
virtual void main() { running = true; loop { INode *senderNode; CMessageBuffer msg; if (!queryWorldCommunicator().recv(msg, NULL, MPTAG_THORREGISTRATION, &senderNode)) return; rank_t sender = queryNodeGroup().rank(senderNode); SocketEndpoint ep = senderNode->endpoint(); StringBuffer url; ep.getUrlStr(url); if (RANK_NULL == sender) { PROGLOG("Node %s trying to deregister is not part of this cluster", url.str()); continue; } RegistryCode code; msg.read((int &)code); if (rc_deregister != code) throwUnexpected(); Owned<IException> e = deserializeException(msg); if (e.get()) EXCLOG(e, "Slave unregistered with exception"); registry.deregisterNode(sender-1); } running = false; }
static void replyError(unsigned errorCode, const char *errorMsg) { SocketEndpoint myEp = queryMyNode()->endpoint(); StringBuffer str("Node '"); myEp.getUrlStr(str); str.append("' exception: ").append(errorMsg); Owned<IException> e = MakeStringException(errorCode, "%s", str.str()); CMessageBuffer msg; serializeException(e, msg); queryWorldCommunicator().send(msg, 0, MPTAG_THORREGISTRATION); }
virtual void init(MemoryBuffer &data, MemoryBuffer &slaveData) override { mpTagRPC = container.queryJobChannel().deserializeMPTag(data); mptag_t barrierTag = container.queryJobChannel().deserializeMPTag(data); barrier.setown(container.queryJobChannel().createBarrier(barrierTag)); portbase = allocPort(NUMSLAVEPORTS); ActPrintLog("MSortSlaveActivity::init portbase = %d, mpTagRPC = %d",portbase,(int)mpTagRPC); server.setLocalHost(portbase); helper = (IHThorSortArg *)queryHelper(); sorter.setown(CreateThorSorter(this, server,&container.queryJob().queryIDiskUsage(),&queryJobChannel().queryJobComm(),mpTagRPC)); server.serialize(slaveData); }
CDafsThread(SocketEndpoint &_listenep,bool requireauthenticate) : listenep(_listenep) { if (listenep.port==0) listenep.port = DAFILESRV_PORT; StringBuffer eps; if (listenep.isNull()) eps.append(listenep.port); else listenep.getUrlStr(eps); enableDafsAuthentication(requireauthenticate); server.setown(createRemoteFileServer()); }
virtual void preStart(size32_t parentExtractSz, const byte *parentExtract) { CMasterActivity::preStart(parentExtractSz, parentExtract); ActPrintLog("preStart"); imaster = CreateThorSorterMaster(this); unsigned s=0; for (; s<container.queryJob().querySlaves(); s++) { SocketEndpoint ep; ep.deserialize(queryInitializationData(s)); // this is a bit of a Kludge until we get proper MP Thor imaster->AddSlave(&container.queryJob().queryJobComm(), s+1, ep,mpTagRPC); } }
bool CDfuPlusHelper::checkLocalDaFileSvr(const char *eps,SocketEndpoint &epout) { if (!eps||!*eps) epout.setLocalHost(DAFILESRV_PORT); else { epout.set(eps,DAFILESRV_PORT); if (!epout.isLocal()) return false; } progress("Checking for local Dali File Server\n"); if (!testDaliServixPresent(epout)) // only lookup local runLocalDaFileSvr(epout,false,0); return true; }
bool UnregisterSelf(IException *e) { if (!hasMPServerStarted()) return false; StringBuffer slfStr; slfEp.getUrlStr(slfStr); LOG(MCdebugProgress, thorJob, "Unregistering slave : %s", slfStr.str()); try { CMessageBuffer msg; msg.append((int)rc_deregister); serializeException(e, msg); // NB: allows exception to be NULL if (!queryWorldCommunicator().send(msg, masterNode, MPTAG_THORREGISTRATION, 60*1000)) { LOG(MCerror, thorJob, "Failed to unregister slave : %s", slfStr.str()); return false; } LOG(MCdebugProgress, thorJob, "Unregistered slave : %s", slfStr.str()); return true; } catch (IException *e) { if (!jobListenerStopped) FLLOG(MCexception(e), thorJob, e,"slave unregistration error"); e->Release(); } return false; }
bool updateDaliEnv(IPropertyTree *env, bool forceGroupUpdate, const char *daliIp) { Owned<IPropertyTreeIterator> dalis = env->getElements("Software/DaliServerProcess/Instance"); if (!dalis||!dalis->first()) { fprintf(stderr,"Could not find DaliServerProcess\n"); return false; } SocketEndpoint daliep; loop { const char *ps = dalis->get().queryProp("@port"); unsigned port = ps?atoi(ps):0; if (!port) port = DALI_SERVER_PORT; daliep.set(dalis->get().queryProp("@netAddress"),port); if (daliIp && *daliIp) { SocketEndpoint testep; testep.set(daliIp,DALI_SERVER_PORT); if (testep.equals(daliep)) break; daliep.set(NULL,0); } if (!dalis->next()) break; if (!daliep.isNull()) { fprintf(stderr,"Ambiguous DaliServerProcess instance\n"); return false; } } if (daliep.isNull()) { fprintf(stderr,"Could not find DaliServerProcess instance\n"); return false; } SocketEndpointArray epa; epa.append(daliep); Owned<IGroup> group = createIGroup(epa); bool ret = true; initClientProcess(group, DCR_Util); StringBuffer response; if (querySDS().updateEnvironment(env, forceGroupUpdate, response)) { StringBuffer tmp; PROGLOG("Environment and node groups updated in dali at %s",daliep.getUrlStr(tmp).str()); } else ret = false; if (response.length()) WARNLOG("%s", response.str()); closedownClientProcess(); return ret; }
void SetServerAddr(SocketEndpoint &endpoint) { free(hostname); StringBuffer ipname; endpoint.getIpText(ipname); hostname = strdup(ipname.str()); hostport = endpoint.port; }
void run() { // Get params from HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\DaFileSrv\Parameters int requireauthenticate=0; HKEY hkey; if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Services\\DaFileSrv\\Parameters", 0, KEY_QUERY_VALUE, &hkey) == ERROR_SUCCESS) { DWORD dwType = 0; DWORD dwSize = sizeof(requireauthenticate); RegQueryValueEx(hkey, "RequireAuthentication", NULL, &dwType, (BYTE*)&requireauthenticate, &dwSize); RegCloseKey(hkey); } StringBuffer eps; if (listenep.isNull()) eps.append(listenep.port); else listenep.getUrlStr(eps); enableDafsAuthentication(requireauthenticate!=0); PROGLOG("Opening " DAFS_SERVICE_DISPLAY_NAME " on %s%s", useSSL?"SECURE ":"",eps.str()); const char * verstring = remoteServerVersionString(); PROGLOG("Version: %s", verstring); PROGLOG("Authentication:%s required",requireauthenticate?"":" not"); PROGLOG(DAFS_SERVICE_DISPLAY_NAME " Running"); server.setown(createRemoteFileServer(maxThreads, maxThreadsDelayMs, maxAsyncCopy)); server->setThrottle(ThrottleStd, parallelRequestLimit, throttleDelayMs, throttleCPULimit); server->setThrottle(ThrottleSlow, parallelSlowRequestLimit, throttleSlowDelayMs, throttleSlowCPULimit); try { server->run(listenep, useSSL); } catch (IException *e) { EXCLOG(e,DAFS_SERVICE_NAME); e->Release(); } PROGLOG(DAFS_SERVICE_DISPLAY_NAME " Stopped"); stopped = true; }
virtual ~CMergeReadStream() { if (stream) { char url[100]; endpoint.getUrlStr(url,sizeof(url)); PrintLog("SORT Merge READ: EOS via destructor for %s",url); stream->stop(); } eos(); }
void update(HeartBeatPacketHeader &packet) { alive = true; if (markdead) { markdead = false; StringBuffer epstr; ep.getUrlStr(epstr); LOG(MCdebugProgress, unknownJob, "Watchdog : Marking Machine as Up! [%s]", epstr.str()); } }
virtual void stop() { if (stream) { #ifdef _FULL_TRACE char url[100]; endpoint.getUrlStr(url,sizeof(url)); PrintLog("SORT Merge READ: stop for %s",url); #endif stream->stop(); eos(); } }
// IThorSlaveActivity virtual void init(MemoryBuffer & data, MemoryBuffer &slaveData) override { if(!isLocal) { mpTagRPC = container.queryJobChannel().deserializeMPTag(data); mptag_t barrierTag = container.queryJobChannel().deserializeMPTag(data); barrier.setown(container.queryJobChannel().createBarrier(barrierTag)); portbase = allocPort(NUMSLAVEPORTS); server.setLocalHost(portbase); sorter.setown(CreateThorSorter(this, server,&container.queryJob().queryIDiskUsage(),&queryJobChannel().queryJobComm(),mpTagRPC)); server.serialize(slaveData); } compare = helper->queryCompareLeft(); // NB not CompareLeftRight keyserializer = helper->querySerializeLeft(); // hopefully never need right if(isLightweight) ActPrintLog("SELFJOIN: LIGHTWEIGHT"); else if(isLocal) ActPrintLog("SELFJOIN: LOCAL"); else ActPrintLog("SELFJOIN: GLOBAL"); }
StringBuffer & CSmartSocketFactory::getUrlStr(StringBuffer &url, bool useHostName) { SmartSocketEndpoint * sep = nextSmartEndpoint(); if (sep) { SocketEndpoint ep; if(useHostName && sep->name.length()) { url.append(sep->name.str()); ep = sep->ep; if (ep.port) url.append(':').append((unsigned)ep.port); } else { sep->checkHost(dnsInterval); SocketEndpoint ep = sep->ep; ep.getUrlStr(url); } } return url; }
bool TcpServer::Start(const SocketEndpoint& localEndpoint, int Backlog) { if (m_listenSocket == INVALID_SOCKET) { m_listenSocket = socket(localEndpoint.GetAddressFamily(), SOCK_STREAM, IPPROTO_TCP); if (m_listenSocket != INVALID_SOCKET) { if (bind(m_listenSocket, localEndpoint.GetIpGeneric(), static_cast<int>(localEndpoint.GetGenericIpSize())) == 0) { if (listen(m_listenSocket, Backlog) == 0) { m_localEndpoint = localEndpoint; return true; } } CleanUp(); } } return false; }
// IThorSlaveActivity virtual void init(MemoryBuffer & data, MemoryBuffer &slaveData) { appendOutputLinked(this); if(!isLocal) { mpTagRPC = container.queryJob().deserializeMPTag(data); mptag_t barrierTag = container.queryJob().deserializeMPTag(data); barrier.setown(container.queryJob().createBarrier(barrierTag)); portbase = allocPort(NUMSLAVEPORTS); SocketEndpoint server; server.setLocalHost(portbase); sorter.setown(CreateThorSorter(this, server,&container.queryJob().queryIDiskUsage(),&container.queryJob().queryJobComm(),mpTagRPC)); server.serialize(slaveData); } helper = static_cast <IHThorJoinArg *> (queryHelper()); compare = helper->queryCompareLeft(); // NB not CompareLeftRight keyserializer = helper->querySerializeLeft(); // hopefully never need right if(isLightweight) ActPrintLog("SELFJOIN: LIGHTWEIGHT"); else if(isLocal) ActPrintLog("SELFJOIN: LOCAL"); else ActPrintLog("SELFJOIN: GLOBAL"); }
void toEp(SocketEndpoint & ep, LPCSTR epTxt) { char * tmp = static_cast <char *> (alloca(strlen(epTxt) + 1)); strcpy(tmp, epTxt); for(int i = strlen(tmp); i >= 0; i--) { if(*(tmp + i) == ':') { ep.port = atoi(tmp + i + 1); *(tmp + i) = 0; ep.ipset(tmp); break; } } }
CMergeReadStream(IRowInterfaces *rowif, unsigned streamno,SocketEndpoint &targetep, rowcount_t startrec, rowcount_t numrecs) { endpoint = targetep; #ifdef _TRACE char url[100]; targetep.getUrlStr(url,sizeof(url)); PrintLog("SORT Merge READ: Stream(%u) %s, pos=%" RCPF "d len=%" RCPF "u",streamno,url,startrec,numrecs); #endif SocketEndpoint mergeep = targetep; mergeep.port+=SOCKETSERVERINC; stream = ConnectMergeRead(streamno,rowif,mergeep,startrec,numrecs); #ifdef _TRACE PrintLog("SORT Merge READ: Stream(%u) connected to %s",streamno,url); #endif }
const void *nextRow() { if (stream) { OwnedConstThorRow row = stream->nextRow(); if (row) return row.getClear(); #ifdef _FULL_TRACE char url[100]; endpoint.getUrlStr(url,sizeof(url)); PrintLog("SORT Merge READ: EOS for %s",url); #endif eos(); } return NULL; }
void UnregisterSelf() { StringBuffer slfStr; slfEp.getUrlStr(slfStr); LOG(MCdebugProgress, thorJob, "Unregistering slave : %s", slfStr.toCharArray()); try { CMessageBuffer msg; msg.append((int)rc_deregister); if (!queryWorldCommunicator().send(msg, masterNode, MPTAG_THORREGISTRATION, 60*1000)) { LOG(MCerror, thorJob, "Failed to unregister slave : %s", slfStr.toCharArray()); return; } LOG(MCdebugProgress, thorJob, "Unregistered slave : %s", slfStr.toCharArray()); } catch (IException *e) { FLLOG(MCexception(e), thorJob, e,"slave unregistration error"); e->Release(); } }
virtual void Bind(SocketEndpoint &endpoint,int qsize) { if (serversock) { serversock->Release(); serversock = NULL; } try { StringBuffer ipname; endpoint.getIpText(ipname); serversock = ISocket::create_ip(endpoint.port,ipname.str(),qsize); } catch (IJSOCK_Exception *e) { int hrpcerr; switch (e->errorCode()) { case JSOCKERR_port_in_use: hrpcerr = HRPCERR_transport_port_in_use; break; default: throw; } THROWHRPCEXCEPTIONEXC(hrpcerr,e); e->Release(); } assertex(serversock); }
int main(int argc, char* argv[]) { InitModuleObjects(); int exitCode = 1; try { if (argc<2) { printf("usage: dalistop <server_ip:port> [/nowait]\n"); printf("eg: dalistop . -- stop dali server running locally\n"); printf(" dalistop eq0001016 -- stop dali server running remotely\n"); } else { SocketEndpoint ep; ep.set(argv[1],DALI_SERVER_PORT); bool nowait = false; if (argc>=3) nowait = stricmp(argv[2],"/nowait")==0; printf("Stopping Dali Server on %s\n",argv[1]); startMPServer(0); Owned<IGroup> group = createIGroup(1,&ep); Owned<ICommunicator> comm = createCommunicator(group); CMessageBuffer mb; int fn=-1; mb.append(fn); if (comm->verifyConnection(0,2000)) { comm->send(mb,0,MPTAG_DALI_COVEN_REQUEST,MP_ASYNC_SEND); if (nowait) { Sleep(1000); exitCode = 0; } else { // verifyConnection() has a min conn timeout of 10s // use recv() instead to check for socket closed ... try { while (!comm->recv(mb,0,MPTAG_DALI_COVEN_REQUEST,nullptr,5000)) { printf("Waiting for Dali Server to stop....\n"); } exitCode = 0; } catch (IMP_Exception *e) { if (e->errorCode() == MPERR_link_closed) exitCode = 0; e->Release(); } } } else fprintf(stderr, "Dali not responding\n"); stopMPServer(); } } catch (IException *e) { pexception("Exception",e); stopMPServer(); } releaseAtoms(); return exitCode; }
void TransferServer::deserializeAction(MemoryBuffer & msg, unsigned action) { SocketEndpoint ep; ep.deserialize(msg); if (!ep.isLocal()) { StringBuffer host, expected; queryHostIP().getIpText(host); ep.getIpText(expected); throwError2(DFTERR_WrongComputer, expected.str(), host.str()); } srcFormat.deserialize(msg); tgtFormat.deserialize(msg); msg.read(calcInputCRC); msg.read(calcOutputCRC); deserialize(partition, msg); msg.read(numParallelSlaves); msg.read(updateFrequency); msg.read(replicate); msg.read(mirror); msg.read(isSafeMode); srand((unsigned)get_cycles_now()); int adjust = (rand() * rand() * rand()) % updateFrequency - (updateFrequency/2); lastTick = msTick() + adjust; StringBuffer localFilename; if (action == FTactionpull) { partition.item(0).outputName.getPath(localFilename); LOG(MCdebugProgress, unknownJob, "Process Pull Command: %s", localFilename.str()); } else { partition.item(0).inputName.getPath(localFilename); LOG(MCdebugProgress, unknownJob, "Process Push Command: %s", localFilename.str()); } LOG(MCdebugProgress, unknownJob, "Num Parallel Slaves=%d Adjust=%d/%d", numParallelSlaves, adjust, updateFrequency); LOG(MCdebugProgress, unknownJob, "replicate(%d) mirror(%d) safe(%d) incrc(%d) outcrc(%d)", replicate, mirror, isSafeMode, calcInputCRC, calcOutputCRC); displayPartition(partition); unsigned numProgress; msg.read(numProgress); for (unsigned i = 0; i < numProgress; i++) { OutputProgress & next = *new OutputProgress; next.deserializeCore(msg); progress.append(next); } if (msg.remaining()) msg.read(throttleNicSpeed); if (msg.remaining()) msg.read(compressedInput).read(compressOutput); if (msg.remaining()) msg.read(copyCompressed); if (msg.remaining()) msg.read(transferBufferSize); if (msg.remaining()) msg.read(encryptKey).read(decryptKey); if (msg.remaining()) { srcFormat.deserializeExtra(msg, 1); tgtFormat.deserializeExtra(msg, 1); } ForEachItemIn(i1, progress) progress.item(i1).deserializeExtra(msg, 1); LOG(MCdebugProgress, unknownJob, "throttle(%d), transferBufferSize(%d)", throttleNicSpeed, transferBufferSize); PROGLOG("compressedInput(%d), compressedOutput(%d), copyCompressed(%d)", compressedInput?1:0, compressOutput?1:0, copyCompressed?1:0); PROGLOG("encrypt(%d), decrypt(%d)", encryptKey.isEmpty()?0:1, decryptKey.isEmpty()?0:1); //---Finished deserializing --- displayProgress(progress); totalLengthRead = 0; totalLengthToRead = 0; ForEachItemIn(idx, partition) totalLengthToRead += partition.item(idx).inputLength; }
virtual int run() { Thread::Link(); int ret = 0; try { char peername[256]; int clientport = m_client->peer_name(peername, 256); char inbuf[1024]; char outbuf[1024]; memset(inbuf, 0, 1024); memset(outbuf, 0, 1024); unsigned int len = 0; unsigned int lenread = 0; m_client->read(inbuf, 8, 8, lenread); if(lenread != 8) { DBGLOG("didn't get the first 8 bytes, invalid socks request."); return -1; } len += lenread; m_client->read(inbuf + len, 0, 1, lenread); StringBuffer username; while(lenread > 0) { len += lenread; if(len >= 1023) { len = 0; } if(inbuf[len - 1] == '\0') { break; } char c = inbuf[len - 1]; username.append(c); m_client->read(inbuf + len, 0, 1, lenread); } if(http_tracelevel >= 5) fprintf(m_ofile, "\n>>receivd SOCKS request from %s:%d, user %s\n", peername, clientport, username.str()); outbuf[0] = '\0'; outbuf[1] = (char)0x5a; m_client->write(outbuf, 8); char ubyte = inbuf[2]; char lbyte = inbuf[3]; unsigned short port = (unsigned short)ubyte; port = port << 8; port += lbyte; // TBD IPV6 (should use serialize/deserialize) IpAddress ip; ip.setNetAddress(4,inbuf+4); StringBuffer ipstr; ip.getIpText(ipstr); if(http_tracelevel >= 5) fprintf(m_ofile, "\n>>The request is for %s:%d\n", ipstr.str(), port); SocketEndpoint ep; ep.set(port, ip); m_remotesocket.setown(ISocket::connect(ep)); m_client->set_nonblock(false); m_remotesocket->set_nonblock(false); CReadWriteThread t1(m_client.get(), m_remotesocket.get(), m_ofile); CReadWriteThread t2(m_remotesocket.get(), m_client.get(), m_ofile); t1.start(); t2.start(); t1.join(); t2.join(); m_remotesocket->shutdown(); m_remotesocket->close(); m_client->shutdown(); m_client->close(); } catch(IException *excpt) { StringBuffer errMsg; DBGLOG("%s", excpt->errorMessage(errMsg).str()); ret = -1; } catch(...) { DBGLOG("unknown exception"); ret = -1; } Thread::Release(); return 0; }
int CHttpProxyThread::run() { Thread::Link(); int ret = 0; try { char peername[256]; int clientport = m_client->peer_name(peername, 256); if(http_tracelevel >= 5) fprintf(m_ofile, "\n>>receivd request from %s:%d\n", peername, clientport); char oneline[2049]; memset(oneline, 0, 2049); bool socketclosed = false; int lenread = readline(m_client.get(), oneline, 2048, socketclosed); if(http_tracelevel >= 10) printf("firstline=%s\n", oneline); if(strncmp(oneline, "CONNECT ", 8) == 0) { char* curptr = oneline + 8; while(*curptr && *curptr == ' ') curptr++; const char* hostptr = curptr; while(*curptr && *curptr != ':' && *curptr != ' ') curptr++; int port = 80; if(*curptr == ':') { *curptr = 0; curptr++; const char* portptr = curptr; while(*curptr && *curptr != ' ') curptr++; *curptr = 0; if(*portptr) port = atoi(portptr); } StringBuffer host(hostptr); while(lenread > 2 && !socketclosed) lenread = readline(m_client.get(), oneline, 2048, socketclosed); SocketEndpoint ep; ep.set(host.str(), port); m_remotesocket.setown(ISocket::connect(ep)); const char* resp = "HTTP/1.0 200 Connection established\r\n" "Proxy-agent: Netscape-Proxy/1.1\r\n\r\n"; m_client->write(resp, strlen(resp)); m_client->set_nonblock(false); m_remotesocket->set_nonblock(false); CReadWriteThread t1(m_client.get(), m_remotesocket.get(), m_ofile); CReadWriteThread t2(m_remotesocket.get(), m_client.get(), m_ofile); t1.start(); t2.start(); t1.join(); t2.join(); //printf("read/write threads returned\n"); m_remotesocket->shutdown(); m_remotesocket->close(); m_client->shutdown(); m_client->close(); } else { const char* http = strstr(oneline, "http://"); if(!http) http = strstr(oneline, "HTTP://"); if(!http) throw MakeStringException(-1, "protocol not recognized\n"); StringBuffer requestbuf; requestbuf.append(http - oneline, oneline); const char* slash = http + 7; while(*slash && *slash != '/' && *slash != ' ' && *slash != '\r') slash++; if(*slash != '/') requestbuf.append('/'); else requestbuf.append(slash); Owned<IByteOutputStream> reqstream = createOutputStream(requestbuf); bool isRoxie; Http::receiveData(m_client, reqstream.get(), false, isRoxie, "Proxy-Connection", NULL, NULL, true); if(http_tracelevel >= 5) fprintf(m_ofile, "Received request from %s\n", peername); if(http_tracelevel >= 10) fprintf(m_ofile, "%s\n", requestbuf.str()); const char* hostname = http + 7; char* ptr = (char*)hostname; while(*ptr && *ptr != ':' && *ptr != '/' && *ptr != ' ') ptr++; int port = 80; if(*ptr == ':') { *ptr = 0; ptr++; const char* portptr = ptr; while(*ptr && *ptr != ' ' && *ptr != '/') ptr++; if(*ptr) *ptr = 0; if(portptr) port = atoi(portptr); } else *ptr = 0; SocketEndpoint ep; ep.set(hostname, port); m_remotesocket.setown(ISocket::connect(ep)); if(http_tracelevel >= 5) fprintf(m_ofile, ">>sending request to %s:%d\n", hostname, port); m_remotesocket->write(requestbuf.str(), requestbuf.length()); StringBuffer respbuf; Owned<CSocketOutputStream> respstream = new CSocketOutputStream(m_client.get()); Http::receiveData(m_remotesocket.get(), respstream.get(), true, isRoxie); if(http_tracelevel >= 5) fprintf(m_ofile, ">>receivd response from %s:%d:\n", hostname, port); if(http_tracelevel >= 5) fprintf(m_ofile, ">>sent response back to %s:%d\n", peername, clientport); fflush(m_ofile); m_remotesocket->shutdown(); m_remotesocket->close(); m_client->shutdown(); m_client->close(); } } catch(IException *excpt) { StringBuffer errMsg; DBGLOG("%s", excpt->errorMessage(errMsg).str()); ret = -1; } catch(...) { DBGLOG("unknown exception"); ret = -1; } Thread::Release(); return 0; }