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;
87 * \param msgb Pointer to msgb
90 void fwp_msgb_free(struct fwp_msgb *msgb)
97 * Routine is usually called after putting data of length len to msgb
98 * to adjust internal tail pointer and len fields of msgb
100 * \param msgb Pointer to msgb
101 * \param len The lenght data put to msgb
102 * \return Previous tail pointer
105 unsigned char* fwp_msgb_put(struct fwp_msgb *msgb, unsigned int len)
107 unsigned char *tmp= msgb->tail;
115 * Routine is usually called after reading data of lenght len from msgb to
116 * adjust internal data pointer and len of msgb
118 * \param msgb Pointer to msgb
119 * \param len The lenght data
120 * \return Previous tail pointer on NULL in case of lack of space in the buffer.
123 unsigned char* fwp_msgb_pull(struct fwp_msgb *msgb, unsigned int len)
129 return msgb->data += len;
133 * Routine is usually called after fwp_msgb_reserve after reserved area is filled
136 * \param msgb Pointer to msgb
137 * \param len The lenght data
138 * \return Current data pointer
141 unsigned char* fwp_msgb_push(struct fwp_msgb* msgb, unsigned int len)
148 unsigned char* fwp_msgb_shift(struct fwp_msgb *msgb, unsigned int len)
150 if (msgb->data + len > msgb->tail)
153 return msgb->data += len;
157 * Sets data pointer to the start of buffer
159 * \param msgb Pointer to msgb
162 void fwp_msgb_reset_data_pointer(struct fwp_msgb *msgb)
164 msgb->data = (unsigned char*) msgb + sizeof(struct fwp_msgb);
168 * Sets data pointer to the start of buffer and the length of used data to zero
170 * \param msgb Pointer to msgb
172 void fwp_msgb_reset_data(struct fwp_msgb* msgb)
175 msgb->data = (unsigned char*) msgb + sizeof(struct fwp_msgb);
176 msgb->tail = msgb->data;
180 * Reserve place of length len in msgb
182 * \param msgb Pointer to msgb
183 * \param len The lenght data
185 void fwp_msgb_reserve(fwp_msgb_t *msgb, unsigned int len)
191 /*struct fwp_socket* fwp_socket_create(struct sockaddr *_addr, socklen_t _addrlen)
193 struct fwp_socket* fwpsock;
196 fwpsock = (struct fwp_socket*) malloc(sizeof(struct fwp_socket) +
201 addr = (unsigned char*) fwpsock + sizeof(struct fwp_socket);
202 memcpy(addr, (void*) _addr, _addrlen);
204 fwpsock->addr = (struct sockaddr*) addr;
205 fwpsock->addrlen = _addrlen;
210 inline void fwp_socket_set(struct fwp_socket *fwpsock, struct sockaddr *_addr,
213 fwpsock->addr = _addr;
214 fwpsock->addrlen = _addrlen;
217 void fwp_socket_free(struct fwp_socket *fwpsock)
219 free((void*)fwpsock);