Squid Web Cache master
Loading...
Searching...
No Matches
Icmp.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2025 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9/* DEBUG: section 37 ICMP Routines */
10
11#include "squid.h"
12#include "compat/unistd.h"
13#include "debug/Stream.h"
14#include "Icmp.h"
15#include "time/gadgets.h"
16
18{
19#if USE_ICMP
20 icmp_sock = -1;
21 icmp_ident = 0;
22#endif
23}
24
25void
27{
28#if USE_ICMP
29 if (icmp_sock > 0)
31 icmp_sock = -1;
32 icmp_ident = 0;
33#endif
34}
35
36#if USE_ICMP
37
38int
39Icmp::CheckSum(unsigned short *ptr, int size)
40{
41 long sum;
42 unsigned short oddbyte;
43 unsigned short answer;
44
45 if (!ptr) return (int)htons(0xffff); // bad input.
46
47 sum = 0;
48
49 while (size > 1) {
50 sum += *ptr;
51 ++ptr;
52 size -= 2;
53 }
54
55 if (size == 1) {
56 oddbyte = 0;
57 *((unsigned char *) &oddbyte) = *(unsigned char *) ptr;
58 sum += oddbyte;
59 }
60
61 sum = (sum >> 16) + (sum & 0xffff);
62 sum += (sum >> 16);
63 answer = (unsigned short) ~sum;
64 return (answer);
65}
66
67int
69{
70 if (ttl < 33)
71 return 33 - ttl;
72
73 if (ttl < 63)
74 return 63 - ttl; /* 62 = (64+60)/2 */
75
76 if (ttl < 65)
77 return 65 - ttl; /* 62 = (64+60)/2 */
78
79 if (ttl < 129)
80 return 129 - ttl;
81
82 if (ttl < 193)
83 return 193 - ttl;
84
85 return 256 - ttl;
86}
87
88void
89Icmp::Log(const Ip::Address &addr, const uint8_t type, const char* pkt_str, const int rtt, const int hops)
90{
91 debugs(42, 2, "pingerLog: " << current_time << " " << std::left <<
92 std::setw(45) << addr << " " << type <<
93 " " << std::setw(15) << pkt_str << " " << rtt <<
94 "ms " << hops << " hops");
95}
96
97#endif /* USE_ICMP */
98
int size
Definition ModDevPoll.cc:70
virtual void Close()
Shutdown pinger helper and control channel.
Definition Icmp.cc:26
int CheckSum(unsigned short *ptr, int size)
Calculate a packet checksum.
Definition Icmp.cc:39
int icmp_ident
Definition Icmp.h:122
void Log(const Ip::Address &addr, const uint8_t type, const char *pkt_str, const int rtt, const int hops)
Log the packet.
Definition Icmp.cc:89
int icmp_sock
Definition Icmp.h:121
Icmp()
Definition Icmp.cc:17
int ipHops(int ttl)
Definition Icmp.cc:68
#define debugs(SECTION, LEVEL, CONTENT)
Definition Stream.h:192
struct timeval current_time
the current UNIX time in timeval {seconds, microseconds} format
Definition gadgets.cc:18
int xclose(int fd)
POSIX close(2) equivalent.
Definition unistd.h:43