/*
 * WARNING: THIS PROGRAM IS A HACK, DO NOT USE IT AS AN EXAMPLE OF
 * GOOD CODING STYLE, OR YOUR DOG MAY BITE YOU.
 *
 * Argument should be the destination IP address.  A small UDP
 * datagram will be sent to that address.
 *
 * Jeremy Elson, jelson@circlemud.org
 * November 24, 1997
 */

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <ctype.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <netinet/in.h>
#include <unistd.h>
#include <netdb.h>
#include <string.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>



#define BUFLEN	512

int main(int argc, char *argv[])
{
   int s, opt;
   struct sockaddr_in sa;
   struct in_addr ad;
   char buf[BUFLEN];

   if (argc != 2) {
     fprintf(stderr, "usage: %s <target-ip>\n", argv[0]);
     exit(1);
   }

#if 0   
   if ((s = socket(PF_INET, SOCK_RAW, IPPROTO_UDP)) < 0) {
      perror("Create socket");
      exit(1);
   }
#endif

   if ((s = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
      perror("Create socket");
      exit(1);
   }

  opt = 1;
  if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, (char *) &opt, sizeof(opt)) < 0) {
    perror("SYSERR: setsockopt BROADCAST");
    exit(1);
  }

#if 0
  opt = 1;
  if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL, (char *) &opt, sizeof(opt)) < 0) {
    perror("SYSERR: setsockopt TTL");
    exit(1);
  } else {
    fprintf(stderr, "ttl set to %d\n", opt);
  }
#endif

  sa.sin_family = AF_INET;
  sa.sin_port = htons(41508);
  
  sa.sin_addr.s_addr = inet_addr(argv[1]);


/*
   For systems that have it

   if (!(inet_makeaddr(argv[1], &ad))) {
     fprintf(stderr, "%s: invalid IP address %s\n", argv[0], argv[1]);
     exit(1);
   }

   sa.sin_addr = ad;
*/

   strcpy(buf, "This is a test message");
   if (sendto(s, buf, strlen(buf), 0, (struct sockaddr *)&sa, sizeof(sa)) < 0)
     perror("sending packet");
   exit(0);
}
