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.

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