aboutsummaryrefslogtreecommitdiffstats
path: root/ctrl/libhicnctrl/src/modules/hicn_light/policy.c
blob: d48ce014de5c4dde98db4e6b9206a9cb30733808 (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
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
#include "policy.h"

/* POLICY CREATE */

static int _hcng_policy_create_internal(hc_sock_t *socket, hc_policy_t *policy,
                                        bool async) {
#if 0
  if (!IS_VALID_FAMILY(policy->family)) return -1;

  struct {
    cmd_header_t hdr;
    cmd_policy_add_t payload;
  } msg = {.hdr =
               {
                   .message_type = REQUEST_LIGHT,
                   COMMAND_TYPE_POLICY_ADD,
                   .length = 1,
                   .seq_num = 0,
               },
           .payload = {
               .address = policy->remote_addr,
               .family = policy->family,
               .len = policy->len,
               .policy = policy->policy,
           }};

  hc_command_params_t params = {
      .cmd = ACTION_CREATE,
      .cmd_id = COMMAND_TYPE_POLICY_ADD,
      .size_in = sizeof(cmd_policy_add_t),
      .size_out = 0,
      .parse = NULL,
  };

  return _hcng_execute_command(socket, (hc_msg_t *)&msg, sizeof(msg), &params,
                               NULL, async);
#endif
  return 0;  // XXX added
}

static int _hcng_policy_create(hc_sock_t *s, hc_policy_t *policy) {
  return _hcng_policy_create_internal(s, policy, false);
}

static int _hcng_policy_create_async(hc_sock_t *s, hc_policy_t *policy) {
  return _hcng_policy_create_internal(s, policy, true);
}

/* POLICY DELETE */

static int _hcng_policy_delete_internal(hc_sock_t *socket, hc_policy_t *policy,
                                        bool async) {
#if 0
  if (!IS_VALID_FAMILY(policy->family)) return -1;

  struct {
    cmd_header_t hdr;
    cmd_policy_remove_t payload;
  } msg = {.hdr =
               {
                   .message_type = REQUEST_LIGHT,
                   .command_id = COMMAND_TYPE_POLICY_REMOVE,
                   .length = 1,
                   .seq_num = 0,
               },
           .payload = {
               .address = policy->remote_addr,
               .family = policy->family,
               .len = policy->len,
           }};

  hc_command_params_t params = {
      .cmd = ACTION_DELETE,
      .cmd_id = COMMAND_TYPE_POLICY_REMOVE,
      .size_in = sizeof(cmd_policy_remove_t),
      .size_out = 0,
      .parse = NULL,
  };

  return _hcng_execute_command(socket, (hc_msg_t *)&msg, sizeof(msg), &params,
                               NULL, async);
#endif
  return 0;  // XXX added
}

static int _hcng_policy_delete(hc_sock_t *s, hc_policy_t *policy) {
  return _hcng_policy_delete_internal(s, policy, false);
}

static int _hcng_policy_delete_async(hc_sock_t *s, hc_policy_t *policy) {
  return _hcng_policy_delete_internal(s, policy, true);
}

/* POLICY PARSE */

static int hc_policy_parse(void *in, hc_policy_t *policy) {
  hc_policy_t *item = (hc_policy_t *)in;

  if (!IS_VALID_ADDRESS(item->address)) {
    ERROR("[hc_policy_parse] Invalid address");
    return -1;
  }
  if (!IS_VALID_FAMILY(item->family)) {
    ERROR("[hc_policy_parse] Invalid family");
    return -1;
  }
  if (!IS_VALID_PREFIX_LEN(item->len)) {
    ERROR("[hc_policy_parse] Invalid len");
    return -1;
  }
  if (!IS_VALID_POLICY(item->policy)) {
    ERROR("[hc_policy_parse] Invalid policy");
    return -1;
  }

  *policy = (hc_policy_t){
      .family = item->family,
      .remote_addr = item->remote_addr,
      .len = item->len,
      .policy = item->policy,
  };
  return 0;
}

/* POLICY LIST */

static int _hcng_policy_list_internal(hc_sock_t *socket, hc_data_t **pdata,
                                      bool async) {
#if 0
  struct {
    cmd_header_t hdr;
  } msg = {
      .hdr =
          {
              .message_type = REQUEST_LIGHT,
              .command_id = COMMAND_TYPE_POLICY_LIST,
              .length = 0,
              .seq_num = 0,
          },
  };

  hc_command_params_t params = {
      .cmd = ACTION_LIST,
      .cmd_id = COMMAND_TYPE_POLICY_LIST,
      .size_in = sizeof(hc_policy_t),
      .size_out = sizeof(hc_policy_t),
      .parse = (HC_PARSE)hc_policy_parse,
  };

  return _hcng_execute_command(socket, (hc_msg_t *)&msg, sizeof(msg), &params,
                               pdata, async);
#endif
  return 0;  // XXX added
}

static int _hcng_policy_list(hc_sock_t *s, hc_data_t **pdata) {
  return _hcng_policy_list_internal(s, pdata, false);
}

static int _hcng_policy_list_async(hc_sock_t *s, hc_data_t **pdata) {
  return _hcng_policy_list_internal(s, pdata, true);
}