summaryrefslogtreecommitdiffstats
path: root/ctrl/libhicnctrl/src/api_private.h
blob: 2f483ad2dc8e27472e65e370fa01f0e983c746c3 (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
/*
 * Copyright (c) 2021 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef HICN_API_PRIVATE_H
#define HICN_API_PRIVATE_H

#include <hicn/ctrl/api.h>
#include <hicn/util/token.h>
#include <hicn/util/log.h>
#include <hicn/util/map.h>
#ifndef HICN_VPP_PLUGIN
#include <hicn/util/sstrncpy.h>
#endif
#include <hicn/validation.h>
#include <ctype.h>

#define ARRAY_SIZE(array) (sizeof(array) / sizeof(*array))

/*
 * Input validation
 */

static inline bool IS_VALID_ADDRESS(const hicn_ip_address_t *addr, int family) {
  char addr_str[INET6_ADDRSTRLEN];
  return !hicn_ip_address_empty(addr) &&
         hicn_ip_address_ntop(addr, addr_str, INET6_ADDRSTRLEN, family) >= 0;
}

static inline bool IS_VALID_PREFIX_LEN(u8 len) {
  return len <= MAX_IPV6_PREFIX_LEN;
}

// https://github.com/shemminger/iproute2/blob/50b668bdbf0ebc270495eb4b352d0c3982159d0a/lib/utils.c#L825
static inline bool IS_VALID_INTERFACE_NAME(const char *name) {
  size_t len = strnlen_s(name, INTERFACE_LEN);
  if (len == 0 || len >= IFNAMSIZ) return true;

  while (*name) {
    if (*name == '/' || isspace(*name)) return false;
    ++name;
  }

  return true;
}

static inline bool IS_VALID_NAME(const char *name) {
  return is_symbolic_name(name, SYMBOLIC_NAME_LEN);
}

static inline bool IS_VALID_STR_ID(const char *name) {
  return is_number(name, SYMBOLIC_NAME_LEN);
}

#define IS_VALID_TYPE(x) IS_VALID_ENUM_TYPE(FACE_TYPE, x)
#define IS_VALID_ADDR_TYPE(x) ((x >= ADDR_INET) && (x <= ADDR_UNIX))
#define IS_VALID_FACE_ID(x) ((x) != INVALID_FACE_ID)

#define IS_VALID_ID(x) (1)
#define IS_VALID_POLICY(x) (1)

typedef struct hc_sock_impl_s hc_sock_impl_t;

int hc_data_ensure_available(hc_data_t *data, size_t count);
u8 *hc_data_get_next(hc_data_t *data);

#endif  // HICN_API_PRIVATE_H