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.

882 lines
25 KiB

7 years ago
​
6 years ago
7 years ago
6 years ago
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
5 years ago
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
5 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
7 years ago
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
5 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
6 years ago
7 years ago
​
​
​
​
​
​
6 years ago
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
6 years ago
6 years ago
​
​
7 years ago
​
​
5 years ago
​
7 years ago
​
​
​
​
5 years ago
7 years ago
5 years ago
7 years ago
5 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
6 years ago
6 years ago
7 years ago
  1. #include "basicFunctionality.h"
  2. #include "../../alg/generic/md5.h"
  3. //#include <sys/sendfile.h>
  4. #ifdef DEBUG
  5. #include "basicLogCollection.h"
  6. #define URGENT_LOG(X) basicLogCollection::getInstance()->Log("basic Functionality", NULL, X )
  7. #endif
  8. #ifdef __linux__
  9. int chown( const char *file_path, const char *user_name, const char *group_name){
  10. uid_t uid;
  11. gid_t gid;
  12. struct passwd *pwd;
  13. struct group *grp;
  14. pwd = getpwnam(user_name);
  15. if (pwd == NULL) {
  16. #ifdef DEBUG
  17. URGENT_LOG("Failed to get uid!");
  18. #endif
  19. return -1;
  20. };
  21. uid = pwd->pw_uid;
  22. grp = getgrnam(group_name);
  23. if (grp == NULL){
  24. #ifdef DEBUG
  25. URGENT_LOG("Failed to get gid!");
  26. #endif
  27. return -1;
  28. };
  29. gid = grp->gr_gid;
  30. return chown(file_path, uid, gid);
  31. };
  32. #endif
  33. int checkCmdExists( CSTR cmd ){
  34. FILE * fd = popen( cmd, "r" );
  35. char tB[1024];
  36. int rVal = -1;
  37. if( fd!= NULL ){
  38. while ( !feof(fd)) {
  39. fgets((CSTR)tB, 1024, fd );
  40. trim( tB );
  41. if( strstr(tB, "command not found" ) != NULL){
  42. rVal = 0;
  43. break;
  44. };
  45. };
  46. if( rVal < 1 ) rVal = 1;
  47. fclose(fd);
  48. };
  49. return rVal;
  50. }
  51. #ifdef __linux__
  52. int fileCopy( const char* source, const char* destination ){
  53. int input, output;
  54. if ((input = open(source, O_RDONLY)) == -1) return -1;
  55. if ((output = creat(destination, 0660)) == -1){
  56. close(input);
  57. return -1;
  58. };
  59. //Here we use kernel-space copying for performance reasons
  60. #if defined(__APPLE__) || defined(__FreeBSD__)
  61. //fcopyfile works on FreeBSD and OS X 10.5+
  62. int result = fcopyfile(input, output, 0, COPYFILE_ALL);
  63. #else
  64. //sendfile will work with non-socket output (i.e. regular file) on Linux 2.6.33+
  65. off_t bytesCopied = 0;
  66. struct stat fileinfo = {0};
  67. fstat(input, &fileinfo);
  68. int result = sendfile(output, input, &bytesCopied, fileinfo.st_size);
  69. #endif
  70. close(input);
  71. close(output);
  72. return result;
  73. };
  74. #endif
  75. bool fileExist(const char *strFilename) {
  76. struct stat stFileInfo;
  77. int intStat;
  78. intStat = stat( strFilename ,&stFileInfo);
  79. if( intStat == 0 ){
  80. return true;
  81. };
  82. return false;
  83. };
  84. bool dirEmpty( const char *dirname ){
  85. int n = 0;
  86. struct dirent *d;
  87. DIR *dir = opendir(dirname);
  88. if( dir == NULL) return true;
  89. while( (d = readdir(dir)) != NULL )if( ++n > 2 ) break;
  90. closedir( dir );
  91. if( n <= 2 ) return true;
  92. return false;
  93. };
  94. #ifndef __linux__
  95. #else
  96. #include <sys/types.h>
  97. #include <unistd.h>
  98. #include <sys/syscall.h>
  99. #include <sys/types.h>
  100. pid_t gettid( void ){
  101. return syscall(SYS_gettid);
  102. };
  103. #endif
  104. const char *__BuildDate = (__DATE__);
  105. const char *__BuildTime = (__TIME__);
  106. const char *__BuildTS = (__TIMESTAMP__);
  107. CSTR getBuildID( void ){
  108. return NULL;
  109. };
  110. /* Global Time Diff Value to correct Timestamps */
  111. static int g_iTimeDiff = 0;
  112. /**
  113. * number of seconds from 1 Jan. 1601 00:00 to 1 Jan 1970 00:00 UTC
  114. */
  115. // Defined in Header File
  116. //#define EPOCH_DIFF 11644473600LL
  117. //#define UNIX_TimeStamp_2_WINDOWS_FileTime_SHIFT 116444736000000000
  118. bool correctSysTime( int diff ){
  119. #ifdef __linux__
  120. struct timeval tData;
  121. struct timezone tTZ;
  122. tTZ.tz_minuteswest = 0;
  123. tTZ.tz_dsttime = 0;
  124. if( gettimeofday( &tData, &tTZ ) == -1 ){
  125. return false;
  126. } else {
  127. tData.tv_sec += diff / 1000;
  128. tData.tv_usec += ( diff * 1000 ) % 1000000;
  129. };
  130. if( settimeofday( &tData, &tTZ ) == -1 ){
  131. return false;
  132. };
  133. #else
  134. unsigned __int64 tMilliseconds;
  135. SYSTEMTIME Ztime;
  136. GetSystemTime(&Ztime);
  137. SystemTimeToFileTime( &Ztime, (FILETIME*) &tMilliseconds );
  138. tMilliseconds += diff * 10000;
  139. FileTimeToSystemTime( (FILETIME*) &tMilliseconds, &Ztime );
  140. if( 0 == SetSystemTime(&Ztime) ) return false;
  141. #endif
  142. return true;
  143. };
  144. bool correctSoftTime( int diff ){
  145. g_iTimeDiff = diff;
  146. return true;
  147. };
  148. #ifdef __linux__
  149. unsigned long long getTimeValue( void ){
  150. unsigned long long tMilliseconds;
  151. struct timeval tData;
  152. struct timezone tTZ;
  153. tMilliseconds = 0;
  154. tTZ.tz_minuteswest = 0;
  155. tTZ.tz_dsttime = 0;
  156. if( gettimeofday( &tData, &tTZ ) == -1 ){
  157. } else {
  158. tMilliseconds = tData.tv_sec;
  159. tMilliseconds = tMilliseconds *1000;
  160. tMilliseconds += tData.tv_usec / 1000;
  161. };
  162. #else
  163. /*
  164. void TimetToFileTime( time_t t, LPFILETIME pft ){
  165. LONGLONG ll = Int32x32To64(t, 10000000) + 116444736000000000;
  166. pft->dwLowDateTime = (DWORD) ll;
  167. pft->dwHighDateTime = ll >>32;
  168. }*/
  169. unsigned __int64 getTimeValue( void ){
  170. unsigned __int64 tMilliseconds;
  171. SYSTEMTIME Ztime;
  172. GetSystemTime(&Ztime);
  173. SystemTimeToFileTime( &Ztime, (FILETIME*) &tMilliseconds );
  174. tMilliseconds -= UNIX_TimeStamp_2_WINDOWS_FileTime_SHIFT;
  175. tMilliseconds = tMilliseconds / 10000;
  176. #endif
  177. tMilliseconds += g_iTimeDiff;
  178. return tMilliseconds;
  179. };
  180. CSTR getTimeStamp( void ){
  181. CSTR RetVal = NULL;
  182. time_t rawtime;
  183. uint32_t msec;
  184. struct tm * ptm = NULL;
  185. time ( &rawtime );
  186. ptm = gmtime ( &rawtime );
  187. msec = getTimeValue() % 1000;
  188. //2019-03-24T12:32:59.670Z
  189. if( ptm != NULL ) {
  190. RetVal = (CSTR) malloc( 128 );
  191. if( RetVal!=NULL ){
  192. sprintf( RetVal, "%04d-%02d-%02dT%02d:%02d:%02d.%03dZ",
  193. (ptm->tm_year+1900),
  194. (ptm->tm_mon+1),
  195. ptm->tm_mday,
  196. ptm->tm_hour,
  197. ptm->tm_min,
  198. ptm->tm_sec,
  199. msec
  200. );
  201. };
  202. };
  203. return RetVal;
  204. };
  205. CSTR getUniqID_MD5( void ){
  206. CSTR tVal = NULL;
  207. CSTR tMD5 = NULL;
  208. char buf[33];
  209. tVal = getTimeStamp();
  210. buf[0] = (char)0;
  211. if( tVal ){
  212. tMD5 = MD5String( tVal );
  213. if( tMD5 ) {
  214. sprintf( buf, "%s", tMD5 );
  215. free( tMD5 );
  216. };
  217. free( tVal );
  218. };
  219. #ifdef __linux__
  220. return strdup( buf );
  221. #else
  222. return _strdup( buf );
  223. #endif
  224. };
  225. CSTR getUniqID_SHA256(void){ return NULL;};
  226. int rand( int a, int b ){
  227. if( b > a ){
  228. return a + (rand() *( b - a )) / RAND_MAX;
  229. };
  230. return b + ( rand() * ( a - b )) / RAND_MAX;
  231. };
  232. CSTR getUniqID_UUID( uint32_t SEED ){
  233. /*
  234. char buf[46];
  235. if( SEED ) {
  236. srand( SEED );
  237. } else {
  238. srand( getMilliseconds() );// time(NULL) );
  239. };
  240. snprintf( buf, 42, "%8.8x-%4.4x-%4.4x-%4.4x-%6.6x%6.6x",
  241. rand() & 0xFFFFFFFF,
  242. rand() & 0xFFFF,
  243. (rand() & 0x0FFF) | 0x4000, // M 4 MSB version => version 4
  244. (rand() & 0x1FFF) | 0x8000, // N: 3 MSB variant => variant 1
  245. rand() & 0xFFFFFF, rand() & 0xFFFFFF
  246. );
  247. #ifdef __linux__
  248. return strdup( buf );
  249. #else
  250. return _strdup( buf );
  251. #endif
  252. */
  253. CSTR tVal = NULL;
  254. CSTR tMD5 = NULL;
  255. char buf[37];
  256. tVal = getTimeStamp();
  257. buf[0] = (char)0;
  258. if( tVal ){
  259. char tt[256];
  260. sprintf( tt, "%s %d", tVal ,SEED );
  261. tMD5 = MD5String( tt );
  262. if( tMD5 ) {
  263. memcpy( buf, tMD5, 8 );
  264. buf[8] = '-';
  265. memcpy( buf+9, tMD5+8, 4 );
  266. buf[13] = '-';
  267. memcpy( buf+14, tMD5+12, 4 );
  268. buf[18] = '-';
  269. memcpy( buf+19, tMD5+16, 4 );
  270. buf[23] = '-';
  271. memcpy( buf+24, tMD5+20, 12 );
  272. buf[36] = (char)0;
  273. free( tMD5 );
  274. };
  275. free( tVal );
  276. };
  277. #ifdef __linux__
  278. return strdup( buf );
  279. #else
  280. return _strdup( buf );
  281. #endif
  282. };
  283. CSTR trim( CSTR val ){
  284. int i = 0;
  285. int j = strlen( val );
  286. if( j ){
  287. while(( i < j ) && (((unsigned char)val[i])< 0x21 )) i++;
  288. while(( j > i ) && (((unsigned char)val[j])< 0x21 )) j--;
  289. if( ( i > 0 ) && ((j-i+1) > 0 ) ) memcpy( val, val+i, j-i+1 );
  290. val[j-i+1]=(char)0;
  291. };
  292. return val;
  293. };
  294. // orig - input String to Search In
  295. // rep - token to search
  296. // with - replacement for token
  297. // in case with is NULL - cutting off the token
  298. // You must free the result if result is non-NULL.
  299. CSTR strReplace( CSTR orig, CSTR rep, CSTR with ){
  300. char *result; // the return string
  301. char *ins; // the next insert point
  302. char *tmp; // varies
  303. int len_rep; // length of rep (the string to remove)
  304. int len_with; // length of with (the string to replace rep with)
  305. int len_front; // distance between rep and end of last rep
  306. int count; // number of replacements
  307. // sanity checks and initialization
  308. if (!orig || !rep) return NULL;
  309. len_rep = strlen(rep);
  310. if (len_rep == 0) return NULL; // empty rep causes infinite loop during count
  311. if (!with) with =(CSTR)""; // In case With not suplied - just cut off Token
  312. len_with = strlen(with);
  313. // count the number of replacements needed
  314. ins = orig;
  315. for (count = 0; tmp = strstr(ins, rep); ++count) {
  316. ins = tmp + len_rep;
  317. }
  318. tmp = result = (CSTR) malloc(strlen(orig) + (len_with - len_rep) * count + 1);
  319. if( !result ) return NULL;
  320. // first time through the loop, all the variable are set correctly
  321. // from here on,
  322. // tmp points to the end of the result string
  323. // ins points to the next occurrence of rep in orig
  324. // orig points to the remainder of orig after "end of rep"
  325. while (count--) {
  326. ins = strstr(orig, rep);
  327. len_front = ins - orig;
  328. tmp = strncpy(tmp, orig, len_front) + len_front;
  329. tmp = strcpy(tmp, with) + len_with;
  330. orig += len_front + len_rep; // move to next "end of rep"
  331. }
  332. strcpy(tmp, orig);
  333. return result;
  334. };
  335. uint8_t checksum8( uint8_t *Buffer, size_t BufferSize ){
  336. uint16_t TempVal;
  337. TempVal = 0;
  338. for( int index = 0; index < BufferSize; index++ ) {
  339. TempVal+=Buffer[index];
  340. };
  341. return (uint8_t)(TempVal & 0x00FF);
  342. };
  343. uint16_t checksum16( uint8_t *Buffer, size_t BufferSize ){
  344. uint32_t TempVal;
  345. TempVal = 0;
  346. for( int index = 0; index < BufferSize; index++ ) {
  347. TempVal+=Buffer[index];
  348. };
  349. return (uint16_t)(TempVal & 0x0000FFFF);
  350. };
  351. uint8_t checksumLGS( uint8_t *Buffer, size_t BufferSize ){
  352. uint16_t TempVal;
  353. TempVal = 0;
  354. for( int index = 0; index < BufferSize; index++ ) {
  355. TempVal+=Buffer[index];
  356. };
  357. return (uint8_t)(TempVal & 0x00FF);
  358. };
  359. // Calculating the Check Sum for Ping package
  360. uint16_t checksumPing(void *b, size_t len){
  361. uint16_t *buf = (uint16_t *) b;
  362. uint32_t sum=0;
  363. uint16_t result;
  364. for ( sum = 0; len > 1; len -= 2 )
  365. sum += *buf++;
  366. if ( len == 1 )
  367. sum += *(unsigned char*)buf;
  368. sum = (sum >> 16) + (sum & 0xFFFF);
  369. sum += (sum >> 16);
  370. result = ~sum;
  371. return result;
  372. };
  373. CArray *explode( char Delim, CSTR S_Str ){
  374. bool ws = false;
  375. CArray * RetVal = NULL;
  376. CSTR b_ptr = S_Str;
  377. U16 s_ptr = 0;
  378. if( S_Str ){
  379. RetVal = new CArray;
  380. if( RetVal ){
  381. while ((U8)S_Str[s_ptr]) {
  382. if( (( U8 ) Delim ) == (( U8 )S_Str[s_ptr]) ){
  383. if (!ws) {
  384. ws = true;
  385. S_Str[s_ptr] = (char)0;
  386. RetVal->push_back( b_ptr );
  387. s_ptr++;
  388. b_ptr = &S_Str[s_ptr];
  389. } else s_ptr++;
  390. } else {
  391. ws = false;
  392. s_ptr++;
  393. };
  394. };
  395. RetVal->push_back( b_ptr );
  396. };
  397. };
  398. return RetVal;
  399. };
  400. CSTR implode( CArray *Data, CSTR S_Str, U8 Start, U8 End ){
  401. CSTR RetVal;
  402. U8 RStart = Start;
  403. U8 REnd = End;
  404. if( Start > Data->size() ) return NULL;
  405. if( End < Start ) return NULL;
  406. if( End > Data->size() ) REnd = (U8)(Data->size()-1);
  407. if( strlen( (*Data).at( RStart )) ){
  408. RetVal = (CSTR) malloc( strlen( (*Data).at( RStart )) + 1 );
  409. sprintf( RetVal,"%s",(*Data).at( RStart ));
  410. } else {
  411. RetVal = (CSTR) malloc( 1 );
  412. *RetVal=(char)0;
  413. };
  414. if (RetVal != NULL) {
  415. for (int i = RStart + 1; i < REnd; i++) {
  416. RetVal = (CSTR)realloc(RetVal, strlen(RetVal) + strlen((*Data).at(i)) + 1 + strlen(S_Str));
  417. sprintf(RetVal, "%s%s%s", RetVal, S_Str, (*Data).at(i));
  418. };
  419. };
  420. return RetVal;
  421. };
  422. int getIP4( if_info **arrParam, int max_ifs ){
  423. int index = 0;
  424. #ifdef __linux__
  425. struct ifaddrs *addrs, *tmp;
  426. getifaddrs(&addrs);
  427. tmp = addrs;
  428. //arrParam= malloc( 10 * sezeof(if_info) );
  429. while (tmp){
  430. if (tmp->ifa_addr && tmp->ifa_addr->sa_family == AF_INET){
  431. if( strncmp( tmp->ifa_name, "lo", 2 ) != 0 ){
  432. (*arrParam)[index].if_name = (CSTR)malloc(strlen(tmp->ifa_name)+1);
  433. snprintf( (*arrParam)[index].if_name, strlen(tmp->ifa_name)+1,"%s", tmp->ifa_name );
  434. (*arrParam)[index].if_addr = (CSTR)malloc(16);
  435. struct sockaddr_in *pAddr = (struct sockaddr_in *)tmp->ifa_addr;
  436. snprintf( (*arrParam)[index].if_addr, 16, "%s", inet_ntoa(pAddr->sin_addr) );
  437. index++;
  438. if( index == max_ifs ) break;
  439. }
  440. };
  441. tmp = tmp->ifa_next;
  442. };
  443. freeifaddrs(addrs);
  444. #endif
  445. return index;
  446. };
  447. #ifdef __linux__
  448. long long getFreeSpace( CSTR DirName ){
  449. struct statvfs fiData;
  450. if( ( statvfs( DirName, &fiData ) ) < 0 ) {
  451. return -1;
  452. };
  453. return fiData.f_bsize * fiData.f_bfree;
  454. };
  455. long long getTotalSpace( CSTR DirName ){
  456. struct statvfs fiData;
  457. if( ( statvfs( DirName, &fiData ) ) < 0 ) {
  458. return -1;
  459. };
  460. return fiData.f_bsize * fiData.f_blocks;
  461. };
  462. #else
  463. __int64 getFreeSpace( CSTR DirName ){
  464. return -1;
  465. };
  466. __int64 getTotalSpace( CSTR DirName ){
  467. return -1;
  468. };
  469. #endif
  470. CSTR getTStr( CSTR Fmt ){
  471. //CSTR RetVal = NULL;
  472. char DateStr[200];
  473. time_t t;
  474. struct tm *tmp;
  475. t = time(NULL);
  476. tmp = gmtime(&t);
  477. if( tmp == NULL) {
  478. fprintf(stderr, "localtime!");
  479. return NULL;
  480. };
  481. if( Fmt == NULL ) {
  482. if( strftime( DateStr, 200, "%F %T GMT", tmp )==0 ){
  483. fprintf( stderr, "strftime returned 0" );
  484. return NULL;
  485. };
  486. } else {
  487. if( strftime( DateStr, 200, Fmt, tmp )==0 ){
  488. fprintf( stderr, "strftime returned 0" );
  489. return NULL;
  490. };
  491. }
  492. //RetVal =(CSTR) malloc( strlen(DateStr) +1 );
  493. //if( RetVal != NULL ) strcpy( RetVal, DateStr );
  494. #ifdef __linux__
  495. return strdup(DateStr);
  496. #else
  497. return _strdup(DateStr);
  498. #endif
  499. };
  500. #ifdef __linux__
  501. uint64_t getMilliseconds( void ){
  502. uint64_t tMilliseconds = (uint64_t) 0;
  503. struct timeval tData;
  504. struct timezone tTZ;
  505. tTZ.tz_minuteswest = 0;
  506. tTZ.tz_dsttime = 0;
  507. if( gettimeofday( &tData, &tTZ ) == -1 ){
  508. tMilliseconds = (uint64_t) 0;
  509. #ifdef DEBUG
  510. URGENT_LOG( "EbioMat'!");
  511. #endif
  512. } else {
  513. tMilliseconds = (uint64_t) tData.tv_sec;
  514. tMilliseconds = ( tMilliseconds * 1000 );
  515. tMilliseconds += (uint64_t)( tData.tv_usec / 1000 );
  516. //URGENT_LOG("MilSecCollected!");
  517. };
  518. #else
  519. #include<ctime>
  520. unsigned __int64 getMilliseconds( void ){
  521. unsigned __int64 tMilliseconds;
  522. SYSTEMTIME time;
  523. GetSystemTime(&time);
  524. tMilliseconds = (uint32_t)std::time(NULL);
  525. tMilliseconds *= 1000;
  526. tMilliseconds += time.wMilliseconds;
  527. #endif
  528. return tMilliseconds;
  529. };
  530. #ifdef __linux__
  531. bool enumerateDevices( enumeratorInfo *mlist, const char *dTemplate){
  532. DIR *d;
  533. struct dirent *dir;
  534. DeviceInfo *tDevInfo;
  535. if( mlist == NULL )return false;
  536. if( dTemplate == NULL )return false;
  537. for( int i = 0; i < mlist->size(); i++ ) {
  538. delete( mlist->at(i) );
  539. };
  540. mlist->clear();
  541. d = opendir("/dev");
  542. if( d ){
  543. while((dir = readdir(d)) != NULL){
  544. if( dir->d_type == DT_CHR || dir->d_type == DT_BLK ){
  545. if( strncmp( dir->d_name, dTemplate, 5 ) == 0 ) {
  546. tDevInfo = (DeviceInfo *) malloc( sizeof(DeviceInfo) );
  547. sprintf( tDevInfo->DevicePath, "/dev/%s", dir->d_name );
  548. tDevInfo->FriendlyName[0] =(char)0;
  549. if( strncmp( dTemplate, "video", 5 ) == 0 ){
  550. FILE *fd;
  551. char CMD[256];
  552. sprintf( CMD, "v4l2-ctl -d %s -D", tDevInfo->DevicePath );
  553. fd = popen( CMD, "r");
  554. if( fd != NULL ) {
  555. char * line = NULL;
  556. size_t len = 0;
  557. ssize_t rd;
  558. while ((rd = getline(&line, &len, fd)) != -1) {
  559. char *tS1, *tS2;
  560. // Future use strcasestr() or make both stings same case
  561. if( ( tS1 = strstr( line, "Bus info" )) != NULL ) {
  562. tS2 = trim( strstr( tS1, ":" ) + 2 );
  563. sprintf( tDevInfo->FriendlyName, "%s", tS2 );
  564. };
  565. };
  566. fclose( fd );
  567. if( line ) free( line );
  568. };
  569. };
  570. mlist->push_back( tDevInfo );
  571. //printf("%s\n", dir->d_name );
  572. };
  573. //if( strncmp( dir->d_name, "ttyS", 4 ) == 0 ){
  574. // tBuffer = ( char * ) malloc( strlen(dir->d_name)+1 );
  575. // strncpy( tBuffer, dir->d_name, strlen(dir->d_name)+1 );
  576. // mlist->pushBack( tBuffer );
  577. // //printf("%s\n", dir->d_name );
  578. //};
  579. };
  580. };
  581. closedir(d);
  582. } else {
  583. return false;
  584. };
  585. return true;
  586. };
  587. #else
  588. #ifndef IID_PPV_ARGS
  589. extern "C++" {
  590. template<typename T> void** IID_PPV_ARGS_Helper(T** pp)
  591. {
  592. // make sure everyone derives from IUnknown
  593. static_cast<IUnknown*>(*pp);
  594. return reinterpret_cast<void**>(pp);
  595. }
  596. }
  597. #define IID_PPV_ARGS(ppType) __uuidof(**(ppType)), IID_PPV_ARGS_Helper(ppType)
  598. #endif
  599. #pragma comment(lib,"Strmiids.lib")
  600. bool enumerateDevices(enumeratorInfo *mlist, REFGUID category){
  601. HRESULT hr;
  602. bool initOk = true;
  603. hr = CoInitializeEx( NULL, COINIT_SPEED_OVER_MEMORY | COINIT_MULTITHREADED | COINIT_DISABLE_OLE1DDE );
  604. if( hr == E_INVALIDARG ){
  605. printf("Couldn't Init! hr=0x%x", hr);
  606. return false;
  607. } else if ( hr == E_OUTOFMEMORY ){
  608. printf("Couldn't Init! hr=0x%x", hr);
  609. return false;
  610. } else if ( hr == E_UNEXPECTED ){
  611. printf("Couldn't Init! hr=0x%x", hr);
  612. return false;
  613. } else if( hr == RPC_E_CHANGED_MODE ){
  614. initOk = false;
  615. };
  616. if( hr == S_OK || hr == RPC_E_CHANGED_MODE ){
  617. // Create the system device enumerator
  618. ICreateDevEnum *pDevEnum;
  619. hr = CoCreateInstance( CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pDevEnum) );
  620. if( hr != S_OK ){
  621. printf("Couldn't create system enumerator! hr=0x%x", hr);
  622. CoUninitialize();
  623. return false;
  624. };
  625. if( pDevEnum != NULL ){
  626. IEnumMoniker *pClsEnum = NULL;
  627. hr = pDevEnum->CreateClassEnumerator(category, &pClsEnum, 0);
  628. if( hr == S_FALSE ){
  629. // The category is empty. Treat as an error.
  630. pDevEnum->Release();
  631. CoUninitialize();
  632. return false;
  633. };
  634. if( pClsEnum != NULL ){
  635. IMoniker *pMoniker = NULL;
  636. while( pClsEnum->Next(1, &pMoniker, NULL) == S_OK ){
  637. IPropertyBag *pPropBag = NULL;
  638. hr = pMoniker->BindToStorage(0, 0, IID_PPV_ARGS(&pPropBag));
  639. if( FAILED(hr) ){
  640. pMoniker->Release();
  641. continue;
  642. };
  643. VARIANT var;
  644. VariantInit(&var);
  645. DeviceInfo *tRec;
  646. tRec = (DeviceInfo *)malloc(sizeof(DeviceInfo));
  647. // Get description or friendly name.
  648. hr = pPropBag->Read(L"Description", &var, 0);
  649. if( FAILED(hr) ){
  650. hr = pPropBag->Read(L"FriendlyName", &var, 0);
  651. };
  652. if (SUCCEEDED(hr)){
  653. if (tRec != NULL) {
  654. sprintf(tRec->FriendlyName, "%S", var.bstrVal);
  655. mlist->push_back(tRec);
  656. };
  657. VariantClear(&var);
  658. };
  659. hr = pPropBag->Write(L"FriendlyName", &var);
  660. // WaveInID applies only to audio capture devices.
  661. hr = pPropBag->Read(L"WaveInID", &var, 0);
  662. if (SUCCEEDED(hr)){
  663. //printf("WaveIn ID: %d\n", var.lVal);
  664. VariantClear(&var);
  665. };
  666. hr = pPropBag->Read(L"DevicePath", &var, 0);
  667. if (SUCCEEDED(hr)){
  668. // The device path is not intended for display.
  669. if( tRec != NULL) {
  670. sprintf(tRec->DevicePath, "%S", var.bstrVal);
  671. };
  672. VariantClear(&var);
  673. };
  674. pPropBag->Release();
  675. pMoniker->Release();
  676. };
  677. pClsEnum->Release();
  678. };
  679. pDevEnum->Release();
  680. };
  681. if( initOk ) CoUninitialize();
  682. };
  683. return true;
  684. };
  685. #endif
  686. localIpAddresses g_vLocalIpAdressess;
  687. bool g_bAdressesAreDiscovered = false;
  688. #ifdef __linux__
  689. #include <linux/if.h>
  690. localIpAddresses *getLocalIP(){
  691. // Check for Filter LO and TUN Devices!
  692. if( !g_bAdressesAreDiscovered ) {
  693. struct ifaddrs *ifaddr, *ifa;
  694. localIp4Info *tIpAddress = NULL;
  695. if (getifaddrs(&ifaddr) != -1) {
  696. /* Walk through linked list, maintaining head pointer so we can free list later */
  697. for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
  698. // Filter Loopback if
  699. if( strstr( ifa->ifa_name, "lo" ) != NULL ) continue;
  700. // Filter vpn TUN if
  701. if( strstr( ifa->ifa_name, "tun" ) != NULL ) continue;
  702. char protocol[IFNAMSIZ] = {0};
  703. if (ifa->ifa_addr == NULL || ifa->ifa_addr->sa_family != AF_INET) continue;
  704. tIpAddress = (localIp4Info*)malloc(sizeof(localIp4Info));
  705. if(tIpAddress!=NULL){
  706. sprintf( tIpAddress->address , "%d.%d.%d.%d",
  707. (uint8_t)ifa->ifa_addr->sa_data[2],
  708. (uint8_t)ifa->ifa_addr->sa_data[3],
  709. (uint8_t)ifa->ifa_addr->sa_data[4],
  710. (uint8_t)ifa->ifa_addr->sa_data[5]
  711. );
  712. sprintf( tIpAddress->netmask , "%d.%d.%d.%d",
  713. (uint8_t)ifa->ifa_netmask->sa_data[2],
  714. (uint8_t)ifa->ifa_netmask->sa_data[3],
  715. (uint8_t)ifa->ifa_netmask->sa_data[4],
  716. (uint8_t)ifa->ifa_netmask->sa_data[5]
  717. );
  718. sprintf( tIpAddress->broadcast , "%d.%d.%d.%d",
  719. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[2],
  720. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[3],
  721. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[4],
  722. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[5]
  723. );
  724. g_vLocalIpAdressess.push_back(tIpAddress);
  725. };
  726. };
  727. freeifaddrs(ifaddr);
  728. };
  729. g_bAdressesAreDiscovered = true;
  730. };
  731. return &g_vLocalIpAdressess;
  732. };
  733. #else
  734. //https://stackoverflow.com/questions/5213629/how-to-enumerate-all-available-network-interfaces
  735. #include <iphlpapi.h>
  736. #pragma comment(lib, "IPHLPAPI.lib")
  737. localIpAddresses* getLocalIP() {
  738. char psBuffer[128];
  739. FILE* fd;
  740. BYTE mB;
  741. localIp4Info* tIpAddress;
  742. if (!g_bAdressesAreDiscovered) {
  743. ULONG outBufLen = 0;
  744. DWORD dwRetVal = 0;
  745. IP_ADAPTER_INFO* pAdapterInfos = (IP_ADAPTER_INFO*)malloc(sizeof(IP_ADAPTER_INFO));
  746. // retry up to 5 times, to get the adapter infos needed
  747. for (int i = 0; i < 5 && (dwRetVal == ERROR_BUFFER_OVERFLOW || dwRetVal == NO_ERROR); ++i) {
  748. dwRetVal = GetAdaptersInfo(pAdapterInfos, &outBufLen);
  749. if (dwRetVal == NO_ERROR) {
  750. break;
  751. } else {
  752. if (dwRetVal == ERROR_BUFFER_OVERFLOW) {
  753. free(pAdapterInfos);
  754. pAdapterInfos = (IP_ADAPTER_INFO*)malloc(outBufLen);
  755. } else {
  756. //pAdapterInfos = 0;
  757. break;
  758. };
  759. };
  760. };
  761. if (dwRetVal == NO_ERROR) {
  762. IP_ADAPTER_INFO* pAdapterInfo = pAdapterInfos;
  763. while (pAdapterInfo) {
  764. IP_ADDR_STRING* pIpAddress = &(pAdapterInfo->IpAddressList);
  765. while (pIpAddress != 0) {
  766. uint8_t A, B, C, D;
  767. uint8_t M, O, S, K, G, U, T, E;
  768. uint32_t Z1, Z2, Z3, Z4;
  769. int RetVal;
  770. RetVal = sscanf(pIpAddress->IpAddress.String, "%d.%d.%d.%d", &Z1, &Z2, &Z3, &Z4);
  771. A = Z1 & 0xFF;
  772. B = Z2 & 0xFF;
  773. C = Z3 & 0xFF;
  774. D = Z4 & 0xFF;
  775. RetVal = sscanf(pIpAddress->IpMask.String, "%d.%d.%d.%d", &Z1, &Z2, &Z3, &Z4);
  776. M = Z1 & 0xFF;
  777. O = Z2 & 0xFF;
  778. S = Z3 & 0xFF;
  779. K = Z4 & 0xFF;
  780. //
  781. // <<<<
  782. // here pAdapterInfo->Address should contain the MAC address
  783. // >>>>
  784. //
  785. if (A > 0) {
  786. tIpAddress = (localIp4Info*)malloc(sizeof(localIp4Info));
  787. if (tIpAddress != NULL) {
  788. sprintf(tIpAddress->address, "%d.%d.%d.%d", A, B, C, D );
  789. sprintf(tIpAddress->netmask, "%d.%d.%d.%d", M, O, S, K );
  790. G = (A & M) | (~M);
  791. U = (B & O) | (~O);
  792. T = (C & S) | (~S);
  793. E = (D & K) | (~K);
  794. sprintf(tIpAddress->broadcast, "%d.%d.%d.%d", G, U, T, E );
  795. g_vLocalIpAdressess.push_back(tIpAddress);
  796. };
  797. };
  798. pIpAddress = pIpAddress->Next;
  799. };
  800. pAdapterInfo = pAdapterInfo->Next;
  801. };
  802. };
  803. free(pAdapterInfos);
  804. };
  805. return &g_vLocalIpAdressess;
  806. };
  807. #endif
  808. #ifdef DEBUG
  809. #undef URGENT_LOG
  810. #endif