2013-01-16 15:46:33 +01:00
|
|
|
/* -*- compile-command: "make MEMDEBUG=TRUE -j3"; -*- */
|
|
|
|
/*
|
|
|
|
* Copyright 2013 by Eric House (xwords@eehouse.org). All rights reserved.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <errno.h>
|
2013-01-23 16:43:58 +01:00
|
|
|
#include <stdbool.h>
|
2017-10-18 06:32:11 +02:00
|
|
|
#include <curl/curl.h>
|
|
|
|
#include <json-c/json.h>
|
|
|
|
|
2013-01-16 15:46:33 +01:00
|
|
|
|
|
|
|
#include "relaycon.h"
|
2015-07-01 16:10:45 +02:00
|
|
|
#include "linuxmain.h"
|
2013-01-16 15:46:33 +01:00
|
|
|
#include "comtypes.h"
|
|
|
|
|
|
|
|
typedef struct _RelayConStorage {
|
|
|
|
int socket;
|
|
|
|
RelayConnProcs procs;
|
|
|
|
void* procsClosure;
|
2013-01-19 23:37:49 +01:00
|
|
|
struct sockaddr_in saddr;
|
2013-07-20 20:57:10 +02:00
|
|
|
uint32_t nextID;
|
2013-08-25 21:34:35 +02:00
|
|
|
XWPDevProto proto;
|
2015-07-01 16:10:45 +02:00
|
|
|
LaunchParams* params;
|
2013-01-16 15:46:33 +01:00
|
|
|
} RelayConStorage;
|
|
|
|
|
2013-01-23 16:43:58 +01:00
|
|
|
typedef struct _MsgHeader {
|
|
|
|
XWRelayReg cmd;
|
|
|
|
uint32_t packetID;
|
|
|
|
} MsgHeader;
|
|
|
|
|
2013-01-16 15:46:33 +01:00
|
|
|
static RelayConStorage* getStorage( LaunchParams* params );
|
|
|
|
static XP_U32 hostNameToIP( const XP_UCHAR* name );
|
2014-11-05 16:41:20 +01:00
|
|
|
static gboolean relaycon_receive( GIOChannel *source, GIOCondition condition,
|
|
|
|
gpointer data );
|
2013-01-19 23:37:49 +01:00
|
|
|
static ssize_t sendIt( RelayConStorage* storage, const XP_U8* msgbuf, XP_U16 len );
|
2013-08-26 06:20:26 +02:00
|
|
|
static size_t addVLIStr( XP_U8* buf, size_t len, const XP_UCHAR* str );
|
2013-01-23 16:43:58 +01:00
|
|
|
static void getNetString( const XP_U8** ptr, XP_U16 len, XP_UCHAR* buf );
|
|
|
|
static XP_U16 getNetShort( const XP_U8** ptr );
|
2013-01-29 16:42:10 +01:00
|
|
|
static XP_U32 getNetLong( const XP_U8** ptr );
|
2013-07-20 20:57:10 +02:00
|
|
|
static int writeHeader( RelayConStorage* storage, XP_U8* dest, XWRelayReg cmd );
|
2013-01-23 16:43:58 +01:00
|
|
|
static bool readHeader( const XP_U8** buf, MsgHeader* header );
|
2013-01-29 16:42:10 +01:00
|
|
|
static size_t writeDevID( XP_U8* buf, size_t len, const XP_UCHAR* str );
|
2013-08-20 17:09:45 +02:00
|
|
|
static size_t writeShort( XP_U8* buf, size_t len, XP_U16 shrt );
|
2015-07-01 16:10:45 +02:00
|
|
|
static size_t writeLong( XP_U8* buf, size_t len, XP_U32 lng );
|
2015-07-07 05:28:16 +02:00
|
|
|
static size_t writeBytes( XP_U8* buf, size_t len, const XP_U8* bytes,
|
|
|
|
size_t nBytes );
|
2013-08-26 01:59:07 +02:00
|
|
|
static size_t writeVLI( XP_U8* out, uint32_t nn );
|
2013-08-25 21:34:35 +02:00
|
|
|
static size_t un2vli( int nn, uint8_t* buf );
|
2013-08-26 01:14:42 +02:00
|
|
|
static bool vli2un( const uint8_t** inp, uint32_t* outp );
|
2013-08-25 21:34:35 +02:00
|
|
|
|
2013-01-16 15:46:33 +01:00
|
|
|
|
|
|
|
void
|
|
|
|
relaycon_init( LaunchParams* params, const RelayConnProcs* procs,
|
2013-01-19 23:37:49 +01:00
|
|
|
void* procsClosure, const char* host, int port )
|
2013-01-16 15:46:33 +01:00
|
|
|
{
|
|
|
|
XP_ASSERT( !params->relayConStorage );
|
|
|
|
RelayConStorage* storage = getStorage( params );
|
|
|
|
XP_MEMCPY( &storage->procs, procs, sizeof(storage->procs) );
|
|
|
|
storage->procsClosure = procsClosure;
|
|
|
|
|
|
|
|
storage->socket = socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP );
|
2014-11-05 16:41:20 +01:00
|
|
|
(*procs->socketAdded)( storage, storage->socket, relaycon_receive );
|
2013-01-16 15:46:33 +01:00
|
|
|
|
2013-01-19 23:37:49 +01:00
|
|
|
XP_MEMSET( &storage->saddr, 0, sizeof(storage->saddr) );
|
|
|
|
storage->saddr.sin_family = PF_INET;
|
|
|
|
storage->saddr.sin_addr.s_addr = htonl( hostNameToIP(host) );
|
|
|
|
storage->saddr.sin_port = htons(port);
|
2013-08-25 21:34:35 +02:00
|
|
|
|
2015-07-01 16:10:45 +02:00
|
|
|
storage->params = params;
|
|
|
|
|
2013-08-25 21:34:35 +02:00
|
|
|
storage->proto = XWPDEV_PROTO_VERSION_1;
|
2013-01-19 23:37:49 +01:00
|
|
|
}
|
|
|
|
|
2013-09-15 06:06:14 +02:00
|
|
|
/* Send existing relay-assigned rDevID to relay, or empty string if we have
|
|
|
|
none. Send local devID and type, ID_TYPE_NONE, if we aren't providing an
|
|
|
|
update. It's an error for neither to be provided. */
|
2013-01-19 23:37:49 +01:00
|
|
|
void
|
2013-09-15 06:06:14 +02:00
|
|
|
relaycon_reg( LaunchParams* params, const XP_UCHAR* rDevID,
|
|
|
|
DevIDType typ, const XP_UCHAR* devID )
|
2013-01-19 23:37:49 +01:00
|
|
|
{
|
2013-09-15 06:06:14 +02:00
|
|
|
XP_LOGF( "%s(typ=%d)", __func__, typ );
|
2013-08-20 17:09:45 +02:00
|
|
|
XP_U8 tmpbuf[256];
|
2013-01-19 23:37:49 +01:00
|
|
|
int indx = 0;
|
2013-09-15 06:06:14 +02:00
|
|
|
|
2013-01-19 23:37:49 +01:00
|
|
|
RelayConStorage* storage = getStorage( params );
|
2013-07-20 20:57:10 +02:00
|
|
|
indx += writeHeader( storage, tmpbuf, XWPDEV_REG );
|
2013-09-15 06:06:14 +02:00
|
|
|
indx += addVLIStr( &tmpbuf[indx], sizeof(tmpbuf) - indx, rDevID );
|
|
|
|
|
2013-09-26 17:37:39 +02:00
|
|
|
assert( ID_TYPE_RELAY != typ );
|
2013-01-19 23:37:49 +01:00
|
|
|
tmpbuf[indx++] = typ;
|
2013-09-15 06:06:14 +02:00
|
|
|
if ( ID_TYPE_NONE != typ ) {
|
|
|
|
indx += writeDevID( &tmpbuf[indx], sizeof(tmpbuf) - indx, devID );
|
|
|
|
}
|
2013-08-20 17:09:45 +02:00
|
|
|
indx += writeShort( &tmpbuf[indx], sizeof(tmpbuf) - indx,
|
|
|
|
INITIAL_CLIENT_VERS );
|
2013-08-26 06:20:26 +02:00
|
|
|
indx += addVLIStr( &tmpbuf[indx], sizeof(tmpbuf) - indx, SVN_REV );
|
|
|
|
indx += addVLIStr( &tmpbuf[indx], sizeof(tmpbuf) - indx, "linux box" );
|
2013-09-08 01:17:07 +02:00
|
|
|
indx += addVLIStr( &tmpbuf[indx], sizeof(tmpbuf) - indx, "linux version" );
|
2013-01-19 23:37:49 +01:00
|
|
|
|
|
|
|
sendIt( storage, tmpbuf, indx );
|
2013-01-16 15:46:33 +01:00
|
|
|
}
|
|
|
|
|
2015-07-01 16:10:45 +02:00
|
|
|
void
|
2015-07-07 05:28:16 +02:00
|
|
|
relaycon_invite( LaunchParams* params, XP_U32 destDevID,
|
2015-07-11 08:19:38 +02:00
|
|
|
const XP_UCHAR* relayID, NetLaunchInfo* invit )
|
2015-07-01 16:10:45 +02:00
|
|
|
{
|
|
|
|
XP_U8 tmpbuf[256];
|
|
|
|
int indx = 0;
|
|
|
|
|
|
|
|
RelayConStorage* storage = getStorage( params );
|
|
|
|
indx += writeHeader( storage, tmpbuf, XWPDEV_INVITE );
|
|
|
|
XP_U32 me = linux_getDevIDRelay( params );
|
|
|
|
indx += writeLong( &tmpbuf[indx], sizeof(tmpbuf) - indx, me );
|
2015-07-07 05:28:16 +02:00
|
|
|
|
|
|
|
/* write relayID <connname>/<hid>, or if we have an actual devID write a
|
|
|
|
null byte plus it. */
|
|
|
|
if ( 0 == destDevID ) {
|
|
|
|
XP_ASSERT( '\0' != relayID[0] );
|
|
|
|
indx += writeBytes( &tmpbuf[indx], sizeof(tmpbuf) - indx,
|
|
|
|
(XP_U8*)relayID, 1 + XP_STRLEN( relayID ) );
|
|
|
|
} else {
|
|
|
|
tmpbuf[indx++] = '\0'; /* null byte: zero-len str */
|
|
|
|
indx += writeLong( &tmpbuf[indx], sizeof(tmpbuf) - indx, destDevID );
|
|
|
|
}
|
2015-07-01 16:10:45 +02:00
|
|
|
|
|
|
|
XWStreamCtxt* stream = mem_stream_make( MPPARM(params->mpool)
|
|
|
|
params->vtMgr, params,
|
|
|
|
CHANNEL_NONE, NULL );
|
2015-07-11 17:40:49 +02:00
|
|
|
nli_saveToStream( invit, stream );
|
2015-07-01 16:10:45 +02:00
|
|
|
XP_U16 len = stream_getSize( stream );
|
|
|
|
indx += writeShort( &tmpbuf[indx], sizeof(tmpbuf) - indx, len );
|
|
|
|
XP_ASSERT( indx + len < sizeof(tmpbuf) );
|
|
|
|
const XP_U8* ptr = stream_getPtr( stream );
|
2015-07-07 05:28:16 +02:00
|
|
|
indx += writeBytes( &tmpbuf[indx], sizeof(tmpbuf) - indx, ptr, len );
|
2015-07-01 16:10:45 +02:00
|
|
|
stream_destroy( stream );
|
|
|
|
|
|
|
|
sendIt( storage, tmpbuf, indx );
|
|
|
|
LOG_RETURN_VOID();
|
|
|
|
}
|
|
|
|
|
2013-01-16 15:46:33 +01:00
|
|
|
XP_S16
|
|
|
|
relaycon_send( LaunchParams* params, const XP_U8* buf, XP_U16 buflen,
|
2013-01-19 23:37:49 +01:00
|
|
|
XP_U32 gameToken, const CommsAddrRec* XP_UNUSED(addrRec) )
|
2013-01-16 15:46:33 +01:00
|
|
|
{
|
2013-01-25 04:20:35 +01:00
|
|
|
XP_ASSERT( 0 != gameToken );
|
2013-01-16 15:46:33 +01:00
|
|
|
ssize_t nSent = -1;
|
|
|
|
RelayConStorage* storage = getStorage( params );
|
|
|
|
|
2013-01-23 16:43:58 +01:00
|
|
|
XP_U8 tmpbuf[1 + 4 + 1 + sizeof(gameToken) + buflen];
|
|
|
|
int indx = 0;
|
2013-07-20 20:57:10 +02:00
|
|
|
indx += writeHeader( storage, tmpbuf, XWPDEV_MSG );
|
2015-07-07 05:28:16 +02:00
|
|
|
indx += writeLong( &tmpbuf[indx], sizeof(tmpbuf) - indx, gameToken );
|
|
|
|
indx += writeBytes( &tmpbuf[indx], sizeof(tmpbuf) - indx, buf, buflen );
|
2013-01-23 16:43:58 +01:00
|
|
|
nSent = sendIt( storage, tmpbuf, indx );
|
2013-01-19 23:37:49 +01:00
|
|
|
if ( nSent > buflen ) {
|
|
|
|
nSent = buflen;
|
|
|
|
}
|
2013-12-28 04:13:54 +01:00
|
|
|
LOG_RETURNF( "%zd", nSent );
|
2013-01-19 23:37:49 +01:00
|
|
|
return nSent;
|
|
|
|
}
|
|
|
|
|
|
|
|
XP_S16
|
|
|
|
relaycon_sendnoconn( LaunchParams* params, const XP_U8* buf, XP_U16 buflen,
|
|
|
|
const XP_UCHAR* relayID, XP_U32 gameToken )
|
|
|
|
{
|
|
|
|
XP_LOGF( "%s(relayID=%s)", __func__, relayID );
|
2013-01-25 04:20:35 +01:00
|
|
|
XP_ASSERT( 0 != gameToken );
|
2013-01-19 23:37:49 +01:00
|
|
|
XP_U16 indx = 0;
|
|
|
|
ssize_t nSent = -1;
|
|
|
|
RelayConStorage* storage = getStorage( params );
|
|
|
|
|
|
|
|
XP_U16 idLen = XP_STRLEN( relayID );
|
2013-01-23 16:43:58 +01:00
|
|
|
XP_U8 tmpbuf[1 + 4 + 1 +
|
2013-01-19 23:37:49 +01:00
|
|
|
1 + idLen +
|
|
|
|
sizeof(gameToken) + buflen];
|
2013-07-20 20:57:10 +02:00
|
|
|
indx += writeHeader( storage, tmpbuf, XWPDEV_MSGNOCONN );
|
2015-07-07 05:28:16 +02:00
|
|
|
indx += writeLong( &tmpbuf[indx], sizeof(tmpbuf) - indx, gameToken );
|
|
|
|
indx += writeBytes( &tmpbuf[indx], sizeof(tmpbuf) - indx,
|
|
|
|
(const XP_U8*)relayID, idLen );
|
2013-01-19 23:37:49 +01:00
|
|
|
tmpbuf[indx++] = '\n';
|
2015-07-07 05:28:16 +02:00
|
|
|
indx += writeBytes( &tmpbuf[indx], sizeof(tmpbuf) - indx, buf, buflen );
|
|
|
|
nSent = sendIt( storage, tmpbuf, indx );
|
2013-01-19 23:37:49 +01:00
|
|
|
if ( nSent > buflen ) {
|
|
|
|
nSent = buflen;
|
2013-01-16 15:46:33 +01:00
|
|
|
}
|
2013-12-28 04:13:54 +01:00
|
|
|
LOG_RETURNF( "%zd", nSent );
|
2013-01-16 15:46:33 +01:00
|
|
|
return nSent;
|
|
|
|
}
|
|
|
|
|
2013-01-19 23:37:49 +01:00
|
|
|
void
|
|
|
|
relaycon_requestMsgs( LaunchParams* params, const XP_UCHAR* devID )
|
|
|
|
{
|
|
|
|
XP_LOGF( "%s(devID=%s)", __func__, devID );
|
|
|
|
RelayConStorage* storage = getStorage( params );
|
|
|
|
|
|
|
|
XP_U8 tmpbuf[128];
|
|
|
|
int indx = 0;
|
2013-07-20 20:57:10 +02:00
|
|
|
indx += writeHeader( storage, tmpbuf, XWPDEV_RQSTMSGS );
|
2013-08-26 06:20:26 +02:00
|
|
|
indx += addVLIStr( &tmpbuf[indx], sizeof(tmpbuf) - indx, devID );
|
2013-01-19 23:37:49 +01:00
|
|
|
|
|
|
|
sendIt( storage, tmpbuf, indx );
|
|
|
|
}
|
|
|
|
|
2013-01-29 16:42:10 +01:00
|
|
|
void
|
|
|
|
relaycon_deleted( LaunchParams* params, const XP_UCHAR* devID,
|
|
|
|
XP_U32 gameToken )
|
|
|
|
{
|
|
|
|
LOG_FUNC();
|
|
|
|
RelayConStorage* storage = getStorage( params );
|
|
|
|
XP_U8 tmpbuf[128];
|
|
|
|
int indx = 0;
|
2013-07-20 20:57:10 +02:00
|
|
|
indx += writeHeader( storage, tmpbuf, XWPDEV_DELGAME );
|
2013-01-29 16:42:10 +01:00
|
|
|
indx += writeDevID( &tmpbuf[indx], sizeof(tmpbuf) - indx, devID );
|
2015-07-07 05:28:16 +02:00
|
|
|
indx += writeLong( &tmpbuf[indx], sizeof(tmpbuf) - indx, gameToken );
|
2013-01-29 16:42:10 +01:00
|
|
|
|
|
|
|
sendIt( storage, tmpbuf, indx );
|
|
|
|
}
|
|
|
|
|
2013-01-23 16:43:58 +01:00
|
|
|
static void
|
|
|
|
sendAckIf( RelayConStorage* storage, const MsgHeader* header )
|
|
|
|
{
|
|
|
|
if ( header->cmd != XWPDEV_ACK ) {
|
|
|
|
XP_U8 tmpbuf[16];
|
2013-07-20 20:57:10 +02:00
|
|
|
int indx = writeHeader( storage, tmpbuf, XWPDEV_ACK );
|
2013-08-26 01:59:07 +02:00
|
|
|
indx += writeVLI( &tmpbuf[indx], header->packetID );
|
2013-01-23 16:43:58 +01:00
|
|
|
sendIt( storage, tmpbuf, indx );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-05 16:41:20 +01:00
|
|
|
static gboolean
|
2014-11-06 15:32:00 +01:00
|
|
|
relaycon_receive( GIOChannel* source, GIOCondition XP_UNUSED_DBG(condition), gpointer data )
|
2013-01-16 15:46:33 +01:00
|
|
|
{
|
2014-11-05 16:41:20 +01:00
|
|
|
XP_ASSERT( 0 != (G_IO_IN & condition) ); /* FIX ME */
|
|
|
|
RelayConStorage* storage = (RelayConStorage*)data;
|
2013-01-16 15:46:33 +01:00
|
|
|
XP_U8 buf[512];
|
|
|
|
struct sockaddr_in from;
|
|
|
|
socklen_t fromlen = sizeof(from);
|
|
|
|
|
2014-11-05 16:41:20 +01:00
|
|
|
int socket = g_io_channel_unix_get_fd( source );
|
2013-01-16 15:46:33 +01:00
|
|
|
XP_LOGF( "%s: calling recvfrom on socket %d", __func__, socket );
|
|
|
|
|
|
|
|
ssize_t nRead = recvfrom( socket, buf, sizeof(buf), 0, /* flags */
|
|
|
|
(struct sockaddr*)&from, &fromlen );
|
2013-07-18 16:07:25 +02:00
|
|
|
|
|
|
|
gchar* b64 = g_base64_encode( (const guchar*)buf,
|
|
|
|
((0 <= nRead)? nRead : 0) );
|
2013-12-28 04:13:54 +01:00
|
|
|
XP_LOGF( "%s: read %zd bytes ('%s')", __func__, nRead, b64 );
|
2013-07-18 16:07:25 +02:00
|
|
|
g_free( b64 );
|
2014-11-06 06:45:14 +01:00
|
|
|
#ifdef COMMS_CHECKSUM
|
2014-11-06 06:42:03 +01:00
|
|
|
gchar* sum = g_compute_checksum_for_data( G_CHECKSUM_MD5, buf, nRead );
|
|
|
|
XP_LOGF( "%s: read %zd bytes ('%s')(sum=%s)", __func__, nRead, b64, sum );
|
|
|
|
g_free( sum );
|
|
|
|
#endif
|
2013-01-16 15:46:33 +01:00
|
|
|
if ( 0 <= nRead ) {
|
2013-01-23 16:43:58 +01:00
|
|
|
const XP_U8* ptr = buf;
|
2013-01-16 15:46:33 +01:00
|
|
|
const XP_U8* end = buf + nRead;
|
2013-01-23 16:43:58 +01:00
|
|
|
MsgHeader header;
|
|
|
|
if ( readHeader( &ptr, &header ) ) {
|
|
|
|
sendAckIf( storage, &header );
|
|
|
|
switch( header.cmd ) {
|
|
|
|
case XWPDEV_REGRSP: {
|
2013-08-26 06:20:26 +02:00
|
|
|
uint32_t len;
|
|
|
|
if ( !vli2un( &ptr, &len ) ) {
|
|
|
|
assert(0);
|
|
|
|
}
|
2013-01-23 16:43:58 +01:00
|
|
|
XP_UCHAR devID[len+1];
|
|
|
|
getNetString( &ptr, len, devID );
|
2013-07-29 16:27:30 +02:00
|
|
|
XP_U16 maxInterval = getNetShort( &ptr );
|
|
|
|
XP_LOGF( "%s: maxInterval=%d", __func__, maxInterval );
|
2013-09-15 06:06:14 +02:00
|
|
|
(*storage->procs.devIDReceived)( storage->procsClosure, devID,
|
|
|
|
maxInterval );
|
2013-01-23 16:43:58 +01:00
|
|
|
}
|
|
|
|
break;
|
2014-11-05 16:41:20 +01:00
|
|
|
case XWPDEV_MSG: {
|
|
|
|
CommsAddrRec addr = {0};
|
|
|
|
addr_addType( &addr, COMMS_CONN_RELAY );
|
|
|
|
(*storage->procs.msgReceived)( storage->procsClosure, &addr,
|
2013-01-23 16:43:58 +01:00
|
|
|
ptr, end - ptr );
|
2014-11-05 16:41:20 +01:00
|
|
|
}
|
2013-01-23 16:43:58 +01:00
|
|
|
break;
|
|
|
|
case XWPDEV_BADREG:
|
2013-09-15 06:06:14 +02:00
|
|
|
(*storage->procs.devIDReceived)( storage->procsClosure, NULL, 0 );
|
2013-01-23 16:43:58 +01:00
|
|
|
break;
|
|
|
|
case XWPDEV_HAVEMSGS: {
|
2013-01-25 04:20:35 +01:00
|
|
|
(*storage->procs.msgNoticeReceived)( storage->procsClosure );
|
2013-01-23 16:43:58 +01:00
|
|
|
break;
|
|
|
|
}
|
2013-08-20 17:09:45 +02:00
|
|
|
case XWPDEV_UNAVAIL: {
|
2014-02-28 13:58:25 +01:00
|
|
|
#ifdef DEBUG
|
2013-08-20 17:09:45 +02:00
|
|
|
XP_U32 unavail = getNetLong( &ptr );
|
2014-01-07 15:58:20 +01:00
|
|
|
XP_LOGF( "%s: unavail = %u", __func__, unavail );
|
2014-02-28 13:58:25 +01:00
|
|
|
#endif
|
2013-08-26 06:20:26 +02:00
|
|
|
uint32_t len;
|
|
|
|
if ( !vli2un( &ptr, &len ) ) {
|
|
|
|
assert(0);
|
|
|
|
}
|
2013-01-23 16:43:58 +01:00
|
|
|
XP_UCHAR buf[len+1];
|
|
|
|
getNetString( &ptr, len, buf );
|
2013-08-26 06:20:26 +02:00
|
|
|
|
2013-01-23 16:43:58 +01:00
|
|
|
(*storage->procs.msgErrorMsg)( storage->procsClosure, buf );
|
|
|
|
break;
|
|
|
|
}
|
2013-01-29 16:42:10 +01:00
|
|
|
case XWPDEV_ACK: {
|
2013-08-26 01:59:07 +02:00
|
|
|
uint32_t packetID;
|
|
|
|
if ( !vli2un( &ptr, &packetID ) ) {
|
|
|
|
assert( 0 );
|
|
|
|
}
|
2013-07-17 15:52:35 +02:00
|
|
|
XP_USE( packetID );
|
2013-08-26 01:59:07 +02:00
|
|
|
XP_LOGF( "got ack for packetID %d", packetID );
|
2013-01-29 16:42:10 +01:00
|
|
|
break;
|
|
|
|
}
|
2013-08-20 17:09:45 +02:00
|
|
|
case XWPDEV_ALERT: {
|
2013-08-26 06:20:26 +02:00
|
|
|
uint32_t len;
|
|
|
|
if ( !vli2un( &ptr, &len ) ) {
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
XP_UCHAR buf[len + 1];
|
|
|
|
getNetString( &ptr, len, buf );
|
2013-08-17 23:55:19 +02:00
|
|
|
XP_LOGF( "%s: got message: %s", __func__, buf );
|
|
|
|
break;
|
|
|
|
}
|
2015-07-01 16:10:45 +02:00
|
|
|
case XWPDEV_GOTINVITE: {
|
|
|
|
XP_LOGF( "%s(): got XWPDEV_GOTINVITE", __func__ );
|
2015-07-07 05:28:16 +02:00
|
|
|
#ifdef DEBUG
|
|
|
|
XP_U32 sender =
|
|
|
|
#endif
|
|
|
|
getNetLong( &ptr );
|
2015-07-01 16:10:45 +02:00
|
|
|
XP_U16 len = getNetShort( &ptr );
|
|
|
|
XWStreamCtxt* stream = mem_stream_make( MPPARM(storage->params->mpool)
|
|
|
|
storage->params->vtMgr, storage,
|
|
|
|
CHANNEL_NONE, NULL );
|
|
|
|
stream_putBytes( stream, ptr, len );
|
2015-07-11 08:19:38 +02:00
|
|
|
NetLaunchInfo invit;
|
2015-07-11 17:40:49 +02:00
|
|
|
XP_Bool success = nli_makeFromStream( &invit, stream );
|
2015-07-11 23:27:01 +02:00
|
|
|
XP_LOGF( "sender: %d", sender );
|
2015-07-01 16:10:45 +02:00
|
|
|
stream_destroy( stream );
|
|
|
|
|
|
|
|
if ( success ) {
|
|
|
|
(*storage->procs.inviteReceived)( storage->procsClosure,
|
|
|
|
&invit );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2013-01-23 16:43:58 +01:00
|
|
|
default:
|
|
|
|
XP_LOGF( "%s: Unexpected cmd %d", __func__, header.cmd );
|
|
|
|
XP_ASSERT( 0 );
|
|
|
|
}
|
2013-01-16 15:46:33 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
XP_LOGF( "%s: error reading udp socket: %d (%s)", __func__,
|
|
|
|
errno, strerror(errno) );
|
|
|
|
}
|
2014-11-05 16:41:20 +01:00
|
|
|
return TRUE;
|
2013-01-16 15:46:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
relaycon_cleanup( LaunchParams* params )
|
|
|
|
{
|
|
|
|
XP_FREEP( params->mpool, ¶ms->relayConStorage );
|
|
|
|
}
|
|
|
|
|
|
|
|
static RelayConStorage*
|
|
|
|
getStorage( LaunchParams* params )
|
|
|
|
{
|
2013-08-08 06:09:48 +02:00
|
|
|
XP_ASSERT( params->useUdp );
|
2013-01-16 15:46:33 +01:00
|
|
|
RelayConStorage* storage = (RelayConStorage*)params->relayConStorage;
|
|
|
|
if ( NULL == storage ) {
|
|
|
|
storage = XP_CALLOC( params->mpool, sizeof(*storage) );
|
|
|
|
storage->socket = -1;
|
|
|
|
params->relayConStorage = storage;
|
|
|
|
}
|
|
|
|
return storage;
|
|
|
|
}
|
|
|
|
|
|
|
|
static XP_U32
|
|
|
|
hostNameToIP( const XP_UCHAR* name )
|
|
|
|
{
|
|
|
|
XP_U32 ip;
|
|
|
|
struct hostent* host;
|
|
|
|
XP_LOGF( "%s: looking up %s", __func__, name );
|
|
|
|
host = gethostbyname( name );
|
|
|
|
if ( NULL == host ) {
|
|
|
|
XP_WARNF( "gethostbyname returned NULL\n" );
|
|
|
|
} else {
|
|
|
|
XP_MEMCPY( &ip, host->h_addr_list[0], sizeof(ip) );
|
|
|
|
ip = ntohl(ip);
|
|
|
|
}
|
2014-01-07 15:58:20 +01:00
|
|
|
XP_LOGF( "%s found %x for %s", __func__, ip, name );
|
2013-01-16 15:46:33 +01:00
|
|
|
return ip;
|
|
|
|
}
|
2013-01-19 23:37:49 +01:00
|
|
|
|
2017-10-18 06:32:11 +02:00
|
|
|
typedef struct _ReadState {
|
|
|
|
gchar* ptr;
|
|
|
|
size_t curSize;
|
|
|
|
} ReadState;
|
|
|
|
|
|
|
|
static size_t
|
|
|
|
write_callback(void *contents, size_t size, size_t nmemb, void* data)
|
|
|
|
{
|
|
|
|
ReadState* rs = (ReadState*)data;
|
|
|
|
XP_LOGF( "%s(size=%ld, nmemb=%ld)", __func__, size, nmemb );
|
|
|
|
// void** pp = (void**)data;
|
|
|
|
size_t oldLen = rs->curSize;
|
|
|
|
size_t newLength = size * nmemb;
|
|
|
|
rs->ptr = g_realloc( rs->ptr, oldLen + newLength );
|
|
|
|
memcpy( rs->ptr + oldLen - 1, contents, newLength );
|
|
|
|
rs->ptr[oldLen + newLength - 1] = '\0';
|
|
|
|
size_t result = size * nmemb;
|
|
|
|
// XP_LOGF( "%s() => %ld: (passed: \"%s\")", __func__, result, *strp );
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t
|
|
|
|
post( const XP_U8* msgbuf, XP_U16 len )
|
|
|
|
{
|
|
|
|
const char* data = g_base64_encode( msgbuf, len );
|
|
|
|
struct json_object* jobj = json_object_new_object();
|
|
|
|
struct json_object* jstr = json_object_new_string(data);
|
|
|
|
// g_free( data );
|
|
|
|
json_object_object_add( jobj, "data", jstr );
|
|
|
|
const char* asStr = json_object_to_json_string( jobj );
|
|
|
|
XP_LOGF( "%s: added str: %s", __func__, asStr );
|
|
|
|
|
|
|
|
ReadState rs = {
|
|
|
|
.ptr = g_malloc0(1),
|
|
|
|
.curSize = 1L
|
|
|
|
};
|
|
|
|
|
|
|
|
CURLcode res = curl_global_init(CURL_GLOBAL_DEFAULT);
|
|
|
|
XP_ASSERT(res == CURLE_OK);
|
|
|
|
CURL* curl = curl_easy_init();
|
|
|
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_URL, "http://localhost/relay.py/post");
|
|
|
|
curl_easy_setopt(curl, CURLOPT_POST, 1L);
|
|
|
|
|
|
|
|
char* curl_params = curl_easy_escape( curl, asStr, strlen(asStr) );
|
|
|
|
char buf[4*1024];
|
|
|
|
size_t buflen = snprintf( buf, sizeof(buf), "params=%s", curl_params);
|
|
|
|
XP_ASSERT( buflen < sizeof(buf) );
|
|
|
|
curl_free(curl_params);
|
|
|
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, buf);
|
|
|
|
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)strlen(buf));
|
|
|
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback );
|
|
|
|
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &rs );
|
|
|
|
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
|
|
|
|
|
|
|
|
res = curl_easy_perform(curl);
|
|
|
|
XP_LOGF( "%s(): curl_easy_perform() => %d", __func__, res );
|
|
|
|
/* Check for errors */
|
|
|
|
if (res != CURLE_OK) {
|
|
|
|
XP_LOGF( "curl_easy_perform() failed: %s", curl_easy_strerror(res));
|
|
|
|
}
|
|
|
|
/* always cleanup */
|
|
|
|
curl_easy_cleanup(curl);
|
|
|
|
curl_global_cleanup();
|
|
|
|
|
|
|
|
XP_LOGF( "%s(): got \"%s\"", __func__, rs.ptr );
|
|
|
|
g_free( rs.ptr );
|
|
|
|
|
|
|
|
(void)json_object_put( jobj );
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2013-01-19 23:37:49 +01:00
|
|
|
static ssize_t
|
|
|
|
sendIt( RelayConStorage* storage, const XP_U8* msgbuf, XP_U16 len )
|
|
|
|
{
|
2017-10-18 06:32:11 +02:00
|
|
|
ssize_t nSent;
|
|
|
|
if (1) {
|
|
|
|
nSent = post( msgbuf, len );
|
|
|
|
} else {
|
|
|
|
nSent = sendto( storage->socket, msgbuf, len, 0, /* flags */
|
2013-10-01 16:40:07 +02:00
|
|
|
(struct sockaddr*)&storage->saddr,
|
|
|
|
sizeof(storage->saddr) );
|
2014-11-06 06:45:14 +01:00
|
|
|
#ifdef COMMS_CHECKSUM
|
2014-11-06 06:42:03 +01:00
|
|
|
gchar* sum = g_compute_checksum_for_data( G_CHECKSUM_MD5, msgbuf, len );
|
|
|
|
XP_LOGF( "%s: sent %d bytes with sum %s", __func__, len, sum );
|
|
|
|
g_free( sum );
|
|
|
|
#else
|
2013-12-28 04:13:54 +01:00
|
|
|
XP_LOGF( "%s()=>%zd", __func__, nSent );
|
2014-11-06 06:42:03 +01:00
|
|
|
#endif
|
2017-10-18 06:32:11 +02:00
|
|
|
}
|
2013-01-20 19:04:24 +01:00
|
|
|
return nSent;
|
2013-01-19 23:37:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static size_t
|
2013-08-26 06:20:26 +02:00
|
|
|
addVLIStr( XP_U8* buf, size_t buflen, const XP_UCHAR* str )
|
2013-01-19 23:37:49 +01:00
|
|
|
{
|
2013-08-26 06:20:26 +02:00
|
|
|
uint32_t len = !!str? strlen( str ) : 0;
|
|
|
|
uint8_t nbuf[5];
|
|
|
|
size_t nsize = un2vli( len, nbuf );
|
|
|
|
if ( nsize + len <= buflen ) {
|
|
|
|
memcpy( buf, nbuf, nsize );
|
|
|
|
buf += nsize;
|
2013-01-19 23:37:49 +01:00
|
|
|
XP_MEMCPY( buf, str, len );
|
|
|
|
}
|
2013-08-26 06:20:26 +02:00
|
|
|
return nsize + len;
|
2013-01-19 23:37:49 +01:00
|
|
|
}
|
2013-01-20 22:07:01 +01:00
|
|
|
|
2013-01-29 16:42:10 +01:00
|
|
|
static size_t
|
|
|
|
writeDevID( XP_U8* buf, size_t len, const XP_UCHAR* str )
|
|
|
|
{
|
2013-08-26 06:20:26 +02:00
|
|
|
return addVLIStr( buf, len, str );
|
2013-01-29 16:42:10 +01:00
|
|
|
}
|
|
|
|
|
2013-08-20 17:09:45 +02:00
|
|
|
static size_t
|
|
|
|
writeShort( XP_U8* buf, size_t len, XP_U16 shrt )
|
|
|
|
{
|
|
|
|
shrt = htons( shrt );
|
|
|
|
assert( sizeof( shrt ) <= len );
|
2015-07-07 05:28:16 +02:00
|
|
|
XP_MEMCPY( buf, &shrt, sizeof(shrt) );
|
2013-08-20 17:09:45 +02:00
|
|
|
return sizeof(shrt);
|
|
|
|
}
|
|
|
|
|
2015-07-01 16:10:45 +02:00
|
|
|
static size_t
|
|
|
|
writeLong( XP_U8* buf, size_t len, XP_U32 lng )
|
|
|
|
{
|
|
|
|
lng = htonl( lng );
|
|
|
|
assert( sizeof( lng ) <= len );
|
|
|
|
memcpy( buf, &lng, sizeof(lng) );
|
|
|
|
return sizeof(lng);
|
|
|
|
}
|
|
|
|
|
2015-07-07 05:28:16 +02:00
|
|
|
static size_t
|
|
|
|
writeBytes( XP_U8* buf, size_t len, const XP_U8* bytes, size_t nBytes )
|
|
|
|
{
|
|
|
|
assert( nBytes <= len );
|
|
|
|
XP_MEMCPY( buf, bytes, nBytes );
|
|
|
|
return nBytes;
|
|
|
|
}
|
|
|
|
|
2013-08-26 01:59:07 +02:00
|
|
|
static size_t
|
|
|
|
writeVLI( XP_U8* out, uint32_t nn )
|
|
|
|
{
|
|
|
|
uint8_t buf[5];
|
|
|
|
size_t numSiz = un2vli( nn, buf );
|
|
|
|
memcpy( out, buf, numSiz );
|
|
|
|
return numSiz;
|
|
|
|
}
|
|
|
|
|
2013-01-20 22:07:01 +01:00
|
|
|
static XP_U16
|
2013-01-23 16:43:58 +01:00
|
|
|
getNetShort( const XP_U8** ptr )
|
2013-01-20 22:07:01 +01:00
|
|
|
{
|
|
|
|
XP_U16 result;
|
|
|
|
memcpy( &result, *ptr, sizeof(result) );
|
|
|
|
*ptr += sizeof(result);
|
|
|
|
return ntohs( result );
|
|
|
|
}
|
|
|
|
|
2013-01-29 16:42:10 +01:00
|
|
|
static XP_U32
|
|
|
|
getNetLong( const XP_U8** ptr )
|
|
|
|
{
|
|
|
|
XP_U32 result;
|
|
|
|
memcpy( &result, *ptr, sizeof(result) );
|
|
|
|
*ptr += sizeof(result);
|
|
|
|
return ntohl( result );
|
|
|
|
}
|
|
|
|
|
2013-01-20 22:07:01 +01:00
|
|
|
static void
|
2013-01-23 16:43:58 +01:00
|
|
|
getNetString( const XP_U8** ptr, XP_U16 len, XP_UCHAR* buf )
|
2013-01-20 22:07:01 +01:00
|
|
|
{
|
|
|
|
memcpy( buf, *ptr, len );
|
|
|
|
*ptr += len;
|
|
|
|
buf[len] = '\0';
|
|
|
|
}
|
2013-01-23 16:43:58 +01:00
|
|
|
|
|
|
|
static int
|
2013-07-20 20:57:10 +02:00
|
|
|
writeHeader( RelayConStorage* storage, XP_U8* dest, XWRelayReg cmd )
|
2013-01-23 16:43:58 +01:00
|
|
|
{
|
|
|
|
int indx = 0;
|
2013-08-25 21:34:35 +02:00
|
|
|
dest[indx++] = storage->proto;
|
2015-07-01 16:10:45 +02:00
|
|
|
XP_LOGF( "%s: wrote proto %d", __func__, storage->proto );
|
2013-08-25 21:34:35 +02:00
|
|
|
uint32_t packetNum = 0;
|
|
|
|
if ( XWPDEV_ACK != cmd ) {
|
|
|
|
packetNum = storage->nextID++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( XWPDEV_PROTO_VERSION_1 == storage->proto ) {
|
2013-08-26 01:59:07 +02:00
|
|
|
indx += writeVLI( &dest[indx], packetNum );
|
|
|
|
} else {
|
|
|
|
assert( 0 );
|
2013-08-25 21:34:35 +02:00
|
|
|
}
|
|
|
|
|
2013-01-23 16:43:58 +01:00
|
|
|
dest[indx++] = cmd;
|
|
|
|
return indx;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
readHeader( const XP_U8** buf, MsgHeader* header )
|
|
|
|
{
|
|
|
|
const XP_U8* ptr = *buf;
|
2013-08-25 21:34:35 +02:00
|
|
|
bool ok = XWPDEV_PROTO_VERSION_1 == *ptr++;
|
2013-01-23 16:43:58 +01:00
|
|
|
assert( ok );
|
2013-08-25 21:34:35 +02:00
|
|
|
|
2013-08-26 01:14:42 +02:00
|
|
|
if ( !vli2un( &ptr, &header->packetID ) ) {
|
2013-08-25 21:34:35 +02:00
|
|
|
assert( 0 );
|
|
|
|
}
|
2013-01-23 16:43:58 +01:00
|
|
|
XP_LOGF( "%s: got packet %d", __func__, header->packetID );
|
2013-08-25 21:34:35 +02:00
|
|
|
|
2013-01-23 16:43:58 +01:00
|
|
|
header->cmd = *ptr++;
|
|
|
|
*buf = ptr;
|
|
|
|
return ok;
|
|
|
|
}
|
2013-07-12 05:01:17 +02:00
|
|
|
|
|
|
|
/* Utilities */
|
|
|
|
#define TOKEN_MULT 1000000
|
|
|
|
XP_U32
|
|
|
|
makeClientToken( sqlite3_int64 rowid, XP_U16 seed )
|
|
|
|
{
|
|
|
|
XP_ASSERT( rowid < 0x0000FFFF );
|
|
|
|
XP_U32 result = rowid;
|
|
|
|
result *= TOKEN_MULT; /* so visible when displayed as base-10 */
|
|
|
|
result += seed;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rowidFromToken( XP_U32 clientToken, sqlite3_int64* rowid, XP_U16* seed )
|
|
|
|
{
|
|
|
|
*rowid = clientToken / TOKEN_MULT;
|
|
|
|
*seed = clientToken % TOKEN_MULT;
|
|
|
|
}
|
2013-08-25 21:34:35 +02:00
|
|
|
|
|
|
|
static size_t
|
|
|
|
un2vli( int nn, uint8_t* buf )
|
|
|
|
{
|
|
|
|
int indx = 0;
|
|
|
|
bool done = false;
|
|
|
|
do {
|
|
|
|
uint8_t byt = nn & 0x7F;
|
|
|
|
nn >>= 7;
|
|
|
|
done = 0 == nn;
|
|
|
|
if ( done ) {
|
|
|
|
byt |= 0x80;
|
|
|
|
}
|
|
|
|
buf[indx++] = byt;
|
|
|
|
} while ( !done );
|
|
|
|
|
|
|
|
return indx;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2013-08-26 01:14:42 +02:00
|
|
|
vli2un( const uint8_t** inp, uint32_t* outp )
|
2013-08-25 21:34:35 +02:00
|
|
|
{
|
|
|
|
uint32_t result = 0;
|
|
|
|
const uint8_t* in = *inp;
|
|
|
|
const uint8_t* end = in + 5;
|
|
|
|
|
|
|
|
int count;
|
|
|
|
for ( count = 0; in < end; ++count ) {
|
|
|
|
unsigned int byt = *in++;
|
|
|
|
bool done = 0 != (byt & 0x80);
|
|
|
|
if ( done ) {
|
|
|
|
byt &= 0x7F;
|
|
|
|
}
|
|
|
|
result |= byt << (7 * count);
|
|
|
|
if ( done ) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool success = in < end;
|
|
|
|
if ( success ) {
|
|
|
|
*inp = in;
|
|
|
|
*outp = result;
|
|
|
|
}
|
|
|
|
return success;
|
|
|
|
}
|