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.

491 lines
16 KiB

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