libutil++  1.9.3
 All Classes Functions Variables
EtherSocket.h
1 /*
2 ** libutil++
3 ** $Id: EtherSocket.h 1653 2016-02-28 19:54:59Z sella $
4 ** Copyright (c) 2011-2016 Digital Genesis, LLC. All Rights Reserved.
5 ** Released under the LGPL Version 2.1 License.
6 ** http://www.digitalgenesis.com
7 */
8 
9 #ifndef __libutilxx__sella__net__EtherSocket_H__
10 #define __libutilxx__sella__net__EtherSocket_H__
11 
12 #include "../../common.h"
13 #include "Socket.h"
14 
15 #include <sys/socket.h>
16 
17 #include <linux/if_ether.h>
18 #include <linux/if_packet.h>
19 
20 namespace sella {
21  namespace net {
22  class EtherSocket;
23  }
24 }
25 
27  public:
28  typedef std::shared_ptr<EtherSocket> shared;
29  typedef EtherSocket *ptr;
30 
31  /* NOTE: Full list in <linux/if_ether.h> */
32  static const int EthAll = ETH_P_ALL; /* 0x0003 */
33  static const int EthIP = ETH_P_IP; /* 0x8000 */
34  static const int EthARP = ETH_P_ARP; /* 0x0806 */
35  static const int EthRARP = ETH_P_RARP; /* 0x8035 */
36  static const int Eth8021Q = ETH_P_8021Q; /* 0x8100 */
37  static const int EthMPLSUC = ETH_P_MPLS_UC; /* 0x8847 */
38  static const int EthMPLSMC = ETH_P_MPLS_MC; /* 0x8848 */
39 
40  static const unsigned char BroadcastMAC[];
41 
42  public:
43  EtherSocket(int etherType, const std::string &interface, bool raw = false) throw (SocketException);
44  EtherSocket(int etherType, int ifIndex, bool raw = false) throw (SocketException);
45  EtherSocket(int etherType, bool raw = false) throw (SocketException);
46  EtherSocket(const EtherSocket &sock) = delete;
47  virtual ~EtherSocket();
48  void operator=(const EtherSocket &sock) = delete;
49 
50  static const EtherSocket::shared shared_ptr(int etherType) throw (SocketException);
51 
52  EtherSocket& setInterface(const std::string &interface) throw (SocketException);
53  EtherSocket& setIfIndex(int index) throw ();
54 
55  int getEtherType(void) const throw ();
56  std::string getInterface(void) const throw (SocketException);
57  int getIfIndex(void) const throw ();
58  bool isRaw(void) const throw ();
59 
60  void bindInterface(void) throw (SocketException); /* Applies to incoming traffic. Works with recv(), but not recvfrom(). */
61  void setSoBindToDevice(const std::string &interface) throw (SocketException); /* Applies to outgoing traffic */
62  void setSoBindToDevice(void) throw (SocketException); /* Applies to outgoing traffic. */
63  void setPromiscous(bool on = true) throw (SocketException);
64 
65  ssize_t sendto(const void *data, size_t len, const std::string &dst_mac, int flags = 0) throw (SocketException);
66  ssize_t sendto(const void *data, size_t len, const unsigned char *dst_mac = BroadcastMAC, int flags = 0) throw (SocketException);
67  ssize_t sendto(const void *data, size_t len, int flags, const struct sockaddr_ll *dst_addr, socklen_t addrlen) throw (SocketException);
68  ssize_t recvfrom(void *data, size_t len, int flags = 0, struct sockaddr_ll *src_addr = NULL, socklen_t *addrlen = NULL) throw (SocketException);
69  ssize_t recvfrom(void *data, size_t len, std::string &src_mac, int flags = 0) throw (SocketException);
70 
71  void bind(const IPAddressPort &addressport) throw (SocketException) = delete;
72  void bind(const IPAddress &address, const Port &port) throw (SocketException) = delete;
73  void bind(const std::string &address = std::string(), const unsigned short port = 0) throw (SocketException) = delete;
74  void connect(const IPAddressPort &addressport) throw (SocketException) = delete;
75  void connect(const IPAddress &address, const Port &port) throw (SocketException) = delete;
76  void connect(const std::string &address, const unsigned short port) throw (SocketException) = delete;
77 
78  virtual ssize_t send(const void *data, size_t len, int flags = 0) throw (SocketException) {
79  THROW(SocketException, "Not supported with EtherSocket");
80  }
81 
82  virtual ssize_t recv(void *data, size_t len, int flags = 0) throw (SocketException) {
83  THROW(SocketException, "Not supported with EtherSocket");
84  }
85 
86  protected:
87  private:
88  int etherType;
89  int ifIndex;
90  bool raw;
91 };
92 
93 #endif
94 
95 /*
96 ** vim: noet ts=3 sw=3
97 */