diff options
Diffstat (limited to 'plugins/Ping/src/rawping.cpp')
-rw-r--r-- | plugins/Ping/src/rawping.cpp | 148 |
1 files changed, 72 insertions, 76 deletions
diff --git a/plugins/Ping/src/rawping.cpp b/plugins/Ping/src/rawping.cpp index c5289dd16f..6b0f152d94 100644 --- a/plugins/Ping/src/rawping.cpp +++ b/plugins/Ping/src/rawping.cpp @@ -1,30 +1,24 @@ #include "common.h"
-#include "rawping.h"
-//#define _WSPIAPI_COUNTOF
-#ifndef _MSC_VER
-#include <ws2tcpip.h>
-#endif
-
-USHORT ip_checksum(USHORT* buffer, int size)
+USHORT ip_checksum(USHORT* buffer, int size)
{
- unsigned long cksum = 0;
-
- // Sum all the words together, adding the final byte if size is odd
- while (size > 1) {
- cksum += *buffer++;
- size -= sizeof(USHORT);
- }
- if (size) {
- cksum += *(UCHAR*)buffer;
- }
-
- // Do a little shuffling
- cksum = (cksum >> 16) + (cksum & 0xffff);
- cksum += (cksum >> 16);
-
- // Return the bitwise complement of the resulting mishmash
- return (USHORT)(~cksum);
+ unsigned long cksum = 0;
+
+ // Sum all the words together, adding the final byte if size is odd
+ while (size > 1) {
+ cksum += *buffer++;
+ size -= sizeof(USHORT);
+ }
+ if (size) {
+ cksum += *(UCHAR*)buffer;
+ }
+
+ // Do a little shuffling
+ cksum = (cksum >> 16) + (cksum & 0xffff);
+ cksum += (cksum >> 16);
+
+ // Return the bitwise complement of the resulting mishmash
+ return (USHORT)(~cksum);
}
SOCKET sd = -1;
@@ -33,38 +27,38 @@ char recv_buff[1024]; USHORT seq_no = 0;
bool inited = false;
-extern int init_raw_ping() {
- WSAData wsaData;
- if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0) {
+extern int init_raw_ping()
+{
+ WSAData wsaData;
+ if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0) {
WSACleanup();
return 1;
- }
+ }
if(sd != -1)
closesocket(sd);
- // Create the socket
- //sd = WSASocket(AF_INET, SOCK_RAW, IPPROTO_ICMP, 0, 0, 0);
+ // Create the socket
+ //sd = WSASocket(AF_INET, SOCK_RAW, IPPROTO_ICMP, 0, 0, 0);
sd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
- if (sd == INVALID_SOCKET) {
- return 2;
- }
+ if (sd == INVALID_SOCKET) {
+ return 2;
+ }
int ttl = 255;
- if (setsockopt(sd, IPPROTO_IP, IP_TTL, (const char*)&ttl, sizeof(ttl)) == SOCKET_ERROR) {
- return 3;
- }
+ if (setsockopt(sd, IPPROTO_IP, IP_TTL, (const char*)&ttl, sizeof(ttl)) == SOCKET_ERROR) {
+ return 3;
+ }
int our_recv_timeout = 100; // so we ca do multiple recv calls
if(setsockopt(sd, SOL_SOCKET, SO_RCVTIMEO, (const char *)&our_recv_timeout, sizeof(int)) == SOCKET_ERROR) {
return 4;
}
-
ICMPHeader *header = (ICMPHeader *)packet;
- header->type = PT_ICMP_ECHO_REQUEST;
- header->code = 0;
- header->id = (USHORT)GetCurrentProcessId();
+ header->type = PT_ICMP_ECHO_REQUEST;
+ header->code = 0;
+ header->id = (USHORT)GetCurrentProcessId();
inited = true;
if(raw_ping("127.0.0.1", 500) >= 0) {
@@ -75,40 +69,41 @@ extern int init_raw_ping() { return 5;
}
-extern int raw_ping(char *host, int timeout) {
+extern int raw_ping(char *host, int timeout)
+{
if(!inited) return -1;
- // Initialize the destination host info block
+ // Initialize the destination host info block
sockaddr_in dest;
- memset(&dest, 0, sizeof(dest));
-
- // Turn first passed parameter into an IP address to ping
- unsigned int addr = inet_addr(host);
- if (addr != INADDR_NONE) {
- // It was a dotted quad number, so save result
- dest.sin_addr.s_addr = addr;
- dest.sin_family = AF_INET;
- }
- else {
- // Not in dotted quad form, so try and look it up
- hostent* hp = gethostbyname(host);
- if (hp != 0) {
- // Found an address for that host, so save it
- memcpy(&(dest.sin_addr), hp->h_addr, hp->h_length);
- //dest.sin_family = hp->h_addrtype;
+ memset(&dest, 0, sizeof(dest));
+
+ // Turn first passed parameter into an IP address to ping
+ unsigned int addr = inet_addr(host);
+ if (addr != INADDR_NONE) {
+ // It was a dotted quad number, so save result
+ dest.sin_addr.s_addr = addr;
+ dest.sin_family = AF_INET;
+ }
+ else {
+ // Not in dotted quad form, so try and look it up
+ hostent* hp = gethostbyname(host);
+ if (hp != 0) {
+ // Found an address for that host, so save it
+ memcpy(&(dest.sin_addr), hp->h_addr, hp->h_length);
+ //dest.sin_family = hp->h_addrtype;
dest.sin_family = AF_INET;
- }
- else {
- // Not a recognized hostname either!
+ }
+ else {
+ // Not a recognized hostname either!
if(options.logging) CallService(PLUG "/Log", (WPARAM)"rawping error: unrecognised host", 0);
- return -1;
- }
- }
+ return -1;
+ }
+ }
ICMPHeader *header = (ICMPHeader *)packet;
- header->seq = ++seq_no;
+ header->seq = ++seq_no;
header->checksum = 0;
- header->checksum = ip_checksum((USHORT*)header, sizeof(ICMPHeader));
+ header->checksum = ip_checksum((USHORT*)header, sizeof(ICMPHeader));
bool use_hi_res = false;
LARGE_INTEGER hr_freq, hr_send_time;
@@ -120,15 +115,15 @@ extern int raw_ping(char *host, int timeout) { send_time = GetTickCount();
// send packet
- int bwrote = sendto(sd, (char*)packet, sizeof(ICMPHeader), 0, (sockaddr*)&dest, sizeof(dest));
- if (bwrote == SOCKET_ERROR) {
+ int bwrote = sendto(sd, (char*)packet, sizeof(ICMPHeader), 0, (sockaddr*)&dest, sizeof(dest));
+ if (bwrote == SOCKET_ERROR) {
if(options.logging) CallService(PLUG "/Log", (WPARAM)"rawping error: unable to send", 0);
- return -1;
- }
+ return -1;
+ }
- // Wait for the ping reply
+ // Wait for the ping reply
sockaddr_in source;
- int fromlen = sizeof(source);
+ int fromlen = sizeof(source);
IPHeader *reply_header = (IPHeader *)recv_buff;
ICMPHeader *reply;
DWORD start, current_time;
@@ -145,13 +140,13 @@ extern int raw_ping(char *host, int timeout) { while(((use_hi_res && (hr_current_time.QuadPart < hr_start.QuadPart + hr_timeout.QuadPart))
|| (!use_hi_res && current_time < start + timeout)))
{
- int bread = recvfrom(sd, recv_buff, 1024, 0, (sockaddr*)&source, &fromlen);
+ int bread = recvfrom(sd, recv_buff, 1024, 0, (sockaddr*)&source, &fromlen);
if(use_hi_res)
QueryPerformanceCounter(&hr_current_time);
else
current_time = GetTickCount();
-
+
if (bread == SOCKET_ERROR) {
if(WSAGetLastError() != WSAETIMEDOUT) {
if(options.logging) CallService(PLUG "/Log", (WPARAM)"rawping error: socket error...cycling", 0);
@@ -181,7 +176,7 @@ extern int raw_ping(char *host, int timeout) { if(options.logging) CallService(PLUG "/Log", (WPARAM)"rawping error: wrong type...cycling", 0);
continue;
}
-
+
//if(reply->seq < seq_no) continue;
//if(reply->seq > seq_no) return -1;
if(reply->seq != seq_no) {
@@ -207,7 +202,8 @@ extern int raw_ping(char *host, int timeout) { return -1;
}
-extern int cleanup_raw_ping() {
+extern int cleanup_raw_ping()
+{
if(inited) {
closesocket(sd);
sd = -1;
|