summaryrefslogtreecommitdiff
path: root/proto/bmp/buffer.h
blob: f752cf5d5cf30ba4ca2767759e4263443afdd205 (plain)
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
/*
 *	BIRD -- The BGP Monitoring Protocol (BMP)
 *
 *	(c) 2020 Akamai Technologies, Inc. (Pawel Maslanka, pmaslank@akamai.com)
 *
 *	Can be freely distributed and used under the terms of the GNU GPL.
 */

#ifndef _BIRD_BMP_BUFFER_H_
#define _BIRD_BMP_BUFFER_H_

#include "proto/bmp/bmp.h"

#include <stdlib.h>

#include "lib/resource.h"

buffer
bmp_buffer_alloc(pool *ppool, const size_t n);

void
bmp_buffer_free(buffer *buf);

static inline void
bmp_buffer_flush(buffer *buf)
{
  buf->pos = buf->start;
}

static inline size_t
bmp_buffer_size(const buffer *buf)
{
  return buf->end - buf->start;
}

static inline size_t
bmp_buffer_avail(const buffer *buf)
{
  return buf->end - buf->pos;
}

static inline size_t
bmp_buffer_pos(const buffer *buf)
{
  return buf->pos - buf->start;
}

static inline byte *
bmp_buffer_data(const buffer *buf)
{
  return buf->start;
}

void
bmp_buffer_need(buffer *buf, const size_t n);

// Idea for following macros has been taken from |proto/mrt/mrt.c|
#define BMP_DEFINE_PUT_FUNC(S, T)                               \
  static inline void                                            \
  bmp_put_##S(buffer *b, const T x)                             \
  {                                                             \
    bmp_buffer_need(b, sizeof(T));                              \
    put_##S(b->pos, x);                                    \
    b->pos += sizeof(T);                                   \
  }

BMP_DEFINE_PUT_FUNC(u8, u8)
BMP_DEFINE_PUT_FUNC(u16, u16)
BMP_DEFINE_PUT_FUNC(u32, u32)
BMP_DEFINE_PUT_FUNC(u64, u64)
BMP_DEFINE_PUT_FUNC(ip4, ip4_addr)
BMP_DEFINE_PUT_FUNC(ip6, ip6_addr)

void
bmp_put_data(buffer *buf, const void *src, const size_t n);

#endif /* _BIRD_BMP_BUFFER_H_ */