Icmp.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2019 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.h"
13 #include "Icmp.h"
14 #include "SquidTime.h"
15 
17 {
18 #if USE_ICMP
19  icmp_sock = -1;
20  icmp_ident = 0;
21 #endif
22 }
23 
24 void
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: " << 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");
96 }
97 
98 #endif /* USE_ICMP */
99 
int type
Definition: errorpage.cc:152
int ipHops(int ttl)
Definition: Icmp.cc:67
struct timeval current_time
Definition: stub_time.cc:15
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:88
int icmp_sock
Definition: Icmp.h:121
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Debug.h:124
int CheckSum(unsigned short *ptr, int size)
Calculate a packet checksum.
Definition: Icmp.cc:38
void * addr
Definition: membanger.c:46
int icmp_ident
Definition: Icmp.h:122
Icmp()
Definition: Icmp.cc:16
int size
Definition: ModDevPoll.cc:77
virtual void Close()
Shutdown pinger helper and control channel.
Definition: Icmp.cc:25

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors