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

#include <hicn/ctrl/hicn-light.h>

static int hicnlight_strategy_parse(const u8 *buffer, size_t size,
                                    hc_strategy_t *strategy) {
  return -1;
}

int _hicnlight_strategy_parse(const uint8_t *buffer, size_t size,
                              hc_object_t *object) {
  return hicnlight_strategy_parse(buffer, size, &object->strategy);
}

int hicnlight_strategy_serialize_create(const hc_object_t *object,
                                        uint8_t *packet) {
  return -1;
}

int hicnlight_strategy_serialize_delete(const hc_object_t *object,
                                        uint8_t *packet) {
  return -1;
}

int hicnlight_strategy_serialize_list(const hc_object_t *object,
                                      uint8_t *packet) {
  assert(!object);
  return -1;
}

int hicnlight_strategy_serialize_set(const hc_object_t *object,
                                     uint8_t *packet) {
  const hc_strategy_t *strategy = &object->strategy;

  msg_strategy_set_t *msg = (msg_strategy_set_t *)packet;
  *msg = (msg_strategy_set_t){.header =
                                  {
                                      .message_type = REQUEST_LIGHT,
                                      .command_id = COMMAND_TYPE_STRATEGY_SET,
                                      .length = 1,
                                      .seq_num = 0,
                                  },
                              .payload = {
                                  .address = strategy->address,
                                  .family = strategy->family,
                                  .len = strategy->len,
                                  .type = strategy->type,
                              }};

  return sizeof(msg_strategy_set_t);
}

#if 0
static hc_result_t *_strategy_add_local_prefix_serialize(
    hc_sock_t *socket, hc_strategy_t *strategy) {
  hc_result_t *res = malloc(sizeof(*res));
  char strategy_s[MAXSZ_HC_STRATEGY];
  strncpy(strategy->name, strategy_str(strategy->type),
          MAXSZ_STRATEGY_NAME - 1);

  int rc = hc_strategy_snprintf(strategy_s, MAXSZ_HC_STRATEGY, strategy);
  if (rc >= MAXSZ_HC_STRATEGY)
    WARN("[hicnlight_strategy_create] Unexpected truncation of strategy string");
  DEBUG("[hicnlight_strategy_create] strategy=%s", strategy_s);

  if (!IS_VALID_FAMILY(strategy->family) ||
      !IS_VALID_STRATEGY_TYPE(strategy->type) ||
      !IS_VALID_FAMILY(strategy->local_family)) {
    res->success = false;
    return res;
  }

  msg_strategy_add_local_prefix_t msg = {
      .header =
          {
              .message_type = REQUEST_LIGHT,
              .command_id = COMMAND_TYPE_STRATEGY_ADD_LOCAL_PREFIX,
              .length = 1,
              .seq_num = 0,
          },
      .payload = {
          .type = strategy->type,
          .address = strategy->address,
          .family = strategy->family,
          .len = strategy->len,
          .local_address = strategy->local_address,
          .local_family = strategy->local_family,
          .local_len = strategy->local_len,
      }};

  hc_command_params_t params = {
      .cmd = ACTION_SET,
      .cmd_id = COMMAND_TYPE_STRATEGY_ADD_LOCAL_PREFIX,
      .size_in = sizeof(cmd_strategy_add_local_prefix_t),
      .size_out = 0,
      .parse = NULL,
  };

  *res = (hc_result_t){
      .msg =
          (hc_msg_t){
              .header = msg.header,
              .payload.strategy_add_local_prefix = msg.payload,
          },
      .params = params,
      .async = false,
      .success = true,
  };
  return res;
}

/* How to retrieve that from the forwarder ? */
static const char *strategies[] = {
    "random",
    "load_balancer",
};

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

static int hicnlight_strategy_list(hc_sock_t *s, hc_data_t **data) {
  int rc;

  *data = hc_data_create(0, sizeof(hc_strategy_t), NULL);

  for (unsigned i = 0; i < ARRAY_SIZE(strategies); i++) {
    hc_strategy_t *strategy = (hc_strategy_t *)hc_data_get_next(*data);
    if (!strategy) return -1;
    rc = snprintf(strategy->name, MAXSZ_STRATEGY_NAME, "%s", strategies[i]);
    if (rc >= MAXSZ_STRATEGY_NAME)
      WARN("[hc_strategy_list] Unexpected truncation of strategy name string");
    (*data)->size++;
  }
  return -1;
}
#endif

DECLARE_MODULE_OBJECT_OPS(hicnlight, strategy);