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.

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