9 #ifndef __libutilxx__sella__net__Socket_H__
10 #define __libutilxx__sella__net__Socket_H__
12 #include "../../common.h"
13 #include "SocketException.h"
14 #include "IPAddressPort.h"
16 #include <sys/types.h>
17 #include <linux/if_packet.h>
31 typedef std::shared_ptr<Socket> shared;
34 static const size_t RecvSizeDefault = 65536;
39 void operator=(
const Socket &sock) =
delete;
41 void bind(
const IPAddressPort &addressport)
throw (SocketException);
42 void bind(
const IPAddress &address,
const Port &port)
throw (SocketException);
43 void bind(
const std::string &address = std::string(),
const unsigned short port = 0)
throw (SocketException);
44 void connect(
const IPAddressPort &addressport)
throw (SocketException);
45 void connect(
const IPAddress &address,
const Port &port)
throw (SocketException);
46 void connect(
const std::string &address,
const unsigned short port)
throw (SocketException);
47 virtual ssize_t send(
const void *data,
size_t len,
int flags = 0)
throw (SocketException);
48 virtual ssize_t send(
const std::string &data,
int flags = 0)
throw (SocketException);
49 virtual ssize_t send(
const std::vector<uint8_t> &data,
int flags = 0)
throw (SocketException);
50 virtual ssize_t recv(
void *data,
size_t len,
int flags = 0)
throw (SocketException);
51 virtual ssize_t recv(std::string &data,
size_t len = RecvSizeDefault,
int flags = 0)
throw (SocketException);
52 virtual ssize_t recv(std::vector<uint8_t> &data,
size_t len = RecvSizeDefault,
int flags = 0)
throw (SocketException);
54 int getFD(
void)
const;
56 bool isRead(
void)
const;
57 bool isWrite(
void)
const;
58 bool isExcept(
void)
const;
59 bool hasData(
void)
const throw (SocketException);
61 int getQueuedBytes(
void)
const throw (SocketException);
63 IPAddressPort getLocalIPAddressPort(
void)
const throw (SocketException);
64 IPAddress getLocalIPAddress(
void)
const throw (SocketException);
65 Port getLocalPort(
void)
const throw (SocketException);
67 IPAddressPort getRemoteIPAddressPort(
void)
const throw (SocketException);
68 IPAddress getRemoteIPAddress(
void)
const throw (SocketException);
69 Port getRemotePort(
void)
const throw (SocketException);
71 int getFamily(
void)
const throw ();
73 bool isUsable(
void)
const throw ();
74 bool hasMux(
void)
const throw ();
76 virtual bool isSecure(
void)
const throw ();
79 void setBlock(
bool on =
true)
throw (SocketException);
80 bool isBlock(
void)
throw (SocketException);
83 void setIpOptions(
const void *option, socklen_t len)
throw (SocketException);
84 void setIpPktInfo(
bool on =
true)
throw (SocketException);
85 void setIpRecvTOS(
bool on =
true)
throw (SocketException);
86 void setIpRecvTTL(
bool on =
true)
throw (SocketException);
87 void setIpRecvOpts(
bool on =
true)
throw (SocketException);
88 void setIpRetOpts(
bool on =
true)
throw (SocketException);
89 void setIpTOS(
unsigned char tos)
throw (SocketException);
90 unsigned char getIpTOS(
void)
const throw (SocketException);
91 void setIpTTL(
int ttl)
throw (SocketException);
92 int getIpTTL(
void)
const throw (SocketException);
93 void setIpHdrIncl(
bool on =
true)
throw (SocketException);
94 void setIpRecvErr(
bool on =
true)
throw (SocketException);
95 void setIpMtuDiscover(
bool on =
true)
throw (SocketException);
96 int getIpMtu(
void)
const throw (SocketException);
97 void setIpRouterAlert(
bool on =
true)
throw (SocketException);
98 void setIpMulticastTTL(
int ttl)
throw (SocketException);
99 int getIpMulticastTTL(
void)
const throw (SocketException);
100 void setIpMulticastLoop(
bool on =
true)
throw (SocketException);
101 void setIpAddMembership(
struct ip_mreqn& m)
throw (SocketException);
102 void setIpDropMembership(
struct ip_mreqn& m)
throw (SocketException);
105 void setSoReuseAddr(
bool on =
true)
throw (SocketException);
106 void setSoKeepAlive(
bool on =
true)
throw (SocketException);
107 void setSoNoSigPipe(
bool on =
true)
throw (SocketException);
108 bool getSoAcceptConn(
void)
const throw (SocketException);
109 void setSoBindToDevice(
const std::string &interface)
throw (SocketException);
110 void setSoBroadcast(
bool on =
true)
throw (SocketException);
111 void setSoDebug(
bool on =
true)
throw (SocketException);
112 int getSoError(
void)
const throw (SocketException);
113 void setSoDontRoute(
bool on =
true)
throw (SocketException);
114 void setSoLinger(
int onoff,
int linger)
throw (SocketException);
115 void setSoPriority(
int pri)
throw (SocketException);
116 void setSoRcvTimeo(
struct timeval &tv)
throw (SocketException);
117 void setSoSndTimeo(
struct timeval &tv)
throw (SocketException);
118 void setSoRcvBuf(
int bytes)
throw (SocketException);
119 void setSoRcvBufForce(
int bytes)
throw (SocketException);
120 int getSoRcvBuf(
void)
const throw (SocketException);
121 void setSoSndBuf(
int bytes)
throw (SocketException);
122 void setSoSndBufForce(
int bytes)
throw (SocketException);
123 int getSoSndBuf(
void)
const throw (SocketException);
124 void setSoTimestamp(
bool on =
true)
throw (SocketException);
125 int getSoType(
void)
throw (SocketException);
128 void setIpv6AddrForm(
int family = AF_INET)
throw (SocketException);
129 void setIp6AddMembership(
struct ipv6_mreq& m)
throw (SocketException);
130 void setIp6DropMembership(
struct ipv6_mreq& m)
throw (SocketException);
131 void setIpv6Mtu(
int bytes)
throw (SocketException);
132 void setIpv6MtuDiscover(
bool on =
true)
throw (SocketException);
133 void setIpv6MutlicastHops(
int hops = -1)
throw (SocketException);
134 void setIpv6MutlicastIf(
int ifindex)
throw (SocketException);
135 void setIpv6MulticastLoop(
bool on =
true)
throw (SocketException);
136 void setIpv6RecvPktInfo(
bool on =
true)
throw (SocketException);
137 void setIpv6RtHdr(
bool on =
true)
throw (SocketException);
138 void setIpv6AuthHdr(
bool on =
true)
throw (SocketException);
139 void setIpv6DstOpts(
bool on =
true)
throw (SocketException);
140 void setIpv6HopOpts(
bool on =
true)
throw (SocketException);
141 void setIpv6FlowInfo(
bool on =
true)
throw (SocketException);
142 void setIpv6HopLimit(
bool on =
true)
throw (SocketException);
143 void setIpv6RecvErr(
bool on =
true)
throw (SocketException);
144 void setIpv6RouterAlert(
int option = -1)
throw (SocketException);
145 void setIpv6UnicastHops(
int limit = -1)
throw (SocketException);
146 void setIpv6V6Only(
bool on =
true)
throw (SocketException);
147 bool getIpv6V6Only(
void)
const throw (SocketException);
150 void mmapRingBuffer(
unsigned char** ring,
struct tpacket_req &req)
throw (SocketException);
151 void munmapRingBuffer(
unsigned char** ring,
struct tpacket_req &req)
throw (SocketException);
152 void setPacketRxRing(
struct tpacket_req &req,
int version = TPACKET_V2)
throw (SocketException);
153 void setPacketTxRing(
struct tpacket_req &req,
int version = TPACKET_V2)
throw (SocketException);
154 int getPacketHdrLen(
int version = TPACKET_V2)
throw (SocketException);
155 void setPacketLoss(
bool drop =
true)
throw (SocketException);
157 static unsigned short resolveService(
const std::string &service,
const std::string &protocol = std::string(
"tcp"))
throw (SocketException);
162 Socket(
int s)
throw (SocketException);
163 Socket(
int family,
int type,
int protocol)
throw (SocketException);
165 virtual void close(
void)
throw (SocketException);
166 static void initAddr(
int family,
const std::string &address,
unsigned short port, sockaddr_storage &addr, socklen_t size)
throw (SocketException);