You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

402 lines
14 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "netServer.h"
  2. #include "../../App/3dSERVER/LRF_ClientHandler.h"
  3. #define LOG( X ) m_pLog->Log( "networkServer", this, X )
  4. #define LOG_ERROR( X ) m_pLog->Log( "networkServer", this, _LOG_ERROR, X )
  5. void signal_void_handler( int SIG ){
  6. };
  7. networkServer::networkServer( config_setting_t *Settings ) : basicThread(){
  8. m_pLog = basicLogCollection::getInstance();
  9. m_bConnectionType = 0;
  10. m_vHandlersPool.clear();
  11. MainSocketAddress = NULL;
  12. if( Settings != NULL ) {
  13. m_pSettings = Settings;
  14. } else {
  15. m_pSettings = basicSettingsProvider::getInstance()->getSubNode( NULL, "NETWORK_SERVER" );
  16. };
  17. };
  18. networkServer::~networkServer(){
  19. m_bThreadStatus = TH_ENDED;
  20. if( m_pHostName != NULL ) free( m_pHostName );
  21. };
  22. bool networkServer::BootUp( void ){
  23. config_setting_t *pSetting;
  24. int tVal;
  25. char tBuffer[CTEXT];
  26. m_pHostName = NULL;
  27. if( m_pSettings != NULL ) {
  28. LOG("NetServer Getting Settings ...");
  29. if( !config_setting_lookup_string( m_pSettings, NWS_SETTINGS_HOST, (const char**)&m_pHostName ) ){
  30. m_pHostName = (CSTR)malloc(CTEXT);
  31. sprintf( m_pHostName, "localhost" );
  32. pSetting = config_setting_add( m_pSettings, NWS_SETTINGS_HOST, CONFIG_TYPE_STRING );
  33. config_setting_set_string( pSetting, m_pHostName );
  34. basicSettingsProvider::getInstance()->compromise();
  35. LOG("No Host defined");
  36. } else {
  37. sprintf( tBuffer, "1 Read From Settings: %s", m_pHostName );
  38. LOG( tBuffer );
  39. };
  40. if (!config_setting_lookup_int( m_pSettings, NWS_SETTINGS_PORT, &tVal )){
  41. m_nPortNumber = 2222;
  42. pSetting = config_setting_add( m_pSettings, NWS_SETTINGS_PORT, CONFIG_TYPE_INT);
  43. config_setting_set_int(pSetting, m_nPortNumber );
  44. basicSettingsProvider::getInstance()->compromise();
  45. LOG("No Port defined");
  46. } else {
  47. m_nPortNumber = tVal;
  48. sprintf( tBuffer, "2 Read From Settings: %d", m_nPortNumber );
  49. LOG( tBuffer );
  50. };
  51. if( basicSettingsProvider::getInstance()->isCompromised() ) basicSettingsProvider::getInstance()->validate();
  52. return BootUp( m_pHostName, m_nPortNumber );
  53. } else {
  54. // Last try to bootup by detecting host and default port 2222
  55. return BootUp( 2222 );
  56. };
  57. };
  58. bool networkServer::BootUp( CSTR HOST_NAME, U16 PORT ){
  59. char temp[CTEXT];
  60. int RetCode;
  61. #ifdef __linux__
  62. struct hostent *h_info;
  63. LOG( "Core BootUp" );
  64. MainSocket = socket( AF_INET, SOCK_STREAM, 0 );
  65. if( MainSocket == -1){
  66. LOG_ERROR( "Some throuble with networking on this machine (Socket Error)!");
  67. return false;
  68. };
  69. MainSocketAddress = NULL;
  70. MainSocketAddress = ( struct sockaddr * ) malloc( sizeof( struct sockaddr_in ) );
  71. if( MainSocketAddress == NULL ){
  72. LOG_ERROR( "Some throuble with networking on this machine (Cannot Alloc Sockaddr Struct)!" );
  73. return false;
  74. };
  75. bzero( MainSocketAddress, sizeof( struct sockaddr_in ) );
  76. h_info = gethostbyname ( HOST_NAME );
  77. if ( h_info == NULL ){
  78. LOG_ERROR( "Some throuble with networking on this machine (Cannot Get Hostname)!" );
  79. return false;
  80. };
  81. //*****************************************
  82. //* there we building our network address *
  83. //*****************************************
  84. bcopy( h_info->h_addr, &(((struct sockaddr_in *)MainSocketAddress)->sin_addr), h_info->h_length );
  85. ((struct sockaddr_in *)MainSocketAddress)->sin_family = h_info->h_addrtype;
  86. ((struct sockaddr_in *)MainSocketAddress)->sin_port = htons( PORT );
  87. RetCode = bind( MainSocket , MainSocketAddress, sizeof( struct sockaddr_in ) );
  88. if ( RetCode != 0 ){
  89. LOG_ERROR( "Error Binding to Port/Address" );
  90. return false;
  91. };
  92. RetCode = listen( MainSocket, 128);
  93. if ( RetCode != 0 ){
  94. LOG_ERROR( "Cannot Listen Port/Address" );
  95. return false;
  96. };
  97. U8 dontblock;
  98. dontblock = fcntl( MainSocket, F_GETFL, NULL );
  99. dontblock |= O_NONBLOCK | O_NDELAY;
  100. RetCode = ioctl( MainSocket, FIONBIO, (char *) &dontblock );
  101. if ( RetCode == 0 ){
  102. LOG( "Set N-Blocking Mode of Accepting Socket" );
  103. } else {
  104. LOG( "Can't set N-Blocking Mode of Accepting Socket" );
  105. };
  106. #else
  107. WSADATA wsaData;
  108. int iResult;
  109. // Initialize Winsock
  110. iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
  111. if( iResult != 0 ){
  112. sprintf(temp, "WSAStartup failed with error: %d\n", iResult);
  113. LOG_ERROR(temp);
  114. return false;
  115. };
  116. MainSocket = INVALID_SOCKET;
  117. SOCKET ClientSocket = INVALID_SOCKET;
  118. struct addrinfo *result = NULL;
  119. struct addrinfo hints;
  120. int iSendResult;
  121. char recvbuf[512];
  122. int recvbuflen = 512;
  123. ZeroMemory(&hints, sizeof(hints));
  124. hints.ai_family = AF_INET;
  125. hints.ai_socktype = SOCK_STREAM;
  126. hints.ai_protocol = IPPROTO_TCP;
  127. hints.ai_flags = AI_PASSIVE;
  128. // Resolve the server address and port
  129. sprintf( temp, "%d", PORT );
  130. iResult = getaddrinfo(NULL, temp, &hints, &result);
  131. if (iResult != 0) {
  132. sprintf( temp, "getaddrinfo failed with error: %d\n", iResult );
  133. LOG_ERROR(temp);
  134. WSACleanup();
  135. return false;
  136. };
  137. // Create a SOCKET for connecting to server
  138. MainSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
  139. if (MainSocket == INVALID_SOCKET) {
  140. sprintf( temp, "socket failed with error: %ld\n", WSAGetLastError() );
  141. LOG_ERROR(temp);
  142. freeaddrinfo(result);
  143. WSACleanup();
  144. return false;
  145. };
  146. // Setup the TCP listening socket
  147. iResult = bind(MainSocket, result->ai_addr, (int)result->ai_addrlen);
  148. if (iResult == SOCKET_ERROR) {
  149. sprintf( temp, "bind failed with error: %d\n", WSAGetLastError());
  150. LOG_ERROR(temp);
  151. freeaddrinfo(result);
  152. closesocket(MainSocket);
  153. WSACleanup();
  154. return false;
  155. };
  156. freeaddrinfo(result);
  157. iResult = listen(MainSocket, SOMAXCONN);
  158. if (iResult == SOCKET_ERROR) {
  159. sprintf( temp, "listen failed with error: %d\n", WSAGetLastError());
  160. LOG_ERROR(temp);
  161. closesocket(MainSocket);
  162. WSACleanup();
  163. return false;
  164. };
  165. #endif
  166. m_bConnectionType = 1;
  167. return init();
  168. };
  169. bool networkServer::BootUp( U16 Port ){
  170. char temp[CTEXT];
  171. m_nPortNumber = Port;
  172. #ifdef __linux__
  173. int RetCode;
  174. size_t S_NAMLEN = CTEXT;
  175. RetCode = gethostname( m_pHostName, S_NAMLEN );
  176. sprintf( temp, "Running on host: %s:%d", m_pHostName, m_nPortNumber );
  177. LOG( temp );
  178. #else
  179. sprintf( m_pHostName, "" );
  180. #endif
  181. return BootUp( m_pHostName, m_nPortNumber );
  182. };
  183. bool networkServer::attachEngine( basicEngine *ptr ){
  184. m_pDetectorKernel = ptr;
  185. if( m_vHandlersPool.size()) {
  186. // Pass Kernel to all already connected nodes
  187. for( vectorNetHandlers::iterator index = m_vHandlersPool.begin(); index < m_vHandlersPool.end(); index++ ) {
  188. ((LRF_ClientHandler *)(*index))->attachEngine(m_pDetectorKernel);
  189. };
  190. };
  191. return true;
  192. };
  193. #ifdef __linux__
  194. bool networkServer::BootUp(CSTR SOCKET_NAME){
  195. struct sockaddr_un MainPort;
  196. size_t S_NAMLEN = CTEXT;
  197. int RetCode;
  198. char localhost[S_NAMLEN];
  199. char temp[CTEXT];
  200. LOG( "Core BootUp" );
  201. MainSocket = socket( PF_UNIX, SOCK_STREAM, 0 );
  202. if( MainSocket == -1){
  203. LOG_ERROR( "Some throuble with Kernel on this machine (UnixSocket Error)!" );
  204. return false;
  205. };
  206. if( unlink( SOCKET_NAME ) == -1 ){
  207. LOG( "Old Socket File was not deleted!" );
  208. };
  209. localhost[0] = 0;
  210. //RetCode = gethostname( localhost, S_NAMLEN );
  211. sprintf( temp, "Running Unix Socket: %s::%s", localhost, SOCKET_NAME );
  212. LOG( temp );
  213. MainSocketAddress = ( sockaddr *) malloc( sizeof( struct sockaddr_un ) );
  214. bzero( MainSocketAddress, sizeof( struct sockaddr_un ) );
  215. //*****************************************
  216. //* there we building our network address *
  217. //*****************************************
  218. ((struct sockaddr_un *) MainSocketAddress )->sun_family = AF_UNIX;
  219. snprintf( ((struct sockaddr_un *) MainSocketAddress )->sun_path, UNIX_PATH_MAX, SOCKET_NAME );
  220. RetCode = bind( MainSocket , MainSocketAddress, sizeof( struct sockaddr_un ) );
  221. if ( RetCode != 0 ){
  222. LOG_ERROR( "Error Binding to Port/Address" );
  223. return false;
  224. };
  225. RetCode = listen( MainSocket, 128);
  226. if ( RetCode != 0 ){
  227. LOG_ERROR( "Cannot Listen UNIX SOCKET" );
  228. return false;
  229. };
  230. U8 dontblock;
  231. // Set Socket permitions 666
  232. fchmod( MainSocket, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH );
  233. dontblock = fcntl( MainSocket, F_GETFL, NULL );
  234. dontblock |= O_NONBLOCK | O_NDELAY;
  235. RetCode = ioctl( MainSocket, FIONBIO, (char *) &dontblock );
  236. if (RetCode == 0 ){
  237. LOG( "Set N-Blocking Mode of Accepting Socket" );
  238. } else {
  239. LOG( "Can't set N-Blocking Mode of Accepting Socket" );
  240. };
  241. m_bConnectionType = 2;
  242. return init();
  243. };
  244. #endif
  245. void networkServer::m_fThreadProc( void ){
  246. struct sockaddr ClientAddress;
  247. socklen_t size;
  248. #ifdef __linux__
  249. int ClientSocket;
  250. if( m_bConnectionType == 1 ){
  251. size = (socklen_t) sizeof( struct sockaddr_in );
  252. } else {
  253. size = (socklen_t) sizeof( struct sockaddr_un );
  254. };
  255. #else
  256. SOCKET ClientSocket;
  257. #endif
  258. LOG( "Core Worker Thread Started" );
  259. LOG( "Ready to Accept Connections" );
  260. m_bThreadStatus = TH_WORKING;
  261. while (m_bThreadStatus < TH_ENDED ){
  262. ClientSocket = accept ( MainSocket, &ClientAddress, &size );
  263. if ( ClientSocket == -1 ) {
  264. switch( errno ){
  265. case EAGAIN:{
  266. // Checkup client disconnection
  267. if( m_vHandlersPool.size()) {
  268. for( vectorNetHandlers::iterator index = m_vHandlersPool.begin(); index < m_vHandlersPool.end(); index++ ) {
  269. LRF_ClientHandler *tClient = (LRF_ClientHandler *)(*index);
  270. if( tClient->getConnectionStatus() == CS_DISCONNECTED ){
  271. // Remove Client Handler
  272. LOG("Remove Client");
  273. delete( tClient );
  274. m_vHandlersPool.erase( index );
  275. LOG("Client Stoped");
  276. LOG("Remove Client Done");
  277. };
  278. };
  279. };
  280. SLEEP(10);
  281. continue;
  282. };
  283. //case EWOULDBLOCK: me->Log(" The socket is marked non-blocking and no connections are present to be accepted."); break;
  284. case EBADF: LOG_ERROR( "The descriptor is invalid." ); break;
  285. case ECONNABORTED: LOG_ERROR( "A connection has been aborted."); break;
  286. case EINTR: LOG_ERROR( "The system call was interrupted by a signal that was caught before a valid connection arrived."); break;
  287. case EINVAL: LOG_ERROR( "Socket is not listening for connections, or addrlen is invalid (e.g., is negative)."); break;
  288. case EMFILE: LOG_ERROR( "The per-process limit of open file descriptors has been reached."); break;
  289. case ENFILE: LOG_ERROR( "The system limit on the total number of open files has been reached."); break;
  290. case ENOTSOCK: LOG_ERROR( "The descriptor references a file, not a socket."); break;
  291. case EOPNOTSUPP: LOG_ERROR( "The referenced socket is not of type SOCK_STREAM."); break;
  292. case EFAULT: LOG_ERROR( "The addr argument is not in a writable part of the user address space."); break;
  293. case ENOBUFS:
  294. case ENOMEM: LOG_ERROR( "Not enough free memory. This often means that the memory allocation is limited by the socket buffer limits, not by the system memory."); break;
  295. case EPROTO: LOG_ERROR( "Protocol error."); break;
  296. case EPERM: LOG_ERROR( "Firewall rules forbid connection."); break;
  297. case ENOSR:
  298. #ifdef __linux__
  299. case ESOCKTNOSUPPORT:
  300. #endif
  301. case EPROTONOSUPPORT:
  302. case ETIMEDOUT: LOG_ERROR( " Unusual error in Kernel (Timeout, No Socket Support in Kernel, Unsupported Protocol)"); break;
  303. //case ERESTARTSYS: me->Log(" Unusual error by Kernel"); break;
  304. default: LOG_ERROR( " Unhandled error in Kernel"); break;
  305. };
  306. m_bThreadStatus = TH_ENDED;
  307. } else {
  308. if ( ClientSocket >= 0 ) {
  309. if( m_bConnectionType == 1 ){
  310. char tstr[CQUERY];
  311. #ifdef __linux__
  312. inet_ntop( AF_INET, &(((struct sockaddr_in*) &(ClientAddress ))->sin_addr ), tstr, size );
  313. #else
  314. strcpy( tstr, inet_ntoa( ((struct sockaddr_in*)&ClientAddress)->sin_addr ) );
  315. //InetNtop(AF_INET, &(((struct sockaddr_in*) &(ClientAddress ))->sin_addr ), tstr, size );
  316. #endif
  317. };
  318. LOG( "Client Connection Accepted" );
  319. LRF_ClientHandler *tHandle;
  320. tHandle = new LRF_ClientHandler( ClientSocket );
  321. if( tHandle != NULL ) {
  322. tHandle->attachEngine( m_pDetectorKernel );
  323. m_vHandlersPool.push_back( (networkClientHandler *) tHandle );
  324. };
  325. } else {
  326. LOG_ERROR( "Client Socket Error" );
  327. switch( errno ){
  328. //case EAGAIN: continue;
  329. case EWOULDBLOCK: LOG_ERROR( " The socket is marked non-blocking and no connections are present to be accepted." ); break;
  330. case EBADF: LOG_ERROR( " The descriptor is invalid." ); break;
  331. case ECONNABORTED: LOG_ERROR( " A connection has been aborted." ); break;
  332. case EINTR:{// If Interrupted by Signal - No Harm was done
  333. LOG_ERROR( " The system call was interrupted by a signal that was caught before a valid connection arrived." );
  334. continue;
  335. };
  336. case EINVAL: LOG_ERROR( " Socket is not listening for connections, or addrlen is invalid (e.g., is negative)." ); break;
  337. case EMFILE: LOG_ERROR( " The per-process limit of open file descriptors has been reached."); break;
  338. case ENFILE: LOG_ERROR( " The system limit on the total number of open files has been reached."); break;
  339. case ENOTSOCK: LOG_ERROR( " The descriptor references a file, not a socket."); break;
  340. case EOPNOTSUPP: LOG_ERROR( " The referenced socket is not of type SOCK_STREAM." ); break;
  341. case EFAULT: LOG_ERROR( " The addr argument is not in a writable part of the user address space." ); break;
  342. case ENOBUFS:
  343. case ENOMEM: LOG_ERROR( " Not enough free memory. This often means that the memory allocation is limited by the socket buffer limits, not by the system memory." ); break;
  344. case EPROTO: LOG_ERROR( " Protocol error." ); break;
  345. case EPERM: LOG_ERROR( " Firewall rules forbid connection." ); break;
  346. case ENOSR:
  347. #ifdef __linux__
  348. case ESOCKTNOSUPPORT:
  349. #endif
  350. case EPROTONOSUPPORT:
  351. case ETIMEDOUT: LOG_ERROR( " Unusual error in Kernel (Timeout, No Socket Support in Kernel, Unsupported Protocol)" ); break;
  352. //case ERESTARTSYS: me->Log(" Unusual error by Kernel"); break;
  353. default: LOG_ERROR( " Unhandled error in Kernel" ); break;
  354. };
  355. m_bThreadStatus = TH_ENDED;
  356. };
  357. };
  358. };
  359. LOG( "Core Worker Thread Ended" );
  360. };
  361. void networkServer::dispatchEvent( basicEvent *Evt ){
  362. if( Evt == NULL ) return;
  363. if( Evt->m_eClass == EVT_CLASS_SYSTEM ) {
  364. // Handle System Events
  365. };
  366. };
  367. #undef LOG
  368. #undef LOG_ERROR