/* Connection-Reflection Protocol - Tests if listening socket is reachable
 * Copyright (C) 2000  David Helder
 *
 * 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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include <gnet/gnet.h>

#include "util/util.h"
#include "crp.h"


#define MAX_BUFFER_IN	80	/* Don't read more than 80 bytes */
#define	CONN_TIMEOUT	30000	/* 30 seconds to connect max */


static void crpcheck_func (const gchar* hostname, gint port, CRPStatus status, 
			   GInetAddr* addr, gpointer user_data);



int
main(int argc, char** argv)
{
  int port = 0;
  GURL* url;
  GMainLoop* main_loop = NULL;

  if (argc != 4)
    {
      g_print ("usage: %s <url> <hostname> <port>\n", argv[0]);
      exit(EXIT_FAILURE);
    }

  url = gnet_url_new (argv[1]);
  port = atoi (argv[3]);

  if (!url)
    my_error ("Bad URL: %s\n", argv[1]);

  crp_check (url, argv[2], port, crpcheck_func, NULL);

  g_print ("Checking %s:%d using CRP server %s\n", argv[2], port, argv[1]);

  /* Start the main loop */
  main_loop = g_main_new(FALSE);
  g_main_run (main_loop);

  exit (EXIT_SUCCESS);
  return 0;
}


static void
crpcheck_func (const gchar* hostname, gint port, CRPStatus status, 
	       GInetAddr* addr, gpointer user_data)
{
  switch (status)
    {
    case CRP_STATUS_GOOD:
      g_print ("%s:%d is good\n", hostname, port);
      break;

    case CRP_STATUS_BAD_PAIR:
      g_print ("%s:%d has a bad hostname-port pair\n", hostname, port);
      break;

    case CRP_STATUS_BAD_NAME:
      g_print ("%s:%d has a bad hostname\n", hostname, port);
      break;

    case CRP_STATUS_BAD_PORT:
      g_print ("%s:%d has a bad port\n", hostname, port);
      break;

    case CRP_STATUS_ERROR:
      g_print ("Error.\n");
      break;
    }

  exit (EXIT_SUCCESS);
}


syntax highlighted by Code2HTML, v. 0.9.1