int main(void) { WSADATA Data; SOCKADDR_IN recvSockAddr; SOCKET recvSocket; struct hostent * remoteHost; char * ip; const char * host_name = "pb-homework.appspot.com"; char maxBuff[MAXBUFLEN] = "\0"; int status = initDLL(Data); if(status == 1) { fprintf(stderr, "Cannot initialize dll - WSAStartup failed."); exit(1); } remoteHost = gethostbyname(host_name); ip = inet_ntoa(*(struct in_addr *)*remoteHost->h_addr_list); memset(&recvSockAddr, 0, sizeof(recvSockAddr)); recvSockAddr.sin_port=htons(PORT); recvSockAddr.sin_family=AF_INET; recvSockAddr.sin_addr.s_addr= inet_addr(ip); recvSocket = socket_create(); socket_connect(recvSocket, recvSockAddr); socket_sendRequest(host_name, recvSocket); socket_receiveInfo(recvSocket, maxBuff); getSecret(maxBuff, host_name, recvSocket); socket_receiveInfo(recvSocket, maxBuff); printf("******************************************\n" "Answer to request from R.A. server\n" "%s\n" "******************************************\n", maxBuff); // Find the longest word here! char longest[80] = "\0"; longestWord(maxBuff, longest); printf("\n\n******************************************\n" "Longest word was: [%s]\n" "******************************************\n", longest); socket_sendPost(recvSocket, host_name, longest); socket_receiveInfo(recvSocket, maxBuff); printf("\n\n******************************************\n" "Answer from R.A. server to my POST:\n" "%s\n" "******************************************\n", maxBuff); // Close all sockets. closesocket(recvSocket); // Free memory associated with sockets. WSACleanup(); return 0; }
bool CycleTimerHelper::initValues() { debugOutput( DEBUG_LEVEL_VERBOSE, "(%p) Init values...\n", this ); Util::MutexLockHelper lock(*m_update_lock); // initialize the 'prev ctr' values uint64_t local_time; int maxtries2 = 10; do { debugOutput( DEBUG_LEVEL_VERBOSE, "Read CTR...\n" ); if(!m_Parent.readCycleTimerReg(&m_cycle_timer_prev, &local_time)) { debugError("Could not read cycle timer register\n"); return false; } if (m_cycle_timer_prev == 0) { debugOutput(DEBUG_LEVEL_VERBOSE, "Bogus CTR: %08X on try %02d\n", m_cycle_timer_prev, maxtries2); } debugOutput( DEBUG_LEVEL_VERBOSE, " read : CTR: %11u, local: %17"PRIu64"\n", m_cycle_timer_prev, local_time); debugOutput(DEBUG_LEVEL_VERBOSE, " ctr : 0x%08X %11"PRIu64" (%03us %04ucy %04uticks)\n", (uint32_t)m_cycle_timer_prev, (uint64_t)CYCLE_TIMER_TO_TICKS(m_cycle_timer_prev), (unsigned int)CYCLE_TIMER_GET_SECS( m_cycle_timer_prev ), (unsigned int)CYCLE_TIMER_GET_CYCLES( m_cycle_timer_prev ), (unsigned int)CYCLE_TIMER_GET_OFFSET( m_cycle_timer_prev ) ); } while (m_cycle_timer_prev == 0 && maxtries2--); m_cycle_timer_ticks_prev = CYCLE_TIMER_TO_TICKS(m_cycle_timer_prev); #if IEEE1394SERVICE_USE_CYCLETIMER_DLL debugOutput( DEBUG_LEVEL_VERBOSE, "requesting DLL re-init...\n" ); Util::SystemTimeSource::SleepUsecRelative(1000); // some time to settle if(!initDLL()) { debugError("(%p) Could not init DLL\n", this); return false; } // make the DLL re-init itself as if it were started up m_first_run = true; #endif debugOutput( DEBUG_LEVEL_VERBOSE, "ready...\n" ); return true; }
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) { switch (fdwReason) { case DLL_PROCESS_ATTACH: //MessageBox(NULL, "AntiChatSpam Loaded", "Plugin", MB_OK); initDLL(); break; case DLL_PROCESS_DETACH: FILTER = FALSE; WaitForSingleObject(hThread, INFINITE); closeDB(); CloseHandle(chatStackMutex); delete plog; break; case DLL_THREAD_ATTACH: case DLL_THREAD_DETACH: break; } return TRUE; // succesful }
bool CycleTimerHelper::Execute() { debugOutput( DEBUG_LEVEL_ULTRA_VERBOSE, "Execute %p...\n", this); #ifdef DEBUG uint64_t now = m_Parent.getCurrentTimeAsUsecs(); int diff = now - m_last_loop_entry; if(diff < 100) { debugOutputExtreme(DEBUG_LEVEL_VERY_VERBOSE, "(%p) short loop detected (%d usec), cnt: %d\n", this, diff, m_successive_short_loops); m_successive_short_loops++; if(m_successive_short_loops > 100) { debugError("Shutting down runaway thread\n"); return false; } } else { // reset the counter m_successive_short_loops = 0; } m_last_loop_entry = now; #endif if (!m_first_run) { // wait for the next update period //#if DEBUG_EXTREME_ENABLE #ifdef DEBUG ffado_microsecs_t now = Util::SystemTimeSource::getCurrentTimeAsUsecs(); int sleep_time = m_sleep_until - now; debugOutput( DEBUG_LEVEL_ULTRA_VERBOSE, "(%p) Sleep until %"PRId64"/%f (now: %"PRId64", diff=%d) ...\n", this, m_sleep_until, m_next_time_usecs, now, sleep_time); #endif Util::SystemTimeSource::SleepUsecAbsolute(m_sleep_until); debugOutput( DEBUG_LEVEL_ULTRA_VERBOSE, " (%p) back...\n", this); } else { // Since getCycleTimerTicks() is called below, // m_shadow_vars[m_current_shadow_idx] must contain valid data. On // the first run through, however, it won't because the contents of // m_shadow_vars[] are only set later on in this function. Thus // set up some vaguely realistic values to prevent unnecessary // delays when reading the cycle timer for the first time. struct compute_vars new_vars; new_vars.ticks = (uint64_t)(m_current_time_ticks); new_vars.usecs = (uint64_t)m_current_time_usecs; new_vars.rate = getRate(); m_shadow_vars[0] = new_vars; } uint32_t cycle_timer; uint64_t local_time; int64_t usecs_late; int ntries=10; uint64_t cycle_timer_ticks; int64_t err_ticks; bool not_good; // if the difference between the predicted value at readout time and the // actual value seems to be too large, retry reading the cycle timer // some host controllers return bogus values on some reads // (looks like a non-atomic update of the register) do { debugOutput( DEBUG_LEVEL_ULTRA_VERBOSE, "(%p) reading cycle timer register...\n", this); if(!readCycleTimerWithRetry(&cycle_timer, &local_time, 10)) { debugError("Could not read cycle timer register\n"); return false; } usecs_late = local_time - m_sleep_until; cycle_timer_ticks = CYCLE_TIMER_TO_TICKS(cycle_timer); // calculate the CTR_TICKS we expect to read at "local_time" // then calculate the difference with what we actually read, // taking wraparound into account. If these deviate too much // from eachother then read the register again (bogus read). int64_t expected_ticks = getCycleTimerTicks(local_time); err_ticks = diffTicks(cycle_timer_ticks, expected_ticks); // check for unrealistic CTR reads (NEC controller does that sometimes) not_good = (-err_ticks > 1*TICKS_PER_CYCLE || err_ticks > 1*TICKS_PER_CYCLE); if(not_good) { debugOutput(DEBUG_LEVEL_VERBOSE, "(%p) have to retry CTR read, diff unrealistic: diff: %"PRId64", max: +/- %u (try: %d) %"PRId64"\n", this, err_ticks, 1*TICKS_PER_CYCLE, ntries, expected_ticks); // sleep half a cycle to make sure the hardware moved on Util::SystemTimeSource::SleepUsecRelative(USECS_PER_CYCLE / 2); } } while(not_good && --ntries && !m_first_run && !m_unhandled_busreset); // grab the lock after sleeping, otherwise we can't be interrupted by // the busreset thread (lower prio) // also grab it after reading the CTR register such that the jitter between // wakeup and read is as small as possible Util::MutexLockHelper lock(*m_update_lock); // the difference between the measured and the expected time int64_t diff_ticks = diffTicks(cycle_timer_ticks, (int64_t)m_next_time_ticks); // // simulate a random scheduling delay between (0-10ms) // ffado_microsecs_t tmp = Util::SystemTimeSource::SleepUsecRandom(10000); // debugOutput( DEBUG_LEVEL_VERBOSE, " (%p) random sleep of %u usecs...\n", this, tmp); if(m_unhandled_busreset) { debugOutput(DEBUG_LEVEL_VERBOSE, "(%p) Skipping DLL update due to unhandled busreset\n", this); m_sleep_until += m_usecs_per_update; // keep the thread running return true; } debugOutputExtreme( DEBUG_LEVEL_ULTRA_VERBOSE, " read : CTR: %11u, local: %17"PRIu64"\n", cycle_timer, local_time); debugOutputExtreme(DEBUG_LEVEL_ULTRA_VERBOSE, " ctr : 0x%08X %11"PRIu64" (%03us %04ucy %04uticks)\n", (uint32_t)cycle_timer, (uint64_t)cycle_timer_ticks, (unsigned int)TICKS_TO_SECS( (uint64_t)cycle_timer_ticks ), (unsigned int)TICKS_TO_CYCLES( (uint64_t)cycle_timer_ticks ), (unsigned int)TICKS_TO_OFFSET( (uint64_t)cycle_timer_ticks ) ); if (m_first_run) { if(!initDLL()) { debugError("(%p) Could not init DLL\n", this); return false; } m_first_run = false; } else if (diff_ticks > m_ticks_per_update * 20) { debugOutput(DEBUG_LEVEL_VERBOSE, "re-init dll due to too large tick diff: %"PRId64" >> %f\n", diff_ticks, (float)(m_ticks_per_update * 20)); if(!initDLL()) { debugError("(%p) Could not init DLL\n", this); return false; } } else { // calculate next sleep time m_sleep_until += m_usecs_per_update; // correct for the latency between the wakeup and the actual CTR // read. The only time we can trust is the time returned by the // CTR read kernel call, since that (should be) atomically read // together with the ctr register itself. // if we are usecs_late usecs late // the cycle timer has ticked approx ticks_late ticks too much // if we are woken up early (which shouldn't happen according to POSIX) // the cycle timer has ticked approx -ticks_late too little int64_t ticks_late = (usecs_late * TICKS_PER_SECOND) / 1000000LL; // the corrected difference between predicted and actual ctr // i.e. DLL error signal int64_t diff_ticks_corr; if (ticks_late >= 0) { diff_ticks_corr = diff_ticks - ticks_late; debugOutputExtreme(DEBUG_LEVEL_ULTRA_VERBOSE, "diff_ticks_corr=%"PRId64", diff_ticks = %"PRId64", ticks_late = %"PRId64"\n", diff_ticks_corr, diff_ticks, ticks_late); } else { debugError("Early wakeup, should not happen!\n"); // recover diff_ticks_corr = diff_ticks + ticks_late; } #ifdef DEBUG // makes no sense if not running realtime if(m_realtime && usecs_late > 1000) { debugOutput(DEBUG_LEVEL_VERBOSE, "Rather late wakeup: %"PRId64" usecs\n", usecs_late); } #endif // update the x-axis values m_current_time_ticks = m_next_time_ticks; // decide what coefficients to use // it should be ok to not do this in tick space // since diff_ticks_corr should not be near wrapping // (otherwise we are out of range. we need a few calls // w/o wrapping for this to work. That should not be // an issue as long as the update interval is smaller // than the wrapping interval.) // and coeff_b < 1, hence tmp is not near wrapping double diff_ticks_corr_d = (double)diff_ticks_corr; double step_ticks = (m_dll_coeff_b * diff_ticks_corr_d); debugOutputExtreme(DEBUG_LEVEL_VERY_VERBOSE, "diff_ticks_corr=%f, step_ticks=%f\n", diff_ticks_corr_d, step_ticks); // the same goes for m_dll_e2, which should be approx equal // to the ticks/usec rate (= 24.576) hence also not near // wrapping step_ticks += m_dll_e2; debugOutputExtreme(DEBUG_LEVEL_VERY_VERBOSE, "add %f ticks to step_ticks => step_ticks=%f\n", m_dll_e2, step_ticks); // it can't be that we have to update to a value in the past if(step_ticks < 0) { debugError("negative step: %f! (correcting to nominal)\n", step_ticks); // recover to an estimated value step_ticks = (double)m_ticks_per_update; } if(step_ticks > TICKS_PER_SECOND) { debugWarning("rather large step: %f ticks (> 1sec)\n", step_ticks); } // now add the step ticks with wrapping. m_next_time_ticks = (double)(addTicks((uint64_t)m_current_time_ticks, (uint64_t)step_ticks)); // update the DLL state m_dll_e2 += m_dll_coeff_c * diff_ticks_corr_d; // update the y-axis values m_current_time_usecs = m_next_time_usecs; m_next_time_usecs += m_usecs_per_update; debugOutputExtreme(DEBUG_LEVEL_VERY_VERBOSE, " usecs: current: %f next: %f usecs_late=%"PRId64" ticks_late=%"PRId64"\n", m_current_time_usecs, m_next_time_usecs, usecs_late, ticks_late); debugOutputExtreme(DEBUG_LEVEL_VERY_VERBOSE, " ticks: current: %f next: %f diff=%"PRId64"\n", m_current_time_ticks, m_next_time_ticks, diff_ticks); debugOutputExtreme(DEBUG_LEVEL_VERY_VERBOSE, " ticks: current: %011"PRIu64" (%03us %04ucy %04uticks)\n", (uint64_t)m_current_time_ticks, (unsigned int)TICKS_TO_SECS( (uint64_t)m_current_time_ticks ), (unsigned int)TICKS_TO_CYCLES( (uint64_t)m_current_time_ticks ), (unsigned int)TICKS_TO_OFFSET( (uint64_t)m_current_time_ticks ) ); debugOutputExtreme(DEBUG_LEVEL_VERY_VERBOSE, " ticks: next : %011"PRIu64" (%03us %04ucy %04uticks)\n", (uint64_t)m_next_time_ticks, (unsigned int)TICKS_TO_SECS( (uint64_t)m_next_time_ticks ), (unsigned int)TICKS_TO_CYCLES( (uint64_t)m_next_time_ticks ), (unsigned int)TICKS_TO_OFFSET( (uint64_t)m_next_time_ticks ) ); debugOutputExtreme(DEBUG_LEVEL_VERY_VERBOSE, " state: local: %11"PRIu64", dll_e2: %f, rate: %f\n", local_time, m_dll_e2, getRate()); } // prepare the new compute vars struct compute_vars new_vars; new_vars.ticks = (uint64_t)(m_current_time_ticks); new_vars.usecs = (uint64_t)m_current_time_usecs; new_vars.rate = getRate(); // get the next index unsigned int next_idx = (m_current_shadow_idx + 1) % CTRHELPER_NB_SHADOW_VARS; // update the next index position m_shadow_vars[next_idx] = new_vars; // then we can update the current index m_current_shadow_idx = next_idx; #ifdef DEBUG // do some verification // we re-read a valid ctr timestamp // then we use the attached system time to calculate // the DLL generated timestamp and we check what the // difference is if(!readCycleTimerWithRetry(&cycle_timer, &local_time, 10)) { debugError("Could not read cycle timer register (verify)\n"); return true; // true since this is a check only } cycle_timer_ticks = CYCLE_TIMER_TO_TICKS(cycle_timer); // only check when successful int64_t time_diff = local_time - new_vars.usecs; double y_step_in_ticks = ((double)time_diff) * new_vars.rate; int64_t y_step_in_ticks_int = (int64_t)y_step_in_ticks; uint64_t offset_in_ticks_int = new_vars.ticks; uint32_t dll_time; if (y_step_in_ticks_int > 0) { dll_time = addTicks(offset_in_ticks_int, y_step_in_ticks_int); } else { dll_time = substractTicks(offset_in_ticks_int, -y_step_in_ticks_int); } int32_t ctr_diff = cycle_timer_ticks-dll_time; debugOutput(DEBUG_LEVEL_ULTRA_VERBOSE, "(%p) CTR DIFF: HW %010"PRIu64" - DLL %010u = %010d (%s)\n", this, cycle_timer_ticks, dll_time, ctr_diff, (ctr_diff>0?"lag":"lead")); #endif return true; }
bool CPath::Init() { USES_CONVERSION; // Declare variables CString sDLLfile; char map[_MAX_PATH]; TCHAR szTemp[_MAX_PATH] = {0}; // Init windows NT dll initDLL(); // Find several paths _getcwd(map, _MAX_PATH); // Updater executable GetModuleFileName(NULL, szTemp, _MAX_PATH); m_sPathUpdaterExecutable = szTemp; // Updater (working) path m_sPathUpdater = map; m_sPathUpdater += _T("\\"); // Temp path GetTempPath(_MAX_PATH, szTemp); m_sPathTemp = szTemp; // System path GetSystemDirectory(szTemp, _MAX_PATH); m_sPathSystem = szTemp; m_sPathSystem += _T("\\"); // Program files path SHGetSpecialFolderPath(NULL, szTemp, CSIDL_PROGRAM_FILES, FALSE); m_sPathProgramfiles = szTemp; m_sPathProgramfiles += _T("\\"); // Application data path SHGetSpecialFolderPath(NULL, szTemp, CSIDL_APPDATA, FALSE); m_sPathAppData = szTemp; m_sPathAppData += _T("\\"); // Common application data path SHGetSpecialFolderPath(NULL, szTemp, CSIDL_COMMON_APPDATA, FALSE); m_sPathCommonAppData = szTemp; m_sPathCommonAppData += _T("\\"); // User home directory path SHGetSpecialFolderPath(NULL, szTemp, CSIDL_PROFILE, FALSE); m_sPathUserProfile = szTemp; m_sPathUserProfile += _T("\\"); // Quick launch - current user path m_sPathQuickLaunchCurrentUser = m_sPathAppData + _T("\\Microsoft\\Internet Explorer\\Quick Launch\\"); // Menu start - current user path SHGetSpecialFolderPath(NULL, szTemp, CSIDL_STARTMENU, FALSE); m_sPathMenuStartCurrentUser = szTemp; m_sPathMenuStartCurrentUser += _T("\\"); // Desktop - current user path SHGetSpecialFolderPath(NULL, szTemp, CSIDL_DESKTOPDIRECTORY, FALSE); m_sPathDesktopCurrentUser = szTemp; m_sPathDesktopCurrentUser += _T("\\"); // Quick launch - all users path m_sPathQuickLaunchAllUsers = m_sPathCommonAppData + _T("\\Microsoft\\Internet Explorer\\Quick Launch\\"); // Menu start - all users path SHGetSpecialFolderPath(NULL, szTemp, CSIDL_COMMON_STARTMENU, FALSE); m_sPathMenuStartAllUsers = szTemp; m_sPathMenuStartAllUsers += _T("\\"); // Desktop - all users path SHGetSpecialFolderPath(NULL, szTemp, CSIDL_COMMON_DESKTOPDIRECTORY, FALSE); m_sPathDesktopAllUsers = szTemp; m_sPathDesktopAllUsers += _T("\\"); // Windows Directory path GetWindowsDirectory(szTemp, _MAX_PATH); m_sPathWindowsDirectory = szTemp; m_sPathWindowsDirectory += _T("\\"); return true; }
//------------------------------------------------- BOOL APIENTRY DllMain(HINSTANCE hModule, DWORD reason, LPVOID reserved) { HANDLE hMapObject = NULL; /* handle to file mapping */ BOOL fInit, fIgnore; // ThreadData *pThread; switch (reason) { case DLL_PROCESS_ATTACH: /* Create a named file mapping object */ MyInstance = hModule; hMapObject = CreateFileMapping( (HANDLE) 0xFFFFFFFF, /* use paging file */ NULL, /* no security attr. */ PAGE_READWRITE, /* read/write access */ 0, /* size: high 32-bits */ sizeof(SharedMem), /* size: low 32-bits */ _TEXT("UniKeyHookSharedMem3.65 Release"));/* name of map object */ if (hMapObject == NULL) return FALSE; fInit = (GetLastError() != ERROR_ALREADY_EXISTS); /* Get a pointer to the file-mapped shared memory. */ pShMem = (SharedMem *) MapViewOfFile( hMapObject, /* object to map view of */ FILE_MAP_WRITE, /* read/write access */ 0, /* high offset: map from */ 0, /* low offset: beginning */ 0); /* default: map entire file */ if (pShMem == NULL) return FALSE; InitProcess(); if (fInit) initDLL(); // TlsIdx = TlsAlloc(); // InitThread(); break; /* * The DLL is detaching from a process due to * process termination or a call to FreeLibrary. */ /* case DLL_THREAD_ATTACH: InitThread(); break; case DLL_THREAD_DETACH: pThread = (ThreadData *)TlsGetValue(TlsIdx); if (pThread != NULL) delete pThread; break; */ case DLL_PROCESS_DETACH: /* Unmap shared memory from the process's address space. */ fIgnore = UnmapViewOfFile(pShMem); /* Close the process's handle to the file-mapping object. */ fIgnore = CloseHandle(hMapObject); // TlsFree(TlsIdx); break; default: break; } return TRUE; UNREFERENCED_PARAMETER(hModule); UNREFERENCED_PARAMETER(reserved); }