From 218f81d5be8ed17de76c7c3f97ca141d27a82e2b Mon Sep 17 00:00:00 2001 From: Auzan Date: Wed, 18 Apr 2018 12:43:52 +0700 Subject: [PATCH] Add MumbleUser and MumbleChannel --- include/mumlib.hpp | 23 ++++++++++- include/mumlib/CryptState.hpp | 7 +++- src/CryptState.cpp | 38 +++++++++++++++++- src/Transport.cpp | 1 - src/mumlib.cpp | 74 +++++++++++++++++++++++++++++++++-- 5 files changed, 134 insertions(+), 9 deletions(-) diff --git a/include/mumlib.hpp b/include/mumlib.hpp index ee18567..96b2165 100644 --- a/include/mumlib.hpp +++ b/include/mumlib.hpp @@ -29,6 +29,17 @@ namespace mumlib { // additional fields will be added in the future }; + struct MumbleUser { + int32_t sessionId; + string name; + }; + + struct MumbleChannel { + int32_t channelId; + string name; + string description; + }; + struct _Mumlib_Private; @@ -54,6 +65,10 @@ namespace mumlib { int getChannelId(); + vector getListUser(); + + vector getListChannel(); + void sendAudioData(int16_t *pcmData, int pcmLength); void sendAudioDataTarget(int targetId, int16_t *pcmData, int pcmLength); @@ -62,13 +77,19 @@ namespace mumlib { void joinChannel(int channelId); + void joinChannel(std::string channelName); + void sendVoiceTarget(int targetId, int channelId); + void sendVoiceTarget(int targetId, std::string channelName); + void sendUserState(mumlib::UserState state, bool val); void sendUserState(mumlib::UserState state, std::string value); - + private: _Mumlib_Private *impl; + + int getListChannelIdBy(std::string channelName); }; } diff --git a/include/mumlib/CryptState.hpp b/include/mumlib/CryptState.hpp index cf5b61f..5d53c73 100644 --- a/include/mumlib/CryptState.hpp +++ b/include/mumlib/CryptState.hpp @@ -36,7 +36,7 @@ namespace mumlib { class CryptState : boost::noncopyable { - public: + private: unsigned char raw_key[AES_BLOCK_SIZE]; unsigned char encrypt_iv[AES_BLOCK_SIZE]; unsigned char decrypt_iv[AES_BLOCK_SIZE]; @@ -56,14 +56,19 @@ namespace mumlib { AES_KEY decrypt_key; bool bInit; + public: CryptState(); bool isValid() const; + void genKey(); + void setKey(const unsigned char *rkey, const unsigned char *eiv, const unsigned char *div); void setDecryptIV(const unsigned char *iv); + const unsigned char* getEncryptIV() const; + void ocb_encrypt(const unsigned char *plain, unsigned char *encrypted, unsigned int len, const unsigned char *nonce, unsigned char *tag); diff --git a/src/CryptState.cpp b/src/CryptState.cpp index 467955d..55ca26d 100644 --- a/src/CryptState.cpp +++ b/src/CryptState.cpp @@ -40,6 +40,7 @@ #include "mumlib/CryptState.hpp" +#include #include #include @@ -57,6 +58,15 @@ bool mumlib::CryptState::isValid() const { return bInit; } +void mumlib::CryptState::genKey() { + RAND_bytes(raw_key, AES_BLOCK_SIZE); + RAND_bytes(encrypt_iv, AES_BLOCK_SIZE); + RAND_bytes(decrypt_iv, AES_BLOCK_SIZE); + AES_set_encrypt_key(raw_key, 128, &encrypt_key); + AES_set_decrypt_key(raw_key, 128, &decrypt_key); + bInit = true; +} + void mumlib::CryptState::setKey(const unsigned char *rkey, const unsigned char *eiv, const unsigned char *div) { memcpy(raw_key, rkey, AES_BLOCK_SIZE); memcpy(encrypt_iv, eiv, AES_BLOCK_SIZE); @@ -70,6 +80,10 @@ void mumlib::CryptState::setDecryptIV(const unsigned char *iv) { memcpy(decrypt_iv, iv, AES_BLOCK_SIZE); } +const unsigned char* mumlib::CryptState::getEncryptIV() const { + return encrypt_iv; +} + void mumlib::CryptState::encrypt(const unsigned char *source, unsigned char *dst, unsigned int plain_length) { unsigned char tag[AES_BLOCK_SIZE]; @@ -177,14 +191,34 @@ bool mumlib::CryptState::decrypt(const unsigned char *source, unsigned char *dst return true; } +#if defined(__LP64__) + #define BLOCKSIZE 2 #define SHIFTBITS 63 typedef uint64_t subblock; -// #define SWAP64(x) ({register uint64_t __out, __in = (x); __asm__("bswap %q0" : "=r"(__out) : "0"(__in)); __out;}) -#define SWAP64(x) (__builtin_bswap64(x)) +#ifdef __x86_64__ +static inline uint64_t SWAP64(register uint64_t __in) { register uint64_t __out; __asm__("bswap %q0" : "=r"(__out) : "0"(__in)); return __out; } +#else +#define SWAP64(x) ((static_cast(x) << 56) | \ + ((static_cast(x) << 40) & 0xff000000000000ULL) | \ + ((static_cast(x) << 24) & 0xff0000000000ULL) | \ + ((static_cast(x) << 8) & 0xff00000000ULL) | \ + ((static_cast(x) >> 8) & 0xff000000ULL) | \ + ((static_cast(x) >> 24) & 0xff0000ULL) | \ + ((static_cast(x) >> 40) & 0xff00ULL) | \ + ((static_cast(x) >> 56))) +#endif +// #define SWAP64(x) (__builtin_bswap64(x)) #define SWAPPED(x) SWAP64(x) +#else +#define BLOCKSIZE 4 +#define SHIFTBITS 31 +typedef uint32_t subblock; +#define SWAPPED(x) htonl(x) +#endif + typedef subblock keyblock[BLOCKSIZE]; static void inline XOR(subblock *dst, const subblock *a, const subblock *b) { diff --git a/src/Transport.cpp b/src/Transport.cpp index b3803f6..01b712a 100644 --- a/src/Transport.cpp +++ b/src/Transport.cpp @@ -3,7 +3,6 @@ #include "Mumble.pb.h" #include -#include using namespace std; diff --git a/src/mumlib.cpp b/src/mumlib.cpp index e21d1e1..ff3c76b 100644 --- a/src/mumlib.cpp +++ b/src/mumlib.cpp @@ -34,6 +34,9 @@ namespace mumlib { int sessionId = 0; int channelId = 0; + std::vector listMumbleUser; + std::vector listMumbleChannel; + _Mumlib_Private(Callback &callback, MumlibConfiguration &configuration) : _Mumlib_Private(callback, *(new io_service()), configuration) { externalIoService = false; @@ -154,7 +157,14 @@ namespace mumlib { links_remove.push_back(channelState.links_remove(i)); } - // this->channelId = channel_id; + MumbleChannel mumbleChannel; + mumbleChannel.channelId = channel_id; + mumbleChannel.name = channelState.name(); + mumbleChannel.description = channelState.description(); + + if(not isListChannelContains(channel_id)) { + listMumbleChannel.push_back(mumbleChannel); + } callback.channelState( channelState.name(), @@ -177,6 +187,10 @@ namespace mumlib { bool ban = user_remove.has_ban() ? user_remove.ban() : false; //todo make sure it's correct to assume it's false + if(isListUserContains(user_remove.session())) { + listUserRemovedBy(user_remove.session()); + } + callback.userRemove( user_remove.session(), actor, @@ -205,6 +219,15 @@ namespace mumlib { if(session == this->sessionId) { this->channelId = channel_id; } + + MumbleUser mumbleUser; + mumbleUser.name = userState.name(); + mumbleUser.sessionId = session; + + if(not isListUserContains(session)) { + listMumbleUser.push_back(mumbleUser); + } + callback.userState(session, actor, userState.name(), @@ -339,7 +362,25 @@ namespace mumlib { return true; } + bool isListUserContains(int sessionId) { + for(int i = 0; i < listMumbleUser.size(); i++) + if(listMumbleUser[i].sessionId == sessionId) + return true; + return false; + } + void listUserRemovedBy(int sessionId) { + for(int i = 0; i < listMumbleUser.size(); i++) + if(listMumbleUser[i].sessionId == sessionId) + listMumbleUser.erase(listMumbleUser.begin() + i); + } + + bool isListChannelContains(int channelId) { + for(int i = 0; i < listMumbleChannel.size(); i++) + if(listMumbleChannel[i].channelId == channelId) + return true; + return false; + } }; Mumlib::Mumlib(Callback &callback) { @@ -372,6 +413,14 @@ namespace mumlib { return impl->channelId; } + vector Mumlib::getListUser() { + return impl->listMumbleUser; + } + + vector Mumlib::getListChannel() { + return impl->listMumbleChannel; + } + void Mumlib::connect(string host, int port, string user, string password) { impl->transport.connect(host, port, user, password); } @@ -394,9 +443,7 @@ namespace mumlib { } void Mumlib::sendAudioData(int16_t *pcmData, int pcmLength) { - uint8_t encodedData[5000]; - int length = impl->audio.encodeAudioPacket(0, pcmData, pcmLength, encodedData, 5000); - impl->transport.sendEncodedAudioPacket(encodedData, length); + Mumlib::sendAudioDataTarget(0, pcmData, pcmLength); } void Mumlib::sendAudioDataTarget(int targetId, int16_t *pcmData, int pcmLength) { @@ -420,6 +467,11 @@ namespace mumlib { impl->channelId = channelId; } + void Mumlib::joinChannel(string name) { + int channelId = Mumlib::getListChannelIdBy(name); + Mumlib::joinChannel(channelId); + } + void Mumlib::sendVoiceTarget(int targetId, int channelId) { MumbleProto::VoiceTarget voiceTarget; MumbleProto::VoiceTarget_Target voiceTargetTarget; @@ -430,6 +482,11 @@ namespace mumlib { impl->transport.sendControlMessage(MessageType::VOICETARGET, voiceTarget); } + void Mumlib::sendVoiceTarget(int targetId, string channelName) { + int channelId = Mumlib::getListChannelIdBy(channelName); + Mumlib::sendVoiceTarget(targetId, channelId); + } + void Mumlib::sendUserState(mumlib::UserState field, bool val) { MumbleProto::UserState userState; @@ -493,4 +550,13 @@ namespace mumlib { impl->transport.sendControlMessage(MessageType::USERSTATE, userState); } + + int Mumlib::getListChannelIdBy(string name) { + vector listMumbleChannel = impl->listMumbleChannel; + int channelId = impl->channelId; + for(int i = 0; i < listMumbleChannel.size(); i++) + if(listMumbleChannel[i].name == name) + channelId = listMumbleChannel[i].channelId; + return channelId; + } }