home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / Exploit and vulnerability / w00w00 / sectools / fragrouter / Libnet-0.99b / src / build_icmp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-07-26  |  5.7 KB  |  171 lines

  1. /*
  2.  *  $Id: build_icmp.c,v 1.1.1.1 1999/05/18 15:33:42 dugsong Exp $
  3.  *
  4.  *  libnet
  5.  *  build_icmp.c - ICMP packet assembler
  6.  *
  7.  *  Copyright (c) 1998, 1999 Mike D. Schiffman <mike@infonexus.com>
  8.  *                           route|daemon9 <route@infonexus.com>
  9.  *  All rights reserved.
  10.  *
  11.  * Redistribution and use in source and binary forms, with or without
  12.  * modification, are permitted provided that the following conditions
  13.  * are met:
  14.  * 1. Redistributions of source code must retain the above copyright
  15.  *    notice, this list of conditions and the following disclaimer.
  16.  * 2. Redistributions in binary form must reproduce the above copyright
  17.  *    notice, this list of conditions and the following disclaimer in the
  18.  *    documentation and/or other materials provided with the distribution.
  19.  *
  20.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  21.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  24.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  26.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  27.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  28.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  29.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30.  * SUCH DAMAGE.
  31.  *
  32.  */
  33.  
  34. #if (HAVE_CONFIG_H)
  35. #include "../include/config.h"
  36. #endif
  37. #include "../include/libnet.h"
  38.  
  39. void
  40. build_icmp_echo(u_char type, u_char code, u_short id, u_short seq,
  41.         const u_char *payload, int payload_s, u_char *buf)
  42. {
  43.     struct libnet_icmp_hdr icmp_hdr;
  44.  
  45.     assert(buf);
  46.  
  47.     icmp_hdr.icmp_type = type;         /* packet type */
  48.     icmp_hdr.icmp_code = code;         /* packet code */
  49.     icmp_hdr.icmp_id   = htons(id);    /* packet id */
  50.     icmp_hdr.icmp_seq  = htons(seq);   /* packet seq */
  51.  
  52.     if (payload && payload_s)
  53.     {
  54.         /*
  55.          *  Unchecked runtime error for buf + ICMP_H payload to be greater than
  56.          *  the allocated heap memory.
  57.          */
  58.         memcpy(buf + ICMP_H, payload, payload_s);
  59.     }
  60.     memcpy(buf, &icmp_hdr, sizeof(icmp_hdr));
  61. }
  62.  
  63.  
  64. void
  65. build_icmp_mask(u_char type, u_char code, u_short id, u_short seq, u_long mask,
  66.         const u_char *payload, int payload_s, u_char *buf)
  67. {
  68.     struct libnet_icmp_hdr icmp_hdr;
  69.  
  70.     assert(buf);
  71.  
  72.     icmp_hdr.icmp_type = type;         /* packet type */
  73.     icmp_hdr.icmp_code = code;         /* packet code */
  74.     icmp_hdr.icmp_id   = htons(id);    /* packet id */
  75.     icmp_hdr.icmp_seq  = htons(seq);   /* packet seq */
  76.     icmp_hdr.icmp_mask = htonl(mask);  /* address mask */
  77.  
  78.     if (payload && payload_s)
  79.     {
  80.         /*
  81.          *  Unchecked runtime error for buf + ICMP_H payload to begreater than
  82.          *  the allocated heap memory.
  83.          */
  84.         memcpy(buf + ICMP_H, payload, payload_s);
  85.     }
  86.     memcpy(buf, &icmp_hdr, sizeof(icmp_hdr));
  87. }
  88.  
  89.  
  90. void
  91. build_icmp_unreach(u_char type, u_char code, u_short orig_len, u_char orig_tos,
  92.         u_short orig_id, u_short orig_frag, u_char orig_ttl, u_char orig_prot, 
  93.         u_long orig_src, u_long orig_dst, const u_char *orig_payload, int
  94.         payload_s, u_char *buf)
  95. {
  96.     struct libnet_icmp_hdr icmp_hdr;
  97.  
  98.     assert(buf);
  99.  
  100.     icmp_hdr.icmp_type = type;          /* packet type */
  101.     icmp_hdr.icmp_code = code;          /* packet code */
  102.     icmp_hdr.icmp_id   = 0;             /* must be 0 */
  103.     icmp_hdr.icmp_seq  = 0;             /* must be 0 */
  104.  
  105.     /*
  106.      *  How convenient!  We can use our build_ip function to tack on the
  107.      *  original header!
  108.      */
  109.     build_ip(0, orig_tos, orig_id, orig_frag, orig_ttl, orig_prot, orig_src,
  110.             orig_dst, orig_payload, payload_s, buf + ICMP_UNREACH_H);
  111.  
  112.     memcpy(buf, &icmp_hdr, ICMP_UNREACH_H);
  113. }
  114.  
  115.  
  116. void
  117. build_icmp_timeexceed(u_char type, u_char code, u_short orig_len,
  118.         u_char orig_tos, u_short orig_id, u_short orig_frag, u_char orig_ttl,
  119.         u_char orig_prot, u_long orig_src, u_long orig_dst,
  120.         const u_char *orig_payload, int payload_s, u_char *buf)
  121. {
  122.     struct libnet_icmp_hdr icmp_hdr;
  123.  
  124.     assert(buf);
  125.  
  126.     icmp_hdr.icmp_type = type;          /* packet type */
  127.     icmp_hdr.icmp_code = code;          /* packet code */
  128.     icmp_hdr.icmp_id   = 0;             /* must be 0 */
  129.     icmp_hdr.icmp_seq  = 0;             /* must be 0 */
  130.  
  131.     /*
  132.      *  How convenient!  We can use our build_ip function to tack on the
  133.      *  original header!
  134.      */
  135.     build_ip(0, orig_tos, orig_id, orig_frag, orig_ttl, orig_prot, orig_src,
  136.             orig_dst, orig_payload, payload_s, buf + ICMP_TIMXCEED_H);
  137.  
  138.     memcpy(buf, &icmp_hdr, ICMP_TIMXCEED_H);
  139. }
  140.  
  141.  
  142. void
  143. build_icmp_timestamp(u_char type, u_char code, u_short id, u_short seq,
  144.         n_time otime, n_time rtime, n_time ttime, const u_char *payload,
  145.         int payload_s, u_char *buf)
  146. {
  147.     struct libnet_icmp_hdr icmp_hdr;
  148.  
  149.     assert(buf);
  150.  
  151.     icmp_hdr.icmp_type   = type;            /* packet type */
  152.     icmp_hdr.icmp_code   = code;            /* packet code */
  153.     icmp_hdr.icmp_id     = htons(id);       /* packet id */
  154.     icmp_hdr.icmp_seq    = htons(seq);      /* packet seq */
  155.     icmp_hdr.icmp_otime  = htonl(otime);    /* original timestamp */
  156.     icmp_hdr.icmp_rtime  = htonl(rtime);    /* receive timestamp */
  157.     icmp_hdr.icmp_ttime  = htonl(ttime);    /* transmit timestamp */
  158.  
  159.     if (payload && payload_s)
  160.     {
  161.         /*
  162.          *  Unchecked runtime error for buf + ICMP_TS_H payload to be greater
  163.          *  than the allocated heap memory.
  164.          */
  165.         memcpy(buf + ICMP_TS_H, payload, payload_s);
  166.     }
  167.     memcpy(buf, &icmp_hdr, sizeof(icmp_hdr));
  168. }
  169.  
  170. /* EOF */
  171.