summaryrefslogtreecommitdiff
path: root/ping/utils.cpp
diff options
context:
space:
mode:
authorsje <sje@4f64403b-2f21-0410-a795-97e2b3489a10>2006-11-01 14:44:17 +0000
committersje <sje@4f64403b-2f21-0410-a795-97e2b3489a10>2006-11-01 14:44:17 +0000
commitb61df7acc1235b3b2a66217d0d7358199c5d37f9 (patch)
tree70f74e5b04249d9ce70259ef70b38bcdf869cf64 /ping/utils.cpp
parent9c6d8a640911d336cc1e758310bbfccaf547d2d2 (diff)
git-svn-id: https://server.scottellis.com.au/svn/mim_plugs@11 4f64403b-2f21-0410-a795-97e2b3489a10
Diffstat (limited to 'ping/utils.cpp')
-rw-r--r--ping/utils.cpp362
1 files changed, 362 insertions, 0 deletions
diff --git a/ping/utils.cpp b/ping/utils.cpp
new file mode 100644
index 0000000..06a89a0
--- /dev/null
+++ b/ping/utils.cpp
@@ -0,0 +1,362 @@
+#include "common.h"
+#include "utils.h"
+
+LRESULT CALLBACK NullWindowProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam )
+{
+ switch( message ) {
+ case WM_COMMAND: {
+ PUDeletePopUp( hWnd );
+ break;
+ }
+
+ case WM_CONTEXTMENU:
+ PUDeletePopUp( hWnd );
+ break;
+ }
+
+ return DefWindowProc(hWnd, message, wParam, lParam);
+}
+
+void CALLBACK sttMainThreadCallback( ULONG dwParam )
+{
+ POPUPDATAEX* ppd = ( POPUPDATAEX* )dwParam;
+
+ if ( ServiceExists(MS_POPUP_ADDPOPUPEX) )
+ CallService( MS_POPUP_ADDPOPUPEX, ( WPARAM )ppd, 0 );
+ else
+ if ( ServiceExists(MS_POPUP_ADDPOPUP) )
+ CallService( MS_POPUP_ADDPOPUP, ( WPARAM )ppd, 0 );
+
+ free( ppd );
+}
+
+void __stdcall ShowPopup( const char* line1, const char* line2, int flags )
+{
+ if(CallService(MS_SYSTEM_TERMINATED, 0, 0)) return;
+
+ if ( !ServiceExists( MS_POPUP_ADDPOPUP )) {
+ MessageBox( NULL, line2, PLUG " Message", MB_OK | MB_ICONINFORMATION );
+ return;
+ }
+
+ POPUPDATAEX* ppd = ( POPUPDATAEX* )calloc( sizeof( POPUPDATAEX ), 1 );
+
+ ppd->lchContact = NULL;
+ ppd->lchIcon = (flags ? hIconResponding : hIconNotResponding);
+ strcpy( ppd->lpzContactName, line1 );
+ strcpy( ppd->lpzText, line2 );
+
+ ppd->colorBack = GetSysColor( COLOR_BTNFACE );
+ ppd->colorText = GetSysColor( COLOR_WINDOWTEXT );
+ ppd->iSeconds = 10;
+
+ ppd->PluginWindowProc = ( WNDPROC )NullWindowProc;
+ ppd->PluginData = NULL;
+
+ QueueUserAPC( sttMainThreadCallback , mainThread, ( ULONG )ppd );
+}
+
+// service functions
+
+// wParam is zero
+// lParam is address of PINGADDRESS structure where ping result is placed (i.e. modifies 'responding'
+// and 'round_trip_time')
+int PluginPing(WPARAM wParam,LPARAM lParam)
+{
+ PINGADDRESS *pa = (PINGADDRESS *)lParam;
+
+ if(pa->port == -1) {
+ // ICMP echo
+ if(use_raw_ping) {
+ pa->round_trip_time = raw_ping(pa->pszName, options.ping_timeout * 1000);
+ pa->responding = (pa->round_trip_time != -1);
+ } else {
+
+ IP_ECHO_REPLY result;
+ pa->responding = ICMP::get_instance()->ping(pa->pszName, result);
+ if(pa->responding)
+ pa->round_trip_time = (short)result.RoundTripTime;
+ else
+ pa->round_trip_time = -1;
+ }
+ } else if(hNetlibUser) {
+ // TCP connect
+
+ clock_t start_tcp = clock();
+
+ //GetLocalTime(&systime);
+ NETLIBOPENCONNECTION conn = {0};
+ conn.cbSize = sizeof(NETLIBOPENCONNECTION);
+ conn.szHost = pa->pszName;
+ conn.wPort = pa->port;
+ conn.timeout = options.ping_timeout;
+
+ HANDLE s = (HANDLE)CallService(MS_NETLIB_OPENCONNECTION, (WPARAM)hNetlibUser, (LPARAM)&conn);
+
+ clock_t end_tcp = clock();
+
+ if(s) {
+ LINGER l;
+ char buf[1024];
+ SOCKET socket = (SOCKET)CallService(MS_NETLIB_GETSOCKET, (WPARAM)s, (LPARAM)NLOCF_HTTP);
+ l.l_onoff = 1;
+ l.l_linger = 0;
+ setsockopt(socket, SOL_SOCKET, SO_LINGER, (char *)&l, sizeof(l));
+
+ Netlib_Send(s, "OUT\r\n\r\n", 7, 0); //MSG_RAW);
+
+ //Sleep(ICMP::get_instance()->get_timeout());
+ Sleep(options.ping_timeout * 1000);
+ unsigned long bytes_remaining;
+ ioctlsocket(socket, FIONBIO, &bytes_remaining);
+
+ if(bytes_remaining > 0) {
+ int retval, rx = 0;
+ while((retval = Netlib_Recv(s, buf, 1024, 0)) != SOCKET_ERROR && (retval > 0) && rx < 2048) {
+ rx += retval; // recv at most 2kb before closing connection
+ }
+ }
+ closesocket(socket);
+ pa->responding = true;
+ pa->round_trip_time = (int)(((end_tcp - start_tcp) / (double)CLOCKS_PER_SEC) * 1000);
+
+ Netlib_CloseHandle(s);
+ } else {
+ pa->responding = false;
+ pa->round_trip_time = -1;
+ }
+
+ }
+ return 0;
+}
+
+void Lock(CRITICAL_SECTION *cs, char *lab) {
+// if(logging) {
+// std::ostringstream oss1;
+// oss1 << "Locking cs: " << cs << ", " << lab;
+// CallService(PROTO "/Log", (WPARAM)oss1.str().c_str(), 0);
+// }
+ EnterCriticalSection(cs);
+// if(logging) {
+// std::ostringstream oss2;
+// oss2 << "Locked cs: " << cs;
+// CallService(PROTO "/Log", (WPARAM)oss2.str().c_str(), 0);
+// }
+}
+
+void Unlock(CRITICAL_SECTION *cs) {
+// if(logging) {
+// std::ostringstream oss1;
+// oss1 << "Unlocking cs: " << cs;
+// CallService(PROTO "/Log", (WPARAM)oss1.str().c_str(), 0);
+// }
+ LeaveCriticalSection(cs);
+}
+
+int PingDisableAll(WPARAM wParam, LPARAM lParam) {
+ PINGLIST pl;
+ CallService(PLUG "/GetPingList", 0, (LPARAM)&pl);
+ for(PINGLIST::Iterator i = pl.start(); i.has_val(); i.next()) {
+ i.val().status = PS_DISABLED;
+ i.val().miss_count = 0;
+ }
+ CallService(PLUG "/SetPingList", (WPARAM)&pl, 0);
+ return 0;
+}
+
+int PingEnableAll(WPARAM wParam, LPARAM lParam) {
+ PINGLIST pl;
+ CallService(PLUG "/GetPingList", 0, (LPARAM)&pl);
+ for(PINGLIST::Iterator i = pl.start(); i.has_val(); i.next()) {
+ if(i.val().status == PS_DISABLED) {
+ i.val().status = PS_NOTRESPONDING;
+ }
+ }
+ CallService(PLUG "/SetPingList", (WPARAM)&pl, 0);
+ return 0;
+}
+
+
+int ToggleEnabled(WPARAM wParam, LPARAM lParam) {
+ int retval = 0;
+ PINGLIST pl;
+ CallService(PLUG "/GetPingList", 0, (LPARAM)&pl);
+ for(PINGLIST::Iterator i = pl.start(); i.has_val(); i.next()) {
+ if(i.val().item_id == (DWORD)wParam) {
+
+ if(i.val().status == PS_DISABLED)
+ i.val().status = PS_NOTRESPONDING;
+ else {
+ i.val().status = PS_DISABLED;
+ i.val().miss_count = 0;
+ retval = 1;
+ }
+ }
+ }
+ CallService(PLUG "/SetPingList", (WPARAM)&pl, 0);
+ return 0;
+}
+
+int EditContact(WPARAM wParam, LPARAM lParam) {
+ PINGLIST pl;
+ HWND hwndList = (HWND)CallService(MS_CLUI_GETHWND, 0, 0);
+
+ CallService(PLUG "/GetPingList", 0, (LPARAM)&pl);
+ for(PINGLIST::Iterator i = pl.start(); i.has_val(); i.next()) {
+ if(i.val().item_id == (DWORD)wParam) {
+
+ add_edit_addr = i.val();
+
+ if(DialogBox(hInst, MAKEINTRESOURCE(IDD_DIALOG3), hwndList, DlgProcDestEdit) == IDOK) {
+
+ i.val() = add_edit_addr;
+ CallService(PLUG "/SetAndSavePingList", (WPARAM)&pl, 0);
+ return 0;
+ }
+ }
+ }
+ return 1;
+}
+
+int DblClick(WPARAM wParam, LPARAM lParam) {
+ PINGLIST pl;
+ CallService(PLUG "/GetPingList", 0, (LPARAM)&pl);
+ for(PINGLIST::Iterator i = pl.start(); i.has_val(); i.next()) {
+ if(i.val().item_id == (DWORD)wParam) {
+ if(strlen(i.val().pszCommand)) {
+ ShellExecute(0, "open", i.val().pszCommand, i.val().pszParams, 0, SW_SHOW);
+ } else {
+ return CallService(PLUG "/ToggleEnabled", wParam, 0);
+ }
+ }
+ }
+ return 0;
+}
+
+
+void import_ping_address(int index, PINGADDRESS &pa) {
+ DBVARIANT dbv;
+ char buf[256];
+ mir_snprintf(buf, 256, "Address%d", index);
+ if(!DBGetContactSetting(0, "PingPlug", buf, &dbv)) {
+ strncpy(pa.pszName, dbv.pszVal, MAX_PINGADDRESS_STRING_LENGTH);
+ DBFreeVariant(&dbv);
+ } else
+ strcpy(pa.pszName, Translate("Unknown Address"));
+
+ mir_snprintf(buf, 256, "Label%d", index);
+ if(!DBGetContactSetting(0, "PingPlug", buf, &dbv)) {
+ strncpy(pa.pszLabel, dbv.pszVal, MAX_PINGADDRESS_STRING_LENGTH);
+ DBFreeVariant(&dbv);
+ } else
+ strcpy(pa.pszLabel, Translate("Unknown"));
+
+ mir_snprintf(buf, 256, "Port%d", index);
+ pa.port = (int)DBGetContactSettingDword(0, "PingPlug", buf, -1);
+
+ mir_snprintf(buf, 256, "Proto%d", index);
+ if(!DBGetContactSetting(0, "PingPlug", buf, &dbv)) {
+ strncpy(pa.pszProto, dbv.pszVal, MAX_PINGADDRESS_STRING_LENGTH);
+ DBFreeVariant(&dbv);
+ mir_snprintf(buf, 256, "Status%d", index);
+ pa.set_status = DBGetContactSettingWord(0, "PingPlug", buf, ID_STATUS_ONLINE);
+ mir_snprintf(buf, 256, "Status2%d", index);
+ pa.get_status = DBGetContactSettingWord(0, "PingPlug", buf, ID_STATUS_OFFLINE);
+ } else
+ pa.pszProto[0] = '\0';
+
+
+ pa.responding = false;
+ pa.round_trip_time = 0;
+ pa.miss_count = 0;
+ pa.index = index;
+ pa.pszCommand[0] = '\0';
+ pa.pszParams[0] = '\0';
+
+ pa.item_id = 0;
+ mir_snprintf(buf, 256, "Enabled%d", index);
+ if(DBGetContactSettingByte(0, "PingPlug", buf, 1) == 1)
+ pa.status = PS_NOTRESPONDING;
+ else
+ pa.status = PS_DISABLED;
+}
+
+// read in addresses from old pingplug
+void import_ping_addresses() {
+ int count = DBGetContactSettingDword(0, "PingPlug", "NumEntries", 0);
+ PINGADDRESS pa;
+
+ EnterCriticalSection(&list_cs);
+ list_items.clear();
+ for(int index = 0; index < count; index++) {
+ import_ping_address(index, pa);
+ list_items.add(pa);
+ }
+ write_ping_addresses();
+ LeaveCriticalSection(&list_cs);
+}
+
+HANDLE hIcoLibIconsChanged;
+
+HICON hIconResponding, hIconNotResponding, hIconTesting, hIconDisabled;
+
+int ReloadIcons(WPARAM wParam, LPARAM lParam) {
+ hIconResponding = (HICON)CallService(MS_SKIN2_GETICON, 0, (LPARAM)"ping_responding");
+ hIconNotResponding = (HICON)CallService(MS_SKIN2_GETICON, 0, (LPARAM)"ping_not_responding");
+ hIconTesting = (HICON)CallService(MS_SKIN2_GETICON, 0, (LPARAM)"ping_testing");
+ hIconDisabled = (HICON)CallService(MS_SKIN2_GETICON, 0, (LPARAM)"ping_disabled");
+
+ RefreshWindow(0, 0);
+ return 0;
+}
+
+void InitIcons() {
+ if(ServiceExists(MS_SKIN2_ADDICON)) {
+ SKINICONDESC2 sid;
+
+ sid.cbSize = sizeof(SKINICONDESC2);
+ sid.pszSection = "Ping";
+
+ sid.pszDescription = Translate("Responding");
+ sid.pszName = "ping_responding";
+ sid.pszDefaultFile = "ping.dll";
+ sid.iDefaultIndex = 0;
+ sid.hDefaultIcon = (HICON)LoadImage(hInst, MAKEINTRESOURCE(IDI_ICON_RESPONDING), IMAGE_ICON, 16, 16, 0);
+ CallService(MS_SKIN2_ADDICON, 0, (LPARAM)&sid);
+
+ sid.pszDescription = Translate("Not Responding");
+ sid.pszName = "ping_not_responding";
+ sid.pszDefaultFile = "ping.dll";
+ sid.iDefaultIndex = 1;
+ sid.hDefaultIcon = (HICON)LoadImage(hInst, MAKEINTRESOURCE(IDI_ICON_NOTRESPONDING), IMAGE_ICON, 16, 16, 0);//LR_LOADTRANSPARENT | LR_LOADMAP3DCOLORS );
+ CallService(MS_SKIN2_ADDICON, 0, (LPARAM)&sid);
+
+ sid.pszDescription = Translate("Testing");
+ sid.pszName = "ping_testing";
+ sid.pszDefaultFile = "ping.dll";
+ sid.iDefaultIndex = 2;
+ sid.hDefaultIcon = (HICON)LoadImage(hInst, MAKEINTRESOURCE(IDI_ICON_TESTING), IMAGE_ICON, 16, 16, 0);//LR_LOADTRANSPARENT | LR_LOADMAP3DCOLORS );
+ CallService(MS_SKIN2_ADDICON, 0, (LPARAM)&sid);
+
+ sid.pszDescription = Translate("Disabled");
+ sid.pszName = "ping_disabled";
+ sid.pszDefaultFile = "ping.dll";
+ sid.iDefaultIndex = 3;
+ sid.hDefaultIcon = (HICON)LoadImage(hInst, MAKEINTRESOURCE(IDI_ICON_DISABLED), IMAGE_ICON, 16, 16, 0);//LR_LOADTRANSPARENT | LR_LOADMAP3DCOLORS );
+ CallService(MS_SKIN2_ADDICON, 0, (LPARAM)&sid);
+
+ hIconResponding = (HICON)CallService(MS_SKIN2_GETICON, 0, (LPARAM)"ping_responding");
+ hIconNotResponding = (HICON)CallService(MS_SKIN2_GETICON, 0, (LPARAM)"ping_not_responding");
+ hIconTesting = (HICON)CallService(MS_SKIN2_GETICON, 0, (LPARAM)"ping_testing");
+ hIconDisabled = (HICON)CallService(MS_SKIN2_GETICON, 0, (LPARAM)"ping_disabled");
+
+ hIcoLibIconsChanged = HookEvent(ME_SKIN2_ICONSCHANGED, ReloadIcons);
+ } else {
+ hIconResponding = (HICON)LoadImage(hInst, MAKEINTRESOURCE(IDI_ICON_RESPONDING), IMAGE_ICON, 16, 16, 0);//LR_LOADTRANSPARENT | LR_LOADMAP3DCOLORS );
+ hIconNotResponding = (HICON)LoadImage(hInst, MAKEINTRESOURCE(IDI_ICON_NOTRESPONDING), IMAGE_ICON, 16, 16, 0);//LR_LOADTRANSPARENT | LR_LOADMAP3DCOLORS );
+ hIconTesting = (HICON)LoadImage(hInst, MAKEINTRESOURCE(IDI_ICON_TESTING), IMAGE_ICON, 16, 16, 0);//LR_LOADTRANSPARENT | LR_LOADMAP3DCOLORS );
+ hIconDisabled = (HICON)LoadImage(hInst, MAKEINTRESOURCE(IDI_ICON_DISABLED), IMAGE_ICON, 16, 16, 0);//LR_LOADTRANSPARENT | LR_LOADMAP3DCOLORS );
+ }
+}
+
+