blob: 6e315ccf4ec0970876e25d8ad16de0d627e6f4cd [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 Tan00b8f342015-09-17 17:48:49 -070020#if defined(__ANDROID__)
21#include <linux/icmp.h>
22#else
Samuel Tanf66080e2015-06-18 15:53:00 -070023#include <netinet/ip_icmp.h>
Samuel Tan00b8f342015-09-17 17:48:49 -070024#endif // __ANDROID__
Samuel Tanf66080e2015-06-18 15:53:00 -070025
Ben Chancd477322014-10-17 14:19:30 -070026#include <memory>
27
28#include <base/macros.h>
Paul Stewart960d4692013-12-09 07:41:59 -080029
30namespace shill {
31
32class IPAddress;
33class ScopedSocketCloser;
34class Sockets;
35
36// The Icmp class encapsulates the task of sending ICMP frames.
37class Icmp {
38 public:
Samuel Tanf66080e2015-06-18 15:53:00 -070039 static const int kIcmpEchoCode;
40
Alex Vakulenko016fa0e2014-08-11 15:59:58 -070041 Icmp();
Paul Stewart960d4692013-12-09 07:41:59 -080042 virtual ~Icmp();
43
Alex Vakulenko016fa0e2014-08-11 15:59:58 -070044 // Create a socket for transmission of ICMP frames.
Paul Stewart960d4692013-12-09 07:41:59 -080045 virtual bool Start();
46
47 // Destroy the transmit socket.
48 virtual void Stop();
49
50 // Returns whether an ICMP socket is open.
51 virtual bool IsStarted() const;
52
Samuel Tanf66080e2015-06-18 15:53:00 -070053 // Send an ICMP Echo Request (Ping) packet to |destination|. The ID and
54 // sequence number fields of the echo request will be set to |id| and
55 // |seq_num| respectively.
56 virtual bool TransmitEchoRequest(const IPAddress& destination, uint16_t id,
57 uint16_t seq_num);
58
59 int socket() { return socket_; }
Paul Stewart960d4692013-12-09 07:41:59 -080060
61 private:
62 friend class IcmpTest;
63
Samuel Tanf66080e2015-06-18 15:53:00 -070064 // Compute the checksum for Echo Request |hdr| of length |len| according to
65 // specifications in RFC 792.
66 static uint16_t ComputeIcmpChecksum(const struct icmphdr& hdr, size_t len);
67
Ben Chancd477322014-10-17 14:19:30 -070068 std::unique_ptr<Sockets> sockets_;
69 std::unique_ptr<ScopedSocketCloser> socket_closer_;
Paul Stewart960d4692013-12-09 07:41:59 -080070 int socket_;
71
72 DISALLOW_COPY_AND_ASSIGN(Icmp);
73};
74
75} // namespace shill
76
77#endif // SHILL_ICMP_H_