Skip to content

Commit

Permalink
Rename libpeer specific configs with CONFIG_LIBPEER_ prefix.
Browse files Browse the repository at this point in the history
  • Loading branch information
ciniml committed Jan 18, 2025
1 parent 734b80c commit fcb046d
Show file tree
Hide file tree
Showing 17 changed files with 186 additions and 98 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ if (DEFINED ENV{IDF_PATH})
PRIV_INCLUDE_DIRS "./src"
REQUIRES mbedtls srtp json esp_netif
)
add_definitions("-DCONFIG_USE_LWIP=1" "-DCONFIG_USE_USRSCTP=0" "-DCONFIG_AUDIO_BUFFER_SIZE=8096" "-DCONFIG_DATA_BUFFER_SIZE=102400" "-D__BYTE_ORDER=__LITTLE_ENDIAN" "-DHTTP_DO_NOT_USE_CUSTOM_CONFIG" "-DMQTT_DO_NOT_USE_CUSTOM_CONFIG")
add_definitions("-DCONFIG_LIBPEER_USE_LWIP=1" "-DCONFIG_LIBPEER_USE_USRSCTP=0" "-D__BYTE_ORDER=__LITTLE_ENDIAN" "-DHTTP_DO_NOT_USE_CUSTOM_CONFIG" "-DMQTT_DO_NOT_USE_CUSTOM_CONFIG")
return()
endif()

Expand Down
61 changes: 61 additions & 0 deletions Kconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
menu "libpeer"
config LIBPEER_SCTP_MTU
int "SCTP MTU"
default 1200
help
The SCTP MTU.
config LIBPEER_MTU
int "MTU"
default 1300
help
The MTU.
config LIBPEER_VIDEO_BUFFER_SIZE_MTUS
int "Video Buffer Size in MTUs"
default 7
help
The Video RB Data MTUs.
config LIBPEER_AUDIO_BUFFER_SIZE_MTUS
int "Audio Buffer Size in MTUs"
default 7
help
The Audio RB Data MTUs.
config LIBPEER_DATA_BUFFER_SIZE_MTUS
int "Data Buffer Size in MTUs"
default 86
help
The Data RB Data MTUs.
config LIBPEER_SDP_BUFFER_SIZE
int "SDP Buffer Size"
default 8096
help
The SDP Buffer Size.
config LIBPEER_MQTT_BUFFER_SIZE
int "MQTT Buffer Size"
default 8096
help
The MQTT Buffer Size.
config LIBPEER_AUDIO_LATENCY_MS
int "Audio Latency (ms)"
default 20
help
The Audio Latency in milliseconds.
config LIBPEER_KEEPALIVE_CONNCHECK
int "Keepalive Conncheck interval"
default 0
help
The Keepalive Conncheck.
choice LIBPEER_LOG_LEVEL
prompt "Log Level"
default LIBPEER_LOG_LEVEL_INFO
help
The Log Level.
config LIBPEER_LOG_LEVEL_DEBUG
bool "Debug"
config LIBPEER_LOG_LEVEL_INFO
bool "Info"
config LIBPEER_LOG_LEVEL_WARN
bool "Warn"
config LIBPEER_LOG_LEVEL_ERROR
bool "Error"
endchoice
endmenu
2 changes: 1 addition & 1 deletion src/address.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#define ADDRESS_H_

#include "config.h"
#if CONFIG_USE_LWIP
#if CONFIG_LIBPEER_USE_LWIP
#include <lwip/sockets.h>
#else
#include <arpa/inet.h>
Expand Down
10 changes: 5 additions & 5 deletions src/agent.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ int agent_create(Agent* agent) {
}
LOGI("create IPv4 UDP socket: %d", agent->udp_sockets[0].fd);

