BUILD/MEDIUM: deviceatlas: updating the addon part.

- Reflecing the changes done in addons/deviceatlas/Makefile.inc.
 Enabling the cache feature and its disabling option as well.
- Now the `dadwsch` application is part of the API's package for more
general purposes, we remove it.
- Minor and transparent to user changes into da.c's workflow, also
making more noticeable some notices with appropriate logging levels.
- Adding support for the new `deviceatlas-cache-size` config keyword,
 a no-op when the cache support is disabled.
- Adding missing compilation units and relevant api updates to
the dummy library version.
This commit is contained in:
David Carlier 2024-01-25 09:11:18 +00:00 committed by Willy Tarreau
parent f960cde786
commit 5c196f0d8d
10 changed files with 120 additions and 318 deletions

View File

@ -667,28 +667,13 @@ endif
ifneq ($(USE_DEVICEATLAS),)
# Use DEVICEATLAS_SRC and possibly DEVICEATLAS_INC and DEVICEATLAS_LIB to force path
# to DeviceAtlas headers and libraries if needed.
# to DeviceAtlas headers and libraries if needed. In this context, DEVICEATLAS_NOCACHE
# can be used to disable the cache support if needed (this also removes the necessity of having
# a C++ toolchain installed).
DEVICEATLAS_INC = $(DEVICEATLAS_SRC)
DEVICEATLAS_LIB = $(DEVICEATLAS_SRC)
ifeq ($(DEVICEATLAS_SRC),)
DEVICEATLAS_LDFLAGS += -lda
else
ifeq ($(USE_PCRE),)
ifeq ($(USE_PCRE2),)
$(error the DeviceAtlas module needs the PCRE or the PCRE2 library in order to compile)
endif
endif
ifneq ($(USE_PCRE2),)
DEVICEATLAS_CFLAGS += -DDA_REGEX_HDR=\"dac_pcre2.c\" -DDA_REGEX_TAG=2
endif
OPTIONS_OBJS += $(DEVICEATLAS_LIB)/Os/daunix.o
OPTIONS_OBJS += $(DEVICEATLAS_LIB)/dadwcom.o
OPTIONS_OBJS += $(DEVICEATLAS_LIB)/dasch.o
OPTIONS_OBJS += $(DEVICEATLAS_LIB)/json.o
OPTIONS_OBJS += $(DEVICEATLAS_LIB)/dac.o
endif
include addons/deviceatlas/Makefile.inc
OPTIONS_OBJS += addons/deviceatlas/da.o
DEVICEATLAS_CFLAGS += $(if $(DEVICEATLAS_INC),-I$(DEVICEATLAS_INC)) $(if $(DEVICEATLAS_SRC),-DDATLAS_DA_NOCACHE)
endif
# Use 51DEGREES_SRC and possibly 51DEGREES_INC and 51DEGREES_LIB to force path

View File

@ -1,48 +0,0 @@
# DEVICEATLAS_SRC : DeviceAtlas API source root path
OS := $(shell uname -s)
OBJS := dadwsch.o
CFLAGS := -g -O2
LDFLAGS :=
CURL_CONFIG := curl-config
CURLDIR := $(shell $(CURL_CONFIG) --prefix 2>/dev/null || echo /usr/local)
CURL_INC := $(CURLDIR)/include
CURL_LIB := $(CURLDIR)/lib
CURL_LDFLAGS := $(shell $(CURL_CONFIG) --libs 2>/dev/null || echo -L /usr/local/lib -lcurl)
PCRE2_CONFIG := pcre2-config
PCRE2DIR := $(shell $(PCRE2_CONFIG) --prefix 2>/dev/null || echo /usr/local)
PCRE2_INC := $(PCRE2DIR)/include
PCRE2_LIB := $(PCRE2DIR)/lib
PCRE2_LDFLAGS := $(shell $(PCRE2_CONFIG) --libs8 2>/dev/null || echo /usr/local)
ifeq ($(DEVICEATLAS_SRC),)
dadwsch: dadwsch.c
$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)
LDFLAGS += -lda
else
DEVICEATLAS_INC = $(DEVICEATLAS_SRC)
DEVICEATLAS_LIB = $(DEVICEATLAS_SRC)
CFLAGS += -DDA_REGEX_HDR=\"dac_pcre2.c\" -DDA_REGEX_TAG=2
CFLAGS += -DMOBI_CURL -DMOBI_CURLSSET -DMOBI_GZ -DMOBI_ZIP
CFLAGS += -I$(DEVICEATLAS_INC) -I$(CURL_INC) -I$(PCRE2DIR)
LDFLAGS += $(CURL_LDFLAGS) $(PCRE2_LDFLAGS) -lz -lzip -lpthread
dadwsch: dadwsch.c $(DEVICEATLAS_SRC)/dac.c $(DEVICEATLAS_SRC)/dasch.c $(DEVICEATLAS_SRC)/dadwarc.c $(DEVICEATLAS_SRC)/dadwcom.c $(DEVICEATLAS_SRC)/dadwcurl.c $(DEVICEATLAS_SRC)/json.c $(DEVICEATLAS_SRC)/Os/daunix.c
$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)
endif
ifeq ($(OS), Linux)
LDFLAGS += -lrt
endif
ifeq ($(OS), SunOS)
LDFLAGS += -lrt
endif
clean:
rm -f *.o
rm -f $(DEVICEATLAS_LIB)*.o
rm -f dadwsch

