Browse Source

Merge pull request #1669 from eyedeekay/c-wrapper-libi2pd-api

C wrapper for part of the libi2pd api
pull/1677/head
orignal 3 years ago committed by GitHub
parent
commit
d6ce5f9fa1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 29
      Makefile
  2. 2
      filelist.mk
  3. 15
      libi2pd_wrapper/api.go
  4. 8
      libi2pd_wrapper/api.swigcxx
  5. 106
      libi2pd_wrapper/capi.cpp
  6. 29
      libi2pd_wrapper/capi.h

29
Makefile

@ -1,25 +1,28 @@
SYS := $(shell $(CXX) -dumpmachine) SYS := $(shell $(CXX) -dumpmachine)
SHLIB := libi2pd.so SHLIB := libi2pd.so
ARLIB := libi2pd.a ARLIB := libi2pd.a
SHLIB_CLIENT := libi2pdclient.so
ARLIB_CLIENT := libi2pdclient.a
SHLIB_LANG := libi2pdlang.so SHLIB_LANG := libi2pdlang.so
ARLIB_LANG := libi2pdlang.a ARLIB_LANG := libi2pdlang.a
SHLIB_CLIENT := libi2pdclient.so
ARLIB_CLIENT := libi2pdclient.a
SHLIB_WRAP := libi2pdwrapper.so
ARLIB_WRAP := libi2pdwrapper.a
I2PD := i2pd I2PD := i2pd
LIB_SRC_DIR := libi2pd LIB_SRC_DIR := libi2pd
LIB_CLIENT_SRC_DIR := libi2pd_client LIB_CLIENT_SRC_DIR := libi2pd_client
WRAP_SRC_DIR := libi2pd_wrapper
LANG_SRC_DIR := i18n LANG_SRC_DIR := i18n
DAEMON_SRC_DIR := daemon DAEMON_SRC_DIR := daemon
# import source files lists # import source files lists
include filelist.mk include filelist.mk
USE_AESNI := yes USE_AESNI := $(or $(USE_AESNI),yes)
USE_STATIC := no USE_STATIC := $(or $(USE_STATIC),no)
USE_MESHNET := no USE_MESHNET := $(or $(USE_MESHNET),no)
USE_UPNP := no USE_UPNP := $(or $(USE_UPNP),no)
DEBUG := yes DEBUG := $(or $(DEBUG),yes)
ifeq ($(DEBUG),yes) ifeq ($(DEBUG),yes)
CXX_DEBUG = -g CXX_DEBUG = -g
@ -56,6 +59,7 @@ NEEDED_CXXFLAGS += -MMD -MP -I$(LIB_SRC_DIR) -I$(LIB_CLIENT_SRC_DIR) -I$(LANG_SR
LIB_OBJS += $(patsubst %.cpp,obj/%.o,$(LIB_SRC)) LIB_OBJS += $(patsubst %.cpp,obj/%.o,$(LIB_SRC))
LIB_CLIENT_OBJS += $(patsubst %.cpp,obj/%.o,$(LIB_CLIENT_SRC)) LIB_CLIENT_OBJS += $(patsubst %.cpp,obj/%.o,$(LIB_CLIENT_SRC))
WRAP_LIB_OBJS += $(patsubst %.cpp,obj/%.o,$(WRAP_LIB_SRC))
LANG_OBJS += $(patsubst %.cpp,obj/%.o,$(LANG_SRC)) LANG_OBJS += $(patsubst %.cpp,obj/%.o,$(LANG_SRC))
DAEMON_OBJS += $(patsubst %.cpp,obj/%.o,$(DAEMON_SRC)) DAEMON_OBJS += $(patsubst %.cpp,obj/%.o,$(DAEMON_SRC))
DEPS += $(LIB_OBJS:.o=.d) $(LIB_CLIENT_OBJS:.o=.d) $(LANG_OBJS:.o=.d) $(DAEMON_OBJS:.o=.d) DEPS += $(LIB_OBJS:.o=.d) $(LIB_CLIENT_OBJS:.o=.d) $(LANG_OBJS:.o=.d) $(DAEMON_OBJS:.o=.d)
@ -68,13 +72,16 @@ mk_obj_dir:
@mkdir -p obj/$(LIB_SRC_DIR) @mkdir -p obj/$(LIB_SRC_DIR)
@mkdir -p obj/$(LIB_CLIENT_SRC_DIR) @mkdir -p obj/$(LIB_CLIENT_SRC_DIR)
@mkdir -p obj/$(LANG_SRC_DIR) @mkdir -p obj/$(LANG_SRC_DIR)
@mkdir -p obj/$(WRAP_SRC_DIR)
@mkdir -p obj/$(DAEMON_SRC_DIR) @mkdir -p obj/$(DAEMON_SRC_DIR)
api: mk_obj_dir $(SHLIB) $(ARLIB) api: mk_obj_dir $(SHLIB) $(ARLIB)
client: mk_obj_dir $(SHLIB_CLIENT) $(ARLIB_CLIENT) client: mk_obj_dir $(SHLIB_CLIENT) $(ARLIB_CLIENT)
api_client: mk_obj_dir $(SHLIB) $(ARLIB) $(SHLIB_CLIENT) $(ARLIB_CLIENT) api_client: mk_obj_dir $(SHLIB) $(ARLIB) $(SHLIB_CLIENT) $(ARLIB_CLIENT)
wrapper: api_client $(SHLIB_WRAP) $(ARLIB_WRAP)
lang: mk_obj_dir $(SHLIB_LANG) $(ARLIB_LANG) lang: mk_obj_dir $(SHLIB_LANG) $(ARLIB_LANG)
## NOTE: The NEEDED_CXXFLAGS are here so that CXXFLAGS can be specified at build time ## NOTE: The NEEDED_CXXFLAGS are here so that CXXFLAGS can be specified at build time
## **without** overwriting the CXXFLAGS which we need in order to build. ## **without** overwriting the CXXFLAGS which we need in order to build.
## For example, when adding 'hardening flags' to the build ## For example, when adding 'hardening flags' to the build
@ -101,6 +108,11 @@ ifneq ($(USE_STATIC),yes)
$(CXX) $(LDFLAGS) -shared -o $@ $^ $(LDLIBS) $(SHLIB) $(CXX) $(LDFLAGS) -shared -o $@ $^ $(LDLIBS) $(SHLIB)
endif endif
$(SHLIB_WRAP): $(WRAP_LIB_OBJS)
ifneq ($(USE_STATIC),yes)
$(CXX) $(LDFLAGS) -shared -o $@ $^ $(LDLIBS)
endif
$(SHLIB_LANG): $(LANG_OBJS) $(SHLIB_LANG): $(LANG_OBJS)
ifneq ($(USE_STATIC),yes) ifneq ($(USE_STATIC),yes)
$(CXX) $(LDFLAGS) -shared -o $@ $^ $(LDLIBS) $(CXX) $(LDFLAGS) -shared -o $@ $^ $(LDLIBS)
@ -112,6 +124,9 @@ $(ARLIB): $(LIB_OBJS)
$(ARLIB_CLIENT): $(LIB_CLIENT_OBJS) $(ARLIB_CLIENT): $(LIB_CLIENT_OBJS)
$(AR) -r $@ $^ $(AR) -r $@ $^
$(ARLIB_WRAP): $(WRAP_LIB_OBJS)
$(AR) -r $@ $^
$(ARLIB_LANG): $(LANG_OBJS) $(ARLIB_LANG): $(LANG_OBJS)
$(AR) -r $@ $^ $(AR) -r $@ $^

2
filelist.mk

@ -21,4 +21,6 @@ LIB_CLIENT_SRC = $(wildcard $(LIB_CLIENT_SRC_DIR)/*.cpp)
LANG_SRC = $(wildcard $(LANG_SRC_DIR)/*.cpp) LANG_SRC = $(wildcard $(LANG_SRC_DIR)/*.cpp)
WRAP_LIB_SRC = $(wildcard $(WRAP_SRC_DIR)/*.cpp)
DAEMON_SRC = $(wildcard $(DAEMON_SRC_DIR)/*.cpp) DAEMON_SRC = $(wildcard $(DAEMON_SRC_DIR)/*.cpp)

15
libi2pd_wrapper/api.go

@ -0,0 +1,15 @@
package api
/*
* Copyright (c) 2013-2020, The PurpleI2P Project
*
* This file is part of Purple i2pd project and licensed under BSD3
*
* See full license text in LICENSE file at top of project tree
*/
/*
#cgo CXXFLAGS: -I${SRCDIR}/../i18n -I${SRCDIR}/../libi2pd_client -I${SRCDIR}/../libi2pd -g -Wall -Wextra -Wno-unused-parameter -pedantic -Wno-psabi -fPIC -D__AES__ -maes
#cgo LDFLAGS: -L${SRCDIR}/../ -l:libi2pd.a -l:libi2pdlang.a -latomic -lcrypto -lssl -lz -lboost_system -lboost_date_time -lboost_filesystem -lboost_program_options -lpthread -lstdc++
*/
import "C"

8
libi2pd_wrapper/api.swigcxx

@ -0,0 +1,8 @@
// See swig.org for more inteface options,
// e.g. map std::string to Go string
%{
#include "capi.h"
%}
%include "capi.h"

106
libi2pd_wrapper/capi.cpp

@ -0,0 +1,106 @@
/*
* Copyright (c) 2013-2020, The PurpleI2P Project
*
* This file is part of Purple i2pd project and licensed under BSD3
*
* See full license text in LICENSE file at top of project tree
*/
#include "api.h"
#include "capi.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
// Uses the example from: https://stackoverflow.com/a/9210560
// See also https://stackoverflow.com/questions/9210528/split-string-with-delimiters-in-c/9210560#
// Does not handle consecutive delimiters, this is only for passing
// lists of arguments by value to InitI2P from C_InitI2P
char** str_split(char* a_str, const char a_delim)
{
char** result = 0;
size_t count = 0;
char* tmp = a_str;
char* last_comma = 0;
char delim[2];
delim[0] = a_delim;
delim[1] = 0;
/* Count how many elements will be extracted. */
while (*tmp)
{
if (a_delim == *tmp)
{
count++;
last_comma = tmp;
}
tmp++;
}
/* Add space for trailing token. */
count += last_comma < (a_str + strlen(a_str) - 1);
/* Add space for terminating null string so caller
knows where the list of returned strings ends. */
count++;
result = (char**) malloc(sizeof(char*) * count);
if (result)
{
size_t idx = 0;
char* token = strtok(a_str, delim);
while (token)
{
assert(idx < count);
*(result + idx++) = strdup(token);
token = strtok(0, delim);
}
assert(idx == count - 1);
*(result + idx) = 0;
}
return result;
}
#ifdef __cplusplus
extern "C" {
#endif
void C_InitI2P (int argc, char argv[], const char * appName)
{
const char* delim = " ";
char* vargs = strdup(argv);
char** args = str_split(vargs, *delim);
std::cout << argv;
return i2p::api::InitI2P(argc, args, appName);
}
void C_TerminateI2P ()
{
return i2p::api::TerminateI2P();
}
void C_StartI2P ()
{
std::shared_ptr<std::ostream> logStream;
return i2p::api::StartI2P(logStream);
}
void C_StopI2P ()
{
return i2p::api::StopI2P();
}
void C_RunPeerTest ()
{
return i2p::api::RunPeerTest();
}
#ifdef __cplusplus
}
#endif

29
libi2pd_wrapper/capi.h

@ -0,0 +1,29 @@
/*
* Copyright (c) 2013-2020, The PurpleI2P Project
*
* This file is part of Purple i2pd project and licensed under BSD3
*
* See full license text in LICENSE file at top of project tree
*/
#ifndef CAPI_H__
#define CAPI_H__
#ifdef __cplusplus
extern "C" {
#endif
// initialization start and stop
void C_InitI2P (int argc, char argv[], const char * appName);
//void C_InitI2P (int argc, char** argv, const char * appName);
void C_TerminateI2P ();
void C_StartI2P ();
// write system log to logStream, if not specified to <appName>.log in application's folder
void C_StopI2P ();
void C_RunPeerTest (); // should be called after UPnP
#ifdef __cplusplus
}
#endif
#endif
Loading…
Cancel
Save