summaryrefslogtreecommitdiff
path: root/example
diff options
context:
space:
mode:
authorLi Baiyang <[email protected]>2018-11-13 10:33:59 +0800
committerLi Baiyang <[email protected]>2018-11-13 10:33:59 +0800
commit45fc5bdffc619b424608016c94c7174897f318e9 (patch)
tree28777e26d3b979eb7567ae804a4d01e33bdcaca0 /example
initialize
Diffstat (limited to 'example')
-rw-r--r--example/Makefile17
-rw-r--r--example/get_ip_coun.c109
2 files changed, 126 insertions, 0 deletions
diff --git a/example/Makefile b/example/Makefile
new file mode 100644
index 0000000..5cc82b8
--- /dev/null
+++ b/example/Makefile
@@ -0,0 +1,17 @@
+CC = gcc
+CFLAGS = -g -Wall -fPIC
+LDFLAGS = -L/usr/local/lib/
+LIB = -lmaxminddb
+OBJ = get_ip_coun.o
+TARGET = get_ip_coun_example
+
+.o:
+ $(CC) -c $(CFLAGS)
+
+$(TARGET): $(OBJ)
+ $(CC) -o $@ $(OBJ) $(LDFLAGS) $(LIB)
+
+clean:
+ -rm $(TARGET) $(OBJ)
+
+.PHONY: clean \ No newline at end of file
diff --git a/example/get_ip_coun.c b/example/get_ip_coun.c
new file mode 100644
index 0000000..b43ec15
--- /dev/null
+++ b/example/get_ip_coun.c
@@ -0,0 +1,109 @@
+#include <errno.h>
+#include <stdlib.h>
+#include <string.h>
+#include "../include/maxminddb.h"
+
+#define xdebug(fmt, arg...) \
+ do{ \
+ printf("%s %d : ", __FILE__, __LINE__); \
+ printf(fmt, ##arg); \
+ printf("\n"); \
+ }while(0)
+
+int main(int argc, char **argv)
+{
+ if(argc < 2)
+ {
+ xdebug("Usage : %s dbfilename IP", argv[0]);
+ return 0;
+ }
+
+ char *filename = argv[1];
+ char *ip_address = argv[2];
+
+ MMDB_s mmdb;
+ int status = MMDB_open(filename, MMDB_MODE_MMAP, &mmdb);
+
+ if(MMDB_SUCCESS != status)
+ {
+ fprintf(stderr, "\n Can't open %s - %s\n",
+ filename, MMDB_strerror(status));
+
+ if(MMDB_IO_ERROR == status)
+ {
+ fprintf(stderr, "IO error: %s\n", strerror(errno));
+ }
+ exit(1);
+ }
+
+ int gai_error, mmdb_error;
+
+ MMDB_lookup_result_s result =
+ MMDB_lookup_string(&mmdb, ip_address, &gai_error, &mmdb_error);
+
+ if (0 != gai_error)
+ {
+ fprintf(stderr,
+ "\n Error from getaddrinfo for %s - %s\n\n",
+ ip_address, gai_strerror(gai_error));
+ exit(2);
+ }
+
+ if (MMDB_SUCCESS != mmdb_error)
+ {
+ fprintf(stderr,
+ "\n Got an error from libmaxminddb: %s\n\n",
+ MMDB_strerror(mmdb_error));
+ exit(3);
+ }
+
+ // MMDB_entry_data_list_s *entry_data_list = NULL;
+ MMDB_entry_data_s entry_data;
+
+ int exit_code = 0;
+ if (result.found_entry)
+ {
+ // int status = MMDB_get_entry_data_list(&result.entry,
+ // &entry_data_list);
+
+ int status = MMDB_get_value(&result.entry, &entry_data, "COUNTRY", NULL);
+
+ if (MMDB_SUCCESS != status)
+ {
+ fprintf(
+ stderr,
+ "Got an error looking up the entry data - %s\n",
+ MMDB_strerror(status));
+ exit_code = 4;
+ goto end;
+ }
+
+ // if (NULL != entry_data_list)
+ // {
+ // MMDB_dump_entry_data_list(stdout, entry_data_list, 2);
+ // }
+
+ if (entry_data.has_data)
+ {
+ char *name_en = malloc(sizeof(entry_data.data_size + 1));
+ memset(name_en, '\0', sizeof(entry_data.data_size + 1));
+ memcpy(name_en, entry_data.utf8_string, entry_data.data_size);
+
+ printf ("%s\n", name_en);
+ free(name_en);
+ name_en = NULL;
+ }
+ }
+ else
+ {
+ fprintf(
+ stderr,
+ "\n No entry for this IP address (%s) was found\n\n",
+ ip_address);
+ exit_code = 5;
+ }
+end:
+ // MMDB_free_entry_data_list(entry_data_list);
+ MMDB_close(&mmdb);
+ exit(exit_code);
+} \ No newline at end of file