blob: ebeea333c1c96c7e163fe8c112625f4d80d263b7 [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/*
2 * Packet interface
3 * Copyright (C) 1999 Kunihiro Ishiguro
4 *
5 * This file is part of GNU Zebra.
6 *
7 * GNU Zebra is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
10 * later version.
11 *
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with GNU Zebra; see the file COPYING. If not, write to the Free
19 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 * 02111-1307, USA.
21 */
22
23#include <zebra.h>
24
25#include "stream.h"
26#include "memory.h"
27#include "network.h"
28#include "prefix.h"
29
30
31/*A macro to check pointers in order to not
32 go behind the allocated mem block
33 S -- stream reference
34 Z -- size of data to be written
35*/
36
37#define CHECK_SIZE(S, Z) \
paul9985f832005-02-09 15:51:56 +000038 if (((S)->endp + (Z)) > (S)->size) \
39 (Z) = (S)->size - (S)->endp;
paul718e3742002-12-13 20:15:29 +000040
41/* Stream is fixed length buffer for network output/input. */
42
43/* Make stream buffer. */
44struct stream *
45stream_new (size_t size)
46{
47 struct stream *s;
48
paul0e43a2b2004-12-22 00:15:34 +000049 assert (size > 0);
50
51 if (size == 0)
52 return NULL;
53
paul718e3742002-12-13 20:15:29 +000054 s = XCALLOC (MTYPE_STREAM, sizeof (struct stream));
55
56 s->data = XCALLOC (MTYPE_STREAM_DATA, size);
57 s->size = size;
58 return s;
59}
60
61/* Free it now. */
62void
63stream_free (struct stream *s)
64{
65 XFREE (MTYPE_STREAM_DATA, s->data);
66 XFREE (MTYPE_STREAM, s);
67}
68
paulf2e6c422005-02-12 14:35:49 +000069size_t
paul718e3742002-12-13 20:15:29 +000070stream_get_getp (struct stream *s)
71{
72 return s->getp;
73}
74
paulf2e6c422005-02-12 14:35:49 +000075size_t
paul718e3742002-12-13 20:15:29 +000076stream_get_endp (struct stream *s)
77{
78 return s->endp;
79}
80
paulf2e6c422005-02-12 14:35:49 +000081size_t
paul718e3742002-12-13 20:15:29 +000082stream_get_size (struct stream *s)
83{
84 return s->size;
85}
86
87/* Stream structre' stream pointer related functions. */
88void
paulf2e6c422005-02-12 14:35:49 +000089stream_set_getp (struct stream *s, size_t pos)
paul718e3742002-12-13 20:15:29 +000090{
91 s->getp = pos;
92}
93
paul718e3742002-12-13 20:15:29 +000094/* Forward pointer. */
95void
paul9985f832005-02-09 15:51:56 +000096stream_forward_getp (struct stream *s, int size)
paul718e3742002-12-13 20:15:29 +000097{
98 s->getp += size;
99}
paul9985f832005-02-09 15:51:56 +0000100
101void
102stream_forward_endp (struct stream *s, int size)
103{
104 s->endp += size;
105}
paul718e3742002-12-13 20:15:29 +0000106
107/* Copy from stream to destination. */
108void
109stream_get (void *dst, struct stream *s, size_t size)
110{
111 memcpy (dst, s->data + s->getp, size);
112 s->getp += size;
113}
114
115/* Get next character from the stream. */
116u_char
117stream_getc (struct stream *s)
118{
119 u_char c;
120
121 c = s->data[s->getp];
122 s->getp++;
123 return c;
124}
125
126/* Get next character from the stream. */
127u_char
paulf2e6c422005-02-12 14:35:49 +0000128stream_getc_from (struct stream *s, size_t from)
paul718e3742002-12-13 20:15:29 +0000129{
130 u_char c;
131
132 c = s->data[from];
133 return c;
134}
135
136/* Get next word from the stream. */
137u_int16_t
138stream_getw (struct stream *s)
139{
140 u_int16_t w;
141
142 w = s->data[s->getp++] << 8;
143 w |= s->data[s->getp++];
144 return w;
145}
146
147/* Get next word from the stream. */
148u_int16_t
paulf2e6c422005-02-12 14:35:49 +0000149stream_getw_from (struct stream *s, size_t from)
paul718e3742002-12-13 20:15:29 +0000150{
151 u_int16_t w;
152
153 w = s->data[from++] << 8;
154 w |= s->data[from];
155 return w;
156}
157
158/* Get next long word from the stream. */
159u_int32_t
160stream_getl (struct stream *s)
161{
162 u_int32_t l;
163
164 l = s->data[s->getp++] << 24;
165 l |= s->data[s->getp++] << 16;
166 l |= s->data[s->getp++] << 8;
167 l |= s->data[s->getp++];
168 return l;
169}
170
171/* Get next long word from the stream. */
172u_int32_t
173stream_get_ipv4 (struct stream *s)
174{
175 u_int32_t l;
176
177 memcpy (&l, s->data + s->getp, 4);
178 s->getp += 4;
179
180 return l;
181}
182
183/* Copy to source to stream. */
184void
185stream_put (struct stream *s, void *src, size_t size)
186{
187
188 CHECK_SIZE(s, size);
189
190 if (src)
paul9985f832005-02-09 15:51:56 +0000191 memcpy (s->data + s->endp, src, size);
paul718e3742002-12-13 20:15:29 +0000192 else
paul9985f832005-02-09 15:51:56 +0000193 memset (s->data + s->endp, 0, size);
paul718e3742002-12-13 20:15:29 +0000194
paul9985f832005-02-09 15:51:56 +0000195 s->endp += size;
paul718e3742002-12-13 20:15:29 +0000196}
197
198/* Put character to the stream. */
199int
200stream_putc (struct stream *s, u_char c)
201{
paul9985f832005-02-09 15:51:56 +0000202 if (s->endp >= s->size) return 0;
paul718e3742002-12-13 20:15:29 +0000203
paul9985f832005-02-09 15:51:56 +0000204 s->data[s->endp] = c;
205 s->endp++;
206
paul718e3742002-12-13 20:15:29 +0000207 return 1;
208}
209
210/* Put word to the stream. */
211int
212stream_putw (struct stream *s, u_int16_t w)
213{
paul9985f832005-02-09 15:51:56 +0000214 if ((s->size - s->endp) < 2) return 0;
paul718e3742002-12-13 20:15:29 +0000215
paul9985f832005-02-09 15:51:56 +0000216 s->data[s->endp++] = (u_char)(w >> 8);
217 s->data[s->endp++] = (u_char) w;
paul718e3742002-12-13 20:15:29 +0000218
paul718e3742002-12-13 20:15:29 +0000219 return 2;
220}
221
222/* Put long word to the stream. */
223int
224stream_putl (struct stream *s, u_int32_t l)
225{
paul9985f832005-02-09 15:51:56 +0000226 if ((s->size - s->endp) < 4) return 0;
paul718e3742002-12-13 20:15:29 +0000227
paul9985f832005-02-09 15:51:56 +0000228 s->data[s->endp++] = (u_char)(l >> 24);
229 s->data[s->endp++] = (u_char)(l >> 16);
230 s->data[s->endp++] = (u_char)(l >> 8);
231 s->data[s->endp++] = (u_char)l;
paul718e3742002-12-13 20:15:29 +0000232
paul718e3742002-12-13 20:15:29 +0000233 return 4;
234}
235
236int
paulf2e6c422005-02-12 14:35:49 +0000237stream_putc_at (struct stream *s, size_t putp, u_char c)
paul718e3742002-12-13 20:15:29 +0000238{
239 s->data[putp] = c;
240 return 1;
241}
242
243int
paulf2e6c422005-02-12 14:35:49 +0000244stream_putw_at (struct stream *s, size_t putp, u_int16_t w)
paul718e3742002-12-13 20:15:29 +0000245{
246 s->data[putp] = (u_char)(w >> 8);
247 s->data[putp + 1] = (u_char) w;
248 return 2;
249}
250
251int
paulf2e6c422005-02-12 14:35:49 +0000252stream_putl_at (struct stream *s, size_t putp, u_int32_t l)
paul718e3742002-12-13 20:15:29 +0000253{
254 s->data[putp] = (u_char)(l >> 24);
255 s->data[putp + 1] = (u_char)(l >> 16);
256 s->data[putp + 2] = (u_char)(l >> 8);
257 s->data[putp + 3] = (u_char)l;
258 return 4;
259}
260
261/* Put long word to the stream. */
262int
263stream_put_ipv4 (struct stream *s, u_int32_t l)
264{
paul9985f832005-02-09 15:51:56 +0000265 if ((s->size - s->endp) < 4)
paul718e3742002-12-13 20:15:29 +0000266 return 0;
267
paul9985f832005-02-09 15:51:56 +0000268 memcpy (s->data + s->endp, &l, 4);
269 s->endp += 4;
paul718e3742002-12-13 20:15:29 +0000270
paul718e3742002-12-13 20:15:29 +0000271 return 4;
272}
273
274/* Put long word to the stream. */
275int
276stream_put_in_addr (struct stream *s, struct in_addr *addr)
277{
paul9985f832005-02-09 15:51:56 +0000278 if ((s->size - s->endp) < 4)
paul718e3742002-12-13 20:15:29 +0000279 return 0;
280
paul9985f832005-02-09 15:51:56 +0000281 memcpy (s->data + s->endp, addr, 4);
282 s->endp += 4;
paul718e3742002-12-13 20:15:29 +0000283
paul718e3742002-12-13 20:15:29 +0000284 return 4;
285}
286
287/* Put prefix by nlri type format. */
288int
289stream_put_prefix (struct stream *s, struct prefix *p)
290{
291 u_char psize;
292
293 psize = PSIZE (p->prefixlen);
294
paul9985f832005-02-09 15:51:56 +0000295 if ((s->size - s->endp) < psize) return 0;
paul718e3742002-12-13 20:15:29 +0000296
297 stream_putc (s, p->prefixlen);
paul9985f832005-02-09 15:51:56 +0000298 memcpy (s->data + s->endp, &p->u.prefix, psize);
299 s->endp += psize;
paul718e3742002-12-13 20:15:29 +0000300
paul718e3742002-12-13 20:15:29 +0000301 return psize;
302}
303
304/* Read size from fd. */
305int
306stream_read (struct stream *s, int fd, size_t size)
307{
308 int nbytes;
309
paul9985f832005-02-09 15:51:56 +0000310 nbytes = readn (fd, s->data + s->endp, size);
paul718e3742002-12-13 20:15:29 +0000311
312 if (nbytes > 0)
paul9985f832005-02-09 15:51:56 +0000313 s->endp += nbytes;
314
paul718e3742002-12-13 20:15:29 +0000315 return nbytes;
316}
317
318/* Read size from fd. */
319int
320stream_read_unblock (struct stream *s, int fd, size_t size)
321{
322 int nbytes;
323 int val;
324
325 val = fcntl (fd, F_GETFL, 0);
326 fcntl (fd, F_SETFL, val|O_NONBLOCK);
paul9985f832005-02-09 15:51:56 +0000327 nbytes = read (fd, s->data + s->endp, size);
paul718e3742002-12-13 20:15:29 +0000328 fcntl (fd, F_SETFL, val);
329
330 if (nbytes > 0)
paul9985f832005-02-09 15:51:56 +0000331 s->endp += nbytes;
332
paul718e3742002-12-13 20:15:29 +0000333 return nbytes;
334}
335
336/* Write data to buffer. */
337int
338stream_write (struct stream *s, u_char *ptr, size_t size)
339{
340
341 CHECK_SIZE(s, size);
342
paul9985f832005-02-09 15:51:56 +0000343 memcpy (s->data + s->endp, ptr, size);
344 s->endp += size;
345
paul718e3742002-12-13 20:15:29 +0000346 return size;
347}
348
349/* Return current read pointer. */
350u_char *
351stream_pnt (struct stream *s)
352{
353 return s->data + s->getp;
354}
355
356/* Check does this stream empty? */
357int
358stream_empty (struct stream *s)
359{
paul9985f832005-02-09 15:51:56 +0000360 if (s->endp == 0 && s->getp == 0)
paul718e3742002-12-13 20:15:29 +0000361 return 1;
362 else
363 return 0;
364}
365
366/* Reset stream. */
367void
368stream_reset (struct stream *s)
369{
paul718e3742002-12-13 20:15:29 +0000370 s->endp = 0;
371 s->getp = 0;
372}
373
374/* Write stream contens to the file discriptor. */
375int
376stream_flush (struct stream *s, int fd)
377{
378 int nbytes;
379
380 nbytes = write (fd, s->data + s->getp, s->endp - s->getp);
381
382 return nbytes;
383}
384
385/* Stream first in first out queue. */
386
387struct stream_fifo *
388stream_fifo_new ()
389{
390 struct stream_fifo *new;
391
392 new = XCALLOC (MTYPE_STREAM_FIFO, sizeof (struct stream_fifo));
393 return new;
394}
395
396/* Add new stream to fifo. */
397void
398stream_fifo_push (struct stream_fifo *fifo, struct stream *s)
399{
400 if (fifo->tail)
401 fifo->tail->next = s;
402 else
403 fifo->head = s;
404
405 fifo->tail = s;
406
407 fifo->count++;
408}
409
410/* Delete first stream from fifo. */
411struct stream *
412stream_fifo_pop (struct stream_fifo *fifo)
413{
414 struct stream *s;
415
416 s = fifo->head;
417
418 if (s)
419 {
420 fifo->head = s->next;
421
422 if (fifo->head == NULL)
423 fifo->tail = NULL;
424 }
425
426 fifo->count--;
427
428 return s;
429}
430
431/* Return first fifo entry. */
432struct stream *
433stream_fifo_head (struct stream_fifo *fifo)
434{
435 return fifo->head;
436}
437
438void
439stream_fifo_clean (struct stream_fifo *fifo)
440{
441 struct stream *s;
442 struct stream *next;
443
444 for (s = fifo->head; s; s = next)
445 {
446 next = s->next;
447 stream_free (s);
448 }
449 fifo->head = fifo->tail = NULL;
450 fifo->count = 0;
451}
452
453void
454stream_fifo_free (struct stream_fifo *fifo)
455{
456 stream_fifo_clean (fifo);
457 XFREE (MTYPE_STREAM_FIFO, fifo);
458}