]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/capture/cap_debug/packet.c
More fixes after cppcheck.
[stg.git] / projects / stargazer / plugins / capture / cap_debug / packet.c
1 /* $Id: packet.c,v 1.1 2005/12/12 18:14:22 nobunaga Exp $ 
2
3 Copyright (C) 2002 Marc Kirchner <kirchner@stud.fh-heilbronn.de>
4
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.
9
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.
14
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
18 */
19
20 #include "libpal.h"
21
22 /*
23 <++doc++>
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.
29 <--doc-->
30 */
31 int
32 pkt_init(struct packet *pkt, unsigned long type, unsigned int size)
33 {
34         if ((pkt->pkt = (unsigned char *) malloc(size)) == NULL) {
35                 /* no mem, we assume errno will be set */
36                 return -1;
37         } else {
38                 /* clear mem out */
39                 memset(pkt->pkt, 0, size);
40                 /* init vars */
41                 pkt->pkt_type = type;
42                 pkt->pkt_size = size;
43                 /* set active ptr to beginning of mem */
44                 pkt->pkt_ptr = pkt->pkt;
45                 pkt->pkt_pos = 0;
46                 return 0;
47         }
48 }
49
50 /*
51 <++doc++>
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
55 <--doc-->
56 */
57 int
58 pkt_free(struct packet *pkt)
59 {
60         if (pkt) {
61                 free(pkt->pkt);
62                 pkt->pkt=NULL;
63                 return 0;
64         }
65         return EPKTINVALPTR;
66 }
67
68 /*
69 <++doc++>
70 @name int pkt_move_actptr(struct packet *pkt, int relmov) 
71 @desc This function moves the active pointer inside the packet.
72 @param pkt the packet whose active ptr is to be moved
73 @param relmov number of bytes the active ptr shall be moved. To move it backward, negative values may be used.
74 <--doc-->
75 */
76 int 
77 pkt_move_actptr(struct packet *pkt, int relmov)
78 {
79         if (!pkt)
80                 return EPKTINVALPTR;
81         if ((pkt->pkt_pos + relmov > pkt->pkt_size) ||
82                 (pkt->pkt_pos + relmov < 0)) {
83                 return EPKTRANGE;
84         } else {
85                 pkt->pkt_ptr += relmov;
86                 return PKTOK;
87         }
88 }