]> rtime.felk.cvut.cz Git - frescor/fwp.git/blob - fwp/lib/fwp/fwp_msgb.c
Rename lib/core to lib/fwp. Clean-ups
[frescor/fwp.git] / fwp / lib / fwp / fwp_msgb.c
1 /**
2  * \file fwp_msgb.c
3  *
4  * Routines for manipulation with fwp message buffer (msgb)
5  *
6  */
7
8 #include "fwp_msgb.h"
9 #include "fwp_utils.h"
10 #include <stdlib.h>
11
12 /*unsigned char* fwp_msgb_dealloc(size_t buf_size) */
13 /*unsigned char* fwp_msgb_pool_init(size_t buf_size) */
14
15 /**
16  * Allocates msgb.
17  *
18  * \param buf_size Size of msgb
19  * \return Allocated msgb
20  *
21  */
22 struct fwp_msgb* fwp_msgb_alloc(size_t buf_size)
23 {
24         struct fwp_msgb* msgb;
25
26         /*fwp_msgb_pool_dequeue; other choice*/
27         msgb = (struct fwp_msgb*) malloc(sizeof(struct fwp_msgb) + buf_size);
28         if (!msgb)
29                 return NULL;
30         
31         msgb->buffer_size = buf_size;
32         msgb->len = 0;
33         msgb->data = (unsigned char*) msgb + sizeof(struct fwp_msgb);
34         msgb->tail = msgb->data;
35         msgb->peer = NULL;
36         
37         return msgb;
38 }
39
40 /**
41  * Deallocates msgb.
42  *
43  * \param msgb Pointer to msgb
44  *
45  */
46 void fwp_msgb_free(struct fwp_msgb *msgb)
47 {
48         free((void*) msgb);
49         msgb = NULL;
50 }
51
52 /**
53  * Routine is usually called after putting data of length len to msgb 
54  * to adjust internal tail pointer and len fields of msgb
55  *
56  * \param msgb Pointer to msgb
57  * \param len The lenght data put to msgb 
58  * \return Previous tail pointer
59  *
60  */
61 inline unsigned char* fwp_msgb_put(struct fwp_msgb *msgb, unsigned int len)
62 {
63         unsigned char *tmp= msgb->tail;
64         
65         msgb->tail+=len;
66         msgb->len+=len;
67         return tmp;
68 }
69
70 /**
71  * Routine is usually called after reading data of lenght len from msgb to 
72  * adjust internal data pointer and len of msgb
73  *
74  * \param msgb Pointer to msgb
75  * \param len The lenght data 
76  * \return Previous tail pointer on NULL in case of lack of space in the buffer.
77  *
78  */
79 inline unsigned char* fwp_msgb_pull(struct fwp_msgb *msgb, unsigned int len)
80 {
81         if (len > msgb->len)
82                 return NULL;
83         
84         msgb->len -= len;
85         return msgb->data += len;
86 }
87
88 /**
89  * Routine is usually called after fwp_msgb_reserve after reserved area is filled 
90  * adjust pointers 
91  *
92  * \param msgb Pointer to msgb
93  * \param len The lenght data 
94  * \return Current data pointer
95  *
96  */ 
97 inline unsigned char* fwp_msgb_push(struct fwp_msgb* msgb, unsigned int len)
98 {
99         msgb->data-=len;
100         msgb->len+=len;
101         return msgb->data;
102 }
103
104 inline unsigned char* fwp_msgb_shift(struct fwp_msgb *msgb, unsigned int len)
105 {
106         if (msgb->data + len > msgb->tail)
107                 return NULL;
108         
109         return msgb->data += len;
110 }
111
112 /**
113  * Sets data pointer to the start of buffer
114  *
115  * \param msgb Pointer to msgb
116  *
117  */
118 inline void fwp_msgb_reset_data_pointer(struct fwp_msgb *msgb)
119 {
120         msgb->data = (unsigned char*) msgb + sizeof(struct fwp_msgb);
121 }
122
123 /**
124  * Sets data pointer to the start of buffer and the length of used data to zero
125  *
126  * \param msgb Pointer to msgb
127  */
128 inline void fwp_msgb_reset_data(struct fwp_msgb* msgb)
129 {
130         msgb->len = 0;
131         msgb->data = (unsigned char*) msgb + sizeof(struct fwp_msgb);
132         msgb->tail = msgb->data;
133 }
134
135 /**
136  * Reserve place of length len in msgb
137  *
138  * \param msgb Pointer to msgb
139  * \param len The lenght data 
140  */
141 inline void fwp_msgb_reserve(fwp_msgb_t *msgb, unsigned int len)
142 {
143         msgb->data+=len;
144         msgb->tail+=len;
145 }
146
147 /*struct fwp_socket* fwp_socket_create(struct sockaddr *_addr, socklen_t _addrlen)
148 {
149         struct fwp_socket* fwpsock;
150         unsigned char *addr;
151         
152         fwpsock = (struct fwp_socket*) malloc(sizeof(struct fwp_socket) + 
153                                               _addrlen);
154         if (!fwpsock)
155                 return NULL;
156         
157         addr = (unsigned char*) fwpsock + sizeof(struct fwp_socket);
158         memcpy(addr, (void*) _addr, _addrlen);
159
160         fwpsock->addr = (struct sockaddr*) addr;
161         fwpsock->addrlen = _addrlen;
162
163         return fwpsock;
164 }
165
166 inline void fwp_socket_set(struct fwp_socket *fwpsock, struct sockaddr *_addr, 
167                                   socklen_t _addrlen)
168 {       
169         fwpsock->addr = _addr;
170         fwpsock->addrlen = _addrlen;
171 }
172
173 void fwp_socket_free(struct fwp_socket *fwpsock)
174 {
175         free((void*)fwpsock);
176         fwpsock = NULL;
177 }*/