summaryrefslogtreecommitdiff
path: root/proto/bmp/utils.h
blob: faca2ba0b5b0f0885f12cdf76be08a7969d18b49 (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
 *	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_UTILS_H_
#define _BIRD_BMP_UTILS_H_

/**
 * TODO:
 * - Provide descriptive explanation for the follwing enums
 */
enum bmp_result {
  BMP_E_NONE = 0,
  BMP_E_INVALID_ARG,
  BMP_E_NULL_REF,
  BMP_E_EXISTS,
  BMP_E_NOT_EXISTS,
  BMP_E_OPEN_SOCKET,
  BMP_E_CONNECT_TO_SRV,
  BMP_E_SEND_DATA,
  BMP_E_BIRDSOCK_NULL_REF,
  BMP_E_REMOTE_CAPS_NULL_REF,
  BMP_E_NEW_TX_EVENT
};

#define BMP_FAILED(v) \
  (v != BMP_E_NONE)

#define IF_BMP_FAILED_RETURN_RC(func) \
  do {                                \
    enum bmp_result rc = func;        \
    if (BMP_FAILED(rc))               \
    {                                 \
      return rc;                      \
    }                                 \
  } while (0)

// The following macro requires to define locally enum bmp_result rc;
#define IF_BMP_FAILED_GOTO_LABEL(func, label)     \
  do {                                            \
    rc = func;                                    \
    if (BMP_FAILED(rc))                           \
    {                                             \
      goto label;                                 \
    }                                             \
  } while (0)

#define IF_BMP_FAILED_PRINT_ERR_MSG(func, msg)   \
  do {                                           \
    enum bmp_result rc = func;                   \
    if (BMP_FAILED(rc))                          \
    {                                            \
      log(L_WARN "[BMP] " msg " (rc = %d)", rc); \
    }                                            \
  } while (0)

#define IF_BMP_FAILED_PRINT_ERR_MSG_AND_GOTO_LABEL(func, msg, label) \
  do {                                                               \
    enum bmp_result rc = func;                                       \
    if (BMP_FAILED(rc))                                              \
    {                                                                \
      log(L_WARN "[BMP] " msg " (rc = %d)", rc);                     \
      goto label;                                                    \
    }                                                                \
  } while (0)

#define IS_NULL(ptr) \
  ((ptr) == NULL)

#define IF_COND_TRUE_PRINT_ERR_MSG_AND_RETURN_OPT_VAL(expr, msg, rv...)     \
  do {                                                                      \
    if ((expr))                                                             \
    {                                                                       \
      log(L_WARN "[BMP] " msg);                                             \
      return rv;                                                            \
    }                                                                       \
  } while (0)

#define IF_PTR_IS_NULL_PRINT_ERR_MSG_AND_RETURN_OPT_VAL(p, msg, rv...)   \
  do {                                                                   \
    IF_COND_TRUE_PRINT_ERR_MSG_AND_RETURN_OPT_VAL(IS_NULL(p), msg, rv);  \
  } while (0)

#define IF_BMP_FAILED_PRINT_ERR_MSG_AND_RETURN_OPT_VAL(func, msg, rv...)     \
  do {                                                                       \
    enum bmp_result rc = func;                                               \
    IF_COND_TRUE_PRINT_ERR_MSG_AND_RETURN_OPT_VAL(BMP_FAILED(rc), msg, rv);  \
  } while (0)

#endif /* _BIRD_BMP_UTILS_H_ */