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.

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