Skip to content
This repository has been archived by the owner on Mar 4, 2021. It is now read-only.

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
bassosimone committed Oct 30, 2018
0 parents commit 5fed6f2
Show file tree
Hide file tree
Showing 14 changed files with 503 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .appveyor.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
image: Visual Studio 2017
environment:
matrix:
- CMAKE_GENERATOR: "Visual Studio 15 2017 Win64"
- CMAKE_GENERATOR: "Visual Studio 15 2017"
build_script:
- cmd: git submodule update --init --recursive
- cmd: .\.ci\common\script\appveyor.bat "%CMAKE_GENERATOR%"
1 change: 1 addition & 0 deletions .ci/common
Submodule common added at 30a13d
4 changes: 4 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
BasedOnStyle: Google
ColumnLimit: 0
AllowShortIfStatementsOnASingleLine: true
AllowShortCaseLabelsOnASingleLine: true
37 changes: 37 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/ALL_BUILD.vcxproj
/ALL_BUILD.vcxproj.filters
/CMakeCache.txt
/CMakeFiles/
/CTestTestfile.cmake
/GeoLite2-ASN_20*/
/GeoLite2-Country_20*/
/INSTALL.vcxproj
/INSTALL.vcxproj.filters
/MK_DIST/
/Makefile
/RUN_TESTS.vcxproj
/RUN_TESTS.vcxproj.filters
/Release/
/Testing
/ZERO_CHECK.vcxproj
/ZERO_CHECK.vcxproj.filters
/argh.h
/asn.mmdb
/ca-bundle.pem
/catch.hpp
/cmake_install.cmake
/country.mmdb
/geolite2-asn.tar.gz
/geolite2-country.tar.gz
/install_manifest.txt
/libmkmmdb.a
/mkcurl.h
/mkdata.h
/mkmmdb-client
/mkmmdb-client.dir/
/mkmmdb-client.vcxproj
/mkmmdb-client.vcxproj.filters
/mkmmdb.sln
/windows-curl-*.tar.gz
/windows-libmaxminddb-*.tar.gz
/x64/
6 changes: 6 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[submodule "cmake/Modules"]
path = cmake/Modules
url = https://github.com/measurement-kit/cmake-modules
[submodule ".ci/common"]
path = .ci/common
url = https://github.com/measurement-kit/ci-common
14 changes: 14 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
language: c++
services:
- docker
sudo: required
matrix:
include:
- env: BUILD_TYPE="asan"
- env: BUILD_TYPE="clang"
- env: BUILD_TYPE="coverage"
- env: BUILD_TYPE="lsan"
- env: BUILD_TYPE="ubsan"
- env: BUILD_TYPE="vanilla"
script:
- ./.ci/common/script/travis $BUILD_TYPE
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Simone Basso <bassosimone@gmail.com>
65 changes: 65 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
cmake_minimum_required(VERSION 3.1.0)
project(mkmmdb LANGUAGES CXX)

list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake/Modules")
include(MkUtils)

# Download dependencies
# ---------------------

MkDownloadMMDBDatabases()

if("${MSVC}")
MkDownloadMeasurementKitPrebuiltWindowsLibmaxminddb()
list(APPEND CMAKE_REQUIRED_INCLUDES "${MK_WINDOWS_LIBMAXMINDDB_INCLUDE_PATH}")
list(APPEND CMAKE_LIBRARY_PATH "${MK_WINDOWS_LIBMAXMINDDB_LIBRARY_PATH}")
endif()

# Checks
# ------

include(CheckIncludeFiles)

check_include_files(maxminddb.h MKMMDB_HAVE_MAXMINDDB_H)
find_library(LIBMAXMINDDB_LIBRARY maxminddb)
if (("${MKMMDB_HAVE_MAXMINDDB_H}" STREQUAL "") OR
("${LIBMAXMINDDB_LIBRARY}" STREQUAL "LIBMAXMINDDB_LIBRARY-NOTFOUND"))
message(FATAL_ERROR "Cannot find libmaxminddb")
endif()
list(APPEND MKMMDB_LIBS "${LIBMAXMINDDB_LIBRARY}")

find_package(CURL REQUIRED)

# Compiler flags
# --------------

MkSetCompilerFlags()

# Library and binary
# ------------------

set(MKMMDB_INCLUDES ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR}
${CURL_INCLUDE_DIRS} ${CMAKE_REQUIRED_INCLUDES})

