1 /* $Id: packet.c,v 1.1 2005/12/12 18:14:22 nobunaga Exp $
3 Copyright (C) 2002 Marc Kirchner <kirchner@stud.fh-heilbronn.de>
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 @name int pkt_init(struct packet *pkt, unsigned long type, unsigned int size)
25 @desc Allocates memory for a new packet and initializes its internal variables
26 @param pkt pointer to the new packet
27 @param type type of the packet. Its setting is currently ignored, but this is likely to change. Possible values are e.g. PKT_NET_IP|PKT_TRANS_TCP. See packet.h for other values
28 @param size overall size if the packet not including ethernet headers or trailers.
32 pkt_init(struct packet *pkt, unsigned long type, unsigned int size)
34 if ((pkt->pkt = (unsigned char *) malloc(size)) == NULL) {
35 /* no mem, we assume errno will be set */
39 memset(pkt->pkt, 0, size);
43 /* set active ptr to beginning of mem */
44 pkt->pkt_ptr = pkt->pkt;
52 @name int pkt_free(struct packet *pkt)
53 @desc Used to destroy a packet and to free used memory
54 @param pkt the packet to destroy
58 pkt_free(struct packet *pkt)
70 @name int pkt_set_actptr(struct packet *pkt, unsigned int bytepos)
71 @desc This function sets the active pointer position inside the packet.
72 @param pkt the packet whose active ptr is to be set
73 @param bytepos the byte position where to set the active ptr to, starting from 0
78 pkt_set_actptr(struct packet *pkt, unsigned int bytepos)
82 if (bytepos > pkt->pkt_size) {
85 pkt->pkt_ptr = pkt->pkt + bytepos;
92 @name int pkt_move_actptr(struct packet *pkt, int relmov)
93 @desc This function moves the active pointer inside the packet.
94 @param pkt the packet whose active ptr is to be moved
95 @param relmov number of bytes the active ptr shall be moved. To move it backward, negative values may be used.
99 pkt_move_actptr(struct packet *pkt, int relmov)
103 if ((pkt->pkt_pos + relmov > pkt->pkt_size) ||
104 (pkt->pkt_pos + relmov < 0)) {
107 pkt->pkt_ptr += relmov;
114 @name int pkt_add_data(struct packet *pkt, char *data, size_t data_len)
115 @desc Adds supplied data at the current active ptr position of the given packet.This basically is a memcpy() wrapper function.
116 @param pkt data will be written into this packet
117 @param data a pointer to the data that shall be copied into the packet
118 @param data_len the amount of data (in bytes) that will be copied
122 pkt_add_data(struct packet *pkt, char *data, size_t data_len)
126 if (pkt->pkt_size >= (pkt->pkt_pos + data_len)) {
127 memcpy(pkt->pkt_ptr, data, data_len);
135 @name int pkt_resize(struct packet *pkt, unsigned int newsize)
136 @desc The given packet will be resized to newsize
137 @param pkt the packet to resize
138 @param newsize the new size of the packet
142 pkt_resize(struct packet *pkt, unsigned int newsize)
144 unsigned char *newpkt;
149 if ((newpkt = (unsigned char *) malloc(newsize)) != NULL) {
150 memset(newpkt, 0, newsize);
151 memcpy(newpkt, pkt->pkt, (pkt->pkt_size < newsize ? pkt->pkt_size : newsize));
152 pkt->pkt_size = newsize;
153 pkt->pkt_ptr = newpkt;