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.

464 lines
16 KiB

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