1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
|
/*
* Packet interface
* Copyright (C) 1999 Kunihiro Ishiguro
*
* This file is part of GNU Zebra.
*
* GNU Zebra is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2, or (at your option) any
* later version.
*
* GNU Zebra is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GNU Zebra; see the file COPYING. If not, write to the Free
* Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
* 02111-1307, USA.
*/
#ifndef _ZEBRA_STREAM_H
#define _ZEBRA_STREAM_H
#include "misc.h"
#include "prefix.h"
/*==============================================================================
* A stream is an arbitrary buffer, whose contents generally are assumed to
* be in network order.
*
* The principle function of a stream is to support the reading and writing of
* PDU. A PDU may be read into a stream buffer, and then that may be read as
* some sequence of values (byte, word, long word, etc.). A PDU may be
* constructed in a stream by writing a sequence of values (byte, word, long
* word, etc.).
*
* A stream has a fixed size -- it does not automatically adjust itself as
* stuff is read/written.
*
* Attempting to read beyond the current end of a stream will return zeros and
* set the "overrun" flag. So readers may plough on without having to
* constantly check for running off the end of the stream, and check for
* overrun at convenient moments.
*
* Similarly, attempting to write beyond the end of the stream buffer will
* discard the excess, and set the "overflow" flag. So writers can plough on
* and check for overflow at some convenient moment.
*
* A stream has the following attributes associated with it:
*
* - size: the allocated, invariant size of the buffer.
*
* - getp: the get position marker, denoting the offset in the stream where
* the next read (or 'get') will be from.
*
* - endp: the end position marker, denoting the offset in the stream where
* valid data ends, and where any data would be written (or 'put') to.
*
* These attributes are all size_t values.
*
* Constraints:
*
* 1. endp can never exceed size
*
* hence, if endp == size, then the stream is full, and no more data can be
* written to the stream.
*
* Whenever data is written to the stream the endp is clamped to size, and
* in the event of overflow the overflow flag is set.
*
* Whenever the endp is set, it is clamped.
*
* 2. getp can never exceed endp (which can never exceed size)
*
* hence if getp == endp, there is no more valid data that can read from the
* stream -- though, the user may reposition getp to earlier in the stream,
* if they wish.
*
* Whenever data is written to the stream the endp is clamped to size, and
* in the event of overflow the overflow flag is set.
*
* So, the following will be true (unless something has gone wrong):
*
* getp <= endp <= size
*
* but the stream may overflow or overrun, which the caller may wish to check.
*
* A stream therefore can be thought of like this:
*
* ---------------------------------------------------
* |XXXXXXXXXXXXXXXXXXXXXXXX |
* ---------------------------------------------------
* ^ ^ ^
* getp endp size
*
* This shows a stream containing data (shown as 'X') up to the endp offset.
* The stream is empty from endp to size. Without adjusting getp, there are
* still endp-getp bytes of valid data to be read from the stream.
*
* Methods are provided to get and put to/from the stream, as well as
* retrieve the values of the 3 markers and manipulate the getp marker.
*/
/* Stream buffer. */
struct stream
{
struct stream *next;
/* Remainder is ***private*** to stream
* direct access is frowned upon!
* Use the appropriate functions/macros
*/
size_t getp; /* next get position */
size_t endp; /* last valid data position */
size_t size; /* size of data segment */
size_t startp ; /* not used by stream itself */
bool overflow ; /* set if attempts to put beyond size */
bool overrun ; /* set if attempts to get beyond endp */
byte* data ; /* data pointer */
};
/* First in first out queue structure. */
struct stream_fifo
{
size_t count;
struct stream *head;
struct stream *tail;
};
/*==============================================================================
* Utility macros -- deprecated -- do not use in new code
*/
#define STREAM_PNT(S) stream_get_pnt(S)
#define STREAM_DATA(S) stream_get_data(S)
#define STREAM_REMAIN(S) stream_get_write_left(S)
/*==============================================================================
* Stream prototypes.
* For stream_{put,get}S, the S suffix mean:
*
* c: character (unsigned byte)
* w: word (two bytes)
* l: long (two words)
* q: quad (four words)
*/
extern struct stream *stream_new (size_t);
extern void stream_free (struct stream *);
extern struct stream * stream_copy (struct stream *, struct stream *src);
extern struct stream *stream_dup (struct stream *);
extern size_t stream_resize (struct stream *, size_t);
Inline void stream_reset (struct stream *s) ;
Inline bool stream_is_empty (struct stream *s) ;
Inline size_t stream_get_getp(struct stream* s);
Inline size_t stream_get_endp(struct stream* s);
Inline size_t stream_get_len(struct stream* s) ;
Inline size_t stream_get_size(struct stream* s) ;
Inline size_t stream_get_startp(struct stream* s) ;
Inline byte* stream_get_data(struct stream* s) ;
Inline byte* stream_get_pnt(struct stream* s) ;
Inline byte* stream_get_pnt_to (struct stream *s, size_t pos) ;
Inline size_t stream_get_read_left(struct stream* s) ;
Inline size_t stream_get_read_left_from(struct stream* s, size_t from) ;
Inline bool stream_has_read_left(struct stream* s, size_t len) ;
Inline size_t stream_get_write_left(struct stream* s) ;
Inline size_t stream_get_write_left_at(struct stream* s, size_t at) ;
Inline bool stream_has_write_left(struct stream* s, size_t len) ;
Inline bool stream_has_overrun(struct stream* s) ;
Inline bool stream_has_overflowed(struct stream* s) ;
Inline void stream_clear_overrun(struct stream* s) ;
Inline void stream_clear_overflow(struct stream* s) ;
Inline bool stream_has_written_beyond(struct stream* s, size_t limit) ;
Inline void stream_set_getp(struct stream *, size_t);
Inline void stream_set_endp(struct stream *, size_t);
Inline void stream_set_startp(struct stream* s, size_t) ;
Inline void stream_reset_getp(struct stream* s) ;
Inline void stream_forward_getp(struct stream *, size_t);
Inline void stream_forward_endp(struct stream *, size_t);
Inline size_t stream_push_endp(struct stream* s, size_t len) ;
Inline bool stream_pop_endp(struct stream* s, size_t old_endp) ;
extern void stream_put (struct stream *, const void *, size_t);
extern void stream_putc (struct stream *, byte);
extern void stream_putc_at (struct stream *, size_t, byte);
extern void stream_putw (struct stream *, uint16_t);
extern void stream_putw_at (struct stream *, size_t, uint16_t);
extern void stream_putl (struct stream *, uint32_t);
extern void stream_putl_at (struct stream *, size_t, uint32_t);
extern void stream_putq (struct stream *, uint64_t);
extern void stream_putq_at (struct stream *, size_t, uint64_t);
extern void stream_put_ipv4 (struct stream *, in_addr_t);
extern void stream_put_in_addr (struct stream *, struct in_addr *);
extern void stream_put_prefix (struct stream *, struct prefix *);
extern void stream_get (void *, struct stream *, size_t) ;
extern void* stream_get_bytes(struct stream *s, size_t want, size_t* have) ;
extern void* stream_get_bytes_left(struct stream *s, size_t* have) ;
extern byte stream_getc (struct stream *);
extern byte stream_getc_from (struct stream *, size_t);
extern uint16_t stream_getw (struct stream *);
extern uint16_t stream_getw_from (struct stream *, size_t);
extern uint32_t stream_getl (struct stream *);
extern uint32_t stream_getl_from (struct stream *, size_t);
extern uint64_t stream_getq (struct stream *);
extern uint64_t stream_getq_from (struct stream *, size_t);
extern in_addr_t stream_get_ipv4 (struct stream *);
extern void stream_get_prefix(struct stream *s,
struct prefix* p, sa_family_t family) ;
extern uint stream_get_prefix_from(struct stream *s, size_t from,
struct prefix* p, sa_family_t family) ;
/* Deprecated: assumes blocking I/O. Will be removed.
Use stream_read_try instead. */
extern int stream_read (struct stream *, int, size_t);
extern int stream_readn (struct stream* s, int fd, size_t size) ;
extern ssize_t stream_read_try(struct stream *s, int fd, size_t size);
extern ssize_t stream_recvmsg (struct stream *s, int fd, struct msghdr *,
int flags, size_t size);
extern ssize_t stream_recvfrom (struct stream *s, int fd, size_t len,
int flags, struct sockaddr *from,
socklen_t *fromlen);
extern int stream_flush_try(struct stream* s, int fd) ;
extern void* stream_transfer(void* p, struct stream* s, void* limit) ;
/* Stream fifo. */
extern struct stream_fifo *stream_fifo_new (void);
extern void stream_fifo_push (struct stream_fifo *fifo, struct stream *s);
extern struct stream *stream_fifo_pop (struct stream_fifo *fifo);
extern struct stream *stream_fifo_head (struct stream_fifo *fifo);
extern void stream_fifo_reset (struct stream_fifo *fifo);
extern void stream_fifo_clean (struct stream_fifo *fifo);
extern void stream_fifo_free (struct stream_fifo *fifo);
/*==============================================================================
* The Inlines
*/
Private void stream_set_overs(struct stream* s) ;
/*------------------------------------------------------------------------------
* qassert that the s->getp, s->endp and s->size are consistent.
*/
Inline void
qassert_stream(struct stream* s)
{
qassert((s->getp <= s->endp) && (s->endp <= s->size)) ;
} ;
/*------------------------------------------------------------------------------
* Is there anything in this stream ?
*/
Inline bool
stream_is_empty (struct stream *s)
{
qassert_stream(s) ;
return (s->endp == 0);
}
/*------------------------------------------------------------------------------
* Reset to empty and not overflow or overrun.
*
* NB: contents of body are untouched.
*/
Inline void
stream_reset (struct stream *s)
{
s->getp = s->endp = s->startp = 0 ;
s->overflow = s->overrun = false ;
}
/*------------------------------------------------------------------------------
* The current s->getp.
*/
Inline size_t
stream_get_getp(struct stream* s)
{
qassert_stream(s) ;
return s->getp ;
} ;
/*------------------------------------------------------------------------------
* The current s->endp.
*
* This is also the current total length of the stream data.
*
* This generally used for recording the position of some field which will be
* updated later by stream_putX_at() -- typically a length field.
*/
Inline size_t
stream_get_endp(struct stream* s)
{
qassert_stream(s) ;
return s->endp ;
} ;
/*------------------------------------------------------------------------------
* The current total length of the stream data -- from start to s->endp
*
* This is the same as stream_get_endp() -- but clearer in some contexts.
*
* NB: if the stream has overflowed, the length is same as the size of the
* stream -- it is NOT the length that the stream would have been had there
* been enough room.
*/
Inline size_t
stream_get_len(struct stream *s)
{
qassert_stream(s) ;
return s->endp ;
} ;
/*------------------------------------------------------------------------------
* The current size of the stream data body
*
* May be zero ! (In which case stream_get_data() will return NULL.)
*/
Inline size_t
stream_get_size(struct stream* s)
{
qassert_stream(s) ;
return s->size ;
} ;
/*------------------------------------------------------------------------------
* The current s->startp
*
* s->startp is set to zero when a stream is created or reset.
*
* Otherwise the s->startp is of no interest to the stream code itself, but may
* be used for whatever purpose by users of the stream.
*/
Inline size_t
stream_get_startp(struct stream* s)
{
qassert_stream(s) ;
return s->startp ;
} ;
/*------------------------------------------------------------------------------
* The current stream data body
*
* May be NULL -- if size is zero.
*
* NB: if the stream size is changed, the address returned here may become out
* of date.
*
* NB: for ordinary processing of the contents of a stream, the various
* get/put functions are *recommended* !
*/
Inline byte*
stream_get_data(struct stream* s)
{
qassert_stream(s) ;
return s->data ;
} ;
/*------------------------------------------------------------------------------
* Return pointer to byte at current s->getp.
*
* NB: if the stream size is changed, the address returned here may become out
* of date.
*
* NB: for ordinary processing of the contents of a stream, the various
* get/put functions are *recommended* !
*/
Inline byte*
stream_get_pnt (struct stream *s)
{
qassert_stream(s) ;
return s->data + s->getp;
}
/*------------------------------------------------------------------------------
* Return pointer to byte at given position
*
* If the given position is > s->endp, then returns position of s->endp.
*
* stream_get_read_left_from() will get the number of bytes available at the
* given position.
*
* NB: if the stream size is changed, the address returned here may become out
* of date.
*
* NB: for ordinary processing of the contents of a stream, the various
* get/put functions are *recommended* !
*/
Inline byte*
stream_get_pnt_to (struct stream *s, size_t pos)
{
qassert_stream(s) ;
return s->data + ((pos <= s->endp) ? pos : s->endp) ;
}
/*------------------------------------------------------------------------------
* Count of bytes between s->getp and s->endp.
*/
Inline size_t
stream_get_read_left(struct stream* s)
{
qassert_stream(s) ;
return (s->getp < s->endp) ? s->endp - s->getp : 0 ;
} ;
/*------------------------------------------------------------------------------
* Count of bytes between given position and s->endp.
*
* If the given position is > s->endp, returns 0.
*/
Inline size_t
stream_get_read_left_from(struct stream* s, size_t from)
{
qassert_stream(s) ;
return (from < s->endp) ? s->endp - from : 0 ;
} ;
/*------------------------------------------------------------------------------
* See if has at least len bytes between s->getp and s->endp
*/
Inline bool
stream_has_read_left(struct stream* s, size_t len)
{
return len <= stream_get_read_left(s) ;
} ;
/*------------------------------------------------------------------------------
* Count of bytes between s->endp and s->size.
*/
Inline size_t
stream_get_write_left(struct stream* s)
{
qassert_stream(s) ;
return (s->endp < s->size) ? s->size - s->endp : 0 ;
}
/*------------------------------------------------------------------------------
* Count of bytes between given position and s->size.
*
* If the given position is > s->size, returns 0.
*/
Inline size_t
stream_get_write_left_at(struct stream* s, size_t at)
{
qassert_stream(s) ;
return (at < s->size) ? s->size - at : 0 ;
}
/*------------------------------------------------------------------------------
* See if has at least len bytes between s->endp and s->size
*/
Inline bool
stream_has_write_left(struct stream* s, size_t len)
{
return len <= stream_get_write_left(s) ;
} ;
/*------------------------------------------------------------------------------
* Return the overrun flag
*/
Inline bool
stream_has_overrun(struct stream* s)
{
qassert_stream(s) ;
return s->overrun ;
} ;
/*------------------------------------------------------------------------------
* Return the overflow flag
*/
Inline bool
stream_has_overflowed(struct stream* s)
{
qassert_stream(s) ;
return s->overflow ;
} ;
/*------------------------------------------------------------------------------
* Clear the overrun flag
*/
Inline void
stream_clear_overrun(struct stream* s)
{
s->overrun = false ;
}
/*------------------------------------------------------------------------------
* Clear the overflow flag
*/
Inline void
stream_clear_overflow(struct stream* s)
{
s->overflow = false ;
}
/*------------------------------------------------------------------------------
* Test if the endp is beyond the given limit
*
* A stream may be set up to be longer than some actual limit, so that the
* extent of overflowing beyond that limit can be measured. This test can
* then be used to see if the endp is *currently* within the given limit.
*
* Returns: true <=> endp is *beyond* the given limit.
*
* NB: the limit MUST be less than the size -- for if not, this is *always*
* going to return false, because the s->endp is *always* <= s->size !
*/
Inline bool
stream_has_written_beyond(struct stream* s, size_t limit)
{
qassert_stream(s) ;
qassert(limit < s->size) ;
return (s->endp > limit) ;
} ;
/*------------------------------------------------------------------------------
* Set s->getp to given value.
*
* If value > s->endp will force to s->endp and set s->overrun.
*/
Inline void
stream_set_getp (struct stream *s, size_t pos)
{
qassert_stream(s) ;
s->getp = pos ;
if (s->getp > s->endp)
stream_set_overs(s) ;
} ;
/*------------------------------------------------------------------------------
* Set s->endp to given value.
*
* If value > s->size will force to s->size and set s->overflow
*
* If value < s->getp will force s->getp to new value and set s->overrun.
*
* NB: moving the s->endp around is unusual
*
* Returning to a position previously returned by stream_get_endp() is
* plausible. Otherwise, the user must beware ! In particular, moving
* s->endp forwards adds whatever is currently in the stream body
* beyond s->endp to the stream.
*
* But see stream_push_endp()/stream_pop_endp().
*/
Inline void
stream_set_endp (struct stream *s, size_t pos)
{
qassert_stream(s) ;
s->endp = pos;
if ((s->endp > s->size) || (s->endp < s->getp))
stream_set_overs(s) ;
} ;
/*------------------------------------------------------------------------------
* Set the s->startp to the given value.
*
* The s->startp is of no interest to the stream code itself, but may
* be used for whatever purpose by users of the stream.
*/
Inline void
stream_set_startp(struct stream* s, size_t pos)
{
qassert_stream(s) ;
s->startp = pos ;
} ;
/*------------------------------------------------------------------------------
* Reset s->getp to the current s->startp
*
* s->startp is set to zero when a stream is created or reset.
*
* Otherwise the s->startp is of no interest to the stream code itself, but may
* be used for whatever purpose by users of the stream.
*
* If s->getp is now > s->endp will force to s->endp and set s->overrun.
*/
Inline void
stream_reset_getp (struct stream *s)
{
qassert_stream(s) ;
s->getp = s->startp ;
if (s->getp > s->endp)
stream_set_overs(s) ;
} ;
/*------------------------------------------------------------------------------
* Move s->getp forwards by given step.
*
* If result > s->endp will force to s->endp and set s->overrun.
*/
Inline void
stream_forward_getp (struct stream *s, size_t step)
{
qassert_stream(s) ;
s->getp += step ;
if (s->getp > s->endp)
stream_set_overs(s) ;
} ;
/*------------------------------------------------------------------------------
* Move s->endp forwards by given step.
*
* If value > s->size will force to s->size and set s->overflow
*
* NB: moving the s->endp around is unusual -- see stream_set_endp().
*/
Inline void
stream_forward_endp (struct stream *s, size_t step)
{
qassert_stream(s) ;
s->endp += step;
if ((s->endp > s->size) || (s->endp < s->getp))
stream_set_overs(s) ;
} ;
/*------------------------------------------------------------------------------
* Return current s->endp and set a new s->end *wrt* current s->getp (push)
*
* This may be used when reader knows that is about to read some unit of data
* which is expected to be len bytes long.
*
* If there are fewer than len bytes between s->getp and s->endp, leaves
* s->endp as it is and sets overrun.
*
* Caller can check overrun immediately or leave for later -- proceeding to
* read will hit the current s->endp and set overrun again.
*
* In any case, restoring (pop) the saved value will work fine (even if failed).
*/
Inline size_t
stream_push_endp(struct stream* s, size_t len)
{
size_t new_endp, old_endp ;
qassert_stream(s) ;
old_endp = s->endp ;
new_endp = s->getp + len ;
if (new_endp <= old_endp)
s->endp = new_endp ;
else
s->overrun = true ;
return old_endp ;
} ;
/*------------------------------------------------------------------------------
* Pop saved value for s->endp -- restore to as before stream_push_endp()
*
* This deemed to be OK if s->getp == s->endp -- ie the s->getp has reached the
* end of the unit of data was about to process when did stream_push_endp(),
* AND has not overrun.
*
* If not OK will force s->getp to the current s->endp.
*
* NB: if have not read everything, returns false without setting s->overrun.
* So, provided s->overrun was not set at the time of the push, can
* distinguish underrun from overrun.
*
* Expects the value being restored to be valid -- but checks for overflow and
* overrun just in case !
*
* Returns: (getp == old endp) && not overrun
*/
Inline bool
stream_pop_endp(struct stream* s, size_t old_endp)
{
bool ok ;
qassert_stream(s) ;
qassert((s->endp <= old_endp) && (old_endp <= s->size)) ;
ok = (s->getp == s->endp) ;
if (!ok)
s->getp = s->endp ;
s->endp = old_endp ;
if ((s->endp > s->size) || (s->getp > s->endp)) /* impossible ! */
stream_set_overs(s) ;
return ok && !s->overrun ;
} ;
#endif /* _ZEBRA_STREAM_H */
|