blob: c6872970fac07c592d7e73be0eecd62f2a2c5b93 [file] [log] [blame]
Peter Qiuc0beca52015-09-03 11:25:46 -07001//
2// Copyright (C) 2013 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
Paul Stewart960d4692013-12-09 07:41:59 -080016
17#ifndef SHILL_ICMP_H_
18#define SHILL_ICMP_H_
19
Samuel Tanf66080e2015-06-18 15:53:00 -070020#include <netinet/ip_icmp.h>
21
Ben Chancd477322014-10-17 14:19:30 -070022#include <memory>
23
24#include <base/macros.h>
Paul Stewart960d4692013-12-09 07:41:59 -080025
26namespace shill {
27
28class IPAddress;
29class ScopedSocketCloser;
30class Sockets;
31
32// The Icmp class encapsulates the task of sending ICMP frames.
33class Icmp {
34 public:
Samuel Tanf66080e2015-06-18 15:53:00 -070035 static const int kIcmpEchoCode;
36
Alex Vakulenko016fa0e2014-08-11 15:59:58 -070037 Icmp();
Paul Stewart960d4692013-12-09 07:41:59 -080038 virtual ~Icmp();
39
Alex Vakulenko016fa0e2014-08-11 15:59:58 -070040 // Create a socket for transmission of ICMP frames.
Paul Stewart960d4692013-12-09 07:41:59 -080041 virtual bool Start();
42
43 // Destroy the transmit socket.
44 virtual void Stop();
45
46 // Returns whether an ICMP socket is open.
47 virtual bool IsStarted() const;
48
Samuel Tanf66080e2015-06-18 15:53:00 -070049 // Send an ICMP Echo Request (Ping) packet to |destination|. The ID and
50 // sequence number fields of the echo request will be set to |id| and
51 // |seq_num| respectively.
52 virtual bool TransmitEchoRequest(const IPAddress& destination, uint16_t id,
53 uint16_t seq_num);
54
55 int socket() { return socket_; }
Paul Stewart960d4692013-12-09 07:41:59 -080056
57 private:
58 friend class IcmpTest;
59
Samuel Tanf66080e2015-06-18 15:53:00 -070060 // Compute the checksum for Echo Request |hdr| of length |len| according to
61 // specifications in RFC 792.
62 static uint16_t ComputeIcmpChecksum(const struct icmphdr& hdr, size_t len);
63
Ben Chancd477322014-10-17 14:19:30 -070064 std::unique_ptr<Sockets> sockets_;
65 std::unique_ptr<ScopedSocketCloser> socket_closer_;
Paul Stewart960d4692013-12-09 07:41:59 -080066 int socket_;
67
68 DISALLOW_COPY_AND_ASSIGN(Icmp);
69};
70
71} // namespace shill
72
73#endif // SHILL_ICMP_H_