1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
|
/* $Id: metaserver.c,v 1.13 2002/11/14 11:29:33 riq Exp $ */
/* Tenes Empanadas Graciela
*
* Copyright (C) 2000 Ricardo Quesada
*
* Author: Ricardo Calixto Quesada <rquesada@core-sdi.com>
*
* 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; only version 2 of the License
*
* 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.
*/
/*
* Handles the metaserver's commands
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#ifdef HAVE_PTHREADS
#include <pthread.h>
#endif
#include "server.h"
TEG_STATUS metaserver_status(int fd, char *str);
TEG_STATUS metaserver_on(int fd, char *str);
TEG_STATUS metaserver_off(int fd, char *str);
TEG_STATUS metaserver_address(int fd, char *str);
TEG_STATUS metaserver_help(int fd, char *str);
TEG_STATUS metaserver_lookup( int fd, PARSER *p );
static int g_just_once = 0;
static char *g_comment;
struct {
char *label; TEG_STATUS (*func) ();
char *help;
} meta_options[] = {
{ OPTION_META_STATUS, metaserver_status, N_("Shows status of this server this the metaserver") },
{ OPTION_META_ON, metaserver_on, N_("Add this server to the metaserver, with an optional message") },
{ OPTION_META_OFF, metaserver_off, N_("Delete this server from the metaserver") },
{ OPTION_META_ADDRESS, metaserver_address, N_("Show/Change the metaserver address") },
{ OPTION_HELP, metaserver_help, N_("Shows the metaserver options") },
};
#define NOPTIONS (sizeof(meta_options)/sizeof(meta_options[0]))
/* threaded funcions ! */
void * thread_meta_on( char * comment )
{
int s;
if( ! g_just_once ) {
con_text_out_wop(M_INF,_("Adding %s:%d to metaserver %s:%d...\n"),
g_server.name, g_server.port,
g_server.metaserver_name, g_server.metaserver_port
);
}
s = net_connect_tcp( g_server.metaserver_name, g_server.metaserver_port);
if( s < 0 ) {
con_text_out(M_INF,_("Error\n"));
goto error;
}
net_printf(s, "GET /addServer/%s/%d/%s/%s HTTP/1.0\r\n\r\n",
g_server.name,
g_server.port,
VERSION,
comment);
if( ! g_just_once ) {
con_text_out(M_INF,_("OK\n"));
g_just_once = 1;
}
net_close(s);
/* g_server.metaserver_on = 1; */
return NULL;
error:
/* g_server.metaserver_on = 0; */
return NULL;
}
void * thread_meta_off( void * unused )
{
int s;
con_text_out_wop(M_INF,_("Deleting %s:%d from metaserver %s:%d...\n"),
g_server.name, g_server.port,
g_server.metaserver_name, g_server.metaserver_port
);
s = net_connect_tcp( g_server.metaserver_name, g_server.metaserver_port);
if( s < 0 ) {
con_text_out(M_INF,_("Error\n"));
goto error;
}
net_printf(s, "GET /delServer/%s/%d HTTP/1.0\r\n\r\n",
g_server.name,
g_server.port );
con_text_out(M_INF,_("OK\n"));
net_close(s);
error:
g_just_once = 0;
g_server.metaserver_on = 0;
return NULL;
}
/* main thread functions */
static TEG_STATUS view_current_server( void )
{
con_text_out_wop(M_INF,_("Current metaserver: %s port: %d\n"), g_server.metaserver_name, g_server.metaserver_port);
return TEG_STATUS_SUCCESS;
}
TEG_STATUS metaserver_address( int fd, char *str )
{
char *ptr;
if( ! str || ! str[0] )
return view_current_server();
if( (ptr = strchr(str,' ')) == NULL ) {
con_text_out_wop(M_ERR,_("Invalid address. Examples:\n metaserver address\n metaserver address ggz.homeip.net 2002\n"));
goto error;
}
g_server.metaserver_port = atoi( ptr+1);
*ptr = '\0';
strncpy( g_server.metaserver_name, str, sizeof(g_server.metaserver_name)-1);
view_current_server();
return TEG_STATUS_SUCCESS;
error:
return TEG_STATUS_ERROR;
}
TEG_STATUS metaserver_status( int fd, char *str )
{
con_text_out_wop(M_INF,_("Metaserver status:\n"));
con_text_out_wop(M_INF,_(" Metaserver is: %s\n"), ( g_server.metaserver_on ? _("ON") : _("OFF") ));
view_current_server();
return TEG_STATUS_SUCCESS;
}
/* str is an optional message */
TEG_STATUS metaserver_on( int fd, char *str )
{
/* translate spaces into '+' */
if( str && str[0] )
{
int i;
int l = strlen(str);
for(i=0;i<l;i++) {
if (str[i] == ' ' )
str[i] = '+';
}
} else {
str = "[empty+description]";
}
if( g_comment )
free( g_comment );
g_comment = strdup( str );
#ifdef HAVE_PTHREADS
{
pthread_t tid;
pthread_create( &tid, NULL, thread_meta_on, g_comment );
}
#else
thread_meta_on( str );
#endif
return TEG_STATUS_SUCCESS;
}
TEG_STATUS metaserver_off( int fd, char *str )
{
#ifdef HAVE_PTHREADS
pthread_t tid;
pthread_create( &tid, NULL, thread_meta_off, NULL);
#else
thread_meta_off( str );
#endif
return TEG_STATUS_SUCCESS;
}
TEG_STATUS metaserver_help( int fd, char *str )
{
int i;
for(i=0;i<NOPTIONS;i++) {
if(meta_options[i].func)
net_printf(fd, "'%s' %s\n",meta_options[i].label,_(meta_options[i].help));
}
return TEG_STATUS_SUCCESS;
}
TEG_STATUS metaserver_lookup( int fd, PARSER *p )
{
int i;
for(i = 0; i < NOPTIONS; i++) {
if(strcasecmp( p->token, meta_options[i].label )==0 ){
if (meta_options[i].func)
return( (meta_options[i].func)(fd,p->value));
return TEG_STATUS_TOKENNULL;
}
}
net_printf(fd,_("Unknow option. Try using `%s %s' for help\n"),TOKEN_METASERVER,OPTION_HELP);
return TEG_STATUS_TOKENNOTFOUND;
}
TEG_STATUS metaserver_parse( int fd, char *str)
{
int i;
PARSER p;
DELIM igualador={ '=', ' ', ':' };
DELIM separador=DELIM_NULL;
p.igualador = &igualador;
p.separador = &separador;
p.data = str;
do {
if( (i=parser_call( &p )) ) {
if( metaserver_lookup( fd,&p ) == TEG_STATUS_CONNCLOSED ) {
return TEG_STATUS_CONNCLOSED;
}
}
} while( i && p.hay_otro);
return TEG_STATUS_SUCCESS;
}
TEG_STATUS metaserver_init()
{
strncpy( g_server.metaserver_name, "ggz.homeip.net", sizeof( g_server.metaserver_name) -1 );
g_server.metaserver_port = 2002;
g_server.metaserver_on = 0;
return TEG_STATUS_SUCCESS;
}
TEG_STATUS metaserver_publish()
{
if( ! g_comment )
g_comment = strdup( "[default+description]" );
if( g_server.metaserver_on )
{
#ifdef HAVE_PTHREADS
pthread_t tid;
pthread_create( &tid, NULL, thread_meta_on, g_comment );
#else
thread_meta_on( g_comment );
#endif
}
return TEG_STATUS_SUCCESS;
}
|