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.

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