+
+void send_ktls_close(int sock)
+{
+ uint8_t record_type = 21; // Alert.
+ uint8_t body[] = {
+ 1, // Warning level (but still fatal!).
+ 0, // close_notify.
+ };
+
+ int cmsg_len = sizeof(record_type);
+ char buf[CMSG_SPACE(cmsg_len)];
+
+ msghdr msg = {0};
+ msg.msg_control = buf;
+ msg.msg_controllen = sizeof(buf);
+ cmsghdr *cmsg = CMSG_FIRSTHDR(&msg);
+ cmsg->cmsg_level = SOL_TLS;
+ cmsg->cmsg_type = TLS_SET_RECORD_TYPE;
+ cmsg->cmsg_len = CMSG_LEN(cmsg_len);
+ *CMSG_DATA(cmsg) = record_type;
+ msg.msg_controllen = cmsg->cmsg_len;
+
+ iovec msg_iov;
+ msg_iov.iov_base = body;
+ msg_iov.iov_len = sizeof(body);
+ msg.msg_iov = &msg_iov;
+ msg.msg_iovlen = 1;
+
+ int err;
+ do {
+ err = sendmsg(sock, &msg, 0);
+ } while (err == -1 && errno == EINTR); // Ignore all other errors.
+}
+
+} // namespace