libutil++  1.9.3
 All Classes Functions Variables
Socket.h
1 /*
2 ** libutil++
3 ** $Id: Socket.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__Socket_H__
10 #define __libutilxx__sella__net__Socket_H__
11 
12 #include "../../common.h"
13 #include "SocketException.h"
14 #include "IPAddressPort.h"
15 
16 #include <sys/types.h>
17 #include <linux/if_packet.h>
18 
19 #include <string>
20 #include <vector>
21 #include <memory>
22 
23 namespace sella {
24  namespace net {
25  class Socket;
26  }
27 }
28 
30  public:
31  typedef std::shared_ptr<Socket> shared;
32  typedef Socket *ptr;
33 
34  static const size_t RecvSizeDefault = 65536;
35 
36  public:
37  Socket(const Socket &sock) = delete;
38  virtual ~Socket();
39  void operator=(const Socket &sock) = delete;
40 
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);
53 
54  int getFD(void) const;
55 
56  bool isRead(void) const;
57  bool isWrite(void) const;
58  bool isExcept(void) const;
59  bool hasData(void) const throw (SocketException);
60 
61  int getQueuedBytes(void) const throw (SocketException);
62 
63  IPAddressPort getLocalIPAddressPort(void) const throw (SocketException);
64  IPAddress getLocalIPAddress(void) const throw (SocketException);
65  Port getLocalPort(void) const throw (SocketException);
66 
67  IPAddressPort getRemoteIPAddressPort(void) const throw (SocketException);
68  IPAddress getRemoteIPAddress(void) const throw (SocketException);
69  Port getRemotePort(void) const throw (SocketException);
70 
71  int getFamily(void) const throw ();
72 
73  bool isUsable(void) const throw ();
74  bool hasMux(void) const throw ();
75 
76  virtual bool isSecure(void) const throw ();
77 
78  /* fcntl - man fcntl */
79  void setBlock(bool on = true) throw (SocketException);
80  bool isBlock(void) throw (SocketException);
81 
82  /* IP Options - man 7 ip */
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);
103 
104  /* Socket Options - man 7 socket */
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); /* Applies to outgoing traffic */
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);
126 
127  /* Socket Options (IPv6) - man 7 ipv6 - NOTE: Set before bind() for most of these */
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);
148 
149  /* Packet Options - man 7 packet */
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); /* Zero copy */
153  void setPacketTxRing(struct tpacket_req &req, int version = TPACKET_V2) throw (SocketException); /* Zero copy */
154  int getPacketHdrLen(int version = TPACKET_V2) throw (SocketException);
155  void setPacketLoss(bool drop = true) throw (SocketException);
156 
157  static unsigned short resolveService(const std::string &service, const std::string &protocol = std::string("tcp")) throw (SocketException);
158 
159  friend class SocketMux;
160 
161  protected:
162  Socket(int s) throw (SocketException);
163  Socket(int family, int type, int protocol) throw (SocketException);
164 
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);
167 
168  int s;
169  int family;
170 
171  bool usable;
172  bool mux;
173 
174  bool read;
175  bool write;
176  bool except;
177 };
178 
179 #endif
180 
181 /*
182 ** vim: noet ts=3 sw=3
183 */