Browse Source

C_InitI2P is compatible with more things if it passes argv by reference, it would appear. So to pass arguments to InitI2P you need to turn them back into char* argv[] by tokenizing them and copying them into an array which you then pass to InitI2P from C_InitI2P. The Streaming and Destination Creation parts need to have wrappers for over Identity.h, Streaming.h to be useful so remove them.

pull/1669/head
idk 3 years ago
parent
commit
f5db34b98b
No known key found for this signature in database
GPG Key ID: D75C03B39B5E14E1
  1. 47
      Makefile
  2. 5
      libi2pd/api.go
  3. 8
      libi2pd/api.swigcxx
  4. 107
      libi2pd/capi.cpp
  5. 17
      libi2pd/capi.h

47
Makefile

@ -28,12 +28,6 @@ else
LD_DEBUG = -s LD_DEBUG = -s
endif endif
ifeq ($(USE_STATIC),yes)
NEEDED_CXXFLAGS+= -static
else
endif
ifneq (, $(findstring darwin, $(SYS))) ifneq (, $(findstring darwin, $(SYS)))
DAEMON_SRC += $(DAEMON_SRC_DIR)/UnixDaemon.cpp DAEMON_SRC += $(DAEMON_SRC_DIR)/UnixDaemon.cpp
ifeq ($(HOMEBREW),1) ifeq ($(HOMEBREW),1)
@ -76,9 +70,9 @@ mk_obj_dir:
@mkdir -p obj/$(LANG_SRC_DIR) @mkdir -p obj/$(LANG_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)
langs: mk_obj_dir $(LANG_OBJS) $(SHLIB_LANG) $(ARLIB_LANG) langs: mk_obj_dir $(LANG_OBJS) $(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
@ -97,31 +91,30 @@ obj/%.o: %.cpp
$(I2PD): $(LANG_OBJS) $(DAEMON_OBJS) $(ARLIB) $(ARLIB_CLIENT) $(I2PD): $(LANG_OBJS) $(DAEMON_OBJS) $(ARLIB) $(ARLIB_CLIENT)
$(CXX) -o $@ $(LDFLAGS) $^ $(LDLIBS) $(CXX) -o $@ $(LDFLAGS) $^ $(LDLIBS)
$(SHLIB): $(LIB_OBJS) $(SHLIB): $(LIB_OBJS)
ifneq ($(USE_STATIC),yes) ifneq ($(USE_STATIC),yes)
$(CXX) $(LDFLAGS) -shared -o $@ $^ $(LDLIBS) $(CXX) $(LDFLAGS) -shared -o $@ $^ $(LDLIBS)
endif endif
$(SHLIB_CLIENT): $(LIB_CLIENT_OBJS) $(SHLIB_CLIENT): $(LIB_CLIENT_OBJS)
ifneq ($(USE_STATIC),yes) ifneq ($(USE_STATIC),yes)
$(CXX) $(LDFLAGS) -shared -o $@ $^ $(LDLIBS) $(SHLIB) $(CXX) $(LDFLAGS) -shared -o $@ $^ $(LDLIBS) $(SHLIB)
endif 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)
endif endif
$(ARLIB): $(LIB_OBJS) $(ARLIB): $(LIB_OBJS)
$(AR) -r $@ $^ $(AR) -r $@ $^
$(ARLIB_CLIENT): $(LIB_CLIENT_OBJS) $(ARLIB_CLIENT): $(LIB_CLIENT_OBJS)
$(AR) -r $@ $^ $(AR) -r $@ $^
$(ARLIB_LANG): $(LANG_OBJS) $(ARLIB_LANG): $(LANG_OBJS)
$(AR) -r $@ $^ $(AR) -r $@ $^
clean: clean:
$(RM) -r obj $(RM) -r obj
$(RM) -r docs/generated $(RM) -r docs/generated
@ -154,25 +147,3 @@ doxygen:
.PHONY: mk_obj_dir .PHONY: mk_obj_dir
.PHONY: install .PHONY: install
.PHONY: strip .PHONY: strip
flags:
@echo $(CXXFLAGS)
@echo $(NEEDED_CXXFLAGS)
@echo $(INCFLAGS)
@echo $(LDFLAGS)
@echo $(LDLIBS)
@echo $(USE_AESNI)
@echo $(USE_STATIC)
@echo $(USE_MESHNET)
@echo $(USE_UPNP)
@echo $(DEBUG)
##TODO: delete this before a PR
testc: api api_client
g++ -Ii18n -c _test.c -o test.o
# gcc -llibi2pd.so -c _test.c -o test.o
# $(CXX) $(LDFLAGS) $(LDLIBS) -static -Ii18n -Ilibi2pd -Ilibi2pd_client -g -Wall -o test.o _test.c libi2pd.a libi2pdclient.a #obj/libi2pd/*.o obj/i18n/*.o #libi2pd.so
# $(CXX) $(LDFLAGS) $(LDLIBS) -static -Ii18n -g -Wall -o test.o _test.c libi2pd.a libi2pdclient.a #obj/libi2pd/*.o obj/i18n/*.o #libi2pd.so
# gcc -o i2pd _test.c libi2pd.a -lstdc++ -llibi2pd -Llibi2pd
# gcc -Ii18n -I/usr/include/c++/10 -I/usr/include/x86_64-linux-gnu/c++/10 -llibi2pd.a -c test.c -o test.o
g++ test.o libi2pd.a libi2pdclient.a libi2pdlang.a -o test.main

