OSDN Git Service

2013.10.24
[uclinux-h8/uClinux-dist.git] / lib / Libnet / src / libnet_packet_mem.c
1 /*
2  *  $Id: libnet_packet_mem.c,v 1.1.1.1 2000/05/25 00:28:49 route Exp $
3  *
4  *  libnet
5  *  libnet_packet_mem.c - Packet memory managment routines.
6  *
7  *  Copyright (c) 1998 - 2001 Mike D. Schiffman <mike@infonexus.com>
8  *  All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
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.
18  *
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
29  * SUCH DAMAGE.
30  *
31  */
32
33 #if (HAVE_CONFIG_H)
34 #include "../include/config.h"
35 #endif
36 #include "../include/libnet.h"
37
38 int
39 libnet_init_packet(int p_size, u_char **buf)
40 {
41     if (p_size <= 0)
42     {
43         /*
44          *  This is a reasonably good choice.  Aligned for your happiness.
45          */
46         p_size = LIBNET_MAX_PACKET + 1;
47     }
48
49     *buf = (u_char *)malloc(p_size);
50     if (!*buf)
51     {
52 #if (__DEBUG)
53         perror("init_packet malloc failed:");
54 #endif
55         return (-1);
56     }
57     else
58     {
59         memset(*buf, 0, p_size);
60         return (1);
61     }
62 }
63
64
65 int
66 libnet_destroy_packet(u_char **buf)
67 {
68     if (!*buf)
69     {
70         return (-1);
71     }
72
73     free(*buf);
74     *buf = NULL;
75
76     return (1);
77 }
78
79
80 int
81 libnet_init_packet_arena(struct libnet_arena **arena, int p_size,
82             u_short p_num)
83 {
84     u_long arena_size;
85 #if (__DEBUG)
86     int alignment_amt;
87 #endif
88
89     if (!*arena)
90     {
91         return (-1);
92     }
93
94     if (p_size <= 0)
95     {
96         /*
97          *  This is a reasonably good choice.  Aligned for your happiness.
98          */
99         p_size = LIBNET_MAX_PACKET + 1;
100     }
101
102     /*
103      *  Align the packets in the arena on a 4-byte boundary, suitable for
104      *  strict architectures (such as SPARC).
105      */
106     while (p_size % 4)
107     {
108         ++p_size;
109     }
110
111     if (p_num <= 0)
112     {
113         /* Reasonable AND sensible. */
114         p_num = 3;
115     }
116
117     arena_size = (p_size * p_num);
118
119 #if (__DEBUG)
120     if ((alignment_amt = (arena_size - (p_size * p_num))) != 0)
121     {
122         libnet_error(LIBNET_ERR_WARNING,
123                 "init_packet_arena: had to align %d byte(s)\n",
124                 alignment_amt);
125     }
126 #endif
127
128     (*arena)->memory_pool = (u_char *)malloc(arena_size);
129     if (!(*arena)->memory_pool)
130     {
131 #if (__DEBUG)
132         perror("init_packet_arena malloc failed:");
133 #endif
134         return (-1);
135     }
136
137     memset((*arena)->memory_pool, 0, arena_size);
138     (*arena)->current  = 0;
139     (*arena)->size     = arena_size;
140     return (1);
141 }
142
143
144 u_char *
145 libnet_next_packet_from_arena(struct libnet_arena **arena, int p_size)
146 {
147     if (!*arena)
148     {
149         return (NULL);
150     }
151
152     if (p_size <= 0)
153     {
154         /*
155          *  This is a reasonably good choice.  Aligned for your happiness.
156          */
157         p_size = LIBNET_MAX_PACKET + 1;
158     }
159
160     /*
161      *  Align the arena on a 4-byte boundary, suitable for strict architectures
162      *  (such as SPARC).
163      */
164     while (p_size % 4)
165     {
166         ++p_size;
167     }
168
169     /*
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.
172      */
173     if (((*arena)->current + p_size) > (*arena)->size)
174     {
175 #if (__DEBUG)
176         libnet_error(LIBNET_ERR_CRITICAL,
177                 "next_packet_arena ran out of memory\n");
178 #endif
179         return (NULL);
180     }
181
182     /*
183      *  Special case of empty arena.
184      */
185     if ((*arena)->current == 0)
186     {
187         (*arena)->current = p_size;
188         return ((*arena)->memory_pool);
189     }
190
191     (*arena)->current += p_size;
192     return ((*arena)->memory_pool + (*arena)->current);
193 }
194
195
196 int
197 libnet_destroy_packet_arena(struct libnet_arena **arena)
198 {
199     if (!*arena)
200     {
201         return (-1);
202     }
203
204     free((*arena)->memory_pool);
205
206     (*arena)->memory_pool = NULL;
207     (*arena)->current     = -1;
208     (*arena)->size        = 0;
209
210     return (1);
211 }
212
213
214 /* EOF */