summaryrefslogtreecommitdiff
path: root/plugins/Ping/src/rawping.cpp
blob: 007200fa237cfb2808e5795ff93c37563f38ed90 (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
#include "common.h"

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);
}

SOCKET sd = -1;
char packet[1024];
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) {
		WSACleanup();
		return 1;
	}

	if(sd != -1)
		closesocket(sd);

	// 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;
	}

	int ttl = 255;
	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();

	inited = true;
	if(raw_ping("127.0.0.1", 500) >= 0) {
		return 0;
	}

	inited = false;
	return 5;
}

extern int raw_ping(char *host, int timeout)
{
	if(!inited) return -1;

	// 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;
			dest.sin_family = AF_INET;
		}
		else {
			// Not a recognized hostname either!
			if(options.logging) CallService(PLUG "/Log", (WPARAM)_T("rawping error: unrecognised host"), 0);
			return -1;
		}
	}

	ICMPHeader *header = (ICMPHeader *)packet;
	header->seq = ++seq_no;
	header->checksum = 0;
	header->checksum = ip_checksum((USHORT*)header, sizeof(ICMPHeader));

	bool use_hi_res = false;
	LARGE_INTEGER hr_freq, hr_send_time;
	DWORD send_time;
	if(QueryPerformanceFrequency(&hr_freq)) {
		use_hi_res = true;
		QueryPerformanceCounter(&hr_send_time);
	} else 
		send_time = GetTickCount();

	// send packet
	int bwrote = sendto(sd, (char*)packet, sizeof(ICMPHeader), 0, (sockaddr*)&dest, sizeof(dest));
	if (bwrote == SOCKET_ERROR) {
		if(options.logging) CallService(PLUG "/Log", (WPARAM)_T("rawping error: unable to send"), 0);
		return -1;
	}

	// Wait for the ping reply
	sockaddr_in source;
	int fromlen = sizeof(source);
	IPHeader *reply_header = (IPHeader *)recv_buff;
	ICMPHeader *reply;
	DWORD start, current_time;
	LARGE_INTEGER hr_start, hr_current_time, hr_timeout;
	if(use_hi_res) {
		hr_timeout.QuadPart = (timeout * hr_freq.QuadPart / 1000);
		QueryPerformanceCounter(&hr_start);
		hr_current_time = hr_start;
	} else {
		start = GetTickCount();
		current_time = start;
	}

	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);

		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)_T("rawping error: socket error...cycling"), 0);
			}
			continue;
		}

		if(reply_header->proto != ICMP_PROTO)
			if(options.logging) CallService(PLUG "/Log", (WPARAM)_T("rawping error: packet not ICMP...cycling"), 0);
			continue;

		if(reply_header->tos != 0)
			if(options.logging) CallService(PLUG "/Log", (WPARAM)_T("rawping error: TOS not 0...cycling"), 0);
			continue;

		reply = (ICMPHeader *)(recv_buff + reply_header->h_len * 4);		
		if((unsigned)bread < reply_header->h_len * 4 + sizeof(ICMPHeader)) {
			if(options.logging) CallService(PLUG "/Log", (WPARAM)_T("rawping error: short header"), 0);
			continue;
		}

		if(reply->id != (USHORT)GetCurrentProcessId())
			if(options.logging) CallService(PLUG "/Log", (WPARAM)_T("rawping error: wrong ID...cycling"), 0);
			continue;

		if(reply->type != PT_ICMP_ECHO_REPLY && reply->type != PT_ICMP_SOURCE_QUENCH) {
			if(options.logging) CallService(PLUG "/Log", (WPARAM)_T("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) {
			if(options.logging) CallService(PLUG "/Log", (WPARAM)_T("rawping error: wrong sequence number...cycling"), 0);
			continue;
		}

		if(reply->type == PT_ICMP_SOURCE_QUENCH) {
			char buff[1024];
			mir_snprintf(buff, SIZEOF(buff), Translate("Host %s requests that you reduce the amount of traffic you are sending."), host);
			MessageBoxA(0, buff, Translate(PLUG " Warning"), MB_OK | MB_ICONWARNING);
		}

		if(use_hi_res) {
			LARGE_INTEGER ticks;
			ticks.QuadPart = hr_current_time.QuadPart - hr_send_time.QuadPart;
			return (int)(ticks.QuadPart * 1000 / hr_freq.QuadPart); 
		} else
			return current_time - send_time;
	}
	if(options.logging) CallService(PLUG "/Log", (WPARAM)_T("rawping error: timeout"), 0);

	return -1;
}

extern int cleanup_raw_ping()
{
	if(inited) {
		closesocket(sd);
		sd = -1;
		WSACleanup();
	}
	return 0;
}