Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

here is the solution #685

Open
coderknk opened this issue Feb 10, 2025 · 0 comments
Open

here is the solution #685

coderknk opened this issue Feb 10, 2025 · 0 comments

Comments

@coderknk
Copy link

`#include <openvpn/openvpn-plugin.h>
#include <stdlib.h>

#define PLUGIN_NAME "Issue Demo"

typedef struct {
struct openvpn_plugin_callbacks *callbacks;
void *client_instance;
} plugin_context_t;

OPENVPN_EXPORT int openvpn_plugin_open_v3(
int version,
const struct openvpn_plugin_args_open_in *args,
struct openvpn_plugin_args_open_return *ret)
{
plugin_context_t *context = calloc(1, sizeof(plugin_context_t));
if (!context) {
return OPENVPN_PLUGIN_FUNC_ERROR;
}

context->callbacks = args->callbacks;
ret->handle = context;
return OPENVPN_PLUGIN_FUNC_SUCCESS;

}

OPENVPN_EXPORT int openvpn_plugin_func_v3(
int version,
const struct openvpn_plugin_args_func_in *args,
struct openvpn_plugin_args_func_return *ret)
{
return OPENVPN_PLUGIN_FUNC_ERROR;
}

OPENVPN_EXPORT void openvpn_plugin_close_v1(openvpn_plugin_handle_t handle)
{
plugin_context_t *context = handle;
if (context) {
free(context);
}
}

OPENVPN_EXPORT void *openvpn_plugin_client_constructor_v1(openvpn_plugin_handle_t handle)
{
plugin_context_t *context = handle;

if (context->client_instance) {
    free(context->client_instance);
    context->client_instance = NULL;
}

void *per_client_context = calloc(1, sizeof(int));
context->client_instance = per_client_context;
return per_client_context;

}

OPENVPN_EXPORT void openvpn_plugin_client_destructor_v1(
openvpn_plugin_handle_t handle,
void *per_client_context)
{
plugin_context_t *context = handle;

free(per_client_context);
context->client_instance = NULL;

}

OPENVPN_EXPORT int openvpn_plugin_min_version_required_v1()
{
return 3;
}`

/*Plugin Context: We create a small structure (plugin_context_t) to keep track of the plugin's callbacks and the current client instance. This helps us manage things better.

Plugin Open: When the plugin starts, we allocate memory for the context and store the callbacks. This is like setting up the basics for the plugin to work.

Plugin Func: This function doesn’t do much in this example. It just returns an error. You can add your logic here if needed.

Plugin Close: When the plugin is closed, we free the memory we allocated for the context. This is like cleaning up after ourselves.

Client Constructor: When a new client is created, we first check if there’s an old client instance. If yes, we clean it up. Then we allocate memory for the new client and return it. This ensures no memory is wasted.

Client Destructor: When a client is no longer needed, we free its memory and clear the pointer. This is like saying goodbye and cleaning up.

Min Version: We tell OpenVPN that this plugin needs at least version 3 to work */

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant