summaryrefslogtreecommitdiffstats
path: root/ctrl/libhicnctrl/src/modules/hicn_light/face.c
blob: 4dcd96191a3f6195b969b5328c92b8d14cf5f0f1 (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
163
164
165
#include <hicn/ctrl/objects/listener.h>
#include <hicn/util/log.h>

#include "base.h"
#include "face.h"

int hc_face_from_connection(const hc_connection_t *connection,
                            hc_face_t *face) {
  int rc;
  switch (connection->type) {
    case FACE_TYPE_TCP:
      *face = (hc_face_t){
          .id = connection->id,
          .type = FACE_TYPE_TCP,
          .family = connection->family,
          .local_addr = connection->local_addr,
          .local_port = connection->local_port,
          .remote_addr = connection->remote_addr,
          .remote_port = connection->remote_port,
          .admin_state = connection->admin_state,
          .state = connection->state,
          .priority = connection->priority,
          .tags = connection->tags,
      };
      break;
    case FACE_TYPE_UDP:
      *face = (hc_face_t){
          .id = connection->id,
          .type = FACE_TYPE_UDP,
          .family = connection->family,
          .local_addr = connection->local_addr,
          .local_port = connection->local_port,
          .remote_addr = connection->remote_addr,
          .remote_port = connection->remote_port,
          .admin_state = connection->admin_state,
          .state = connection->state,
          .priority = connection->priority,
          .tags = connection->tags,
      };
      break;
    case FACE_TYPE_HICN:
      *face = (hc_face_t){
          .id = connection->id,
          .type = FACE_TYPE_HICN,
          .family = connection->family,
          .netdevice.index = NETDEVICE_UNDEFINED_INDEX,  // XXX
          .local_addr = connection->local_addr,
          .remote_addr = connection->remote_addr,
          .admin_state = connection->admin_state,
          .state = connection->state,
          .priority = connection->priority,
          .tags = connection->tags,
      };
      break;
    default:
      return -1;
  }
  face->netdevice.name[0] = '\0';
  face->netdevice.index = 0;
  rc = snprintf(face->name, SYMBOLIC_NAME_LEN, "%s", connection->name);
  if (rc >= SYMBOLIC_NAME_LEN)
    WARN(
        "[hc_connection_to_face] Unexpected truncation of symbolic name "
        "string");
  rc = snprintf(face->netdevice.name, INTERFACE_LEN, "%s",
                connection->interface_name);
  if (rc >= INTERFACE_LEN)
    WARN(
        "[hc_connection_to_face] Unexpected truncation of interface name "
        "string");
  netdevice_update_index(&face->netdevice);
  return 0;
}

int hc_face_to_connection(const hc_face_t *face, hc_connection_t *connection,
                          bool generate_name) {
  int rc;

  switch (face->type) {
    case FACE_TYPE_HICN:
      *connection = (hc_connection_t){
          .type = FACE_TYPE_HICN,
          .family = face->family,
          .local_addr = face->local_addr,
          .local_port = 0,
          .remote_addr = face->remote_addr,
          .remote_port = 0,
          .admin_state = face->admin_state,
          .state = face->state,
          .priority = face->priority,
          .tags = face->tags,
      };
      rc = snprintf(connection->name, SYMBOLIC_NAME_LEN, "%s",
                    face->netdevice.name);
      if (rc >= SYMBOLIC_NAME_LEN)
        WARN(
            "[hc_face_to_connection] Unexpected truncation of symbolic "
            "name string");
      break;
    case FACE_TYPE_TCP:
      *connection = (hc_connection_t){
          .type = FACE_TYPE_TCP,
          .family = face->family,
          .local_addr = face->local_addr,
          .local_port = face->local_port,
          .remote_addr = face->remote_addr,
          .remote_port = face->remote_port,
          .admin_state = face->admin_state,
          .state = face->state,
          .priority = face->priority,
          .tags = face->tags,
      };
      if (generate_name) {
        rc = snprintf(connection->name, SYMBOLIC_NAME_LEN, "tcp%u", RANDBYTE());
        if (rc >= SYMBOLIC_NAME_LEN)
          WARN(
              "[hc_face_to_connection] Unexpected truncation of "
              "symbolic name string");
      } else {
        memset(connection->name, 0, SYMBOLIC_NAME_LEN);
      }
      break;
    case FACE_TYPE_UDP:
      *connection = (hc_connection_t){
          .type = FACE_TYPE_UDP,
          .family = face->family,
          .local_addr = face->local_addr,
          .local_port = face->local_port,
          .remote_addr = face->remote_addr,
          .remote_port = face->remote_port,
          .admin_state = face->admin_state,
          .state = face->state,
          .priority = face->priority,
          .tags = face->tags,
      };
      if (generate_name) {
        rc = snprintf(connection->name, SYMBOLIC_NAME_LEN, "udp%u", RANDBYTE());
        if (rc >= SYMBOLIC_NAME_LEN)
          WARN(
              "[hc_face_to_connection] Unexpected truncation of "
              "symbolic name string");
      } else {
        memset(connection->name, 0, SYMBOLIC_NAME_LEN);
      }
      snprintf(connection->interface_name, INTERFACE_LEN, "%s",
               face->netdevice.name);
      break;
    default:
      return -1;
  }

  connection->id = face->id;
  rc = snprintf(connection->interface_name, INTERFACE_LEN, "%s",
                face->netdevice.name);
  if (rc >= INTERFACE_LEN)
    WARN(
        "hc_face_to_connection] Unexpected truncation of interface name "
        "string");

  return 0;
}

int hc_face_to_listener(const hc_face_t *face, hc_listener_t *listener) {
  return -99; /* XXX Not implemented */
}