Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vcpkg example #298

Merged
merged 5 commits into from
Mar 5, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -40,9 +40,11 @@ if (PACKAGE_MANAGER STREQUAL "hunter")
include("cmake/Hunter/init.cmake")
endif ()

project(libp2p VERSION 0.1.17 LANGUAGES C CXX)
project(libp2p VERSION 0.1.32 LANGUAGES C CXX)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

option(TESTING "Build tests" ON)
option(EXAMPLES "Build examples" ON)
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# CPP-Libp2p

> Fully compatible C++17 implementation of libp2p library
> Fully compatible C++20 implementation of libp2p library

Libp2p is a modular networking stack described in [spec](https://github.com/libp2p/specs)

11 changes: 6 additions & 5 deletions cmake/Hunter/config.cmake
Original file line number Diff line number Diff line change
@@ -17,9 +17,9 @@

hunter_config(
soralog
URL https://github.com/xDimon/soralog/archive/4dfffd3d949b1c16a04db2e5756555a4031732f7.tar.gz
SHA1 60e3dcaab2d8e43f0ed4fd22087677663c618716
# VERSION 0.2.4
VERSION 0.2.5
URL https://github.com/qdrvm/soralog/archive/refs/tags/v0.2.5.tar.gz
SHA1 67da2d17e93954c198b4419daa55911342c924a9
KEEP_PACKAGE_SOURCES
)

@@ -32,8 +32,9 @@ hunter_config(

hunter_config(
qtils
URL https://github.com/qdrvm/qtils/archive/1e492cf09a3640570cae59a951502614320c0797.tar.gz
SHA1 033dd907e2566c95ce2ccf1fa6dd9766bc896894
VERSION 0.1.0
URL https://github.com/qdrvm/qtils/archive/refs/tags/v0.1.0.tar.gz
SHA1 acc28902af7dc5d74ac33d486ad2261906716f5e
CMAKE_ARGS
FORMAT_ERROR_WITH_FULLTYPE=ON
KEEP_PACKAGE_SOURCES
13 changes: 13 additions & 0 deletions example/00-vcpkg-install/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
cmake_minimum_required(VERSION 3.15)
project(libp2p-example CXX)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(libsecp256k1 CONFIG REQUIRED)
find_package(ZLIB REQUIRED)

find_package(libp2p CONFIG REQUIRED)

add_executable(example main.cpp)
target_link_libraries(example PRIVATE p2p::p2p)
46 changes: 46 additions & 0 deletions example/00-vcpkg-install/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# LibP2P Example with vcpkg

This example demonstrates how to build and run a simple LibP2P application using vcpkg as the package manager.

## Prerequisites

- CMake 3.15 or higher
- A C++20 compatible compiler
- Git
- vcpkg

## Getting Started

1. Clone vcpkg if you haven't already:

```bash
git clone https://github.com/microsoft/vcpkg.git

./vcpkg/bootstrap-vcpkg.sh # For Linux/macOS
.\vcpkg\bootstrap-vcpkg.bat # For Windows
```

2. Clone this repository:

```bash
git clone https://github.com/libp2p/libp2p.git
cd libp2p/example/00-vcpkg-install
```

3. Install LibP2P dependencies using vcpkg:

```bash
cmake -B build -S . -DCMAKE_TOOLCHAIN_FILE=<path-to-vcpkg>/scripts/buildsystems/vcpkg.cmake
cmake --build build
```

4. Run the example:

```bash
./build/example
```

## Notes

- This example assumes you have a working vcpkg installation. If you encounter any issues, please refer to the [vcpkg documentation](https://vcpkg.io/en/getting-started.html) for troubleshooting.
- The example demonstrates how to build and run a simple LibP2P application using vcpkg as the package manager.
12 changes: 12 additions & 0 deletions example/00-vcpkg-install/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include <iostream>
#include <libp2p/multi/multiaddress.hpp>
#include <libp2p/peer/peer_id.hpp>

int main() {
auto ma_res = libp2p::multi::Multiaddress::create("/ip4/127.0.0.1/tcp/8080");
if (ma_res.has_value()) {
std::cout << "Created multiaddress: "
<< ma_res.value().getStringAddress() << std::endl;
}
return 0;
}
10 changes: 10 additions & 0 deletions example/00-vcpkg-install/vcpkg-configuration.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"default-registry": {
"kind": "git",
"baseline": "fe1cde61e971d53c9687cf9a46308f8f55da19fa",
"repository": "https://github.com/microsoft/vcpkg"
},
"overlay-ports": [
"vcpkg-overlay"
]
}
14 changes: 14 additions & 0 deletions example/00-vcpkg-install/vcpkg-overlay/boost-di/portfile.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
vcpkg_check_linkage(ONLY_STATIC_LIBRARY)
vcpkg_from_github(
OUT_SOURCE_PATH SOURCE_PATH
REPO qdrvm/boost-di
REF d5de6c9840c7fc2e44bf37134b4a14b88151ecc4
SHA512 98e924d02bde23940ab59330730de6d4198d1e98cbb303ef713e3ee06f88e90707a952e6aa9a0372213b46d6c9d3478e86ed5cd64d74899861984e786e21c319
)
vcpkg_cmake_configure(
SOURCE_PATH "${SOURCE_PATH}"
OPTIONS
-DBOOST_DI_OPT_BUILD_TESTS=OFF
-DBOOST_DI_OPT_BUILD_EXAMPLES=OFF
)
vcpkg_cmake_install()
8 changes: 8 additions & 0 deletions example/00-vcpkg-install/vcpkg-overlay/boost-di/vcpkg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "boost-di",
"version": "1.1.0.1",
"dependencies": [
{ "name": "vcpkg-cmake", "host": true },
{ "name": "vcpkg-cmake-config", "host": true }
]
}
20 changes: 20 additions & 0 deletions example/00-vcpkg-install/vcpkg-overlay/fmt/portfile.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
vcpkg_from_github(
OUT_SOURCE_PATH SOURCE_PATH
REPO fmtlib/fmt
REF 10.1.1
SHA512 288c349baac5f96f527d5b1bed0fa5f031aa509b4526560c684281388e91909a280c3262a2474d963b5d1bf7064b1c9930c6677fe54a0d8f86982d063296a54c
)

vcpkg_cmake_configure(
SOURCE_PATH "${SOURCE_PATH}"
OPTIONS
-DFMT_DOC=OFF
-DFMT_TEST=OFF
-DFMT_INSTALL=ON
-DCMAKE_CXX_STANDARD=20
)

vcpkg_cmake_install()
vcpkg_copy_pdbs()
vcpkg_cmake_config_fixup(PACKAGE_NAME fmt CONFIG_PATH lib/cmake/fmt)
file(REMOVE_RECURSE "${CURRENT_PACKAGES_DIR}/debug/include")
8 changes: 8 additions & 0 deletions example/00-vcpkg-install/vcpkg-overlay/fmt/vcpkg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "fmt",
"version": "10.1.1",
"dependencies": [
{ "name": "vcpkg-cmake", "host": true },
{ "name": "vcpkg-cmake-config", "host": true }
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 65c4776..5d4086a 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -60,12 +60,12 @@ ENDIF()

IF(CMAKE_BUILD_TYPE STREQUAL "Debug")
SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -O0 -g3")
- IF(CMAKE_C_COMPILER MATCHES "clang" AND
- NOT "$ENV{TRAVIS}" MATCHES "^true$" AND
- NOT "$ENV{EXTRA_CFLAGS}" MATCHES "-fsanitize")
- SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -fsanitize=address")
- SET(LIBS ${LIBS} -fsanitize=address)
- ENDIF()
+ # IF(CMAKE_C_COMPILER MATCHES "clang" AND
+ # NOT "$ENV{TRAVIS}" MATCHES "^true$" AND
+ # NOT "$ENV{EXTRA_CFLAGS}" MATCHES "-fsanitize")
+ # SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -fsanitize=address")
+ # SET(LIBS ${LIBS} -fsanitize=address)
+ # ENDIF()
# Uncomment to enable cleartext protocol mode (no crypto):
#SET (MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -DLSQUIC_ENABLE_HANDSHAKE_DISABLE=1")
ELSE()
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 5d4086a..e085a83 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -120,10 +120,12 @@ IF(CMAKE_BUILD_TYPE STREQUAL "Debug")
SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -Od")
#SET (MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -DFIU_ENABLE=1")
#SET(LIBS ${LIBS} fiu)
+ SET(LIB_NAME ssld cryptod)
ELSE()
SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -Ox")
# Comment out the following line to compile out debug messages:
#SET(MY_CMAKE_FLAGS "${MY_CMAKE_FLAGS} -DLSQUIC_LOWEST_LOG_LEVEL=LSQ_LOG_INFO")
+ SET(LIB_NAME ssl crypto)
ENDIF()

ENDIF() #MSVC
@@ -191,7 +193,7 @@ IF (NOT DEFINED BORINGSSL_LIB AND DEFINED BORINGSSL_DIR)
ELSE()


- FOREACH(LIB_NAME ssl crypto)
+ FOREACH(LIB ${LIB_NAME})
# If BORINGSSL_LIB is defined, try find each lib. Otherwise, user should define BORINGSSL_LIB_ssl,
# BORINGSSL_LIB_crypto and so on explicitly. For example, including boringssl and lsquic both via
# add_subdirectory:
@@ -201,20 +203,20 @@ ELSE()
# add_subdirectory(third_party/lsquic)
IF (DEFINED BORINGSSL_LIB)
IF (CMAKE_SYSTEM_NAME STREQUAL Windows)
- FIND_LIBRARY(BORINGSSL_LIB_${LIB_NAME}
- NAMES ${LIB_NAME}
+ FIND_LIBRARY(BORINGSSL_LIB_${LIB}
+ NAMES ${LIB}
PATHS ${BORINGSSL_LIB}
PATH_SUFFIXES Debug Release MinSizeRel RelWithDebInfo
NO_DEFAULT_PATH)
ELSE()
- FIND_LIBRARY(BORINGSSL_LIB_${LIB_NAME}
- NAMES lib${LIB_NAME}${LIB_SUFFIX}
+ FIND_LIBRARY(BORINGSSL_LIB_${LIB}
+ NAMES lib${LI}${LIB_SUFFIX}
PATHS ${BORINGSSL_LIB}
- PATH_SUFFIXES ${LIB_NAME}
+ PATH_SUFFIXES ${LIB}
NO_DEFAULT_PATH)
ENDIF()
ENDIF()
- IF(BORINGSSL_LIB_${LIB_NAME})
+ IF(BORINGSSL_LIB_${LIB})
MESSAGE(STATUS "Found ${LIB_NAME} library: ${BORINGSSL_LIB_${LIB_NAME}}")
ELSE()
MESSAGE(FATAL_ERROR "BORINGSSL_LIB_${LIB_NAME} library not found")
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
diff --git a/include/lsquic.h b/include/lsquic.h
index 389fbcc..c38d027 100644
--- a/include/lsquic.h
+++ b/include/lsquic.h
@@ -1671,6 +1671,10 @@ int lsquic_stream_close(lsquic_stream_t *s);
int
lsquic_stream_has_unacked_data (lsquic_stream_t *s);

+/* Return SSL object associated with this connection */
+struct ssl_st *
+lsquic_conn_ssl(struct lsquic_conn *conn);
+
/**
* Get certificate chain returned by the server. This can be used for
* server certificate verification.
diff --git a/src/liblsquic/lsquic_conn.c b/src/liblsquic/lsquic_conn.c
index f76550d..31e5285 100644
--- a/src/liblsquic/lsquic_conn.c
+++ b/src/liblsquic/lsquic_conn.c
@@ -128,6 +128,12 @@ lsquic_conn_crypto_alg_keysize (const lsquic_conn_t *lconn)
}


+struct ssl_st *
+lsquic_conn_ssl(struct lsquic_conn *lconn) {
+ return lconn->cn_esf_c->esf_get_ssl(lconn->cn_enc_session);
+}
+
+
struct stack_st_X509 *
lsquic_conn_get_server_cert_chain (struct lsquic_conn *lconn)
{
diff --git a/src/liblsquic/lsquic_enc_sess.h b/src/liblsquic/lsquic_enc_sess.h
index f45c15f..3505fbd 100644
--- a/src/liblsquic/lsquic_enc_sess.h
+++ b/src/liblsquic/lsquic_enc_sess.h
@@ -115,6 +115,9 @@ struct enc_session_funcs_common
(*esf_decrypt_packet)(enc_session_t *, struct lsquic_engine_public *,
const struct lsquic_conn *, struct lsquic_packet_in *);

+ struct ssl_st *
+ (*esf_get_ssl)(enc_session_t *);
+
struct stack_st_X509 *
(*esf_get_server_cert_chain) (enc_session_t *);

diff --git a/src/liblsquic/lsquic_enc_sess_ietf.c b/src/liblsquic/lsquic_enc_sess_ietf.c
index 66329c1..076c4c5 100644
--- a/src/liblsquic/lsquic_enc_sess_ietf.c
+++ b/src/liblsquic/lsquic_enc_sess_ietf.c
@@ -2519,6 +2519,13 @@ iquic_esf_global_cleanup (void)
}


+static struct ssl_st *
+iquic_esf_get_ssl(enc_session_t *enc_session_p) {
+ struct enc_sess_iquic *const enc_sess = enc_session_p;
+ return enc_sess->esi_ssl;
+}
+
+
static struct stack_st_X509 *
iquic_esf_get_server_cert_chain (enc_session_t *enc_session_p)
{
@@ -2744,6 +2751,7 @@ const struct enc_session_funcs_common lsquic_enc_session_common_ietf_v1 =
.esf_global_cleanup = iquic_esf_global_cleanup,
.esf_global_init = iquic_esf_global_init,
.esf_tag_len = IQUIC_TAG_LEN,
+ .esf_get_ssl = iquic_esf_get_ssl,
.esf_get_server_cert_chain
= iquic_esf_get_server_cert_chain,
.esf_get_sni = iquic_esf_get_sni,
@@ -2763,6 +2771,7 @@ const struct enc_session_funcs_common lsquic_enc_session_common_ietf_v1_no_flush
.esf_global_cleanup = iquic_esf_global_cleanup,
.esf_global_init = iquic_esf_global_init,
.esf_tag_len = IQUIC_TAG_LEN,
+ .esf_get_ssl = iquic_esf_get_ssl,
.esf_get_server_cert_chain
= iquic_esf_get_server_cert_chain,
.esf_get_sni = iquic_esf_get_sni,
Loading