/*
 * $Header: /home/gene/library/website/docsrc/sca/src/RCS/msg-portmapper.c,v 395.1 2008/04/20 17:25:48 gene Exp $
 *
 *  Copyright (c) 2006 Gene Michael Stover.  All rights reserved.
 * 
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser 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 Lesser General Public License for more details.
 * 
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
 *  USA
 */

#include "this.h"

/*
 */
static char *S_progname = "";

/*
 */
enum { ACTION_RUN, ACTION_HELP };
static int S_action = ACTION_RUN;

/*
 */
static int
S_CommandLine (int argc, char *argv[])
{
  int rc = 0;
#if IS_UNIX
  int c;
#endif
#if IS_WINDERS
  int optind = 1;
#endif

  S_progname = argv[0];
#if IS_UNIX
  while (rc == 0 && (c = getopt (argc, argv, "h")) != -1) {
    switch (c) {
    case 'h':
      S_action = ACTION_HELP;
      break;
    default:
      fprintf (stderr, "\n%s:%d: warning:", __FILE__, __LINE__);
      fprintf (stderr, " Ignoring unexpected command line");
      fprintf (stderr, " option \"%c\".", c);
    }
  }
#endif
#if IS_WINDERS
  /* Winders doesn't have getopt. */
  while (rc == 0 && optind < argc && argv[optind][0] == '-') {
    if (strcmp (argv[optind], "-h") == 0) {
      S_action = ACTION_HELP;
    } else {
      fprintf (stderr, "\n%s:%d: Ignoring unexpected switch, \"%s\".",
	       __FILE__, __LINE__, argv[optind]);
    }
    ++optind;
  }
#endif
  if (optind < argc) {
    fprintf (stderr, "\n%s:%d:", __FILE__, __LINE__);
    fprintf (stderr, " Ignoring extra command line arguments.");
  }
  return rc;
}

/*
 */
static int
S_Help ()
{
  printf ("\nUsage: %s [-h]", S_progname);
  printf ("\n");
  return 0;
}

/*
 */
static int
S_Run ()
{
  int rc = 0;
  XDR xdrs;
  struct rpc_msg msg;
  struct pmap spmap;

  xdrstdio_create (&xdrs, stdout, XDR_ENCODE);
  bzero (&msg, sizeof msg);
  msg.rm_xid = 17;                      /* msg id, pulled from an orifice */
  msg.rm_direction = CALL;
  msg.ru.RM_cmb.cb_rpcvers = 2;
  msg.ru.RM_cmb.cb_prog = PMAPPROG;
  msg.ru.RM_cmb.cb_vers = PMAPVERS;
  msg.ru.RM_cmb.cb_proc = PMAPPROC_NULL;
  if (xdr_callmsg (&xdrs, &msg)) {
    bzero (&spmap, sizeof spmap);
    /* We'll ask the portmapper for the portmapper. */
    spmap.pm_prog = PMAPPROG;
    spmap.pm_vers = PMAPVERS;
    spmap.pm_prot = IPPROTO_UDP;
    /* spmap_pm_port = ?; */
    if (xdr_pmap (&xdrs, &spmap)) {
      /* good */
    } else {
      fprintf (stderr, "\n%s:%d: xdr_pmap failed", __FILE__, __LINE__);
      rc = 234;
    }
  } else {
    fprintf (stderr, "\n%s:%d: xdr_callmsg failed", __FILE__, __LINE__);
    rc = 333;
  }
  return rc;
}

/*
 */
static int
S_Dispatch ()
{
  int rc = 0;

  switch (S_action) {
  case ACTION_RUN:
    rc = S_Run ();
    break;
  case ACTION_HELP:
    rc = S_Help ();
    break;
  default:
    fprintf (stderr, "\n%s:%d:", __FILE__, __LINE__);
    fprintf (stderr, " S_action is %d.", S_action);
    fprintf (stderr, "  This should never happen!");
    rc = 102;
  }
  return rc;
}

int
main (int argc, char *argv[])
{
  int rc = 0;

  if (S_CommandLine (argc, argv) == 0) {
    if (S_Dispatch () == 0) {
      /* good */
    } else {
      fprintf (stderr, "\n%s:%d: S_Dispatchfailed", __FILE__, __LINE__);
      rc = 234;
    }
  } else {
    fprintf (stderr, "\n%s:%d: S_CommandLine failed", __FILE__, __LINE__);
    rc = 234;
  }
  return rc == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}

/* --- end of file --- */