View File

@ -18,7 +18,7 @@
#include <dac.h>
#define ATLASTOKSZ PATH_MAX
#define ATLASMAPNM "/hapdeviceatlas"
#define ATLASMAPNM "/da_map_sch_data"
static struct {
void *atlasimgptr;
@ -26,6 +26,7 @@ static struct {
char *jsonpath;
char *cookiename;
size_t cookienamelen;
size_t cachesize;
int atlasfd;
da_atlas_t atlas;
da_evidence_id_t useragentid;
@ -37,6 +38,7 @@ static struct {
.jsonpath = 0,
.cookiename = 0,
.cookienamelen = 0,
.cachesize = 0,
.atlasmap = NULL,
.atlasfd = -1,
.useragentid = 0,
@ -104,6 +106,29 @@ static int da_properties_cookie(char **args, int section_type, struct proxy *cur
return 0;
}
static int da_cache_size(char **args, int section_type, struct proxy *curpx,
const struct proxy *defpx, const char *file, int line,
char **err)
{
int cachesize;
if (*(args[1]) == 0) {
memprintf(err, "deviceatlas cache size : expects an integer argument.\n");
return -1;
}
cachesize = atol(args[1]);
if (cachesize < 0 || cachesize > DA_CACHE_MAX) {
memprintf(err, "deviceatlas cache size : expects a cache size between 0 and %d, %s given.\n", DA_CACHE_MAX, args[1]);
} else {
#ifdef APINOCACHE
fprintf(stdout, "deviceatlas cache size : no-op, its support is disabled.\n");
#endif
global_deviceatlas.cachesize = (size_t)cachesize;
}
return 0;
}
static size_t da_haproxy_read(void *ctx, size_t len, char *buf)
{
return fread(buf, 1, len, ctx);
@ -168,6 +193,8 @@ static int init_deviceatlas(void)
goto out;
}
global_deviceatlas.atlas.config.cache_size = global_deviceatlas.cachesize;
if (global_deviceatlas.cookiename == 0) {
global_deviceatlas.cookiename = strdup(DA_COOKIENAME_DEFAULT);
global_deviceatlas.cookienamelen = strlen(global_deviceatlas.cookiename);
@ -222,48 +249,57 @@ static void da_haproxy_checkinst(void)
base = (char *)global_deviceatlas.atlasmap;
if (base[0] != 0) {
void *cnew;
size_t atlassz;
char atlasp[ATLASTOKSZ] = {0};
da_atlas_t inst;
da_property_decl_t extraprops[1] = {{NULL, 0}};
FILE *jsonp;
void *cnew;
da_status_t status;
size_t atlassz;
char atlasp[ATLASTOKSZ] = {0};
da_atlas_t inst;
da_property_decl_t extraprops[1] = {{NULL, 0}};
#ifdef USE_THREAD
HA_SPIN_LOCK(OTHER_LOCK, &dadwsch_lock);
HA_SPIN_LOCK(OTHER_LOCK, &dadwsch_lock);
#endif
strlcpy2(atlasp, base, sizeof(atlasp));
if (da_atlas_read_mapped(atlasp, NULL, &cnew, &atlassz) == DA_OK) {
if (da_atlas_open(&inst, extraprops, cnew, atlassz) == DA_OK) {
char jsonbuf[26];
time_t jsond;
strlcpy2(atlasp, base + sizeof(char), sizeof(atlasp));
jsonp = fopen(atlasp, "r");
if (jsonp == 0) {
ha_alert("deviceatlas : '%s' json file has invalid path or is not readable.\n",
atlasp);
#ifdef USE_THREAD
HA_SPIN_UNLOCK(OTHER_LOCK, &dadwsch_lock);
#endif
return;
}
da_atlas_close(&global_deviceatlas.atlas);
free(global_deviceatlas.atlasimgptr);
global_deviceatlas.atlasimgptr = cnew;
global_deviceatlas.atlas = inst;
memset(base, 0, ATLASTOKSZ);
jsond = da_getdatacreation(&global_deviceatlas.atlas);
ctime_r(&jsond, jsonbuf);
jsonbuf[24] = 0;
printf("deviceatlas: new instance, data file date `%s`.\n", jsonbuf);
} else {
ha_warning("deviceatlas: instance update failed.\n");
memset(base, 0, ATLASTOKSZ);
free(cnew);
}
}
status = da_atlas_compile(jsonp, da_haproxy_read, da_haproxy_seek,
&cnew, &atlassz);
fclose(jsonp);
if (status == DA_OK) {
if (da_atlas_open(&inst, extraprops, cnew, atlassz) == DA_OK) {
da_atlas_close(&global_deviceatlas.atlas);
free(global_deviceatlas.atlasimgptr);
global_deviceatlas.atlasimgptr = cnew;
global_deviceatlas.atlas = inst;
base[0] = 0;
ha_notice("deviceatlas : new instance, data file date `%s`.\n",
da_getdatacreationiso8601(&global_deviceatlas.atlas));
} else {
ha_alert("deviceatlas : instance update failed.\n");
free(cnew);
}
}
#ifdef USE_THREAD
HA_SPIN_UNLOCK(OTHER_LOCK, &dadwsch_lock);
HA_SPIN_UNLOCK(OTHER_LOCK, &dadwsch_lock);
#endif
}
}
}
}
}
static int da_haproxy(const struct arg *args, struct sample *smp, da_deviceinfo_t *devinfo)
{
struct buffer *tmp;
da_propid_t prop, *pprop;
da_status_t status;
da_type_t proptype;
struct buffer *tmp;
da_propid_t prop, *pprop;
da_status_t status;
da_type_t proptype;
const char *propname;
int i;
@ -463,6 +499,7 @@ static struct cfg_kw_list dacfg_kws = {{ }, {
{ CFG_GLOBAL, "deviceatlas-log-level", da_log_level },
{ CFG_GLOBAL, "deviceatlas-property-separator", da_property_separator },
{ CFG_GLOBAL, "deviceatlas-properties-cookie", da_properties_cookie },
{ CFG_GLOBAL, "deviceatlas-cache-size", da_cache_size },
{ 0, NULL, NULL },
}};
@ -486,10 +523,10 @@ static void da_haproxy_register_build_options()
{
char *ptr = NULL;
#ifdef MOBI_DA_DUMMY_LIBRARY
#ifdef DATLAS_DA_DUMMY_LIBRARY
memprintf(&ptr, "Built with DeviceAtlas support (dummy library only).");
#else
memprintf(&ptr, "Built with DeviceAtlas support (library version %u.%u).", MOBI_DA_MAJOR, MOBI_DA_MINOR);
memprintf(&ptr, "Built with DeviceAtlas support (library version %u.%u).", DATLAS_DA_MAJOR, DATLAS_DA_MINOR);
#endif
hap_register_build_opts(ptr, 1);
}

View File

@ -1,195 +0,0 @@
#define _GNU_SOURCE
#include <dac.h>
#include <dadwcurl.h>
#include <dadwarc.h>
#include <getopt.h>
#include <stdlib.h>
#include <signal.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/mman.h>
#define ATLASTOKSZ PATH_MAX
#define ATLASMAPNM "/hapdeviceatlas"
const char *__pgname;
static struct {
da_dwatlas_t o;
int ofd;
void* atlasmap;
} global_deviceatlassch = {
.ofd = -1,
.atlasmap = NULL
};
void usage(void)
{
fprintf(stderr, "%s -u download URL [-d hour (in H:M:S format) current hour by default] [-p path for the downloaded file, /tmp by default]\n", __pgname);
exit(EXIT_FAILURE);
}
static size_t jsonread(void *ctx, size_t count, char *buf)
{
return fread(buf, 1, count, ctx);
}
static da_status_t jsonseek(void *ctx, off_t pos)
{
return fseek(ctx, pos, SEEK_SET) != -1 ? DA_OK : DA_SYS;
}
static void dadwlog(dw_config_t cfg, const char* msg)
{
time_t now = time(NULL);
char buf[26] = {0};
ctime_r(&now, buf);
buf[24] = 0;
fprintf(stderr, "%s: %s\n", buf, msg);
}
static dw_status_t dadwnot(void *a, dw_config_t *cfg)
{
da_dwatlas_t *o = (da_dwatlas_t *)a;
if (!o)
return DW_ERR;
char *e;
char jsondbuf[26] = {0}, buf[26] = {0}, atlasp[ATLASTOKSZ] = {0};
time_t now = time(NULL);
time_t jsond;
int fd = -1;
(void)a;
jsond = da_getdatacreation(&o->atlas);
dwgetfinalp(o->dcfg.info, atlasp, sizeof(atlasp));
ctime_r(&jsond, jsondbuf);
ctime_r(&now, buf);
jsondbuf[24] = 0;
buf[24] = 0;
printf("%s: data file generated on `%s`\n", buf, jsondbuf);
int val = 1;
unsigned char *ptr = (unsigned char *)global_deviceatlassch.atlasmap;
memset(ptr, 0, sizeof(atlasp));
strcpy(ptr, atlasp);
return DW_OK;
}
static da_status_t dadwinit(void)
{
if ((global_deviceatlassch.ofd = shm_open(ATLASMAPNM, O_RDWR | O_CREAT, 0660)) == -1) {
fprintf(stderr, "%s\n", strerror(errno));
return DA_SYS;
}
if (ftruncate(global_deviceatlassch.ofd, ATLASTOKSZ) == -1) {
close(global_deviceatlassch.ofd);
return DA_SYS;
}
lseek(global_deviceatlassch.ofd, 0, SEEK_SET);
global_deviceatlassch.atlasmap = mmap(0, ATLASTOKSZ, PROT_READ | PROT_WRITE, MAP_SHARED, global_deviceatlassch.ofd, 0);
if (global_deviceatlassch.atlasmap == MAP_FAILED) {
fprintf(stderr, "%s\n", strerror(errno));
return DA_SYS;
} else {
memset(global_deviceatlassch.atlasmap, 0, ATLASTOKSZ);
return DA_OK;
}
}
static void dadwexit(int sig __attribute__((unused)), siginfo_t *s __attribute__((unused)), void *ctx __attribute__((unused)))
{
ssize_t w;
fprintf(stderr, "%s: exit\n", __pgname);
dw_daatlas_close(&global_deviceatlassch.o);
da_fini();
munmap(global_deviceatlassch.atlasmap, ATLASTOKSZ);
close(global_deviceatlassch.ofd);
shm_unlink(ATLASMAPNM);
exit(EXIT_SUCCESS);
}
int main(int argc, char **argv)
{
const char *opts = "u:p:d:h";
bool dset = false;
size_t i;
int ch;
da_property_decl_t extraprops[1] = {
{ 0, 0 }
};
__pgname = argv[0];
dw_df_dainit_fn = curldwinit;
dw_df_dacleanup_fn = curldwcleanup;
da_init();
memset(&global_deviceatlassch.o.dcfg, 0, sizeof(global_deviceatlassch.o.dcfg));
while ((ch = getopt(argc, argv, opts)) != -1) {
switch (ch) {
case 'u':
global_deviceatlassch.o.dcfg.info.url = strdup(optarg);
break;
case 'p':
global_deviceatlassch.o.dcfg.info.path = strdup(optarg);
break;
case 'd':
if (strptime(optarg, "%H:%M:%S", &global_deviceatlassch.o.dcfg.info.rtm) != NULL)
dset = true;
else
usage();
break;
case 'h':
default:
usage();
}
}
if (!dset) {
time_t now = time(NULL);
struct tm *cnow = gmtime(&now);
memcpy(&global_deviceatlassch.o.dcfg.info.rtm, cnow, offsetof(struct tm, tm_mday));
}
if (!global_deviceatlassch.o.dcfg.info.url)
usage();
struct sigaction sa;
memset(&sa, 0, sizeof(sa));
sa.sa_flags = SA_SIGINFO | SA_RESTART;
sa.sa_sigaction = dadwexit;
global_deviceatlassch.o.dcfg.info.datatm = 1;
global_deviceatlassch.o.dcfg.info.chksum = 1;
global_deviceatlassch.o.dcfg.info.reload = 1;
global_deviceatlassch.o.dcfg.info.tobin = 1;
global_deviceatlassch.o.dcfg.ep = extraprops;
global_deviceatlassch.o.dcfg.dwproc = curldwproc;
global_deviceatlassch.o.dcfg.dwextract = dadwextract;
global_deviceatlassch.o.dcfg.lptr = (void *)stderr;
global_deviceatlassch.o.dcfg.dwlog = &dadwlog;
global_deviceatlassch.o.dcfg.dwnotify_n = &dadwnot;
global_deviceatlassch.o.rfn = jsonread;
global_deviceatlassch.o.posfn = jsonseek;
if (dadwinit() != DA_OK) {
fprintf(stderr, "%s init failed\n", __pgname);
exit(EXIT_FAILURE);
}
if (da_atlas_open_schedule(&global_deviceatlassch.o) != DA_OK) {
fprintf(stderr, "%s scheduling failed\n", __pgname);
exit(EXIT_FAILURE);
}
sigaction(SIGINT, &sa, NULL);
sigaction(SIGQUIT, &sa, NULL);
sigaction(SIGTERM, &sa, NULL);
while (true) sleep(1);
return 0;
}

View File

@ -1,7 +1,7 @@
# makefile for dummy DeviceAtlas library
#
# To enable the DeviceAtlas module support, the following are needed
# make TARGET=<target> DEVICEATLAS_SRC=addons/deviceatlas/dummy USE_PCRE=1 USE_DEVICEATLAS=1
# make TARGET=<target> DEVICEATLAS_SRC=addons/deviceatlas/dummy USE_DEVICEATLAS=1
build: libda.a

View File

@ -37,21 +37,21 @@ da_typename(da_type_t fieldtype)
}
char *
da_getdataversion(da_atlas_t *atlas)
da_getdataversion(const da_atlas_t *atlas)
{
return "dummy library version 1.0";
}
time_t
da_getdatacreation(da_atlas_t *atlas)
da_getdatacreation(const da_atlas_t *atlas)
{
return time(NULL);
}
int
da_getdatarevision(da_atlas_t *atlas)
char *
da_getdatacreationiso8601(const da_atlas_t *atlas)
{
return 1;
return "20000123T012345.678+0900";
}
da_status_t
@ -118,11 +118,6 @@ da_atlas_getpropcount(const da_atlas_t *atlas)
return 1;
}
void
da_atlas_setconfig(da_atlas_t *atlas, da_config_t *config)
{
}
da_status_t
da_searchv(const da_atlas_t *atlas, da_deviceinfo_t *result, da_evidence_t *evidence, size_t count)
{

View File

@ -26,9 +26,8 @@ typedef int _Bool;
#endif
#endif
#define MOBI_DA_MAJOR 2
#define MOBI_DA_MINOR 1
#define MOBI_DA_DUMMY_LIBRARY 1
#define DATLAS_DA_MAJOR 3
#define DATLAS_DA_DUMMY_LIBRARY 1
/**
@ -134,6 +133,7 @@ typedef void (*da_errorfunc_t)(da_severity_t severity, da_status_t status, const
/* Manifest constants. */
enum {
DA_CACHE_MAX = 50000,
/*
* used as the initial guess for the compiled size of an atlas.
* If atlas sizes grow more beyond this, it can be expanded to avoid multiple scans of the data.
@ -142,9 +142,8 @@ enum {
};
struct da_config {
unsigned int ua_props;
unsigned int lang_props;
unsigned int __reserved[14]; /* enough reserved keywords for future use */
unsigned int cache_size;
unsigned int __reserved[15]; /* enough reserved keywords for future use */
};
/**
@ -451,21 +450,22 @@ const char *da_typename(da_type_t type);
* @param atlas
* @return version
*/
char *da_getdataversion(da_atlas_t *atlas);
char *da_getdataversion(const da_atlas_t *atlas);
/**
* @brief returns the date creation's timestamp from the JSON in memory
* @param atlas
* @return version
*/
time_t da_getdatacreation(da_atlas_t *atlas);
time_t da_getdatacreation(const da_atlas_t *atlas);
char *da_getdatacreationiso8601(const da_atlas_t *atlas);
/**
* @brief returns the revision's number from the JSON in memory
* @param atlas
* @return version
*/
int da_getdatarevision(da_atlas_t *atlas);
int da_getdatarevision(const da_atlas_t *atlas);
/**
* @brief returns the name of a global property

View File

@ -0,0 +1,26 @@
#include "dac.h"
extern "C" {
void da_atlas_cache_init(const da_atlas_t *atlas) {
(void)atlas;
}
da_status_t da_atlas_cache_insert(const da_atlas_t *atlas, unsigned long long h, da_deviceinfo_t *info) {
(void)atlas;
(void)h;
(void)info;
return DA_OK;
}
da_status_t da_atlas_cache_search(const da_atlas_t *atlas, unsigned long long h, da_deviceinfo_t **info) {
(void)atlas;
(void)h;
(void)info;
return DA_OK;
}
void da_atlas_cache_close(da_atlas_t *atlas) {
(void)atlas;
}
}

View File

@ -0,0 +1 @@
#include <stdio.h>

View File

@ -0,0 +1 @@
#include <stdio.h>