2 * $Id: libnet_packet_mem.c,v 1.1.1.1 2000/05/25 00:28:49 route Exp $
5 * libnet_packet_mem.c - Packet memory managment routines.
7 * Copyright (c) 1998 - 2001 Mike D. Schiffman <mike@infonexus.com>
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include "../include/config.h"
36 #include "../include/libnet.h"
39 libnet_init_packet(int p_size, u_char **buf)
44 * This is a reasonably good choice. Aligned for your happiness.
46 p_size = LIBNET_MAX_PACKET + 1;
49 *buf = (u_char *)malloc(p_size);
53 perror("init_packet malloc failed:");
59 memset(*buf, 0, p_size);
66 libnet_destroy_packet(u_char **buf)
81 libnet_init_packet_arena(struct libnet_arena **arena, int p_size,
97 * This is a reasonably good choice. Aligned for your happiness.
99 p_size = LIBNET_MAX_PACKET + 1;
103 * Align the packets in the arena on a 4-byte boundary, suitable for
104 * strict architectures (such as SPARC).
113 /* Reasonable AND sensible. */
117 arena_size = (p_size * p_num);
120 if ((alignment_amt = (arena_size - (p_size * p_num))) != 0)
122 libnet_error(LIBNET_ERR_WARNING,
123 "init_packet_arena: had to align %d byte(s)\n",
128 (*arena)->memory_pool = (u_char *)malloc(arena_size);
129 if (!(*arena)->memory_pool)
132 perror("init_packet_arena malloc failed:");
137 memset((*arena)->memory_pool, 0, arena_size);
138 (*arena)->current = 0;
139 (*arena)->size = arena_size;
145 libnet_next_packet_from_arena(struct libnet_arena **arena, int p_size)
155 * This is a reasonably good choice. Aligned for your happiness.
157 p_size = LIBNET_MAX_PACKET + 1;
161 * Align the arena on a 4-byte boundary, suitable for strict architectures
170 * I'm not worried about overflow here. If you actually have somehow
171 * allocated 4 gigs of memory, you're out of control in the first place.
173 if (((*arena)->current + p_size) > (*arena)->size)
176 libnet_error(LIBNET_ERR_CRITICAL,
177 "next_packet_arena ran out of memory\n");
183 * Special case of empty arena.
185 if ((*arena)->current == 0)
187 (*arena)->current = p_size;
188 return ((*arena)->memory_pool);
191 (*arena)->current += p_size;
192 return ((*arena)->memory_pool + (*arena)->current);
197 libnet_destroy_packet_arena(struct libnet_arena **arena)
204 free((*arena)->memory_pool);
206 (*arena)->memory_pool = NULL;
207 (*arena)->current = -1;