summaryrefslogtreecommitdiff
path: root/plugins/Ping/src/utils.cpp
blob: 9372f22eb0387c073734e41075bf4021e93fd376 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#include "common.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_PTR dwParam )
{
	POPUPDATA* ppd = ( POPUPDATA* )dwParam;

	if ( ServiceExists(MS_POPUP_ADDPOPUP))
		PUAddPopUp(ppd);

	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)) {
		POPUPDATA* ppd = ( POPUPDATA* )calloc( sizeof( POPUPDATA ), 1 );

		ppd->lchContact = NULL;
		ppd->lchIcon = (flags ? hIconResponding : hIconNotResponding);
		strncpy( ppd->lpzContactName, line1,MAX_CONTACTNAME);
		strncpy( ppd->lpzText, line2, MAX_SECONDLINE);

		ppd->colorBack = GetSysColor( COLOR_BTNFACE );
		ppd->colorText = GetSysColor( COLOR_WINDOWTEXT );
		ppd->iSeconds = 10;

		ppd->PluginWindowProc = NullWindowProc;
		ppd->PluginData = NULL;

		QueueUserAPC(sttMainThreadCallback, mainThread, ( ULONG )ppd );
	}
	else if(ServiceExists(MS_POPUP_ADDPOPUPCLASS)) {
		POPUPDATACLASS d = {sizeof(d), "pingpopups"};
		d.pwszTitle = (wchar_t *)line1;
		d.pwszText = (wchar_t *)line2;
		CallService(MS_POPUP_ADDPOPUPCLASS, 0, (LPARAM)&d);
	} else {
		MessageBox( NULL, line2, PLUG " Message", MB_OK | MB_ICONINFORMATION );
		return;
	}
}

// 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_PTR 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 {
		
			ICMP_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_PTR PingDisableAll(WPARAM wParam, LPARAM lParam) {
	PINGLIST pl;
	CallService(PLUG "/GetPingList", 0, (LPARAM)&pl);
	for(pinglist_it i = pl.begin(); i != pl.end(); ++i) {
		i->status = PS_DISABLED;
		i->miss_count = 0;
	}
	CallService(PLUG "/SetPingList", (WPARAM)&pl, 0);
	return 0;
}

INT_PTR PingEnableAll(WPARAM wParam, LPARAM lParam) {
	PINGLIST pl;
	CallService(PLUG "/GetPingList", 0, (LPARAM)&pl);
	for(pinglist_it i = pl.begin(); i != pl.end(); ++i) {
		if(i->status == PS_DISABLED) {
			i->status = PS_NOTRESPONDING;
		}
	}
	CallService(PLUG "/SetPingList", (WPARAM)&pl, 0);
	return 0;
}


INT_PTR ToggleEnabled(WPARAM wParam, LPARAM lParam) {
	int retval = 0;
	PINGLIST pl;
	CallService(PLUG "/GetPingList", 0, (LPARAM)&pl);
	for(pinglist_it i = pl.begin(); i != pl.end(); ++i) {
		if(i->item_id == (DWORD)wParam) {

			if(i->status == PS_DISABLED)
				i->status = PS_NOTRESPONDING;
			else {
				i->status = PS_DISABLED;
				i->miss_count = 0;
				retval = 1;
			}
		}
	}
	CallService(PLUG "/SetPingList", (WPARAM)&pl, 0);
	return 0;
}

INT_PTR EditContact(WPARAM wParam, LPARAM lParam) {
	PINGLIST pl;
	HWND hwndList = (HWND)CallService(MS_CLUI_GETHWND, 0, 0);

	CallService(PLUG "/GetPingList", 0, (LPARAM)&pl);
	for(pinglist_it i = pl.begin(); i != pl.end(); ++i) {
		if(i->item_id == (DWORD)wParam) {

			add_edit_addr = *i;
	
			if(DialogBox(hInst, MAKEINTRESOURCE(IDD_DIALOG3), hwndList, DlgProcDestEdit) == IDOK) {
	
				*i = add_edit_addr;
				CallService(PLUG "/SetAndSavePingList", (WPARAM)&pl, 0);
				return 0;
			}
		}
	}
	return 1;
}

INT_PTR DblClick(WPARAM wParam, LPARAM lParam) {
	PINGLIST pl;
	CallService(PLUG "/GetPingList", 0, (LPARAM)&pl);
	for(pinglist_it i = pl.begin(); i != pl.end(); ++i) {
		if(i->item_id == (DWORD)wParam) {
			if(strlen(i->pszCommand)) {
				ShellExecute(0, "open", i->pszCommand, i->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(!db_get(0, "PingPlug", buf, &dbv)) {
		strncpy(pa.pszName, dbv.pszVal, MAX_PINGADDRESS_STRING_LENGTH);
		db_free(&dbv);
	} else
		strcpy(pa.pszName, Translate("Unknown Address"));

	mir_snprintf(buf, 256, "Label%d", index);
	if(!db_get(0, "PingPlug", buf, &dbv)) {
		strncpy(pa.pszLabel, dbv.pszVal, MAX_PINGADDRESS_STRING_LENGTH);
		db_free(&dbv);
	} else
		strcpy(pa.pszLabel, Translate("Unknown"));

	mir_snprintf(buf, 256, "Port%d", index);
	pa.port = (int)db_get_dw(0, "PingPlug", buf, -1);

	mir_snprintf(buf, 256, "Proto%d", index);
	if(!db_get(0, "PingPlug", buf, &dbv)) {
		strncpy(pa.pszProto, dbv.pszVal, MAX_PINGADDRESS_STRING_LENGTH);
		db_free(&dbv);
		mir_snprintf(buf, 256, "Status%d", index);
		pa.set_status = db_get_w(0, "PingPlug", buf, ID_STATUS_ONLINE);
		mir_snprintf(buf, 256, "Status2%d", index);
		pa.get_status = db_get_w(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(db_get_b(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 = db_get_dw(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.push_back(pa);
	}
	write_ping_addresses();
	LeaveCriticalSection(&list_cs);
}

HANDLE hPopupClass;
HICON  hIconResponding, hIconNotResponding, hIconTesting, hIconDisabled;

static int OnShutdown(WPARAM, LPARAM)
{
	Popup_UnregisterClass(hPopupClass);
	return 0;
}

static int ReloadIcons(WPARAM wParam, LPARAM lParam)
{
	hIconResponding = Skin_GetIcon("ping_responding");
	hIconNotResponding = Skin_GetIcon("ping_not_responding");
	hIconTesting = Skin_GetIcon("ping_testing");
	hIconDisabled = Skin_GetIcon("ping_disabled");

	RefreshWindow(0, 0);
	return 0;
}

static IconItem iconList[] = 
{
	{ LPGEN("Responding"), "ping_responding", IDI_ICON_RESPONDING },
	{ LPGEN("Not Responding"), "ping_not_responding", IDI_ICON_NOTRESPONDING },
	{ LPGEN("Testing"), "ping_testing", IDI_ICON_TESTING },
	{ LPGEN("Disabled"), "ping_disabled", IDI_ICON_DISABLED },
};

void InitUtils()
{
	Icon_Register(hInst, LPGENT("Ping"), iconList, SIZEOF(iconList));

	hIconResponding = Skin_GetIcon("ping_responding");
	hIconNotResponding = Skin_GetIcon("ping_not_responding");
	hIconTesting = Skin_GetIcon("ping_testing");
	hIconDisabled = Skin_GetIcon("ping_disabled");

	HookEvent(ME_SKIN2_ICONSCHANGED, ReloadIcons);

	POPUPCLASS test = { sizeof(test) };
	test.flags = PCF_TCHAR;
	test.hIcon = hIconResponding;
	test.iSeconds = -1;
	test.ptszDescription = TranslateT("Ping");
	test.pszName = "pingpopups";
	test.PluginWindowProc = NullWindowProc;
	if (hPopupClass = Popup_RegisterClass(&test))
		HookEvent(ME_SYSTEM_PRESHUTDOWN, OnShutdown);
}