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
|
#include <rfb/rfb.h>
/**
* @example backchannel.c
* This is a simple example demonstrating a protocol extension.
*
* The "back channel" permits sending commands between client and server.
* It works by sending plain text messages.
*
* As suggested in the RFB protocol, the back channel is enabled by asking
* for a "pseudo encoding", and enabling the back channel on the client side
* as soon as it gets a back channel message from the server.
*
* This implements the server part.
*
* Note: If you design your own extension and want it to be useful for others,
* too, you should make sure that
*
* - your server as well as your client can speak to other clients and
* servers respectively (i.e. they are nice if they are talking to a
* program which does not know about your extension).
*
* - if the machine is little endian, all 16-bit and 32-bit integers are
* swapped before they are sent and after they are received.
*
*/
#define rfbBackChannel 155
typedef struct backChannelMsg {
uint8_t type;
uint8_t pad1;
uint16_t pad2;
uint32_t size;
} backChannelMsg;
rfbBool enableBackChannel(rfbClientPtr cl, void** data, int encoding)
{
if(encoding == rfbBackChannel) {
backChannelMsg msg;
const char* text="Server acknowledges back channel encoding\n";
uint32_t length = strlen(text)+1;
int n;
rfbLog("Enabling the back channel\n");
msg.type = rfbBackChannel;
msg.size = Swap32IfLE(length);
if((n = rfbWriteExact(cl, (char*)&msg, sizeof(msg))) <= 0 ||
(n = rfbWriteExact(cl, text, length)) <= 0) {
rfbLogPerror("enableBackChannel: write");
}
return TRUE;
}
return FALSE;
}
static rfbBool handleBackChannelMessage(rfbClientPtr cl, void* data,
const rfbClientToServerMsg* message)
{
if(message->type == rfbBackChannel) {
backChannelMsg msg;
char* text;
int n;
if((n = rfbReadExact(cl, ((char*)&msg)+1, sizeof(backChannelMsg)-1)) <= 0) {
if(n != 0)
rfbLogPerror("handleBackChannelMessage: read");
rfbCloseClient(cl);
return TRUE;
}
msg.size = Swap32IfLE(msg.size);
if((text = malloc(msg.size)) == NULL) {
rfbErr("Could not allocate %d bytes\n", msg.size);
return TRUE;
}
if((n = rfbReadExact(cl, text, msg.size)) <= 0) {
if(n != 0)
rfbLogPerror("handleBackChannelMessage: read");
rfbCloseClient(cl);
return TRUE;
}
rfbLog("got message:\n%s\n", text);
free(text);
return TRUE;
}
return FALSE;
}
static int backChannelEncodings[] = {rfbBackChannel, 0};
static rfbProtocolExtension backChannelExtension = {
NULL, /* newClient */
NULL, /* init */
backChannelEncodings, /* pseudoEncodings */
enableBackChannel, /* enablePseudoEncoding */
handleBackChannelMessage, /* handleMessage */
NULL, /* close */
NULL, /* usage */
NULL, /* processArgument */
NULL /* next extension */
};
int main(int argc,char** argv)
{
rfbScreenInfoPtr server;
rfbRegisterProtocolExtension(&backChannelExtension);
server=rfbGetScreen(&argc,argv,400,300,8,3,4);
if(!server)
return 0;
server->frameBuffer=(char*)malloc(400*300*4);
rfbInitServer(server);
rfbRunEventLoop(server,-1,FALSE);
return(0);
}
|