|
Sponge
CS144's user-space TCP library
|
Go to the documentation of this file.
25 len =
sizeof(actual_value);
26 SystemCall(
"getsockopt", getsockopt(
fd_num(), SOL_SOCKET, SO_DOMAIN, &actual_value, &len));
27 if ((len !=
sizeof(actual_value)) or (actual_value != domain)) {
32 len =
sizeof(actual_value);
33 SystemCall(
"getsockopt", getsockopt(
fd_num(), SOL_SOCKET, SO_TYPE, &actual_value, &len));
34 if ((len !=
sizeof(actual_value)) or (actual_value != type)) {
44 const function<
int(
int, sockaddr *, socklen_t *)> &
function)
const {
46 socklen_t size =
sizeof(address);
50 return {address, size};
83 throw runtime_error(
"Socket::shutdown() called with invalid `how`");
91 datagram.payload.resize(mtu);
93 socklen_t fromlen =
sizeof(datagram_source_address);
98 fd_num(), datagram.payload.data(), datagram.payload.size(), MSG_TRUNC, datagram_source_address, &fromlen));
100 if (recv_len > ssize_t(mtu)) {
105 datagram.source_address = {datagram_source_address, fromlen};
106 datagram.payload.resize(recv_len);
110 received_datagram ret{{
nullptr, 0},
""};
116 const sockaddr *destination_address,
117 const socklen_t destination_address_len,
122 message.msg_name =
const_cast<sockaddr *
>(destination_address);
123 message.msg_namelen = destination_address_len;
124 message.msg_iov = iovecs.data();
125 message.msg_iovlen = iovecs.size();
127 const ssize_t bytes_sent =
SystemCall(
"sendmsg", ::sendmsg(fd_num, &message, 0));
129 if (
size_t(bytes_sent) != payload.
size()) {
130 throw runtime_error(
"datagram payload too big for sendmsg()");
161 template <
typename option_type>
TCPSocket accept()
Accept a new incoming connection.
A wrapper around TCP sockets.
SystemCall("socketpair", ::socketpair(AF_UNIX, SOCK_STREAM, 0, fds.data()))
int fd_num() const
underlying descriptor number
A non-owning temporary view (similar to std::string_view) of a discontiguous string.
Returned by UDPSocket::recv; carries received data and information about the sender.
A reference-counted handle to a file descriptor.
void sendto(const Address &destination, const BufferViewList &payload)
Send a datagram to specified Address.
Wrapper around sockaddr_storage.
void sendmsg_helper(const int fd_num, const sockaddr *destination_address, const socklen_t destination_address_len, const BufferViewList &payload)
Wrapper around IPv4 addresses and DNS operations.
void bind(const Address &address)
Bind a socket to a specified address with bind(2), usually for listen/accept.
void listen(const int backlog=16)
Mark a socket as listening for incoming connections.
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.
size_t size() const
Size of the string.
void connect(const Address &address)
Connect a socket to a specified peer address with connect(2).
received_datagram recv(const size_t mtu=65536)
Receive a datagram and the Address of its sender.
std::vector< iovec > as_iovecs() const
Convert to a vector of iovec structures.
FileDescriptor(std::shared_ptr< FDWrapper > other_shared_ptr)
Private constructor used by duplicate()
void set_reuseaddr()
Allow local address to be reused sooner via SO_REUSEADDR.
void send(const BufferViewList &payload)
Send datagram to the socket's connected address (must call connect() first)
Address local_address() const
Get local address of socket with getsockname(2).
void setsockopt(const int level, const int option, const option_type &option_value)
Wrapper around setsockopt(2).
socklen_t size() const
Size of the underlying address storage.
void shutdown(const int how)
Shut down a socket via shutdown(2).
Socket(const int domain, const int type)
Construct via socket(2).
Address peer_address() const
Get peer address of socket with getpeername(2).
TCPSocket()
Default: construct an unbound, unconnected TCP socket.