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.

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