libtins  4.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
packet_sender.h
1 /*
2  * Copyright (c) 2017, Matias Fontanini
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * * Redistributions in binary form must reproduce the above
12  * copyright notice, this list of conditions and the following disclaimer
13  * in the documentation and/or other materials provided with the
14  * distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  */
29 
30 #ifndef TINS_PACKET_SENDER_H
31 #define TINS_PACKET_SENDER_H
32 
33 
34 #include <string>
35 #include <vector>
36 #include <stdint.h>
37 #include <map>
38 #include <tins/config.h>
39 #ifdef TINS_HAVE_PACKET_SENDER_PCAP_SENDPACKET
40  #include <pcap.h>
41 #endif // TINS_HAVE_PACKET_SENDER_PCAP_SENDPACKET
42 #include <tins/network_interface.h>
43 #include <tins/macros.h>
44 #include <tins/cxxstd.h>
45 
46 struct timeval;
47 struct sockaddr;
48 
49 namespace Tins {
50 
51 class PDU;
52 
116 class TINS_API PacketSender {
117 public:
121  static const uint32_t DEFAULT_TIMEOUT;
122 
126  enum SocketType {
127  ETHER_SOCKET,
128  IP_TCP_SOCKET,
129  IP_UDP_SOCKET,
130  IP_RAW_SOCKET,
131  ARP_SOCKET,
132  ICMP_SOCKET,
133  IPV6_SOCKET,
134  SOCKETS_END
135  };
136 
144  uint32_t recv_timeout = DEFAULT_TIMEOUT,
145  uint32_t usec = 0);
146 
147  #if TINS_IS_CXX11
148 
152  PacketSender(PacketSender &&rhs) TINS_NOEXCEPT {
153  *this = std::move(rhs);
154  }
155 
160  PacketSender& operator=(PacketSender &&rhs) TINS_NOEXCEPT {
161  sockets_ = std::move(rhs.sockets_);
162  rhs.sockets_ = std::vector<int>(SOCKETS_END, INVALID_RAW_SOCKET);
163  #ifndef _WIN32
164  #if defined(BSD) || defined(__FreeBSD_kernel__)
165  ether_socket_ = std::move(rhs.ether_socket_);
166  #else
167  ether_socket_ = rhs.ether_socket_;
168  rhs.ether_socket_ = INVALID_RAW_SOCKET;
169  #endif
170  #endif
171  types_ = rhs.types_; // no move
172  _timeout = rhs._timeout;
173  timeout_usec_ = rhs.timeout_usec_;
174  default_iface_ = rhs.default_iface_;
175  return* this;
176  }
177  #endif
178 
184  ~PacketSender();
185 
186  #if !defined(_WIN32) || defined(TINS_HAVE_PACKET_SENDER_PCAP_SENDPACKET)
187 
192  void open_l2_socket(const NetworkInterface& iface = NetworkInterface());
193  #endif // !_WIN32 || defined(TINS_HAVE_PACKET_SENDER_PCAP_SENDPACKET)
194 
206  void open_l3_socket(SocketType type);
207 
219  void close_socket(SocketType type, const NetworkInterface& iface = NetworkInterface());
220 
227  void default_interface(const NetworkInterface& iface);
228 
234  const NetworkInterface& default_interface() const;
235 
251  void send(PDU& pdu);
252 
268  void send(PDU& pdu, const NetworkInterface& iface);
269 
291  PDU* send_recv(PDU& pdu);
292 
304  PDU* send_recv(PDU& pdu, const NetworkInterface& iface);
305 
306  #ifndef _WIN32
307 
321  PDU* recv_l2(PDU& pdu, struct sockaddr* link_addr, uint32_t len_addr,
322  const NetworkInterface& iface = NetworkInterface());
323 
324  #endif // _WIN32
325 
326  #if !defined(_WIN32) || defined(TINS_HAVE_PACKET_SENDER_PCAP_SENDPACKET)
327 
342  void send_l2(PDU& pdu, struct sockaddr* link_addr, uint32_t len_addr,
343  const NetworkInterface& iface = NetworkInterface());
344  #endif // !_WIN32 || TINS_HAVE_PACKET_SENDER_PCAP_SENDPACKET
345 
360  PDU* recv_l3(PDU& pdu, struct sockaddr* link_addr, uint32_t len_addr, SocketType type);
361 
377  void send_l3(PDU& pdu, struct sockaddr* link_addr, uint32_t len_addr, SocketType type);
378 private:
379  static const int INVALID_RAW_SOCKET;
380 
381  typedef std::map<SocketType, int> SocketTypeMap;
382 
383  PacketSender(const PacketSender&);
384  PacketSender& operator=(const PacketSender&);
385  int find_type(SocketType type);
386  #ifndef _WIN32
387  bool ether_socket_initialized(const NetworkInterface& iface = NetworkInterface()) const;
388  int get_ether_socket(const NetworkInterface& iface = NetworkInterface());
389  #endif
390  template<typename T>
391  void send(PDU& pdu, const NetworkInterface& iface) {
392  static_cast<T&>(pdu).send(*this, iface);
393  }
394  #ifdef TINS_HAVE_PACKET_SENDER_PCAP_SENDPACKET
395  pcap_t* make_pcap_handle(const NetworkInterface& iface) const;
396  #endif // TINS_HAVE_PACKET_SENDER_PCAP_SENDPACKET
397 
398  PDU* recv_match_loop(const std::vector<int>& sockets,
399  PDU& pdu,
400  struct sockaddr* link_addr,
401  uint32_t addrlen,
402  bool is_layer_3);
403 
404  std::vector<int> sockets_;
405  #ifndef _WIN32
406  #if defined(BSD) || defined(__FreeBSD_kernel__)
407  typedef std::map<uint32_t, int> BSDEtherSockets;
408  BSDEtherSockets ether_socket_;
409  #else
410  int ether_socket_;
411  #endif
412  #endif
413  SocketTypeMap types_;
414  uint32_t _timeout, timeout_usec_;
415  NetworkInterface default_iface_;
416  // In BSD we need to store the buffer size, retrieved using BIOCGBLEN
417  #if defined(BSD) || defined(__FreeBSD_kernel__)
418  int buffer_size_;
419  #endif // BSD
420  #ifdef TINS_HAVE_PACKET_SENDER_PCAP_SENDPACKET
421  typedef std::map<NetworkInterface, pcap_t*> PcapHandleMap;
422  PcapHandleMap pcap_handles_;
423  #endif // TINS_HAVE_PACKET_SENDER_PCAP_SENDPACKET
424 };
425 
426 } // Tins
427 
428 #endif // TINS_PACKET_SENDER_H
SocketType
Definition: packet_sender.h:126
PacketSender & operator=(PacketSender &&rhs) TINS_NOEXCEPT
Move assignment operator.
Definition: packet_sender.h:160
Sends packets through a network interface.
Definition: packet_sender.h:116
static const uint32_t DEFAULT_TIMEOUT
Definition: packet_sender.h:121
PacketSender(PacketSender &&rhs) TINS_NOEXCEPT
Move constructor.
Definition: packet_sender.h:152
Abstraction of a network interface.
Definition: network_interface.h:47
Base class for protocol data units.
Definition: pdu.h:107