]> git.ipfire.org Git - thirdparty/squid.git/blob - src/icmp/Icmp.cc
Source Format Enforcement (#1234)
[thirdparty/squid.git] / src / icmp / Icmp.cc
1 /*
2 * Copyright (C) 1996-2023 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 "debug/Stream.h"
13 #include "Icmp.h"
14 #include "time/gadgets.h"
15
16 Icmp::Icmp()
17 {
18 #if USE_ICMP
19 icmp_sock = -1;
20 icmp_ident = 0;
21 #endif
22 }
23
24 void
25 Icmp::Close()
26 {
27 #if USE_ICMP
28 if (icmp_sock > 0)
29 close(icmp_sock);
30 icmp_sock = -1;
31 icmp_ident = 0;
32 #endif
33 }
34
35 #if USE_ICMP
36
37 int
38 Icmp::CheckSum(unsigned short *ptr, int size)
39 {
40 long sum;
41 unsigned short oddbyte;
42 unsigned short answer;
43
44 if (!ptr) return (int)htons(0xffff); // bad input.
45
46 sum = 0;
47
48 while (size > 1) {
49 sum += *ptr;
50 ++ptr;
51 size -= 2;
52 }
53
54 if (size == 1) {
55 oddbyte = 0;
56 *((unsigned char *) &oddbyte) = *(unsigned char *) ptr;
57 sum += oddbyte;
58 }
59
60 sum = (sum >> 16) + (sum & 0xffff);
61 sum += (sum >> 16);
62 answer = (unsigned short) ~sum;
63 return (answer);
64 }
65
66 int
67 Icmp::ipHops(int ttl)
68 {
69 if (ttl < 33)
70 return 33 - ttl;
71
72 if (ttl < 63)
73 return 63 - ttl; /* 62 = (64+60)/2 */
74
75 if (ttl < 65)
76 return 65 - ttl; /* 62 = (64+60)/2 */
77
78 if (ttl < 129)
79 return 129 - ttl;
80
81 if (ttl < 193)
82 return 193 - ttl;
83
84 return 256 - ttl;
85 }
86
87 void
88 Icmp::Log(const Ip::Address &addr, const uint8_t type, const char* pkt_str, const int rtt, const int hops)
89 {
90 debugs(42, 2, "pingerLog: " << current_time << " " << std::left <<
91 std::setw(45) << addr << " " << type <<
92 " " << std::setw(15) << pkt_str << " " << rtt <<
93 "ms " << hops << " hops");
94 }
95
96 #endif /* USE_ICMP */
97