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.

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