Commit c1f6fddf authored by Rémi Denis-Courmont's avatar Rémi Denis-Courmont

remoteosd: remove racy b_connection_alive

parent fe5925db
...@@ -162,8 +162,6 @@ static void* vnc_worker_thread ( void * ); ...@@ -162,8 +162,6 @@ static void* vnc_worker_thread ( void * );
static void* update_request_thread( void * ); static void* update_request_thread( void * );
static bool handshaking ( filter_t *p_filter );
static bool process_server_message ( filter_t *p_filter, static bool process_server_message ( filter_t *p_filter,
rfbServerToClientMsg *msg ); rfbServerToClientMsg *msg );
...@@ -206,8 +204,6 @@ struct filter_sys_t ...@@ -206,8 +204,6 @@ struct filter_sys_t
bool b_vnc_mouse_events; /* Send MouseEvents ? */ bool b_vnc_mouse_events; /* Send MouseEvents ? */
bool b_vnc_key_events; /* Send KeyEvents ? */ bool b_vnc_key_events; /* Send KeyEvents ? */
bool b_connection_active; /* Handshaking finished ? */
vlc_mutex_t lock; /* To lock for read/write on picture */ vlc_mutex_t lock; /* To lock for read/write on picture */
picture_t *p_pic; /* The picture with OSD data from VNC */ picture_t *p_pic; /* The picture with OSD data from VNC */
...@@ -355,18 +351,26 @@ static bool write_exact( filter_t *p_filter, ...@@ -355,18 +351,26 @@ static bool write_exact( filter_t *p_filter,
return i_bytes == net_Write( p_filter, i_socket, p_writebuf, i_bytes ); return i_bytes == net_Write( p_filter, i_socket, p_writebuf, i_bytes );
} }
static bool handshaking ( filter_t *p_filter ) static int vnc_connect( filter_t *p_filter )
{ {
filter_sys_t *p_sys = p_filter->p_sys; filter_sys_t *p_sys = p_filter->p_sys;
int port = var_InheritInteger( p_filter, RMTOSD_CFG "port" );
int fd = net_ConnectTCP( p_filter, p_sys->psz_host, port );
if( fd == -1 )
{
msg_Err( p_filter, "Could not connect to VNC host" );
return -1;
}
msg_Dbg( p_filter, "Reading protocol version" ); msg_Dbg( p_filter, "Reading protocol version" );
rfbProtocolVersionMsg pv; rfbProtocolVersionMsg pv;
if ( !read_exact( p_filter, p_sys->i_socket, pv, if ( !read_exact( p_filter, fd, pv, sz_rfbProtocolVersionMsg ) )
sz_rfbProtocolVersionMsg ) )
{ {
msg_Err( p_filter, "Could not read version message" ); msg_Err( p_filter, "Could not read version message" );
return false; goto error;
} }
pv[sz_rfbProtocolVersionMsg] = '\0'; /* pv size is sz_rfbProtocolVersionMsg+1 */ pv[sz_rfbProtocolVersionMsg] = '\0'; /* pv size is sz_rfbProtocolVersionMsg+1 */
...@@ -374,19 +378,18 @@ static bool handshaking ( filter_t *p_filter ) ...@@ -374,19 +378,18 @@ static bool handshaking ( filter_t *p_filter )
strncpy(pv, "RFB 003.003\n", sz_rfbProtocolVersionMsg); strncpy(pv, "RFB 003.003\n", sz_rfbProtocolVersionMsg);
if( !write_exact(p_filter, p_sys->i_socket, pv, if( !write_exact(p_filter, fd, pv, sz_rfbProtocolVersionMsg) )
sz_rfbProtocolVersionMsg) )
{ {
msg_Err( p_filter, "Could not write version message" ); msg_Err( p_filter, "Could not write version message" );
return false; goto error;
} }
msg_Dbg( p_filter, "Reading authentication scheme" ); msg_Dbg( p_filter, "Reading authentication scheme" );
uint32_t i_authScheme; uint32_t i_authScheme;
if( !read_exact( p_filter, p_sys->i_socket, (char*)&i_authScheme, 4 ) ) if( !read_exact( p_filter, fd, (char*)&i_authScheme, 4 ) )
{ {
msg_Err( p_filter, "Could not read authentication scheme" ); msg_Err( p_filter, "Could not read authentication scheme" );
return false; goto error;
} }
i_authScheme = htonl(i_authScheme); i_authScheme = htonl(i_authScheme);
...@@ -394,57 +397,53 @@ static bool handshaking ( filter_t *p_filter ) ...@@ -394,57 +397,53 @@ static bool handshaking ( filter_t *p_filter )
if ( i_authScheme == rfbConnFailed ) if ( i_authScheme == rfbConnFailed )
{ {
msg_Err( p_filter, "Connection rejected by server" ); msg_Err( p_filter, "Connection rejected by server" );
return false; goto error;
} }
if (i_authScheme == rfbVncAuth) if (i_authScheme == rfbVncAuth)
{ {
unsigned char challenge[CHALLENGESIZE]; unsigned char challenge[CHALLENGESIZE];
if ( !read_exact( p_filter, p_sys->i_socket, if ( !read_exact( p_filter, fd, (char*)challenge, CHALLENGESIZE ) )
(char*)challenge, CHALLENGESIZE ) )
{ {
msg_Err( p_filter, "Could not read password challenge" ); msg_Err( p_filter, "Could not read password challenge" );
return false; goto error;
} }
vnc_encrypt_bytes( challenge, p_sys->psz_passwd ); vnc_encrypt_bytes( challenge, p_sys->psz_passwd );
if( !write_exact(p_filter, p_sys->i_socket, if( !write_exact(p_filter, fd, (char*)challenge, CHALLENGESIZE ) )
(char*)challenge, CHALLENGESIZE ) )
{ {
msg_Err( p_filter, "Could not write password" ); msg_Err( p_filter, "Could not write password" );
return false; goto error;
} }
uint32_t i_authResult; uint32_t i_authResult;
if( !read_exact( p_filter, p_sys->i_socket, (char*)&i_authResult, 4 ) ) if( !read_exact( p_filter, fd, (char*)&i_authResult, 4 ) )
{ {
msg_Err( p_filter, "Could not read authentication result" ); msg_Err( p_filter, "Could not read authentication result" );
return false; goto error;
} }
i_authResult = htonl(i_authResult); i_authResult = htonl(i_authResult);
if (i_authResult != rfbVncAuthOK) if (i_authResult != rfbVncAuthOK)
{ {
msg_Err( p_filter, "VNC authentication failed" ); msg_Err( p_filter, "VNC authentication failed" );
return false; goto error;
} }
} }
msg_Dbg( p_filter, "Writing client init message" ); msg_Dbg( p_filter, "Writing client init message" );
rfbClientInitMsg ci; rfbClientInitMsg ci;
ci.shared = 1; ci.shared = 1;
if( !write_exact( p_filter, p_sys->i_socket, if( !write_exact( p_filter, fd, (char*)&ci, sz_rfbClientInitMsg ) )
(char*)&ci, sz_rfbClientInitMsg ) )
{ {
msg_Err( p_filter, "Could not write client init message" ); msg_Err( p_filter, "Could not write client init message" );
return false; goto error;
} }
msg_Dbg( p_filter, "Reading server init message" ); msg_Dbg( p_filter, "Reading server init message" );
rfbServerInitMsg si; rfbServerInitMsg si;
if( !read_exact( p_filter, p_sys->i_socket, if( !read_exact( p_filter, fd, (char*)&si, sz_rfbServerInitMsg ) )
(char*)&si, sz_rfbServerInitMsg ) )
{ {
msg_Err( p_filter, "Could not read server init message" ); msg_Err( p_filter, "Could not read server init message" );
return false; goto error;
} }
si.framebufferWidth = htons(si.framebufferWidth); si.framebufferWidth = htons(si.framebufferWidth);
si.framebufferHeight = htons(si.framebufferHeight); si.framebufferHeight = htons(si.framebufferHeight);
...@@ -470,15 +469,15 @@ static bool handshaking ( filter_t *p_filter ) ...@@ -470,15 +469,15 @@ static bool handshaking ( filter_t *p_filter )
if( i_nameLength > MAX_VNC_SERVER_NAME_LENGTH ) if( i_nameLength > MAX_VNC_SERVER_NAME_LENGTH )
{ {
msg_Err( p_filter, "Server name too long" ); msg_Err( p_filter, "Server name too long" );
return false; goto error;
} }
char s_ServerName[MAX_VNC_SERVER_NAME_LENGTH+1]; char s_ServerName[MAX_VNC_SERVER_NAME_LENGTH+1];
msg_Dbg( p_filter, "Reading server name with size = %u", i_nameLength ); msg_Dbg( p_filter, "Reading server name with size = %u", i_nameLength );
if( !read_exact( p_filter, p_sys->i_socket, s_ServerName, i_nameLength ) ) if( !read_exact( p_filter, fd, s_ServerName, i_nameLength ) )
{ {
msg_Err( p_filter, "Could not read server name" ); msg_Err( p_filter, "Could not read server name" );
return false; goto error;
} }
s_ServerName[i_nameLength] = '\0'; s_ServerName[i_nameLength] = '\0';
...@@ -514,11 +513,10 @@ static bool handshaking ( filter_t *p_filter ) ...@@ -514,11 +513,10 @@ static bool handshaking ( filter_t *p_filter )
sp.format.blueShift = 0; sp.format.blueShift = 0;
sp.format.pad1 = sp.format.pad2 = 0; sp.format.pad1 = sp.format.pad2 = 0;
if( !write_exact( p_filter, p_sys->i_socket, if( !write_exact( p_filter, fd, (char*)&sp, sz_rfbSetPixelFormatMsg) )
(char*)&sp, sz_rfbSetPixelFormatMsg) )
{ {
msg_Err( p_filter, "Could not write SetPixelFormat message" ); msg_Err( p_filter, "Could not write SetPixelFormat message" );
return false; goto error;
} }
msg_Dbg( p_filter, "Writing SetEncodings message" ); msg_Dbg( p_filter, "Writing SetEncodings message" );
...@@ -528,29 +526,28 @@ static bool handshaking ( filter_t *p_filter ) ...@@ -528,29 +526,28 @@ static bool handshaking ( filter_t *p_filter )
se.pad = 0; se.pad = 0;
se.nEncodings = htons( p_sys->b_alpha_from_vnc ? 3 : 2 ); se.nEncodings = htons( p_sys->b_alpha_from_vnc ? 3 : 2 );
if( !write_exact( p_filter, p_sys->i_socket, if( !write_exact( p_filter, fd, (char*)&se, sz_rfbSetEncodingsMsg) )
(char*)&se, sz_rfbSetEncodingsMsg) )
{ {
msg_Err( p_filter, "Could not write SetEncodings message begin" ); msg_Err( p_filter, "Could not write SetEncodings message begin" );
return false; goto error;
} }
uint32_t i_encoding; uint32_t i_encoding;
msg_Dbg( p_filter, "Writing SetEncodings rfbEncodingCopyRect" ); msg_Dbg( p_filter, "Writing SetEncodings rfbEncodingCopyRect" );
i_encoding = htonl(rfbEncodingCopyRect); i_encoding = htonl(rfbEncodingCopyRect);
if( !write_exact( p_filter, p_sys->i_socket, (char*)&i_encoding, 4) ) if( !write_exact( p_filter, fd, (char*)&i_encoding, 4) )
{ {
msg_Err( p_filter, "Could not write encoding type rfbEncodingCopyRect." ); msg_Err( p_filter, "Could not write encoding type rfbEncodingCopyRect." );
return false; goto error;
} }
msg_Dbg( p_filter, "Writing SetEncodings rfbEncodingRRE" ); msg_Dbg( p_filter, "Writing SetEncodings rfbEncodingRRE" );
i_encoding = htonl(rfbEncodingRRE); i_encoding = htonl(rfbEncodingRRE);
if( !write_exact(p_filter, p_sys->i_socket, (char*)&i_encoding, 4) ) if( !write_exact(p_filter, fd, (char*)&i_encoding, 4) )
{ {
msg_Err( p_filter, "Could not write encoding type rfbEncodingRRE." ); msg_Err( p_filter, "Could not write encoding type rfbEncodingRRE." );
return false; goto error;
} }
if( p_sys->b_alpha_from_vnc ) if( p_sys->b_alpha_from_vnc )
...@@ -558,14 +555,17 @@ static bool handshaking ( filter_t *p_filter ) ...@@ -558,14 +555,17 @@ static bool handshaking ( filter_t *p_filter )
msg_Dbg( p_filter, "Writing SetEncodings rfbEncSpecialUseAlpha" ); msg_Dbg( p_filter, "Writing SetEncodings rfbEncSpecialUseAlpha" );
i_encoding = 0x00F0FFFF; /* rfbEncSpecialUseAlpha is 0xFFFFF000 i_encoding = 0x00F0FFFF; /* rfbEncSpecialUseAlpha is 0xFFFFF000
* before we swap it */ * before we swap it */
if( !write_exact(p_filter, p_sys->i_socket, (char*)&i_encoding, 4) ) if( !write_exact(p_filter, fd, (char*)&i_encoding, 4) )
{ {
msg_Err( p_filter, "Could not write encoding type rfbEncSpecialUseAlpha." ); msg_Err( p_filter, "Could not write encoding type rfbEncSpecialUseAlpha." );
return false; goto error;
} }
} }
return true;
return fd;
error:
net_Close( fd );
return -1;
} }
static int write_update_request(filter_t *p_filter, bool incremental) static int write_update_request(filter_t *p_filter, bool incremental)
...@@ -596,26 +596,16 @@ static void* vnc_worker_thread( void *obj ) ...@@ -596,26 +596,16 @@ static void* vnc_worker_thread( void *obj )
msg_Dbg( p_filter, "VNC worker thread started" ); msg_Dbg( p_filter, "VNC worker thread started" );
int i_port = var_InheritInteger( p_filter, RMTOSD_CFG "port" ); int fd = vnc_connect( p_filter );
if( fd == -1 )
p_sys->i_socket = net_ConnectTCP( p_filter, p_sys->psz_host, i_port );
if( p_sys->i_socket == -1 )
{ {
msg_Err( p_filter, "Could not connect to VNC host" ); msg_Err( p_filter, "Error occurred while handshaking VNC host" );
return NULL; return NULL;
} }
if( !handshaking ( p_filter ) )
{
msg_Err( p_filter, "Error occurred while handshaking vnc host" );
goto exit;
}
p_sys->b_connection_active = true; /* to enable sending key
* and mouse events to host */
/* Create an empty picture for VNC the data */ /* Create an empty picture for VNC the data */
vlc_mutex_lock( &p_sys->lock ); vlc_mutex_lock( &p_sys->lock );
p_sys->i_socket = fd;
p_sys->p_pic = picture_New( VLC_CODEC_YUVA, p_sys->p_pic = picture_New( VLC_CODEC_YUVA,
p_sys->i_vnc_width, p_sys->i_vnc_height, 1, 1 ); p_sys->i_vnc_width, p_sys->i_vnc_height, 1, 1 );
vlc_mutex_unlock( &p_sys->lock ); vlc_mutex_unlock( &p_sys->lock );
...@@ -645,7 +635,7 @@ static void* vnc_worker_thread( void *obj ) ...@@ -645,7 +635,7 @@ static void* vnc_worker_thread( void *obj )
memset( &msg, 0, sizeof(msg) ); memset( &msg, 0, sizeof(msg) );
if( !read_exact(p_filter, p_sys->i_socket, (char*)&msg, 1 ) ) if( !read_exact(p_filter, fd, (char*)&msg, 1 ) )
{ {
msg_Err( p_filter, "Error while waiting for next server message"); msg_Err( p_filter, "Error while waiting for next server message");
break; break;
...@@ -678,8 +668,7 @@ static void* vnc_worker_thread( void *obj ) ...@@ -678,8 +668,7 @@ static void* vnc_worker_thread( void *obj )
if( --i_msgSize > 0 ) if( --i_msgSize > 0 )
{ {
if ( !read_exact( p_filter, p_sys->i_socket, if ( !read_exact( p_filter, fd, ((char*)&msg)+1, i_msgSize ) )
((char*)&msg)+1, i_msgSize ) )
{ {
msg_Err( p_filter, "Error while reading message of type %u", msg_Err( p_filter, "Error while reading message of type %u",
msg.type ); msg.type );
...@@ -702,15 +691,14 @@ static void* vnc_worker_thread( void *obj ) ...@@ -702,15 +691,14 @@ static void* vnc_worker_thread( void *obj )
exit: exit:
vlc_mutex_lock( &p_sys->lock ); vlc_mutex_lock( &p_sys->lock );
p_sys->b_connection_active = false; p_sys->i_socket = -1;
net_Close(p_sys->i_socket);
if( p_sys->p_pic ) if( p_sys->p_pic )
picture_Release( p_sys->p_pic ); picture_Release( p_sys->p_pic );
p_sys->b_need_update = true; p_sys->b_need_update = true;
vlc_mutex_unlock( &p_sys->lock ); vlc_mutex_unlock( &p_sys->lock );
net_Close( fd );
msg_Dbg( p_filter, "VNC message reader thread ended" ); msg_Dbg( p_filter, "VNC message reader thread ended" );
vlc_restorecancel (canc); vlc_restorecancel (canc);
...@@ -1235,7 +1223,8 @@ static int MouseEvent( filter_t *p_filter, ...@@ -1235,7 +1223,8 @@ static int MouseEvent( filter_t *p_filter,
msg_Dbg( p_filter, "invalid mouse event? x=%d y=%d btn=%x", i_x, i_y, i_v ); msg_Dbg( p_filter, "invalid mouse event? x=%d y=%d btn=%x", i_x, i_y, i_v );
return VLC_SUCCESS; return VLC_SUCCESS;
} }
if( !p_sys->b_connection_active )
if( p_sys->i_socket == -1 )
{ {
vlc_mutex_unlock( &p_sys->lock ); vlc_mutex_unlock( &p_sys->lock );
return VLC_SUCCESS; return VLC_SUCCESS;
...@@ -1258,7 +1247,6 @@ static int MouseEvent( filter_t *p_filter, ...@@ -1258,7 +1247,6 @@ static int MouseEvent( filter_t *p_filter,
write_exact( p_filter, p_sys->i_socket, write_exact( p_filter, p_sys->i_socket,
(char*)&ev, sz_rfbPointerEventMsg); (char*)&ev, sz_rfbPointerEventMsg);
vlc_mutex_unlock( &p_sys->lock ); vlc_mutex_unlock( &p_sys->lock );
return VLC_EGENERIC; return VLC_EGENERIC;
...@@ -1287,7 +1275,7 @@ static int KeyEvent( vlc_object_t *p_this, char const *psz_var, ...@@ -1287,7 +1275,7 @@ static int KeyEvent( vlc_object_t *p_this, char const *psz_var,
} }
vlc_mutex_lock( &p_sys->lock ); vlc_mutex_lock( &p_sys->lock );
if( !p_sys->b_connection_active ) if( p_sys->i_socket == -1 )
{ {
vlc_mutex_unlock( &p_sys->lock ); vlc_mutex_unlock( &p_sys->lock );
return VLC_SUCCESS; return VLC_SUCCESS;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment