diff --git a/Makefile b/Makefile index 4ab9437..bb9c24a 100644 --- a/Makefile +++ b/Makefile @@ -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 . diff --git a/src/candidate.hpp b/include/rtc/candidate.hpp similarity index 100% rename from src/candidate.hpp rename to include/rtc/candidate.hpp diff --git a/src/channel.hpp b/include/rtc/channel.hpp similarity index 100% rename from src/channel.hpp rename to include/rtc/channel.hpp diff --git a/src/datachannel.hpp b/include/rtc/datachannel.hpp similarity index 100% rename from src/datachannel.hpp rename to include/rtc/datachannel.hpp diff --git a/src/description.hpp b/include/rtc/description.hpp similarity index 100% rename from src/description.hpp rename to include/rtc/description.hpp diff --git a/src/iceconfiguration.hpp b/include/rtc/iceconfiguration.hpp similarity index 98% rename from src/iceconfiguration.hpp rename to include/rtc/iceconfiguration.hpp index e33dbe9..66cf9c7 100644 --- a/src/iceconfiguration.hpp +++ b/include/rtc/iceconfiguration.hpp @@ -21,7 +21,6 @@ #include "include.hpp" #include "message.hpp" -#include "queue.hpp" #include diff --git a/src/include.hpp b/include/rtc/include.hpp similarity index 100% rename from src/include.hpp rename to include/rtc/include.hpp diff --git a/src/message.hpp b/include/rtc/message.hpp similarity index 100% rename from src/message.hpp rename to include/rtc/message.hpp diff --git a/src/peerconnection.hpp b/include/rtc/peerconnection.hpp similarity index 95% rename from src/peerconnection.hpp rename to include/rtc/peerconnection.hpp index 320f614..15c28d4 100644 --- a/src/peerconnection.hpp +++ b/include/rtc/peerconnection.hpp @@ -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 #include -#include -#include #include 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 localDescription() const; std::optional remoteDescription() const; @@ -75,7 +72,7 @@ private: void triggerDataChannel(std::shared_ptr dataChannel); const IceConfiguration mConfig; - const Certificate mCertificate; + const std::shared_ptr mCertificate; std::optional mLocalDescription; std::optional mRemoteDescription; diff --git a/src/reliability.hpp b/include/rtc/reliability.hpp similarity index 100% rename from src/reliability.hpp rename to include/rtc/reliability.hpp diff --git a/include/rtc/rtc.h b/include/rtc/rtc.h new file mode 100644 index 0000000..65c3670 --- /dev/null +++ b/include/rtc/rtc.h @@ -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 + diff --git a/include/rtc/rtc.hpp b/include/rtc/rtc.hpp new file mode 100644 index 0000000..7bc4ba8 --- /dev/null +++ b/include/rtc/rtc.hpp @@ -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" + diff --git a/src/certificate.cpp b/src/certificate.cpp index e11b549..28bc26d 100644 --- a/src/certificate.cpp +++ b/src/certificate.cpp @@ -27,6 +27,7 @@ #include +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 cache; +shared_ptr make_certificate(const string &commonName) { + static std::unordered_map> cache; static std::mutex cacheMutex; std::lock_guard 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(*crt, *privkey); + cache.emplace(std::make_pair(commonName, certificate)); + return certificate; } } // namespace rtc diff --git a/src/certificate.hpp b/src/certificate.hpp index 2c30548..a50ddfe 100644 --- a/src/certificate.hpp +++ b/src/certificate.hpp @@ -21,9 +21,6 @@ #include "include.hpp" -#include -#include - #include namespace rtc { @@ -42,7 +39,7 @@ private: }; string make_fingerprint(gnutls_x509_crt_t crt); -Certificate make_certificate(const string &commonName); +std::shared_ptr make_certificate(const string &commonName); } // namespace rtc diff --git a/src/dtlstransport.cpp b/src/dtlstransport.cpp index 63e8be0..3edf367 100644 --- a/src/dtlstransport.cpp +++ b/src/dtlstransport.cpp @@ -25,6 +25,7 @@ #include +using std::shared_ptr; using std::string; namespace { @@ -44,11 +45,11 @@ namespace rtc { using std::shared_ptr; -DtlsTransport::DtlsTransport(shared_ptr lower, Certificate certificate, +DtlsTransport::DtlsTransport(shared_ptr lower, shared_ptr 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 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); } diff --git a/src/dtlstransport.hpp b/src/dtlstransport.hpp index bbe3f9d..c7fe4db 100644 --- a/src/dtlstransport.hpp +++ b/src/dtlstransport.hpp @@ -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; using ready_callback = std::function; - DtlsTransport(std::shared_ptr lower, Certificate certificate, + DtlsTransport(std::shared_ptr lower, std::shared_ptr 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 mCertificate; gnutls_session_t mSession; Queue mIncomingQueue; diff --git a/src/icetransport.hpp b/src/icetransport.hpp index d59f3f3..fcd5593 100644 --- a/src/icetransport.hpp +++ b/src/icetransport.hpp @@ -81,7 +81,7 @@ private: std::unique_ptr mNiceAgent; std::unique_ptr mMainLoop; std::thread mMainLoopThread; - std::mutex mSendMutex; + candidate_callback mCandidateCallback; ready_callback mReadyCallback; diff --git a/src/peerconnection.cpp b/src/peerconnection.cpp index 56fdab3..7de365b 100644 --- a/src/peerconnection.cpp +++ b/src/peerconnection.cpp @@ -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 PeerConnection::localDescription() const { return mLocalDescription; } std::optional 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)); diff --git a/src/capi.cpp b/src/rtc.cpp similarity index 84% rename from src/capi.cpp rename to src/rtc.cpp index 5d1dac2..850e090 100644 --- a/src/capi.cpp +++ b/src/rtc.cpp @@ -19,33 +19,14 @@ #include "datachannel.hpp" #include "peerconnection.hpp" +#include + #include 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> peerConnectionMap; diff --git a/src/sctptransport.hpp b/src/sctptransport.hpp index 3684c03..91a120e 100644 --- a/src/sctptransport.hpp +++ b/src/sctptransport.hpp @@ -23,7 +23,9 @@ #include "peerconnection.hpp" #include "transport.hpp" +#include #include +#include #include #include diff --git a/test/main.cpp b/test/main.cpp index 6d131a7..8cd85bd 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -16,7 +16,7 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -#include +#include "rtc/rtc.hpp" #include #include