Forráskód Böngészése

Add MumbleUser and MumbleChannel

Auzan 6 éve
szülő
commit
218f81d5be
5 módosított fájl, 134 hozzáadás és 9 törlés
  1. 22 1
      include/mumlib.hpp
  2. 6 1
      include/mumlib/CryptState.hpp
  3. 36 2
      src/CryptState.cpp
  4. 0 1
      src/Transport.cpp
  5. 70 4
      src/mumlib.cpp

+ 22 - 1
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<MumbleUser> getListUser();
+
+        vector<MumbleChannel> 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);
     };
 }

+ 6 - 1
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);

+ 36 - 2
src/CryptState.cpp

@@ -40,6 +40,7 @@
 
 #include "mumlib/CryptState.hpp"
 
+#include <openssl/rand.h>
 #include <cstring>
 #include <cstdint>
 
@@ -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<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)
 
+#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) {

+ 0 - 1
src/Transport.cpp

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

+ 70 - 4
src/mumlib.cpp

@@ -34,6 +34,9 @@ namespace mumlib {
         int sessionId = 0;
         int channelId = 0;
 
+        std::vector<MumbleUser> listMumbleUser;
+        std::vector<MumbleChannel> 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::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) {
         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<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;
+    }
 }