int main(int argc, char **argv) { if(argc != REQ_ARG_NUM) { printf("usage: %s [listen port] [destination host] [destination port]\n", argv[0]); exit(0); } int port = atoi(argv[L_PORT_ARG]); int listenSocket = makeListenSocket(port); printf("\nListen Port %d -> %s (%s)\n", port, argv[D_HOST_ARG], argv[D_PORT_ARG]); fflush(stdout); fd_set rfds; FD_ZERO(&rfds); FD_SET(listenSocket, &rfds); RelayList *relayList = NULL; char relayBuf[RELAY_BUF_SIZE]; for(;;) { fd_set rflg = rfds; int ret = select(FD_SETSIZE, &rflg, NULL, NULL, NULL); if(ret < 0) { perror("main:select"); exit(1); } else if(ret > 0) { RelayList **pp = &relayList; while(*pp != NULL) { int relayRes = 0; if(FD_ISSET((*pp)->clientSocket, &rflg)) { relayRes = relaySocket((*pp)->clientSocket, (*pp)->serverSocket, relayBuf, sizeof(relayBuf)); //printf("%s->:%d[byte]\n", (*pp)->clientHost, relayRes); } else if(FD_ISSET((*pp)->serverSocket, &rflg)) { relayRes = relaySocket((*pp)->serverSocket, (*pp)->clientSocket, relayBuf, sizeof(relayBuf)); //printf("%s<-:%d[byte]\n", (*pp)->clientHost, relayRes); } if(relayRes < 0) { deleteRelayNode(pp, &rfds); continue; } pp = &((*pp)->next); } if(FD_ISSET(listenSocket, &rflg)) { int clientSocket = acceptSocket(listenSocket); if(clientSocket>=0) { int serverSocket = makeServerConnection(argv[D_HOST_ARG], atoi(argv[D_PORT_ARG])); if(serverSocket >= 0) { addRelayNode(&relayList, &rfds, clientSocket, serverSocket); } } } } } }
main(int argc, char **argv) { if(argc != REQ_ARG_NUM) { printf("usage: %s [listen port]\n", argv[0]); exit(0); } int port = atoi(argv[L_PORT_ARG]); int listenSocket = makeListenSocket(port); printf("\nListen Port %d -> %s (%s)\n", port, argv[D_HOST_ARG], argv[D_PORT_ARG]); fflush(stdout); fd_set rfds; FD_ZERO(&rfds); FD_SET(listenSocket, &rfds); RcvList *rcvList = NULL; char rcvBuf[RCV_BUF_SIZE]; for(;;) { fd_set rflg = rfds; int ret = select(FD_SETSIZE, &rflg, NULL, NULL, NULL); if(ret < 0) { perror("main:select"); exit(1); } else if(ret > 0) { RcvList **pp = &rcvList; while(*pp != NULL) { int rcvRes = 0; if(FD_ISSET((*pp)->clientSocket, &rflg)) { rcvRes = rcvSocket((*pp)->clientSocket, rcvBuf, sizeof(rcvBuf)); printf("%s->:%d[byte]\n", (*pp)->clientHost, rcvRes); printf("HEX:"); for(int i=0; i < rcvRes; i++) { printf("%02X,",rcvBuf[i]); } printf("\nSTR:"); for(int i=0; i < rcvRes; i++) { printf("%c",rcvBuf[i]); } printf("\n"); } if(rcvRes < 0) { deleteRcvNode(pp, &rfds); continue; } pp = &((*pp)->next); } if(FD_ISSET(listenSocket, &rflg)) { int clientSocket = acceptSocket(listenSocket); if(clientSocket>=0) { addRcvNode(&rcvList, &rfds, clientSocket); } } } } }
bool Beacon::Run(const list<SockAddr> &controlPorts, const list<IpAddr> &listenAddrs) { if (m_scheduler != NULL) { gLog.LogError("Can not call Beacon::Run twice. Aborting."); return false; } if (controlPorts.empty()) { gLog.LogError("At least one control port is required. Aborting."); return false; } RaiiNullBase<CommandProcessorList, closeCommandProcessorList> commandProcessors(new CommandProcessorList); for (list<SockAddr>::const_iterator it = controlPorts.begin(); it != controlPorts.end(); ++it) { CommandProcessor *processor = MakeCommandProcessor(*this); commandProcessors->push_back(processor); if (!processor->BeginListening(*it)) { gLog.LogError("Failed to start command processing thread on %s. Aborting.", it->ToString()); return false; } } #ifdef USE_KEVENT_SCHEDULER m_scheduler = new KeventScheduler(); #else m_scheduler = new SelectScheduler(); #endif m_packet.AllocBuffers(bfd::MaxPacketSize, Socket::GetMaxControlSizeReceiveDestinationAddress() + Socket::GetMaxControlSizeRecieveTTLOrHops() + +8 /*just in case*/); // We use this "signal channel" to communicate back to ourself in the Scheduler // thread. if (!m_scheduler->CreateSignalChannel(&m_selfSignalId, handleSelfMessageCallback, this)) { gLog.LogError("Failed to create self signal handling. Aborting."); return false; } RaiiNullBase<ListenCallbackDataList, closeListenCallbackDataList> callbackData(new ListenCallbackDataList); for (list<IpAddr>::const_iterator it = listenAddrs.begin(); it != listenAddrs.end(); ++it) { ListenCallbackData *data = new ListenCallbackData; data->beacon = this; callbackData->push_back(data); makeListenSocket(*it, data->socket); if (data->socket.empty()) { gLog.LogError("Failed to create listen socket for %s on BFD port %hd.", it->ToString(), bfd::ListenPort); return false; } if (!m_scheduler->SetSocketCallback(data->socket, handleListenSocketCallback, data)) { gLog.LogError("Failed to set m_scheduler socket processing for %s. Aborting.", it->ToString()); return false; } } bool returnVal = false; if (!m_scheduler->Run()) gLog.LogError("Failed to start m_scheduler. Aborting."); else returnVal = true; commandProcessors.Dispose(); // In theory we should not be using m_scheduler except on the scheduler // callbacks, which end when Scheduler::Run() ends Scheduler *oldScheduler = m_scheduler; m_scheduler = NULL; delete oldScheduler; return returnVal; }