00001
00002
00003
00004
00005
00006
00007
00008
00009 #ifndef __libutilxx__sella__net__Socket_H__
00010 #define __libutilxx__sella__net__Socket_H__
00011
00012 #include "../../common.h"
00013 #include "SocketException.h"
00014 #include "IPAddressPort.h"
00015
00016 #include <sys/types.h>
00017 #include <linux/if_packet.h>
00018
00019 #include <string>
00020 #include <vector>
00021 #include <memory>
00022
00023 namespace sella {
00024 namespace net {
00025 class Socket;
00026 }
00027 }
00028
00029 class sella::net::Socket {
00030 public:
00031 typedef std::shared_ptr<Socket> shared;
00032 typedef Socket *ptr;
00033
00034 static const size_t RecvSizeDefault = 65536;
00035
00036 public:
00037 Socket(const Socket &sock) = delete;
00038 virtual ~Socket();
00039 void operator=(const Socket &sock) = delete;
00040
00041 void bind(const IPAddressPort &addressport) throw (SocketException);
00042 void bind(const IPAddress &address, const Port &port) throw (SocketException);
00043 void bind(const std::string &address = std::string(), const unsigned short port = 0) throw (SocketException);
00044 void connect(const IPAddressPort &addressport) throw (SocketException);
00045 void connect(const IPAddress &address, const Port &port) throw (SocketException);
00046 void connect(const std::string &address, const unsigned short port) throw (SocketException);
00047 ssize_t send(const void *data, size_t len, int flags = 0) throw (SocketException);
00048 ssize_t send(const std::string &data, int flags = 0) throw (SocketException);
00049 ssize_t send(const std::vector<uint8_t> &data, int flags = 0) throw (SocketException);
00050 ssize_t recv(void *data, size_t len, int flags = 0) throw (SocketException);
00051 ssize_t recv(std::string &data, size_t len = RecvSizeDefault, int flags = 0) throw (SocketException);
00052 ssize_t recv(std::vector<uint8_t> &data, size_t len = RecvSizeDefault, int flags = 0) throw (SocketException);
00053
00054 int getFD(void) const;
00055
00056 bool isRead(void) const;
00057 bool isWrite(void) const;
00058 bool isExcept(void) const;
00059 bool hasData(void) const throw (SocketException);
00060
00061 int getQueuedBytes(void) const throw (SocketException);
00062
00063 IPAddressPort getLocalIPAddressPort(void) const throw (SocketException);
00064 IPAddress getLocalIPAddress(void) const throw (SocketException);
00065 Port getLocalPort(void) const throw (SocketException);
00066
00067 IPAddressPort getRemoteIPAddressPort(void) const throw (SocketException);
00068 IPAddress getRemoteIPAddress(void) const throw (SocketException);
00069 Port getRemotePort(void) const throw (SocketException);
00070
00071 int getFamily(void) const throw ();
00072
00073 bool isUsable(void) const throw ();
00074 bool hasMux(void) const throw ();
00075
00076
00077 void setBlock(bool on = true) throw (SocketException);
00078 bool isBlock(void) throw (SocketException);
00079
00080
00081 void setIpOptions(const void *option, socklen_t len) throw (SocketException);
00082 void setIpPktInfo(bool on = true) throw (SocketException);
00083 void setIpRecvTOS(bool on = true) throw (SocketException);
00084 void setIpRecvTTL(bool on = true) throw (SocketException);
00085 void setIpRecvOpts(bool on = true) throw (SocketException);
00086 void setIpRetOpts(bool on = true) throw (SocketException);
00087 void setIpTOS(unsigned char tos) throw (SocketException);
00088 unsigned char getIpTOS(void) const throw (SocketException);
00089 void setIpTTL(int ttl) throw (SocketException);
00090 int getIpTTL(void) const throw (SocketException);
00091 void setIpHdrIncl(bool on = true) throw (SocketException);
00092 void setIpRecvErr(bool on = true) throw (SocketException);
00093 void setIpMtuDiscover(bool on = true) throw (SocketException);
00094 int getIpMtu(void) const throw (SocketException);
00095 void setIpRouterAlert(bool on = true) throw (SocketException);
00096 void setIpMulticastTTL(int ttl) throw (SocketException);
00097 int getIpMulticastTTL(void) const throw (SocketException);
00098 void setIpMulticastLoop(bool on = true) throw (SocketException);
00099 void setIpAddMembership(struct ip_mreqn& m) throw (SocketException);
00100 void setIpDropMembership(struct ip_mreqn& m) throw (SocketException);
00101
00102
00103 void setSoReuseAddr(bool on = true) throw (SocketException);
00104 void setSoKeepAlive(bool on = true) throw (SocketException);
00105 void setSoNoSigPipe(bool on = true) throw (SocketException);
00106 bool getSoAcceptConn(void) const throw (SocketException);
00107 void setSoBindToDevice(const std::string &interface) throw (SocketException);
00108 void setSoBroadcast(bool on = true) throw (SocketException);
00109 void setSoDebug(bool on = true) throw (SocketException);
00110 int getSoError(void) const throw (SocketException);
00111 void setSoDontRoute(bool on = true) throw (SocketException);
00112 void setSoLinger(int onoff, int linger) throw (SocketException);
00113 void setSoPriority(int pri) throw (SocketException);
00114 void setSoRcvTimeo(struct timeval &tv) throw (SocketException);
00115 void setSoSndTimeo(struct timeval &tv) throw (SocketException);
00116 void setSoRcvBuf(int bytes) throw (SocketException);
00117 void setSoRcvBufForce(int bytes) throw (SocketException);
00118 int getSoRcvBuf(void) const throw (SocketException);
00119 void setSoSndBuf(int bytes) throw (SocketException);
00120 void setSoSndBufForce(int bytes) throw (SocketException);
00121 int getSoSndBuf(void) const throw (SocketException);
00122 void setSoTimestamp(bool on = true) throw (SocketException);
00123 int getSoType(void) throw (SocketException);
00124
00125
00126 void setIpv6AddrForm(int family = AF_INET) throw (SocketException);
00127 void setIp6AddMembership(struct ipv6_mreq& m) throw (SocketException);
00128 void setIp6DropMembership(struct ipv6_mreq& m) throw (SocketException);
00129 void setIpv6Mtu(int bytes) throw (SocketException);
00130 void setIpv6MtuDiscover(bool on = true) throw (SocketException);
00131 void setIpv6MutlicastHops(int hops = -1) throw (SocketException);
00132 void setIpv6MutlicastIf(int ifindex) throw (SocketException);
00133 void setIpv6MulticastLoop(bool on = true) throw (SocketException);
00134 void setIpv6RecvPktInfo(bool on = true) throw (SocketException);
00135 void setIpv6RtHdr(bool on = true) throw (SocketException);
00136 void setIpv6AuthHdr(bool on = true) throw (SocketException);
00137 void setIpv6DstOpts(bool on = true) throw (SocketException);
00138 void setIpv6HopOpts(bool on = true) throw (SocketException);
00139 void setIpv6FlowInfo(bool on = true) throw (SocketException);
00140 void setIpv6HopLimit(bool on = true) throw (SocketException);
00141 void setIpv6RecvErr(bool on = true) throw (SocketException);
00142 void setIpv6RouterAlert(int option = -1) throw (SocketException);
00143 void setIpv6UnicastHops(int limit = -1) throw (SocketException);
00144 void setIpv6V6Only(bool on = true) throw (SocketException);
00145 bool getIpv6V6Only(void) const throw (SocketException);
00146
00147
00148 void mmapRingBuffer(unsigned char** ring, struct tpacket_req &req) throw (SocketException);
00149 void munmapRingBuffer(unsigned char** ring, struct tpacket_req &req) throw (SocketException);
00150 void setPacketRxRing(struct tpacket_req &req, int version = TPACKET_V2) throw (SocketException);
00151 void setPacketTxRing(struct tpacket_req &req, int version = TPACKET_V2) throw (SocketException);
00152 int getPacketHdrLen(int version = TPACKET_V2) throw (SocketException);
00153 void setPacketLoss(bool drop = true) throw (SocketException);
00154
00155 static unsigned short resolveService(const std::string &service, const std::string &protocol = std::string("tcp")) throw (SocketException);
00156
00157 friend class SocketMux;
00158
00159 protected:
00160 Socket(int s) throw (SocketException);
00161 Socket(int family, int type, int protocol) throw (SocketException);
00162
00163 void close(void) throw (SocketException);
00164 static void initAddr(int family, const std::string &address, unsigned short port, sockaddr_storage &addr, socklen_t size) throw (SocketException);
00165
00166 int s;
00167 int family;
00168
00169 bool usable;
00170 bool mux;
00171
00172 bool read;
00173 bool write;
00174 bool except;
00175 };
00176
00177 #endif
00178
00179
00180
00181