32 #ifndef NIDAS_UTIL_SOCKET_H
33 #define NIDAS_UTIL_SOCKET_H
44 #include <sys/types.h>
45 #include <sys/socket.h>
50 #include <netinet/in.h>
56 namespace nidas {
namespace util {
216 void close() throw(IOException);
227 void listen() throw(IOException);
249 size_t recv(
void* buf,
size_t len,
int flags=0)
252 size_t recvfrom(
void* buf,
size_t len,
int flags,
269 size_t send(const
void* buf,
size_t len,
int flags = 0)
272 size_t send(const struct iovec* iov,
int iovcnt,
int flags = 0)
275 size_t sendto(const
void* buf,
size_t len,
int flags,
292 void sendall(const
void* buf,
size_t len,
int flags = 0)
441 Socket(
int domain=AF_INET)
throw(IOException);
454 Socket(
const std::string& host,
int port)
469 Socket(
int fd,
const SocketAddress& raddr)
throw(IOException);
483 static std::vector<Socket*> createSocketPair(
int type=SOCK_STREAM)
502 _impl.setNonBlocking(val);
507 return _impl.isNonBlocking();
512 _impl.setTcpNoDelay(val);
517 return _impl.getTcpNoDelay();
525 _impl.setKeepAlive(val);
533 return _impl.getKeepAlive();
556 _impl.setKeepAliveIdleSecs(val);
566 return _impl.getKeepAliveIdleSecs();
571 return _impl.getInQueueSize();
576 return _impl.getOutQueueSize();
584 return _impl.getInterfaces();
590 void connect(
const std::string& host,
int port)
593 _impl.connect(host,port);
602 _impl.connect(addr,port);
617 int getFd()
const {
return _impl.getFd(); }
619 size_t recv(
void* buf,
size_t len,
int flags = 0)
621 return _impl.recv(buf,len,flags);
630 size_t send(
const void* buf,
size_t len,
int flags=MSG_NOSIGNAL)
632 return _impl.send(buf,len,flags);
635 size_t send(
const struct iovec* iov,
int iovcnt,
int flags=MSG_NOSIGNAL)
637 return _impl.send(iov,iovcnt,flags);
651 void sendall(
const void* buf,
size_t len,
int flags=MSG_NOSIGNAL)
653 return _impl.sendall(buf,len,flags);
658 _impl.setReceiveBufferSize(size);
662 return _impl.getReceiveBufferSize();
667 _impl.setSendBufferSize(size);
671 return _impl.getSendBufferSize();
679 return _impl.getRemoteSocketAddress();
685 return _impl.getRemotePort();
693 return _impl.getLocalSocketAddress();
700 return _impl.getLocalPort();
765 ServerSocket(
int port=0,
int backlog=10)
throw(IOException);
788 int getFd()
const {
return _impl.getFd(); }
793 void close() throw(IOException);
810 return _impl.accept();
815 _impl.setReceiveBufferSize(size);
819 return _impl.getReceiveBufferSize();
824 _impl.setSendBufferSize(size);
828 return _impl.getSendBufferSize();
836 return _impl.getLocalSocketAddress();
845 _impl.setNonBlocking(val);
850 return _impl.isNonBlocking();
971 _impl.setTimeout(val);
979 void connect(
const std::string& host,
int port)
982 _impl.connect(host,port);
993 _impl.connect(addr,port);
1004 _impl.connect(addr);
1016 _impl.bind(addr,port);
1031 int getFd()
const {
return _impl.getFd(); }
1040 _impl.receive(packet);
1055 _impl.receive(packet,info,flags);
1060 _impl.send(packet,flags);
1063 size_t recv(
void* buf,
size_t len,
int flags=0) throw(IOException)
1065 return _impl.recv(buf,len,flags);
1071 return _impl.recvfrom(buf,
len,flags,from);
1074 size_t send(
const void* buf,
size_t len,
int flags = 0)
1075 throw(IOException) {
1076 return _impl.send(buf,len,flags);
1079 size_t send(
const struct iovec* iov,
int iovcnt,
int flags=MSG_NOSIGNAL)
1080 throw(IOException) {
1081 return _impl.send(iov,iovcnt,flags);
1087 return _impl.sendto(buf,
len,flags,to);
1090 size_t sendto(
const struct iovec* iov,
int iovcnt,
int flags,
1093 return _impl.sendto(iov,iovcnt,flags,to);
1101 return _impl.getLocalSocketAddress();
1108 return _impl.getLocalPort();
1118 _impl.setNonBlocking(val);
1123 return _impl.isNonBlocking();
1128 _impl.setReceiveBufferSize(size);
1133 return _impl.getReceiveBufferSize();
1138 _impl.setSendBufferSize(size);
1142 return _impl.getSendBufferSize();
1147 return _impl.getInterfaces();
1152 _impl.setBroadcastEnable(val);
1157 return _impl.getBroadcastEnable();
1169 _impl.setPktInfo(val);
1280 _impl.joinGroup(groupAddr);
1293 _impl.joinGroup(groupAddr,iface);
1300 _impl.leaveGroup(groupAddr);
1309 _impl.leaveGroup(groupAddr,iface);
1314 _impl.setTimeToLive(val);
1319 return _impl.getTimeToLive();
1335 _impl.setTimeToLive(val);
1340 return _impl.getTimeToLive();
1356 _impl.setInterface(maddr,iface);
1369 _impl.setInterface(iaddr);
1379 return _impl.findInterface(iaddr);
1387 return _impl.getInterfaces();
void setInterface(Inet4Address iaddr)
Set the interface for a given multicast address.
Definition: Socket.h:1368
bool getPktInfo() const
Definition: Socket.h:344
size_t sendto(const struct iovec *iov, int iovcnt, int flags, const SocketAddress &to)
Definition: Socket.h:1090
Abstract base class for a UDP datagram.
Definition: DatagramPacket.h:39
int getInQueueSize() const
Get number of bytes currently unread in the local input queue.
Definition: Socket.cc:1034
int getKeepAliveIdleSecs() const
Get the current value of TCP_KEEPIDLE on this socket.
Definition: Socket.cc:1021
void setSendBufferSize(int size)
Definition: Socket.h:822
size_t recv(void *buf, size_t len, int flags=0)
Definition: Socket.h:1063
void listen()
Definition: Socket.cc:241
void setNonBlocking(bool val)
Definition: Socket.cc:438
A datagram socket to be used for multicasts.
Definition: Socket.h:1241
int getKeepAliveIdleSecs()
Get the current value of TCP_KEEPIDLE on this socket.
Definition: Socket.h:564
void receive(DatagramPacketBase &packet)
Definition: Socket.cc:474
void setMulticastLoop(bool val)
Whether to set the IP_MULTICAST_LOOP socket option.
Definition: Socket.cc:1300
void setReuseAddress(bool val)
Definition: Socket.h:137
void send(const DatagramPacketBase &packet, int flags=0)
Definition: Socket.h:1058
bool isNonBlocking() const
Definition: Socket.h:1121
int getSendBufferSize()
Definition: Socket.cc:937
bool getBroadcastEnable() const
Definition: Socket.cc:1335
void setPktInfo(bool val)
Control whether a IP_PKTINFO ancillary message is received with each datagram.
Definition: Socket.h:1167
Implementation of a socket, providing a C++ interface to system socket calls: socket,bind,listen,accept,setsockopt, etc.
Definition: Socket.h:76
int getSendBufferSize()
Definition: Socket.h:826
MulticastSocket(int port)
Create multicast socket, bind it to a local port.
Definition: Socket.h:1261
void setReceiveBufferSize(int size)
Definition: Socket.cc:909
Inet4NetworkInterface findInterface(const Inet4Address &) const
Definition: Socket.cc:1207
void setBacklog(int val)
Definition: Socket.h:103
void setTimeout(int val)
Set the timeout for receive(), recv(), and recvfrom() methods.
Definition: Socket.cc:123
void setReceiveBufferSize(int size)
Definition: Socket.h:813
Inet4NetworkInterface getInterface() const
Definition: Socket.h:1372
int getFd() const
Definition: Socket.h:1031
void receive(DatagramPacketBase &packet)
Read a packet from the DatagramSocket.
Definition: Socket.h:1038
void setBroadcastEnable(bool val)
Definition: Socket.h:1150
An interface for a socket address.
Definition: SocketAddress.h:36
MulticastSocket()
Create an unbound multicast socket.
Definition: Socket.h:1253
void receive(DatagramPacketBase &packet, Inet4PacketInfo &info, int flags=0)
Read a packet from the DatagramSocket.
Definition: Socket.h:1053
void connect(const SocketAddress &addr)
Datagrams are connectionless, so this doesn't establish a true connection, it just sets the default d...
Definition: Socket.h:1001
void connect(const Inet4Address &addr, int port)
Datagrams are connectionless, so this doesn't establish a true connection, it just sets the default d...
Definition: Socket.h:990
static Inet4NetworkInterface getInterface(const Inet4Address &addr)
Return an Inet4NetworkInterface corresponding to the given address.
Definition: Inet4NetworkInterface.cc:37
A socket for sending or receiving datagrams, either unicast, broadcast or multicast.
Definition: Socket.h:912
size_t send(const struct iovec *iov, int iovcnt, int flags=MSG_NOSIGNAL)
Definition: Socket.h:1079
bool isNonBlocking() const
Definition: Socket.h:848
int getOutQueueSize() const
Get number of bytes currently unsent in the local output queue.
Definition: Socket.cc:1046
void setMulticastLoop(bool val)
Whether to set the IP_MULTICAST_LOOP socket option.
Definition: Socket.h:1333
int getFd() const
Definition: Socket.h:788
void setKeepAliveIdleSecs(int val)
Set the number of seconds a connection needs to be idle before TCP begins sending out keep-alive prob...
Definition: Socket.cc:997
int getFd() const
Definition: Socket.h:95
void setTcpNoDelay(bool val)
Definition: Socket.h:510
bool getMulticastLoop() const
Definition: Socket.cc:1311
std::list< Inet4NetworkInterface > getInterfaces() const
Definition: Socket.h:1145
int getTimeToLive() const
Definition: Socket.h:1317
std::list< Inet4NetworkInterface > getInterfaces() const
Get a list of all my network interfaces.
Definition: Socket.h:582
size_t recvfrom(void *buf, size_t len, int flags, SocketAddress &from)
Definition: Socket.cc:749
void setKeepAlive(bool val)
Set or unset the SO_KEEPALIVE socket option.
Definition: Socket.cc:973
A IP version 4 socket address, containing a host address, and a port number.
Definition: Inet4SocketAddress.h:41
~SocketImpl()
Definition: Socket.cc:117
size_t sendto(const void *buf, size_t len, int flags, const SocketAddress &to)
Definition: Socket.h:1084
bool _pktInfo
Definition: Socket.h:392
void connect(const std::string &host, int port)
Datagrams are connectionless, so this doesn't establish a true connection, it just sets the default d...
Definition: Socket.h:979
int getReceiveBufferSize()
Definition: Socket.h:660
int getSendBufferSize()
Definition: Socket.h:1140
void close()
Definition: Socket.h:958
void bind(const Inet4Address &addr, int port)
Bind the DatagramSocket to the specified local address.
Definition: Socket.h:1013
int getOutQueueSize() const
Definition: Socket.h:574
void setSendBufferSize(int size)
Definition: Socket.h:1136
const SocketAddress & getRemoteSocketAddress() const
Get remote socket address of this socket.
Definition: Socket.h:124
size_t recvfrom(void *buf, size_t len, int flags, SocketAddress &from)
Definition: Socket.h:1068
int getLocalPort() const
Get local port number of this socket.
Definition: Socket.h:1107
void bind(int port)
Definition: Socket.cc:179
int getDomain() const
Definition: Socket.h:841
const SocketAddress & getLocalSocketAddress() const
Fetch the local address that this socket is bound to.
Definition: Socket.h:834
A stream (TCP) socket that is used to listen for connections.
Definition: Socket.h:755
Socket * accept()
Definition: Socket.cc:251
int getLocalPort() const
Get local port number of this socket.
Definition: Socket.h:699
size_t sendto(const void *buf, size_t len, int flags, const SocketAddress &to)
Definition: Socket.cc:860
void getLocalAddr()
Do system call to determine local address of this socket.
Definition: Socket.cc:390
int _backlog
Definition: Socket.h:387
bool _hasTimeout
Definition: Socket.h:389
bool getTcpNoDelay()
Get the TCP_NODELAY (man 7 tcp) option on the socket.
Definition: Socket.cc:961
void setSendBufferSize(int size)
Definition: Socket.cc:929
SocketImpl & operator=(const SocketImpl &rhs)
Assignment operator.
Definition: Socket.cc:99
void bind(const SocketAddress &addr)
Bind the DatagramSocket to the specified local address.
Definition: Socket.h:1025
bool getTcpNoDelay()
Definition: Socket.h:515
void connect(const Inet4Address &addr, int port)
Connect to a given remote host address and port.
Definition: Socket.h:599
void close()
Definition: Socket.cc:135
size_t recv(void *buf, size_t len, int flags=0)
Receive data on a socket.
Definition: Socket.cc:684
int _socktype
Definition: Socket.h:383
int getInQueueSize() const
Definition: Socket.h:569
int getFd() const
Fetch the file descriptor associate with this socket.
Definition: Socket.h:617
void connect(const std::string &dest, int port)
Definition: Socket.cc:146
void joinGroup(Inet4Address groupAddr)
Join a multicast group on all interfaces.
Definition: Socket.cc:1060
bool _reuseaddr
Definition: Socket.h:388
void leaveGroup(Inet4Address groupAddr, const Inet4NetworkInterface &iface)
Leave a multicast group on a given interface.
Definition: Socket.h:1306
void getRemoteAddr()
Do system call to determine address of remote end.
Definition: Socket.cc:414
void setInterface(Inet4Address maddr, const Inet4NetworkInterface &iaddr)
~ServerSocket()
Destructor.
Definition: Socket.h:785
void setReceiveBufferSize(int size)
Definition: Socket.h:1126
const SocketAddress & getLocalSocketAddress() const
Get local address of this socket.
Definition: Socket.h:1099
Definition: Inet4NetworkInterface.h:35
void joinGroup(Inet4Address groupAddr)
Join a multicast group on all interfaces.
Definition: Socket.h:1279
size_t send(const void *buf, size_t len, int flags=0)
Definition: Socket.h:1074
void setSendBufferSize(int size)
Definition: Socket.h:665
int getReceiveBufferSize()
Definition: Socket.cc:917
void joinGroup(Inet4Address groupAddr, const Inet4NetworkInterface &iface)
Join a multicast group on a given interface.
Definition: Socket.h:1292
void setTimeToLive(int val)
Definition: Socket.cc:1277
void setKeepAlive(bool val)
Set or unset the SO_KEEPALIVE socket option.
Definition: Socket.h:523
void leaveGroup(Inet4Address groupAddr)
Leave a multicast group on all interfaces.
Definition: Socket.cc:1141
int getLocalPort() const
Definition: Socket.h:839
bool getKeepAlive() const
Get the current value of the SO_KEEPALIVE socket option.
Definition: Socket.cc:985
void setBroadcastEnable(bool val)
Enable or disable SO_BROADCAST.
Definition: Socket.cc:1324
void setPktInfo(bool val)
Control whether a IP_PKTINFO ancillary message is received with each datagram.
Definition: Socket.cc:1435
int len
Definition: sing.cc:934
void setTimeout(int val)
Set the timeout for receive(), recv(), and recvfrom() methods.
Definition: Socket.h:970
SocketImpl _impl
Definition: Socket.h:706
bool getMulticastLoop() const
Definition: Socket.h:1338
int fd
Definition: twod.c:56
void connect(const std::string &host, int port)
Connect to a given remote host and port.
Definition: Socket.h:590
const SocketAddress & getRemoteSocketAddress() const
Get remote address of this socket.
Definition: Socket.h:677
int getRemotePort() const
Get remote port number of this socket.
Definition: Socket.h:684
std::list< Inet4NetworkInterface > getInterfaces() const
Definition: Socket.cc:1388
Definition: IOException.h:37
int getLocalPort() const
Get local port number of this socket.
Definition: Socket.h:116
bool isNonBlocking() const
Definition: Socket.h:505
int getReceiveBufferSize()
Definition: Socket.h:817
virtual ~DatagramSocket()
MulticastSocket is derived from DatagramSocket, so we provide a virtual destructor.
Definition: Socket.h:954
int getRemotePort() const
Get remote port number of this socket.
Definition: Socket.h:132
int getDomain() const
Get the domain of this socket: AF_UNIX, AF_INET, etc, from sys/socket.h.
Definition: Socket.h:101
SocketAddress * _remoteaddr
Definition: Socket.h:385
SocketAddress * _localaddr
Definition: Socket.h:384
int _fd
Definition: Socket.h:386
bool isNonBlocking() const
Definition: Socket.cc:463
Inet4NetworkInterface getInterface() const
Definition: Socket.cc:1228
Ancillary information that can be determined about an incoming UDP packet.
Definition: Inet4PacketInfo.h:39
static n_u::SerialPort port
Definition: sing.cc:68
int getSendBufferSize()
Definition: Socket.h:669
A stream (TCP) socket.
Definition: Socket.h:430
size_t recv(void *buf, size_t len, int flags=0)
Definition: Socket.h:619
virtual int getPort() const =0
return the port number of this address, or -1 if there is no associated port number, e.g.
size_t send(const void *buf, size_t len, int flags=MSG_NOSIGNAL)
send data on socket, see man page for send system function.
Definition: Socket.h:630
std::list< Inet4NetworkInterface > getInterfaces() const
Return the IP addresses of all my network interfaces.
Definition: Socket.h:1385
int getTimeout() const
Definition: Socket.cc:130
void leaveGroup(Inet4Address groupAddr)
Leave a multicast group on all interfaces.
Definition: Socket.h:1299
int getDomain() const
Definition: Socket.h:703
void setTimeToLive(int val)
Definition: Socket.h:1312
~Socket()
Definition: Socket.h:471
void setNonBlocking(bool val)
Do fcntl system call to set O_NONBLOCK file descriptor flag on the socket.
Definition: Socket.h:1116
const SocketAddress & getLocalSocketAddress() const
Get local address of this socket.
Definition: Socket.h:691
int getReceiveBufferSize()
Definition: Socket.h:1131
void setInterface(Inet4Address maddr, const Inet4NetworkInterface &iface)
Set the interface for a given multicast address.
Definition: Socket.h:1355
void sendall(const void *buf, size_t len, int flags=0)
send all data in buffer on socket, repeating send() as necessary, until all data is sent (or an excep...
Definition: Socket.cc:891
SocketImpl(int domain, int type)
Definition: Socket.cc:62
const SocketAddress & getLocalSocketAddress() const
Get local socket address of this socket.
Definition: Socket.h:108
struct timespec _timeout
Definition: Socket.h:390
Definition: UnknownHostException.h:35
void connect(const SocketAddress &addr)
Connect to a given remote socket address.
Definition: Socket.h:608
void setNonBlocking(bool val)
Do fcntl system call to set O_NONBLOCK file descriptor flag on the socket.
Definition: Socket.h:500
void send(const DatagramPacketBase &packet, int flags=0)
Definition: Socket.cc:817
Support for IP version 4 host address.
Definition: Inet4Address.h:46
SocketImpl _impl
Definition: Socket.h:854
void setNonBlocking(bool val)
Definition: Socket.h:843
Inet4NetworkInterface findInterface(const Inet4Address &iaddr) const
Definition: Socket.h:1376
bool getBroadcastEnable() const
Definition: Socket.h:1155
int _sockdomain
Definition: Socket.h:382
void setReceiveBufferSize(int size)
Definition: Socket.h:656
void setTcpNoDelay(bool val)
Set the TCP_NODELAY (man 7 tcp) option on the socket.
Definition: Socket.cc:949
bool getKeepAlive()
Get the current value of the SO_KEEPALIVE socket option.
Definition: Socket.h:531
int getTimeToLive() const
Definition: Socket.cc:1287
void setKeepAliveIdleSecs(int val)
Set the number of seconds a connection needs to be idle before TCP begins sending out keep-alive prob...
Definition: Socket.h:554
size_t send(const struct iovec *iov, int iovcnt, int flags=MSG_NOSIGNAL)
Definition: Socket.h:635
SocketImpl _impl
Definition: Socket.h:1173
void close()
Definition: Socket.h:474
void sendall(const void *buf, size_t len, int flags=MSG_NOSIGNAL)
send all data in buffer on socket, repeating send() as necessary, until all data is sent (or an excep...
Definition: Socket.h:651