aboutsummaryrefslogtreecommitdiffstats
path: root/ctrl/libhicnctrl/includes/hicn/ctrl/socket.h
blob: e53032955dc96e2a14e2a46aa3a89713966ba246 (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
166
167
168
169
170
171
172
173
174
/*
 * Copyright (c) 2021-2022 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.
 */

/**
 * \file socket.h
 * \brief Control socket
 */

#ifndef HICNCTRL_SOCKET_H
#define HICNCTRL_SOCKET_H

#include <hicn/ctrl/data.h>

/* With UDP, the buffer should be able to receieve a full packet, and thus MTU
 * (max 9000) is sufficient. Messages will be received fully one by one.
 * With TCP, the buffer should be at least able to receive a message header and
 * the maximum size of a data element, so any reasonable size will be correct,
 * it might just optimize performance. Messages might arrive in chunks that the
 * library is able to parse.
 */
#define JUMBO_MTU 9000
#define RECV_BUFLEN 65535

#define foreach_forwarder_type \
  _(UNDEFINED)                 \
  _(HICNLIGHT)                 \
  _(VPP)                       \
  _(N)

typedef enum {
#define _(x) FORWARDER_TYPE_##x,
  foreach_forwarder_type
#undef _
} forwarder_type_t;

extern const char *forwarder_type_str[];

#define forwarder_type_str(x) forwarder_type_str[x]

forwarder_type_t forwarder_type_from_str(const char *str);

/**
 * \brief Holds the state of an hICN control socket
 */
typedef struct hc_sock_s hc_sock_t;

/**
 * \brief Create an hICN control socket using the provided forwarder.
 * \return an hICN control socket
 */
hc_sock_t *hc_sock_create_forwarder(forwarder_type_t forwarder);

/**
 * \brief Create an hICN control socket using the provided forwarder and a
 * URL. \return an hICN control socket
 */
hc_sock_t *hc_sock_create_forwarder_url(forwarder_type_t forwarder,
                                        const char *url);

/**
 * \brief Create an hICN control socket using the default connection type.
 * XXX doc
 * \return an hICN control socket
 */
hc_sock_t *hc_sock_create(forwarder_type_t forwarder, const char *url);

/**
 * \brief Frees an hICN control socket
 * \param [in] s - hICN control socket
 */
void hc_sock_free(hc_sock_t *s);

/**
 * \brief Returns the next available sequence number to use for requests to
 * the API. \param [in] s - hICN control socket
 */
int hc_sock_get_next_seq(hc_sock_t *s);

/**
 * \brief Sets the socket as non-blocking
 * \param [in] s - hICN control socket
 * \return Error code
 */
int hc_sock_set_nonblocking(hc_sock_t *s);

/**
 * \brief Return the file descriptor associated to the hICN contorl sock
 * \param [in] s - hICN control socket
 * \return The file descriptor (positive value), or a negative integer in case
 * of error
 */
int hc_sock_get_fd(hc_sock_t *s);

/**
 * \brief Connect the socket
 * \return Error code
 */
int hc_sock_connect(hc_sock_t *s);

/**
 * \brief Return the offset and size of available buffer space
 * \param [in] s - hICN control socket
 * \param [out] buffer - Offset in buffer
 * \param [out] size - Remaining size
 * \return Error code
 */
int hc_sock_get_recv_buffer(hc_sock_t *s, uint8_t **buffer, size_t *size);

#if 0
/**
 * \brief Write/read iexchance on the control socket (internal helper
 * function) \param [in] s - hICN control socket \param [in] msg - Message to
 * send \param [in] msglen - Length of the message to send \return Error code
 */
int hc_sock_send(hc_sock_t *s, hc_msg_t *msg, size_t msglen, uint32_t seq);
#endif

/**
 * \brief Processing data received by socket
 * \param [in] s - hICN control socket
 * \param [in] parse - Parse function to convert remote types into lib native
 *      types, or NULL not to perform any translation.
 * \return Error code
 */
int hc_sock_process(hc_sock_t *s, hc_data_t **data);

int hc_sock_receive(hc_sock_t *s, hc_data_t **data);
int hc_sock_receive_all(hc_sock_t *s, hc_data_t **data);

#if 0
/**
 * \brief Callback used in async mode when data is available on the socket
 * \param [in] s - hICN control socket
 * \return Error code
 */
int hc_sock_callback(hc_sock_t *s, hc_data_t **data);
#endif

/**
 * \brief Reset the state of the sock (eg. to handle a reconnecton)
 * \param [in] s - hICN control socket
 * \return Error code
 */
int hc_sock_reset(hc_sock_t *s);

void hc_sock_increment_woff(hc_sock_t *s, size_t bytes);

#if 0
int hc_sock_prepare_send(hc_sock_t *s, hc_result_t *result,
                         data_callback_t complete_cb, void *complete_cb_data);

#endif

int hc_sock_set_recv_timeout_ms(hc_sock_t *s, long timeout_ms);

int hc_sock_set_async(hc_sock_t *s);

int hc_sock_is_async(hc_sock_t *s);

int hc_sock_on_receive(hc_sock_t *s, size_t count);

#endif /* HICNCTRL_SOCKET_H */