2 * Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
21 #include "vine-event-loop.h"
22 #include "vine-data-path.h"
23 #include "vine-data-path-plugin.h"
24 #include "vine-security.h"
26 #include "vine-utils.h"
29 vine_address_family_e addr_family;
32 vine_security_h security;
34 vine_data_path_opened_cb opened_cb;
36 vine_data_path_accepted_cb accepted_cb;
37 void *accepted_cb_data;
38 vine_data_path_connected_cb connected_cb;
39 void *connected_cb_data;
40 vine_data_path_received_cb recv_cb;
42 vine_data_path_terminated_cb terminated_cb;
43 void *terminated_cb_data;
45 vine_dp_plugin_h plugin_handle;
46 vine_event_queue_h event_queue;
48 VineDataPathState *state;
49 established_notifier established;
51 vine_data_path_h listen_dp; // only for client dp in the server side
57 } vine_dp_opened_event;
61 } vine_dp_connected_event;
65 } vine_dp_received_event;
68 vine_data_path_s *connected_dp;
69 } vine_dp_accepted_event;
71 vine_dp_plugin_fn g_dp_plugin_fn = {
74 .register_callbacks = NULL,
75 .process_event = NULL,
84 static vine_dp_plugin_callbacks g_dp_plugin_cbs = {
91 .terminated_cb = NULL,
94 static void (*__init_plugin)(vine_dp_plugin_fn *fn);
95 static vine_data_path_s *_vine_data_path_create(vine_address_family_e addr_family,
96 const char *addr, int port, vine_security_h security,
97 void *plugin_data, vine_event_queue_h event_queue);
99 static vine_error_e __convert_data_path_error_to_vine_error(vine_data_path_error error)
102 case VINE_DATA_PATH_ERROR_NONE:
103 return VINE_ERROR_NONE;
104 case VINE_DATA_PATH_ERROR_INVALID_PARAMETER:
105 return VINE_ERROR_INVALID_PARAMETER;
106 case VINE_DATA_PATH_ERROR_INVALID_OPERATION:
107 return VINE_ERROR_INVALID_OPERATION;
108 case VINE_DATA_PATH_ERROR_OPERATION_FAILED:
109 return VINE_ERROR_OPERATION_FAILED;
110 case VINE_DATA_PATH_ERROR_OUT_OF_MEMORY:
111 return VINE_ERROR_OUT_OF_MEMORY;
113 return VINE_ERROR_OPERATION_FAILED;
117 static vine_error_e __convert_address_family(vine_address_family_e addr_family,
118 vine_dp_addr_family_e *dp_addr_family)
120 switch (addr_family) {
121 case VINE_ADDRESS_FAMILY_DEFAULT:
122 *dp_addr_family = VINE_DP_IPV4_IPV6;
124 case VINE_ADDRESS_FAMILY_IPV4:
125 *dp_addr_family = VINE_DP_IPV4;
127 case VINE_ADDRESS_FAMILY_IPV6:
128 *dp_addr_family = VINE_DP_IPV6;
131 return VINE_ERROR_INVALID_PARAMETER;
134 return VINE_ERROR_NONE;
137 void __vine_dp_poll_handler(int fd, int events, void *user_data)
139 g_dp_plugin_fn.process_event(fd, events);
142 void __vine_dp_op_handler(int fd, int events, void *user_data)
146 vine_event_loop_del_io_handler(fd);
147 if (read(fd, &v, sizeof(v)) == -1) {
148 VINE_LOGE("Read error(%d)", errno);
151 g_dp_plugin_fn.process_event(fd, events);
154 static void __pollfd_cb(vine_data_path_pollfd_op_e op, int fd, int events)
157 case VINE_DATA_PATH_POLLFD_ADD:
158 vine_event_loop_add_io_handler(fd, events, __vine_dp_poll_handler, NULL);
160 case VINE_DATA_PATH_POLLFD_DEL:
161 vine_event_loop_del_io_handler(fd);
163 case VINE_DATA_PATH_POLLFD_MOD:
164 vine_event_loop_mod_io_handler(fd, events, __vine_dp_poll_handler, NULL);
166 case VINE_DATA_PATH_POLLFD_OP:
167 vine_event_loop_add_io_handler(fd, events, __vine_dp_op_handler, NULL);
169 case VINE_DATA_PATH_POLLFD_LOCK_UNLOCK:
173 VINE_LOGE("invalid operation.");
178 static void __free_accepted_event(void *data)
183 vine_dp_accepted_event *event = (vine_dp_accepted_event *)data;
184 // CHECK: connected_dp should not be freed
188 static void __invoke_accepted_user_cb(void *event, void *user_data)
190 RET_IF(event == NULL, "event is NULL");
192 vine_dp_accepted_event *accepted_event = (vine_dp_accepted_event *)event;
193 vine_data_path_s *listen_dp = (vine_data_path_s *)user_data;
195 VINE_LOGD("user callback is invoked by event queue.");
197 if (listen_dp && listen_dp->accepted_cb)
198 listen_dp->accepted_cb(accepted_event->connected_dp, listen_dp->accepted_cb_data);
201 void notify_accepted(vine_data_path_h datapath, int result)
203 VINE_LOGD("notify_accepted dp[%p] result[%d]", datapath, result);
205 RET_IF(datapath == NULL, "dp is NULL");
206 vine_data_path_s *dp = (vine_data_path_s *)datapath;
209 // TODO: Handle auth failure
210 VINE_LOGE("Failure dp[%p]", datapath);
211 vine_data_path_destroy(datapath);
215 vine_dp_accepted_event *accepted_event =
216 (vine_dp_accepted_event *)calloc(1, sizeof(vine_dp_accepted_event));
217 RET_IF(accepted_event == NULL, "Out of memory");
218 accepted_event->connected_dp = dp;
220 VINE_LOGD("Create a accepted_event[%p]", accepted_event);
222 vine_data_path_s *listen_dp = (vine_data_path_s *)dp->listen_dp;
223 VINE_LOGD("listen_dp[%p]", listen_dp);
225 vine_event_loop_add_event(listen_dp->event_queue, accepted_event,
226 __invoke_accepted_user_cb, __free_accepted_event, listen_dp);
229 static void __invoke_opened_user_cb(void *event, void *user_data)
231 RET_IF(event == NULL, "event is null");
233 vine_dp_opened_event *opened_event = (vine_dp_opened_event *)event;
234 vine_data_path_s *dp = (vine_data_path_s *)user_data;
236 VINE_LOGD("user callback is invoked by event queue");
238 if (dp && dp->opened_cb)
239 dp->opened_cb(dp, opened_event->result, opened_event->port, dp->opened_cb_data);
241 static void __opened_cb(int result, int port, void *user_data)
243 RET_IF(user_data == NULL, "dp is NULL");
245 vine_data_path_s *dp = (vine_data_path_s *)user_data;
246 vine_dp_opened_event *opened_event =
247 (vine_dp_opened_event *)calloc(1, sizeof(vine_dp_opened_event));
248 opened_event->result = result;
249 opened_event->port = port;
251 vine_event_loop_add_event(dp->event_queue, opened_event,
252 __invoke_opened_user_cb, free, dp);
255 static vine_address_family_e __convert_addr_family(vine_dp_addr_family_e addr_family)
257 switch (addr_family) {
258 case VINE_DP_IPV4_IPV6:
259 return VINE_ADDRESS_FAMILY_DEFAULT;
261 return VINE_ADDRESS_FAMILY_IPV4;
263 return VINE_ADDRESS_FAMILY_IPV6;
265 return VINE_ADDRESS_FAMILY_DEFAULT;
268 static void __accepted_cb(vine_dp_addr_family_e addr_family, char *addr,
269 int port, void *plugin_data, void *user_data)
271 RET_IF(user_data == NULL, "listen_dp is NULL");
273 vine_data_path_s *listen_dp = (vine_data_path_s *)user_data;
275 VINE_LOGE("listen_dp[%p], security[%p]", listen_dp, listen_dp->security);
276 vine_data_path_s *connected_dp = _vine_data_path_create(__convert_addr_family(addr_family),
277 addr, port, listen_dp->security, plugin_data, listen_dp->event_queue);
278 RET_IF(connected_dp == NULL, "Out of memory");
280 VINE_LOGD("Accepted dp[%p] addr[%s] port[%d] listen_dp[%p]", connected_dp, addr, port, user_data);
281 connected_dp->listen_dp = listen_dp;
283 VINE_LOGD("Start Authentication");
284 connected_dp->established = notify_accepted;
285 start_accepted_state(connected_dp, connected_dp->plugin_handle, connected_dp->state);
288 static void __invoke_connected_user_cb(void *event, void *user_data)
290 RET_IF(event == NULL, "event is null");
292 vine_dp_connected_event *connected_event = (vine_dp_connected_event *)event;
293 vine_data_path_s *dp = (vine_data_path_s *)user_data;
295 VINE_LOGD("user callback is invoked by event queue.");
297 if (dp && dp->connected_cb)
298 dp->connected_cb(dp, connected_event->result, dp->connected_cb_data);
301 void notify_connected(vine_data_path_h datapath, int result)
303 VINE_LOGD("notify_connected dp[%p] result[%d]", datapath, result);
305 RET_IF(datapath == NULL, "dp is NULL");
306 vine_dp_connected_event *connected_event =
307 (vine_dp_connected_event *)calloc(1, sizeof(vine_dp_connected_event));
308 RET_IF(connected_event == NULL, "Out of memory");
310 connected_event->result = result;
311 vine_data_path_s *dp = (vine_data_path_s *)datapath;
312 vine_event_loop_add_event(dp->event_queue, connected_event,
313 __invoke_connected_user_cb, free, datapath);
316 static void __connected_cb(int result, void *user_data)
318 VINE_LOGD("Connected dp[%p] result[%d]", user_data, result);
320 RET_IF(user_data == NULL, "dp is NULL");
322 vine_data_path_s *dp = (vine_data_path_s *)user_data;
324 VINE_LOGE("Fail to connect");
325 notify_connected(dp, result);
329 VINE_LOGD("Start Authentication");
330 dp->established = notify_connected;
331 start_connected_state(dp, dp->plugin_handle, dp->state);
334 static void __invoke_received_user_cb(void *event, void *user_data)
336 RET_IF(event == NULL, "Out of memory");
338 vine_dp_received_event *received_event = (vine_dp_received_event *)event;
339 vine_data_path_s *dp = (vine_data_path_s *)user_data;
341 VINE_LOGD("user callback is invoked by event queue.");
343 if (dp && dp->recv_cb) {
344 // user can read the data in recv_cb().
345 dp->recv_cb(dp, received_event->bytes, dp->recv_cb_data);
349 void notify_data_received(vine_data_path_h datapath, size_t bytes)
351 vine_dp_received_event *received_event =
352 (vine_dp_received_event *)calloc(1, sizeof(vine_dp_received_event));
353 RET_IF(received_event == NULL, "Out of memory");
355 received_event->bytes = bytes;
357 VINE_LOGD("Create a received_event[%p] datapath[%p]", received_event, datapath);
359 vine_data_path_s *dp = (vine_data_path_s *)datapath;
361 vine_event_loop_add_event(dp->event_queue, received_event,
362 __invoke_received_user_cb, free, datapath);
365 static void __received_cb(size_t bytes, void *user_data)
367 RET_IF(user_data == NULL, "dp is NULL");
369 vine_data_path_s *dp = (vine_data_path_s *)user_data;
370 dp->state->received_cb(bytes);
373 static void __written_cb(int bytes, void *user_data)
375 VINE_LOGD("%d bytes are written through datapath[%p].", bytes, user_data);
376 vine_data_path_s *dp = (vine_data_path_s *)user_data;
377 dp->state->written_cb(bytes);
380 static void __invoke_terminated_user_cb(void *event, void *user_data)
382 RET_IF(user_data == NULL, "dp is NULL");
384 vine_data_path_s *dp = (vine_data_path_s *)user_data;
386 VINE_LOGD("user callback is invoked by event queue.");
388 start_default_state(dp, dp->plugin_handle, dp->state);
389 if (dp->terminated_cb)
390 dp->terminated_cb(dp, dp->terminated_cb_data);
393 static void __terminated_cb(void *user_data)
395 vine_data_path_s *dp = (vine_data_path_s *)user_data;
397 vine_event_loop_add_event(dp->event_queue, NULL,
398 __invoke_terminated_user_cb, NULL, user_data);
401 static int _load_data_path_plugins(void)
403 void *handle = dlopen(DATA_PATH_PLUGIN_PATH, RTLD_LAZY | RTLD_NODELETE);
405 __init_plugin = (void (*)(vine_dp_plugin_fn *))dlsym(handle, "vine_data_path_plugin_init");
407 VINE_LOGI("Loaded %s", DATA_PATH_PLUGIN_PATH);
409 VINE_LOGE("%s doesn't exist", DATA_PATH_PLUGIN_PATH);
410 return VINE_ERROR_OPERATION_FAILED;
413 return VINE_ERROR_NONE;
416 int vine_data_path_set_received_cb(
417 vine_data_path_h datapath, vine_data_path_received_cb callback, void *user_data)
419 RET_VAL_IF(datapath == NULL, VINE_ERROR_INVALID_PARAMETER, "datapath is NULL");
420 RET_VAL_IF(callback == NULL, VINE_ERROR_INVALID_PARAMETER, "callback is NULL");
422 vine_data_path_s *dp = (vine_data_path_s *)datapath;
423 dp->recv_cb = callback;
424 dp->recv_cb_data = user_data;
426 return VINE_ERROR_NONE;
429 int vine_data_path_unset_received_cb(vine_data_path_h datapath)
431 RET_VAL_IF(datapath == NULL, VINE_ERROR_INVALID_PARAMETER, "datapath is NULL");
433 vine_data_path_s *dp = (vine_data_path_s *)datapath;
435 dp->recv_cb_data = NULL;
437 return VINE_ERROR_NONE;
440 int vine_data_path_write(vine_data_path_h datapath, unsigned char *buf, size_t len)
442 RET_VAL_IF(datapath == NULL, VINE_ERROR_INVALID_PARAMETER, "datapath is NULL");
443 RET_VAL_IF(buf == NULL, VINE_ERROR_INVALID_PARAMETER, "buf is NULL");
444 RET_VAL_IF(len == 0, VINE_ERROR_INVALID_PARAMETER, "len cannot be 0");
446 vine_data_path_s *dp = (vine_data_path_s *)datapath;
447 return dp->state->write(buf, len);
450 int vine_data_path_read(vine_data_path_h datapath,
451 unsigned char *buf, size_t buf_len, size_t *read_len)
453 RET_VAL_IF(datapath == NULL, VINE_ERROR_INVALID_PARAMETER, "datapath is NULL");
454 RET_VAL_IF(buf == NULL, VINE_ERROR_INVALID_PARAMETER, "buf is NULL");
455 RET_VAL_IF(buf_len == 0, VINE_ERROR_INVALID_PARAMETER, "buf_len cannot be 0");
456 RET_VAL_IF(read_len == NULL, VINE_ERROR_INVALID_PARAMETER, "read_len is NULL");
458 vine_data_path_s *dp = (vine_data_path_s *)datapath;
459 return dp->state->read(buf, buf_len, read_len);
462 int vine_data_path_close(vine_data_path_h datapath)
464 RET_VAL_IF(datapath == NULL, VINE_ERROR_INVALID_PARAMETER, "datapath is NULL");
466 vine_data_path_s *dp = (vine_data_path_s *)datapath;
467 g_dp_plugin_fn.close(dp->plugin_handle);
469 return VINE_ERROR_NONE;
472 static vine_data_path_s *_vine_data_path_create(vine_address_family_e addr_family,
473 const char *addr, int port, vine_security_h security,
474 void *plugin_data, vine_event_queue_h event_queue)
476 RET_VAL_IF(addr == NULL, NULL, "addr is NULL");
478 int ret = VINE_ERROR_NONE;
479 vine_data_path_s *dp = NULL;
481 dp = (vine_data_path_s *)calloc(1, sizeof(vine_data_path_s));
482 RET_VAL_IF(dp == NULL, NULL, "Out of memory");
484 dp->addr_family = addr_family;
485 dp->addr = STRDUP(addr);
486 if (dp->addr == NULL) {
487 VINE_LOGE("Out of memory");
493 dp->recv_cb_data = NULL;
494 dp->event_queue = event_queue;
495 if (security != NULL) {
496 ret = _vine_security_clone(&(dp->security), security);
497 if (ret != VINE_ERROR_NONE) {
498 VINE_LOGE("Fail to clone security %d", ret);
508 ret = g_dp_plugin_fn.create(&dp->plugin_handle, plugin_data, dp);
509 if (ret != VINE_DATA_PATH_ERROR_NONE) {
511 _vine_security_destroy(dp->security);
517 dp->state = vine_get_default_state(dp, dp->plugin_handle);
518 VINE_LOGD("datapath[%p] is created.", dp);
522 int vine_data_path_destroy(vine_data_path_h datapath)
524 RET_VAL_IF(datapath == NULL, VINE_ERROR_INVALID_PARAMETER, "datapath is NULL");
525 vine_data_path_s *dp = (vine_data_path_s *)datapath;
527 g_dp_plugin_fn.destroy(dp->plugin_handle);
528 dp->plugin_handle = NULL;
531 _vine_security_destroy(dp->security);
533 dp->listen_dp = NULL;
536 VINE_LOGD("data_path[%p] is destroyed", datapath);
538 return VINE_ERROR_NONE;
541 const char *vine_data_path_get_ip(vine_data_path_h datapath)
543 RET_VAL_IF(datapath == NULL, NULL, "datapath is NULL");
544 vine_data_path_s *dp = (vine_data_path_s *)datapath;
545 return (const char *)dp->addr;
548 vine_address_family_e vine_data_path_get_addr_family(vine_data_path_h datapath)
550 RET_VAL_IF(datapath == NULL, VINE_ADDRESS_FAMILY_DEFAULT, "datapath is NULL");
551 vine_data_path_s *dp = (vine_data_path_s *)datapath;
552 return dp->addr_family;
555 int vine_data_path_get_port(vine_data_path_h datapath)
557 RET_VAL_IF(datapath == NULL, VINE_ERROR_INVALID_PARAMETER, "datapath is NULL");
558 vine_data_path_s *dp = (vine_data_path_s *)datapath;
562 static void __set_state(vine_data_path_h datapath, VineDataPathState *state)
564 RET_IF(datapath == NULL, "datapath is NULL");
566 vine_data_path_s *dp = (vine_data_path_s *)datapath;
567 VINE_LOGD("Datapath[%p] state: %s --> %s",
568 datapath, dp->state->str().c_str(), state->str().c_str());
572 static void __established(vine_data_path_h datapath, int result)
574 RET_IF(datapath == NULL, "datapath is NULL");
576 vine_data_path_s *dp = (vine_data_path_s *)datapath;
577 dp->established(dp, result);
580 static void __data_received(vine_data_path_h dp, size_t bytes)
582 notify_data_received(dp, bytes);
585 int vine_data_path_init(void)
587 int ret = _load_data_path_plugins();
588 RET_VAL_IF(ret != VINE_ERROR_NONE, ret, "Fail to load plugins");
589 __init_plugin(&g_dp_plugin_fn);
591 ret = g_dp_plugin_fn.init();
592 RET_VAL_IF(ret != VINE_DATA_PATH_ERROR_NONE,
593 __convert_data_path_error_to_vine_error((vine_data_path_error)ret),
594 "Filed to init dp-plugin");
596 g_dp_plugin_cbs.pollfd_cb = __pollfd_cb;
597 g_dp_plugin_cbs.opened_cb = __opened_cb;
598 g_dp_plugin_cbs.accepted_cb = __accepted_cb;
599 g_dp_plugin_cbs.connected_cb = __connected_cb;
600 g_dp_plugin_cbs.received_cb = __received_cb;
601 g_dp_plugin_cbs.written_cb = __written_cb;
602 g_dp_plugin_cbs.terminated_cb = __terminated_cb;
603 g_dp_plugin_fn.register_callbacks(g_dp_plugin_cbs);
605 vine_dp_state_set_state_changed_notifier(__set_state);
606 vine_dp_state_set_established_notifier(__established);
607 vine_dp_state_set_data_received_notifier(__data_received);
608 return VINE_ERROR_NONE;
611 int vine_data_path_deinit(void)
613 g_dp_plugin_fn.deinit();
615 return VINE_ERROR_NONE;
618 static vine_dp_tls_version_e __convert_tls_version(vine_security_tls_version_e version)
621 case VINE_SECURITY_TLS_VERSION_DEFAULT:
622 return VINE_DP_TLS_VERSION_DEFAULT;
623 case VINE_SECURITY_TLS_VERSION_1_0:
624 return VINE_DP_TLS_VERSION_1_0;
625 case VINE_SECURITY_TLS_VERSION_1_1:
626 return VINE_DP_TLS_VERSION_1_1;
627 case VINE_SECURITY_TLS_VERSION_1_2:
628 return VINE_DP_TLS_VERSION_1_2;
629 case VINE_SECURITY_TLS_VERSION_1_3:
630 return VINE_DP_TLS_VERSION_1_3;
632 return VINE_DP_TLS_VERSION_MAX;
636 static void _extract_security_info(vine_security_h src, vine_dp_ssl *dest)
638 RET_IF(src == NULL, "src is NULL");
639 RET_IF(dest == NULL, "dest is NULL");
641 vine_security_type_e type = VINE_SECURITY_TYPE_NONE;
642 vine_security_tls_version_e version = VINE_SECURITY_TLS_VERSION_DEFAULT;
645 _vine_security_get_type(src, &type);
646 dest->use_ssl = (type != VINE_SECURITY_TYPE_NONE) ? true : false;
651 _vine_security_get_tls_version(src, &version);
652 dest->tls_version = __convert_tls_version(version);
654 _vine_security_get_verification_flags(src, &flags);
655 dest->vflags |= (flags & VINE_SECURITY_VERIFICATION_FLAG_ALLOW_SELF_SIGNED) ?
656 VINE_DATA_PATH_V_FLAG_ALLOW_SS_CERTIFICATE : 0;
657 dest->vflags |= (flags & VINE_SECURITY_VERIFICATION_FLAG_SKIP_HOST_NAME_CHECK) ?
658 VINE_DATA_PATH_V_FLAG_SKIP_CN_CHECK : 0;
660 _vine_security_get_ca_path(src, &dest->ca_path);
661 _vine_security_get_cert_path(src, &dest->cert_path);
662 _vine_security_get_private_key(src, &dest->key_path);
665 static void _destroy_security_info(vine_dp_ssl *dest)
667 RET_IF(dest == NULL, "dest is NULL");
670 free(dest->cert_path);
671 free(dest->key_path);
674 int vine_data_path_open(vine_address_family_e addr_family, int port, const char *iface_name,
675 int max_conn, vine_security_h security, const char *host_name,
676 vine_data_path_opened_cb opened_cb, void *opened_cb_data,
677 vine_data_path_accepted_cb accepted_cb, void *accepted_cb_data,
678 vine_data_path_h *opened_datapath,
679 vine_event_queue_h event_queue)
681 vine_dp_addr_family_e dp_addr_family;
682 int ret = __convert_address_family(addr_family, &dp_addr_family);
683 if (ret != VINE_ERROR_NONE) {
684 VINE_LOGE("Cannot convert address family.");
688 vine_data_path_s *dp =
689 _vine_data_path_create(addr_family, "", port, security, NULL, event_queue);
690 RET_VAL_IF(dp == NULL, VINE_ERROR_OUT_OF_MEMORY, "Out of memory");
692 vine_dp_ssl ssl = {false, VINE_DP_TLS_VERSION_DEFAULT, 0, NULL, NULL, NULL};
693 _extract_security_info(security, &ssl);
695 dp->opened_cb = opened_cb;
696 dp->opened_cb_data = opened_cb_data;
697 dp->accepted_cb = accepted_cb;
698 dp->accepted_cb_data = accepted_cb_data;
702 ret = g_dp_plugin_fn.set_host_name(dp->plugin_handle, host_name);
703 if (ret != VINE_DATA_PATH_ERROR_NONE) {
704 vine_data_path_destroy(dp);
705 return __convert_data_path_error_to_vine_error((vine_data_path_error)ret);
709 ret = g_dp_plugin_fn.open(dp->plugin_handle, dp_addr_family, port, iface_name, max_conn, ssl);
710 _destroy_security_info(&ssl);
712 if (ret != VINE_DATA_PATH_ERROR_NONE) {
713 vine_data_path_destroy(dp);
714 return __convert_data_path_error_to_vine_error((vine_data_path_error)ret);
717 if (*opened_datapath) {
718 VINE_LOGI("Destroy a stale datapath.");
719 vine_data_path_destroy(*opened_datapath);
721 *opened_datapath = dp;
723 return VINE_ERROR_NONE;
726 int vine_data_path_connect(vine_address_family_e addr_family,
727 const char *ip, int port, const char *iface_name,
728 vine_security_h security, const char *host_name, const char *token,
729 vine_data_path_connected_cb callback, void *user_data,
730 vine_data_path_h *connected_datapath, vine_event_queue_h event_queue)
732 vine_data_path_s *dp =
733 _vine_data_path_create(addr_family, ip, port, security, NULL, event_queue);
734 RET_VAL_IF(dp == NULL, VINE_ERROR_OUT_OF_MEMORY, "Out of memory");
735 RET_VAL_IF(connected_datapath == NULL,
736 VINE_ERROR_INVALID_PARAMETER, "connected_datapath is NULL");
738 dp->connected_cb = callback;
739 dp->connected_cb_data = user_data;
741 vine_dp_ssl ssl = {false, VINE_DP_TLS_VERSION_DEFAULT, 0, NULL, NULL, NULL};
742 _extract_security_info(security, &ssl);
748 ret = g_dp_plugin_fn.set_host_name(dp->plugin_handle, host_name);
749 if (ret != VINE_DATA_PATH_ERROR_NONE) {
750 vine_data_path_destroy(dp);
751 return __convert_data_path_error_to_vine_error((vine_data_path_error)ret);
756 ret = g_dp_plugin_fn.set_token(dp->plugin_handle, token);
757 if (ret != VINE_DATA_PATH_ERROR_NONE) {
758 vine_data_path_destroy(dp);
759 _destroy_security_info(&ssl);
760 return __convert_data_path_error_to_vine_error((vine_data_path_error)ret);
764 ret = g_dp_plugin_fn.connect(dp->plugin_handle,
765 addr_family == VINE_ADDRESS_FAMILY_IPV4 ? VINE_DP_IPV4 : VINE_DP_IPV6,
766 ip, port, iface_name, ssl);
767 _destroy_security_info(&ssl);
769 if (*connected_datapath) {
770 VINE_LOGI("Destroy a stale datapath.");
771 vine_data_path_destroy(*connected_datapath);
774 *connected_datapath = dp;
776 return __convert_data_path_error_to_vine_error((vine_data_path_error)ret);
779 int vine_data_path_set_terminated_cb(vine_data_path_h datapath,
780 vine_data_path_terminated_cb callback, void *user_data)
782 RET_VAL_IF(datapath == NULL, VINE_ERROR_INVALID_PARAMETER, "datapath is NULL");
783 RET_VAL_IF(callback == NULL, VINE_ERROR_INVALID_PARAMETER, "callback is NULL");
785 vine_data_path_s *dp = (vine_data_path_s *)datapath;
786 dp->terminated_cb = callback;
787 dp->terminated_cb_data = user_data;
789 return VINE_ERROR_NONE;
792 int vine_data_path_get_token(vine_data_path_h datapath, char **token)
794 RET_VAL_IF(datapath == NULL, VINE_ERROR_INVALID_PARAMETER, "datapath is NULL");
795 RET_VAL_IF(token == NULL, VINE_ERROR_INVALID_PARAMETER, "token is NULL");
797 vine_data_path_s *dp = (vine_data_path_s *)datapath;
798 int ret = g_dp_plugin_fn.get_token(dp->plugin_handle, token);
799 return __convert_data_path_error_to_vine_error((vine_data_path_error)ret);
802 vine_security_h vine_data_path_get_security(vine_data_path_h datapath)
804 RET_VAL_IF(datapath == NULL, NULL, "datapath is NULL");
806 vine_data_path_s *dp = (vine_data_path_s *)datapath;
810 int vine_data_path_get_local_address_info(vine_data_path_h datapath, int *address_family,
811 char **ip, int *port)
813 RET_VAL_IF(datapath == NULL, VINE_ERROR_INVALID_PARAMETER, "datapath is NULL");
814 RET_VAL_IF(address_family == NULL, VINE_ERROR_INVALID_PARAMETER, "address_family is NULL");
815 RET_VAL_IF(ip == NULL, VINE_ERROR_INVALID_PARAMETER, "ip is NULL");
816 RET_VAL_IF(port == NULL, VINE_ERROR_INVALID_PARAMETER, "port is NULL");
818 vine_data_path_s *dp = (vine_data_path_s *)datapath;
820 int addr_family, local_port;
821 char local_ip[VINE_MAX_IP_LEN + 1] = {0, };
822 int ret = g_dp_plugin_fn.get_local_address_info(dp->plugin_handle,
823 &addr_family, local_ip, &local_port);
825 error = __convert_data_path_error_to_vine_error((vine_data_path_error)ret);
826 if (error != VINE_ERROR_NONE)
829 *address_family = addr_family;
831 *ip = STRDUP(local_ip);
833 return VINE_ERROR_NONE;