Skip to content

Commit

Permalink
Adds lastError to rfbClientRec, adds 'error changed' callback
Browse files Browse the repository at this point in the history
New rfbClientSetErr() function is only being called in auth.c, when simple password authentication fails.
  • Loading branch information
nicmorais committed May 12, 2024
1 parent b44665c commit fa40867
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
14 changes: 14 additions & 0 deletions include/rfb/rfb.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ typedef int (*rfbFileTransferPermitted) (struct _rfbClientRec* cl);
/** Handle the textchat messages */
typedef void (*rfbSetTextChat) (struct _rfbClientRec* cl, int length, char *string);

/* error handling (server side) */
typedef void (*ClientErrorChangedProc)(struct _rfbClientRec *client);

typedef struct {
uint32_t count;
rfbBool is16; /**< is the data format short? */
Expand Down Expand Up @@ -302,6 +305,8 @@ typedef struct _rfbScreenInfo
rfbFileTransferPermitted getFileTransferPermission;
rfbSetTextChat setTextChat;

ClientErrorChangedProc clientErrorChanged;

/** newClientHook is called just after a new client is created */
rfbNewClientHookPtr newClientHook;
/** displayHook is called just before a frame buffer update */
Expand Down Expand Up @@ -440,6 +445,13 @@ typedef struct _rfbClientRec {
* This is useful if the IO functions have to behave client specific.
*/
void* clientData;

/**
* error handling
*/

char* lastError;

ClientGoneHookPtr clientGoneHook;

rfbSocket sock;
Expand Down Expand Up @@ -1017,6 +1029,8 @@ extern rfbBool rfbProcessSizeArguments(int* width,int* height,int* bpp,int* argc

extern void rfbLogEnable(int enabled);
typedef void (*rfbLogProc)(const char *format, ...);
typedef void (*rfbClientSetErrProc)(rfbClientPtr cl, const char *format, ...);
extern rfbClientSetErrProc rfbClientSetErr;
extern rfbLogProc rfbLog, rfbErr;
extern void rfbLogPerror(const char *str);

Expand Down
1 change: 1 addition & 0 deletions src/libvncserver/auth.c
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,7 @@ rfbAuthProcessClientMessage(rfbClientPtr cl)

if(!cl->screen->passwordCheck(cl,(const char*)response,CHALLENGESIZE)) {
rfbErr("rfbAuthProcessClientMessage: password check failed\n");
rfbClientSetErr(cl, "rfbAuthProcessClientMessage: password check failed");
authResult = Swap32IfLE(rfbVncAuthFailed);
if (rfbWriteExact(cl, (char *)&authResult, 4) < 0) {
rfbLogPerror("rfbAuthProcessClientMessage: write");
Expand Down
28 changes: 28 additions & 0 deletions src/libvncserver/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,36 @@ rfbDefaultLog(const char *format, ...)
UNLOCK(logMutex);
}

static void rfbDefaultClientSetErr(rfbClientPtr cl, const char *format, ...) {
// props to josemr goudetalvim (@josealvim) for this logic
va_list ap;
va_start(ap, format);
size_t len = (size_t)vsnprintf(NULL, 0, format, ap);
va_end(ap);

char *buffer = malloc(len + 1);
if (!buffer)
return;

va_start(ap, format);
vsprintf(buffer, format, ap);

va_end(ap);

char *oldError = cl->lastError;
cl->lastError = buffer;

// call the callback if it exists
if (cl->screen->clientErrorChanged != NULL)
cl->screen->clientErrorChanged(cl);

if (oldError)
free(oldError);
}

rfbLogProc rfbLog=rfbDefaultLog;
rfbLogProc rfbErr=rfbDefaultLog;
rfbClientSetErrProc rfbClientSetErr = rfbDefaultClientSetErr;

void rfbLogPerror(const char *str)
{
Expand Down
4 changes: 3 additions & 1 deletion src/libvncserver/rfbserver.c
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,9 @@ rfbProcessClientProtocolVersion(rfbClientPtr cl)
pv[sz_rfbProtocolVersionMsg] = 0;
if (sscanf(pv,rfbProtocolVersionFormat,&major_,&minor_) != 2) {
rfbErr("rfbProcessClientProtocolVersion: not a valid RFB client: %s\n", pv);
rfbCloseClient(cl);
rfbClientSetErr(
cl, "rfbProcessClientProtocolVersion: not a valid RFB client: %s\n", pv);
rfbCloseClient(cl);
return;
}
rfbLog("Client Protocol Version %d.%d\n", major_, minor_);
Expand Down

0 comments on commit fa40867

Please sign in to comment.