]> git.ipfire.org Git - thirdparty/squid.git/blame - src/icmp/Icmp.cc
SourceFormat Enforcement
[thirdparty/squid.git] / src / icmp / Icmp.cc
CommitLineData
fab463b4 1/*
4ac4a490 2 * Copyright (C) 1996-2017 The Squid Software Foundation and contributors
e25c139f 3 *
bbc27441
AJ
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.
fab463b4 7 */
bbc27441
AJ
8
9/* DEBUG: section 37 ICMP Routines */
10
582c2af2 11#include "squid.h"
602d9612 12#include "Debug.h"
b826ffb5 13#include "Icmp.h"
f19d2c69 14#include "SquidTime.h"
6ffa5ad9 15
b826ffb5 16Icmp::Icmp()
cc192b50 17{
e97f40f4 18#if USE_ICMP
cc192b50 19 icmp_sock = -1;
20 icmp_ident = 0;
674ac814 21#endif
6ffa5ad9 22}
23
cc192b50 24void
b826ffb5 25Icmp::Close()
6ffa5ad9 26{
cc192b50 27#if USE_ICMP
26ac0430 28 if (icmp_sock > 0)
cc192b50 29 close(icmp_sock);
30 icmp_sock = -1;
31 icmp_ident = 0;
674ac814 32#endif
6ffa5ad9 33}
34
cc192b50 35#if USE_ICMP
62e76326 36
cc192b50 37int
b826ffb5 38Icmp::CheckSum(unsigned short *ptr, int size)
cc192b50 39{
40 long sum;
41 unsigned short oddbyte;
42 unsigned short answer;
62e76326 43
4d6662d9 44 if (!ptr) return (int)htons(0xffff); // bad input.
62e76326 45
cc192b50 46 sum = 0;
62e76326 47
cc192b50 48 while (size > 1) {
aec55359
FC
49 sum += *ptr;
50 ++ptr;
cc192b50 51 size -= 2;
16b204c4 52 }
16b204c4 53
cc192b50 54 if (size == 1) {
55 oddbyte = 0;
56 *((unsigned char *) &oddbyte) = *(unsigned char *) ptr;
57 sum += oddbyte;
58 }
bf8fe701 59
cc192b50 60 sum = (sum >> 16) + (sum & 0xffff);
61 sum += (sum >> 16);
62 answer = (unsigned short) ~sum;
63 return (answer);
e97f40f4 64}
8a6218c6 65
cc192b50 66int
b826ffb5 67Icmp::ipHops(int ttl)
16b204c4 68{
cc192b50 69 if (ttl < 33)
70 return 33 - ttl;
62e76326 71
cc192b50 72 if (ttl < 63)
73 return 63 - ttl; /* 62 = (64+60)/2 */
62e76326 74
cc192b50 75 if (ttl < 65)
76 return 65 - ttl; /* 62 = (64+60)/2 */
62e76326 77
cc192b50 78 if (ttl < 129)
79 return 129 - ttl;
16b204c4 80
cc192b50 81 if (ttl < 193)
82 return 193 - ttl;
62e76326 83
cc192b50 84 return 256 - ttl;
16b204c4 85}
86
e97f40f4 87void
09aabd84 88Icmp::Log(const Ip::Address &addr, const uint8_t type, const char* pkt_str, const int rtt, const int hops)
16b204c4 89{
cc192b50 90 debugs(42, 2, "pingerLog: " << std::setw(9) << current_time.tv_sec <<
91 "." << std::setfill('0') << std::setw(6) <<
92 current_time.tv_usec << " " << std::left << std::setfill(' ') <<
93 std::setw(45) << addr << " " << type <<
94 " " << std::setw(15) << pkt_str << " " << rtt <<
95 "ms " << hops << " hops");
16b204c4 96}
fab463b4 97
cc192b50 98#endif /* USE_ICMP */
f53969cc 99