| [a0528c4] | 1 | #include "stdafx.h" |
|---|
| 2 | #include "strEx.h" |
|---|
| 3 | #include "NSParser.h" |
|---|
| 4 | #include "NSClient++.h" |
|---|
| 5 | #include "TCPSocketResponder.h" |
|---|
| [8cf56a5] | 6 | #include "Settings.h" |
|---|
| [a0528c4] | 7 | |
|---|
| 8 | extern NSClient mainClient; |
|---|
| 9 | |
|---|
| 10 | /** |
|---|
| 11 | * Default c-tor |
|---|
| 12 | */ |
|---|
| 13 | TCPSocketResponder::TCPSocketResponder() : running_(false) { |
|---|
| 14 | } |
|---|
| 15 | /** |
|---|
| 16 | * Return true if the socket is "running". |
|---|
| 17 | * @return true if the socket is spoused to be running |
|---|
| 18 | */ |
|---|
| 19 | bool TCPSocketResponder::isRunning(void) { |
|---|
| 20 | MutexLock mutex(mutexHandler); |
|---|
| 21 | if (!mutex.hasMutex()) { |
|---|
| 22 | LOG_ERROR_STD("Failed to get Mutex: " + strEx::itos(mutex.getWaitResult())); |
|---|
| 23 | return false; |
|---|
| 24 | } |
|---|
| 25 | return running_; |
|---|
| 26 | } |
|---|
| 27 | /** |
|---|
| 28 | * Set the running flag to "stop" (will prevent the socket to start waiting again) |
|---|
| 29 | */ |
|---|
| 30 | void TCPSocketResponder::stopRunning(void) { |
|---|
| 31 | MutexLock mutex(mutexHandler); |
|---|
| 32 | if (!mutex.hasMutex()) { |
|---|
| 33 | LOG_ERROR_STD("Failed to get Mutex!" + strEx::itos(mutex.getWaitResult())); |
|---|
| 34 | return; |
|---|
| 35 | } |
|---|
| 36 | running_ = false; |
|---|
| 37 | } |
|---|
| 38 | /** |
|---|
| 39 | * Set the running flag to "start" (will make the socket wait again) |
|---|
| 40 | */ |
|---|
| 41 | void TCPSocketResponder::startRunning(void) { |
|---|
| 42 | MutexLock mutex(mutexHandler); |
|---|
| 43 | if (!mutex.hasMutex()) { |
|---|
| 44 | LOG_ERROR_STD("Failed to get Mutex!" + strEx::itos(mutex.getWaitResult())); |
|---|
| 45 | return; |
|---|
| 46 | } |
|---|
| 47 | running_ = true; |
|---|
| 48 | } |
|---|
| 49 | /** |
|---|
| 50 | * Parse a command string into command, password and arguments. Then the command will be executed bu the core. |
|---|
| 51 | * @param request A request string to parse |
|---|
| 52 | * @return The result (if any) of the executed command. |
|---|
| 53 | */ |
|---|
| 54 | std::string TCPSocketResponder::parseCommand(char* request) { |
|---|
| 55 | std::list<std::string> args = charEx::split(request, '&'); |
|---|
| 56 | if (args.size() < 2) { |
|---|
| 57 | return "Insufficient arguments!"; |
|---|
| 58 | } |
|---|
| 59 | std::string password = args.front(); args.pop_front(); |
|---|
| 60 | std::string command = args.front(); args.pop_front(); |
|---|
| 61 | return mainClient.execute(password, command, args); |
|---|
| 62 | } |
|---|
| 63 | /** |
|---|
| 64 | * Thread procedure for the socket listener |
|---|
| 65 | * @param lpParameter Potential argument to the thread proc. |
|---|
| 66 | * @return thread exit status |
|---|
| 67 | * @todo This needs to be reworked, possibly completely redone ? |
|---|
| 68 | */ |
|---|
| 69 | DWORD TCPSocketResponder::threadProc(LPVOID lpParameter) |
|---|
| 70 | { |
|---|
| 71 | startRunning(); |
|---|
| 72 | WSADATA wsaData; |
|---|
| 73 | sockaddr_in local; |
|---|
| 74 | int wsaret=WSAStartup(0x101,&wsaData); |
|---|
| 75 | if(wsaret!=0) { |
|---|
| 76 | LOG_ERROR_STD("WSA Startup failed: " + strEx::itos(wsaret)); |
|---|
| 77 | return 0; |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | local.sin_family=AF_INET; |
|---|
| 81 | local.sin_addr.s_addr=INADDR_ANY; |
|---|
| [8cf56a5] | 82 | local.sin_port=htons(static_cast<u_short>(Settings::getInstance()->getInt("generic", "port", DEFAULT_TCP_PORT))); |
|---|
| [a0528c4] | 83 | server=socket(AF_INET,SOCK_STREAM,0); |
|---|
| 84 | if(server==INVALID_SOCKET) { |
|---|
| 85 | LOG_ERROR_STD("Could not create listening socket: " + strEx::itos(GetLastError())); |
|---|
| 86 | return 0; |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | if(bind(server,(sockaddr*)&local,sizeof(local))!=0) { |
|---|
| 90 | LOG_ERROR_STD("Could not bind socket: " + strEx::itos(GetLastError())); |
|---|
| 91 | return 0; |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | if(listen(server,10)!=0) { |
|---|
| 95 | LOG_ERROR_STD("Could not open socket: " + strEx::itos(GetLastError())); |
|---|
| 96 | return 0; |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | SOCKET client; |
|---|
| 100 | sockaddr_in from; |
|---|
| 101 | int fromlen=sizeof(from); |
|---|
| 102 | |
|---|
| 103 | while(isRunning()) { |
|---|
| 104 | client=accept(server, (struct sockaddr*)&from,&fromlen); |
|---|
| [8cf56a5] | 105 | if (client != INVALID_SOCKET) { |
|---|
| 106 | char *buff = new char[RECV_BUFFER_LEN+1]; |
|---|
| 107 | int n=recv(client,buff,RECV_BUFFER_LEN,0); |
|---|
| 108 | if ((n!=SOCKET_ERROR )&&(n > 0)&&(n < RECV_BUFFER_LEN)) { |
|---|
| 109 | buff[n] = '\0'; |
|---|
| 110 | LOG_DEBUG("Incoming data: "); |
|---|
| 111 | LOG_DEBUG(buff); |
|---|
| 112 | std::string ret = parseCommand(buff); |
|---|
| 113 | LOG_DEBUG("Outgoing data: "); |
|---|
| 114 | LOG_DEBUG(ret.c_str()); |
|---|
| 115 | send(client, ret.c_str(), ret.length(), 0); |
|---|
| 116 | } else { |
|---|
| 117 | std::string str = "ERROR: Unknown socket error"; |
|---|
| 118 | send(client,str.c_str(),str.length(),0); |
|---|
| 119 | } |
|---|
| 120 | delete [] buff; |
|---|
| 121 | closesocket(client); |
|---|
| [a0528c4] | 122 | } |
|---|
| 123 | } |
|---|
| 124 | closesocket(server); |
|---|
| 125 | WSACleanup(); |
|---|
| 126 | LOG_DEBUG("Socket closed!"); |
|---|
| 127 | return 0; |
|---|
| 128 | } |
|---|
| 129 | |
|---|
| 130 | /** |
|---|
| 131 | * Exit thread callback proc. |
|---|
| 132 | * This is called by the thread manager when the thread should initiate a shutdown procedure. |
|---|
| 133 | * The thread manager is responsible for waiting for the actual termination of the thread. |
|---|
| 134 | */ |
|---|
| 135 | void TCPSocketResponder::exitThread(void) { |
|---|
| 136 | LOG_DEBUG("Requesting shutdown!"); |
|---|
| 137 | stopRunning(); |
|---|
| 138 | closesocket(server); |
|---|
| 139 | } |
|---|