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_tcpo.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-07-26  |  3.3 KB  |  114 lines

  1. /*
  2.  *  $Id: insert_tcpo.c,v 1.1.1.1 1999/05/18 15:33:42 dugsong Exp $
  3.  *
  4.  *  libnet
  5.  *  insert_tcpo.c - inserts TCP 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_tcpo(struct tcpoption *opt, u_char opt_len, u_char *buf)
  41. {
  42.     struct ip *ip_hdr;
  43.     struct tcphdr *tcp_hdr;
  44.     u_char *p;
  45.     u_short s, j;
  46.     u_char i;
  47.  
  48.     assert(buf);
  49.  
  50.     ip_hdr = (struct ip *)(buf);
  51.  
  52.     if (ip_hdr->ip_p != IPPROTO_TCP)
  53.     {
  54.         /*
  55.          *  Hey retard, where's the TCP header?
  56.          */
  57. #if (__DEBUG)
  58.         fprintf(stderr,
  59.             "insert_tcpo: tried to insert TCP options into a NON TCP packet!\n");
  60. #endif
  61.         return (-1);
  62.     }
  63.  
  64.     s = UNFIX(ip_hdr->ip_len);
  65.  
  66.     if ((s + opt_len) > IP_MAXPACKET)
  67.     {
  68.         /*
  69.          *  Nope.  Too big.
  70.          */
  71. #if (__DEBUG)
  72.         fprintf(stderr,
  73.             "insert_tcpo: options list would result in too large of a packet\n");
  74. #endif
  75.         return (-1);
  76.     }
  77.  
  78.     tcp_hdr = (struct tcphdr *)(buf + IP_H);
  79.     /*
  80.      *  Do we have more then just an TCP header?  (We are ignoring the IP
  81.      *  header at this point).
  82.      */
  83.     if (s > IP_H + TCP_H)
  84.     {
  85.         /*
  86.          *  Move over whatever's in the way.
  87.          */
  88.         memmove((u_char *)tcp_hdr + TCP_H + opt_len, (u_char *)tcp_hdr +
  89.             TCP_H, opt_len);
  90.     }
  91.  
  92.     /*
  93.      *  Copy over option list.  We rely on the programmer having been
  94.      *  smart enough to allocate enough heap memory here.  Uh oh.
  95.      */
  96.     p = (u_char *)tcp_hdr + TCP_H;
  97.     memcpy((u_char *)p, opt->tcpopt_list, opt_len);
  98.  
  99.     /*
  100.      *  Count up number of 32-bit words in options list, padding if
  101.      *  neccessary.
  102.      */
  103.     for (i = 0, j = 0; i < opt_len; i++) (i % 4) ? j : j++;
  104.  
  105.     tcp_hdr->th_off += j;
  106.     tcp_hdr->th_sum = 0;
  107.     ip_hdr->ip_len  = FIX(opt_len + s);
  108.  
  109.     return (1);
  110. }
  111.  
  112.  
  113. /* EOF */
  114.