Paul Stewart | 960d469 | 2013-12-09 07:41:59 -0800 | [diff] [blame] | 1 | // Copyright (c) 2013 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef SHILL_ICMP_H_ |
| 6 | #define SHILL_ICMP_H_ |
| 7 | |
Samuel Tan | f66080e | 2015-06-18 15:53:00 -0700 | [diff] [blame] | 8 | #include <netinet/ip_icmp.h> |
| 9 | |
Ben Chan | cd47732 | 2014-10-17 14:19:30 -0700 | [diff] [blame] | 10 | #include <memory> |
| 11 | |
| 12 | #include <base/macros.h> |
Paul Stewart | 960d469 | 2013-12-09 07:41:59 -0800 | [diff] [blame] | 13 | |
| 14 | namespace shill { |
| 15 | |
| 16 | class IPAddress; |
| 17 | class ScopedSocketCloser; |
| 18 | class Sockets; |
| 19 | |
| 20 | // The Icmp class encapsulates the task of sending ICMP frames. |
| 21 | class Icmp { |
| 22 | public: |
Samuel Tan | f66080e | 2015-06-18 15:53:00 -0700 | [diff] [blame] | 23 | static const int kIcmpEchoCode; |
| 24 | |
Alex Vakulenko | 016fa0e | 2014-08-11 15:59:58 -0700 | [diff] [blame] | 25 | Icmp(); |
Paul Stewart | 960d469 | 2013-12-09 07:41:59 -0800 | [diff] [blame] | 26 | virtual ~Icmp(); |
| 27 | |
Alex Vakulenko | 016fa0e | 2014-08-11 15:59:58 -0700 | [diff] [blame] | 28 | // Create a socket for transmission of ICMP frames. |
Paul Stewart | 960d469 | 2013-12-09 07:41:59 -0800 | [diff] [blame] | 29 | virtual bool Start(); |
| 30 | |
| 31 | // Destroy the transmit socket. |
| 32 | virtual void Stop(); |
| 33 | |
| 34 | // Returns whether an ICMP socket is open. |
| 35 | virtual bool IsStarted() const; |
| 36 | |
Samuel Tan | f66080e | 2015-06-18 15:53:00 -0700 | [diff] [blame] | 37 | // Send an ICMP Echo Request (Ping) packet to |destination|. The ID and |
| 38 | // sequence number fields of the echo request will be set to |id| and |
| 39 | // |seq_num| respectively. |
| 40 | virtual bool TransmitEchoRequest(const IPAddress& destination, uint16_t id, |
| 41 | uint16_t seq_num); |
| 42 | |
| 43 | int socket() { return socket_; } |
Paul Stewart | 960d469 | 2013-12-09 07:41:59 -0800 | [diff] [blame] | 44 | |
| 45 | private: |
| 46 | friend class IcmpTest; |
| 47 | |
Samuel Tan | f66080e | 2015-06-18 15:53:00 -0700 | [diff] [blame] | 48 | // Compute the checksum for Echo Request |hdr| of length |len| according to |
| 49 | // specifications in RFC 792. |
| 50 | static uint16_t ComputeIcmpChecksum(const struct icmphdr& hdr, size_t len); |
| 51 | |
Ben Chan | cd47732 | 2014-10-17 14:19:30 -0700 | [diff] [blame] | 52 | std::unique_ptr<Sockets> sockets_; |
| 53 | std::unique_ptr<ScopedSocketCloser> socket_closer_; |
Paul Stewart | 960d469 | 2013-12-09 07:41:59 -0800 | [diff] [blame] | 54 | int socket_; |
| 55 | |
| 56 | DISALLOW_COPY_AND_ASSIGN(Icmp); |
| 57 | }; |
| 58 | |
| 59 | } // namespace shill |
| 60 | |
| 61 | #endif // SHILL_ICMP_H_ |