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.

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