]> rtime.felk.cvut.cz Git - frescor/fwp.git/blob - fwp/lib/fwp/fwp_msgb.c
Implemented synchronous and asynchronous sending
[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         
81         return msgb;
82 }
83
84 /**
85  * Deallocates msgb.
86  *
87  * \param msgb Pointer to msgb
88  *
89  */
90 void fwp_msgb_free(struct fwp_msgb *msgb)
91 {
92         free((void*) msgb);
93         msgb = NULL;
94 }
95
96 /**
97  * Routine is usually called after putting data of length len to msgb 
98  * to adjust internal tail pointer and len fields of msgb
99  *
100  * \param msgb Pointer to msgb
101  * \param len The lenght data put to msgb 
102  * \return Previous tail pointer
103  *
104  */
105 unsigned char* fwp_msgb_put(struct fwp_msgb *msgb, unsigned int len)
106 {
107         unsigned char *tmp= msgb->tail;
108         
109         msgb->tail+=len;
110         msgb->len+=len;
111         return tmp;
112 }
113
114 /**
115  * Routine is usually called after reading data of lenght len from msgb to 
116  * adjust internal data pointer and len of msgb
117  *
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.
121  *
122  */
123 unsigned char* fwp_msgb_pull(struct fwp_msgb *msgb, unsigned int len)
124 {
125         if (len > msgb->len)
126                 return NULL;
127         
128         msgb->len -= len;
129         return msgb->data += len;
130 }
131
132 /**
133  * Routine is usually called after fwp_msgb_reserve after reserved area is filled 
134  * adjust pointers 
135  *
136  * \param msgb Pointer to msgb
137  * \param len The lenght data 
138  * \return Current data pointer
139  *
140  */ 
141 unsigned char* fwp_msgb_push(struct fwp_msgb* msgb, unsigned int len)
142 {
143         msgb->data-=len;
144         msgb->len+=len;
145         return msgb->data;
146 }
147
148 unsigned char* fwp_msgb_shift(struct fwp_msgb *msgb, unsigned int len)
149 {
150         if (msgb->data + len > msgb->tail)
151                 return NULL;
152         
153         return msgb->data += len;
154 }
155
156 /**
157  * Sets data pointer to the start of buffer
158  *
159  * \param msgb Pointer to msgb
160  *
161  */
162 void fwp_msgb_reset_data_pointer(struct fwp_msgb *msgb)
163 {
164         msgb->data = (unsigned char*) msgb + sizeof(struct fwp_msgb);
165 }
166
167 /**
168  * Sets data pointer to the start of buffer and the length of used data to zero
169  *
170  * \param msgb Pointer to msgb
171  */
172 void fwp_msgb_reset_data(struct fwp_msgb* msgb)
173 {
174         msgb->len = 0;
175         msgb->data = (unsigned char*) msgb + sizeof(struct fwp_msgb);
176         msgb->tail = msgb->data;
177 }
178
179 /**
180  * Reserve place of length len in msgb
181  *
182  * \param msgb Pointer to msgb
183  * \param len The lenght data 
184  */
185 void fwp_msgb_reserve(fwp_msgb_t *msgb, unsigned int len)
186 {
187         msgb->data+=len;
188         msgb->tail+=len;
189 }
190
191 /*struct fwp_socket* fwp_socket_create(struct sockaddr *_addr, socklen_t _addrlen)
192 {
193         struct fwp_socket* fwpsock;
194         unsigned char *addr;
195         
196         fwpsock = (struct fwp_socket*) malloc(sizeof(struct fwp_socket) + 
197                                               _addrlen);
198         if (!fwpsock)
199                 return NULL;
200         
201         addr = (unsigned char*) fwpsock + sizeof(struct fwp_socket);
202         memcpy(addr, (void*) _addr, _addrlen);
203
204         fwpsock->addr = (struct sockaddr*) addr;
205         fwpsock->addrlen = _addrlen;
206
207         return fwpsock;
208 }
209
210 inline void fwp_socket_set(struct fwp_socket *fwpsock, struct sockaddr *_addr, 
211                                   socklen_t _addrlen)
212 {       
213         fwpsock->addr = _addr;
214         fwpsock->addrlen = _addrlen;
215 }
216
217 void fwp_socket_free(struct fwp_socket *fwpsock)
218 {
219         free((void*)fwpsock);
220         fwpsock = NULL;
221 }*/