respondd: use C modules instead of Lua to provide data
This commit is contained in:
parent
bdb56bba02
commit
fd06c7d67d
|
@ -13,7 +13,7 @@ include $(INCLUDE_DIR)/cmake.mk
|
||||||
define Package/respondd
|
define Package/respondd
|
||||||
SECTION:=net
|
SECTION:=net
|
||||||
CATEGORY:=Network
|
CATEGORY:=Network
|
||||||
DEPENDS:=+liblua
|
DEPENDS:=+libjson-c
|
||||||
TITLE:=Responds to multicast queries with answers generated by Lua code
|
TITLE:=Responds to multicast queries with answers generated by Lua code
|
||||||
endef
|
endef
|
||||||
|
|
||||||
|
@ -29,4 +29,9 @@ define Package/respondd/install
|
||||||
$(INSTALL_BIN) $(PKG_INSTALL_DIR)/usr/bin/respondd $(1)/usr/bin/
|
$(INSTALL_BIN) $(PKG_INSTALL_DIR)/usr/bin/respondd $(1)/usr/bin/
|
||||||
endef
|
endef
|
||||||
|
|
||||||
|
define Build/InstallDev
|
||||||
|
$(INSTALL_DIR) $(1)/usr/include
|
||||||
|
$(INSTALL_DATA) $(PKG_BUILD_DIR)/respondd.h $(1)/usr/include/
|
||||||
|
endef
|
||||||
|
|
||||||
$(eval $(call BuildPackage,respondd))
|
$(eval $(call BuildPackage,respondd))
|
||||||
|
|
|
@ -0,0 +1,37 @@
|
||||||
|
respondd providers are C modules (shared objects). These modules should include
|
||||||
|
<json-c/json.h> and <respondd.h>, the latter of which provides the following definitions:
|
||||||
|
|
||||||
|
typedef struct json_object * (*respondd_provider)(void);
|
||||||
|
|
||||||
|
struct respondd_provider_info {
|
||||||
|
const char *request;
|
||||||
|
const respondd_provider provider;
|
||||||
|
};
|
||||||
|
|
||||||
|
extern const struct respondd_provider_info respondd_providers[];
|
||||||
|
|
||||||
|
|
||||||
|
The module must define the array `respondd_providers`, e.g. like this:
|
||||||
|
|
||||||
|
static struct json_object * respondd_provider_nodeinfo(void) {
|
||||||
|
...
|
||||||
|
}
|
||||||
|
|
||||||
|
static struct json_object * respondd_provider_statistics(void) {
|
||||||
|
...
|
||||||
|
}
|
||||||
|
|
||||||
|
const struct respondd_provider_info respondd_providers[] = {
|
||||||
|
{"nodeinfo", respondd_provider_nodeinfo},
|
||||||
|
{"statistics", respondd_provider_statistics},
|
||||||
|
{}
|
||||||
|
};
|
||||||
|
|
||||||
|
The array must be terminated with an empty entry. The provider for each
|
||||||
|
request type must return a [JSON-C] JSON object. This JSON object must have exactly 1
|
||||||
|
reference, and all other memory must be freed by the provider before returning.
|
||||||
|
|
||||||
|
The JSON objects returned by different provider modules for the same request type
|
||||||
|
are merged.
|
||||||
|
|
||||||
|
[JSON-C]: https://github.com/json-c/json-c/wiki
|
|
@ -1,14 +1,19 @@
|
||||||
cmake_minimum_required(VERSION 2.6)
|
cmake_minimum_required(VERSION 2.6)
|
||||||
|
|
||||||
|
set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR})
|
||||||
|
|
||||||
project(respondd C)
|
project(respondd C)
|
||||||
|
|
||||||
find_package(Lua51 REQUIRED)
|
find_package(JSON_C REQUIRED)
|
||||||
|
|
||||||
set_property(DIRECTORY PROPERTY COMPILE_DEFINITIONS _GNU_SOURCE)
|
set_property(DIRECTORY PROPERTY COMPILE_DEFINITIONS _GNU_SOURCE)
|
||||||
include_directories(${LUA_INCLUDE_DIR})
|
|
||||||
|
|
||||||
add_executable(respondd respondd.c)
|
add_executable(respondd respondd.c)
|
||||||
set_property(TARGET respondd PROPERTY COMPILE_FLAGS "-Wall -std=c99")
|
set_property(TARGET respondd PROPERTY COMPILE_FLAGS "-Wall -std=c99 -fno-strict-aliasing ${JSON_C_CFLAGS_OTHER}")
|
||||||
target_link_libraries(respondd ${LUA_LIBRARIES} dl)
|
set_property(TARGET respondd PROPERTY LINK_FLAGS "${JSON_C_LDFLAGS_OTHER}")
|
||||||
|
set_property(TARGET respondd APPEND PROPERTY INCLUDE_DIRECTORIES ${JSON_C_INCLUDE_DIR})
|
||||||
|
target_link_libraries(respondd ${JSON_C_LIBRARIES} dl)
|
||||||
|
|
||||||
install(TARGETS respondd RUNTIME DESTINATION bin)
|
install(TARGETS respondd RUNTIME DESTINATION bin)
|
||||||
|
|
||||||
|
install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/respondd.h DESTINATION include)
|
||||||
|
|
|
@ -0,0 +1,20 @@
|
||||||
|
# Defines the following variables:
|
||||||
|
# JSON_C_FOUND
|
||||||
|
# JSON_C_INCLUDE_DIR
|
||||||
|
# JSON_C_LIBRARIES
|
||||||
|
# JSON_C_CFLAGS_OTHER
|
||||||
|
# JSON_C_LDFLAGS_OTHER
|
||||||
|
|
||||||
|
|
||||||
|
find_package(PkgConfig REQUIRED QUIET)
|
||||||
|
|
||||||
|
pkg_check_modules(_JSON_C json-c)
|
||||||
|
|
||||||
|
find_path(JSON_C_INCLUDE_DIR NAMES json-c/json.h HINTS ${_JSON_C_INCLUDE_DIRS})
|
||||||
|
find_library(JSON_C_LIBRARIES NAMES json-c HINTS ${_JSON_C_LIBRARY_DIRS})
|
||||||
|
|
||||||
|
set(JSON_C_CFLAGS_OTHER "${_JSON_C_CFLAGS_OTHER}" CACHE STRING "Additional compiler flags for json-c")
|
||||||
|
set(JSON_C_LDFLAGS_OTHER "${_JSON_C_LDFLAGS_OTHER}" CACHE STRING "Additional linker flags for json-c")
|
||||||
|
|
||||||
|
find_package_handle_standard_args(JSON_C REQUIRED_VARS JSON_C_LIBRARIES JSON_C_INCLUDE_DIR)
|
||||||
|
mark_as_advanced(JSON_C_INCLUDE_DIR JSON_C_LIBRARIES JSON_C_CFLAGS_OTHER JSON_C_LDFLAGS_OTHER)
|
File diff suppressed because it is too large
Load Diff
|
@ -1,6 +1,6 @@
|
||||||
/*
|
/*
|
||||||
Copyright (c) 2014-2015, Nils Schneider <nils@nilsschneider.net>
|
Copyright (c) 2014-2015, Nils Schneider <nils@nilsschneider.net>
|
||||||
Copyright (c) 2015, Matthias Schiffer <mschiffer@universe-factory.net>
|
Copyright (c) 2015-2016, Matthias Schiffer <mschiffer@universe-factory.net>
|
||||||
All rights reserved.
|
All rights reserved.
|
||||||
|
|
||||||
Redistribution and use in source and binary forms, with or without
|
Redistribution and use in source and binary forms, with or without
|
||||||
|
@ -24,10 +24,18 @@
|
||||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <lua.h>
|
#include "respondd.h"
|
||||||
#include <lauxlib.h>
|
|
||||||
#include <lualib.h>
|
|
||||||
|
|
||||||
|
#include "miniz.c"
|
||||||
|
|
||||||
|
#include <json-c/json.h>
|
||||||
|
|
||||||
|
#include <alloca.h>
|
||||||
|
#include <dirent.h>
|
||||||
|
#include <dlfcn.h>
|
||||||
|
#include <inttypes.h>
|
||||||
|
#include <search.h>
|
||||||
|
#include <stdbool.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
@ -41,150 +49,395 @@
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
|
|
||||||
|
|
||||||
|
struct provider_list {
|
||||||
|
struct provider_list *next;
|
||||||
|
|
||||||
|
char *name;
|
||||||
|
respondd_provider provider;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct request_type {
|
||||||
|
struct provider_list *providers;
|
||||||
|
|
||||||
|
struct json_object *cache;
|
||||||
|
uint64_t cache_time;
|
||||||
|
int64_t cache_timeout;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
static int64_t now;
|
||||||
|
static struct hsearch_data htab;
|
||||||
|
|
||||||
|
|
||||||
|
static struct json_object * merge_json(struct json_object *a, struct json_object *b);
|
||||||
|
|
||||||
|
|
||||||
static void usage() {
|
static void usage() {
|
||||||
puts("Usage: respondd [-h] -g <group> -p <port> -i <if0> [-i <if1> ..]");
|
puts("Usage:");
|
||||||
puts(" -g <ip6> multicast group, e.g. ff02::2:1001");
|
puts(" respondd -h");
|
||||||
puts(" -p <int> port number to listen on");
|
puts(" respondd [-p <port>] [-g <group> -i <if0> [-i <if1> ..]] [-d <dir>]");
|
||||||
puts(" -i <string> interface on which the group is joined");
|
puts(" -p <int> port number to listen on");
|
||||||
puts(" -c <string> Lua command");
|
puts(" -g <ip6> multicast group, e.g. ff02::2:1001");
|
||||||
puts(" -h this help\n");
|
puts(" -i <string> interface on which the group is joined");
|
||||||
|
puts(" -d <string> data provider directory (default: current directory)");
|
||||||
|
puts(" -h this help\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
static void join_mcast(const int sock, const struct in6_addr addr, const char *iface) {
|
static void join_mcast(const int sock, const struct in6_addr addr, const char *iface) {
|
||||||
struct ipv6_mreq mreq;
|
struct ipv6_mreq mreq;
|
||||||
|
|
||||||
mreq.ipv6mr_multiaddr = addr;
|
mreq.ipv6mr_multiaddr = addr;
|
||||||
mreq.ipv6mr_interface = if_nametoindex(iface);
|
mreq.ipv6mr_interface = if_nametoindex(iface);
|
||||||
|
|
||||||
if (mreq.ipv6mr_interface == 0)
|
if (mreq.ipv6mr_interface == 0)
|
||||||
goto error;
|
goto error;
|
||||||
|
|
||||||
if (setsockopt(sock, IPPROTO_IPV6, IPV6_JOIN_GROUP, &mreq, sizeof(mreq)) == -1)
|
if (setsockopt(sock, IPPROTO_IPV6, IPV6_JOIN_GROUP, &mreq, sizeof(mreq)) == -1)
|
||||||
goto error;
|
goto error;
|
||||||
|
|
||||||
return;
|
return;
|
||||||
|
|
||||||
error:
|
error:
|
||||||
fprintf(stderr, "Could not join multicast group on %s: ", iface);
|
fprintf(stderr, "Could not join multicast group on %s: ", iface);
|
||||||
perror(NULL);
|
perror(NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void serve(lua_State *L, int sock) {
|
|
||||||
char buffer[256];
|
|
||||||
ssize_t read_bytes;
|
|
||||||
struct timespec tp;
|
|
||||||
|
|
||||||
lua_pushvalue(L, -1);
|
static void update_time(void) {
|
||||||
|
struct timespec tp;
|
||||||
|
clock_gettime(CLOCK_MONOTONIC, &tp);
|
||||||
|
|
||||||
struct sockaddr_in6 addr;
|
now = (int64_t)tp.tv_sec * 1000 + tp.tv_nsec / 1000000;
|
||||||
socklen_t addrlen = sizeof(addr);
|
|
||||||
|
|
||||||
read_bytes = recvfrom(sock, buffer, sizeof(buffer), 0, (struct sockaddr *)&addr, &addrlen);
|
|
||||||
|
|
||||||
if (read_bytes < 0) {
|
|
||||||
perror("recvfrom failed");
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
clock_gettime(CLOCK_MONOTONIC, &tp);
|
|
||||||
|
|
||||||
lua_pushlstring(L, buffer, read_bytes);
|
|
||||||
lua_pushnumber(L, tp.tv_sec + 1.0e-9*tp.tv_nsec);
|
|
||||||
|
|
||||||
if (lua_pcall(L, 2, 1, 0)) {
|
|
||||||
fprintf(stderr, "%s\n", lua_tostring(L, -1));
|
|
||||||
}
|
|
||||||
else if (lua_isstring(L, -1)) {
|
|
||||||
size_t msg_length;
|
|
||||||
const char *msg = lua_tolstring(L, -1, &msg_length);
|
|
||||||
|
|
||||||
if (sendto(sock, msg, msg_length, 0, (struct sockaddr *)&addr, addrlen) < 0)
|
|
||||||
perror("sendto failed");
|
|
||||||
}
|
|
||||||
|
|
||||||
lua_pop(L, 1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Merges two JSON objects
|
||||||
|
*
|
||||||
|
* On conflicts, object a will be preferred.
|
||||||
|
*
|
||||||
|
* Internally, this functions merges all entries from object a into object b,
|
||||||
|
* so merging a small object a with a big object b is faster than vice-versa.
|
||||||
|
*/
|
||||||
|
static struct json_object * merge_json(struct json_object *a, struct json_object *b) {
|
||||||
|
if (!json_object_is_type(a, json_type_object) || !json_object_is_type(b, json_type_object)) {
|
||||||
|
json_object_put(b);
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
|
||||||
|
json_object_object_foreach(a, key, val_a) {
|
||||||
|
struct json_object *val_b;
|
||||||
|
|
||||||
|
json_object_get(val_a);
|
||||||
|
|
||||||
|
if (!json_object_object_get_ex(b, key, &val_b)) {
|
||||||
|
json_object_object_add(b, key, val_a);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
json_object_get(val_b);
|
||||||
|
|
||||||
|
json_object_object_add(b, key, merge_json(val_a, val_b));
|
||||||
|
}
|
||||||
|
|
||||||
|
json_object_put(a);
|
||||||
|
return b;
|
||||||
|
}
|
||||||
|
|
||||||
|
static const struct respondd_provider_info * get_providers(const char *filename) {
|
||||||
|
/*
|
||||||
|
Prefix the filename with "./" to open the module in the current directory
|
||||||
|
(dlopen looks in the standard library paths by default)
|
||||||
|
*/
|
||||||
|
char path[2 + strlen(filename) + 1];
|
||||||
|
snprintf(path, sizeof(path), "./%s", filename);
|
||||||
|
|
||||||
|
void *handle = dlopen(path, RTLD_NOW|RTLD_LOCAL);
|
||||||
|
if (!handle)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
const struct respondd_provider_info *ret = dlsym(handle, "respondd_providers");
|
||||||
|
if (!ret) {
|
||||||
|
dlclose(handle);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void load_cache_time(struct request_type *r, const char *name) {
|
||||||
|
r->cache = NULL;
|
||||||
|
r->cache_time = 0;
|
||||||
|
r->cache_timeout = now;
|
||||||
|
|
||||||
|
char filename[strlen(name) + 7];
|
||||||
|
snprintf(filename, sizeof(filename), "%s.cache", name);
|
||||||
|
|
||||||
|
FILE *f = fopen(filename, "r");
|
||||||
|
if (!f)
|
||||||
|
return;
|
||||||
|
|
||||||
|
fscanf(f, "%"SCNu64, &r->cache_time);
|
||||||
|
fclose(f);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
static void add_provider(const char *name, const struct respondd_provider_info *provider) {
|
||||||
|
ENTRY key = {
|
||||||
|
.key = (char *)provider->request,
|
||||||
|
.data = NULL,
|
||||||
|
};
|
||||||
|
ENTRY *entry;
|
||||||
|
if (!hsearch_r(key, FIND, &entry, &htab)) {
|
||||||
|
struct request_type *r = malloc(sizeof(*r));
|
||||||
|
r->providers = NULL;
|
||||||
|
load_cache_time(r, provider->request);
|
||||||
|
|
||||||
|
key.data = r;
|
||||||
|
if (!hsearch_r(key, ENTER, &entry, &htab)) {
|
||||||
|
perror("hsearch_r");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct request_type *r = entry->data;
|
||||||
|
|
||||||
|
struct provider_list *pentry = malloc(sizeof(*pentry));
|
||||||
|
pentry->name = strdup(name);
|
||||||
|
pentry->provider = provider->provider;
|
||||||
|
|
||||||
|
struct provider_list **pos;
|
||||||
|
for (pos = &r->providers; *pos; pos = &(*pos)->next) {
|
||||||
|
if (strcmp(pentry->name, (*pos)->name) < 0)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
pentry->next = *pos;
|
||||||
|
*pos = pentry;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void load_providers(void) {
|
||||||
|
update_time();
|
||||||
|
|
||||||
|
/* Maximum number of request types, might be made configurable in the future */
|
||||||
|
if (!hcreate_r(32, &htab)) {
|
||||||
|
perror("hcreate_r");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
DIR *dir = opendir(".");
|
||||||
|
if (!dir) {
|
||||||
|
perror("opendir");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
struct dirent *ent;
|
||||||
|
while ((ent = readdir(dir)) != NULL) {
|
||||||
|
if (ent->d_name[0] == '.')
|
||||||
|
continue;
|
||||||
|
|
||||||
|
size_t len = strlen(ent->d_name);
|
||||||
|
|
||||||
|
if (len < 4)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if (strcmp(&ent->d_name[len-3], ".so"))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
const struct respondd_provider_info *providers = get_providers(ent->d_name);
|
||||||
|
if (!providers)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
for (; providers->request; providers++)
|
||||||
|
add_provider(ent->d_name, providers);
|
||||||
|
}
|
||||||
|
|
||||||
|
closedir(dir);
|
||||||
|
}
|
||||||
|
|
||||||
|
static struct json_object * eval_providers(struct provider_list *providers) {
|
||||||
|
struct json_object *ret = json_object_new_object();
|
||||||
|
|
||||||
|
for (; providers; providers = providers->next)
|
||||||
|
ret = merge_json(providers->provider(), ret);
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
static struct json_object * single_request(char *type) {
|
||||||
|
ENTRY key = {
|
||||||
|
.key = type,
|
||||||
|
.data = NULL,
|
||||||
|
};
|
||||||
|
ENTRY *entry;
|
||||||
|
if (!hsearch_r(key, FIND, &entry, &htab))
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
struct request_type *r = entry->data;
|
||||||
|
|
||||||
|
if (r->cache_time && now < r->cache_timeout)
|
||||||
|
return json_object_get(r->cache);
|
||||||
|
|
||||||
|
struct json_object *ret = eval_providers(r->providers);
|
||||||
|
|
||||||
|
if (r->cache_time) {
|
||||||
|
if (r->cache)
|
||||||
|
json_object_put(r->cache);
|
||||||
|
|
||||||
|
r->cache = json_object_get(ret);
|
||||||
|
r->cache_timeout = now + r->cache_time;
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
static struct json_object * multi_request(char *types) {
|
||||||
|
struct json_object *ret = json_object_new_object();
|
||||||
|
char *type, *saveptr;
|
||||||
|
|
||||||
|
for (type = strtok_r(types, " ", &saveptr); type; type = strtok_r(NULL, " ", &saveptr)) {
|
||||||
|
struct json_object *sub = single_request(type);
|
||||||
|
if (sub)
|
||||||
|
json_object_object_add(ret, type, sub);
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
static struct json_object * handle_request(char *request, bool *compress) {
|
||||||
|
if (!*request)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
update_time();
|
||||||
|
|
||||||
|
if (!strncmp(request, "GET ", 4)) {
|
||||||
|
*compress = true;
|
||||||
|
return multi_request(request+4);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
*compress = false;
|
||||||
|
return single_request(request);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void serve(int sock) {
|
||||||
|
char input[256];
|
||||||
|
const char *output = NULL;
|
||||||
|
ssize_t input_bytes, output_bytes;
|
||||||
|
struct sockaddr_in6 addr;
|
||||||
|
socklen_t addrlen = sizeof(addr);
|
||||||
|
bool compress;
|
||||||
|
|
||||||
|
input_bytes = recvfrom(sock, input, sizeof(input)-1, 0, (struct sockaddr *)&addr, &addrlen);
|
||||||
|
|
||||||
|
if (input_bytes < 0) {
|
||||||
|
perror("recvfrom failed");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
input[input_bytes] = 0;
|
||||||
|
|
||||||
|
struct json_object *result = handle_request(input, &compress);
|
||||||
|
if (!result)
|
||||||
|
return;
|
||||||
|
|
||||||
|
const char *str = json_object_to_json_string_ext(result, JSON_C_TO_STRING_PLAIN);
|
||||||
|
|
||||||
|
if (compress) {
|
||||||
|
size_t str_bytes = strlen(str);
|
||||||
|
|
||||||
|
mz_ulong compressed_bytes = mz_compressBound(str_bytes);
|
||||||
|
unsigned char *compressed = alloca(compressed_bytes);
|
||||||
|
|
||||||
|
if (!mz_compress(compressed, &compressed_bytes, (const unsigned char *)str, str_bytes)) {
|
||||||
|
output = (const char*)compressed;
|
||||||
|
output_bytes = compressed_bytes;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
output = str;
|
||||||
|
output_bytes = strlen(str);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (output) {
|
||||||
|
if (sendto(sock, output, output_bytes, 0, (struct sockaddr *)&addr, addrlen) < 0)
|
||||||
|
perror("sendto failed");
|
||||||
|
}
|
||||||
|
|
||||||
|
json_object_put(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
int sock;
|
int sock;
|
||||||
struct sockaddr_in6 server_addr = {};
|
struct sockaddr_in6 server_addr = {};
|
||||||
struct in6_addr mgroup_addr;
|
struct in6_addr mgroup_addr;
|
||||||
const char *command = NULL;
|
|
||||||
|
|
||||||
sock = socket(PF_INET6, SOCK_DGRAM, 0);
|
sock = socket(PF_INET6, SOCK_DGRAM, 0);
|
||||||
|
|
||||||
if (sock < 0) {
|
if (sock < 0) {
|
||||||
perror("creating socket");
|
perror("creating socket");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
server_addr.sin6_family = AF_INET6;
|
server_addr.sin6_family = AF_INET6;
|
||||||
server_addr.sin6_addr = in6addr_any;
|
server_addr.sin6_addr = in6addr_any;
|
||||||
|
|
||||||
opterr = 0;
|
opterr = 0;
|
||||||
|
|
||||||
int group_set = 0;
|
int group_set = 0;
|
||||||
|
|
||||||
int c;
|
int c;
|
||||||
while ((c = getopt(argc, argv, "p:g:i:c:h")) != -1) {
|
while ((c = getopt(argc, argv, "p:g:i:d:h")) != -1) {
|
||||||
switch (c) {
|
switch (c) {
|
||||||
case 'p':
|
case 'p':
|
||||||
server_addr.sin6_port = htons(atoi(optarg));
|
server_addr.sin6_port = htons(atoi(optarg));
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'g':
|
case 'g':
|
||||||
if (!inet_pton(AF_INET6, optarg, &mgroup_addr)) {
|
if (!inet_pton(AF_INET6, optarg, &mgroup_addr)) {
|
||||||
perror("Invalid multicast group. This message will probably confuse you");
|
perror("Invalid multicast group. This message will probably confuse you");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
group_set = 1;
|
group_set = 1;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'i':
|
case 'i':
|
||||||
if (!group_set) {
|
if (!group_set) {
|
||||||
fprintf(stderr, "Multicast group must be given before interface.\n");
|
fprintf(stderr, "Multicast group must be given before interface.\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
join_mcast(sock, mgroup_addr, optarg);
|
join_mcast(sock, mgroup_addr, optarg);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'c':
|
case 'd':
|
||||||
command = optarg;
|
if (chdir(optarg)) {
|
||||||
break;
|
perror("Unable to change to given directory");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
case 'h':
|
case 'h':
|
||||||
usage();
|
usage();
|
||||||
exit(EXIT_SUCCESS);
|
exit(EXIT_SUCCESS);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
fprintf(stderr, "Invalid parameter %c ignored.\n", c);
|
fprintf(stderr, "Invalid parameter -%c ignored.\n", optopt);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!command) {
|
if (bind(sock, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
|
||||||
fprintf(stderr, "No command given.\n");
|
perror("bind failed");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (bind(sock, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
|
load_providers();
|
||||||
perror("bind failed");
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
lua_State *L = luaL_newstate();
|
while (true)
|
||||||
luaL_openlibs(L);
|
serve(sock);
|
||||||
|
|
||||||
if (luaL_loadstring(L, command)) {
|
return EXIT_FAILURE;
|
||||||
perror("Unable to load Lua command");
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
lua_call(L, 0, 1);
|
|
||||||
|
|
||||||
while (1)
|
|
||||||
serve(L, sock);
|
|
||||||
|
|
||||||
return EXIT_FAILURE;
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,39 @@
|
||||||
|
/*
|
||||||
|
Copyright (c) 2016, Matthias Schiffer <mschiffer@universe-factory.net>
|
||||||
|
All rights reserved.
|
||||||
|
|
||||||
|
Redistribution and use in source and binary forms, with or without
|
||||||
|
modification, are permitted provided that the following conditions are met:
|
||||||
|
|
||||||
|
1. Redistributions of source code must retain the above copyright notice,
|
||||||
|
this list of conditions and the following disclaimer.
|
||||||
|
2. Redistributions in binary form must reproduce the above copyright notice,
|
||||||
|
this list of conditions and the following disclaimer in the documentation
|
||||||
|
and/or other materials provided with the distribution.
|
||||||
|
|
||||||
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||||
|
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||||
|
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||||
|
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||||
|
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||||
|
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||||
|
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#ifndef _RESPONDD_H_
|
||||||
|
#define _RESPONDD_H_
|
||||||
|
|
||||||
|
typedef struct json_object * (*respondd_provider)(void);
|
||||||
|
|
||||||
|
struct respondd_provider_info {
|
||||||
|
const char *request;
|
||||||
|
const respondd_provider provider;
|
||||||
|
};
|
||||||
|
|
||||||
|
extern const struct respondd_provider_info respondd_providers[];
|
||||||
|
|
||||||
|
#endif /* _RESPONDD_H_ */
|
Loading…
Reference in New Issue