-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwinping.h
152 lines (127 loc) · 4.95 KB
/
winping.h
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
/*
* Author : Mark Zammit
* Contact : markzammit@me.com
*/
/** Winsock 2.1+ C++ Ping Wrapper
*
* This is a wrapper for rawping.cpp
* Credit to http://tangentsoft.net/wskfaq/examples/rawping.html
* for the source code for this ping method. The method has been
* slightly modified to allow for timeout specifications as well
* as a ping information struct to be filled out along the way.
*/
#ifndef _WINPING_H_
#define _WINPING_H_
/* Checks that Microsoft Visual C++ compiler is in use
* otherwise this will not compile */
#if !defined(_MSC_VER)
#error winfs.h only supported by MSVC Compiler 2005+
#endif
#if defined(_MSC_VER)
#include <rawping.h>
#include <vector>
#ifndef TSTR
/* UNICODE SUPPORT */
#if defined(UNICODE) || defined(_UNICODE_)
#define TSTR std::wstring
#else /* ASCII SUPPORT */
#define TSTR std::string
#endif
#endif
#ifndef _STRCONV
#define _STRCONV
/* std::string <-> std::wstring conversions */
template <class _R, class _T> _R strconv(_T s) { return _R(s.begin(), s.end()); }
#endif
#ifndef _TSPRINTF_S
#define _TSPRINTF_S
/* UNICODE SUPPORT */
#if defined(UNICODE) || defined(_UNICODE_)
#define TSPRINTF_S(x,y,z,...) swprintf_s(x,y,z,__VA_ARGS__)
#else /* ASCII SUPPORT */
#define TSPRINTF_S(x,y,z,...) sprintf_s(x,y,z,__VA_ARGS__)
#endif
#endif
#define IS_PING_ERR(x) ((x & 0xefff0000) >= 0xe0000000)
#define GET_ERR_VALUE(x) (x & 0xffff)
#define GET_ERR_VALUE_HIGH(x) (x & 0xff00)
#define GET_ERR_VALUE_LOW(x) (x & 0x00ff)
#define DEFAULT_ATTEMPTS 1
#define DEFUALT_TIMEOUT_MS 4000
#define REQUEST_TIMEOUT -1
#define PING_INFINITE 0xffffffff
#define VERBOSE_LOGGING true
void printpr(pingreq&);
/* Ping Statistic structure
* Keeps a record of every ping request sent by winping()
*/
typedef struct _ping_stat_
{
std::vector<pingreq*> pings;
~_ping_stat_()
{
/* Clears memory allocation of ping requests */
for(int i = 0; i < pings.size(); ++i)
delete pings[i];
}
} pingstat;
class winping
{
private:
bool verbose_logging; // For verbose live ping requests instead
// of waiting for ping to finish all attempts
DWORD err; // Keeps the last error result
public:
/* Initialize winping() with verbose logging, default to no logging */
winping(bool = false);
~winping(void);
/** NOT IMPLEMENTED YET
*/
int tracert(TSTR host,
pingstat *,
int = DEFAULT_PACKET_SIZE,
int = DEFAULT_TTL);
/** Pings a host address either by IPv4 address or by its
* DNS resolvable hostname.
* NB: hostname might not necessarily resolve if not fully qualified
* @host : IPv4 address or fully qualified hostname.
* @pingstats : Dynamically allocated pingreq structs with the ping result
* of each ping sequentially. Disabled under PING_INFINITE option.
* @packetsize : (Optional) Packet size to ping not exceeding MAX_PING_PACKET_SIZE.
* @ttl : (Optional) TTL (Time to Live) value not exceeding MAX_TTL.
* @attempts : (Optional) Number of ping attempts to make total, if
* PING_INFINITE is supplied it will disable memory allocation
* of @pingstats.
* NB: Requires forced program exit to cease activity (e.g. CTRL^C)
* @timeout : (Optional) Timeout in milliseconds to wait for host response per ping
*
* Returns : WSASUCCESS on normal operation, otherwise will return an error code.
* NB: Use print_error() to display the formatted error result and value.
*/
int ping(TSTR host,
pingstat *,
int = DEFAULT_PACKET_SIZE,
int = DEFAULT_TTL,
int = DEFAULT_ATTEMPTS,
int = DEFUALT_TIMEOUT_MS);
/** Returns the last error code generated by winping()
*
* NB: See possible error code table above for 99% of return codes
*/
int error(void);
/** Formats the last return code generated by winping()
* and prints the formatted message to stdout.
* This implements windows FormatMessage() function as well
* as custom error codes in the same styling FormatMessage().
*
* NB: Some custom error codes are masks which store error values
* using error() alone will not always be helpful in deciphering
* the error information.
*/
void print_error(void);
private:
/** Sets the last return code before returning */
int returnc(int);
};
#endif /* Microsoft Compiler check */
#endif /* _WINPING_H_ */