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

  1. /*
  2.  *  $Id: packet_mem.c,v 1.1.1.1 1999/05/18 15:33:42 dugsong Exp $
  3.  *
  4.  *  libnet
  5.  *  packet_mem.c - Packet memory managment routines.
  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. init_packet(size_t p_size, u_char **buf)
  41. {
  42.     if (p_size <= 0)
  43.     {
  44.         /*
  45.          *  This is a reasonably good choice.  Aligned for your happiness.
  46.          */
  47.         p_size = MAX_PACKET - 3;
  48.     }
  49.  
  50.     *buf = (u_char *)malloc(p_size);
  51.     if (!*buf)
  52.     {
  53. #if (__DEBUG)
  54.         perror("init_packet malloc failed:");
  55. #endif
  56.         return (-1);
  57.     }
  58.     else
  59.     {
  60.         memset(*buf, 0, p_size);
  61.         return (1);
  62.     }
  63. }
  64.  
  65.  
  66. void
  67. destroy_packet(u_char **buf)
  68. {
  69.     assert(*buf);
  70.  
  71.     free(*buf);
  72.     *buf = NULL;
  73. }
  74.  
  75.  
  76. int
  77. init_packet_arena(struct libnet_arena **arena, u_short p_size, u_short p_num)
  78. {
  79.     u_long arena_size;
  80. #if (__DEBUG)
  81.     int alignment_amt;
  82. #endif
  83.  
  84.     assert(*arena);
  85.  
  86.     if (p_num <= 0)
  87.     {
  88.         /* Reasonable AND sensible. */
  89.         p_num = 10;
  90.     }
  91.  
  92.     if (p_size <= 0)
  93.     {
  94.         /*
  95.          *  This is a reasonably good choice.  Aligned for your happiness.
  96.          */
  97.         p_size = MAX_PACKET - 3;
  98.     }
  99.  
  100.     arena_size = (p_size * p_num);
  101.  
  102.     /*
  103.      *  Align the arena on a 4-byte boundary, suitable for strict architectures
  104.      *  (such as SPARC).
  105.      */
  106.     while (arena_size % 4)
  107.     {
  108.         ++arena_size;
  109.     }
  110. #if (__DEBUG)
  111.     if ((alignment_amt = (arena_size - (p_size * p_num))) != 0)
  112.     {
  113.         fprintf(stderr, "init_packet_arena: had to align %d byte(s)\n",
  114.                 alignment_amt);
  115.     }
  116. #endif
  117.  
  118.     (*arena)->memory_pool = (u_char *)malloc(arena_size);
  119.     if (!(*arena)->memory_pool)
  120.     {
  121. #if (__DEBUG)
  122.         perror("init_packet_arena malloc failed:");
  123. #endif
  124.         return (-1);
  125.     }
  126.  
  127.     memset((*arena)->memory_pool, 0, arena_size);
  128.     (*arena)->current  = 0;
  129.     (*arena)->size     = arena_size;
  130.     return (1);
  131. }
  132.  
  133.  
  134. u_char *
  135. next_packet_from_arena(struct libnet_arena **arena, u_short p_size)
  136. {
  137.     assert(*arena);
  138.  
  139.     if (p_size <= 0)
  140.     {
  141.         /*
  142.          *  This is a reasonably good choice.  Aligned for your happiness.
  143.          */
  144.         p_size = MAX_PACKET - 3;
  145.     }
  146.  
  147.     /*
  148.      *  Align the arena on a 4-byte boundary, suitable for strict architectures
  149.      *  (such as SPARC).
  150.      */
  151.     while (p_size % 4)
  152.     {
  153.         ++p_size;
  154.     }
  155.  
  156.     /*
  157.      *  I'm not worried about overflow here.  If you actually have somehow
  158.      *  allocated 4 gigs of memory, you're out of control in the first place.
  159.      */
  160.     if (((*arena)->current + p_size) > (*arena)->size)
  161.     {
  162. #if (__DEBUG)
  163.         fprintf(stderr, "next_packet_arena ran out of memory\n");
  164. #endif
  165.         return (NULL);
  166.     }
  167.  
  168.     (*arena)->current += p_size;
  169.  
  170.     return ((*arena)->memory_pool + (*arena)->current);
  171. }
  172.  
  173.  
  174. void
  175. destroy_packet_arena(struct libnet_arena **arena)
  176. {
  177.     assert(*arena);
  178.  
  179.     free((*arena)->memory_pool);
  180.  
  181.     (*arena)->memory_pool = NULL;
  182.     (*arena)->current     = -1;
  183.     (*arena)->size        = 0;
  184. }
  185.  
  186.  
  187. /* EOF */
  188.