1 /**************************************************************************/
2 /* ---------------------------------------------------------------------- */
3 /* Copyright (C) 2006 - 2008 FRESCOR consortium partners: */
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 */
12 /* Thales Communication S.A. FRANCE */
13 /* Visual Tools S.A. SPAIN */
14 /* Rapita Systems Ltd UK */
17 /* See http://www.frescor.org for a link to partners' websites */
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. */
25 /* This file is part of FWP (Frescor WLAN Protocol) */
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. */
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 /**************************************************************************/
49 * Routines for manipulation with fwp message buffer (msgb)
54 #include "fwp_utils.h"
57 /*unsigned char* fwp_msgb_dealloc(size_t buf_size) */
58 /*unsigned char* fwp_msgb_pool_init(size_t buf_size) */
63 * \param buf_size Size of msgb
64 * \return Allocated msgb
67 struct fwp_msgb* fwp_msgb_alloc(size_t buf_size)
69 struct fwp_msgb* msgb;
71 /*fwp_msgb_pool_dequeue; other choice*/
72 msgb = (struct fwp_msgb*) malloc(sizeof(struct fwp_msgb) + buf_size);
76 msgb->buffer_size = buf_size;
78 msgb->data = (unsigned char*) msgb + sizeof(struct fwp_msgb);
79 msgb->tail = msgb->data;
88 * \param msgb Pointer to msgb
91 void fwp_msgb_free(struct fwp_msgb *msgb)
98 * Routine is usually called after putting data of length len to msgb
99 * to adjust internal tail pointer and len fields of msgb
101 * \param msgb Pointer to msgb
102 * \param len The lenght data put to msgb
103 * \return Previous tail pointer
106 inline unsigned char* fwp_msgb_put(struct fwp_msgb *msgb, unsigned int len)
108 unsigned char *tmp= msgb->tail;
116 * Routine is usually called after reading data of lenght len from msgb to
117 * adjust internal data pointer and len of msgb
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.
124 inline unsigned char* fwp_msgb_pull(struct fwp_msgb *msgb, unsigned int len)
130 return msgb->data += len;
134 * Routine is usually called after fwp_msgb_reserve after reserved area is filled
137 * \param msgb Pointer to msgb
138 * \param len The lenght data
139 * \return Current data pointer
142 inline unsigned char* fwp_msgb_push(struct fwp_msgb* msgb, unsigned int len)
149 inline unsigned char* fwp_msgb_shift(struct fwp_msgb *msgb, unsigned int len)
151 if (msgb->data + len > msgb->tail)
154 return msgb->data += len;
158 * Sets data pointer to the start of buffer
160 * \param msgb Pointer to msgb
163 inline void fwp_msgb_reset_data_pointer(struct fwp_msgb *msgb)
165 msgb->data = (unsigned char*) msgb + sizeof(struct fwp_msgb);
169 * Sets data pointer to the start of buffer and the length of used data to zero
171 * \param msgb Pointer to msgb
173 inline void fwp_msgb_reset_data(struct fwp_msgb* msgb)
176 msgb->data = (unsigned char*) msgb + sizeof(struct fwp_msgb);
177 msgb->tail = msgb->data;
181 * Reserve place of length len in msgb
183 * \param msgb Pointer to msgb
184 * \param len The lenght data
186 inline void fwp_msgb_reserve(fwp_msgb_t *msgb, unsigned int len)
192 /*struct fwp_socket* fwp_socket_create(struct sockaddr *_addr, socklen_t _addrlen)
194 struct fwp_socket* fwpsock;
197 fwpsock = (struct fwp_socket*) malloc(sizeof(struct fwp_socket) +
202 addr = (unsigned char*) fwpsock + sizeof(struct fwp_socket);
203 memcpy(addr, (void*) _addr, _addrlen);
205 fwpsock->addr = (struct sockaddr*) addr;
206 fwpsock->addrlen = _addrlen;
211 inline void fwp_socket_set(struct fwp_socket *fwpsock, struct sockaddr *_addr,
214 fwpsock->addr = _addr;
215 fwpsock->addrlen = _addrlen;
218 void fwp_socket_free(struct fwp_socket *fwpsock)
220 free((void*)fwpsock);