/*
    Copyright (C) 1991, Marcus J. Ranum. All rights reserved.
*/

/*
useable by an mwhod wizard to send a command to a remote mwhod.
It is assumed that the user knows all the syntax EXACTLY for the
command to send. heh.

primarily useful for defining new peers while the server is running
without having to kill and restart the server

for example:

sample of a mwhocmd to define a mud to a running server
note that the actual tabs here must be converted when you type it.
mwhocmd -s hussar -c 'W\twizard\twizardpasswd\tC hostname MUDname MUDpw desc'
*/


#include    "os.h"

#ifdef WIN32
#include "getopt.h"
#endif

#define DGRAMPORT       6888


int main (int ac, char *av[])
{
  SOCKET dgramfd;
  struct sockaddr_in addr;

#ifndef NO_HUGE_RESOLVER_CODE
  struct hostent *hp;
#endif
  char *p;
  char *mycmd;
  char *server;


  while ((dgramfd = getopt (ac, av, "c:s:")) != EOF) {
    switch (dgramfd) {
    case 's':
      server = optarg;
      break;

    case 'c':
      mycmd = optarg;
      break;

    default:
      fprintf (stderr, "usage.\n");
      exit (1);
    }
  }

  WIN32STARTUP

  p = server;
  while (*p != '\0' && (*p == '.' || isdigit (*p)))
    p++;

  if (*p != '\0') {
#ifndef NO_HUGE_RESOLVER_CODE
    if ((hp = gethostbyname (server)) == (struct hostent *) 0) {
      WIN32CLEANUP
      return (1);
    }
    (void) bcopy (hp->h_addr, (char *) &addr.sin_addr, hp->h_length);
#else
    WIN32CLEANUP
    return (1);
#endif
  } else {
#ifdef WIN32
    unsigned long f;
#else
    struct in_addr f;
#endif

#ifdef WIN32
    if ((f = inet_addr (server)) == INADDR_NONE) {
#else
    if (inet_aton (server, &f) == 0) {
#endif
      WIN32CLEANUP
      return (1);
    }
    (void) bcopy ((char *) &f, (char *) &addr.sin_addr, sizeof (f));
  }

  addr.sin_port = htons (DGRAMPORT);
  addr.sin_family = AF_INET;

  if ((dgramfd = socket (AF_INET, SOCK_DGRAM, 0)) == INVALID_SOCKET) {
    WIN32CLEANUP
    return (1);
  }
  sendto (dgramfd, mycmd, strlen (mycmd), 0, (struct sockaddr *) &addr, sizeof (addr));
  WIN32CLEANUP
  return (0);
}