5
libi2pd/api.go

@ -1,10 +1,7 @@
package api package api
/* /*
//void Go_InitI2P (int argc, char argv[], const char * appName){ #cgo CXXFLAGS: -I${SRCDIR}/../i18n -I${SRCDIR}/../libi2pd_client -g -Wall -Wextra -Wno-unused-parameter -pedantic -Wno-psabi -fPIC -D__AES__ -maes
//}
#cgo CPPFLAGS: -I${SRCDIR}/../i18n -I${SRCDIR}/../libi2pd_client -g -Wall -Wextra -Wno-unused-parameter -pedantic -Wno-psabi -fPIC -D__AES__ -maes
#cgo LDFLAGS: -latomic -lcrypto -lssl -lz -lboost_system -lboost_date_time -lboost_filesystem -lboost_program_options -lpthread -lstdc++ #cgo LDFLAGS: -latomic -lcrypto -lssl -lz -lboost_system -lboost_date_time -lboost_filesystem -lboost_program_options -lpthread -lstdc++
*/ */
import "C" import "C"

8
libi2pd/api.swigcxx

@ -3,14 +3,6 @@
%{ %{
#include "capi.h" #include "capi.h"
//#include "Streaming.h"
//#include "Destination.h"
//#include "Identity.h"
//#include "Tag.h"
%} %}
%include "capi.h" %include "capi.h"
//%include "Streaming.h"
//%include "Destination.h"
//%include "Identity.h"
//%include "Tag.h"

107
libi2pd/capi.cpp

@ -8,6 +8,64 @@
#include "api.h" #include "api.h"
#include "capi.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 #ifdef __cplusplus
extern "C" { extern "C" {
@ -15,7 +73,11 @@ extern "C" {
void C_InitI2P (int argc, char argv[], const char * appName) void C_InitI2P (int argc, char argv[], const char * appName)
{ {
return i2p::api::InitI2P(argc, &argv, 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 () void C_TerminateI2P ()
@ -40,49 +102,6 @@ void C_RunPeerTest ()
return i2p::api::RunPeerTest(); return i2p::api::RunPeerTest();
} }
i2p::client::ClientDestination *C_CreateLocalDestination (const i2p::data::PrivateKeys& keys, bool isPublic,
const std::map<std::string, std::string> * params)
{
return i2p::api::CreateLocalDestination(keys, isPublic, params).get();
}
i2p::client::ClientDestination *C_CreateTransientLocalDestination (bool isPublic, i2p::data::SigningKeyType sigType,
const std::map<std::string, std::string> * params)
{
return i2p::api::CreateLocalDestination(isPublic, sigType, params).get();
}
void C_DestroyLocalDestination (i2p::client::ClientDestination *dest)
{
std::shared_ptr<i2p::client::ClientDestination> cppDest(dest);
return i2p::api::DestroyLocalDestination(cppDest);
}
void C_RequestLeaseSet (i2p::client::ClientDestination *dest, const i2p::data::IdentHash& remote)
{
std::shared_ptr<i2p::client::ClientDestination> cppDest(dest);
return i2p::api::RequestLeaseSet(cppDest, remote);
}
i2p::stream::Stream *C_CreateStream (i2p::client::ClientDestination *dest, const i2p::data::IdentHash& remote)
{
std::shared_ptr<i2p::client::ClientDestination> cppDest(dest);
return i2p::api::CreateStream(cppDest, remote).get();
}
void C_AcceptStream (i2p::client::ClientDestination *dest, const i2p::stream::StreamingDestination::Acceptor& acceptor)
{
std::shared_ptr<i2p::client::ClientDestination> cppDest(dest);
return i2p::api::AcceptStream(cppDest, acceptor);
}
void C_DestroyStream (i2p::stream::Stream *stream)
{
std::shared_ptr<i2p::stream::Stream> cppStream(stream);
return i2p::api::DestroyStream(cppStream);
}
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

17
libi2pd/capi.h

@ -9,10 +9,6 @@
#ifndef CAPI_H__ #ifndef CAPI_H__
#define CAPI_H__ #define CAPI_H__
#include "Identity.h"
#include "Destination.h"
#include "Streaming.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
@ -26,19 +22,6 @@ void C_StartI2P (); //std::ostream *logStream = nullptr);
void C_StopI2P (); void C_StopI2P ();
void C_RunPeerTest (); // should be called after UPnP void C_RunPeerTest (); // should be called after UPnP
// destinations
i2p::client::ClientDestination *C_CreateLocalDestination (const i2p::data::PrivateKeys& keys, bool isPublic = true,
const std::map<std::string, std::string> * params = nullptr);
i2p::client::ClientDestination *C_CreateTransientLocalDestination (bool isPublic = false, i2p::data::SigningKeyType sigType = i2p::data::SIGNING_KEY_TYPE_ECDSA_SHA256_P256,
const std::map<std::string, std::string> * params = nullptr); // transient destinations usually not published
void C_DestroyLocalDestination (i2p::client::ClientDestination *dest);
// streams
void C_RequestLeaseSet (i2p::client::ClientDestination *dest, const i2p::data::IdentHash& remote);
i2p::stream::Stream *C_CreateStream (i2p::client::ClientDestination *dest, const i2p::data::IdentHash& remote);
void C_AcceptStream (i2p::client::ClientDestination *dest, const i2p::stream::StreamingDestination::Acceptor& acceptor);
void C_DestroyStream (i2p::stream::Stream *stream);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

Loading…
Cancel
Save