00001
00002
00003
00004
00005
00006
00007
00008
00009 #ifndef __libutilxx__sella__net__EtherSocket_H__
00010 #define __libutilxx__sella__net__EtherSocket_H__
00011
00012 #include "../../common.h"
00013 #include "Socket.h"
00014
00015 #include <sys/socket.h>
00016
00017 #include <linux/if_ether.h>
00018 #include <linux/if_packet.h>
00019
00020 namespace sella {
00021 namespace net {
00022 class EtherSocket;
00023 }
00024 }
00025
00026 class sella::net::EtherSocket : public Socket {
00027 public:
00028 typedef std::shared_ptr<EtherSocket> shared;
00029 typedef EtherSocket *ptr;
00030
00031
00032 static const int EthAll = ETH_P_ALL;
00033 static const int EthIP = ETH_P_IP;
00034 static const int EthARP = ETH_P_ARP;
00035 static const int EthRARP = ETH_P_RARP;
00036 static const int Eth8021Q = ETH_P_8021Q;
00037 static const int EthMPLSUC = ETH_P_MPLS_UC;
00038 static const int EthMPLSMC = ETH_P_MPLS_MC;
00039
00040 static const unsigned char BroadcastMAC[];
00041
00042 public:
00043 EtherSocket(int etherType, const std::string &interface, bool raw = false) throw (SocketException);
00044 EtherSocket(int etherType, int ifIndex, bool raw = false) throw (SocketException);
00045 EtherSocket(int etherType, bool raw = false) throw (SocketException);
00046 EtherSocket(const EtherSocket &sock) = delete;
00047 virtual ~EtherSocket();
00048 void operator=(const EtherSocket &sock) = delete;
00049
00050 static const EtherSocket::shared shared_ptr(int etherType) throw (SocketException);
00051
00052 EtherSocket& setInterface(const std::string &interface) throw (SocketException);
00053 EtherSocket& setIfIndex(int index) throw ();
00054
00055 int getEtherType(void) const throw ();
00056 std::string getInterface(void) const throw (SocketException);
00057 int getIfIndex(void) const throw ();
00058 bool isRaw(void) const throw ();
00059
00060 void bindInterface(void) throw (SocketException);
00061 void setSoBindToDevice(const std::string &interface) throw (SocketException);
00062 void setSoBindToDevice(void) throw (SocketException);
00063 void setPromiscous(bool on = true) throw (SocketException);
00064
00065 ssize_t sendto(const void *data, size_t len, const std::string &dst_mac, int flags = 0) throw (SocketException);
00066 ssize_t sendto(const void *data, size_t len, const unsigned char *dst_mac = BroadcastMAC, int flags = 0) throw (SocketException);
00067 ssize_t sendto(const void *data, size_t len, int flags, const struct sockaddr_ll *dst_addr, socklen_t addrlen) throw (SocketException);
00068 ssize_t recvfrom(void *data, size_t len, int flags = 0, struct sockaddr_ll *src_addr = NULL, socklen_t *addrlen = NULL) throw (SocketException);
00069 ssize_t recvfrom(void *data, size_t len, std::string &src_mac, int flags = 0) throw (SocketException);
00070
00071 void bind(const IPAddressPort &addressport) throw (SocketException) = delete;
00072 void bind(const IPAddress &address, const Port &port) throw (SocketException) = delete;
00073 void bind(const std::string &address = std::string(), const unsigned short port = 0) throw (SocketException) = delete;
00074 void connect(const IPAddressPort &addressport) throw (SocketException) = delete;
00075 void connect(const IPAddress &address, const Port &port) throw (SocketException) = delete;
00076 void connect(const std::string &address, const unsigned short port) throw (SocketException) = delete;
00077 ssize_t send(const void *data, size_t len, int flags = 0) throw (SocketException) = delete;
00078 ssize_t recv(void *data, size_t len, int flags = 0) throw (SocketException) = delete;
00079
00080 protected:
00081 private:
00082 int etherType;
00083 int ifIndex;
00084 bool raw;
00085 };
00086
00087 #endif
00088
00089
00090
00091