Coda Distributed File System
Loading...
Searching...
No Matches
ctp.h
Go to the documentation of this file.
1/* BLURB lgpl
2
3 Coda File System
4 Release 8
5
6 Copyright (c) 2026 Carnegie Mellon University
7 Additional copyrights listed below
8
9This code is distributed "AS IS" without warranty of any kind under
10the terms of the GNU Library General Public Licence Version 2, as
11shown in the file LICENSE. The technical and financial contributors to
12Coda are listed in the file CREDITS.
13
14 Additional copyrights
15
16#*/
17
18/* Pure wire protocol for codatunneld. No libuv, no gnutls, so this can be
19 * included (and unit-tested) without pulling the tunnel dependencies into
20 * a build. The same header wraps both the daemon-to-daemon TLS records and
21 * the Unix-socket ("vside") exchanges between an app and its codatunneld.
22 *
23 * The `opcode` word is the former `is_init0` hint, reused in place. The byte
24 * layout of ctp_t is unchanged, so sizeof(ctp_t) and the "magic01" magic word
25 * are preserved for backward compatibility with peers that only ever wrote the
26 * word to 0 (a plain packet) or 1 (INIT0).
27 */
28
29#ifndef _CODATUNNEL_CTP_H_
30#define _CODATUNNEL_CTP_H_
31
32#include <stdint.h>
33#include <sys/socket.h>
34#include <sys/types.h>
35
36#define CT_MAGIC "magic01"
37#define CT_MAGICSZ 8
38
40{
41 CT_PKT = 0, /* encapsulated RPC2 packet (today's plain packet) */
42 CT_INIT0 = 1, /* peername hint (NUL-padded body) */
43 CT_FILEREG = 2, /* app->daemon: register a local transfer */
44 CT_FILEUNREG = 3, /* app->daemon: cancel/release a registration */
45 CT_FILEDONE = 4, /* daemon->app: terminal state of a registration */
47 5, /* daemon->daemon: sink (driver end) is open, proceed */
48 CT_TRANSFER_DATA = 6, /* daemon->daemon: file bytes @{cookie,offset} */
49 CT_TRANSFER_EOF = 7, /* daemon->daemon: no more bytes for cookie */
50 CT_TRANSFER_ERROR = 8, /* daemon->daemon: local open/xfer failed */
52 9, /* daemon->daemon: sink asks the source for bytes @{cookie,offset} */
53};
54
55/* role values for ct_filereg.role */
56#define CT_SOURCE 0
57#define CT_SINK 1
58
59/*
60 * ct_filereg.flags bits. The "driver" bit drives the CT_TRANSFER_READY start
61 * rule: a single daemon can only see its own FILEREG, so it cannot tell which
62 * end registered first. The RPC-server side (CheckSE) is always the driver
63 * end — it passes the client's cookie back through *cookie, which is the
64 * "non-zero in -> use as-is (server side)" convention, so the app sets this
65 * bit from that single fact. A SOURCE that is "driver" (a fetch) starts
66 * streaming immediately (the sink pre-registered); a SOURCE that is not
67 * "driver" (a store) waits for the sink's CT_TRANSFER_READY before streaming.
68 */
69#define CT_FILREG_DRIVER 0x1
70
71/* CT_CHUNKMAX, CT_MAX_RECORD and the TLS payload ceiling live further down,
72 * after ctp_t and ct_transfer_data are defined (the payload size is derived
73 * from sizeof() of both). */
74
75/* Status values carried in ct_filedone.status and ct_transfer_error.status,
76 * in the SFTP error space (0 == success). The daemon maps its local errno
77 * into these; the app's SE layer maps them back to SE status at finalize. */
78#define CT_STATUS_SUCCESS 0
79#define CT_STATUS_NOENT 1 /* file does not exist */
80#define CT_STATUS_ACCES 2 /* permission denied */
81#define CT_STATUS_NOSPC 3 /* disk / quota full */
82#define CT_STATUS_IOERR 4 /* any other local I/O or resource error */
83#define CT_STATUS_TIMEOUT 5 /* channel death or cancellation */
84
85typedef struct codatunnel_packet {
86 char magic[8];
88 uint32_t opcode; /* was is_init0; values from enum ct_opcode */
93
94/* The layout must stay exactly: magic + four 32-bit words + the address.
95 Written against the field widths (not a hard-coded total) because
96 sizeof(struct sockaddr_storage) is platform dependent. The typedef form is
97 used (rather than a C11 _Static_assert) so this compiles under C99 too. */
98typedef char ctp_size_invariant[(sizeof(ctp_t) ==
99 (size_t)CT_MAGICSZ + 4 * sizeof(uint32_t) +
100 sizeof(struct sockaddr_storage)) ?
101 1 :
102 -1];
103
104typedef struct ct_filedone {
105 uint64_t cookie;
106 uint64_t nbytes;
107 uint32_t status; /* SFTP error space; 0 == success */
110
111typedef struct ct_filereg {
112 uint64_t cookie;
113 uint32_t role; /* CT_SOURCE or CT_SINK */
114 uint32_t flags; /* CT_FILREG_* bits */
117 uint64_t offset;
118 uint64_t length; /* total file length / expected sink bytes */
119 struct sockaddr_storage peer; /* which tunnel channel this rides */
121
122/*
123 * The daemon-to-daemon data-path payloads, as the file-transfer opcodes
124 * arrive in the body region of a ctp_t envelope over the TLS channel
125 * (opcode == 5..8). The body itself is written in network byte order there
126 * (the ctp_t header does that already); the structs here are the pure byte
127 * layout. Only the ctp_t envelope is the shared framing; these ride in its
128 * body region. CT_TRANSFER_DATA is followed (in the same record) by up to
129 * CT_CHUNKMAX payload bytes, located at offset sizeof(ct_transfer_data).
130 */
134
135typedef struct ct_transfer_data {
136 uint64_t cookie;
137 uint64_t offset; /* absolute file offset of the payload that follows */
139
140typedef struct ct_transfer_eof {
141 uint64_t cookie;
143
144typedef struct ct_transfer_error {
145 uint64_t cookie;
146 uint32_t status; /* SFTP error space; 0 == none */
149
150typedef struct ct_transfer_request {
151 uint64_t cookie;
152 uint64_t offset; /* absolute file offset of the requested bytes */
153 uint64_t len; /* how many bytes the sink wants (<= CT_CHUNKMAX) */
155
156/* A TLS record's content payload size is negotiated per session and, in
157 * practice, ranges from 512 up to 16384 bytes depending on configuration and
158 * TLS extensions. A full-chunk CT_TRANSFER_DATA record is the ctp_t header
159 * (whose addr field is a whole sockaddr_storage on every platform we build
160 * for) + the ct_transfer_data header + the payload. Size the payload to the
161 * largest record we could possibly expect (16384) so the scratch and
162 * reassembly buffers cover any record the daemon will send or accept. Each
163 * daemon actually reads/sends/accepts only what its own session negotiated
164 * (gnutls_record_get_max_size()), which is <= this, so every record stays
165 * inside a single TLS fragment and gnutls never splits it. */
166#define CT_TLSMAXPAYLOAD 16384
167#define CT_CHUNKMAX \
168 (CT_TLSMAXPAYLOAD - (size_t)sizeof(ctp_t) - sizeof(ct_transfer_data))
169
170/* Largest record a daemon will accept on a channel: a full-chunk
171 * CT_TRANSFER_DATA. Everything the daemon decrypts today must fit in this;
172 * bigger is a protocol violation (the connection is dropped). */
173#define CT_MAX_RECORD (sizeof(ctp_t) + sizeof(ct_transfer_data) + CT_CHUNKMAX)
174
175#endif /* _CODATUNNEL_CTP_H_ */
unsigned short uint16_t
Definition coda.h:103
unsigned int uint32_t
Definition coda.h:105
ct_opcode
Definition ctp.h:40
@ CT_FILEDONE
Definition ctp.h:45
@ CT_TRANSFER_REQUEST
Definition ctp.h:51
@ CT_FILEREG
Definition ctp.h:43
@ CT_TRANSFER_ERROR
Definition ctp.h:50
@ CT_TRANSFER_READY
Definition ctp.h:46
@ CT_TRANSFER_EOF
Definition ctp.h:49
@ CT_INIT0
Definition ctp.h:42
@ CT_PKT
Definition ctp.h:41
@ CT_FILEUNREG
Definition ctp.h:44
@ CT_TRANSFER_DATA
Definition ctp.h:48
#define CT_MAGICSZ
Definition ctp.h:37
char ctp_size_invariant[(sizeof(ctp_t)==(size_t) CT_MAGICSZ+4 *sizeof(uint32_t)+sizeof(struct sockaddr_storage)) ? 1 :-1]
Definition ctp.h:102
struct codatunnel_packet ctp_t
Definition ctp.h:85
struct sockaddr_storage addr
Definition ctp.h:91
char magic[8]
Definition ctp.h:86
uint32_t msglen
Definition ctp.h:89
uint32_t opcode
Definition ctp.h:88
uint32_t addrlen
Definition ctp.h:90
uint32_t is_retry
Definition ctp.h:87
Definition ctp.h:104
uint32_t pad
Definition ctp.h:108
uint64_t cookie
Definition ctp.h:105
uint64_t nbytes
Definition ctp.h:106
uint32_t status
Definition ctp.h:107
Definition ctp.h:111
uint64_t cookie
Definition ctp.h:112
uint16_t peerlen
Definition ctp.h:115
uint32_t role
Definition ctp.h:113
uint64_t length
Definition ctp.h:118
uint32_t flags
Definition ctp.h:114
uint64_t offset
Definition ctp.h:117
struct sockaddr_storage peer
Definition ctp.h:119
uint16_t pad
Definition ctp.h:116
Definition ctp.h:135
uint64_t offset
Definition ctp.h:137
uint64_t cookie
Definition ctp.h:136
Definition ctp.h:140
uint64_t cookie
Definition ctp.h:141
Definition ctp.h:144
uint32_t pad
Definition ctp.h:147
uint32_t status
Definition ctp.h:146
uint64_t cookie
Definition ctp.h:145
Definition ctp.h:131
uint64_t cookie
Definition ctp.h:132
Definition ctp.h:150
uint64_t offset
Definition ctp.h:152
uint64_t len
Definition ctp.h:153
uint64_t cookie
Definition ctp.h:151
Definition rpc2.private.h:63