]> rtime.felk.cvut.cz Git - frescor/fwp.git/blob - fwp/lib/fwp/fwp_msgb.c
77143a2fc402cb77c427f0f38756dafa88bca97c
[frescor/fwp.git] / fwp / lib / fwp / fwp_msgb.c
1 /**************************************************************************/
2 /* ---------------------------------------------------------------------- */
3 /* Copyright (C) 2006 - 2008 FRESCOR consortium partners:                 */
4 /*                                                                        */
5 /*   Universidad de Cantabria,              SPAIN                         */
6 /*   University of York,                    UK                            */
7 /*   Scuola Superiore Sant'Anna,            ITALY                         */
8 /*   Kaiserslautern University,             GERMANY                       */
9 /*   Univ. Politécnica  Valencia,           SPAIN                        */
10 /*   Czech Technical University in Prague,  CZECH REPUBLIC                */
11 /*   ENEA                                   SWEDEN                        */
12 /*   Thales Communication S.A.              FRANCE                        */
13 /*   Visual Tools S.A.                      SPAIN                         */
14 /*   Rapita Systems Ltd                     UK                            */
15 /*   Evidence                               ITALY                         */
16 /*                                                                        */
17 /*   See http://www.frescor.org for a link to partners' websites          */
18 /*                                                                        */
19 /*          FRESCOR project (FP6/2005/IST/5-034026) is funded             */
20 /*       in part by the European Union Sixth Framework Programme          */
21 /*       The European Union is not liable of any use that may be          */
22 /*       made of this code.                                               */
23 /*                                                                        */
24 /*                                                                        */
25 /*  This file is part of FWP (Frescor WLAN Protocol)                      */
26 /*                                                                        */
27 /* FWP is free software; you can redistribute it and/or modify it         */
28 /* under terms of the GNU General Public License as published by the      */
29 /* Free Software Foundation; either version 2, or (at your option) any    */
30 /* later version.  FWP is distributed in the hope that it will be         */
31 /* useful, but WITHOUT ANY WARRANTY; without even the implied warranty    */
32 /* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU    */
33 /* General Public License for more details. You should have received a    */
34 /* copy of the GNU General Public License along with FWP; see file        */
35 /* COPYING. If not, write to the Free Software Foundation, 675 Mass Ave,  */
36 /* Cambridge, MA 02139, USA.                                              */
37 /*                                                                        */
38 /* As a special exception, including FWP header files in a file,          */
39 /* instantiating FWP generics or templates, or linking other files        */
40 /* with FWP objects to produce an executable application, does not        */
41 /* by itself cause the resulting executable application to be covered     */
42 /* by the GNU General Public License. This exception does not             */
43 /* however invalidate any other reasons why the executable file might be  */
44 /* covered by the GNU Public License.                                     */
45 /**************************************************************************/
46 /**
47  * \file fwp_msgb.c
48  *
49  * Routines for manipulation with fwp message buffer (msgb)
50  *
51  */
52
53 #include "fwp_msgb.h"
54 #include "fwp_utils.h"
55 #include <stdlib.h>
56
57 /*unsigned char* fwp_msgb_dealloc(size_t buf_size) */
58 /*unsigned char* fwp_msgb_pool_init(size_t buf_size) */
59
60 /**
61  * Allocates msgb.
62  *
63  * \param buf_size Size of msgb
64  * \return Allocated msgb
65  *
66  */
67 struct fwp_msgb* fwp_msgb_alloc(size_t buf_size)
68 {
69         struct fwp_msgb* msgb;
70
71         /*fwp_msgb_pool_dequeue; other choice*/
72         msgb = (struct fwp_msgb*) malloc(sizeof(struct fwp_msgb) + buf_size);
73         if (!msgb)
74                 return NULL;
75         
76         msgb->buffer_size = buf_size;
77         msgb->len = 0;
78         msgb->data = (unsigned char*) msgb + sizeof(struct fwp_msgb);
79         msgb->tail = msgb->data;
80         msgb->peer = NULL;
81         
82         return msgb;
83 }
84
85 /**
86  * Deallocates msgb.
87  *
88  * \param msgb Pointer to msgb
89  *
90  */
91 void fwp_msgb_free(struct fwp_msgb *msgb)
92 {
93         free((void*) msgb);
94         msgb = NULL;
95 }
96
97 /**
98  * Routine is usually called after putting data of length len to msgb 
99  * to adjust internal tail pointer and len fields of msgb
100  *
101  * \param msgb Pointer to msgb
102  * \param len The lenght data put to msgb 
103  * \return Previous tail pointer
104  *
105  */
106 inline unsigned char* fwp_msgb_put(struct fwp_msgb *msgb, unsigned int len)
107 {
108         unsigned char *tmp= msgb->tail;
109         
110         msgb->tail+=len;
111         msgb->len+=len;
112         return tmp;
113 }
114
115 /**
116  * Routine is usually called after reading data of lenght len from msgb to 
117  * adjust internal data pointer and len of msgb
118  *
119  * \param msgb Pointer to msgb
120  * \param len The lenght data 
121  * \return Previous tail pointer on NULL in case of lack of space in the buffer.
122  *
123  */
124 inline unsigned char* fwp_msgb_pull(struct fwp_msgb *msgb, unsigned int len)
125 {
126         if (len > msgb->len)
127                 return NULL;
128         
129         msgb->len -= len;
130         return msgb->data += len;
131 }
132
133 /**
134  * Routine is usually called after fwp_msgb_reserve after reserved area is filled 
135  * adjust pointers 
136  *
137  * \param msgb Pointer to msgb
138  * \param len The lenght data 
139  * \return Current data pointer
140  *
141  */ 
142 inline unsigned char* fwp_msgb_push(struct fwp_msgb* msgb, unsigned int len)
143 {
144         msgb->data-=len;
145         msgb->len+=len;
146         return msgb->data;
147 }
148
149 inline unsigned char* fwp_msgb_shift(struct fwp_msgb *msgb, unsigned int len)
150 {
151         if (msgb->data + len > msgb->tail)
152                 return NULL;
153         
154         return msgb->data += len;
155 }
156
157 /**
158  * Sets data pointer to the start of buffer
159  *
160  * \param msgb Pointer to msgb
161  *
162  */
163 inline void fwp_msgb_reset_data_pointer(struct fwp_msgb *msgb)
164 {
165         msgb->data = (unsigned char*) msgb + sizeof(struct fwp_msgb);
166 }
167
168 /**
169  * Sets data pointer to the start of buffer and the length of used data to zero
170  *
171  * \param msgb Pointer to msgb
172  */
173 inline void fwp_msgb_reset_data(struct fwp_msgb* msgb)
174 {
175         msgb->len = 0;
176         msgb->data = (unsigned char*) msgb + sizeof(struct fwp_msgb);
177         msgb->tail = msgb->data;
178 }
179
180 /**
181  * Reserve place of length len in msgb
182  *
183  * \param msgb Pointer to msgb
184  * \param len The lenght data 
185  */
186 inline void fwp_msgb_reserve(fwp_msgb_t *msgb, unsigned int len)
187 {
188         msgb->data+=len;
189         msgb->tail+=len;
190 }
191
192 /*struct fwp_socket* fwp_socket_create(struct sockaddr *_addr, socklen_t _addrlen)
193 {
194         struct fwp_socket* fwpsock;
195         unsigned char *addr;
196         
197         fwpsock = (struct fwp_socket*) malloc(sizeof(struct fwp_socket) + 
198                                               _addrlen);
199         if (!fwpsock)
200                 return NULL;
201         
202         addr = (unsigned char*) fwpsock + sizeof(struct fwp_socket);
203         memcpy(addr, (void*) _addr, _addrlen);
204
205         fwpsock->addr = (struct sockaddr*) addr;
206         fwpsock->addrlen = _addrlen;
207
208         return fwpsock;
209 }
210
211 inline void fwp_socket_set(struct fwp_socket *fwpsock, struct sockaddr *_addr, 
212                                   socklen_t _addrlen)
213 {       
214         fwpsock->addr = _addr;
215         fwpsock->addrlen = _addrlen;
216 }
217
218 void fwp_socket_free(struct fwp_socket *fwpsock)
219 {
220         free((void*)fwpsock);
221         fwpsock = NULL;
222 }*/