home *** CD-ROM | disk | FTP | other *** search
/ Hackers Toolkit v2.0 / Hackers_Toolkit_v2.0.iso / HTML / archive / Unix / c-src / arnup100.c < prev    next >
C/C++ Source or Header  |  1999-11-04  |  5KB  |  197 lines

  1.  
  2.  
  3. /************************************************************************/
  4. /* arnudp100.c - sends a single UDP datagram with the source and    */
  5. /* destination address and port set to whatever you want.        */
  6. /* Known to work on:  Linux pre2.0.7 (486),  SunOS 5.4 (SPARC) and    */
  7. /* FreeBSD 2.1.0 (486).                            */
  8. /* Old kernels such as SunOS 4.1 and Linux 1.2 will overwrite the    */
  9. /* source address with the real address of the interface.        */
  10. /* Should compile fine with just an ANSI compiler (such as gcc) under    */
  11. /* Linux and SunOS 4.1, but with SunOS 5.4 you may have to specify    */
  12. /* extra libraries on the command line:                    */
  13. /*     /usr/ucb/cc -o arnudp arnudp100.c -lsocket -lnsl        */
  14. /* I'll state the obvious - this needs to be run as root!  Do not use    */
  15. /* this program unless you know what you are doing, as it is possible    */
  16. /* that you could confuse parts of your network    / internet.        */
  17. /* Written by R.T.Arnold (arny@geek.org.uk) for Netcraft Ltd.        */
  18. /* (c) copyright 1996 R.T. Arnold.  This is not free software.        */
  19. /************************************************************************/
  20.  
  21. #include<sys/types.h>
  22. #include<sys/socket.h>
  23. #include<netinet/in.h>
  24. #include<arpa/inet.h>
  25. #include<netdb.h>
  26. #include<errno.h>
  27. #include<stdio.h>
  28. #include<stdlib.h>
  29. #include<string.h>
  30.  
  31. #ifndef INADDR_NONE
  32. #define INADDR_NONE     0xffffffff
  33. #endif
  34.  
  35. u_long getip(char*);
  36. u_short getportudp(char*);
  37.  
  38. u_long cptol(u_char*);
  39. u_char *stocp(u_char*,u_short);
  40. u_char *ltocp(u_char*,u_long);
  41.  
  42. union shortunion { u_short s; u_char c[2]; };
  43. union longunion { u_long l; u_char c[4]; };
  44.  
  45. struct sockaddr sa;
  46.  
  47. main(int argc,char **argv)
  48. {
  49. int fd;
  50. int x=1;
  51. u_long host;
  52. u_short service;
  53. struct sockaddr_in *p;
  54. u_char gram[]=
  55.     {
  56.     0x45,    0x00,    0,    0,
  57.     0x00,    0x00,    0x00,    0x00,
  58.     0xFF,    0x11,    0x00,    0x00,
  59.     0,    0,    0,    0,
  60.     0,    0,    0,    0,
  61.  
  62.     0,    0,    0,    0,
  63.     0x00,    0x12,    0x00,    0x00,
  64.  
  65.     '1','2','3','4','5','6','7','8','9','0'
  66.     };
  67.  
  68. /* sanity check... */
  69. if((sizeof(u_char)!=1)||(sizeof(u_short)!=2)||(sizeof(u_long)!=4))
  70.     {
  71.     fprintf(stderr,"error: integer types aren't the right size.\n");
  72.     exit(1);
  73.     };
  74.  
  75. /* it does seem odd that the total length needs to be in host byte order */
  76. stocp(gram+2,(u_short)sizeof(gram));
  77.  
  78. if(argc!=5)
  79.     {
  80.     fprintf(stderr,"usage: %s sourcename sourceport destinationname destinationport\n",*argv);
  81.     exit(1);
  82.     };
  83.  
  84. /* fill in source/destination addresses/ports and the socket address...*/
  85. if(strcmp(argv[1],"255.255.255.255")==0) host=0xffffffff; else if((host=getip(argv[1]))==INADDR_NONE) exit(1);
  86. ltocp(gram+12,host);
  87. if(strcmp(argv[3],"255.255.255.255")==0) host=0xffffffff; else if((host=getip(argv[3]))==INADDR_NONE) exit(1);
  88. ltocp(gram+16,host);
  89. p=(struct sockaddr_in*)&sa;
  90. p->sin_family=AF_INET;
  91. p->sin_addr.s_addr=host;
  92. if(strcmp(argv[2],"0")==0) service=0; else if((service=getportudp(argv[2]))==0) exit(1);
  93. stocp(gram+20,service);
  94. if(strcmp(argv[4],"0")==0) service=0; else if((service=getportudp(argv[4]))==0) exit(1);
  95. stocp(gram+22,service);
  96.  
  97. if((fd=socket(AF_INET,SOCK_RAW,IPPROTO_RAW))== -1)
  98.     {
  99.     perror("socket");
  100.     exit(1);
  101.     };
  102.  
  103. #ifdef IP_HDRINCL
  104. if (setsockopt(fd,IPPROTO_IP,IP_HDRINCL,(char*)&x,sizeof(x))<0)
  105.     {
  106.     perror("setsockopt IP_HDRINCL");
  107.     exit(1);
  108.         };
  109. #else
  110. fprintf(stderr,"the IP_HDRINCL option didn't exist on the system this was compliled on.\n");
  111. #endif
  112.  
  113. if((sendto(fd,&gram,sizeof(gram),0,&sa,sizeof(sa)))== -1)
  114.     {
  115.     perror("sendto");
  116.     exit(1);
  117.     };
  118.  
  119. printf("datagram passed to kernel:");
  120. for(x=0;x<(sizeof(gram)/sizeof(u_char));x++)
  121.     {
  122.     if(!(x%4)) putchar('\n');
  123.     printf("%02x",gram[x]);
  124.     };
  125. putchar('\n');
  126. return(0);
  127. }
  128.  
  129. /* returns IP address (as a u_long) given a hostname string        */
  130.  
  131. u_long getip(char *hostname)
  132. {
  133. u_long ip;
  134. struct hostent *he;
  135. if((ip=inet_addr(hostname))==INADDR_NONE)
  136.     {
  137.     if((he=gethostbyname(hostname))==NULL)
  138.         {
  139.         fprintf(stderr,"error: can't resolve hostname \"%s\"\n",hostname);
  140.         return(INADDR_NONE);
  141.         };
  142.     if(he->h_addrtype!=AF_INET) fprintf(stderr,"warning: resolved address type is not AF_INET, will try to carry on anyway.\n");
  143.     if(he->h_length!=4) fprintf(stderr,"warning: resolved address length is not 4, will try to carry on anyway.\n");
  144.     ip=cptol((u_char*)*(he->h_addr_list));
  145.     };
  146. return(ip);
  147. }
  148.  
  149. /* returns port given a UDP service name or port number            */
  150.  
  151. u_short getportudp(char *service)
  152. {
  153. u_short port;
  154. struct servent *se;
  155. if((port=atoi(service))!=0) return(htons(port));
  156. if((se=getservbyname(service,"udp"))==NULL)
  157.     {
  158.     fprintf(stderr,"error: unknown service \"%s\"\n",service);
  159.     return(0);
  160.     };
  161. return(se->s_port);
  162. }
  163.  
  164. /* the following functions convert between types, hopefully in a way thats portable */
  165.  
  166. u_long cptol(u_char *cp)
  167. {
  168. static union longunion l;
  169. l.c[0]=cp[0];
  170. l.c[1]=cp[1];
  171. l.c[2]=cp[2];
  172. l.c[3]=cp[3];
  173. return(l.l);
  174. }
  175.  
  176. u_char *stocp(u_char *cp,u_short us)
  177. {
  178. static union shortunion s;
  179. s.s=us;
  180. cp[0]=s.c[0];
  181. cp[1]=s.c[1];
  182. return(cp);
  183. }
  184.  
  185. u_char *ltocp(u_char *cp,u_long ul)
  186. {
  187. static union longunion l;
  188. l.l=ul;
  189. cp[0]=l.c[0];
  190. cp[1]=l.c[1];
  191. cp[2]=l.c[2];
  192. cp[3]=l.c[3];
  193. return(cp);
  194. }
  195.  
  196.  
  197.