Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
DkSDK FFI OCaml
Quick Start
Your C code must define a variable named dksdk_ffi_hosts that has been initialized with dksdk_ffi_init(...). Here is an example:
/*************************************************************************
* File: dksdk-ffi-ocaml/tests/Units/generic-ffi-main.c *
* *
* Copyright 2023 Diskuv, Inc. *
* *
* Licensed under the Open Software License version 3.0 *
* (the "License"); you may not use this file except in compliance *
* with the License. You may obtain a copy of the License at *
* *
* https://opensource.org/license/osl-3-0-php/ *
* *
*************************************************************************/
#include "dksdk_ffi_c.h"
#include "dksdk_ffi_c_cls_icallable.h"
#include "dksdk_ffi_c_test.h"
#include "dksdk_ffi_ocaml_callback.h"
#include "dksdk_ffi_ocaml_instance.h"
#include <caml/callback.h>
#include <stdlib.h>
#ifdef _WIN32
#define portable_main wmain
#define portable_char wchar_t
#else
#define portable_main main
#define portable_char char
#endif
/**
* Global FFI host array. Each FFI program must have this
* array defined with all but the first item zeroed.
*/
struct dksdk_ffi *dksdk_ffi_hosts[DKSDK_FFI_HOST_SLOTS] = {0};
/** Convenience error handling */
#define ON_ERROR(MSG) \
if (ret != DKSDK_FFI_OK) { \
fprintf(stderr, "FATAL: " #MSG ". Error code = %d\n", ret); \
exit(1); \
}
int(portable_main)(int argc, portable_char **argv) {
int ret;
char *dklogfilevar;
/* Do some logging */
dklogfilevar = getenv("DKSDK_FFI_LOG_CONF_FILE");
if (argc >= 2 && argv[1][0] != '-') {
ret = dksdk_ffi_c_log_configure(argv[1]);
ON_ERROR("dksdk_ffi_c_log_configure");
} else if (dklogfilevar != NULL) {
ret = dksdk_ffi_c_log_configure(dklogfilevar);
ON_ERROR("dksdk_ffi_c_log_configure");
}
/* Initialize FFI using the host at slot 0. Other slots are reserved. */
const int slot = 0;
ret = dksdk_ffi_init(dksdk_ffi_c_test_oid_generate_DO_NOT_USE_IN_PRODUCTION,
NULL, NULL, NULL, 32768, &dksdk_ffi_hosts[slot]);
ON_ERROR("dksdk_ffi_init");
struct dksdk_ffi *ffi = dksdk_ffi_hosts[slot];
/* Register classes */
ret = dksdk_ffi_c_cls_icallable_register(ffi);
ON_ERROR("dk_cls_ICallable_register");
ret = dksdk_ffi_ocaml_cls_callback_register(ffi);
ON_ERROR("dksdk_ffi_ocaml_cls_callback_register");
ret = dksdk_ffi_c_test_POSIX_FILE_register(ffi);
ON_ERROR("dksdk_ffi_c_test_POSIX_FILE_register");
ret = dksdk_ffi_c_test_ToyCalculations_register(ffi);
ON_ERROR("dksdk_ffi_c_test_ToyCalculations_register");
/* Run module initializers of OCaml and wait for them to finish */
caml_startup(argv);
/* Release OCaml resources */
caml_shutdown();
/* Unregister classes */
ret = dksdk_ffi_c_test_ToyCalculations_unregister(ffi);
ON_ERROR("dksdk_ffi_c_test_ToyCalculations_unregister");
ret = dksdk_ffi_c_test_POSIX_FILE_unregister(ffi);
ON_ERROR("dksdk_ffi_c_test_POSIX_FILE_unregister");
ret = dksdk_ffi_ocaml_cls_callback_unregister(ffi);
ON_ERROR("dksdk_ffi_ocaml_cls_callback_unregister");
ret = dksdk_ffi_c_cls_icallable_unregister(ffi);
ON_ERROR("dk_cls_ICallable_unregister");
/* Release DkSDK FFI resources */
ret = dksdk_ffi_terminate(ffi);
if (ret != DKSDK_FFI_OK) {
fprintf(stderr, "FATAL: dksdk_ffi_terminate() = %d\n", ret);
exit(1);
}
return 0;
}
/* This C function is called by FFI OCaml test code to initialize FFI
if it hasn't been already. */
value caml_dksdk_ffi_ocaml_init_if_needed(value v_logfile_opt) {
return Val_unit;
}
Licensing
Full source code and other platforms are available with a "DkSDK SOFTWARE DEVELOPMENT KIT LICENSE AGREEMENT" from https://diskuv.com/pricing, and is free for security engineers, educators and related-field researchers (ex. programming language theory, memory and thread modeling) on request.
The DkSDK FFI OCaml source code, documentation and build scripts are also available under the Open Software License version 3.0, https://opensource.org/license/osl-3-0-php/, at your option.