void dumpConnections(HWND hWnd){ hEdit=hWnd; HANDLE hConnMgrReady = ConnMgrApiReadyEvent(); DWORD dwWait = WaitForSingleObject(hConnMgrReady, 3000); switch (dwWait){ case WAIT_OBJECT_0: break; case WAIT_FAILED: break; case WAIT_ABANDONED: break; default: break; } CloseHandle(hConnMgrReady); filelog(L"dumpConnections...\n"); CONNMGR_CONNECTION_DETAILED_STATUS* pStatusBuffer=NULL; DWORD dwBufferSize=0; HRESULT hResult=0; //query the needed buffer size hResult = ConnMgrQueryDetailedStatus(NULL, &dwBufferSize); if(hResult==(HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER))) { BYTE* bv = (BYTE*) malloc(dwBufferSize); CONNMGR_CONNECTION_DETAILED_STATUS *s= (CONNMGR_CONNECTION_DETAILED_STATUS*)&bv[0]; hResult= ConnMgrQueryDetailedStatus(s, &dwBufferSize); for (CONNMGR_CONNECTION_DETAILED_STATUS *p= s ; p ; p= p->pNext) { dump_details(p); } free(pStatusBuffer); free(bv); } }
UUExperimentSetup::UUExperimentSetup() { InterfaceRefList iflist; InterfaceRef ethIface = NULL; // Get all local interfaces, even the ones which are not 'up' if (getLocalInterfaceList(iflist, false) < 0) { fprintf(stderr, "Could not find any local interfaces\n"); return; } // Find the first one returned, it should be the interface we use // for the experiments for (InterfaceRefList::iterator it = iflist.begin(); it != iflist.end(); it++) { ethIface = *it; if (ethIface->getType() == IFTYPE_ETHERNET || ethIface->getType() == IFTYPE_WIFI) break; } if (!ethIface) { fprintf(stderr, "No valid local interface to configure\n"); return; } #if 0 HRESULT res = S_OK; HANDLE hConnMgrReady = ConnMgrApiReadyEvent(); DWORD retval = WaitForSingleObject(hConnMgrReady, 5000); if (retval == WAIT_TIMEOUT) { fprintf(stderr, "Connection manager is not ready...\n"); } else if (retval == WAIT_FAILED) { fprintf(stderr, "Wait for Connection manager failed!\n"); } else { for (int i = 0; res == S_OK; i++) { CONNMGR_DESTINATION_INFO network_info; res = ConnMgrEnumDestinations(i, &network_info); if (res == S_OK) { printf("Network name %S\n", network_info.szDescription); } } } CloseHandle(hConnMgrReady); #endif struct interface_config *ic = get_interface_config(ethIface->getIdentifier()); if (!ic) { fprintf(stderr, "Could not find matching configuration for interface %s\n", ethIface->getIdentifierStr()); return; } printf("Configuring interface %s\n", ethIface->getIdentifierStr()); unsigned long ifindex = 0; if (!WindowsWiFiUtils::getDeviceIndex(ethIface->getName(), &ifindex)) { fprintf(stderr, "Could not get interface index\n"); return; } if (!WindowsWiFiUtils::setNetworkMode(ifindex, Ndis802_11IBSS)) { fprintf(stderr, "Could not set ad hoc mode\n"); return; } if (!WindowsWiFiUtils::setWiFiSSID(ifindex, EXPERIMENT_SSID)) { fprintf(stderr, "Could not set WiFi SSID\n"); return; } if (!WindowsWiFiUtils::setChannel(ifindex, EXPERIMENT_CHANNEL)) { fprintf(stderr, "Could not set WiFi channel to %u\n", EXPERIMENT_CHANNEL); return; } // Add the IPv4 address. /* if (!WindowsWiFiUtils::addIPv4Address(ifindex, ic->ip, ic->mask)) { fprintf(stderr, "Could not set IP address %s/%s\n", ic->ip, ic->mask); return false;; } */ printf("Interface %s with mac=%s was configured with ip=%s/%s and SSID=%s on channel=%u\n", ethIface->getName(), ethIface->getIdentifierStr(), ic->ip, ic->mask, EXPERIMENT_SSID, EXPERIMENT_CHANNEL); }
DWORD WINAPI OnConnection(LPVOID lpParam) { Event *me = (Event *)lpParam; Configuration *conf; wstring subType; int delay, iterations, curIterations = 0, waitDelay = 0; HANDLE eventHandle, hConnManager = INVALID_HANDLE_VALUE; HRESULT hResult; MSG msg; BOOL connected = FALSE; eventHandle = me->getEvent(); me->setStatus(EVENT_RUNNING); conf = me->getConf(); try { delay = conf->getInt(L"delay") * 1000; } catch (...) { delay = INFINITE; } try { iterations = conf->getInt(L"iter"); } catch (...) { iterations = MAXINT; } // Attendiamo che il ConnectionManager sia pronto hConnManager = ConnMgrApiReadyEvent(); if (hConnManager == NULL || hConnManager == INVALID_HANDLE_VALUE) { DBG_TRACE(L"Debug - Connection.cpp - Connection FAILED [Connection Manager not present]\n", 5, FALSE); me->setStatus(EVENT_STOPPED); return 0; } if (WaitForSingleObject(hConnManager, 5000) != WAIT_OBJECT_0) { CloseHandle(hConnManager); DBG_TRACE(L"Debug - Connection.cpp - Connection FAILED [Connection Manager not ready]\n", 5, FALSE); me->setStatus(EVENT_STOPPED); return 0; } CloseHandle(hConnManager); HWND hWin = CreateWindow(L"STATIC", L"Window", 0, 0, 0, 0, 0, NULL, NULL, GetModuleHandle(NULL), NULL); if (hWin == NULL) { DBG_TRACE(L"Debug - Connection.cpp - Connection FAILED [Cannot create notification window]\n", 5, FALSE); me->setStatus(EVENT_STOPPED); return 0; } UINT uStatusChange = RegisterWindowMessage(CONNMGR_STATUS_CHANGE_NOTIFICATION_MSG); if (uStatusChange == 0) { DestroyWindow(hWin); DBG_TRACE(L"Debug - Connection.cpp - Connection FAILED [Cannot register window message]\n", 5, FALSE); me->setStatus(EVENT_STOPPED); return 0; } hResult = ConnMgrRegisterForStatusChangeNotification(TRUE, hWin); if (hResult != S_OK) { DestroyWindow(hWin); DBG_TRACE(L"Debug - Connection.cpp - Connection FAILED [Cannot register for notifications]\n", 5, FALSE); me->setStatus(EVENT_STOPPED); return 0; } DBG_TRACE(L"Debug - Connection.cpp - Connection Event started\n", 5, FALSE); LOOP { int now = GetTickCount(); if (PeekMessage(&msg, hWin, 0, 0, PM_REMOVE)) { if (msg.message == uStatusChange) { switch (msg.wParam) { case CONNMGR_STATUS_CONNECTED: WaitForSingleObject(eventHandle, 40000); if (me->shouldStop()) { ConnMgrRegisterForStatusChangeNotification(FALSE, hWin); DestroyWindow(hWin); me->setStatus(EVENT_STOPPED); DBG_TRACE(L"Debug - Connection.cpp - Connection clean stop [0]\n", 5, FALSE); return 0; } DBG_TRACE(L"Debug - Connection.cpp - Connection triggered [In Action]\n", 5, FALSE); me->triggerStart(); connected = TRUE; break; case CONNMGR_STATUS_DISCONNECTED: DBG_TRACE(L"Debug - Connection.cpp - Connection triggered [Out Action]\n", 5, FALSE); // Si potrebbe eseguire l'exit action qui me->triggerEnd(); connected = FALSE; break; default: break; } } DispatchMessage(&msg); } waitDelay += GetTickCount() - now; if (connected && waitDelay >= delay && curIterations < iterations) { waitDelay = 0; me->triggerRepeat(); curIterations++; } WaitForSingleObject(eventHandle, 5000); if (me->shouldStop()) { ConnMgrRegisterForStatusChangeNotification(FALSE, hWin); DestroyWindow(hWin); me->setStatus(EVENT_STOPPED); DBG_TRACE(L"Debug - Connection.cpp - Connection clean stop [0]\n", 5, FALSE); return 0; } } me->setStatus(EVENT_STOPPED); return 0; }