mirror of
git://xwords.git.sourceforge.net/gitroot/xwords/xwords
synced 2025-02-06 20:45:54 +01:00
return error to client when flags indicate old version; close sockets
when error occurs in processing message.
This commit is contained in:
parent
6541e56c40
commit
eb8301ade7
4 changed files with 54 additions and 32 deletions
|
@ -165,9 +165,9 @@ XWThreadPool::get_process_packet( int socket )
|
||||||
logf( XW_LOGINFO, "read %d bytes\n", nRead );
|
logf( XW_LOGINFO, "read %d bytes\n", nRead );
|
||||||
|
|
||||||
logf( XW_LOGINFO, "calling m_pFunc" );
|
logf( XW_LOGINFO, "calling m_pFunc" );
|
||||||
(*m_pFunc)( buf, packetSize, socket );
|
int success = (*m_pFunc)( buf, packetSize, socket );
|
||||||
|
|
||||||
return 1;
|
return success;
|
||||||
} /* get_process_packet */
|
} /* get_process_packet */
|
||||||
|
|
||||||
/* static */ void*
|
/* static */ void*
|
||||||
|
@ -244,6 +244,7 @@ XWThreadPool::real_listener()
|
||||||
|
|
||||||
int nMillis = tmgr->getPollTimeout();
|
int nMillis = tmgr->getPollTimeout();
|
||||||
|
|
||||||
|
logf( XW_LOGINFO, "polling %s", log );
|
||||||
int nEvents = poll( fds, nSockets, nMillis ); /* -1: infinite timeout */
|
int nEvents = poll( fds, nSockets, nMillis ); /* -1: infinite timeout */
|
||||||
logf( XW_LOGINFO, "back from poll: %d", nEvents );
|
logf( XW_LOGINFO, "back from poll: %d", nEvents );
|
||||||
if ( nEvents == 0 ) {
|
if ( nEvents == 0 ) {
|
||||||
|
|
|
@ -35,8 +35,8 @@ class XWThreadPool {
|
||||||
|
|
||||||
public:
|
public:
|
||||||
static XWThreadPool* GetTPool();
|
static XWThreadPool* GetTPool();
|
||||||
typedef void (*packet_func)( unsigned char* buf,
|
typedef int (*packet_func)( unsigned char* buf, int bufLen, int socket );
|
||||||
int bufLen, int socket );
|
|
||||||
XWThreadPool();
|
XWThreadPool();
|
||||||
~XWThreadPool();
|
~XWThreadPool();
|
||||||
|
|
||||||
|
|
|
@ -108,12 +108,13 @@ getNetByte( unsigned char** bufpp, unsigned char* end, unsigned char* out )
|
||||||
return ok;
|
return ok;
|
||||||
} /* getNetByte */
|
} /* getNetByte */
|
||||||
|
|
||||||
static void
|
static int
|
||||||
processHeartbeat( unsigned char* buf, int bufLen, int socket )
|
processHeartbeat( unsigned char* buf, int bufLen, int socket )
|
||||||
{
|
{
|
||||||
unsigned char* end = buf + bufLen;
|
unsigned char* end = buf + bufLen;
|
||||||
CookieID cookieID;
|
CookieID cookieID;
|
||||||
HostID hostID;
|
HostID hostID;
|
||||||
|
int success = 0;
|
||||||
|
|
||||||
if ( getNetShort( &buf, end, &cookieID )
|
if ( getNetShort( &buf, end, &cookieID )
|
||||||
&& getNetByte( &buf, end, &hostID ) ) {
|
&& getNetByte( &buf, end, &hostID ) ) {
|
||||||
|
@ -121,10 +122,12 @@ processHeartbeat( unsigned char* buf, int bufLen, int socket )
|
||||||
cookieID, hostID );
|
cookieID, hostID );
|
||||||
|
|
||||||
SafeCref scr( cookieID );
|
SafeCref scr( cookieID );
|
||||||
if ( !scr.HandleHeartbeat( hostID, socket ) ) {
|
success = scr.HandleHeartbeat( hostID, socket );
|
||||||
|
if ( !success ) {
|
||||||
killSocket( socket, "no cref for socket" );
|
killSocket( socket, "no cref for socket" );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return success;
|
||||||
} /* processHeartbeat */
|
} /* processHeartbeat */
|
||||||
|
|
||||||
static int
|
static int
|
||||||
|
@ -142,19 +145,20 @@ readStr( unsigned char** bufp, const unsigned char* end,
|
||||||
return 0;
|
return 0;
|
||||||
} /* readStr */
|
} /* readStr */
|
||||||
|
|
||||||
static int
|
static XWREASON
|
||||||
flagsOK( unsigned char flags )
|
flagsOK( unsigned char flags )
|
||||||
{
|
{
|
||||||
return flags == XWRELAY_PROTO_VERSION;
|
return flags == XWRELAY_PROTO_VERSION ?
|
||||||
|
XWRELAY_ERROR_NONE : XWRELAY_ERROR_OLDFLAGS;
|
||||||
} /* flagsOK */
|
} /* flagsOK */
|
||||||
|
|
||||||
static void
|
static void
|
||||||
denyConnection( int socket )
|
denyConnection( int socket, XWREASON err )
|
||||||
{
|
{
|
||||||
unsigned char buf[2];
|
unsigned char buf[2];
|
||||||
|
|
||||||
buf[0] = XWRELAY_CONNECTDENIED;
|
buf[0] = XWRELAY_CONNECTDENIED;
|
||||||
buf[1] = XWRELAY_ERROR_BADPROTO;
|
buf[1] = err;
|
||||||
|
|
||||||
send_with_length_unsafe( socket, buf, sizeof(buf) );
|
send_with_length_unsafe( socket, buf, sizeof(buf) );
|
||||||
}
|
}
|
||||||
|
@ -189,7 +193,7 @@ send_with_length_unsafe( int socket, unsigned char* buf, int bufLen )
|
||||||
* outstanding. Otherwise close down the socket. And maybe the others in the
|
* outstanding. Otherwise close down the socket. And maybe the others in the
|
||||||
* game?
|
* game?
|
||||||
*/
|
*/
|
||||||
static void
|
static int
|
||||||
processConnect( unsigned char* bufp, int bufLen, int socket )
|
processConnect( unsigned char* bufp, int bufLen, int socket )
|
||||||
{
|
{
|
||||||
char cookie[MAX_COOKIE_LEN+1];
|
char cookie[MAX_COOKIE_LEN+1];
|
||||||
|
@ -201,7 +205,10 @@ processConnect( unsigned char* bufp, int bufLen, int socket )
|
||||||
cookie[0] = '\0';
|
cookie[0] = '\0';
|
||||||
|
|
||||||
unsigned char flags = *bufp++;
|
unsigned char flags = *bufp++;
|
||||||
if ( flagsOK( flags ) ) {
|
XWREASON err = flagsOK( flags );
|
||||||
|
if ( err != XWRELAY_ERROR_NONE ) {
|
||||||
|
denyConnection( socket, err );
|
||||||
|
} else {
|
||||||
HostID srcID;
|
HostID srcID;
|
||||||
unsigned char nPlayersH;
|
unsigned char nPlayersH;
|
||||||
unsigned char nPlayersT;
|
unsigned char nPlayersT;
|
||||||
|
@ -213,29 +220,33 @@ processConnect( unsigned char* bufp, int bufLen, int socket )
|
||||||
SafeCref scr( cookie, 1, srcID, nPlayersH, nPlayersT );
|
SafeCref scr( cookie, 1, srcID, nPlayersH, nPlayersT );
|
||||||
success = scr.Connect( socket, srcID, nPlayersH, nPlayersT );
|
success = scr.Connect( socket, srcID, nPlayersH, nPlayersT );
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if ( !success ) {
|
if ( !success ) {
|
||||||
denyConnection( socket );
|
denyConnection( socket, XWRELAY_ERROR_BADPROTO );
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
return success;
|
||||||
} /* processConnect */
|
} /* processConnect */
|
||||||
|
|
||||||
static void
|
static int
|
||||||
processReconnect( unsigned char* bufp, int bufLen, int socket )
|
processReconnect( unsigned char* bufp, int bufLen, int socket )
|
||||||
{
|
{
|
||||||
char connName[MAX_CONNNAME_LEN+1];
|
|
||||||
unsigned char* end = bufp + bufLen;
|
unsigned char* end = bufp + bufLen;
|
||||||
int success = 0;
|
int success = 0;
|
||||||
|
|
||||||
logf( XW_LOGINFO, "processReconnect" );
|
logf( XW_LOGINFO, "processReconnect" );
|
||||||
|
|
||||||
connName[0] = '\0';
|
|
||||||
|
|
||||||
unsigned char flags = *bufp++;
|
unsigned char flags = *bufp++;
|
||||||
if ( flagsOK( flags ) ) {
|
XWREASON err = flagsOK( flags );
|
||||||
|
if ( err != XWRELAY_ERROR_NONE ) {
|
||||||
|
denyConnection( socket, err );
|
||||||
|
} else {
|
||||||
|
char connName[MAX_CONNNAME_LEN+1];
|
||||||
HostID srcID;
|
HostID srcID;
|
||||||
unsigned char nPlayersH;
|
unsigned char nPlayersH;
|
||||||
unsigned char nPlayersT;
|
unsigned char nPlayersT;
|
||||||
|
|
||||||
|
connName[0] = '\0';
|
||||||
if ( getNetByte( &bufp, end, &srcID )
|
if ( getNetByte( &bufp, end, &srcID )
|
||||||
&& getNetByte( &bufp, end, &nPlayersH )
|
&& getNetByte( &bufp, end, &nPlayersH )
|
||||||
&& getNetByte( &bufp, end, &nPlayersT )
|
&& getNetByte( &bufp, end, &nPlayersT )
|
||||||
|
@ -244,28 +255,32 @@ processReconnect( unsigned char* bufp, int bufLen, int socket )
|
||||||
SafeCref scr( connName, 0, srcID, nPlayersH, nPlayersT );
|
SafeCref scr( connName, 0, srcID, nPlayersH, nPlayersT );
|
||||||
success = scr.Reconnect( socket, srcID, nPlayersH, nPlayersT );
|
success = scr.Reconnect( socket, srcID, nPlayersH, nPlayersT );
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if ( !success ) {
|
if ( !success ) {
|
||||||
denyConnection( socket );
|
denyConnection( socket, XWRELAY_ERROR_BADPROTO );
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
return success;
|
||||||
} /* processReconnect */
|
} /* processReconnect */
|
||||||
|
|
||||||
static void
|
static int
|
||||||
processDisconnect( unsigned char* bufp, int bufLen, int socket )
|
processDisconnect( unsigned char* bufp, int bufLen, int socket )
|
||||||
{
|
{
|
||||||
unsigned char* end = bufp + bufLen;
|
unsigned char* end = bufp + bufLen;
|
||||||
CookieID cookieID;
|
CookieID cookieID;
|
||||||
HostID hostID;
|
HostID hostID;
|
||||||
|
int success = 0;
|
||||||
|
|
||||||
if ( getNetShort( &bufp, end, &cookieID )
|
if ( getNetShort( &bufp, end, &cookieID )
|
||||||
&& getNetByte( &bufp, end, &hostID ) ) {
|
&& getNetByte( &bufp, end, &hostID ) ) {
|
||||||
|
|
||||||
SafeCref scr( cookieID );
|
SafeCref scr( cookieID );
|
||||||
scr.Disconnect( socket, hostID );
|
scr.Disconnect( socket, hostID );
|
||||||
|
success = 1;
|
||||||
} else {
|
} else {
|
||||||
logf( XW_LOGERROR, "dropping XWRELAY_GAME_DISCONNECT; wrong length" );
|
logf( XW_LOGERROR, "dropping XWRELAY_GAME_DISCONNECT; wrong length" );
|
||||||
}
|
}
|
||||||
|
return success;
|
||||||
} /* processDisconnect */
|
} /* processDisconnect */
|
||||||
|
|
||||||
void
|
void
|
||||||
|
@ -308,37 +323,42 @@ forwardMessage( unsigned char* buf, int buflen, int srcSocket )
|
||||||
return success;
|
return success;
|
||||||
} /* forwardMessage */
|
} /* forwardMessage */
|
||||||
|
|
||||||
static void
|
static int
|
||||||
processMessage( unsigned char* buf, int bufLen, int socket )
|
processMessage( unsigned char* buf, int bufLen, int socket )
|
||||||
{
|
{
|
||||||
|
int success = 0; /* default is failure */
|
||||||
XWRELAY_Cmd cmd = *buf;
|
XWRELAY_Cmd cmd = *buf;
|
||||||
switch( cmd ) {
|
switch( cmd ) {
|
||||||
case XWRELAY_GAME_CONNECT:
|
case XWRELAY_GAME_CONNECT:
|
||||||
logf( XW_LOGINFO, "processMessage got XWRELAY_CONNECT" );
|
logf( XW_LOGINFO, "processMessage got XWRELAY_CONNECT" );
|
||||||
processConnect( buf+1, bufLen-1, socket );
|
success = processConnect( buf+1, bufLen-1, socket );
|
||||||
break;
|
break;
|
||||||
case XWRELAY_GAME_RECONNECT:
|
case XWRELAY_GAME_RECONNECT:
|
||||||
logf( XW_LOGINFO, "processMessage got XWRELAY_RECONNECT" );
|
logf( XW_LOGINFO, "processMessage got XWRELAY_RECONNECT" );
|
||||||
processReconnect( buf+1, bufLen-1, socket );
|
success = processReconnect( buf+1, bufLen-1, socket );
|
||||||
break;
|
break;
|
||||||
case XWRELAY_GAME_DISCONNECT:
|
case XWRELAY_GAME_DISCONNECT:
|
||||||
processDisconnect( buf+1, bufLen-1, socket );
|
success = processDisconnect( buf+1, bufLen-1, socket );
|
||||||
break;
|
break;
|
||||||
case XWRELAY_HEARTBEAT:
|
case XWRELAY_HEARTBEAT:
|
||||||
logf( XW_LOGINFO, "processMessage got XWRELAY_HEARTBEAT" );
|
logf( XW_LOGINFO, "processMessage got XWRELAY_HEARTBEAT" );
|
||||||
processHeartbeat( buf + 1, bufLen - 1, socket );
|
success = processHeartbeat( buf + 1, bufLen - 1, socket );
|
||||||
break;
|
break;
|
||||||
case XWRELAY_MSG_TORELAY:
|
case XWRELAY_MSG_TORELAY:
|
||||||
logf( XW_LOGINFO, "processMessage got XWRELAY_MSG_TORELAY" );
|
logf( XW_LOGINFO, "processMessage got XWRELAY_MSG_TORELAY" );
|
||||||
if ( !forwardMessage( buf, bufLen, socket ) ) {
|
success = forwardMessage( buf, bufLen, socket );
|
||||||
killSocket( socket, "couldn't forward message" );
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
logf( XW_LOGINFO, "processMessage bad: %d", cmd );
|
logf( XW_LOGINFO, "processMessage bad: %d", cmd );
|
||||||
break;
|
break;
|
||||||
/* just drop it */
|
/* just drop it */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ( !success ) {
|
||||||
|
killSocket( socket, "couldn't forward message" );
|
||||||
|
}
|
||||||
|
|
||||||
|
return success; /* caller defines non-0 as failure */
|
||||||
} /* processMessage */
|
} /* processMessage */
|
||||||
|
|
||||||
static int
|
static int
|
||||||
|
|
|
@ -109,6 +109,7 @@ typedef
|
||||||
#endif
|
#endif
|
||||||
enum {
|
enum {
|
||||||
XWRELAY_ERROR_NONE
|
XWRELAY_ERROR_NONE
|
||||||
|
,XWRELAY_ERROR_OLDFLAGS /* You have the wrong flags */
|
||||||
,XWRELAY_ERROR_BADPROTO
|
,XWRELAY_ERROR_BADPROTO
|
||||||
,XWRELAY_ERROR_RELAYBUSY
|
,XWRELAY_ERROR_RELAYBUSY
|
||||||
,XWRELAY_ERROR_SHUTDOWN /* relay's going down */
|
,XWRELAY_ERROR_SHUTDOWN /* relay's going down */
|
||||||
|
|
Loading…
Add table
Reference in a new issue