diff options
author | Vadim Dashevskiy <watcherhd@gmail.com> | 2012-11-28 18:44:28 +0000 |
---|---|---|
committer | Vadim Dashevskiy <watcherhd@gmail.com> | 2012-11-28 18:44:28 +0000 |
commit | 68fb5b69ea8403a3f9dcb70b3133eb10e1711000 (patch) | |
tree | c4e4e81724c9ea80fe7f2558991bda699961bc11 /plugins/Ping/src/icmp.cpp | |
parent | 80d65efb84ec7500cd02ca047d0177d642eb1871 (diff) |
forgotten files
git-svn-id: http://svn.miranda-ng.org/main/trunk@2544 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c
Diffstat (limited to 'plugins/Ping/src/icmp.cpp')
-rw-r--r-- | plugins/Ping/src/icmp.cpp | 117 |
1 files changed, 117 insertions, 0 deletions
diff --git a/plugins/Ping/src/icmp.cpp b/plugins/Ping/src/icmp.cpp new file mode 100644 index 0000000000..af9a3605e4 --- /dev/null +++ b/plugins/Ping/src/icmp.cpp @@ -0,0 +1,117 @@ +#include "common.h"
+#include "icmp.h"
+
+char data[] = "AAAABBBBCCCCDDDDEEEEFFFFGGGGHHH";
+ICMP *ICMP::instance = 0;
+
+#define BUFFER_SIZE (8 * (sizeof(ICMP_ECHO_REPLY) + sizeof(data)))
+
+ICMP::ICMP():
+ timeout(2000),
+ functions_loaded(false)
+{
+ hDLL = LoadLibrary(_T("IPHLPAPI.DLL"));
+ if(hDLL) {
+ pIcmpCreateFile = (pfnHV)GetProcAddress(hDLL, "IcmpCreateFile");
+ pIcmpCloseHandle = (pfnBH)GetProcAddress(hDLL, "IcmpCloseHandle");
+ pIcmpSendEcho2 = (pfnDHDPWPipPDD)GetProcAddress(hDLL, "IcmpSendEcho2");
+ }
+ if (hDLL == 0 || pIcmpCreateFile == 0 || pIcmpCloseHandle == 0 || pIcmpSendEcho2 == 0) {
+ hDLL = LoadLibrary(_T("ICMP.DLL"));
+ if(hDLL) {
+ pIcmpCreateFile = (pfnHV)GetProcAddress(hDLL, "IcmpCreateFile");
+ pIcmpCloseHandle = (pfnBH)GetProcAddress(hDLL, "IcmpCloseHandle");
+ pIcmpSendEcho2 = (pfnDHDPWPipPDD)GetProcAddress(hDLL, "IcmpSendEcho2");
+ }
+ if (hDLL == 0 || pIcmpCreateFile == 0 || pIcmpCloseHandle == 0 || pIcmpSendEcho2 == 0)
+ return;
+ else
+ DBWriteContactSettingString(0, PLUG, "PingLib", "ICMP.DLL"); // for debugging
+ } else
+ DBWriteContactSettingString(0, PLUG, "PingLib", "IPHLPAPI.DLL"); // for debugging
+
+ WSAData wsaData;
+ if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0) {
+ WSACleanup();
+ FreeLibrary((HMODULE)hDLL);
+ return;
+ }
+
+ /*
+ hIP = pIcmpCreateFile();
+ if (hIP == INVALID_HANDLE_VALUE) {
+ pIcmpCloseHandle(hIP);
+ return;
+ }
+ */
+
+ buff = new char[BUFFER_SIZE];
+ functions_loaded = true;
+}
+
+void ICMP::stop() {
+ //if(hIP) {
+ // pIcmpCloseHandle(hIP);
+ // hIP = 0;
+ //}
+}
+
+ICMP::~ICMP() {
+ if(hIP) stop();
+ WSACleanup();
+ if(hDLL)
+ FreeLibrary(hDLL);
+ delete[] buff;
+}
+
+bool ICMP::ping(char *host, ICMP_ECHO_REPLY &reply) {
+ if(!functions_loaded) return false;
+
+ HOSTENT *rec;
+ IP_OPTION_INFORMATION ipoi;
+
+ unsigned long address = inet_addr(host);
+ if (address == INADDR_NONE) {
+ rec = gethostbyname(host);
+ if(rec) address = *(unsigned long *)(*rec->h_addr_list);
+ else return false;
+ }
+
+ ipoi.Ttl = 255;
+ ipoi.Tos = 0;
+ ipoi.Flags = 0;
+ ipoi.OptionsSize = 0;
+ ipoi.OptionsData = 0;
+
+ reply.Status = 0;
+
+ hIP = pIcmpCreateFile();
+ if (hIP == INVALID_HANDLE_VALUE) return false;
+
+ //pIcmpSendEcho2(hIP, 0, 0, 0, address, data, sizeof(data), &ipoi, buff, sizeof(ICMP_ECHO_REPLY) + sizeof(data), timeout);
+ if(pIcmpSendEcho2(hIP, 0, 0, 0, address, data, sizeof(data), 0, buff, BUFFER_SIZE, timeout) == 0) {
+ DWORD code = GetLastError();
+ if(code != 11010) {
+ char winmsg[512], msg[1024];
+ FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, 0, code, 0, winmsg, 512, 0);
+ mir_snprintf(msg, 1024, "Ping error (%d): %s", code, winmsg);
+ PUShowMessage(msg, SM_NOTIFY);
+ return false;
+ }
+ }
+ memcpy(&reply, buff, sizeof(ICMP_ECHO_REPLY));
+
+ pIcmpCloseHandle(hIP);
+
+ return (reply.Status == 0);
+}
+
+ICMP *ICMP::get_instance() {
+ if(!instance)
+ instance = new ICMP();
+ return instance;
+}
+
+void ICMP::cleanup() {
+ if(instance) delete instance;
+}
\ No newline at end of file |