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.

724 lines
21 KiB

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