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.

875 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. CSTR getUniqID_UUID( uint32_t SEED ){
  227. /*
  228. char buf[46];
  229. if( SEED ) {
  230. srand( SEED );
  231. } else {
  232. srand( getMilliseconds() );// time(NULL) );
  233. };
  234. snprintf( buf, 42, "%8.8x-%4.4x-%4.4x-%4.4x-%6.6x%6.6x",
  235. rand() & 0xFFFFFFFF,
  236. rand() & 0xFFFF,
  237. (rand() & 0x0FFF) | 0x4000, // M 4 MSB version => version 4
  238. (rand() & 0x1FFF) | 0x8000, // N: 3 MSB variant => variant 1
  239. rand() & 0xFFFFFF, rand() & 0xFFFFFF
  240. );
  241. #ifdef __linux__
  242. return strdup( buf );
  243. #else
  244. return _strdup( buf );
  245. #endif
  246. */
  247. CSTR tVal = NULL;
  248. CSTR tMD5 = NULL;
  249. char buf[37];
  250. tVal = getTimeStamp();
  251. buf[0] = (char)0;
  252. if( tVal ){
  253. char tt[256];
  254. sprintf( tt, "%s %d", tVal ,SEED );
  255. tMD5 = MD5String( tt );
  256. if( tMD5 ) {
  257. memcpy( buf, tMD5, 8 );
  258. buf[8] = '-';
  259. memcpy( buf+9, tMD5+8, 4 );
  260. buf[13] = '-';
  261. memcpy( buf+14, tMD5+12, 4 );
  262. buf[18] = '-';
  263. memcpy( buf+19, tMD5+16, 4 );
  264. buf[23] = '-';
  265. memcpy( buf+24, tMD5+20, 12 );
  266. buf[36] = (char)0;
  267. free( tMD5 );
  268. };
  269. free( tVal );
  270. };
  271. #ifdef __linux__
  272. return strdup( buf );
  273. #else
  274. return _strdup( buf );
  275. #endif
  276. };
  277. CSTR trim( CSTR val ){
  278. int i = 0;
  279. int j = strlen( val );
  280. if( j ){
  281. while(( i < j ) && (((unsigned char)val[i])< 0x21 )) i++;
  282. while(( j > i ) && (((unsigned char)val[j])< 0x21 )) j--;
  283. if( ( i > 0 ) && ((j-i+1) > 0 ) ) memcpy( val, val+i, j-i+1 );
  284. val[j-i+1]=(char)0;
  285. };
  286. return val;
  287. };
  288. // orig - input String to Search In
  289. // rep - token to search
  290. // with - replacement for token
  291. // in case with is NULL - cutting off the token
  292. // You must free the result if result is non-NULL.
  293. CSTR strReplace( CSTR orig, CSTR rep, CSTR with ){
  294. char *result; // the return string
  295. char *ins; // the next insert point
  296. char *tmp; // varies
  297. int len_rep; // length of rep (the string to remove)
  298. int len_with; // length of with (the string to replace rep with)
  299. int len_front; // distance between rep and end of last rep
  300. int count; // number of replacements
  301. // sanity checks and initialization
  302. if (!orig || !rep) return NULL;
  303. len_rep = strlen(rep);
  304. if (len_rep == 0) return NULL; // empty rep causes infinite loop during count
  305. if (!with) with =(CSTR)""; // In case With not suplied - just cut off Token
  306. len_with = strlen(with);
  307. // count the number of replacements needed
  308. ins = orig;
  309. for (count = 0; tmp = strstr(ins, rep); ++count) {
  310. ins = tmp + len_rep;
  311. }
  312. tmp = result = (CSTR) malloc(strlen(orig) + (len_with - len_rep) * count + 1);
  313. if( !result ) return NULL;
  314. // first time through the loop, all the variable are set correctly
  315. // from here on,
  316. // tmp points to the end of the result string
  317. // ins points to the next occurrence of rep in orig
  318. // orig points to the remainder of orig after "end of rep"
  319. while (count--) {
  320. ins = strstr(orig, rep);
  321. len_front = ins - orig;
  322. tmp = strncpy(tmp, orig, len_front) + len_front;
  323. tmp = strcpy(tmp, with) + len_with;
  324. orig += len_front + len_rep; // move to next "end of rep"
  325. }
  326. strcpy(tmp, orig);
  327. return result;
  328. };
  329. uint8_t checksum8( uint8_t *Buffer, size_t BufferSize ){
  330. uint16_t TempVal;
  331. TempVal = 0;
  332. for( int index = 0; index < BufferSize; index++ ) {
  333. TempVal+=Buffer[index];
  334. };
  335. return (uint8_t)(TempVal & 0x00FF);
  336. };
  337. uint16_t checksum16( uint8_t *Buffer, size_t BufferSize ){
  338. uint32_t TempVal;
  339. TempVal = 0;
  340. for( int index = 0; index < BufferSize; index++ ) {
  341. TempVal+=Buffer[index];
  342. };
  343. return (uint16_t)(TempVal & 0x0000FFFF);
  344. };
  345. uint8_t checksumLGS( uint8_t *Buffer, size_t BufferSize ){
  346. uint16_t TempVal;
  347. TempVal = 0;
  348. for( int index = 0; index < BufferSize; index++ ) {
  349. TempVal+=Buffer[index];
  350. };
  351. return (uint8_t)(TempVal & 0x00FF);
  352. };
  353. // Calculating the Check Sum for Ping package
  354. uint16_t checksumPing(void *b, size_t len){
  355. uint16_t *buf = (uint16_t *) b;
  356. uint32_t sum=0;
  357. uint16_t result;
  358. for ( sum = 0; len > 1; len -= 2 )
  359. sum += *buf++;
  360. if ( len == 1 )
  361. sum += *(unsigned char*)buf;
  362. sum = (sum >> 16) + (sum & 0xFFFF);
  363. sum += (sum >> 16);
  364. result = ~sum;
  365. return result;
  366. };
  367. CArray *explode( char Delim, CSTR S_Str ){
  368. bool ws = false;
  369. CArray * RetVal = NULL;
  370. CSTR b_ptr = S_Str;
  371. U16 s_ptr = 0;
  372. if( S_Str ){
  373. RetVal = new CArray;
  374. if( RetVal ){
  375. while ((U8)S_Str[s_ptr]) {
  376. if( (( U8 ) Delim ) == (( U8 )S_Str[s_ptr]) ){
  377. if (!ws) {
  378. ws = true;
  379. S_Str[s_ptr] = (char)0;
  380. RetVal->push_back( b_ptr );
  381. s_ptr++;
  382. b_ptr = &S_Str[s_ptr];
  383. } else s_ptr++;
  384. } else {
  385. ws = false;
  386. s_ptr++;
  387. };
  388. };
  389. RetVal->push_back( b_ptr );
  390. };
  391. };
  392. return RetVal;
  393. };
  394. CSTR implode( CArray *Data, CSTR S_Str, U8 Start, U8 End ){
  395. CSTR RetVal;
  396. U8 RStart = Start;
  397. U8 REnd = End;
  398. if( Start > Data->size() ) return NULL;
  399. if( End < Start ) return NULL;
  400. if( End > Data->size() ) REnd = (U8)(Data->size()-1);
  401. if( strlen( (*Data).at( RStart )) ){
  402. RetVal = (CSTR) malloc( strlen( (*Data).at( RStart )) + 1 );
  403. sprintf( RetVal,"%s",(*Data).at( RStart ));
  404. } else {
  405. RetVal = (CSTR) malloc( 1 );
  406. *RetVal=(char)0;
  407. };
  408. if (RetVal != NULL) {
  409. for (int i = RStart + 1; i < REnd; i++) {
  410. RetVal = (CSTR)realloc(RetVal, strlen(RetVal) + strlen((*Data).at(i)) + 1 + strlen(S_Str));
  411. sprintf(RetVal, "%s%s%s", RetVal, S_Str, (*Data).at(i));
  412. };
  413. };
  414. return RetVal;
  415. };
  416. int getIP4( if_info **arrParam, int max_ifs ){
  417. int index = 0;
  418. #ifdef __linux__
  419. struct ifaddrs *addrs, *tmp;
  420. getifaddrs(&addrs);
  421. tmp = addrs;
  422. //arrParam= malloc( 10 * sezeof(if_info) );
  423. while (tmp){
  424. if (tmp->ifa_addr && tmp->ifa_addr->sa_family == AF_INET){
  425. if( strncmp( tmp->ifa_name, "lo", 2 ) != 0 ){
  426. (*arrParam)[index].if_name = (CSTR)malloc(strlen(tmp->ifa_name)+1);
  427. snprintf( (*arrParam)[index].if_name, strlen(tmp->ifa_name)+1,"%s", tmp->ifa_name );
  428. (*arrParam)[index].if_addr = (CSTR)malloc(16);
  429. struct sockaddr_in *pAddr = (struct sockaddr_in *)tmp->ifa_addr;
  430. snprintf( (*arrParam)[index].if_addr, 16, "%s", inet_ntoa(pAddr->sin_addr) );
  431. index++;
  432. if( index == max_ifs ) break;
  433. }
  434. };
  435. tmp = tmp->ifa_next;
  436. };
  437. freeifaddrs(addrs);
  438. #endif
  439. return index;
  440. };
  441. #ifdef __linux__
  442. long long getFreeSpace( CSTR DirName ){
  443. struct statvfs fiData;
  444. if( ( statvfs( DirName, &fiData ) ) < 0 ) {
  445. return -1;
  446. };
  447. return fiData.f_bsize * fiData.f_bfree;
  448. };
  449. long long getTotalSpace( CSTR DirName ){
  450. struct statvfs fiData;
  451. if( ( statvfs( DirName, &fiData ) ) < 0 ) {
  452. return -1;
  453. };
  454. return fiData.f_bsize * fiData.f_blocks;
  455. };
  456. #else
  457. __int64 getFreeSpace( CSTR DirName ){
  458. return -1;
  459. };
  460. __int64 getTotalSpace( CSTR DirName ){
  461. return -1;
  462. };
  463. #endif
  464. CSTR getTStr( CSTR Fmt ){
  465. //CSTR RetVal = NULL;
  466. char DateStr[200];
  467. time_t t;
  468. struct tm *tmp;
  469. t = time(NULL);
  470. tmp = gmtime(&t);
  471. if( tmp == NULL) {
  472. fprintf(stderr, "localtime!");
  473. return NULL;
  474. };
  475. if( Fmt == NULL ) {
  476. if( strftime( DateStr, 200, "%F %T GMT", tmp )==0 ){
  477. fprintf( stderr, "strftime returned 0" );
  478. return NULL;
  479. };
  480. } else {
  481. if( strftime( DateStr, 200, Fmt, tmp )==0 ){
  482. fprintf( stderr, "strftime returned 0" );
  483. return NULL;
  484. };
  485. }
  486. //RetVal =(CSTR) malloc( strlen(DateStr) +1 );
  487. //if( RetVal != NULL ) strcpy( RetVal, DateStr );
  488. #ifdef __linux__
  489. return strdup(DateStr);
  490. #else
  491. return _strdup(DateStr);
  492. #endif
  493. };
  494. #ifdef __linux__
  495. uint64_t getMilliseconds( void ){
  496. uint64_t tMilliseconds = (uint64_t) 0;
  497. struct timeval tData;
  498. struct timezone tTZ;
  499. tTZ.tz_minuteswest = 0;
  500. tTZ.tz_dsttime = 0;
  501. if( gettimeofday( &tData, &tTZ ) == -1 ){
  502. tMilliseconds = (uint64_t) 0;
  503. #ifdef DEBUG
  504. URGENT_LOG( "EbioMat'!");
  505. #endif
  506. } else {
  507. tMilliseconds = (uint64_t) tData.tv_sec;
  508. tMilliseconds = ( tMilliseconds * 1000 );
  509. tMilliseconds += (uint64_t)( tData.tv_usec / 1000 );
  510. //URGENT_LOG("MilSecCollected!");
  511. };
  512. #else
  513. #include<ctime>
  514. unsigned __int64 getMilliseconds( void ){
  515. unsigned __int64 tMilliseconds;
  516. SYSTEMTIME time;
  517. GetSystemTime(&time);
  518. tMilliseconds = (uint32_t)std::time(NULL);
  519. tMilliseconds *= 1000;
  520. tMilliseconds += time.wMilliseconds;
  521. #endif
  522. return tMilliseconds;
  523. };
  524. #ifdef __linux__
  525. bool enumerateDevices( enumeratorInfo *mlist, const char *dTemplate){
  526. DIR *d;
  527. struct dirent *dir;
  528. DeviceInfo *tDevInfo;
  529. if( mlist == NULL )return false;
  530. if( dTemplate == NULL )return false;
  531. for( int i = 0; i < mlist->size(); i++ ) {
  532. delete( mlist->at(i) );
  533. };
  534. mlist->clear();
  535. d = opendir("/dev");
  536. if( d ){
  537. while((dir = readdir(d)) != NULL){
  538. if( dir->d_type == DT_CHR || dir->d_type == DT_BLK ){
  539. if( strncmp( dir->d_name, dTemplate, 5 ) == 0 ) {
  540. tDevInfo = (DeviceInfo *) malloc( sizeof(DeviceInfo) );
  541. sprintf( tDevInfo->DevicePath, "/dev/%s", dir->d_name );
  542. tDevInfo->FriendlyName[0] =(char)0;
  543. if( strncmp( dTemplate, "video", 5 ) == 0 ){
  544. FILE *fd;
  545. char CMD[256];
  546. sprintf( CMD, "v4l2-ctl -d %s -D", tDevInfo->DevicePath );
  547. fd = popen( CMD, "r");
  548. if( fd != NULL ) {
  549. char * line = NULL;
  550. size_t len = 0;
  551. ssize_t rd;
  552. while ((rd = getline(&line, &len, fd)) != -1) {
  553. char *tS1, *tS2;
  554. // Future use strcasestr() or make both stings same case
  555. if( ( tS1 = strstr( line, "Bus info" )) != NULL ) {
  556. tS2 = trim( strstr( tS1, ":" ) + 2 );
  557. sprintf( tDevInfo->FriendlyName, "%s", tS2 );
  558. };
  559. };
  560. fclose( fd );
  561. if( line ) free( line );
  562. };
  563. };
  564. mlist->push_back( tDevInfo );
  565. //printf("%s\n", dir->d_name );
  566. };
  567. //if( strncmp( dir->d_name, "ttyS", 4 ) == 0 ){
  568. // tBuffer = ( char * ) malloc( strlen(dir->d_name)+1 );
  569. // strncpy( tBuffer, dir->d_name, strlen(dir->d_name)+1 );
  570. // mlist->pushBack( tBuffer );
  571. // //printf("%s\n", dir->d_name );
  572. //};
  573. };
  574. };
  575. closedir(d);
  576. } else {
  577. return false;
  578. };
  579. return true;
  580. };
  581. #else
  582. #ifndef IID_PPV_ARGS
  583. extern "C++" {
  584. template<typename T> void** IID_PPV_ARGS_Helper(T** pp)
  585. {
  586. // make sure everyone derives from IUnknown
  587. static_cast<IUnknown*>(*pp);
  588. return reinterpret_cast<void**>(pp);
  589. }
  590. }
  591. #define IID_PPV_ARGS(ppType) __uuidof(**(ppType)), IID_PPV_ARGS_Helper(ppType)
  592. #endif
  593. #pragma comment(lib,"Strmiids.lib")
  594. bool enumerateDevices(enumeratorInfo *mlist, REFGUID category){
  595. HRESULT hr;
  596. bool initOk = true;
  597. hr = CoInitializeEx( NULL, COINIT_SPEED_OVER_MEMORY | COINIT_MULTITHREADED | COINIT_DISABLE_OLE1DDE );
  598. if( hr == E_INVALIDARG ){
  599. printf("Couldn't Init! hr=0x%x", hr);
  600. return false;
  601. } else if ( hr == E_OUTOFMEMORY ){
  602. printf("Couldn't Init! hr=0x%x", hr);
  603. return false;
  604. } else if ( hr == E_UNEXPECTED ){
  605. printf("Couldn't Init! hr=0x%x", hr);
  606. return false;
  607. } else if( hr == RPC_E_CHANGED_MODE ){
  608. initOk = false;
  609. };
  610. if( hr == S_OK || hr == RPC_E_CHANGED_MODE ){
  611. // Create the system device enumerator
  612. ICreateDevEnum *pDevEnum;
  613. hr = CoCreateInstance( CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pDevEnum) );
  614. if( hr != S_OK ){
  615. printf("Couldn't create system enumerator! hr=0x%x", hr);
  616. CoUninitialize();
  617. return false;
  618. };
  619. if( pDevEnum != NULL ){
  620. IEnumMoniker *pClsEnum = NULL;
  621. hr = pDevEnum->CreateClassEnumerator(category, &pClsEnum, 0);
  622. if( hr == S_FALSE ){
  623. // The category is empty. Treat as an error.
  624. pDevEnum->Release();
  625. CoUninitialize();
  626. return false;
  627. };
  628. if( pClsEnum != NULL ){
  629. IMoniker *pMoniker = NULL;
  630. while( pClsEnum->Next(1, &pMoniker, NULL) == S_OK ){
  631. IPropertyBag *pPropBag = NULL;
  632. hr = pMoniker->BindToStorage(0, 0, IID_PPV_ARGS(&pPropBag));
  633. if( FAILED(hr) ){
  634. pMoniker->Release();
  635. continue;
  636. };
  637. VARIANT var;
  638. VariantInit(&var);
  639. DeviceInfo *tRec;
  640. tRec = (DeviceInfo *)malloc(sizeof(DeviceInfo));
  641. // Get description or friendly name.
  642. hr = pPropBag->Read(L"Description", &var, 0);
  643. if( FAILED(hr) ){
  644. hr = pPropBag->Read(L"FriendlyName", &var, 0);
  645. };
  646. if (SUCCEEDED(hr)){
  647. if (tRec != NULL) {
  648. sprintf(tRec->FriendlyName, "%S", var.bstrVal);
  649. mlist->push_back(tRec);
  650. };
  651. VariantClear(&var);
  652. };
  653. hr = pPropBag->Write(L"FriendlyName", &var);
  654. // WaveInID applies only to audio capture devices.
  655. hr = pPropBag->Read(L"WaveInID", &var, 0);
  656. if (SUCCEEDED(hr)){
  657. //printf("WaveIn ID: %d\n", var.lVal);
  658. VariantClear(&var);
  659. };
  660. hr = pPropBag->Read(L"DevicePath", &var, 0);
  661. if (SUCCEEDED(hr)){
  662. // The device path is not intended for display.
  663. if( tRec != NULL) {
  664. sprintf(tRec->DevicePath, "%S", var.bstrVal);
  665. };
  666. VariantClear(&var);
  667. };
  668. pPropBag->Release();
  669. pMoniker->Release();
  670. };
  671. pClsEnum->Release();
  672. };
  673. pDevEnum->Release();
  674. };
  675. if( initOk ) CoUninitialize();
  676. };
  677. return true;
  678. };
  679. #endif
  680. localIpAddresses g_vLocalIpAdressess;
  681. bool g_bAdressesAreDiscovered = false;
  682. #ifdef __linux__
  683. #include <linux/if.h>
  684. localIpAddresses *getLocalIP(){
  685. // Check for Filter LO and TUN Devices!
  686. if( !g_bAdressesAreDiscovered ) {
  687. struct ifaddrs *ifaddr, *ifa;
  688. localIp4Info *tIpAddress = NULL;
  689. if (getifaddrs(&ifaddr) != -1) {
  690. /* Walk through linked list, maintaining head pointer so we can free list later */
  691. for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
  692. // Filter Loopback if
  693. if( strstr( ifa->ifa_name, "lo" ) != NULL ) continue;
  694. // Filter vpn TUN if
  695. if( strstr( ifa->ifa_name, "tun" ) != NULL ) continue;
  696. char protocol[IFNAMSIZ] = {0};
  697. if (ifa->ifa_addr == NULL || ifa->ifa_addr->sa_family != AF_INET) continue;
  698. tIpAddress = (localIp4Info*)malloc(sizeof(localIp4Info));
  699. if(tIpAddress!=NULL){
  700. sprintf( tIpAddress->address , "%d.%d.%d.%d",
  701. (uint8_t)ifa->ifa_addr->sa_data[2],
  702. (uint8_t)ifa->ifa_addr->sa_data[3],
  703. (uint8_t)ifa->ifa_addr->sa_data[4],
  704. (uint8_t)ifa->ifa_addr->sa_data[5]
  705. );
  706. sprintf( tIpAddress->netmask , "%d.%d.%d.%d",
  707. (uint8_t)ifa->ifa_netmask->sa_data[2],
  708. (uint8_t)ifa->ifa_netmask->sa_data[3],
  709. (uint8_t)ifa->ifa_netmask->sa_data[4],
  710. (uint8_t)ifa->ifa_netmask->sa_data[5]
  711. );
  712. sprintf( tIpAddress->broadcast , "%d.%d.%d.%d",
  713. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[2],
  714. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[3],
  715. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[4],
  716. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[5]
  717. );
  718. g_vLocalIpAdressess.push_back(tIpAddress);
  719. };
  720. };
  721. freeifaddrs(ifaddr);
  722. };
  723. g_bAdressesAreDiscovered = true;
  724. };
  725. return &g_vLocalIpAdressess;
  726. };
  727. #else
  728. //https://stackoverflow.com/questions/5213629/how-to-enumerate-all-available-network-interfaces
  729. #include <iphlpapi.h>
  730. #pragma comment(lib, "IPHLPAPI.lib")
  731. localIpAddresses* getLocalIP() {
  732. char psBuffer[128];
  733. FILE* fd;
  734. BYTE mB;
  735. localIp4Info* tIpAddress;
  736. if (!g_bAdressesAreDiscovered) {
  737. ULONG outBufLen = 0;
  738. DWORD dwRetVal = 0;
  739. IP_ADAPTER_INFO* pAdapterInfos = (IP_ADAPTER_INFO*)malloc(sizeof(IP_ADAPTER_INFO));
  740. // retry up to 5 times, to get the adapter infos needed
  741. for (int i = 0; i < 5 && (dwRetVal == ERROR_BUFFER_OVERFLOW || dwRetVal == NO_ERROR); ++i) {
  742. dwRetVal = GetAdaptersInfo(pAdapterInfos, &outBufLen);
  743. if (dwRetVal == NO_ERROR) {
  744. break;
  745. } else {
  746. if (dwRetVal == ERROR_BUFFER_OVERFLOW) {
  747. free(pAdapterInfos);
  748. pAdapterInfos = (IP_ADAPTER_INFO*)malloc(outBufLen);
  749. } else {
  750. //pAdapterInfos = 0;
  751. break;
  752. };
  753. };
  754. };
  755. if (dwRetVal == NO_ERROR) {
  756. IP_ADAPTER_INFO* pAdapterInfo = pAdapterInfos;
  757. while (pAdapterInfo) {
  758. IP_ADDR_STRING* pIpAddress = &(pAdapterInfo->IpAddressList);
  759. while (pIpAddress != 0) {
  760. uint8_t A, B, C, D;
  761. uint8_t M, O, S, K, G, U, T, E;
  762. uint32_t Z1, Z2, Z3, Z4;
  763. int RetVal;
  764. RetVal = sscanf(pIpAddress->IpAddress.String, "%d.%d.%d.%d", &Z1, &Z2, &Z3, &Z4);
  765. A = Z1 & 0xFF;
  766. B = Z2 & 0xFF;
  767. C = Z3 & 0xFF;
  768. D = Z4 & 0xFF;
  769. RetVal = sscanf(pIpAddress->IpMask.String, "%d.%d.%d.%d", &Z1, &Z2, &Z3, &Z4);
  770. M = Z1 & 0xFF;
  771. O = Z2 & 0xFF;
  772. S = Z3 & 0xFF;
  773. K = Z4 & 0xFF;
  774. //
  775. // <<<<
  776. // here pAdapterInfo->Address should contain the MAC address
  777. // >>>>
  778. //
  779. if (A > 0) {
  780. tIpAddress = (localIp4Info*)malloc(sizeof(localIp4Info));
  781. if (tIpAddress != NULL) {
  782. sprintf(tIpAddress->address, "%d.%d.%d.%d", A, B, C, D );
  783. sprintf(tIpAddress->netmask, "%d.%d.%d.%d", M, O, S, K );
  784. G = (A & M) | (~M);
  785. U = (B & O) | (~O);
  786. T = (C & S) | (~S);
  787. E = (D & K) | (~K);
  788. sprintf(tIpAddress->broadcast, "%d.%d.%d.%d", G, U, T, E );
  789. g_vLocalIpAdressess.push_back(tIpAddress);
  790. };
  791. };
  792. pIpAddress = pIpAddress->Next;
  793. };
  794. pAdapterInfo = pAdapterInfo->Next;
  795. };
  796. };
  797. free(pAdapterInfos);
  798. };
  799. return &g_vLocalIpAdressess;
  800. };
  801. #endif
  802. #ifdef DEBUG
  803. #undef URGENT_LOG
  804. #endif