summaryrefslogtreecommitdiff
path: root/src/helpers.c
blob: 3e4e6c8553b8a1d78310877f76e7071af3cc9d0a (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
/*
 * (C) 2012 by Pablo Neira Ayuso <pablo@netfilter.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation (or any later at your option).
 *
 * This code has been sponsored by Vyatta Inc. <http://www.vyatta.com>
 */

#include "helper.h"

#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dlfcn.h>

static LIST_HEAD(helper_list);

void helper_register(struct ctd_helper *helper)
{
	list_add(&helper->head, &helper_list);
}

static struct ctd_helper *
__helper_find(const char *helper_name, uint8_t l4proto)
{
	struct ctd_helper *cur, *helper = NULL;

	list_for_each_entry(cur, &helper_list, head) {
		if (strncmp(cur->name, helper_name, CTD_HELPER_NAME_LEN) != 0)
			continue;

		if (cur->l4proto != l4proto)
			continue;

		helper = cur;
		break;
	}
	return helper;
}

struct ctd_helper *
helper_find(const char *libdir_path,
	    const char *helper_name, uint8_t l4proto, int flag)
{
	char path[PATH_MAX];
	struct ctd_helper *helper;
	struct stat sb;

	helper = __helper_find(helper_name, l4proto);
	if (helper != NULL)
		return helper;

	snprintf(path, sizeof(path), "%s/ct_helper_%s.so",
		libdir_path, helper_name);

	if (stat(path, &sb) != 0) {
		if (errno == ENOENT)
			return NULL;
		fprintf(stderr, "%s: %s\n", path,
			strerror(errno));
		return NULL;
	}

	if (dlopen(path, flag) == NULL) {
		fprintf(stderr, "%s: %s\n", path, dlerror());
		return NULL;
	}

	return __helper_find(helper_name, l4proto);
}