cscg24-guacamole

CSCG 2024 Challenge 'Guacamole Mashup'
git clone https://git.sinitax.com/sinitax/cscg24-guacamole
Log | Files | Refs | sfeed.txt

rfbclient.h (33873B)


      1#ifndef RFBCLIENT_H
      2#define RFBCLIENT_H
      3
      4/**
      5 * @defgroup libvncclient_api LibVNCClient API Reference
      6 * @{
      7 */
      8
      9/*
     10 *  Copyright (C) 2017 D. R. Commander.  All Rights Reserved.
     11 *  Copyright (C) 2000, 2001 Const Kaplinsky.  All Rights Reserved.
     12 *  Copyright (C) 2000 Tridia Corporation.  All Rights Reserved.
     13 *  Copyright (C) 1999 AT&T Laboratories Cambridge.  All Rights Reserved.
     14 *
     15 *  This is free software; you can redistribute it and/or modify
     16 *  it under the terms of the GNU General Public License as published by
     17 *  the Free Software Foundation; either version 2 of the License, or
     18 *  (at your option) any later version.
     19 *
     20 *  This software is distributed in the hope that it will be useful,
     21 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
     22 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     23 *  GNU General Public License for more details.
     24 *
     25 *  You should have received a copy of the GNU General Public License
     26 *  along with this software; if not, write to the Free Software
     27 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
     28 *  USA.
     29 */
     30
     31/**
     32 * @file rfbclient.h
     33 */
     34
     35#ifdef WIN32
     36#define WIN32_LEAN_AND_MEAN /* Prevent loading any Winsock 1.x headers from windows.h */
     37#ifdef _MSC_VER
     38#pragma warning(disable:4996)
     39#endif
     40#endif
     41
     42#if defined(ANDROID) || defined(LIBVNCSERVER_HAVE_ANDROID)
     43#include <arpa/inet.h>
     44#include <sys/select.h>
     45#endif
     46
     47#include <stdio.h>
     48#include <stdlib.h>
     49#include <string.h>
     50#if LIBVNCSERVER_HAVE_SYS_TIME_H
     51#include <sys/time.h>
     52#endif
     53#if LIBVNCSERVER_HAVE_UNISTD_H
     54#include <unistd.h>
     55#endif
     56#include <rfb/rfbproto.h>
     57#include <rfb/keysym.h>
     58#include <rfb/threading.h>
     59
     60#ifdef LIBVNCSERVER_HAVE_SASL
     61#include <sasl/sasl.h>
     62#endif /* LIBVNCSERVER_HAVE_SASL */
     63
     64#define rfbClientSwap16IfLE(s) \
     65    (*(char *)&client->endianTest ? ((((s) & 0xff) << 8) | (((s) >> 8) & 0xff)) : (s))
     66
     67#define rfbClientSwap32IfLE(l) \
     68    (*(char *)&client->endianTest ? ((((l) >> 24) & 0x000000ff) | \
     69			     (((l) & 0x00ff0000) >> 8)  | \
     70			     (((l) & 0x0000ff00) << 8)  | \
     71			     (((l) & 0x000000ff) << 24))  : (l))
     72
     73#define rfbClientSwap64IfLE(l) \
     74    (*(char *)&client->endianTest ? ((((l) >> 56 ) & 0x00000000000000ffULL) | \
     75			     (((l) & 0x00ff000000000000ULL) >> 40)  | \
     76			     (((l) & 0x0000ff0000000000ULL) >> 24)  | \
     77			     (((l) & 0x000000ff00000000ULL) >> 8)  | \
     78			     (((l) & 0x00000000ff000000ULL) << 8)  | \
     79			     (((l) & 0x0000000000ff0000ULL) << 24)  | \
     80			     (((l) & 0x000000000000ff00ULL) << 40)  | \
     81			     (((l) & 0x00000000000000ffULL) << 56))  : (l))
     82
     83#define FLASH_PORT_OFFSET 5400
     84#define LISTEN_PORT_OFFSET 5500
     85#define TUNNEL_PORT_OFFSET 5500
     86#define SERVER_PORT_OFFSET 5900
     87
     88#define DEFAULT_CONNECT_TIMEOUT 60
     89#define DEFAULT_READ_TIMEOUT 0
     90
     91#define DEFAULT_SSH_CMD "/usr/bin/ssh"
     92#define DEFAULT_TUNNEL_CMD  \
     93  (DEFAULT_SSH_CMD " -f -L %L:localhost:%R %H sleep 20")
     94#define DEFAULT_VIA_CMD     \
     95  (DEFAULT_SSH_CMD " -f -L %L:%H:%R %G sleep 20")
     96
     97#if(defined __cplusplus)
     98extern "C"
     99{
    100#endif
    101
    102/** vncrec */
    103
    104typedef struct {
    105  FILE* file;
    106  struct timeval tv;
    107  rfbBool readTimestamp;
    108  rfbBool doNotSleep;
    109} rfbVNCRec;
    110
    111/** client data */
    112
    113typedef struct rfbClientData {
    114	void* tag;
    115	void* data;
    116	struct rfbClientData* next;
    117} rfbClientData;
    118
    119/** app data (belongs into rfbClient?) */
    120
    121typedef struct {
    122  rfbBool shareDesktop;
    123  rfbBool viewOnly;
    124
    125  const char* encodingsString;
    126
    127  rfbBool useBGR233;
    128  int nColours;
    129  rfbBool forceOwnCmap;
    130  rfbBool forceTrueColour;
    131  int requestedDepth;
    132
    133  int compressLevel;
    134  int qualityLevel;
    135  rfbBool enableJPEG;
    136  rfbBool useRemoteCursor;
    137  rfbBool palmVNC;  /**< use palmvnc specific SetScale (vs ultravnc) */
    138  int scaleSetting; /**< 0 means no scale set, else 1/scaleSetting */
    139} AppData;
    140
    141/** For GetCredentialProc callback function to return */
    142typedef union _rfbCredential
    143{
    144  /** X509 (VeNCrypt) */
    145  struct
    146  {
    147    char *x509CACertFile;
    148    char *x509CACrlFile;
    149    char *x509ClientCertFile;
    150    char *x509ClientKeyFile;
    151    uint8_t x509CrlVerifyMode; /* Only required for OpenSSL - see meanings below */
    152  } x509Credential;
    153  /** Plain (VeNCrypt), MSLogon (UltraVNC) */
    154  struct
    155  {
    156    char *username;
    157    char *password;
    158  } userCredential;
    159} rfbCredential;
    160
    161#define rfbCredentialTypeX509 1
    162#define rfbCredentialTypeUser 2
    163
    164/* When using OpenSSL, CRLs can be included in both the x509CACrlFile and appended
    165   to the x509CACertFile as is common with OpenSSL.  When rfbX509CrlVerifyAll is
    166   specified the CRL list must include CRLs for all certificates in the chain */
    167#define rfbX509CrlVerifyNone   0    /* No CRL checking is performed */
    168#define rfbX509CrlVerifyClient 1    /* Only the leaf server certificate is checked */
    169#define rfbX509CrlVerifyAll    2    /* All certificates in the server chain are checked */
    170
    171struct _rfbClient;
    172
    173/**
    174 * Handles a text chat message. If your application should accept text messages
    175 * from the server, define a function with this prototype and set
    176 * client->HandleTextChat to a pointer to that function subsequent to your
    177 * rfbGetClient() call.
    178 * @param client The client which called the text chat handler
    179 * @param value  text length if text != NULL, or one of rfbTextChatOpen,
    180 * rfbTextChatClose, rfbTextChatFinished if text == NULL
    181 * @param text The text message from the server
    182 */
    183typedef void (*HandleTextChatProc)(struct _rfbClient* client, int value, char *text);
    184/**
    185 * Handles XVP server messages. If your application sends XVP messages to the
    186 * server, you'll want to handle the server's XVP_FAIL and XVP_INIT responses.
    187 * Define a function with this prototype and set client->HandleXvpMsg to a
    188 * pointer to that function subsequent to your rfbGetClient() call.
    189 * @param client The client which called the XVP message handler
    190 * @param version The highest XVP extension version that the server supports
    191 * @param opcode The opcode. 0 is XVP_FAIL, 1 is XVP_INIT
    192 */
    193typedef void (*HandleXvpMsgProc)(struct _rfbClient* client, uint8_t version, uint8_t opcode);
    194typedef void (*HandleKeyboardLedStateProc)(struct _rfbClient* client, int value, int pad);
    195typedef rfbBool (*HandleCursorPosProc)(struct _rfbClient* client, int x, int y);
    196typedef void (*SoftCursorLockAreaProc)(struct _rfbClient* client, int x, int y, int w, int h);
    197typedef void (*SoftCursorUnlockScreenProc)(struct _rfbClient* client);
    198/**
    199   Callback indicating that a rectangular area of the client's framebuffer was updated.
    200   As a server will usually send several rects per rfbFramebufferUpdate message, this
    201   callback is usually called multiple times per rfbFramebufferUpdate message.
    202   @param client The client whose framebuffer was (partially) updated
    203   @param x The x-coordinate of the upper left corner of the updated rectangle
    204   @param y The y-coordinate of the upper left corner of the updated rectangle
    205   @param w The width of the updated rectangle
    206   @param h The heigth of the updated rectangle
    207 */
    208typedef void (*GotFrameBufferUpdateProc)(struct _rfbClient* client, int x, int y, int w, int h);
    209/**
    210   Callback indicating that a client has completely processed an rfbFramebufferUpdate
    211   message sent by a server.
    212   This is called exactly once per each handled rfbFramebufferUpdate message.
    213   @param client The client which finished processing an rfbFramebufferUpdate
    214 */
    215typedef void (*FinishedFrameBufferUpdateProc)(struct _rfbClient* client);
    216typedef char* (*GetPasswordProc)(struct _rfbClient* client);
    217typedef rfbCredential* (*GetCredentialProc)(struct _rfbClient* client, int credentialType);
    218typedef rfbBool (*MallocFrameBufferProc)(struct _rfbClient* client);
    219typedef void (*GotXCutTextProc)(struct _rfbClient* client, const char *text, int textlen);
    220typedef void (*BellProc)(struct _rfbClient* client);
    221/**
    222    Called when a cursor shape update was received from the server. The decoded cursor shape
    223    will be in client->rcSource. It's up to the application to do something with this, e.g. draw
    224    into a viewer's window. If you want the server to draw the cursor into the framebuffer, be
    225    careful not to announce remote cursor support, i.e. not include rfbEncodingXCursor or
    226    rfbEncodingRichCursor in SetFormatAndEncodings().
    227*/
    228typedef void (*GotCursorShapeProc)(struct _rfbClient* client, int xhot, int yhot, int width, int height, int bytesPerPixel);
    229typedef void (*GotCopyRectProc)(struct _rfbClient* client, int src_x, int src_y, int w, int h, int dest_x, int dest_y);
    230typedef void (*GotFillRectProc)(struct _rfbClient* client, int x, int y, int w, int h, uint32_t colour);
    231typedef void (*GotBitmapProc)(struct _rfbClient* client, const uint8_t* buffer, int x, int y, int w, int h);
    232typedef rfbBool (*GotJpegProc)(struct _rfbClient* client, const uint8_t* buffer, int length, int x, int y, int w, int h);
    233typedef rfbBool (*LockWriteToTLSProc)(struct _rfbClient* client);   /** @deprecated */
    234typedef rfbBool (*UnlockWriteToTLSProc)(struct _rfbClient* client); /** @deprecated */
    235
    236#ifdef LIBVNCSERVER_HAVE_SASL
    237typedef char* (*GetUserProc)(struct _rfbClient* client);
    238typedef char* (*GetSASLMechanismProc)(struct _rfbClient* client, char* mechlist);
    239#endif /* LIBVNCSERVER_HAVE_SASL */
    240
    241typedef struct _rfbClient {
    242	uint8_t* frameBuffer;
    243	int width, height;
    244
    245	int endianTest;
    246
    247	AppData appData;
    248
    249	const char* programName;
    250	char* serverHost;
    251	int serverPort; /**< if -1, then use file recorded by vncrec */
    252	rfbBool listenSpecified;
    253	int listenPort, flashPort;
    254
    255	struct {
    256		int x, y, w, h;
    257	} updateRect;
    258
    259	/** Note that the CoRRE encoding uses this buffer and assumes it is big enough
    260	   to hold 255 * 255 * 32 bits -> 260100 bytes.  640*480 = 307200 bytes.
    261	   Hextile also assumes it is big enough to hold 16 * 16 * 32 bits.
    262	   Tight encoding assumes BUFFER_SIZE is at least 16384 bytes. */
    263
    264#define RFB_BUFFER_SIZE (640*480)
    265	char buffer[RFB_BUFFER_SIZE];
    266
    267	/* rfbproto.c */
    268
    269	rfbSocket sock;
    270	rfbBool canUseCoRRE;
    271	rfbBool canUseHextile;
    272	char *desktopName;
    273	rfbPixelFormat format;
    274	rfbServerInitMsg si;
    275
    276	/* sockets.c */
    277#define RFB_BUF_SIZE 8192
    278	char buf[RFB_BUF_SIZE];
    279	char *bufoutptr;
    280	unsigned int buffered;
    281
    282	/* The zlib encoding requires expansion/decompression/deflation of the
    283	   compressed data in the "buffer" above into another, result buffer.
    284	   However, the size of the result buffer can be determined precisely
    285	   based on the bitsPerPixel, height and width of the rectangle.  We
    286	   allocate this buffer one time to be the full size of the buffer. */
    287
    288	/* Ultra Encoding uses this buffer too */
    289	
    290	int ultra_buffer_size;
    291	char *ultra_buffer;
    292
    293	int raw_buffer_size;
    294	char *raw_buffer;
    295
    296#ifdef LIBVNCSERVER_HAVE_LIBZ
    297	z_stream decompStream;
    298	rfbBool decompStreamInited;
    299#endif
    300
    301
    302#ifdef LIBVNCSERVER_HAVE_LIBZ
    303	/*
    304	 * Variables for the ``tight'' encoding implementation.
    305	 */
    306
    307	/** Separate buffer for compressed data. */
    308#define ZLIB_BUFFER_SIZE 30000
    309	char zlib_buffer[ZLIB_BUFFER_SIZE];
    310
    311	/* Four independent compression streams for zlib library. */
    312	z_stream zlibStream[4];
    313	rfbBool zlibStreamActive[4];
    314
    315	/* Filter stuff. Should be initialized by filter initialization code. */
    316	rfbBool cutZeros;
    317	int rectWidth, rectColors;
    318	char tightPalette[256*4];
    319	uint8_t tightPrevRow[2048*3*sizeof(uint16_t)];
    320
    321#ifdef LIBVNCSERVER_HAVE_LIBJPEG
    322	/** JPEG decoder state (obsolete-- do not use). */
    323	rfbBool jpegError;
    324
    325	struct jpeg_source_mgr* jpegSrcManager;
    326	void* jpegBufferPtr;
    327	size_t jpegBufferLen;
    328
    329#endif
    330#endif
    331
    332
    333	/* cursor.c */
    334	/** Holds cursor shape data when received from server. */
    335	uint8_t *rcSource, *rcMask;
    336
    337	/** private data pointer */
    338	rfbClientData* clientData;
    339
    340	rfbVNCRec* vncRec;
    341
    342	/* Keyboard State support (is 'Caps Lock' set on the remote display???) */
    343	int KeyboardLedStateEnabled;
    344	int CurrentKeyboardLedState;
    345
    346	int canHandleNewFBSize;
    347
    348	/* hooks */
    349	HandleTextChatProc         HandleTextChat;
    350	HandleKeyboardLedStateProc HandleKeyboardLedState;
    351	HandleCursorPosProc HandleCursorPos;
    352	SoftCursorLockAreaProc SoftCursorLockArea;
    353	SoftCursorUnlockScreenProc SoftCursorUnlockScreen;
    354	GotFrameBufferUpdateProc GotFrameBufferUpdate;
    355	/** the pointer returned by GetPassword will be freed after use! */
    356	GetPasswordProc GetPassword;
    357	MallocFrameBufferProc MallocFrameBuffer;
    358	GotXCutTextProc GotXCutText;
    359	BellProc Bell;
    360
    361	GotCursorShapeProc GotCursorShape;
    362	GotCopyRectProc GotCopyRect;
    363
    364	/** Which messages are supported by the server
    365	 * This is a *guess* for most servers.
    366	 * (If we can even detect the type of server)
    367	 *
    368	 * If the server supports the "rfbEncodingSupportedMessages"
    369	 * then this will be updated when the encoding is received to
    370	 * accurately reflect the servers capabilities.
    371	 */
    372	rfbSupportedMessages supportedMessages;
    373
    374	/** negotiated protocol version */
    375	int major, minor;
    376
    377	/** The selected security types */
    378	uint32_t authScheme, subAuthScheme;
    379
    380	/** The TLS session for Anonymous TLS and VeNCrypt */
    381	void* tlsSession;
    382
    383	/** To support security types that requires user input (except VNC password
    384	 * authentication), for example VeNCrypt and MSLogon, this callback function
    385	 * must be set before the authentication. Otherwise, it implicates that the
    386	 * caller application does not support it and related security types should
    387	 * be bypassed.
    388	 */
    389	GetCredentialProc GetCredential;
    390
    391	/** The 0-terminated security types supported by the client.
    392	 * Set by function SetClientAuthSchemes() */
    393	uint32_t *clientAuthSchemes;
    394
    395	/** When the server is a repeater, this specifies the final destination */
    396	char *destHost;
    397	int destPort;
    398
    399        /** the QoS IP DSCP for this client */
    400        int QoS_DSCP;
    401
    402        /** hook to handle xvp server messages */
    403	HandleXvpMsgProc           HandleXvpMsg;
    404
    405	/* listen.c */
    406        rfbSocket listenSock;
    407
    408	FinishedFrameBufferUpdateProc FinishedFrameBufferUpdate;
    409
    410	char *listenAddress;
    411        /* IPv6 listen socket, address and port*/
    412        rfbSocket listen6Sock;
    413        char* listen6Address;
    414        int listen6Port;
    415
    416        /* Output Window ID. When set, client application enables libvncclient to perform direct rendering in its window */
    417        unsigned long outputWindow;
    418
    419	/** 
    420	 * These lock/unlock hooks are not used anymore. LibVNCClient will now use 
    421	 * platform-specific synchronization library to protect concurrent TLS R/W.
    422	 *  
    423	 * @deprecated
    424	 */
    425	LockWriteToTLSProc LockWriteToTLS;
    426	UnlockWriteToTLSProc UnlockWriteToTLS;
    427
    428        /** Hooks for custom rendering
    429         *
    430         * VNC rendering boils down to 3 activities:
    431         * - GotCopyRect: copy an area of the framebuffer
    432         * - GotFillRect: fill an area of the framebuffer with a solid color
    433         * - GotBitmap: copy the bitmap in the buffer into the framebuffer
    434         * The client application should either set all three of these or none!
    435         */
    436        GotFillRectProc GotFillRect;
    437        GotBitmapProc GotBitmap;
    438        /** Hook for custom JPEG decoding and rendering */
    439        GotJpegProc GotJpeg;
    440
    441#ifdef LIBVNCSERVER_HAVE_SASL
    442        sasl_conn_t *saslconn;
    443        const char *saslDecoded;
    444        unsigned int saslDecodedLength;
    445        unsigned int saslDecodedOffset;
    446        sasl_secret_t *saslSecret;
    447
    448        /* Callback to allow the client to choose a preferred mechanism. The string returned will
    449           be freed once no longer required. */
    450        GetSASLMechanismProc GetSASLMechanism;
    451        GetUserProc GetUser;
    452
    453#endif /* LIBVNCSERVER_HAVE_SASL */
    454
    455#ifdef LIBVNCSERVER_HAVE_LIBZ
    456#ifdef LIBVNCSERVER_HAVE_LIBJPEG
    457	/** JPEG decoder state. */
    458	void *tjhnd;
    459
    460#endif
    461#endif
    462	/* timeout in seconds for select() after connect() */
    463	unsigned int connectTimeout;
    464
    465	/* timeout in seconds when reading from half-open connections in
    466	 * ReadFromRFBServer() - keep at 0 to disable timeout detection and handling */
    467	unsigned int readTimeout;
    468
    469	/**
    470	 * Mutex to protect concurrent TLS read/write.
    471	 * For internal use only.
    472	 */
    473	MUTEX(tlsRwMutex);
    474
    475	rfbBool requestedResize;
    476        /**
    477         * Used for intended dimensions, rfbClient.width and rfbClient.height are used to manage the real framebuffer dimensions.
    478	 */
    479	rfbExtDesktopScreen screen;
    480} rfbClient;
    481
    482/* cursor.c */
    483/**
    484 * Handles XCursor and RichCursor shape updates from the server.
    485 * We emulate cursor operating on the frame buffer (that is
    486 * why we call it "software cursor"). This decodes the received cursor
    487 * shape and hands it over to GotCursorShapeProc, if set.
    488 */
    489extern rfbBool HandleCursorShape(rfbClient* client,int xhot, int yhot, int width, int height, uint32_t enc);
    490
    491/* listen.c */
    492
    493extern void listenForIncomingConnections(rfbClient* viewer);
    494extern int listenForIncomingConnectionsNoFork(rfbClient* viewer, int usec_timeout);
    495
    496/* rfbproto.c */
    497
    498extern rfbBool rfbEnableClientLogging;
    499typedef void (*rfbClientLogProc)(const char *format, ...);
    500extern rfbClientLogProc rfbClientLog,rfbClientErr;
    501extern rfbBool ConnectToRFBServer(rfbClient* client,const char *hostname, int port);
    502extern rfbBool ConnectToRFBRepeater(rfbClient* client,const char *repeaterHost, int repeaterPort, const char *destHost, int destPort);
    503extern void SetClientAuthSchemes(rfbClient* client,const uint32_t *authSchemes, int size);
    504extern rfbBool InitialiseRFBConnection(rfbClient* client);
    505/**
    506 * Sends format and encoding parameters to the server. Your application can
    507 * modify the 'client' data structure directly. However some changes to this
    508 * structure must be communicated back to the server. For instance, if you
    509 * change the encoding to hextile, the server needs to know that it should send
    510 * framebuffer updates in hextile format. Likewise if you change the pixel
    511 * format of the framebuffer, the server must be notified about this as well.
    512 * Call this function to propagate your changes of the local 'client' structure
    513 * over to the server.
    514 * @li Encoding type
    515 * @li RFB protocol extensions announced via pseudo-encodings
    516 * @li Framebuffer pixel format (like RGB vs ARGB)
    517 * @li Remote cursor support
    518 * @param client The client in which the format or encodings have been changed
    519 * @return true if the format or encodings were sent to the server successfully,
    520 * false otherwise
    521 */
    522extern rfbBool SetFormatAndEncodings(rfbClient* client);
    523extern rfbBool SendIncrementalFramebufferUpdateRequest(rfbClient* client);
    524/**
    525 * Sends a framebuffer update request to the server. A VNC client may request an
    526 * update from the server at any time. You can also specify which portions of
    527 * the screen you want updated. This can be handy if a pointer is at certain
    528 * location and the user pressed a mouse button, for instance. Then you can
    529 * immediately request an update of the region around the pointer from the
    530 * server.
    531 * @note The coordinate system is a left-handed Cartesian coordinate system with
    532 * the Z axis (unused) pointing out of the screen. Alternately you can think of
    533 * it as a right-handed Cartesian coordinate system with the Z axis pointing
    534 * into the screen. The origin is at the upper left corner of the framebuffer.
    535 * @param client The client through which to send the request
    536 * @param x The horizontal position of the update request rectangle
    537 * @param y The vertical position of the update request rectangle
    538 * @param w The width of the update request rectangle
    539 * @param h The height of the update request rectangle
    540 * @param incremental false: server sends rectangle even if nothing changed.
    541 * true: server only sends changed parts of rectangle.
    542 * @return true if the update request was sent successfully, false otherwise
    543 */
    544extern rfbBool SendFramebufferUpdateRequest(rfbClient* client,
    545					 int x, int y, int w, int h,
    546					 rfbBool incremental);
    547extern rfbBool SendScaleSetting(rfbClient* client,int scaleSetting);
    548/**
    549 * Sends a pointer event to the server. A pointer event includes a cursor
    550 * location and a button mask. The button mask indicates which buttons on the
    551 * pointing device are pressed. Each button is represented by a bit in the
    552 * button mask. A 1 indicates the button is pressed while a 0 indicates that it
    553 * is not pressed. You may use these pre-defined button masks by ORing them
    554 * together: rfbButton1Mask, rfbButton2Mask, rfbButton3Mask, rfbButton4Mask
    555 * rfbButton5Mask
    556 * @note  The cursor location is relative to the client's framebuffer, not the
    557 * client's screen itself.
    558 * @note The coordinate system is a left-handed Cartesian coordinate system with
    559 * the Z axis (unused) pointing out of the screen. Alternately you can think of
    560 * it as a right-handed Cartesian coordinate system with the Z axis pointing
    561 * into the screen. The origin is at the upper left corner of the screen.
    562 * @param client The client through which to send the pointer event
    563 * @param x the horizontal location of the cursor
    564 * @param y the vertical location of the cursor
    565 * @param buttonMask the button mask indicating which buttons are pressed
    566 * @return true if the pointer event was sent successfully, false otherwise
    567 */
    568extern rfbBool SendPointerEvent(rfbClient* client,int x, int y, int buttonMask);
    569/**
    570 * Sends a SetDesktopSize event to the server.
    571 * @param client The client through which to send the SetDesktopSize event
    572 * @param width The width of the update request rectangle
    573 * @param height The height of the update request rectangle
    574 * @return true if the SetDesktopSize event was send successfully, false otherwise
    575 */
    576extern rfbBool SendExtDesktopSize(rfbClient* client, uint16_t width, uint16_t height);
    577/**
    578 * Sends a key event to the server. If your application is not merely a VNC
    579 * viewer (i.e. it controls the server), you'll want to send the keys that the
    580 * user presses to the server. Use this function to do that.
    581 * @param client The client through which to send the key event
    582 * @param key An rfbKeySym defined in rfb/keysym.h
    583 * @param down true if this was a key down event, false otherwise
    584 * @return true if the key event was send successfully, false otherwise
    585 */
    586extern rfbBool SendKeyEvent(rfbClient* client,uint32_t key, rfbBool down);
    587/**
    588 * The same as SendKeyEvent, except a key code will be sent along with the
    589 * symbol if the server supports extended key events.
    590 * @param client The client through which to send the key event
    591 * @param keysym An rfbKeySym defined in rfb/keysym.h
    592 * @param keycode An XT key code
    593 * @param down true if this was a key down event, false otherwise
    594 * @return true if the extended key event is supported and was sent
    595 * successfully, false otherwise
    596 */
    597extern rfbBool SendExtendedKeyEvent(rfbClient* client, uint32_t keysym, uint32_t keycode, rfbBool down);
    598/**
    599 * Places a string on the server's clipboard. Use this function if you want to
    600 * be able to copy and paste between the server and your application. For
    601 * instance, when your application is notified that the user copied some text
    602 * onto the clipboard, you would call this function to synchronize the server's
    603 * clipboard with your local clipboard.
    604 * @param client The client structure through which to send the client cut text
    605 * message
    606 * @param str The string to send (doesn't need to be NULL terminated)
    607 * @param len The length of the string
    608 * @return true if the client cut message was sent successfully, false otherwise
    609 */
    610extern rfbBool SendClientCutText(rfbClient* client,char *str, int len);
    611/**
    612 * Handles messages from the RFB server. You must call this function
    613 * intermittently so LibVNCClient can parse messages from the server. For
    614 * example, if your app has a draw loop, you could place a call to this
    615 * function within that draw loop.
    616 * @note You must call WaitForMessage() before you call this function.
    617 * @param client The client which will handle the RFB server messages
    618 * @return true if the client was able to handle the RFB server messages, false
    619 * otherwise
    620 */
    621extern rfbBool HandleRFBServerMessage(rfbClient* client);
    622
    623/**
    624 * Sends a text chat message to the server.
    625 * @param client The client through which to send the message
    626 * @param text The text to send
    627 * @return true if the text was sent successfully, false otherwise
    628 */
    629extern rfbBool TextChatSend(rfbClient* client, char *text);
    630/**
    631 * Opens a text chat window on the server.
    632 * @param client The client through which to send the message
    633 * @return true if the window was opened successfully, false otherwise
    634 */
    635extern rfbBool TextChatOpen(rfbClient* client);
    636/**
    637 * Closes the text chat window on the server.
    638 * @param client The client through which to send the message
    639 * @return true if the window was closed successfully, false otherwise
    640 */
    641extern rfbBool TextChatClose(rfbClient* client);
    642extern rfbBool TextChatFinish(rfbClient* client);
    643extern rfbBool PermitServerInput(rfbClient* client, int enabled);
    644extern rfbBool SendXvpMsg(rfbClient* client, uint8_t version, uint8_t code);
    645
    646extern void PrintPixelFormat(rfbPixelFormat *format);
    647
    648extern rfbBool SupportsClient2Server(rfbClient* client, int messageType);
    649extern rfbBool SupportsServer2Client(rfbClient* client, int messageType);
    650
    651/* client data */
    652
    653/**
    654 * Associates a client data tag with the given pointer. LibVNCClient has
    655 * several events to which you can associate your own handlers. These handlers
    656 * have the client structure as one of their parameters. Sometimes, you may want
    657 * to make data from elsewhere in your application available to these handlers
    658 * without using a global variable. To do this, you call
    659 * rfbClientSetClientData() and associate the data with a tag. Then, your
    660 * handler can call rfbClientGetClientData() and get the a pointer to the data
    661 * associated with that tag.
    662 * @param client The client in which to set the client data
    663 * @param tag A unique tag which identifies the data
    664 * @param data A pointer to the data to associate with the tag
    665 */
    666void rfbClientSetClientData(rfbClient* client, void* tag, void* data);
    667/**
    668 * Returns a pointer to the client data associated with the given tag. See the
    669 * the documentation for rfbClientSetClientData() for a discussion of how you
    670 * can use client data.
    671 * @param client The client from which to get the client data
    672 * @param tag The tag which identifies the client data
    673 * @return a pointer to the client data
    674 */
    675void* rfbClientGetClientData(rfbClient* client, void* tag);
    676
    677/* protocol extensions */
    678
    679typedef struct _rfbClientProtocolExtension {
    680	int* encodings;
    681	/** returns TRUE if the encoding was handled */
    682	rfbBool (*handleEncoding)(rfbClient* cl,
    683		rfbFramebufferUpdateRectHeader* rect);
    684	/** returns TRUE if it handled the message */
    685	rfbBool (*handleMessage)(rfbClient* cl,
    686		 rfbServerToClientMsg* message);
    687	struct _rfbClientProtocolExtension* next;
    688	uint32_t const* securityTypes;
    689	/** returns TRUE if it handled the authentication */
    690	rfbBool (*handleAuthentication)(rfbClient* cl, uint32_t authScheme);
    691} rfbClientProtocolExtension;
    692
    693void rfbClientRegisterExtension(rfbClientProtocolExtension* e);
    694
    695/* sockets.c */
    696
    697extern rfbBool errorMessageOnReadFailure;
    698
    699extern rfbBool ReadFromRFBServer(rfbClient* client, char *out, unsigned int n);
    700extern rfbBool WriteToRFBServer(rfbClient* client, const char *buf, unsigned int n);
    701extern int FindFreeTcpPort(void);
    702extern rfbSocket ListenAtTcpPort(int port);
    703extern rfbSocket ListenAtTcpPortAndAddress(int port, const char *address);
    704/**
    705   Tries to connect to an IPv4 host.
    706   @param host Binary IPv4 address
    707   @param port Port
    708   @return A blocking socket or RFB_INVALID_SOCKET if the connection failed
    709*/
    710extern rfbSocket ConnectClientToTcpAddr(unsigned int host, int port);
    711/**
    712   Tries to connect to an IPv4 or IPv6 host.
    713   @param hostname A hostname or IP address
    714   @param port Port
    715   @return A blocking socket or RFB_INVALID_SOCKET if the connection failed
    716*/
    717extern rfbSocket ConnectClientToTcpAddr6(const char *hostname, int port);
    718/**
    719   Tries to connect to a Unix socket.
    720   @param sockFile Path of the socket file
    721   @return A blocking socket or RFB_INVALID_SOCKET if the connection failed
    722*/
    723extern rfbSocket ConnectClientToUnixSock(const char *sockFile);
    724/**
    725   Tries to connect to an IPv4 host using the given timeout value.
    726   @param host Binary IPv4 address
    727   @param port Port
    728   @param timeout The time in seconds to wait for a connection
    729   @return A nonblocking socket or RFB_INVALID_SOCKET if the connection failed
    730*/
    731extern rfbSocket ConnectClientToTcpAddrWithTimeout(unsigned int host, int port, unsigned int timeout);
    732/**
    733   Tries to connect to an IPv4 or IPv6 host using the given timeout value.
    734   @param hostname A hostname or IP address
    735   @param port Port
    736   @param timeout The time in seconds to wait for a connection
    737   @return A nonblocking socket or RFB_INVALID_SOCKET if the connection failed
    738*/
    739extern rfbSocket ConnectClientToTcpAddr6WithTimeout(const char *hostname, int port, unsigned int timeout);
    740/**
    741   Tries to connect to a Unix socket using the given timeout value.
    742   @param sockFile Path of the socket file
    743   @param timeout The time in seconds to wait for a connection
    744   @return A nonblocking socket or RFB_INVALID_SOCKET if the connection failed
    745*/
    746extern rfbSocket ConnectClientToUnixSockWithTimeout(const char *sockFile, unsigned int timeout);
    747extern rfbSocket AcceptTcpConnection(rfbSocket listenSock);
    748extern rfbBool SetNonBlocking(rfbSocket sock);
    749extern rfbBool SetBlocking(rfbSocket sock);
    750extern rfbBool SetDSCP(rfbSocket sock, int dscp);
    751
    752extern rfbBool StringToIPAddr(const char *str, unsigned int *addr);
    753extern rfbBool SameMachine(rfbSocket sock);
    754/**
    755 * Waits for an RFB message to arrive from the server. Before handling a message
    756 * with HandleRFBServerMessage(), you must wait for your client to receive one.
    757 * This function blocks until a message is received. You may specify a timeout
    758 * in microseconds. Once this number of microseconds have elapsed, the function
    759 * will return.
    760 * @param client The client to cause to wait until a message is received
    761 * @param usecs The timeout in microseconds
    762 * @return the return value of the underlying select() call
    763 */
    764extern int WaitForMessage(rfbClient* client,unsigned int usecs);
    765
    766/* vncviewer.c */
    767/**
    768 * Allocates and returns a pointer to an rfbClient structure. This will probably
    769 * be the first LibVNCClient function your client code calls. Most libVNCClient
    770 * functions operate on an rfbClient structure, and this function allocates
    771 * memory for that structure. When you're done with the rfbClient structure
    772 * pointer this function returns, you should free the memory rfbGetClient()
    773 * allocated by calling rfbClientCleanup().
    774 *
    775 * A pixel is one dot on the screen. The number of bytes in a pixel will depend
    776 * on the number of samples in that pixel and the number of bits in each sample.
    777 * A sample represents one of the primary colors in a color model. The RGB
    778 * color model uses red, green, and blue samples respectively. Suppose you
    779 * wanted to use 16-bit RGB color: You would have three samples per pixel (one
    780 * for each primary color), five bits per sample (the quotient of 16 RGB bits
    781 * divided by three samples), and two bytes per pixel (the smallest multiple of
    782 * eight bits in which the 16-bit pixel will fit). If you wanted 32-bit RGB
    783 * color, you would have three samples per pixel again, eight bits per sample
    784 * (since that's how 32-bit color is defined), and four bytes per pixel (the
    785 * smallest multiple of eight bits in which the 32-bit pixel will fit.
    786 * @param bitsPerSample The number of bits in a sample
    787 * @param samplesPerPixel The number of samples in a pixel
    788 * @param bytesPerPixel The number of bytes in a pixel
    789 * @return a pointer to the allocated rfbClient structure
    790 */
    791rfbClient* rfbGetClient(int bitsPerSample,int samplesPerPixel,int bytesPerPixel);
    792/**
    793 * Initializes the client. The format is {PROGRAM_NAME, [OPTIONS]..., HOST}. This
    794 * function does not initialize the program name if the rfbClient's program
    795 * name is set already. The options are as follows:
    796 * <table>
    797 * <tr><th>Option</th><th>Description</th></tr>
    798 * <tr><td>-listen</td><td>Listen for incoming connections.</td></tr>
    799 * <tr><td>-listennofork</td><td>Listen for incoming connections without forking.
    800 * </td></tr>
    801 * <tr><td>-play</td><td>Set this client to replay a previously recorded session.</td></tr>
    802 * <tr><td>-encodings</td><td>Set the encodings to use. The next item in the
    803 * argv array is the encodings string, consisting of comma separated encodings like 'tight,ultra,raw'.</td></tr>
    804 * <tr><td>-compress</td><td>Set the compression level. The next item in the
    805 * argv array is the compression level as an integer. Ranges from 0 (lowest) to 9 (highest).
    806 * </td></tr>
    807 * <tr><td>-scale</td><td>Set the scaling level. The next item in the
    808 * argv array is the scaling level as an integer. The screen will be scaled down by this factor.</td></tr>
    809 * <tr><td>-qosdscp</td><td>Set the Quality of Service Differentiated Services
    810 * Code Point (QoS DSCP). The next item in the argv array is the code point as
    811 * an integer.</td></tr>
    812 * <tr><td>-repeaterdest</td><td>Set a VNC repeater address. The next item in the argv array is
    813 * the repeater's address as a string.</td></tr>
    814 * </table>
    815 *
    816 * The host may include a port number (delimited by a ':').
    817 * @param client The client to initialize
    818 * @param argc The number of arguments to the initializer
    819 * @param argv The arguments to the initializer as an array of NULL terminated
    820 * strings
    821 * @return true if the client was initialized successfully, false otherwise.
    822 */
    823rfbBool rfbInitClient(rfbClient* client,int* argc,char** argv);
    824/**
    825 * Cleans up the client structure and releases the memory allocated for it. You
    826 * should call this when you're done with the rfbClient structure that you
    827 * allocated with rfbGetClient().
    828 * @note rfbClientCleanup() does not touch client->frameBuffer.
    829 * @param client The client to clean up
    830 */
    831void rfbClientCleanup(rfbClient* client);
    832
    833#if(defined __cplusplus)
    834}
    835#endif
    836
    837/**
    838 * @}
    839 */
    840
    841/**
    842 @page libvncclient_doc LibVNCClient Documentation
    843 @section example_code Example Code
    844 See SDLvncviewer.c for a rather complete client example.
    845*/
    846
    847#endif