aboutsummaryrefslogtreecommitdiff
path: root/src/vmod_geoip.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/vmod_geoip.c')
-rw-r--r--src/vmod_geoip.c71
1 files changed, 68 insertions, 3 deletions
diff --git a/src/vmod_geoip.c b/src/vmod_geoip.c
index 6ad23e2..fd85030 100644
--- a/src/vmod_geoip.c
+++ b/src/vmod_geoip.c
@@ -7,6 +7,7 @@
#include <stdlib.h>
#include <errno.h>
#include <string.h>
+#include <inttypes.h>
#include "vcl.h"
#include "vrt.h"
#include "vcc_if.h"
@@ -94,8 +95,70 @@ conv_utf_string (struct ws *ws, MMDB_entry_data_s *dptr)
return retval;
}
+static char *
+wsprintf(struct ws *ws, char const *fmt, ...)
+{
+ va_list ap;
+ size_t u, n;
+ char *p;
+
+ va_start(ap, fmt);
+ u = WS_Reserve(ws, 0);
+ p = ws->f;
+ n = vsnprintf(p, u, fmt, ap);
+ va_end(ap);
+ if (n)
+ n++;
+ else
+ p = NULL;
+ WS_Release(ws, n);
+ return p;
+}
+
+static char *
+conv_uint16(struct ws *ws, MMDB_entry_data_s *dptr)
+{
+ return wsprintf(ws, "%" PRIu16, dptr->uint16);
+}
+
+static char *
+conv_uint32(struct ws *ws, MMDB_entry_data_s *dptr)
+{
+ return wsprintf(ws, "%" PRIu32, dptr->uint32);
+}
+
+static char *
+conv_int32(struct ws *ws, MMDB_entry_data_s *dptr)
+{
+ return wsprintf(ws, "%" PRIi32, dptr->int32);
+}
+
+static char *
+conv_bool(struct ws *ws, MMDB_entry_data_s *dptr)
+{
+ return wsprintf(ws, "%01d", dptr->boolean ? 1 : 0);
+}
+
+static char *
+conv_double(struct ws *ws, MMDB_entry_data_s *dptr)
+{
+ return wsprintf(ws, "%g", dptr->double_value);
+}
+
+static char *
+conv_float(struct ws *ws, MMDB_entry_data_s *dptr)
+{
+ return wsprintf(ws, "%g", dptr->float_value);
+}
+
static char *(*entry_conv[]) (struct ws *, MMDB_entry_data_s *) = {
- [MMDB_DATA_TYPE_UTF8_STRING] = conv_utf_string
+ [MMDB_DATA_TYPE_UTF8_STRING] = conv_utf_string,
+ [MMDB_DATA_TYPE_UINT16] = conv_uint16,
+ [MMDB_DATA_TYPE_UINT32] = conv_uint32,
+ [MMDB_DATA_TYPE_INT32] = conv_int32,
+ [MMDB_DATA_TYPE_BOOLEAN] = conv_bool,
+ [MMDB_DATA_TYPE_DOUBLE] = conv_double,
+ [MMDB_DATA_TYPE_FLOAT] = conv_float
};
static char *
@@ -125,7 +188,8 @@ lookup_geoip_database(struct ws *ws,
if (!result.found_entry)
return NULL;
- rc = MMDB_aget_value(&result.entry, &entry_data, (const char * const* const) path);
+ rc = MMDB_aget_value(&result.entry, &entry_data,
+ (const char * const* const) path);
if (rc != MMDB_SUCCESS) {
fprintf(stderr, "%s %s: MMDB_aget_value %s\n",
pathstr, ipstr, MMDB_strerror(rc));
@@ -140,7 +204,8 @@ lookup_geoip_database(struct ws *ws,
&& entry_conv[entry_data.type]) {
retval = entry_conv[entry_data.type] (ws, &entry_data);
} else
- retval = NULL;
+ retval = wsprintf(ws, "[%s: can't format %s of type %d]",
+ ipstr, pathstr, entry_data.type);
return retval;
}

Return to:

Send suggestions and report system problems to the System administrator.