| 1 |
// SPDX-License-Identifier: MPL-2.0 |
| 2 |
// (c) Hare authors <https://harelang.org> |
| 3 |
|
| 4 |
use io; |
| 5 |
use net; |
| 6 |
use rt; |
| 7 |
|
| 8 |
// Adds a list of file descriptors to the ancillary data for a sendmsg |
| 9 |
// operation. |
| 10 |
export fn addfiles(buf: *net::msghdr, files: io::file...) (void | nomem) = { |
| 11 |
const msgsz = size(io::file) * len(files); |
| 12 |
let buf = net::addcontrol(buf, msgsz, rt::SOL_SOCKET, rt::SCM_RIGHTS)?; |
| 13 |
let buf = buf: *[*]io::file; |
| 14 |
buf[..len(files)] = files[..]; |
| 15 |
}; |
| 16 |
|
| 17 |
// Prepares an ancillary data buffer to receive files during a recvmsg |
| 18 |
// operation. |
| 19 |
export fn allocfiles(buf: *net::msghdr, nfile: size) (void | nomem) = { |
| 20 |
const msgsz = size(io::file) * nfile; |
| 21 |
net::addcontrol(buf, msgsz, rt::SOL_SOCKET, rt::SCM_RIGHTS)?; |
| 22 |
}; |
| 23 |
|
| 24 |
// Receives files from an ancillary data buffer which was previously prepared |
| 25 |
// with [[allocfiles]]. |
| 26 |
export fn recvfiles(buf: *net::msghdr, nfile: size) []io::file = { |
| 27 |
match (net::getcontrol(buf, |
| 28 |
nfile * size(io::file), |
| 29 |
rt::SOL_SOCKET, rt::SCM_RIGHTS)) { |
| 30 |
case let buf: []u8 => |
| 31 |
return (buf: *[*]io::file)[..nfile]; |
| 32 |
case void => |
| 33 |
return []; |
| 34 |
}; |
| 35 |
}; |