add_library(mkmmdb mkmmdb.cpp)
add_executable(mkmmdb-client mkmmdb-client.cpp)
target_include_directories(mkmmdb-client PUBLIC ${MKMMDB_INCLUDES})
if("${WIN32}" OR "${MINGW}")
list(APPEND MKMMDB_LIBS "ws2_32")
if ("${MINGW}")
list(APPEND MKMMDB_LIBS -static-libgcc -static-libstdc++)
endif()
endif()
list(APPEND MKMMDB_LIBS Threads::Threads)
target_link_libraries(mkmmdb-client "${MKMMDB_LIBS}" mkmmdb)

# Testing
# -------

set(BUILD_TESTING "ON" CACHE BOOL "Whether to build tests")
if(${BUILD_TESTING})
enable_testing()
add_test(NAME basic_test COMMAND mkmmdb-client 8.8.8.8)
add_test(NAME name_not_found_test COMMAND mkmmdb-client 127.0.0.1)
add_test(NAME invalid_ip_test COMMAND mkmmdb-client 8.8.8.x)
endif()
26 changes: 26 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Copyright 2018 Open Observatory of Network Interference (OONI), The Tor Project

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.

3. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.

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.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Measurement Kit MMDB wrappers

[![GitHub license](https://img.shields.io/github/license/measurement-kit/mkmmdb.svg)](https://raw.githubusercontent.com/measurement-kit/mkmmdb/master/LICENSE) [![Github Releases](https://img.shields.io/github/release/measurement-kit/mkmmdb.svg)](https://github.com/measurement-kit/mkmmdb/releases) [![Build Status](https://img.shields.io/travis/measurement-kit/mkmmdb/master.svg?label=travis)](https://travis-ci.org/measurement-kit/mkmmdb) [![codecov](https://codecov.io/gh/measurement-kit/mkmmdb/branch/master/graph/badge.svg)](https://codecov.io/gh/measurement-kit/mkmmdb) [![Build status](https://img.shields.io/appveyor/ci/bassosimone/mkmmdb/master.svg?label=appveyor)](https://ci.appveyor.com/project/bassosimone/mkmmdb/branch/master)

Experimental library to use libmaxminddb from Measurement Kit.
1 change: 1 addition & 0 deletions cmake/Modules
Submodule Modules added at 704121
47 changes: 47 additions & 0 deletions mkmmdb-client.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include <string.h>

#include <iostream>
#include <ios>

#include "mkmmdb.h"

int main(int argc, char **argv) {
if (argc != 2) {
std::clog << "Usage: mkmmdb-client <ip>" << std::endl;
exit(EXIT_FAILURE);
}
std::string ip = argv[1];
{
mkmmdb_uptr db{mkmmdb_open("country.mmdb")};
if (!mkmmdb_good(db.get())) {
std::clog << "Cannot open country.mmdb" << std::endl;
// FALLTHROUGH
}
std::string s = mkmmdb_lookup_cc(db.get(), ip.c_str());
std::clog << "=== BEGIN LOGS === " << std::endl;
std::clog << mkmmdb_get_last_lookup_logs(db.get());
std::clog << "=== END LOGS === " << std::endl;
std::clog << "CC: " << s << std::endl;
}
{
mkmmdb_uptr db{mkmmdb_open("asn.mmdb")};
if (!mkmmdb_good(db.get())) {
std::clog << "Cannot open asn.mmdb" << std::endl;
// FALLTHROUGH
}
{
std::string s = mkmmdb_lookup_org(db.get(), ip.c_str());
std::clog << "=== BEGIN LOGS === " << std::endl;
std::clog << mkmmdb_get_last_lookup_logs(db.get());
std::clog << "=== END LOGS === " << std::endl;
std::clog << "ORG: " << s << std::endl;
}
{
int64_t v = mkmmdb_lookup_asn(db.get(), ip.c_str());
std::clog << "=== BEGIN LOGS === " << std::endl;
std::clog << mkmmdb_get_last_lookup_logs(db.get());
std::clog << "=== END LOGS === " << std::endl;
std::clog << "ASN: " << v << std::endl;
}
}
}
2 changes: 2 additions & 0 deletions mkmmdb.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#define MKMMDB_INLINE_IMPL
#include "mkmmdb.h"
Loading

0 comments on commit 5fed6f2

Please sign in to comment.