/* Jungle Monkey Search Server
* Copyright (C) 2000-2001 The Regents of the University of Michigan
*
* 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 "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <time.h>
#include <sys/types.h>
#ifdef JM_ENABLE_GNOME
#include <popt-gnome.h>
#else
#include <popt.h>
#endif
#include <gnet/gnet.h>
#include "jmintl.h"
#include "util/util.h"
#include "mtp_server.h"
#include "mtp_debug.h"
#define DUMP_SERVER_TIMEOUT 5 * 60 * 1000 /* 5 minutes */
static gboolean dump_server_cb (gpointer data);
/* ******************** */
/* Globals */
static int verbose = 0;
static gchar* server_name = NULL;
static MtpServer* server = NULL;
int main (int argc, char* argv[])
{
const gchar* url = NULL;
GMainLoop* main_loop;
gchar* hostname = NULL;
gchar* hostname_name = NULL;
GInetAddr* interface = NULL;
gchar* interface_name = NULL;
gboolean use_dns = FALSE;
gint port = 0;
gboolean force_port = FALSE;
struct poptOption jm_options[] =
{
{"help", 'h', POPT_ARG_NONE, NULL, 1,
"Show this help message", NULL},
{"usage", '\0', POPT_ARG_NONE, NULL, 2,
"Display brief usage message", NULL},
{"version", 0, POPT_ARG_NONE, NULL, 3,
"Output verson information and exit", NULL},
{"verbose", 'v', POPT_ARG_NONE, &verbose, 0,
"Print lots of stuff", NULL},
{"hostname", '\0', POPT_ARG_STRING, &hostname_name, 0,
"Hostname or address of this host", "HOSTNAME"},
{"interface", '\0', POPT_ARG_STRING, &interface_name, 0,
"Interface address for listening socket", "ADDR"},
{"port", '\0', POPT_ARG_INT, &port, 0,
"Port for listening socket", "PORT"},
{"force-port", '\0', POPT_ARG_NONE, &force_port, 0,
"Fail if can't set port", NULL},
{"use-dns", '\0', POPT_ARG_NONE, &use_dns, 0,
"Use DNS to figure out my hostname", NULL},
{"name", 0, POPT_ARG_STRING, &server_name, 0,
"Name of the rendezvous server (ex: `mofo - MI, USA')", "NAME"},
{"debug-flags", 'd', POPT_ARG_INT, &mtp_debug_flags, 0,
"Set the MTP debug flags", "FLAGS"},
{NULL, '\0', 0, NULL, 0, NULL, NULL}
};
poptContext ctx;
int rc;
int exit_status = EXIT_SUCCESS;
/* ******************** */
/* Initialize NLS */
#ifdef ENABLE_NLS
{
/* gtk_set_locale(); */
bindtextdomain (PACKAGE, LOCALEDIR);
textdomain (PACKAGE);
}
#endif
/* ******************** */
/* Parse command line */
ctx = (poptContext) poptGetContext (NULL, argc, (const char**) argv, jm_options, 0);
poptSetOtherOptionHelp (ctx, _("[OPTION]... [URL]"));
while ((rc = poptGetNextOpt(ctx)) > 0)
{
switch (rc)
{
case 1: goto help; break;
case 2: goto usage; break;
case 3: goto version; break;
}
}
url = poptGetArg (ctx);
poptFreeContext (ctx);
/* If an interface was specified, make sure it resoves to an address */
if (interface_name)
{
/* Convert interface_name to InetAddr. */
interface = gnet_inetaddr_new (interface_name, 0);
if (!interface)
my_error (_("Bad interface name: %s.\n"), interface_name);
}
/* Otherwise, auto-detect the interface */
else
{
interface = gnet_inetaddr_autodetect_internet_interface ();
if (!interface)
my_error (_("Could not auto-detect an internet interface. "
"Use the --interface option to set it manually.\n"));
}
/* If a hostname was specified, use it */
if (hostname_name)
{
hostname = g_strdup (hostname_name);
}
/* Autodetect the hostname */
else
{
if (use_dns)
hostname = gnet_inetaddr_get_name (interface);
if (!hostname ||
!gnet_inetaddr_is_internet_domainname (hostname))
hostname = gnet_inetaddr_get_canonical_name (interface);
g_assert (hostname);
if (!gnet_inetaddr_is_internet_domainname (hostname))
my_error (_("Could not auto-detect hostname. "
"Use the --hostname option to set it manually.\n"));
}
if (server_name == NULL)
{
server_name = gnet_inetaddr_gethostname();
g_assert (server_name);
}
/* ******************** */
/* Create the server */
gnet_inetaddr_set_port (interface, port);
server = mtp_server_new (hostname, interface, force_port, MTP_SERVER_TYPE_RENDEZVOUS, TRUE);
if (!server)
my_error (_("Could not start rendezvous server.\n"));
server->verbose = verbose;
if (verbose)
{
g_print (_("MTP rendezvous server started on port %d\n"), server->server->port);
g_timeout_add (DUMP_SERVER_TIMEOUT, dump_server_cb, NULL);
dump_server_cb (NULL);
}
/* ******************** */
/* Start the main loop */
main_loop = g_main_new (FALSE);
g_main_run (main_loop);
exit (EXIT_SUCCESS);
usage:
poptPrintUsage (ctx, stdout, 0);
exit (exit_status);
help:
g_print (_("`mtprendezvous' - MTP Rendezvous Server\n"));
poptPrintHelp (ctx, stdout, 0);
g_print (_("\n"
"Report bugs to <jm-dev@umich.edu>\n"));
exit (exit_status);
version:
g_print (_("mtprendezvous %s\n"
"Written by David A. Helder\n"
"\n"
"Copyright (C) 2000-2001 The Regents of the University of Michigan\n"
"This is free software; see the source for copying conditions. There is NO\n"
"warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"),
VERSION);
exit (exit_status);
}
/* **************************************** */
gboolean
dump_server_cb (gpointer data)
{
time_t t;
char* time_str;
t = time(NULL);
t = mktime(gmtime(&t));
time_str = ctime(&t); /* don't free string */
time_str[strlen(time_str) - 1] = '\0';
g_print ("time: %s UTC\n", time_str);
mtp_server_print (server, stderr);
return TRUE;
}
syntax highlighted by Code2HTML, v. 0.9.1