#include "basicFunctionality.h" #include "../../alg/generic/md5.h" #ifdef DEBUG #include "basicLogCollection.h" #define URGENT_LOG(X) basicLogCollection::getInstance()->Log("basic Functionality", NULL, X ) #endif bool fileExist(const char *strFilename) { struct stat stFileInfo; int intStat; intStat = stat( strFilename ,&stFileInfo); if( intStat == 0 ){ return true; }; return false; }; #ifndef __linux__ #else #include #include #include #include pid_t gettid( void ){ return syscall(SYS_gettid); }; #endif const char *__BuildDate = (__DATE__); const char *__BuildTime = (__TIME__); const char *__BuildTS = (__TIMESTAMP__); CSTR getBuildID( void ){ return NULL; }; /* Global Time Diff Value to correct Timestamps */ static int g_iTimeDiff = 0; #ifdef __linux__ unsigned long long getSHDTimeValue( void ){ unsigned long long tMilliseconds; struct timeval tData; struct timezone tTZ; tMilliseconds = 0; tTZ.tz_minuteswest = 0; tTZ.tz_dsttime = 0; if( gettimeofday( &tData, &tTZ ) == -1 ){ } else { tMilliseconds = tData.tv_sec; tMilliseconds = tMilliseconds *1000; tMilliseconds += tData.tv_usec / 1000; }; #else /* void TimetToFileTime( time_t t, LPFILETIME pft ){ LONGLONG ll = Int32x32To64(t, 10000000) + 116444736000000000; pft->dwLowDateTime = (DWORD) ll; pft->dwHighDateTime = ll >>32; }*/ unsigned __int64 getSHDTimeValue( void ){ unsigned __int64 tMilliseconds; SYSTEMTIME Ztime; GetSystemTime(&Ztime); SystemTimeToFileTime( &Ztime, (FILETIME*) &tMilliseconds ); tMilliseconds -= UNIX_TimeStamp_2_WINDOWS_FileTime_SHIFT; tMilliseconds = tMilliseconds / 10000; #endif tMilliseconds += g_iTimeDiff; return tMilliseconds; }; CSTR getSHDTimeStamp( void ){ CSTR RetVal = NULL; time_t rawtime; uint32_t msec; struct tm * ptm = NULL; time ( &rawtime ); ptm = gmtime ( &rawtime ); msec = getSHDTimeValue() % 1000; //2019-03-24T12:32:59.670Z if( ptm != NULL ) { RetVal = (CSTR) malloc( 128 ); if( RetVal!=NULL ){ sprintf( RetVal, "%04d-%02d-%02dT%02d:%02d:%02d.%03dZ", (ptm->tm_year+1900), (ptm->tm_mon+1), ptm->tm_mday, ptm->tm_hour, ptm->tm_min, ptm->tm_sec, msec ); }; }; return RetVal; }; CSTR getUniqID_MD5( void ){ CSTR RetVal; RetVal = (CSTR) malloc(64); if(RetVal) sprintf( RetVal, "%s", MD5String( getSHDTimeStamp() ) ); return RetVal; }; CSTR getUniqID_SHA256(void){ return NULL;}; CSTR getUniqID_UUID(void){ return NULL;}; CSTR trim( CSTR val ){ int i = 0; int j = strlen( val ); if ( j ){ while(( i < j ) && (((unsigned char)val[i])< 0x21 )) i++; while(( j > 0 ) && (((unsigned char)val[j])< 0x21 )) j--; if( ( i > 0 ) && ((j-i-1) > 0 ) ) memcpy( val, val+i, j-i-1 ); val[j-i+1]=(char)0; }; return val; }; // You must free the result if result is non-NULL. CSTR strReplace( CSTR orig, CSTR rep, CSTR with ){ char *result; // the return string char *ins; // the next insert point char *tmp; // varies int len_rep; // length of rep (the string to remove) int len_with; // length of with (the string to replace rep with) int len_front; // distance between rep and end of last rep int count; // number of replacements // sanity checks and initialization if (!orig || !rep) return NULL; len_rep = strlen(rep); if (len_rep == 0) return NULL; // empty rep causes infinite loop during count if (!with) with = ""; len_with = strlen(with); // count the number of replacements needed ins = orig; for (count = 0; tmp = strstr(ins, rep); ++count) { ins = tmp + len_rep; } tmp = result = (CSTR) malloc(strlen(orig) + (len_with - len_rep) * count + 1); if (!result) return NULL; // first time through the loop, all the variable are set correctly // from here on, // tmp points to the end of the result string // ins points to the next occurrence of rep in orig // orig points to the remainder of orig after "end of rep" while (count--) { ins = strstr(orig, rep); len_front = ins - orig; tmp = strncpy(tmp, orig, len_front) + len_front; tmp = strcpy(tmp, with) + len_with; orig += len_front + len_rep; // move to next "end of rep" } strcpy(tmp, orig); return result; }; uint8_t checksum8( uint8_t *Buffer, size_t BufferSize ){ uint16_t TempVal; TempVal = 0; for( int index = 0; index < BufferSize; index++ ) { TempVal+=Buffer[index]; }; return (uint8_t)(TempVal & 0x00FF); }; uint16_t checksum16( uint8_t *Buffer, size_t BufferSize ){ uint32_t TempVal; TempVal = 0; for( int index = 0; index < BufferSize; index++ ) { TempVal+=Buffer[index]; }; return (uint16_t)(TempVal & 0x0000FFFF); }; uint8_t checksumLGS( uint8_t *Buffer, size_t BufferSize ){ uint16_t TempVal; TempVal = 0; for( int index = 0; index < BufferSize; index++ ) { TempVal+=Buffer[index]; }; return (uint8_t)(TempVal & 0x00FF); }; CArray *explode( char Delim, CSTR S_Str ){ CArray *RetVal; CSTR b_ptr = S_Str; U16 s_ptr = 0; RetVal = NULL; if( S_Str ){ RetVal = new CArray; while( ( U8 ) S_Str[s_ptr] ){ if( (( U8 ) Delim ) == (( U8 )S_Str[s_ptr]) ){ S_Str[s_ptr] = ( char ) 0; RetVal->push_back( b_ptr ); s_ptr++; b_ptr = &S_Str[s_ptr]; } else { s_ptr++; }; }; RetVal->push_back( b_ptr ); }; return RetVal; }; CSTR implode( CArray *Data, CSTR S_Str, U8 Start, U8 End ){ CSTR RetVal; U8 RStart = Start; U8 REnd = End; if( Start > Data->size() ) return NULL; if( End < Start ) return NULL; if( End > Data->size() ) REnd = (U8)(Data->size()-1); if( strlen( (*Data).at( RStart )) ){ RetVal = (CSTR) malloc( strlen( (*Data).at( RStart )) + 1 ); sprintf( RetVal,"%s",(*Data).at( RStart )); } else { RetVal = (CSTR) malloc( 1 ); *RetVal=(char)0; }; if (RetVal != NULL) { for (int i = RStart + 1; i < REnd; i++) { RetVal = (CSTR)realloc(RetVal, strlen(RetVal) + strlen((*Data).at(i)) + 1 + strlen(S_Str)); sprintf(RetVal, "%s%s%s", RetVal, S_Str, (*Data).at(i)); }; }; return RetVal; }; int getIP4( if_info **arrParam, int max_ifs ){ int index = 0; #ifdef __linux__ struct ifaddrs *addrs, *tmp; getifaddrs(&addrs); tmp = addrs; //arrParam= malloc( 10 * sezeof(if_info) ); while (tmp){ if (tmp->ifa_addr && tmp->ifa_addr->sa_family == AF_INET){ if( strncmp( tmp->ifa_name, "lo", 2 ) != 0 ){ (*arrParam)[index].if_name = (CSTR)malloc(strlen(tmp->ifa_name)+1); snprintf( (*arrParam)[index].if_name, strlen(tmp->ifa_name)+1,"%s", tmp->ifa_name ); (*arrParam)[index].if_addr = (CSTR)malloc(16); struct sockaddr_in *pAddr = (struct sockaddr_in *)tmp->ifa_addr; snprintf( (*arrParam)[index].if_addr, 16, "%s", inet_ntoa(pAddr->sin_addr) ); index++; if( index == max_ifs ) break; } }; tmp = tmp->ifa_next; }; freeifaddrs(addrs); #endif return index; }; #ifdef __linux__ long long getFreeSpace( CSTR DirName ){ struct statvfs fiData; if( ( statvfs( DirName, &fiData ) ) < 0 ) { return -1; }; return fiData.f_bsize * fiData.f_bfree; }; long long getTotalSpace( CSTR DirName ){ struct statvfs fiData; if( ( statvfs( DirName, &fiData ) ) < 0 ) { return -1; }; return fiData.f_bsize * fiData.f_blocks; }; #else __int64 getFreeSpace( CSTR DirName ){ return -1; }; __int64 getTotalSpace( CSTR DirName ){ return -1; }; #endif CSTR getTStr( CSTR Fmt ){ //CSTR RetVal = NULL; char DateStr[200]; time_t t; struct tm *tmp; t = time(NULL); tmp = gmtime(&t); if( tmp == NULL) { fprintf(stderr, "localtime!"); return NULL; }; if( Fmt == NULL ) { if( strftime( DateStr, 200, "%F %T GMT", tmp )==0 ){ fprintf( stderr, "strftime returned 0" ); return NULL; }; } else { if( strftime( DateStr, 200, Fmt, tmp )==0 ){ fprintf( stderr, "strftime returned 0" ); return NULL; }; } //RetVal =(CSTR) malloc( strlen(DateStr) +1 ); //if( RetVal != NULL ) strcpy( RetVal, DateStr ); #ifdef __linux__ return strdup(DateStr); #else return _strdup(DateStr); #endif }; #ifdef __linux__ long long getMilliseconds( void ){ long long tMilliseconds; struct timeval tData; struct timezone tTZ; tMilliseconds = (long long) 0; tTZ.tz_minuteswest = 0; tTZ.tz_dsttime = 0; if( gettimeofday( &tData, &tTZ ) == -1 ){ tMilliseconds = (long long) 0; URGENT_LOG( "EbioMat'!"); } else { tMilliseconds = (long long) tData.tv_sec; tMilliseconds = ( tMilliseconds * 1000 ); tMilliseconds += (long long)( tData.tv_usec / 1000 ); //URGENT_LOG("MilSecCollected!"); }; #else #include unsigned __int64 getMilliseconds( void ){ unsigned __int64 tMilliseconds; SYSTEMTIME time; GetSystemTime(&time); tMilliseconds = (uint32_t)std::time(NULL); tMilliseconds *= 1000; tMilliseconds += time.wMilliseconds; #endif return tMilliseconds; }; #ifdef __linux__ bool enumerateDevices( enumeratorInfo *mlist, const char *dTemplate){ DIR *d; struct dirent *dir; DeviceInfo *tDevInfo; if( mlist == NULL )return false; if( dTemplate == NULL )return false; for( int i = 0; i < mlist->size(); i++ ) { delete( mlist->at(i) ); }; mlist->clear(); d = opendir("/dev"); if( d ){ while((dir = readdir(d)) != NULL){ if( dir->d_type == DT_CHR || dir->d_type == DT_BLK ){ if( strncmp( dir->d_name, dTemplate, 5 ) == 0 ) { tDevInfo = (DeviceInfo *) malloc( sizeof(DeviceInfo) ); sprintf( tDevInfo->DevicePath, "/dev/%s", dir->d_name ); tDevInfo->FriendlyName[0] =(char)0; if( strncmp( dTemplate, "video", 5 ) == 0 ){ FILE *fd; char CMD[256]; sprintf( CMD, "v4l2-ctl -d %s -D", tDevInfo->DevicePath ); fd = popen( CMD, "r"); if( fd != NULL ) { char * line = NULL; size_t len = 0; ssize_t rd; while ((rd = getline(&line, &len, fd)) != -1) { char *tS1, *tS2; // Future use strcasestr() or make both stings same case if( ( tS1 = strstr( line, "Bus info" )) != NULL ) { tS2 = trim( strstr( tS1, ":" ) + 2 ); sprintf( tDevInfo->FriendlyName, "%s", tS2 ); }; }; fclose( fd ); if( line ) free( line ); }; }; mlist->push_back( tDevInfo ); //printf("%s\n", dir->d_name ); }; //if( strncmp( dir->d_name, "ttyS", 4 ) == 0 ){ // tBuffer = ( char * ) malloc( strlen(dir->d_name)+1 ); // strncpy( tBuffer, dir->d_name, strlen(dir->d_name)+1 ); // mlist->pushBack( tBuffer ); // //printf("%s\n", dir->d_name ); //}; }; }; closedir(d); } else { return false; }; return true; }; #else #ifndef IID_PPV_ARGS extern "C++" { template void** IID_PPV_ARGS_Helper(T** pp) { // make sure everyone derives from IUnknown static_cast(*pp); return reinterpret_cast(pp); } } #define IID_PPV_ARGS(ppType) __uuidof(**(ppType)), IID_PPV_ARGS_Helper(ppType) #endif #pragma comment(lib,"Strmiids.lib") bool enumerateDevices(enumeratorInfo *mlist, REFGUID category){ HRESULT hr; bool initOk = true; hr = CoInitializeEx( NULL, COINIT_SPEED_OVER_MEMORY | COINIT_MULTITHREADED | COINIT_DISABLE_OLE1DDE ); if( hr == E_INVALIDARG ){ printf("Couldn't Init! hr=0x%x", hr); return false; } else if ( hr == E_OUTOFMEMORY ){ printf("Couldn't Init! hr=0x%x", hr); return false; } else if ( hr == E_UNEXPECTED ){ printf("Couldn't Init! hr=0x%x", hr); return false; } else if( hr == RPC_E_CHANGED_MODE ){ initOk = false; }; if( hr == S_OK || hr == RPC_E_CHANGED_MODE ){ // Create the system device enumerator ICreateDevEnum *pDevEnum; hr = CoCreateInstance( CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pDevEnum) ); if( hr != S_OK ){ printf("Couldn't create system enumerator! hr=0x%x", hr); CoUninitialize(); return false; }; if( pDevEnum != NULL ){ IEnumMoniker *pClsEnum = NULL; hr = pDevEnum->CreateClassEnumerator(category, &pClsEnum, 0); if( hr == S_FALSE ){ // The category is empty. Treat as an error. pDevEnum->Release(); CoUninitialize(); return false; }; if( pClsEnum != NULL ){ IMoniker *pMoniker = NULL; while( pClsEnum->Next(1, &pMoniker, NULL) == S_OK ){ IPropertyBag *pPropBag = NULL; hr = pMoniker->BindToStorage(0, 0, IID_PPV_ARGS(&pPropBag)); if( FAILED(hr) ){ pMoniker->Release(); continue; }; VARIANT var; VariantInit(&var); DeviceInfo *tRec; tRec = (DeviceInfo *)malloc(sizeof(DeviceInfo)); // Get description or friendly name. hr = pPropBag->Read(L"Description", &var, 0); if( FAILED(hr) ){ hr = pPropBag->Read(L"FriendlyName", &var, 0); }; if (SUCCEEDED(hr)){ if (tRec != NULL) { sprintf(tRec->FriendlyName, "%S", var.bstrVal); mlist->push_back(tRec); }; VariantClear(&var); }; hr = pPropBag->Write(L"FriendlyName", &var); // WaveInID applies only to audio capture devices. hr = pPropBag->Read(L"WaveInID", &var, 0); if (SUCCEEDED(hr)){ //printf("WaveIn ID: %d\n", var.lVal); VariantClear(&var); }; hr = pPropBag->Read(L"DevicePath", &var, 0); if (SUCCEEDED(hr)){ // The device path is not intended for display. if( tRec != NULL) { sprintf(tRec->DevicePath, "%S", var.bstrVal); }; VariantClear(&var); }; pPropBag->Release(); pMoniker->Release(); }; pClsEnum->Release(); }; pDevEnum->Release(); }; if( initOk ) CoUninitialize(); }; return true; }; #endif localIpAddresses g_vLocalIpAdressess; bool g_bAdressesAreDiscovered = false; #ifdef __linux__ #include localIpAddresses *getLocalIP(){ if( !g_bAdressesAreDiscovered ) { struct ifaddrs *ifaddr, *ifa; localIp4Info *tIpAddress = NULL; if (getifaddrs(&ifaddr) != -1) { /* Walk through linked list, maintaining head pointer so we can free list later */ for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { char protocol[IFNAMSIZ] = {0}; if (ifa->ifa_addr == NULL || ifa->ifa_addr->sa_family != AF_INET) continue; tIpAddress = (localIp4Info*)malloc(sizeof(localIp4Info)); if(tIpAddress!=NULL){ sprintf( tIpAddress->address , "%d.%d.%d.%d", (uint8_t)ifa->ifa_addr->sa_data[2], (uint8_t)ifa->ifa_addr->sa_data[3], (uint8_t)ifa->ifa_addr->sa_data[4], (uint8_t)ifa->ifa_addr->sa_data[5] ); sprintf( tIpAddress->netmask , "%d.%d.%d.%d", (uint8_t)ifa->ifa_netmask->sa_data[2], (uint8_t)ifa->ifa_netmask->sa_data[3], (uint8_t)ifa->ifa_netmask->sa_data[4], (uint8_t)ifa->ifa_netmask->sa_data[5] ); sprintf( tIpAddress->broadcast , "%d.%d.%d.%d", (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[2], (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[3], (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[4], (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[5] ); g_vLocalIpAdressess.push_back(tIpAddress); }; }; freeifaddrs(ifaddr); }; g_bAdressesAreDiscovered = true; }; return &g_vLocalIpAdressess; }; #else //https://stackoverflow.com/questions/5213629/how-to-enumerate-all-available-network-interfaces #include #pragma comment(lib, "IPHLPAPI.lib") localIpAddresses* getLocalIP() { char psBuffer[128]; FILE* fd; BYTE mB; localIp4Info* tIpAddress; if (!g_bAdressesAreDiscovered) { ULONG outBufLen = 0; DWORD dwRetVal = 0; IP_ADAPTER_INFO* pAdapterInfos = (IP_ADAPTER_INFO*)malloc(sizeof(IP_ADAPTER_INFO)); // retry up to 5 times, to get the adapter infos needed for (int i = 0; i < 5 && (dwRetVal == ERROR_BUFFER_OVERFLOW || dwRetVal == NO_ERROR); ++i) { dwRetVal = GetAdaptersInfo(pAdapterInfos, &outBufLen); if (dwRetVal == NO_ERROR) { break; } else { if (dwRetVal == ERROR_BUFFER_OVERFLOW) { free(pAdapterInfos); pAdapterInfos = (IP_ADAPTER_INFO*)malloc(outBufLen); } else { //pAdapterInfos = 0; break; }; }; }; if (dwRetVal == NO_ERROR) { IP_ADAPTER_INFO* pAdapterInfo = pAdapterInfos; while (pAdapterInfo) { IP_ADDR_STRING* pIpAddress = &(pAdapterInfo->IpAddressList); while (pIpAddress != 0) { uint8_t A, B, C, D; uint8_t M, O, S, K, G, U, T, E; uint32_t Z1, Z2, Z3, Z4; int RetVal; RetVal = sscanf(pIpAddress->IpAddress.String, "%d.%d.%d.%d", &Z1, &Z2, &Z3, &Z4); A = Z1 & 0xFF; B = Z2 & 0xFF; C = Z3 & 0xFF; D = Z4 & 0xFF; RetVal = sscanf(pIpAddress->IpMask.String, "%d.%d.%d.%d", &Z1, &Z2, &Z3, &Z4); M = Z1 & 0xFF; O = Z2 & 0xFF; S = Z3 & 0xFF; K = Z4 & 0xFF; // // <<<< // here pAdapterInfo->Address should contain the MAC address // >>>> // if (A > 0) { tIpAddress = (localIp4Info*)malloc(sizeof(localIp4Info)); if (tIpAddress != NULL) { sprintf(tIpAddress->address, "%d.%d.%d.%d", A, B, C, D ); sprintf(tIpAddress->netmask, "%d.%d.%d.%d", M, O, S, K ); G = (A & M) | (~M); U = (B & O) | (~O); T = (C & S) | (~S); E = (D & K) | (~K); sprintf(tIpAddress->broadcast, "%d.%d.%d.%d", G, U, T, E ); g_vLocalIpAdressess.push_back(tIpAddress); }; }; pIpAddress = pIpAddress->Next; }; pAdapterInfo = pAdapterInfo->Next; }; }; free(pAdapterInfos); }; return &g_vLocalIpAdressess; }; #endif #ifdef DEBUG #undef URGENT_LOG #endif