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.

848 lines
24 KiB

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