|
| 1 | +#include "sys.h" |
| 2 | +#include "handle.h" |
| 3 | +#include "endpoint.h" |
| 4 | +#include <openkal/datagram.h> |
| 5 | + |
| 6 | +// openkal.datagram upon the kernel's socket calls. |
| 7 | +// |
| 8 | +// A DATAGRAM IS NOT PACKED AS A kal_stream, and the handle type is its own for |
| 9 | +// that reason: kal_stream_read reports a count and not a boundary, so reading a |
| 10 | +// datagram through it would lose the property that distinguishes this interface. |
| 11 | +// The packing is the same, the type is not, and the type is what prevents the |
| 12 | +// mistake. |
| 13 | + |
| 14 | +namespace { |
| 15 | + |
| 16 | +int fd_of(kal_datagram d) { return okl::unpack(d.h); } |
| 17 | + |
| 18 | +} // namespace |
| 19 | + |
| 20 | +extern "C" { |
| 21 | + |
| 22 | +int kal_datagram_open(const kal_endpoint* local, kal_datagram* out) { |
| 23 | + if (out == nullptr) return kal_err_invalid; |
| 24 | + |
| 25 | + // A null local endpoint asks for one that may send and whose receiving |
| 26 | + // address is unspecified. IPv4 is chosen for it, because a family must be |
| 27 | + // named at the point the socket is made and this is the one every |
| 28 | + // environment that has a network at all provides. |
| 29 | + okl_long family = okl::af_inet; |
| 30 | + if (local != nullptr) { |
| 31 | + family = okl::family_of(*local); |
| 32 | + if (family < 0) return kal_err_invalid; |
| 33 | + } |
| 34 | + |
| 35 | + const okl_long fd = okl::sys(okl::nr_socket, family, |
| 36 | + okl::sock_dgram | okl::sock_cloexec, |
| 37 | + okl::ipproto_udp); |
| 38 | + if (okl::failed(fd)) return okl::translate(fd); |
| 39 | + |
| 40 | + if (local != nullptr) { |
| 41 | + okl::ksockaddr_storage ss{}; |
| 42 | + okl_long len = 0; |
| 43 | + if (const int rc = okl::to_kernel(*local, ss, len); rc != kal_ok) { |
| 44 | + okl::sys(okl::nr_close, fd); |
| 45 | + return rc; |
| 46 | + } |
| 47 | + if (const okl_long r = okl::sys(okl::nr_bind, fd, |
| 48 | + reinterpret_cast<okl_long>(&ss), len); |
| 49 | + okl::failed(r)) { |
| 50 | + okl::sys(okl::nr_close, fd); |
| 51 | + return okl::translate(r); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + out->h = okl::pack(static_cast<int>(fd)); |
| 56 | + if (out->h == 0) { okl::sys(okl::nr_close, fd); return kal_err_no_memory; } |
| 57 | + return kal_ok; |
| 58 | +} |
| 59 | + |
| 60 | +int kal_datagram_local(kal_datagram d, kal_endpoint* out) { |
| 61 | + if (out == nullptr) return kal_err_invalid; |
| 62 | + const int fd = fd_of(d); |
| 63 | + if (fd < 0) return kal_err_invalid; |
| 64 | + |
| 65 | + okl::ksockaddr_storage ss{}; |
| 66 | + okl_long len = static_cast<okl_long>(sizeof ss); |
| 67 | + const okl_long r = okl::sys(okl::nr_getsockname, fd, |
| 68 | + reinterpret_cast<okl_long>(&ss), |
| 69 | + reinterpret_cast<okl_long>(&len)); |
| 70 | + if (okl::failed(r)) return okl::translate(r); |
| 71 | + return okl::from_kernel(ss, *out); |
| 72 | +} |
| 73 | + |
| 74 | +kal_io_result kal_datagram_send_to(kal_datagram d, const void* buf, kal_uintptr len, |
| 75 | + const kal_endpoint* to) { |
| 76 | + const int fd = fd_of(d); |
| 77 | + if (fd < 0 || to == nullptr) return { 0, kal_err_invalid }; |
| 78 | + |
| 79 | + okl::ksockaddr_storage ss{}; |
| 80 | + okl_long addrlen = 0; |
| 81 | + if (const int rc = okl::to_kernel(*to, ss, addrlen); rc != kal_ok) |
| 82 | + return { 0, rc }; |
| 83 | + |
| 84 | + for (;;) { |
| 85 | + const okl_long r = okl::sys(okl::nr_sendto, fd, |
| 86 | + reinterpret_cast<okl_long>(buf), |
| 87 | + static_cast<okl_long>(len), 0, |
| 88 | + reinterpret_cast<okl_long>(&ss), addrlen); |
| 89 | + if (okl::interrupted(r)) continue; |
| 90 | + if (okl::failed(r)) return { 0, okl::translate(r) }; |
| 91 | + |
| 92 | + // A MESSAGE IS SENT WHOLE OR NOT AT ALL, which is what this interface |
| 93 | + // states. The kernel reports a count anyway; a count short of the length |
| 94 | + // would mean the medium had split the message, which for a datagram |
| 95 | + // socket it does not do. Reporting the short count as success would give |
| 96 | + // a caller a partial send this interface says cannot occur, so it is |
| 97 | + // reported as a failure of the medium instead. |
| 98 | + const kal_uintptr n = static_cast<kal_uintptr>(r); |
| 99 | + return { n, n == len ? kal_ok : kal_err_io }; |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +kal_io_result kal_datagram_recv_from(kal_datagram d, void* buf, kal_uintptr len, |
| 104 | + kal_endpoint* from) { |
| 105 | + const int fd = fd_of(d); |
| 106 | + if (fd < 0) return { 0, kal_err_invalid }; |
| 107 | + |
| 108 | + okl::ksockaddr_storage ss{}; |
| 109 | + okl_long addrlen = static_cast<okl_long>(sizeof ss); |
| 110 | + |
| 111 | + for (;;) { |
| 112 | + const okl_long r = okl::sys(okl::nr_recvfrom, fd, |
| 113 | + reinterpret_cast<okl_long>(buf), |
| 114 | + static_cast<okl_long>(len), 0, |
| 115 | + reinterpret_cast<okl_long>(&ss), |
| 116 | + reinterpret_cast<okl_long>(&addrlen)); |
| 117 | + if (okl::interrupted(r)) continue; |
| 118 | + if (okl::failed(r)) return { 0, okl::translate(r) }; |
| 119 | + |
| 120 | + // THE COUNT REPORTED IS WHAT WAS PLACED IN THE BUFFER, not what was |
| 121 | + // sent. Without MSG_TRUNC the kernel already reports the former, which |
| 122 | + // is what this interface requires: a caller that trusted the larger |
| 123 | + // number would read beyond its own buffer. |
| 124 | + if (from != nullptr) { |
| 125 | + // A sender whose family this implementation does not know leaves the |
| 126 | + // endpoint zeroed rather than partly filled. The transfer still |
| 127 | + // happened and is reported; what is unknown is who sent it. |
| 128 | + if (okl::from_kernel(ss, *from) != kal_ok) { |
| 129 | + for (auto& b : from->addr) b = 0; |
| 130 | + from->addr_len = 0; |
| 131 | + from->port = 0; |
| 132 | + } |
| 133 | + } |
| 134 | + return { static_cast<kal_uintptr>(r), kal_ok }; |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +void kal_datagram_close(kal_datagram d) { |
| 139 | + const int fd = fd_of(d); |
| 140 | + if (fd < 0) return; |
| 141 | + okl::sys(okl::nr_close, fd); |
| 142 | + okl::retire(d.h); |
| 143 | +} |
| 144 | + |
| 145 | +// Broadcast is not claimed. The kernel provides it only after SO_BROADCAST has |
| 146 | +// been set, and this interface has no operation that would set it; a word |
| 147 | +// claiming a facility no operation reaches is the disagreement clause 6.2 exists |
| 148 | +// to prevent. |
| 149 | +const kal_uintptr kal_datagram_props = KAL_DGRAM_PROP_IPV6; |
| 150 | + |
| 151 | +} // extern "C" |
0 commit comments