4#include <cosmos/error/ApiError.hxx>
5#include <cosmos/error/RangeError.hxx>
6#include <cosmos/error/RuntimeError.hxx>
7#include <cosmos/fs/FileDescriptor.hxx>
8#include <cosmos/utils.hxx>
18using cosmos::to_integral;
25 FileDescriptor sock,
const OptLevel lvl,
26 const OptName name, T &out) {
27 static_assert(std::is_pointer<T>::value !=
true,
28 "this doesn't work for pointer types");
29 socklen_t len =
sizeof(T);
31 const auto res = ::getsockopt(
32 to_integral(sock.raw()),
39 if (cosmos::get_errno() == cosmos::Errno::RANGE) {
46 cosmos_throw (RangeError(
"getsockopt", len));
48 cosmos_throw (ApiError(
"getsockopt()"));
58 FileDescriptor sock,
const OptLevel lvl,
61 const auto len = getsockopt(sock, lvl, name, out);
65 if (len !=
sizeof(T)) {
66 cosmos_throw (RuntimeError(
"short getsockopt read"));
75 FileDescriptor sock,
const OptLevel lvl,
76 const OptName name, T *ptr, socklen_t ptr_len) {
78 const auto res = ::getsockopt(
79 to_integral(sock.raw()),
86 cosmos_throw (ApiError(
"getsockopt()"));
95 FileDescriptor sock,
const OptLevel lvl,
96 const OptName name,
const T val) {
97 static_assert(std::is_pointer<T>::value !=
true,
98 "this doesn't work for pointer types");
99 const auto res = ::setsockopt(
100 to_integral(sock.raw()),
104 socklen_t{
sizeof(T)});
107 cosmos_throw (ApiError(
"setsockopt()"));
112 template <
typename T>
114 FileDescriptor sock,
const OptLevel lvl,
115 const OptName name,
const T *ptr, socklen_t len) {
116 const auto res = ::setsockopt(
117 to_integral(sock.raw()),
124 cosmos_throw (ApiError(
"setsockopt()"));