45#if defined(__FreeBSD__)
49#include <sys/termios.h>
68#include "debug/VNC.hh"
111 else if (revent & POLLNVAL)
142 DPRINTF(VNC,
"Vnc server created at port %d\n",
p.port);
163 warn_once(
"Sockets disabled, not accepting vnc client connections");
183 panic(
"%s: cannot accept a connection if not listening!",
name());
187 warn(
"%s: failed to accept VNC connection!",
name());
192 char message[] =
"vnc server already attached!\n";
207 inform(
"VNC client attached\n");
215 DPRINTF(VNC,
"Vnc client message recieved\n");
235 uint8_t message_type;
236 if (!
read(&message_type))
239 switch (message_type) {
259 warn(
"Unimplemented message type recv from client: %d\n",
266 panic(
"Unknown vnc server state\n");
276 panic(
"vnc not properly attached.\n");
281 }
while (ret == -1 && errno == EINTR);
285 DPRINTF(VNC,
"Read failed %d.\n", ret);
304 return read((uint8_t *)
val,
sizeof(T));
312 panic(
"Vnc client not properly attached.\n");
317 DPRINTF(VNC,
"Write failed.\n");
329 return write((uint8_t *)
val,
sizeof(T));
335 return write((uint8_t *)str, strlen(str));
355 inform(
"VNC client detached\n");
362 uint32_t
len = strlen(error_msg);
373 [[maybe_unused]]
size_t len;
374 char version_string[13];
377 version_string[12] = 0;
379 if (!
read((uint8_t *)version_string,
sizeof(version_string) - 1)) {
380 warn(
"Failed to read protocol version.");
384 uint32_t major_version, minor_version;
387 if (sscanf(version_string,
"RFB %03d.%03d\n", &major_version,
388 &minor_version) != 2) {
389 warn(
" Malformed protocol version %s\n", version_string);
390 sendError(
"Malformed protocol version\n");
395 DPRINTF(VNC,
"Client request protocol version %d.%d\n", major_version,
399 if (major_version != 3 || minor_version < 2) {
400 warn(
"Unsupported VNC client version... disconnecting\n");
407 if (minor_version < 7) {
409 if (!
write(&sec_type))
427 uint8_t security_type;
428 if (!
read(&security_type))
432 warn(
"Unknown VNC security type\n");
436 DPRINTF(VNC,
"Sending security auth OK\n");
439 if (!
write(&success))
449 DPRINTF(VNC,
"Sending server init message to client\n");
467 std::memcpy(msg.
name,
"M5", 2);
477 DPRINTF(VNC,
"Received pixel format from client message\n");
483 DPRINTF(VNC,
" -- bpp = %d; depth = %d; be = %d\n", pfm.
px.
bpp,
485 DPRINTF(VNC,
" -- true color = %d red,green,blue max = %d,%d,%d\n",
501 warn(
"VNC client doesn't support true color raw encoding\n");
509 DPRINTF(VNC,
"Received supported encodings from client\n");
537 warn(
"VNC clients must always support raw encoding\n");
545 DPRINTF(VNC,
"Received frame buffer update request from client\n");
556 DPRINTF(VNC,
" -- x = %d y = %d w = %d h = %d\n", fbr.
x, fbr.
y, fbr.
width,
565 DPRINTF(VNC,
"Received keyboard input from client\n");
581 DPRINTF(VNC,
"Received pointer input from client\n");
589 DPRINTF(VNC,
" -- pointer at x = %d y = %d buttons = %#x\n", pem.
x, pem.
y,
599 DPRINTF(VNC,
"Received client copy buffer message\n");
607 DPRINTF(VNC,
"String length %d\n", data_len);
608 while (data_len > 0) {
609 size_t bytes_to_read = data_len > 1024 ? 1024 : data_len;
610 if (!
read((uint8_t *)&str, bytes_to_read))
612 str[bytes_to_read] = 0;
613 data_len -= bytes_to_read;
614 DPRINTF(VNC,
"Buffer: %s\n", str);
625 DPRINTF(VNC,
"NOT sending framebuffer update\n");
632 DPRINTF(VNC,
"Sending framebuffer update\n");
660 for (
int y = 0; y <
fb->
height(); ++y) {
662 uint8_t *raw_pixel(line_buffer.data());
668 if (!
write(line_buffer.data(), line_buffer.size()))
677 DPRINTF(VNC,
"Sending framebuffer resize\n");
unsigned height() const
Frame buffer height in pixels.
const Pixel & pixel(unsigned x, unsigned y) const
Get a pixel from an (x, y) coordinate.
unsigned width() const
Frame buffer width in pixels.
static bool allDisabled()
virtual std::string name() const
ByteOrder byte_order
Byte order when stored to memory.
Channel ch_b
Blue channel conversion helper.
unsigned depth
Number of bits used to represent one pixel value (excluding padding).
unsigned length
Bytes per pixel when stored in memory (including padding)
Channel ch_r
Red channel conversion helper.
uint32_t fromPixel(const Pixel &pixel) const
Convert a Pixel into a color word.
Channel ch_g
Green channel conversion helper.
virtual void keyPress(uint32_t key, bool down)=0
Called when the vnc server receives a key press event from the client.
virtual void mouseAt(uint16_t x, uint16_t y, uint8_t buttons)=0
called whenever the mouse moves or it's button state changes buttons is a simple mask with each butto...
DataEvent(VncServer *vs, int fd, int e)
Poll event for the data socket.
ListenEvent(VncServer *vs, int fd, int e)
Poll event for the listen socket.
void recvKeyboardInput()
Receive message from client providing new keyboard input.
bool read1(uint8_t *buf, size_t len)
Read len -1 bytes from the client into the buffer provided + 1 assert that we read enough bytes.
void sendServerInit()
Send client our idea about what the frame buffer looks like.
void setEncodings()
Receive encodings message from client and process it.
ListenEvent * listenEvent
bool write(const uint8_t *buf, size_t len)
Write a buffer to the client.
bool supportsResizeEnc
If the vnc client supports the desktop resize command.
void frameBufferResized() override
void sendFrameBufferResized()
Tell the client that the frame buffer resized.
void setPixelFormat()
Receive pixel foramt message from client and process it.
void checkSecurity()
Check that the security exchange was successful.
void requestFbUpdate()
Receive message from client asking for updated frame buffer.
void sendFrameBufferUpdate()
Send a updated frame buffer to the client.
void recvPointerInput()
Recv message from client providing new mouse movement or button click.
void recvCutText()
Receive message from client that there is text in it's paste buffer.
bool supportsRawEnc
If the vnc client supports receiving raw data.
void setDirty() override
The frame buffer uses this call to notify the vnc server that the frame buffer has been updated and a...
static const PixelConverter pixelConverter
void sendError(const char *error_msg)
vnc client Interface
void checkProtocolVersion()
Check the client's protocol verion for compatibility and send the security types we support.
bool read(uint8_t *buf, size_t len)
Read some data from the client.
PixelFormat pixelFormat
The one and only pixel format we support.
VncServer(const Params &p)
VncServer.
ConnectionState curState
The rfb prototol state the connection is in.
bool sendUpdate
An update needs to be sent to the client.
static const uint32_t VncOK
Error conditions.
const char * vncVersion() const
static const uint32_t AuthInvalid
Authentication modes.
static const uint32_t AuthNone
@ ServerFrameBufferUpdate
@ WaitForSecurityResponse
EventQueue * eventQueue() const
#define panic(...)
This implements a cprintf based panic() function.
void remove(PollEvent *event)
void schedule(PollEvent *event)
Bitfield< 27, 25 > encoding
Copyright (c) 2024 - Pranith Kumar Copyright (c) 2020 Inria All rights reserved.
ssize_t atomic_write(int fd, const void *s, size_t n)
unsigned offset
Offset in bits.
unsigned mask
Bit mask (after shifting)
Declaration of a VNC server.