blob: 6c34e3bbc65aecd58cecf310f0fce599949b971f [file] [log] [blame]
Paul Stewart960d4692013-12-09 07:41:59 -08001// 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 Tanf66080e2015-06-18 15:53:00 -07008#include <netinet/ip_icmp.h>
9
Ben Chancd477322014-10-17 14:19:30 -070010#include <memory>
11
12#include <base/macros.h>
Paul Stewart960d4692013-12-09 07:41:59 -080013
14namespace shill {
15
16class IPAddress;
17class ScopedSocketCloser;
18class Sockets;
19
20// The Icmp class encapsulates the task of sending ICMP frames.
21class Icmp {
22 public:
Samuel Tanf66080e2015-06-18 15:53:00 -070023 static const int kIcmpEchoCode;
24
Alex Vakulenko016fa0e2014-08-11 15:59:58 -070025 Icmp();
Paul Stewart960d4692013-12-09 07:41:59 -080026 virtual ~Icmp();
27
Alex Vakulenko016fa0e2014-08-11 15:59:58 -070028 // Create a socket for transmission of ICMP frames.
Paul Stewart960d4692013-12-09 07:41:59 -080029 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 Tanf66080e2015-06-18 15:53:00 -070037 // 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 Stewart960d4692013-12-09 07:41:59 -080044
45 private:
46 friend class IcmpTest;
47
Samuel Tanf66080e2015-06-18 15:53:00 -070048 // 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 Chancd477322014-10-17 14:19:30 -070052 std::unique_ptr<Sockets> sockets_;
53 std::unique_ptr<ScopedSocketCloser> socket_closer_;
Paul Stewart960d4692013-12-09 07:41:59 -080054 int socket_;
55
56 DISALLOW_COPY_AND_ASSIGN(Icmp);
57};
58
59} // namespace shill
60
61#endif // SHILL_ICMP_H_