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.

470 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
​
​
​
​
​
3 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. const int enable = 1;
  139. if( setsockopt( MainSocket, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int)) < 0 ){
  140. LOG_ERROR("setsockopt(SO_REUSEADDR) failed");
  141. };
  142. MainSocketAddress = NULL;
  143. MainSocketAddress = ( struct sockaddr * ) malloc( sizeof( struct sockaddr_in ) );
  144. if( MainSocketAddress == NULL ){
  145. LOG_ERROR( "Some throuble with networking on this machine (Cannot Alloc Sockaddr Struct)!" );
  146. return false;
  147. };
  148. bzero( MainSocketAddress, sizeof( struct sockaddr_in ) );
  149. h_info = gethostbyname ( HOST_NAME );
  150. if ( h_info == NULL ){
  151. LOG_ERROR( "Some throuble with networking on this machine (Cannot Get Hostname)!" );
  152. return false;
  153. };
  154. //*****************************************
  155. //* there we building our network address *
  156. //*****************************************
  157. bcopy( h_info->h_addr, &(((struct sockaddr_in *)MainSocketAddress)->sin_addr), h_info->h_length );
  158. ((struct sockaddr_in *)MainSocketAddress)->sin_family = h_info->h_addrtype;
  159. ((struct sockaddr_in *)MainSocketAddress)->sin_port = htons( PORT );
  160. RetCode = bind( MainSocket , MainSocketAddress, sizeof( struct sockaddr_in ) );
  161. if ( RetCode != 0 ){
  162. LOG_ERROR( "Error Binding to Port/Address" );
  163. return false;
  164. };
  165. RetCode = listen( MainSocket, 128);
  166. if ( RetCode != 0 ){
  167. LOG_ERROR( "Cannot Listen Port/Address" );
  168. return false;
  169. };
  170. U8 dontblock;
  171. dontblock = fcntl( MainSocket, F_GETFL, NULL );
  172. dontblock |= O_NONBLOCK | O_NDELAY;
  173. RetCode = ioctl( MainSocket, FIONBIO, (char *) &dontblock );
  174. if ( RetCode == 0 ){
  175. LOG( "Set N-Blocking Mode of Accepting Socket" );
  176. } else {
  177. LOG( "Can't set N-Blocking Mode of Accepting Socket" );
  178. };
  179. #else
  180. WSADATA wsaData;
  181. int iResult;
  182. // Initialize Winsock
  183. iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
  184. if( iResult != 0 ){
  185. sprintf(temp, "WSAStartup failed with error: %d\n", iResult);
  186. LOG_ERROR(temp);
  187. return false;
  188. };
  189. MainSocket = INVALID_SOCKET;
  190. SOCKET ClientSocket = INVALID_SOCKET;
  191. struct addrinfo *result = NULL;
  192. struct addrinfo hints;
  193. int iSendResult;
  194. char recvbuf[512];
  195. int recvbuflen = 512;
  196. ZeroMemory(&hints, sizeof(hints));
  197. hints.ai_family = AF_INET;
  198. hints.ai_socktype = SOCK_STREAM;
  199. hints.ai_protocol = IPPROTO_TCP;
  200. hints.ai_flags = AI_PASSIVE;
  201. // Resolve the server address and port
  202. sprintf( temp, "%d", PORT );
  203. iResult = getaddrinfo(NULL, temp, &hints, &result);
  204. if (iResult != 0) {
  205. sprintf( temp, "getaddrinfo failed with error: %d\n", iResult );
  206. LOG_ERROR(temp);
  207. WSACleanup();
  208. return false;
  209. };
  210. // Create a SOCKET for connecting to server
  211. MainSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
  212. if (MainSocket == INVALID_SOCKET) {
  213. sprintf( temp, "socket failed with error: %ld\n", WSAGetLastError() );
  214. LOG_ERROR(temp);
  215. freeaddrinfo(result);
  216. WSACleanup();
  217. return false;
  218. };
  219. // Setup the TCP listening socket
  220. iResult = bind(MainSocket, result->ai_addr, (int)result->ai_addrlen);
  221. if (iResult == SOCKET_ERROR) {
  222. sprintf( temp, "bind failed with error: %d\n", WSAGetLastError());
  223. LOG_ERROR(temp);
  224. freeaddrinfo(result);
  225. closesocket(MainSocket);
  226. WSACleanup();
  227. return false;
  228. };
  229. freeaddrinfo(result);
  230. iResult = listen(MainSocket, SOMAXCONN);
  231. if (iResult == SOCKET_ERROR) {
  232. sprintf( temp, "listen failed with error: %d\n", WSAGetLastError());
  233. LOG_ERROR(temp);
  234. closesocket(MainSocket);
  235. WSACleanup();
  236. return false;
  237. };
  238. #endif
  239. m_bConnectionType = 1;
  240. return init();
  241. };
  242. bool networkServer::BootUp( U16 Port ){
  243. char temp[CTEXT];
  244. m_nPortNumber = Port;
  245. #ifdef __linux__
  246. int RetCode;
  247. size_t S_NAMLEN = CTEXT;
  248. RetCode = gethostname( m_pHostName, S_NAMLEN );
  249. sprintf( temp, "Running on host: %s:%d", m_pHostName, m_nPortNumber );
  250. LOG( temp );
  251. #else
  252. sprintf( m_pHostName, "" );
  253. #endif
  254. return BootUp( m_pHostName, m_nPortNumber );
  255. };
  256. bool networkServer::attachEngine( networkServiceEngine *ptr ){
  257. m_pKernelEngine = ptr;
  258. if( m_vHandlersPool.size()) {
  259. // Pass Kernel to all already connected nodes
  260. for( vectorNetHandlers::iterator index = m_vHandlersPool.begin(); index < m_vHandlersPool.end(); index++ ) {
  261. //((LRF_ClientHandler *)(*index))->attachEngine(m_pDetectorKernel);
  262. (*index)->attachEngine( m_pKernelEngine );
  263. };
  264. };
  265. return true;
  266. };
  267. #ifdef __linux__
  268. bool networkServer::BootUp(CSTR SOCKET_NAME){
  269. struct sockaddr_un MainPort;
  270. size_t S_NAMLEN = CTEXT;
  271. int RetCode;
  272. char localhost[S_NAMLEN];
  273. char temp[CTEXT];
  274. LOG( "Core BootUp" );
  275. MainSocket = socket( PF_UNIX, SOCK_STREAM, 0 );
  276. if( MainSocket == -1){
  277. LOG_ERROR( "Some throuble with Kernel on this machine (UnixSocket Error)!" );
  278. return false;
  279. };
  280. if( unlink( SOCKET_NAME ) == -1 ){
  281. LOG( "Old Socket File was not deleted!" );
  282. };
  283. localhost[0] = 0;
  284. //RetCode = gethostname( localhost, S_NAMLEN );
  285. sprintf( temp, "Running Unix Socket: %s::%s", localhost, SOCKET_NAME );
  286. LOG( temp );
  287. MainSocketAddress = ( sockaddr *) malloc( sizeof( struct sockaddr_un ) );
  288. bzero( MainSocketAddress, sizeof( struct sockaddr_un ) );
  289. //*****************************************
  290. //* there we building our network address *
  291. //*****************************************
  292. ((struct sockaddr_un *) MainSocketAddress )->sun_family = AF_UNIX;
  293. snprintf( ((struct sockaddr_un *) MainSocketAddress )->sun_path, UNIX_PATH_MAX, SOCKET_NAME );
  294. RetCode = bind( MainSocket , MainSocketAddress, sizeof( struct sockaddr_un ) );
  295. if ( RetCode != 0 ){
  296. LOG_ERROR( "Error Binding to Port/Address" );
  297. return false;
  298. };
  299. RetCode = listen( MainSocket, 128);
  300. if ( RetCode != 0 ){
  301. LOG_ERROR( "Cannot Listen UNIX SOCKET" );
  302. return false;
  303. };
  304. U8 dontblock;
  305. // Set Socket permitions 666
  306. fchmod( MainSocket, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH );
  307. dontblock = fcntl( MainSocket, F_GETFL, NULL );
  308. dontblock |= O_NONBLOCK | O_NDELAY;
  309. RetCode = ioctl( MainSocket, FIONBIO, (char *) &dontblock );
  310. if (RetCode == 0 ){
  311. LOG( "Set N-Blocking Mode of Accepting Socket" );
  312. } else {
  313. LOG( "Can't set N-Blocking Mode of Accepting Socket" );
  314. };
  315. m_bConnectionType = 2;
  316. return init();
  317. };
  318. #endif
  319. void networkServer::m_fThreadProc( void ){
  320. struct sockaddr ClientAddress;
  321. socklen_t size;
  322. #ifdef __linux__
  323. int ClientSocket;
  324. if( m_bConnectionType == 1 ){
  325. size = (socklen_t) sizeof( struct sockaddr_in );
  326. } else {
  327. size = (socklen_t) sizeof( struct sockaddr_un );
  328. };
  329. #else
  330. SOCKET ClientSocket;
  331. #endif
  332. LOG( "Core Worker Thread Started" );
  333. LOG( "Ready to Accept Connections" );
  334. m_bThreadStatus = TH_WORKING;
  335. while (m_bThreadStatus < TH_ENDED ){
  336. ClientSocket = accept ( MainSocket, &ClientAddress, &size );
  337. if ( ClientSocket < 0 ) {
  338. switch( errno ){
  339. case EAGAIN:{
  340. // Checkup client disconnection
  341. if( m_vHandlersPool.size()) {
  342. for( vectorNetHandlers::iterator index = m_vHandlersPool.begin(); index < m_vHandlersPool.end(); index++ ) {
  343. //LRF_ClientHandler *tClient = (LRF_ClientHandler *)(*index);
  344. if( (*index)->getStatus() == false ){
  345. // Remove Client Handler
  346. LOG("Remove Client");
  347. if( m_pKernelEngine ){
  348. if ( m_pKernelEngine->detachClient( (*index) ) ){
  349. #ifdef DEBUG
  350. LOG("Client Stoped");
  351. #endif
  352. m_vHandlersPool.erase( index );
  353. #ifdef DEBUG
  354. LOG("Remove Client Done");
  355. #endif
  356. } else LOG_ERROR("Client detach from Kernel Failure!");
  357. } else LOG_ERROR("No Kernel Attached!");
  358. };
  359. };
  360. };
  361. SLEEP(10);
  362. continue;
  363. };
  364. //case EWOULDBLOCK: me->Log(" The socket is marked non-blocking and no connections are present to be accepted."); break;
  365. case EBADF: LOG_ERROR( "The descriptor is invalid." ); break;
  366. case ECONNABORTED: LOG_ERROR( "A connection has been aborted."); break;
  367. case EINTR: LOG_ERROR( "The system call was interrupted by a signal that was caught before a valid connection arrived."); break;
  368. case EINVAL: LOG_ERROR( "Socket is not listening for connections, or addrlen is invalid (e.g., is negative)."); break;
  369. case EMFILE: LOG_ERROR( "The per-process limit of open file descriptors has been reached."); break;
  370. case ENFILE: LOG_ERROR( "The system limit on the total number of open files has been reached."); break;
  371. case ENOTSOCK: LOG_ERROR( "The descriptor references a file, not a socket."); break;
  372. case EOPNOTSUPP: LOG_ERROR( "The referenced socket is not of type SOCK_STREAM."); break;
  373. case EFAULT: LOG_ERROR( "The addr argument is not in a writable part of the user address space."); break;
  374. case ENOBUFS:
  375. 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;
  376. case EPROTO: LOG_ERROR( "Protocol error."); break;
  377. case EPERM: LOG_ERROR( "Firewall rules forbid connection."); break;
  378. case ENOSR:
  379. #ifdef __linux__
  380. case ESOCKTNOSUPPORT:
  381. #endif
  382. case EPROTONOSUPPORT:
  383. case ETIMEDOUT: LOG_ERROR( " Unusual error in Kernel (Timeout, No Socket Support in Kernel, Unsupported Protocol)"); break;
  384. //case ERESTARTSYS: me->Log(" Unusual error by Kernel"); break;
  385. default: LOG_ERROR( " Unhandled error in Kernel"); break;
  386. };
  387. m_bThreadStatus = TH_ENDED;
  388. } else {
  389. if( m_bConnectionType == 1 ){
  390. char tstr[CQUERY];
  391. #ifdef __linux__
  392. inet_ntop( AF_INET, &(((struct sockaddr_in*) &(ClientAddress ))->sin_addr ), tstr, size );
  393. #else
  394. strcpy( tstr, inet_ntoa( ((struct sockaddr_in*)&ClientAddress)->sin_addr ) );
  395. //InetNtop(AF_INET, &(((struct sockaddr_in*) &(ClientAddress ))->sin_addr ), tstr, size );
  396. #endif
  397. };
  398. LOG( "Client Connection Accepted" );
  399. if( m_pKernelEngine ) {
  400. networkClientHandler *tHandle;
  401. LOG("Before New Handler");
  402. tHandle = m_pKernelEngine->attachNewClient( ClientSocket );
  403. LOG("After New Handler");
  404. if( tHandle != NULL ) {
  405. LOG("Handle Not NULL");
  406. //tHandle->attachEngine( m_pKernelEngine );
  407. m_vHandlersPool.push_back( tHandle );
  408. } else {
  409. LOG_ERROR("No Client Handler Created!");
  410. shutdown( ClientSocket, 2 );
  411. close( ClientSocket );
  412. };
  413. } else {
  414. LOG_ERROR("No Kernel Attached!");
  415. shutdown( ClientSocket, 2 );
  416. close( ClientSocket );
  417. };
  418. };
  419. };
  420. LOG( "Core Worker Thread Ended" );
  421. };
  422. void networkServer::dispatchEvent( EVENT_CONTAINER *Evt ){
  423. if( Evt == NULL ) return;
  424. if( Evt->m_eClass == EVT_CLASS_SYSTEM ) {
  425. // Handle System Events
  426. };
  427. };
  428. #undef LOG
  429. #undef LOG_ERROR