-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathIcmpSocket.h
More file actions
310 lines (259 loc) · 6.16 KB
/
IcmpSocket.h
File metadata and controls
310 lines (259 loc) · 6.16 KB
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
/*
* This file is part of JICMP.
*
* JICMP is Copyright (C) 2002-2022 The OpenNMS Group, Inc.
* OpenNMS(R) is Copyright (C) 1999-2022 The OpenNMS Group, Inc.
*
* OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.
*
* JICMP is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License, as
* published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* JICMP is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with JICMP. If not, see:
* http://www.gnu.org/licenses/
*
* For more information contact:
* OpenNMS(R) Licensing <license@opennms.com>
* http://www.opennms.com/
*/
#ifndef _ICMPSOCKET_H
#define _ICMPSOCKET_H
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif
#ifdef HAVE_STDIO_H
#include <stdio.h>
#endif
#ifdef HAVE_STDINT_H
#include <stdint.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_MALLOC_H
#include <malloc.h>
#endif
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
#ifdef HAVE_NETINET_IN_SYSTM_H
# include <netinet/in_systm.h>
#endif
#ifdef HAVE_AVAILABILITYMACROS_H
#include <AvailabilityMacros.h>
#endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#ifdef HAVE_NETINET_IP_H
#include <netinet/ip.h>
#endif
#ifdef HAVE_NETINET_IP_ICMP_H
#include <netinet/ip_icmp.h>
#endif
#ifdef HAVE_NETDB_H
#include <netdb.h>
#endif
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#ifdef HAVE_SYS_BYTEORDER_H
#include <sys/byteorder.h>
#endif
#ifdef HAVE_BYTESWAP_H
#include <byteswap.h>
#else
#include "byteswap.h"
#endif
#ifdef HAVE_WINDOWS_H
#include <Windows.h>
#endif
#ifdef HAVE_WINSOCK2_H
# ifdef HAVE_WS2DEF_H
# include <ws2def.h>
# endif
# include <winsock2.h>
# ifdef HAVE_WS2TCPIP_H
# include <ws2tcpip.h>
# endif
# include "icmp.h"
# ifndef HAVE_STDINT_H
typedef u_int in_addr_t;
typedef u_int64 uint64_t;
# endif
# ifdef __MINGW32__
# ifdef HAVE_STDINT_H
typedef u_int in_addr_t;
# endif
# else
/* Visual Studio */
# define close closesocket
# define snprintf _snprintf
# pragma warning(disable: 4996)
# endif
#endif
/**
* Macros for doing byte swapping
*/
#ifdef HAVE_LIBKERN_OSBYTEORDER_H
# include <libkern/OSByteOrder.h>
#endif
#if HAVE_ARCHITECTURE_BYTE_ORDER_H
# include <architecture/byte_order.h>
#endif
#if !defined(ntohll)
# ifdef HAVE_LIBKERN_OSBYTEORDER_H
# define ntohll(_x_) OSSwapBigToHostInt64(_x_)
# elif HAVE_ARCHITECTURE_BYTE_ORDER_H
# define ntohll(_x_) NXSwapBigLongLongToHost(_x_)
# elif defined(WORDS_BIGENDIAN)
# define ntohll(_x_) (_x_)
# elif defined(BSWAP_64)
# define ntohll(_x_) BSWAP_64(_x_)
# elif defined(__bswap_64)
# define ntohll(_x_) __bswap_64(_x_)
# else
# define ntohll(_x_) bswap_64(_x_)
# endif
#endif // !defined(ntohll)
#if !defined(htonll)
# ifdef HAVE_LIBKERN_OSBYTEORDER_H
# define htonll(_x_) OSSwapHostToBigInt64(_x_)
# elif HAVE_ARCHITECTURE_BYTE_ORDER_H
# define htonll(_x_) NXSwapHostLongLongToBig(_x_)
# elif defined(WORDS_BIGENDIAN)
# define htonll(_x_) (_x_)
# elif defined(BSWAP_64)
# define htonll(_x_) BSWAP_64(_x_)
# elif defined(__bswap_64)
# define htonll(_x_) __bswap_64(_x_)
# else
# define htonll(_x_) bswap_64(_x_)
# endif
#endif // !defined(htonll)
#if defined(HAVE_STRUCT_IP)
typedef struct ip iphdr_t;
#elif defined(HAVE_STRUCT_IPHDR)
typedef struct iphdr iphdr_t;
#else
# error "not sure how to get an IP header struct on this platform!"
#endif
#if defined(HAVE_STRUCT_ICMP)
typedef struct icmp icmphdr_t;
#elif defined(HAVE_STRUCT_ICMPHDR)
typedef struct icmphdr icmphdr_t;
#else
# error "not sure how to get an ICMP header struct on this platform!"
#endif
/**
* This macro is used to recover the current time
* in milliseconds.
*/
#ifndef CURRENTTIMEMILLIS
#define CURRENTTIMEMILLIS(_dst_) \
{ \
struct timeval tv; \
gettimeofday(&tv,NULL); \
_dst_ = (uint64_t)tv.tv_sec * 1000UL + (uint64_t)tv.tv_usec / 1000UL; \
}
#endif
/**
* This macro is used to recover the current time
* in microseconds
*/
#ifndef CURRENTTIMEMICROS
#define CURRENTTIMEMICROS(_dst_) \
{ \
struct timeval tv; \
gettimeofday(&tv,NULL); \
_dst_ = (uint64_t)tv.tv_sec * 1000000UL + (uint64_t)tv.tv_usec; \
}
#endif
/**
* converts microseconds to milliseconds
*/
#ifndef MICROS_TO_MILLIS
# define MICROS_TO_MILLIS(_val_) ((_val_) / 1000UL)
#endif
/**
* convert milliseconds to microseconds.
*/
#ifndef MILLIS_TO_MICROS
# define MILLIS_TO_MICROS(_val_) ((_val_) * 1000UL)
#endif
/**
* This constant specifies the length of a
* time field in the buffer
*/
#ifndef TIME_LENGTH
# define TIME_LENGTH sizeof(uint64_t)
#endif
/**
* Specifies the header offset and length
*/
#ifndef ICMP_HEADER_OFFSET
# define ICMP_HEADER_OFFSET 0
# define ICMP_HEADER_LENGTH 8
#endif
/**
* specifies the offset of the sent time.
*/
#ifndef SENTTIME_OFFSET
# define SENTTIME_OFFSET (ICMP_HEADER_OFFSET + ICMP_HEADER_LENGTH)
#endif
/**
* Specifies the offset of the received time.
*/
#ifndef RECVTIME_OFFSET
# define RECVTIME_OFFSET (SENTTIME_OFFSET + TIME_LENGTH)
#endif
/**
* Specifies the offset of the thread identifer
*/
#ifndef THREADID_OFFSET
# define THREADID_OFFSET (RECVTIME_OFFSET + TIME_LENGTH)
#endif
/**
* Specifies the offset of the round trip time
*/
#ifndef RTT_OFFSET
# define RTT_OFFSET (THREADID_OFFSET + TIME_LENGTH)
#endif
/**
* specifies the magic tag and the offset/length of
* the tag in the header.
*/
#ifndef OPENNMS_TAG
# define OPENNMS_TAG "OpenNMS!"
# define OPENNMS_TAG_LEN 8
# define OPENNMS_TAG_OFFSET (RTT_OFFSET + TIME_LENGTH)
#endif
/**
* Winsock uses SOCKET, which is a special kind of Windows
* HANDLE object, not just an int
**/
#ifdef __WIN32__
#define onms_socket SOCKET
#else
#define onms_socket int
#define INVALID_SOCKET -1
#define SOCKET_ERROR -1
#endif
#endif // _ICMPSOCKET_H