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

  1. /*
  2.  *  $Id: insert_ipo.c,v 1.2 1999/09/21 15:47:33 dugsong Exp $
  3.  *
  4.  *  libnet
  5.  *  insert_ipo.c - inserts IP options into a prebuilt IP packet
  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. int
  40. insert_ipo(struct ipoption *opt, u_char opt_len, u_char *buf)
  41. {
  42.     struct ip *ip_hdr;
  43.     u_short s, ip_hl, pad;
  44.     u_char *p;
  45.  
  46.     assert(buf);
  47.  
  48.     ip_hdr = (struct ip *)(buf);
  49.     ip_hl = ip_hdr->ip_hl * 4;
  50.  
  51.     s = ntohs(ip_hdr->ip_len);
  52.     
  53.     /* See how much to pad options out (always terminating with EOL). */
  54.     pad = 4 - (((ip_hl - IP_H) + opt_len) & 3);
  55.  
  56.     if ((s + opt_len + pad) > IP_MAXPACKET)
  57.     {
  58.         /* Nope.  Too big. */
  59. #if (__DEBUG)
  60.         fprintf(stderr,
  61.             "insert_ipo: options list would result in too large of a packet\n");
  62. #endif
  63.         return (-1);
  64.     }
  65.  
  66.     /* Do we have more then just an IP header? */
  67.     if (s > ip_hl)
  68.     {
  69.         /* Move over whatever's in the way. */
  70.         memmove(buf + ip_hl + opt_len + pad, buf + ip_hl, opt_len + pad);
  71.     }
  72.  
  73.     /*
  74.      *  Copy over option list.  We rely on the programmer having been
  75.      *  smart enough to allocate enough heap memory here.  Uh oh.
  76.      */
  77.     p = buf + ip_hl;
  78.     memcpy(p, opt->ipopt_list, opt_len);
  79.  
  80.     /* XXX - this should be 1 if more options are to follow. */
  81.     memset(p + opt_len, 0, pad); 
  82.  
  83.     ip_hdr->ip_hl   += (opt_len + pad) / 4;
  84.     ip_hdr->ip_len  = htons(s + opt_len + pad);
  85.  
  86.     return (1);
  87. }
  88.  
  89. /* EOF */
  90.