home *** CD-ROM | disk | FTP | other *** search
/ HaCKeRz KrOnIcKLeZ 3 / HaCKeRz_KrOnIcKLeZ.iso / ircscripts / warirc / arnudp001.c < prev    next >
C/C++ Source or Header  |  1996-04-23  |  3KB  |  115 lines

  1. /************************************************************************/
  2. /* arnudp.c version 0.01 by Arny - cs6171@scitsc.wlv.ac.uk        */
  3. /* Sends a single udp datagram with the source/destination address/port    */
  4. /* set to whatever you want.  Unfortunately Linux 1.2 and SunOS 4.1    */
  5. /* don't seem to have the IP_HDRINCL option, so the source address will    */
  6. /* be set to the real address.  It does however work ok on SunOS 5.4.    */
  7. /* Should compile fine with just an ANSI compiler (such as gcc) under    */
  8. /* Linux and SunOS 4.1, but with SunOS 5.4 you have to specify extra    */
  9. /* libraries on the command line:                    */
  10. /*     /usr/ucb/cc -o arnudp arnudp001.c -lsocket -lnsl        */
  11. /* I'll state the obvious - this needs to be run as root!  Do not use    */
  12. /* this program unless you know what you are doing, as it is possible    */
  13. /* that you could confuse parts of your network    / internet.        */
  14. /* (c) 1995 Arny - I accept no responsiblity for anything this does.    */
  15. /************************************************************************/
  16. /* I used the source of traceroute as an example while writing this.    */
  17. /* Many thanks to Dan Egnor (egnor@ugcs.caltech.edu) and Rich Stevens    */
  18. /* for pointing me in the right direction.                */
  19. /************************************************************************/
  20.  
  21. #include<sys/types.h>
  22. #include<sys/socket.h>
  23. #include<netinet/in_systm.h>
  24. #include<netinet/in.h>
  25. #include<netinet/ip.h>
  26. #include<netinet/udp.h>
  27. #include<errno.h>
  28. #include<string.h>
  29. #include<netdb.h>
  30. #include<arpa/inet.h>
  31. #include<stdio.h>
  32.  
  33. struct sockaddr sa;
  34.  
  35. main(int argc,char **argv)
  36. {
  37. int fd;
  38. int x=1;
  39. struct sockaddr_in *p;
  40. struct hostent *he;
  41. u_char gram[38]=
  42.     {
  43.     0x45,    0x00,    0x00,    0x26,
  44.     0x12,    0x34,    0x00,    0x00,
  45.     0xFF,    0x11,    0,    0,
  46.     0,    0,    0,    0,
  47.     0,    0,    0,    0,
  48.  
  49.     0,    0,    0,    0,
  50.     0x00,    0x12,    0x00,    0x00,
  51.  
  52.     '1','2','3','4','5','6','7','8','9','0'
  53.     };
  54.  
  55. if(argc!=5)
  56.     {
  57.     fprintf(stderr,"usage: %s sourcename sourceport destinationname destinationport\n",*argv);
  58.     exit(1);
  59.     };
  60.  
  61. if((he=gethostbyname(argv[1]))==NULL)
  62.     {
  63.     fprintf(stderr,"can't resolve source hostname\n");
  64.     exit(1);
  65.     };
  66. bcopy(*(he->h_addr_list),(gram+12),4);
  67.  
  68. if((he=gethostbyname(argv[3]))==NULL)
  69.     {
  70.     fprintf(stderr,"can't resolve destination hostname\n");
  71.     exit(1);
  72.     };
  73. bcopy(*(he->h_addr_list),(gram+16),4);
  74.  
  75. *(u_short*)(gram+20)=htons((u_short)atoi(argv[2]));
  76. *(u_short*)(gram+22)=htons((u_short)atoi(argv[4]));
  77.  
  78. p=(struct sockaddr_in*)&sa;
  79. p->sin_family=AF_INET;
  80. bcopy(*(he->h_addr_list),&(p->sin_addr),sizeof(struct in_addr));
  81.  
  82. if((fd=socket(AF_INET,SOCK_RAW,IPPROTO_RAW))== -1)
  83.     {
  84.     perror("socket");
  85.     exit(1);
  86.     };
  87.  
  88. #ifdef IP_HDRINCL
  89. fprintf(stderr,"we have IP_HDRINCL :-)\n\n");
  90. if (setsockopt(fd,IPPROTO_IP,IP_HDRINCL,(char*)&x,sizeof(x))<0)
  91.     {
  92.     perror("setsockopt IP_HDRINCL");
  93.     exit(1);
  94.         };
  95. #else
  96. fprintf(stderr,"we don't have IP_HDRINCL :-(\n\n");
  97. #endif
  98.  
  99. if((sendto(fd,&gram,sizeof(gram),0,(struct sockaddr*)p,sizeof(struct sockaddr)))== -1)
  100.     {
  101.     perror("sendto");
  102.     exit(1);
  103.     };
  104.  
  105. printf("datagram sent without error:");
  106. for(x=0;x<(sizeof(gram)/sizeof(u_char));x++)
  107.     {
  108.     if(!(x%4)) putchar('\n');
  109.     printf("%02x",gram[x]);
  110.     };
  111. putchar('\n');
  112.  
  113. }
  114.  
  115.