]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/capture/cap_debug/packet.c
Добавление исходников
[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_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
74 <--doc-->
75 */
76
77 int
78 pkt_set_actptr(struct packet *pkt, unsigned int bytepos)
79 {
80         if (!pkt)
81                 return EPKTINVALPTR;
82         if (bytepos > pkt->pkt_size) {
83                 return EPKTRANGE;
84         } else {
85                 pkt->pkt_ptr = pkt->pkt + bytepos;
86                 return PKTOK;
87         }
88 }
89
90 /*
91 <++doc++>
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.
96 <--doc-->
97 */
98 int 
99 pkt_move_actptr(struct packet *pkt, int relmov)
100 {
101         if (!pkt)
102                 return EPKTINVALPTR;
103         if ((pkt->pkt_pos + relmov > pkt->pkt_size) ||
104                 (pkt->pkt_pos + relmov < 0)) {
105                 return EPKTRANGE;
106         } else {
107                 pkt->pkt_ptr += relmov;
108                 return PKTOK;
109         }
110 }
111
112 /*
113 <++doc++>
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
119 <--doc-->
120 */
121 int
122 pkt_add_data(struct packet *pkt, char *data, size_t data_len)
123 {
124         if (!pkt || !data)
125                 return EPKTINVALPTR;
126         if (pkt->pkt_size >= (pkt->pkt_pos + data_len)) {
127                 memcpy(pkt->pkt_ptr, data, data_len);
128                 return 0;
129         } else
130                 return EPKTRANGE;
131 }
132
133 /*
134 <++doc++>
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
139 <--doc-->
140 */
141 int
142 pkt_resize(struct packet *pkt, unsigned int newsize)
143 {
144         unsigned char *newpkt;
145
146         if (!pkt)
147                 return EPKTINVALPTR;
148
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;
154                 pkt->pkt_pos = 0;
155                 /* free old mem */
156                 free (pkt->pkt);
157                 pkt->pkt = newpkt;
158                 return 0;
159         } else
160                 return EERRNO;
161 }