Add MumbleUser and MumbleChannel

This commit is contained in:
Auzan 2018-04-18 12:43:52 +07:00
parent 38f4805660
commit 218f81d5be
5 changed files with 134 additions and 9 deletions

View File

@ -29,6 +29,17 @@ namespace mumlib {
// additional fields will be added in the future // 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; struct _Mumlib_Private;
@ -54,6 +65,10 @@ namespace mumlib {
int getChannelId(); int getChannelId();
vector<MumbleUser> getListUser();
vector<MumbleChannel> getListChannel();
void sendAudioData(int16_t *pcmData, int pcmLength); void sendAudioData(int16_t *pcmData, int pcmLength);
void sendAudioDataTarget(int targetId, 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(int channelId);
void joinChannel(std::string channelName);
void sendVoiceTarget(int targetId, int channelId); 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, bool val);
void sendUserState(mumlib::UserState state, std::string value); void sendUserState(mumlib::UserState state, std::string value);
private: private:
_Mumlib_Private *impl; _Mumlib_Private *impl;
int getListChannelIdBy(std::string channelName);
}; };
} }

View File

@ -36,7 +36,7 @@
namespace mumlib { namespace mumlib {
class CryptState : boost::noncopyable { class CryptState : boost::noncopyable {
public: private:
unsigned char raw_key[AES_BLOCK_SIZE]; unsigned char raw_key[AES_BLOCK_SIZE];
unsigned char encrypt_iv[AES_BLOCK_SIZE]; unsigned char encrypt_iv[AES_BLOCK_SIZE];
unsigned char decrypt_iv[AES_BLOCK_SIZE]; unsigned char decrypt_iv[AES_BLOCK_SIZE];
@ -56,14 +56,19 @@ namespace mumlib {
AES_KEY decrypt_key; AES_KEY decrypt_key;
bool bInit; bool bInit;
public:
CryptState(); CryptState();
bool isValid() const; bool isValid() const;
void genKey();
void setKey(const unsigned char *rkey, const unsigned char *eiv, const unsigned char *div); void setKey(const unsigned char *rkey, const unsigned char *eiv, const unsigned char *div);
void setDecryptIV(const unsigned char *iv); void setDecryptIV(const unsigned char *iv);
const unsigned char* getEncryptIV() const;
void ocb_encrypt(const unsigned char *plain, unsigned char *encrypted, unsigned int len, void ocb_encrypt(const unsigned char *plain, unsigned char *encrypted, unsigned int len,
const unsigned char *nonce, const unsigned char *nonce,
unsigned char *tag); unsigned char *tag);

View File

@ -40,6 +40,7 @@
#include "mumlib/CryptState.hpp" #include "mumlib/CryptState.hpp"
#include <openssl/rand.h>
#include <cstring> #include <cstring>
#include <cstdint> #include <cstdint>
@ -57,6 +58,15 @@ bool mumlib::CryptState::isValid() const {
return bInit; 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) { void mumlib::CryptState::setKey(const unsigned char *rkey, const unsigned char *eiv, const unsigned char *div) {
memcpy(raw_key, rkey, AES_BLOCK_SIZE); memcpy(raw_key, rkey, AES_BLOCK_SIZE);
memcpy(encrypt_iv, eiv, 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); 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) { void mumlib::CryptState::encrypt(const unsigned char *source, unsigned char *dst, unsigned int plain_length) {
unsigned char tag[AES_BLOCK_SIZE]; unsigned char tag[AES_BLOCK_SIZE];
@ -177,14 +191,34 @@ bool mumlib::CryptState::decrypt(const unsigned char *source, unsigned char *dst
return true; return true;
} }
#if defined(__LP64__)
#define BLOCKSIZE 2 #define BLOCKSIZE 2
#define SHIFTBITS 63 #define SHIFTBITS 63
typedef uint64_t subblock; typedef uint64_t subblock;
// #define SWAP64(x) ({register uint64_t __out, __in = (x); __asm__("bswap %q0" : "=r"(__out) : "0"(__in)); __out;}) #ifdef __x86_64__
#define SWAP64(x) (__builtin_bswap64(x)) 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<uint64_t>(x) << 56) | \
((static_cast<uint64_t>(x) << 40) & 0xff000000000000ULL) | \
((static_cast<uint64_t>(x) << 24) & 0xff0000000000ULL) | \
((static_cast<uint64_t>(x) << 8) & 0xff00000000ULL) | \
((static_cast<uint64_t>(x) >> 8) & 0xff000000ULL) | \
((static_cast<uint64_t>(x) >> 24) & 0xff0000ULL) | \
((static_cast<uint64_t>(x) >> 40) & 0xff00ULL) | \
((static_cast<uint64_t>(x) >> 56)))
#endif
// #define SWAP64(x) (__builtin_bswap64(x))
#define SWAPPED(x) SWAP64(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]; typedef subblock keyblock[BLOCKSIZE];
static void inline XOR(subblock *dst, const subblock *a, const subblock *b) { static void inline XOR(subblock *dst, const subblock *a, const subblock *b) {

View File

@ -3,7 +3,6 @@
#include "Mumble.pb.h" #include "Mumble.pb.h"
#include <boost/format.hpp> #include <boost/format.hpp>
#include <utility>
using namespace std; using namespace std;

View File

@ -34,6 +34,9 @@ namespace mumlib {
int sessionId = 0; int sessionId = 0;
int channelId = 0; int channelId = 0;
std::vector<MumbleUser> listMumbleUser;
std::vector<MumbleChannel> listMumbleChannel;
_Mumlib_Private(Callback &callback, MumlibConfiguration &configuration) _Mumlib_Private(Callback &callback, MumlibConfiguration &configuration)
: _Mumlib_Private(callback, *(new io_service()), configuration) { : _Mumlib_Private(callback, *(new io_service()), configuration) {
externalIoService = false; externalIoService = false;
@ -154,7 +157,14 @@ namespace mumlib {
links_remove.push_back(channelState.links_remove(i)); 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( callback.channelState(
channelState.name(), channelState.name(),
@ -177,6 +187,10 @@ namespace mumlib {
bool ban = user_remove.has_ban() ? user_remove.ban() bool ban = user_remove.has_ban() ? user_remove.ban()
: false; //todo make sure it's correct to assume it's false : false; //todo make sure it's correct to assume it's false
if(isListUserContains(user_remove.session())) {
listUserRemovedBy(user_remove.session());
}
callback.userRemove( callback.userRemove(
user_remove.session(), user_remove.session(),
actor, actor,
@ -205,6 +219,15 @@ namespace mumlib {
if(session == this->sessionId) { if(session == this->sessionId) {
this->channelId = channel_id; this->channelId = channel_id;
} }
MumbleUser mumbleUser;
mumbleUser.name = userState.name();
mumbleUser.sessionId = session;
if(not isListUserContains(session)) {
listMumbleUser.push_back(mumbleUser);
}
callback.userState(session, callback.userState(session,
actor, actor,
userState.name(), userState.name(),
@ -339,7 +362,25 @@ namespace mumlib {
return true; 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) { Mumlib::Mumlib(Callback &callback) {
@ -372,6 +413,14 @@ namespace mumlib {
return impl->channelId; return impl->channelId;
} }
vector<mumlib::MumbleUser> Mumlib::getListUser() {
return impl->listMumbleUser;
}
vector<mumlib::MumbleChannel> Mumlib::getListChannel() {
return impl->listMumbleChannel;
}
void Mumlib::connect(string host, int port, string user, string password) { void Mumlib::connect(string host, int port, string user, string password) {
impl->transport.connect(host, port, user, password); impl->transport.connect(host, port, user, password);
} }
@ -394,9 +443,7 @@ namespace mumlib {
} }
void Mumlib::sendAudioData(int16_t *pcmData, int pcmLength) { void Mumlib::sendAudioData(int16_t *pcmData, int pcmLength) {
uint8_t encodedData[5000]; Mumlib::sendAudioDataTarget(0, pcmData, pcmLength);
int length = impl->audio.encodeAudioPacket(0, pcmData, pcmLength, encodedData, 5000);
impl->transport.sendEncodedAudioPacket(encodedData, length);
} }
void Mumlib::sendAudioDataTarget(int targetId, int16_t *pcmData, int pcmLength) { void Mumlib::sendAudioDataTarget(int targetId, int16_t *pcmData, int pcmLength) {
@ -420,6 +467,11 @@ namespace mumlib {
impl->channelId = channelId; impl->channelId = channelId;
} }
void Mumlib::joinChannel(string name) {
int channelId = Mumlib::getListChannelIdBy(name);
Mumlib::joinChannel(channelId);
}
void Mumlib::sendVoiceTarget(int targetId, int channelId) { void Mumlib::sendVoiceTarget(int targetId, int channelId) {
MumbleProto::VoiceTarget voiceTarget; MumbleProto::VoiceTarget voiceTarget;
MumbleProto::VoiceTarget_Target voiceTargetTarget; MumbleProto::VoiceTarget_Target voiceTargetTarget;
@ -430,6 +482,11 @@ namespace mumlib {
impl->transport.sendControlMessage(MessageType::VOICETARGET, voiceTarget); 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) { void Mumlib::sendUserState(mumlib::UserState field, bool val) {
MumbleProto::UserState userState; MumbleProto::UserState userState;
@ -493,4 +550,13 @@ namespace mumlib {
impl->transport.sendControlMessage(MessageType::USERSTATE, userState); impl->transport.sendControlMessage(MessageType::USERSTATE, userState);
} }
int Mumlib::getListChannelIdBy(string name) {
vector<mumlib::MumbleChannel> 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;
}
} }