Sponge
CS144's user-space TCP library
socket.hh
Go to the documentation of this file.
1 #ifndef SPONGE_LIBSPONGE_SOCKET_HH
2 #define SPONGE_LIBSPONGE_SOCKET_HH
3 
4 #include "address.hh"
5 #include "file_descriptor.hh"
6 
7 #include <cstdint>
8 #include <functional>
9 #include <string>
10 #include <sys/socket.h>
11 
14 class Socket : public FileDescriptor {
15  private:
17  Address get_address(const std::string &name_of_function,
18  const std::function<int(int, sockaddr *, socklen_t *)> &function) const;
19 
20  protected:
22  Socket(const int domain, const int type);
23 
25  Socket(FileDescriptor &&fd, const int domain, const int type);
26 
28  template <typename option_type>
29  void setsockopt(const int level, const int option, const option_type &option_value);
30 
31  public:
33  void bind(const Address &address);
34 
36  void connect(const Address &address);
37 
39  void shutdown(const int how);
40 
42  Address local_address() const;
44  Address peer_address() const;
45 
47  void set_reuseaddr();
48 };
49 
51 class UDPSocket : public Socket {
52  protected:
55  explicit UDPSocket(FileDescriptor &&fd) : Socket(std::move(fd), AF_INET, SOCK_DGRAM) {}
56 
57  public:
59  UDPSocket() : Socket(AF_INET, SOCK_DGRAM) {}
60 
65  };
66 
68  received_datagram recv(const size_t mtu = 65536);
69 
71  void recv(received_datagram &datagram, const size_t mtu = 65536);
72 
74  void sendto(const Address &destination, const BufferViewList &payload);
75 
77  void send(const BufferViewList &payload);
78 };
79 
86 
88 class TCPSocket : public Socket {
89  private:
92  explicit TCPSocket(FileDescriptor &&fd) : Socket(std::move(fd), AF_INET, SOCK_STREAM) {}
93 
94  public:
96  TCPSocket() : Socket(AF_INET, SOCK_STREAM) {}
97 
99  void listen(const int backlog = 16);
100 
102  TCPSocket accept();
103 };
104 
111 
113 class LocalStreamSocket : public Socket {
114  public:
116  explicit LocalStreamSocket(FileDescriptor &&fd) : Socket(std::move(fd), AF_UNIX, SOCK_STREAM) {}
117 };
118 
123 
124 #endif // SPONGE_LIBSPONGE_SOCKET_HH
Wrapper around IPv4 addresses and DNS operations.
Definition: address.hh:13
A non-owning temporary view (similar to std::string_view) of a discontiguous string.
Definition: buffer.hh:98
A reference-counted handle to a file descriptor.
A wrapper around Unix-domain stream sockets.
Definition: socket.hh:113
LocalStreamSocket(FileDescriptor &&fd)
Construct from a file descriptor.
Definition: socket.hh:116
Base class for network sockets (TCP, UDP, etc.)
Definition: socket.hh:14
void connect(const Address &address)
Connect a socket to a specified peer address with connect(2).
Definition: socket.cc:65
void shutdown(const int how)
Shut down a socket via shutdown(2).
Definition: socket.cc:69
Address get_address(const std::string &name_of_function, const std::function< int(int, sockaddr *, socklen_t *)> &function) const
Get the local or peer address the socket is connected to.
Definition: socket.cc:43
Socket(const int domain, const int type)
Construct via socket(2).
Definition: socket.cc:14
void setsockopt(const int level, const int option, const option_type &option_value)
Wrapper around setsockopt(2).
Definition: socket.cc:162
void bind(const Address &address)
Bind a socket to a specified address with bind(2), usually for listen/accept.
Definition: socket.cc:61
Address local_address() const
Get local address of socket with getsockname(2).
Definition: socket.cc:54
Address peer_address() const
Get peer address of socket with getpeername(2).
Definition: socket.cc:57
void set_reuseaddr()
Allow local address to be reused sooner via SO_REUSEADDR.
Definition: socket.cc:168
A wrapper around TCP sockets.
Definition: socket.hh:88
TCPSocket accept()
Accept a new incoming connection.
Definition: socket.cc:151
TCPSocket()
Default: construct an unbound, unconnected TCP socket.
Definition: socket.hh:96
void listen(const int backlog=16)
Mark a socket as listening for incoming connections.
Definition: socket.cc:146
TCPSocket(FileDescriptor &&fd)
Construct from FileDescriptor (used by accept())
Definition: socket.hh:92
A wrapper around UDP sockets.
Definition: socket.hh:51
received_datagram recv(const size_t mtu=65536)
Receive a datagram and the Address of its sender.
UDPSocket()
Default: construct an unbound, unconnected UDP socket.
Definition: socket.hh:59
Address source_address
Address from which this datagram was received.
Definition: socket.hh:63
void send(const BufferViewList &payload)
Send datagram to the socket's connected address (must call connect() first)
Definition: socket.cc:139
std::string payload
UDP datagram payload.
Definition: socket.hh:64
void sendto(const Address &destination, const BufferViewList &payload)
Send a datagram to specified Address.
Definition: socket.cc:134
UDPSocket(FileDescriptor &&fd)
Construct from FileDescriptor (used by TCPOverUDPSocketAdapter)
Definition: socket.hh:55
void recv(received_datagram &datagram, const size_t mtu=65536)
Receive a datagram and the Address of its sender (caller can allocate storage)
Returned by UDPSocket::recv; carries received data and information about the sender.
Definition: socket.hh:62