#if CONFIG_IPV6
#if CONFIG_LIBPEER_IPV6
if ((ret = udp_socket_open(&agent->udp_sockets[1], AF_INET6, 0)) < 0) {
LOGE("Failed to create IPv6 UDP socket.");
return ret;
Expand All @@ -48,7 +48,7 @@ void agent_destroy(Agent* agent) {
udp_socket_close(&agent->udp_sockets[0]);
}

#if CONFIG_IPV6
#if CONFIG_LIBPEER_IPV6
if (agent->udp_sockets[1].fd > 0) {
udp_socket_close(&agent->udp_sockets[1]);
}
Expand All @@ -62,7 +62,7 @@ static int agent_socket_recv(Agent* agent, Address* addr, uint8_t* buf, int len)
fd_set rfds;
struct timeval tv;
int addr_type[] = { AF_INET,
#if CONFIG_IPV6
#if CONFIG_LIBPEER_IPV6
AF_INET6,
#endif
};
Expand Down Expand Up @@ -122,10 +122,10 @@ static int agent_socket_send(Agent* agent, Address* addr, const uint8_t* buf, in

static int agent_create_host_addr(Agent* agent) {
int i, j;
const char* iface_prefx[] = {CONFIG_IFACE_PREFIX};
const char* iface_prefx[] = {CONFIG_LIBPEER_IFACE_PREFIX};
IceCandidate* ice_candidate;
int addr_type[] = { AF_INET,
#if CONFIG_IPV6
#if CONFIG_LIBPEER_IPV6
AF_INET6,
#endif
};
Expand Down
95 changes: 61 additions & 34 deletions src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,75 +2,102 @@
#define CONFIG_H_

// uncomment this if you want to handshake with a aiortc
// #define CONFIG_DTLS_USE_ECDSA 1
// #define CONFIG_LIBPEER_DTLS_USE_ECDSA 1

#define SCTP_MTU (1200)
#define CONFIG_MTU (1300)
#ifndef CONFIG_LIBPEER_SCTP_MTU
#define CONFIG_LIBPEER_SCTP_MTU (1200)
#endif
#ifndef CONFIG_LIBPEER_MTU
#define CONFIG_LIBPEER_MTU (1300)
#endif

#ifndef CONFIG_USE_LWIP
#define CONFIG_USE_LWIP 0
#ifndef CONFIG_LIBPEER_USE_LWIP
#define CONFIG_LIBPEER_USE_LWIP 0
#endif

#ifndef CONFIG_MBEDTLS_DEBUG
#define CONFIG_MBEDTLS_DEBUG 0
#ifndef CONFIG_LIBPEER_MBEDTLS_DEBUG
#define CONFIG_LIBPEER_MBEDTLS_DEBUG 0
#endif

#ifndef CONFIG_MBEDTLS_2_X
#define CONFIG_MBEDTLS_2_X 0
#ifndef CONFIG_LIBPEER_MBEDTLS_2_X
#define CONFIG_LIBPEER_MBEDTLS_2_X 0
#endif

#if CONFIG_MBEDTLS_2_X
#if CONFIG_LIBPEER_MBEDTLS_2_X
#define RSA_KEY_LENGTH 512
#else
#define RSA_KEY_LENGTH 1024
#endif

#ifndef CONFIG_DTLS_USE_ECDSA
#define CONFIG_DTLS_USE_ECDSA 0
#ifndef CONFIG_LIBPEER_DTLS_USE_ECDSA
#define CONFIG_LIBPEER_DTLS_USE_ECDSA 0
#endif

#ifndef CONFIG_LIBPEER_USE_USRSCTP
#define CONFIG_LIBPEER_USE_USRSCTP 1
#endif

#ifndef CONFIG_USE_USRSCTP
#define CONFIG_USE_USRSCTP 1
#ifndef CONFIG_LIBPEER_VIDEO_BUFFER_SIZE_MTUS
#define CONFIG_LIBPEER_VIDEO_BUFFER_SIZE_MTUS 256
#endif
#ifndef CONFIG_LIBPEER_AUDIO_BUFFER_SIZE_MTUS
#define CONFIG_LIBPEER_AUDIO_BUFFER_SIZE_MTUS 256
#endif
#ifndef CONFIG_LIBPEER_DATA_BUFFER_SIZE_MTUS
#define CONFIG_LIBPEER_DATA_BUFFER_SIZE_MTUS 128
#endif

#ifndef CONFIG_VIDEO_BUFFER_SIZE
#define CONFIG_VIDEO_BUFFER_SIZE (CONFIG_MTU * 256)
#ifndef CONFIG_LIBPEER_VIDEO_BUFFER_SIZE
#define CONFIG_LIBPEER_VIDEO_BUFFER_SIZE (CONFIG_LIBPEER_MTU * CONFIG_LIBPEER_VIDEO_BUFFER_SIZE_MTUS)
#endif

#ifndef CONFIG_AUDIO_BUFFER_SIZE
#define CONFIG_AUDIO_BUFFER_SIZE (CONFIG_MTU * 256)
#ifndef CONFIG_LIBPEER_AUDIO_BUFFER_SIZE
#define CONFIG_LIBPEER_AUDIO_BUFFER_SIZE (CONFIG_LIBPEER_MTU * CONFIG_LIBPEER_AUDIO_BUFFER_SIZE_MTUS)
#endif

#ifndef CONFIG_DATA_BUFFER_SIZE
#define CONFIG_DATA_BUFFER_SIZE (SCTP_MTU * 128)
#ifndef CONFIG_LIBPEER_DATA_BUFFER_SIZE
#define CONFIG_LIBPEER_DATA_BUFFER_SIZE (CONFIG_LIBPEER_SCTP_MTU * CONFIG_LIBPEER_DATA_BUFFER_SIZE_MTUS)
#endif

#ifndef CONFIG_SDP_BUFFER_SIZE
#define CONFIG_SDP_BUFFER_SIZE 8096
#ifndef CONFIG_LIBPEER_SDP_BUFFER_SIZE
#define CONFIG_LIBPEER_SDP_BUFFER_SIZE 8096
#endif

#ifndef CONFIG_MQTT_BUFFER_SIZE
#define CONFIG_MQTT_BUFFER_SIZE 4096
#ifndef CONFIG_LIBPEER_MQTT_BUFFER_SIZE
#define CONFIG_LIBPEER_MQTT_BUFFER_SIZE 4096
#endif

#ifndef CONFIG_HTTP_BUFFER_SIZE
#define CONFIG_HTTP_BUFFER_SIZE 4096
#ifndef CONFIG_LIBPEER_HTTP_BUFFER_SIZE
#define CONFIG_LIBPEER_HTTP_BUFFER_SIZE 4096
#endif

#ifndef CONFIG_TLS_READ_TIMEOUT
#define CONFIG_TLS_READ_TIMEOUT 3000
#ifndef CONFIG_LIBPEER_TLS_READ_TIMEOUT
#define CONFIG_LIBPEER_TLS_READ_TIMEOUT 3000
#endif

#define AUDIO_LATENCY 20 // ms
#define KEEPALIVE_CONNCHECK 10000
#define CONFIG_IPV6 0
#define CONFIG_LIBPEER_AUDIO_LATENCY 20 // ms
#define CONFIG_LIBPEER_KEEPALIVE_CONNCHECK 10000
#define CONFIG_LIBPEER_IPV6 0
// empty will use first active interface
#define CONFIG_IFACE_PREFIX ""
#define CONFIG_LIBPEER_IFACE_PREFIX ""

// #define LOG_LEVEL LEVEL_DEBUG
#define LOG_REDIRECT 0
#define CONFIG_LIBPEER_LOG_REDIRECT 0

#ifdef CONFIG_LIBPEER_LOG_LEVEL_ERROR
#define LOG_LEVEL LEVEL_ERROR
#endif // CONFIG_LIBPEER_LOG_LEVEL_ERROR
#ifdef CONFIG_LIBPEER_LOG_LEVEL_WARN
#define LOG_LEVEL LEVEL_WARN
#endif // CONFIG_LIBPEER_LOG_LEVEL_WARN
#ifdef CONFIG_LIBPEER_LOG_LEVEL_INFO
#define LOG_LEVEL LEVEL_INFO
#endif // CONFIG_LIBPEER_LOG_LEVEL_INFO
#ifdef CONFIG_LIBPEER_LOG_LEVEL_DEBUG
#define LOG_LEVEL LEVEL_DEBUG
#endif // CONFIG_LIBPEER_LOG_LEVEL_DEBUG

// Disable MQTT and HTTP signaling
// #define DISABLE_PEER_SIGNALING 1
// #define CONFIG_LIBPEER_DISABLE_PEER_SIGNALING 1

#endif // CONFIG_H_
18 changes: 9 additions & 9 deletions src/dtls_srtp.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include "address.h"
#include "config.h"
#include "dtls_srtp.h"
#if CONFIG_MBEDTLS_DEBUG
#if CONFIG_LIBPEER_MBEDTLS_DEBUG
#include "mbedtls/debug.h"
#endif
#include "mbedtls/sha256.h"
Expand Down Expand Up @@ -72,7 +72,7 @@ static int dtls_srtp_selfsign_cert(DtlsSrtp* dtls_srtp) {
mbedtls_x509write_cert crt;

unsigned char* cert_buf = NULL;
#if CONFIG_MBEDTLS_2_X
#if CONFIG_LIBPEER_MBEDTLS_2_X
mbedtls_mpi serial;
#else
const char* serial = "peer";
Expand All @@ -87,7 +87,7 @@ static int dtls_srtp_selfsign_cert(DtlsSrtp* dtls_srtp) {

mbedtls_ctr_drbg_seed(&dtls_srtp->ctr_drbg, mbedtls_entropy_func, &dtls_srtp->entropy, (const unsigned char*)pers, strlen(pers));

#if CONFIG_DTLS_USE_ECDSA
#if CONFIG_LIBPEER_DTLS_USE_ECDSA
mbedtls_pk_setup(&dtls_srtp->pkey, mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY));
mbedtls_ecp_gen_key(MBEDTLS_ECP_DP_SECP256R1, mbedtls_pk_ec(dtls_srtp->pkey), mbedtls_ctr_drbg_random, &dtls_srtp->ctr_drbg);
#else
Expand All @@ -111,7 +111,7 @@ static int dtls_srtp_selfsign_cert(DtlsSrtp* dtls_srtp) {

mbedtls_x509write_crt_set_issuer_name(&crt, "CN=dtls_srtp");

#if CONFIG_MBEDTLS_2_X
#if CONFIG_LIBPEER_MBEDTLS_2_X
mbedtls_mpi_init(&serial);
mbedtls_mpi_fill_random(&serial, 16, mbedtls_ctr_drbg_random, &dtls_srtp->ctr_drbg);
ret = mbedtls_x509write_crt_set_serial(&crt, &serial);
Expand Down Expand Up @@ -139,7 +139,7 @@ static int dtls_srtp_selfsign_cert(DtlsSrtp* dtls_srtp) {
return ret;
}

#if CONFIG_MBEDTLS_DEBUG
#if CONFIG_LIBPEER_MBEDTLS_DEBUG
static void dtls_srtp_debug(void* ctx, int level, const char* file, int line, const char* str) {
LOGD("%s:%04d: %s", file, line, str);
}
Expand All @@ -166,7 +166,7 @@ int dtls_srtp_init(DtlsSrtp* dtls_srtp, DtlsSrtpRole role, void* user_data) {
mbedtls_pk_init(&dtls_srtp->pkey);
mbedtls_entropy_init(&dtls_srtp->entropy);
mbedtls_ctr_drbg_init(&dtls_srtp->ctr_drbg);
#if CONFIG_MBEDTLS_DEBUG
#if CONFIG_LIBPEER_MBEDTLS_DEBUG
mbedtls_debug_set_threshold(3);
mbedtls_ssl_conf_dbg(&dtls_srtp->conf, dtls_srtp_debug, NULL);
#endif
Expand Down Expand Up @@ -314,7 +314,7 @@ static int dtls_srtp_key_derivation(DtlsSrtp* dtls_srtp, const unsigned char* ma
return 0;
}

#if CONFIG_MBEDTLS_2_X
#if CONFIG_LIBPEER_MBEDTLS_2_X
static int dtls_srtp_key_derivation_cb(void* context,
const unsigned char* ms,
const unsigned char* kb,
Expand All @@ -341,7 +341,7 @@ static void dtls_srtp_key_derivation_cb(void* context,
memcpy(randbytes, client_random, 32);
memcpy(randbytes + 32, server_random, 32);

#if CONFIG_MBEDTLS_2_X
#if CONFIG_LIBPEER_MBEDTLS_2_X
memcpy(master_secret, ms, sizeof(master_secret));
return dtls_srtp_key_derivation(dtls_srtp, master_secret, sizeof(master_secret), randbytes, sizeof(randbytes), tls_prf_type);
#else
Expand All @@ -357,7 +357,7 @@ static int dtls_srtp_do_handshake(DtlsSrtp* dtls_srtp) {

mbedtls_ssl_set_timer_cb(&dtls_srtp->ssl, &timer, mbedtls_timing_set_delay, mbedtls_timing_get_delay);

#if CONFIG_MBEDTLS_2_X
#if CONFIG_LIBPEER_MBEDTLS_2_X
mbedtls_ssl_conf_export_keys_ext_cb(&dtls_srtp->conf, dtls_srtp_key_derivation_cb, dtls_srtp);
#else
mbedtls_ssl_set_export_keys_cb(&dtls_srtp->ssl, dtls_srtp_key_derivation_cb, dtls_srtp);
Expand Down
Loading

0 comments on commit fcb046d

Please sign in to comment.