Reorganised project to separate public from private headers

This commit is contained in:
Paul-Louis Ageneau
2019-09-01 16:06:49 +02:00
parent 6a874f39f9
commit 6e3ba6afd7
21 changed files with 102 additions and 53 deletions

View File

@ -7,8 +7,8 @@ RM=rm -f
CPPFLAGS=-O2 -pthread -fPIC -Wall -Wno-address-of-packed-member
CXXFLAGS=-std=c++17
LDFLAGS=-pthread
LDLIBS=$(shell pkg-config --libs glib-2.0 gobject-2.0 nice) -lgnutls
INCLUDES=$(shell pkg-config --cflags glib-2.0 gobject-2.0 nice) -I$(USRSCTP_DIR)/usrsctplib
LDLIBS= -lgnutls $(shell pkg-config --libs glib-2.0 gobject-2.0 nice)
INCLUDES=-Iinclude/rtc -I$(USRSCTP_DIR)/usrsctplib $(shell pkg-config --cflags glib-2.0 gobject-2.0 nice)
USRSCTP_DIR:=usrsctp
USRSCTP_DEFINES:=-DINET -DINET6
@ -22,7 +22,7 @@ src/%.o: src/%.cpp
$(CXX) $(CXXFLAGS) $(CPPFLAGS) $(INCLUDES) $(USRSCTP_DEFINES) -MMD -MP -o $@ -c $<
test/%.o: test/%.cpp
$(CXX) $(CXXFLAGS) $(CPPFLAGS) -Isrc -MMD -MP -o $@ -c $<
$(CXX) $(CXXFLAGS) $(CPPFLAGS) -Iinclude -MMD -MP -o $@ -c $<
-include $(subst .o,.d,$(OBJS))
@ -36,6 +36,7 @@ tests: $(NAME).a test/main.o
$(CXX) $(LDFLAGS) -o $@ test/main.o $(LDLIBS) $(NAME).a libusrsctp.a
clean:
-$(RM) include/rtc/*.d *.d
-$(RM) src/*.o src/*.d
-$(RM) test/*.o test/*.d
@ -44,6 +45,7 @@ dist-clean: clean
-$(RM) $(NAME).so
-$(RM) libusrsctp.a
-$(RM) tests
-$(RM) include/*~
-$(RM) src/*~
-$(RM) test/*~
-cd $(USRSCTP_DIR) && make clean
@ -52,6 +54,6 @@ libusrsctp.a:
cd $(USRSCTP_DIR) && \
./bootstrap && \
./configure --enable-static --disable-debug CFLAGS="$(CPPFLAGS)" && \
$(MAKE)
make
cp $(USRSCTP_DIR)/usrsctplib/.libs/libusrsctp.a .

View File

@ -21,7 +21,6 @@
#include "include.hpp"
#include "message.hpp"
#include "queue.hpp"
#include <vector>

View File

@ -20,7 +20,6 @@
#define RTC_PEER_CONNECTION_H
#include "candidate.hpp"
#include "certificate.hpp"
#include "datachannel.hpp"
#include "description.hpp"
#include "iceconfiguration.hpp"
@ -30,12 +29,11 @@
#include <atomic>
#include <functional>
#include <memory>
#include <optional>
#include <unordered_map>
namespace rtc {
class Certificate;
class IceTransport;
class DtlsTransport;
class SctpTransport;
@ -46,7 +44,6 @@ public:
~PeerConnection();
const IceConfiguration *config() const;
const Certificate *certificate() const;
std::optional<Description> localDescription() const;
std::optional<Description> remoteDescription() const;
@ -75,7 +72,7 @@ private:
void triggerDataChannel(std::shared_ptr<DataChannel> dataChannel);
const IceConfiguration mConfig;
const Certificate mCertificate;
const std::shared_ptr<Certificate> mCertificate;
std::optional<Description> mLocalDescription;
std::optional<Description> mRemoteDescription;

45
include/rtc/rtc.h Normal file
View File

@ -0,0 +1,45 @@
/**
* Copyright (c) 2019 Paul-Louis Ageneau
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifdef __cplusplus
extern "C" {
#endif
// libdatachannel rtc C API
int rtcCreatePeerConnection(const char **iceServers, int iceServersCount);
void rtcDeletePeerConnection(int pc);
int rtcCreateDataChannel(int pc, const char *label);
void rtcDeleteDataChannel(int dc);
void rtcSetDataChannelCallback(int pc, void (*dataChannelCallback)(int, void *));
void rtcSetLocalDescriptionCallback(int pc, void (*descriptionCallback)(const char *, const char *,
void *));
void rtcSetLocalCandidateCallback(int pc,
void (*candidateCallback)(const char *, const char *, void *));
void rtcSetRemoteDescription(int pc, const char *sdp, const char *type);
void rtcSetRemoteCandidate(int pc, const char *candidate, const char *sdpMid);
int rtcGetDataChannelLabel(int dc, char *data, int size);
void rtcSetOpenCallback(int dc, void (*openCallback)(void *));
void rtcSetErrorCallback(int dc, void (*errorCallback)(const char *, void *));
void rtcSetMessageCallback(int dc, void (*messageCallback)(const char *, int, void *));
int rtcSendMessage(int dc, const char *buffer, int size);
void rtcSetUserPointer(int i, void *ptr);
#ifdef __cplusplus
} // extern "C"
#endif

25
include/rtc/rtc.hpp Normal file
View File

@ -0,0 +1,25 @@
/**
* Copyright (c) 2019 Paul-Louis Ageneau
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
// C++ API
#include "datachannel.hpp"
#include "peerconnection.hpp"
// C API
#include "rtc.h"

View File

@ -27,6 +27,7 @@
#include <gnutls/crypto.h>
using std::shared_ptr;
using std::string;
namespace {
@ -137,8 +138,8 @@ string make_fingerprint(gnutls_x509_crt_t crt) {
return oss.str();
}
Certificate make_certificate(const string &commonName) {
static std::unordered_map<string, Certificate> cache;
shared_ptr<Certificate> make_certificate(const string &commonName) {
static std::unordered_map<string, shared_ptr<Certificate>> cache;
static std::mutex cacheMutex;
std::lock_guard<std::mutex> lock(cacheMutex);
@ -170,8 +171,9 @@ Certificate make_certificate(const string &commonName) {
check_gnutls(gnutls_x509_crt_sign2(*crt, *crt, *privkey, GNUTLS_DIG_SHA256, 0),
"Unable to auto-sign certificate");
auto it = cache.emplace(std::make_pair(commonName, Certificate(*crt, *privkey))).first;
return it->second;
auto certificate = std::make_shared<Certificate>(*crt, *privkey);
cache.emplace(std::make_pair(commonName, certificate));
return certificate;
}
} // namespace rtc

View File

@ -21,9 +21,6 @@
#include "include.hpp"
#include <memory>
#include <string>
#include <gnutls/x509.h>
namespace rtc {
@ -42,7 +39,7 @@ private:
};
string make_fingerprint(gnutls_x509_crt_t crt);
Certificate make_certificate(const string &commonName);
std::shared_ptr<Certificate> make_certificate(const string &commonName);
} // namespace rtc

View File

@ -25,6 +25,7 @@
#include <gnutls/dtls.h>
using std::shared_ptr;
using std::string;
namespace {
@ -44,11 +45,11 @@ namespace rtc {
using std::shared_ptr;
DtlsTransport::DtlsTransport(shared_ptr<IceTransport> lower, Certificate certificate,
DtlsTransport::DtlsTransport(shared_ptr<IceTransport> lower, shared_ptr<Certificate> certificate,
verifier_callback verifier, ready_callback ready)
: Transport(lower), mCertificate(std::move(certificate)),
mVerifierCallback(std::move(verifier)), mReadyCallback(std::move(ready)) {
gnutls_certificate_set_verify_function(mCertificate.credentials(), CertificateCallback);
: Transport(lower), mCertificate(certificate), mVerifierCallback(std::move(verifier)),
mReadyCallback(std::move(ready)) {
gnutls_certificate_set_verify_function(mCertificate->credentials(), CertificateCallback);
bool active = lower->role() == Description::Role::Active;
unsigned int flags = GNUTLS_DATAGRAM | (active ? GNUTLS_CLIENT : GNUTLS_SERVER);
@ -66,7 +67,7 @@ DtlsTransport::DtlsTransport(shared_ptr<IceTransport> lower, Certificate certifi
gnutls_transport_set_pull_timeout_function(mSession, TimeoutCallback);
check_gnutls(
gnutls_credentials_set(mSession, GNUTLS_CRD_CERTIFICATE, mCertificate.credentials()));
gnutls_credentials_set(mSession, GNUTLS_CRD_CERTIFICATE, mCertificate->credentials()));
mRecvThread = std::thread(&DtlsTransport::runRecvLoop, this);
}

View File

@ -19,6 +19,7 @@
#ifndef RTC_DTLS_TRANSPORT_H
#define RTC_DTLS_TRANSPORT_H
#include "certificate.hpp"
#include "include.hpp"
#include "peerconnection.hpp"
#include "queue.hpp"
@ -39,19 +40,17 @@ public:
using verifier_callback = std::function<bool(const std::string &fingerprint)>;
using ready_callback = std::function<void(void)>;
DtlsTransport(std::shared_ptr<IceTransport> lower, Certificate certificate,
DtlsTransport(std::shared_ptr<IceTransport> lower, std::shared_ptr<Certificate> certificate,
verifier_callback verifier, ready_callback ready);
~DtlsTransport();
const Certificate *certificate();
bool send(message_ptr message);
private:
void incoming(message_ptr message);
void runRecvLoop();
const Certificate mCertificate;
const std::shared_ptr<Certificate> mCertificate;
gnutls_session_t mSession;
Queue<message_ptr> mIncomingQueue;

View File

@ -81,7 +81,7 @@ private:
std::unique_ptr<NiceAgent, void (*)(gpointer)> mNiceAgent;
std::unique_ptr<GMainLoop, void (*)(GMainLoop *)> mMainLoop;
std::thread mMainLoopThread;
std::mutex mSendMutex;
candidate_callback mCandidateCallback;
ready_callback mReadyCallback;

View File

@ -17,6 +17,7 @@
*/
#include "peerconnection.hpp"
#include "certificate.hpp"
#include "dtlstransport.hpp"
#include "icetransport.hpp"
#include "sctptransport.hpp"
@ -37,8 +38,6 @@ PeerConnection::~PeerConnection() {}
const IceConfiguration *PeerConnection::config() const { return &mConfig; }
const Certificate *PeerConnection::certificate() const { return &mCertificate; }
std::optional<Description> PeerConnection::localDescription() const { return mLocalDescription; }
std::optional<Description> PeerConnection::remoteDescription() const { return mRemoteDescription; }
@ -168,7 +167,7 @@ void PeerConnection::openDataChannels(void) {
void PeerConnection::processLocalDescription(Description description) {
auto remoteSctpPort = mRemoteDescription ? mRemoteDescription->sctpPort() : nullopt;
description.setFingerprint(mCertificate.fingerprint());
description.setFingerprint(mCertificate->fingerprint());
description.setSctpPort(remoteSctpPort.value_or(DEFAULT_SCTP_PORT));
mLocalDescription.emplace(std::move(description));

View File

@ -19,33 +19,14 @@
#include "datachannel.hpp"
#include "peerconnection.hpp"
#include <rtc.h>
#include <unordered_map>
using namespace rtc;
using std::shared_ptr;
using std::string;
// libdatachannel C API
extern "C" {
int rtcCreatePeerConnection(const char **iceServers, int iceServersCount);
void rtcDeletePeerConnection(int pc);
int rtcCreateDataChannel(int pc, const char *label);
void rtcDeleteDataChannel(int dc);
void rtcSetDataChannelCallback(int pc, void (*dataChannelCallback)(int, void *));
void rtcSetLocalDescriptionCallback(int pc, void (*descriptionCallback)(const char *, const char *,
void *));
void rtcSetLocalCandidateCallback(int pc,
void (*candidateCallback)(const char *, const char *, void *));
void rtcSetRemoteDescription(int pc, const char *sdp, const char *type);
void rtcSetRemoteCandidate(int pc, const char *candidate, const char *sdpMid);
int rtcGetDataChannelLabel(int dc, char *data, int size);
void rtcSetOpenCallback(int dc, void (*openCallback)(void *));
void rtcSetErrorCallback(int dc, void (*errorCallback)(const char *, void *));
void rtcSetMessageCallback(int dc, void (*messageCallback)(const char *, int, void *));
int rtcSendMessage(int dc, const char *buffer, int size);
void rtcSetUserPointer(int i, void *ptr);
}
namespace {
std::unordered_map<int, shared_ptr<PeerConnection>> peerConnectionMap;

View File

@ -23,7 +23,9 @@
#include "peerconnection.hpp"
#include "transport.hpp"
#include <condition_variable>
#include <functional>
#include <mutex>
#include <thread>
#include <sys/socket.h>

View File

@ -16,7 +16,7 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <peerconnection.hpp>
#include "rtc/rtc.hpp"
#include <chrono>
#include <iostream>