From d0e707a48e0d4c194edb5d809ed7305aa1e460d4 Mon Sep 17 00:00:00 2001 From: Eric House Date: Thu, 16 Sep 2010 20:13:24 -0700 Subject: [PATCH] get started with test/cmdline app to query relay's new proxy interface. --- xwords4/relay/.gitignore | 1 + xwords4/relay/Makefile | 4 +- xwords4/relay/rq.c | 95 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 xwords4/relay/rq.c diff --git a/xwords4/relay/.gitignore b/xwords4/relay/.gitignore index 3e2659444..67cc22994 100644 --- a/xwords4/relay/.gitignore +++ b/xwords4/relay/.gitignore @@ -1,4 +1,5 @@ *.o +rq core core.* xwrelay.log* diff --git a/xwords4/relay/Makefile b/xwords4/relay/Makefile index 7f196a004..b30821e05 100644 --- a/xwords4/relay/Makefile +++ b/xwords4/relay/Makefile @@ -48,10 +48,12 @@ CPPFLAGS += -DSPAWN_SELF -DDO_HTTP -g -Wall \ # turn on semaphore debugging # CPPFLAGS += -DDEBUG_LOCKS -memdebug all: xwrelay +memdebug all: xwrelay rq xwrelay: $(OBJ) +rq: rq.c + clean: rm -f xwrelay $(OBJ) diff --git a/xwords4/relay/rq.c b/xwords4/relay/rq.c new file mode 100644 index 000000000..a5188f258 --- /dev/null +++ b/xwords4/relay/rq.c @@ -0,0 +1,95 @@ +/* -*-mode: C; fill-column: 78; c-basic-offset: 4; -*- */ + +/* + * Copyright 2010 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 +#include +#include +#include +#include +#include +#include +#include +#include + +#ifndef DEFAULT_PORT +# define DEFAULT_PORT 10998 +#endif +#ifndef DEFAULT_HOST +# define DEFAULT_HOST "localhost" +#endif + +static void +usage( const char * const argv0 ) +{ + fprintf( stderr, "usage: %s \\\n", argv0 ); + fprintf( stderr, "\t[-p ] # (default %d)\\\n", DEFAULT_PORT ); + fprintf( stderr, "\t[-a ] # (default: %s)\\\n", DEFAULT_HOST ); + exit( 1 ); +} + + +int +main( int argc, char * const argv[] ) +{ + int port = DEFAULT_PORT; + const char* host = DEFAULT_HOST; + + for ( ; ; ) { + int opt = getopt( argc, argv, "a:p:" ); + if ( opt < 0 ) { + break; + } + switch ( opt ) { + case 'a': + host = optarg; + break; + case 'p': + port = atoi(optarg); + break; + default: + usage( argv[0] ); + break; + } + } + + fprintf( stderr, "got port %d, host %s\n", port, host ); + + + int sockfd = socket( AF_INET, SOCK_STREAM, 0 ); + struct sockaddr_in to_sock; + memset( &to_sock, 0, sizeof(to_sock) ); + to_sock.sin_family = AF_INET; + to_sock.sin_port = htons( port ); + + struct hostent* hostip; + hostip = gethostbyname( host ); + memcpy( &(to_sock.sin_addr.s_addr), hostip->h_addr_list[0], + sizeof(hostip->h_addr_list[0] ) ); + + if ( 0 != connect( sockfd, (const struct sockaddr*)&to_sock, + sizeof(to_sock) ) ) { + fprintf( stderr, "connect failed: %d (%s)\n", errno, strerror(errno) ); + exit( 1 ); + } + + close( sockfd ); + + return 0; +}