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
TCPSocket::accept
TCPSocket accept()
Accept a new incoming connection.
Definition: socket.cc:151
std::string
TCPSocket
A wrapper around TCP sockets.
Definition: socket.hh:88
file_descriptor.hh
UDPSocket::received_datagram::source_address
Address source_address
Address from which this datagram was received.
Definition: socket.hh:63
UDPSocket::UDPSocket
UDPSocket()
Default: construct an unbound, unconnected UDP socket.
Definition: socket.hh:59
BufferViewList
A non-owning temporary view (similar to std::string_view) of a discontiguous string.
Definition: buffer.hh:97
UDPSocket::received_datagram
Returned by UDPSocket::recv; carries received data and information about the sender.
Definition: socket.hh:62
FileDescriptor
A reference-counted handle to a file descriptor.
Definition: file_descriptor.hh:12
UDPSocket::sendto
void sendto(const Address &destination, const BufferViewList &payload)
Send a datagram to specified Address.
Definition: socket.cc:134
std::function
Address
Wrapper around IPv4 addresses and DNS operations.
Definition: address.hh:13
LocalStreamSocket::LocalStreamSocket
LocalStreamSocket(FileDescriptor &&fd)
Construct from a file descriptor.
Definition: socket.hh:116
UDPSocket::UDPSocket
UDPSocket(FileDescriptor &&fd)
Construct from FileDescriptor (used by TCPOverUDPSocketAdapter)
Definition: socket.hh:55
UDPSocket::received_datagram::payload
std::string payload
UDP datagram payload.
Definition: socket.hh:64
LocalStreamSocket
A wrapper around Unix-domain stream sockets.
Definition: socket.hh:113
Socket::bind
void bind(const Address &address)
Bind a socket to a specified address with bind(2), usually for listen/accept.
Definition: socket.cc:61
TCPSocket::listen
void listen(const int backlog=16)
Mark a socket as listening for incoming connections.
Definition: socket.cc:146
TCPSocket::TCPSocket
TCPSocket(FileDescriptor &&fd)
Construct from FileDescriptor (used by accept())
Definition: socket.hh:92
Socket::get_address
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::connect
void connect(const Address &address)
Connect a socket to a specified peer address with connect(2).
Definition: socket.cc:65
UDPSocket::recv
received_datagram recv(const size_t mtu=65536)
Receive a datagram and the Address of its sender.
Socket::set_reuseaddr
void set_reuseaddr()
Allow local address to be reused sooner via SO_REUSEADDR.
Definition: socket.cc:168
UDPSocket
A wrapper around UDP sockets.
Definition: socket.hh:51
UDPSocket::send
void send(const BufferViewList &payload)
Send datagram to the socket's connected address (must call connect() first)
Definition: socket.cc:139
std
Socket::local_address
Address local_address() const
Get local address of socket with getsockname(2).
Definition: socket.cc:54
Socket
Base class for network sockets (TCP, UDP, etc.)
Definition: socket.hh:14
Socket::setsockopt
void setsockopt(const int level, const int option, const option_type &option_value)
Wrapper around setsockopt(2).
Definition: socket.cc:162
address.hh
Socket::shutdown
void shutdown(const int how)
Shut down a socket via shutdown(2).
Definition: socket.cc:69
Socket::Socket
Socket(const int domain, const int type)
Construct via socket(2).
Definition: socket.cc:14
Socket::peer_address
Address peer_address() const
Get peer address of socket with getpeername(2).
Definition: socket.cc:57
TCPSocket::TCPSocket
TCPSocket()
Default: construct an unbound, unconnected TCP socket.
Definition: socket.hh:96