1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
// Copyright 2025 Dustin McAfee
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! VNC Remote Framebuffer (RFB) protocol constants and structures.
//!
//! This module provides the fundamental building blocks for VNC protocol communication,
//! including protocol version negotiation, message types, security handshakes, encodings,
//! and pixel format definitions. It implements the RFB protocol as specified in RFC 6143.
//!
//! # Protocol Overview
//!
//! The VNC RFB protocol operates in the following phases:
//! 1. **Protocol Version** - Server and client agree on protocol version
//! 2. **Security Handshake** - Authentication method selection and execution
//! 3. **Initialization** - Exchange of framebuffer parameters and capabilities
//! 4. **Normal Operation** - Ongoing message exchange for input events and screen updates
use ;
// Re-export PixelFormat from rfb-encodings
pub use PixelFormat;
// Re-export encoding constants from rfb-encodings
pub use ;
/// The RFB protocol version string advertised by the server.
///
/// This server implements RFB protocol version 3.8, which is widely supported
/// by modern VNC clients. The version string must be exactly 12 bytes including
/// the newline character as specified by the RFB protocol.
pub const PROTOCOL_VERSION: &str = "RFB 003.008\n";
/// Maximum framebuffer update buffer size in bytes (32KB).
///
/// This limit matches the reference VNC implementation and helps prevent
/// overwhelming clients or network infrastructure with very large updates.
/// When an update would exceed this size, it should be split into multiple
/// `FramebufferUpdate` messages.
pub const UPDATE_BUF_SIZE: usize = 32768;
// Client-to-Server Message Types
/// Message type: Client requests to change the pixel format.
///
/// This message allows the client to specify its preferred pixel format
/// for receiving framebuffer updates.
pub const CLIENT_MSG_SET_PIXEL_FORMAT: u8 = 0;
/// Message type: Client specifies supported encodings.
///
/// The client sends a list of encoding types it supports, ordered by preference.
/// The server will use the first mutually supported encoding.
pub const CLIENT_MSG_SET_ENCODINGS: u8 = 2;
/// Message type: Client requests a framebuffer update.
///
/// The client can request either an incremental update (changes only) or
/// a full refresh of a specified rectangular region.
pub const CLIENT_MSG_FRAMEBUFFER_UPDATE_REQUEST: u8 = 3;
/// Message type: Client sends a keyboard event.
///
/// Contains information about a key press or release event, including
/// the key symbol and the press/release state.
pub const CLIENT_MSG_KEY_EVENT: u8 = 4;
/// Message type: Client sends a pointer (mouse) event.
///
/// Contains the current pointer position and button state.
pub const CLIENT_MSG_POINTER_EVENT: u8 = 5;
/// Message type: Client sends cut text (clipboard data).
///
/// Allows the client to transfer clipboard contents to the server.
pub const CLIENT_MSG_CLIENT_CUT_TEXT: u8 = 6;
/// Message type: Client enables or disables Continuous Updates.
///
/// Part of the `ContinuousUpdates` extension. When enabled, the server
/// pushes framebuffer updates without waiting for `FramebufferUpdateRequest`.
/// Message format: type (u8) + enable (u8) + x (u16) + y (u16) + w (u16) + h (u16)
pub const CLIENT_MSG_ENABLE_CONTINUOUS_UPDATES: u8 = 150;
// Server-to-Client Message Types
/// Message type: Server sends a framebuffer update.
///
/// Contains one or more rectangles of pixel data representing screen changes.
/// This is the primary message for transmitting visual updates to the client.
pub const SERVER_MSG_FRAMEBUFFER_UPDATE: u8 = 0;
/// Message type: Server sets colour map entries.
///
/// Used for indexed color modes to define the color palette.
/// Not currently used in this true-color implementation.
pub const SERVER_MSG_SET_COLOUR_MAP_ENTRIES: u8 = 1;
/// Message type: Server sends a bell (beep) notification.
///
/// Signals the client to produce an audible or visual alert.
pub const SERVER_MSG_BELL: u8 = 2;
/// Message type: Server sends cut text (clipboard data).
///
/// Allows the server to transfer clipboard contents to the client.
pub const SERVER_MSG_SERVER_CUT_TEXT: u8 = 3;
/// Message type: Server signals end of continuous updates.
///
/// Part of the `ContinuousUpdates` extension. Sent by the server to indicate
/// it supports the `ContinuousUpdates` extension when client advertises -313.
/// Also sent when continuous updates are disabled.
pub const SERVER_MSG_END_OF_CONTINUOUS_UPDATES: u8 = 150;
// Encoding Types
//
// Note: Most encoding type constants are re-exported from rfb-encodings at the top of this file.
// Only server-specific encodings and pseudo-encodings are defined here.
/// Encoding type: Copy Rectangle.
///
/// Instructs the client to copy a rectangular region from one location
/// to another on the screen. Highly efficient for scrolling operations.
/// This is a server-side operation, not a data encoding format.
pub const ENCODING_COPYRECT: i32 = 1;
/// Encoding type: Tile Run-Length Encoding.
///
/// An efficient encoding for palettized and run-length compressed data.
/// Note: Not currently implemented in rfb-encodings.
pub const ENCODING_TRLE: i32 = 15;
/// Encoding type: H.264 video encoding.
///
/// H.264 video compression for very low bandwidth scenarios.
/// Note: This encoding is defined in the RFB protocol but NOT implemented.
/// standard VNC protocol removed H.264 support in v0.9.11 (2016) due to it being
/// broken and unmaintained. This constant exists for protocol compatibility only.
pub const ENCODING_H264: i32 = 0x4832_3634;
/// Pseudo-encoding: Rich Cursor.
///
/// Allows the server to send cursor shape and hotspot information.
pub const ENCODING_CURSOR: i32 = -239;
/// Pseudo-encoding: Desktop Size.
///
/// Notifies the client of framebuffer dimension changes.
pub const ENCODING_DESKTOP_SIZE: i32 = -223;
/// Pseudo-encoding: JPEG Quality Level 0 (lowest quality, highest compression).
///
/// When included in the client's encoding list, this requests the server
/// to use the lowest JPEG quality setting (approximately 10% quality).
pub const ENCODING_QUALITY_LEVEL_0: i32 = -32;
/// Pseudo-encoding: JPEG Quality Level 9 (highest quality, lowest compression).
///
/// When included in the client's encoding list, this requests the server
/// to use the highest JPEG quality setting (approximately 100% quality).
pub const ENCODING_QUALITY_LEVEL_9: i32 = -23;
/// Pseudo-encoding: Compression Level 0 (no compression, fastest).
///
/// Requests the server to use minimal or no compression for encodings
/// that support adjustable compression levels (e.g., Zlib, Tight).
pub const ENCODING_COMPRESS_LEVEL_0: i32 = -256;
/// Pseudo-encoding: Compression Level 9 (maximum compression, slowest).
///
/// Requests the server to use maximum compression, trading CPU time
/// for reduced bandwidth usage.
pub const ENCODING_COMPRESS_LEVEL_9: i32 = -247;
/// Pseudo-encoding: Continuous Updates.
///
/// When included in the client's encoding list, this indicates the client
/// supports the `ContinuousUpdates` extension. The server should respond with
/// an `EndOfContinuousUpdates` message (type 150) to confirm support.
/// Once confirmed, the client can send `EnableContinuousUpdates` messages.
pub const ENCODING_CONTINUOUS_UPDATES: i32 = -313;
// Note: Hextile and Tight subencoding constants are re-exported from rfb-encodings
// at the top of this file.
// Security Types
/// Security type: Invalid/Unknown.
///
/// Indicates an error or unsupported security mechanism.
pub const SECURITY_TYPE_INVALID: u8 = 0;
/// Security type: None (no authentication).
///
/// No authentication is required. The connection proceeds directly
/// to the initialization phase.
pub const SECURITY_TYPE_NONE: u8 = 1;
/// Security type: VNC Authentication.
///
/// Standard VNC authentication using DES-encrypted challenge-response.
/// The server sends a 16-byte challenge, which the client encrypts with
/// the password and returns.
pub const SECURITY_TYPE_VNC_AUTH: u8 = 2;
// Security Results
/// Security result: Authentication successful.
///
/// Sent by the server to indicate that authentication (if any) succeeded.
pub const SECURITY_RESULT_OK: u32 = 0;
/// Security result: Authentication failed.
///
/// Sent by the server to indicate that authentication failed.
pub const SECURITY_RESULT_FAILED: u32 = 1;
/// Represents the `ServerInit` message sent during VNC initialization.
///
/// This message is sent by the server after security negotiation is complete.
/// It provides the client with framebuffer dimensions, pixel format, and
/// the desktop name.
/// Represents all possible message types that can be sent from a VNC client to the server.
///
/// This enum encapsulates the various client messages defined in the RFB protocol,
/// making it easier to handle client input in a type-safe manner.
/// Represents a rectangle header in a framebuffer update message.
///
/// Each framebuffer update can contain multiple rectangles, each with its own
/// encoding type. The rectangle header specifies the position, dimensions,
/// and encoding of the pixel data that follows.