Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds lastError to rfbClientRec, adds 'error changed' callback #616

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
27 changes: 27 additions & 0 deletions src/libvncserver/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,35 @@ 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);

free(oldError);
}

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

void rfbLogPerror(const char *str)
{
Expand Down
5 changes: 4 additions & 1 deletion src/libvncserver/rfbserver.c
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,7 @@ rfbClientConnectionGone(rfbClientPtr cl)
rfbPrintStats(cl);
rfbResetStats(cl);

free(cl->lastError);
free(cl);
}

Expand Down Expand Up @@ -715,7 +716,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
Loading