From f94b0b38007ff5f4f86b3d1e08b0d22ff3fdfb51 Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Fri, 20 Feb 2004 05:37:47 +0000 Subject: [PATCH] clients can now connect, but all that happens is the qtv console fills up with "hi" --- qtv/include/Makefile.am | 2 +- qtv/include/client.h | 47 +++++++++++++++++++++++ qtv/source/Makefile.am | 2 +- qtv/source/client.c | 82 +++++++++++++++++++++++++++++++++++++++++ qtv/source/qtv.c | 79 +++++++++++++++++++++++++++++++++++++-- 5 files changed, 207 insertions(+), 5 deletions(-) create mode 100644 qtv/include/client.h create mode 100644 qtv/source/client.c diff --git a/qtv/include/Makefile.am b/qtv/include/Makefile.am index 187e0c2c5..b6216f742 100644 --- a/qtv/include/Makefile.am +++ b/qtv/include/Makefile.am @@ -1,4 +1,4 @@ ## Process this file with automake to produce Makefile.in AUTOMAKE_OPTIONS= foreign -EXTRA_DIST = connection.h server.h +EXTRA_DIST = client.h connection.h server.h diff --git a/qtv/include/client.h b/qtv/include/client.h new file mode 100644 index 000000000..ff1c47a29 --- /dev/null +++ b/qtv/include/client.h @@ -0,0 +1,47 @@ +/* + #FILENAME# + + #DESCRIPTION# + + Copyright (C) 2004 #AUTHOR# + + Author: #AUTHOR# + Date: #DATE# + + 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: + + Free Software Foundation, Inc. + 59 Temple Place - Suite 330 + Boston, MA 02111-1307, USA + + $Id$ +*/ + +#ifndef __client_h +#define __client_h + +#include "netchan.h" + +typedef struct client_s { + struct info_s *userinfo; + netchan_t netchan; +} client_t; + +struct connection_s; + +void Client_Init (void); +void Client_Connect (struct connection_s *con, int qport, struct info_s *info); + +#endif//__client_h diff --git a/qtv/source/Makefile.am b/qtv/source/Makefile.am index a9f0189af..41bdb5116 100644 --- a/qtv/source/Makefile.am +++ b/qtv/source/Makefile.am @@ -43,7 +43,7 @@ qtv_LIBS= \ $(top_builddir)/libs/console/libQFconsole.la \ $(top_builddir)/libs/util/libQFutil.la -qtv_SOURCES= connection.c qtv.c server.c +qtv_SOURCES= client.c connection.c qtv.c server.c qtv_LDADD= $(qtv_LIBS) $(NET_LIBS) $(DL_LIBS) $(CURSES_LIBS) qtv_LDFLAGS= $(common_ldflags) qtv_DEPENDENCIES= $(qtv_LIBS) diff --git a/qtv/source/client.c b/qtv/source/client.c new file mode 100644 index 000000000..99bf76dd8 --- /dev/null +++ b/qtv/source/client.c @@ -0,0 +1,82 @@ +/* + #FILENAME# + + #DESCRIPTION# + + Copyright (C) 2004 #AUTHOR# + + Author: #AUTHOR# + Date: #DATE# + + 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: + + Free Software Foundation, Inc. + 59 Temple Place - Suite 330 + Boston, MA 02111-1307, USA + +*/ +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +static __attribute__ ((unused)) const char rcsid[] = + "$Id$"; + +#ifdef HAVE_STRING_H +# include +#endif +#ifdef HAVE_STRINGS_H +# include +#endif + +#include +#include + +#include "QF/cmd.h" +#include "QF/console.h" +#include "QF/hash.h" +#include "QF/info.h" + +#include "client.h" +#include "connection.h" + +void +Client_Init (void) +{ +} + +static void +client_handler (void *object) +{ + client_t *cl = (client_t *) object; + byte d[1]; + + Con_Printf ("hi\n"); + Netchan_Transmit (&cl->netchan, 0, d); +} + +void +Client_Connect (connection_t *con, int qport, info_t *info) +{ + client_t *cl; + + cl = calloc (1, sizeof (client_t)); + Netchan_Setup (&cl->netchan, con->address, qport, NC_READ_QPORT); + cl->userinfo = info; + con->object = cl; + con->handler = client_handler; + Con_Printf ("client %s (%s) connected\n", Info_ValueForKey (info, "name"), + NET_AdrToString (con->address)); +} diff --git a/qtv/source/qtv.c b/qtv/source/qtv.c index 6564049c1..644d0f3ca 100644 --- a/qtv/source/qtv.c +++ b/qtv/source/qtv.c @@ -50,6 +50,7 @@ static __attribute__ ((unused)) const char rcsid[] = #include "QF/cvar.h" #include "QF/dstring.h" #include "QF/idparse.h" +#include "QF/info.h" #include "QF/plugin.h" #include "QF/qargs.h" #include "QF/quakefs.h" @@ -57,15 +58,19 @@ static __attribute__ ((unused)) const char rcsid[] = #include "QF/va.h" #include "QF/zone.h" +#include "client.h" #include "compat.h" #include "connection.h" #include "netchan.h" #include "server.h" #define PORT_QTV 27501 -#define A2A_PING 'k' -#define A2A_ACK 'l' -#define A2C_PRINT 'n' +#define A2A_PING 'k' +#define A2A_ACK 'l' +#define A2C_PRINT 'n' +#define S2C_CHALLENGE 'c' +#define S2C_CONNECTION 'j' +#define PROTOCOL_VERSION 28 typedef enum { RD_NONE, @@ -78,6 +83,8 @@ static plugin_list_t server_plugin_list[] = { SERVER_PLUGIN_LIST }; +double realtime; + cbuf_t *qtv_cbuf; cbuf_args_t *qtv_args; @@ -207,6 +214,7 @@ qtv_net_init (void) "udp port to use"); NET_Init (qtv_port->int_val); Connection_Init (); + net_realtime = &realtime; Netchan_Init (); } @@ -288,6 +296,66 @@ qtv_status (void) qtv_end_redirect (); } +typedef struct { + int challenge; + double time; +} challenge_t; + +static void +qtv_getchallenge (void) +{ + challenge_t *ch; + connection_t *con; + + if ((con = Connection_Find (&net_from))) { + if (con->handler) + return; + ch = con->object; + } else { + ch = malloc (sizeof (challenge_t)); + } + ch->challenge = (rand () << 16) ^ rand (); + ch->time = Sys_DoubleTime (); + if (!con) + Connection_Add (&net_from, ch, 0); + Netchan_OutOfBandPrint (net_from, "%c%i QF", S2C_CHALLENGE, ch->challenge); +} + +static void +qtv_client_connect (void) +{ + info_t *userinfo; + int version, qport, challenge; + connection_t *con; + challenge_t *ch; + + version = atoi (Cmd_Argv (1)); + if (version != PROTOCOL_VERSION) { + Netchan_OutOfBandPrint (net_from, "%c\nServer is version %s.\n", + A2C_PRINT, QW_VERSION); + Con_Printf ("* rejected connect from version %i\n", version); + return; + } + qport = atoi (Cmd_Argv (2)); + challenge = atoi (Cmd_Argv (3)); + if (!(con = Connection_Find (&net_from)) || con->handler + || (ch = con->object)->challenge != challenge) { + Netchan_OutOfBandPrint (net_from, "%c\nBad challenge.\n", + A2C_PRINT); + return; + } + free (con->object); + userinfo = Info_ParseString (Cmd_Argv (4), 0, 0); + if (!userinfo) { + Netchan_OutOfBandPrint (net_from, "%c\nInvalid userinfo string.\n", + A2C_PRINT); + return; + } + + Client_Connect (con, qport, userinfo); + Netchan_OutOfBandPrint (net_from, "%c", S2C_CONNECTION); +} + static void qtv_connectionless_packet (void) { @@ -306,6 +374,10 @@ qtv_connectionless_packet (void) qtv_ping (); } else if (!strcmp (cmd, "status")) { qtv_status (); + } else if (!strcmp (cmd, "getchallenge")) { + qtv_getchallenge (); + } else if (!strcmp (cmd, "connect")) { + qtv_client_connect (); } else { Con_Printf ("bad connectionless packet from %s:\n%s\n", NET_AdrToString (net_from), str); @@ -341,6 +413,7 @@ main (int argc, const char *argv[]) Cbuf_Execute_Stack (qtv_cbuf); Sys_CheckInput (1, net_socket); + realtime = Sys_DoubleTime (); qtv_read_packets ();