mumlib.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. #include "mumlib/CryptState.hpp"
  2. #include "mumlib/VarInt.hpp"
  3. #include "mumlib/enums.hpp"
  4. #include "mumlib/Transport.hpp"
  5. #include "mumlib/Audio.hpp"
  6. #include "mumlib.hpp"
  7. #include <boost/asio.hpp>
  8. #include <boost/bind.hpp>
  9. #include <log4cpp/Category.hh>
  10. #include <Mumble.pb.h>
  11. using namespace std;
  12. using namespace boost::asio;
  13. using namespace mumlib;
  14. namespace mumlib {
  15. struct _Mumlib_Private : boost::noncopyable {
  16. log4cpp::Category &logger = log4cpp::Category::getInstance("mumlib.Mumlib");
  17. bool externalIoService;
  18. io_service &ioService;
  19. Callback &callback;
  20. Transport transport;
  21. Audio audio;
  22. int sessionId = 0;
  23. int channelId = 0;
  24. _Mumlib_Private(Callback &callback, MumlibConfiguration &configuration)
  25. : _Mumlib_Private(callback, *(new io_service()), configuration) {
  26. externalIoService = false;
  27. }
  28. _Mumlib_Private(Callback &callback, io_service &ioService, MumlibConfiguration &configuration)
  29. : callback(callback),
  30. ioService(ioService),
  31. externalIoService(true),
  32. transport(ioService, boost::bind(&_Mumlib_Private::processIncomingTcpMessage, this, _1, _2, _3),
  33. boost::bind(&_Mumlib_Private::processAudioPacket, this, _1, _2, _3)),
  34. audio(configuration.opusSampleRate, configuration.opusEncoderBitrate) {
  35. audio.setOpusEncoderBitrate(configuration.opusEncoderBitrate);
  36. }
  37. virtual ~_Mumlib_Private() {
  38. if (not externalIoService) {
  39. delete &ioService;
  40. }
  41. }
  42. bool processAudioPacket(AudioPacketType type, uint8_t *buffer, int length) {
  43. logger.info("Got %d B of encoded audio data.", length);
  44. try {
  45. auto incomingAudioPacket = audio.decodeIncomingAudioPacket(buffer, length);
  46. if (type == AudioPacketType::OPUS) {
  47. int16_t pcmData[5000];
  48. auto status = audio.decodeOpusPayload(incomingAudioPacket.audioPayload,
  49. incomingAudioPacket.audioPayloadLength,
  50. pcmData,
  51. 5000);
  52. callback.audio(incomingAudioPacket.target,
  53. incomingAudioPacket.sessionId,
  54. incomingAudioPacket.sequenceNumber,
  55. pcmData,
  56. status.first);
  57. } else {
  58. logger.warn("Incoming audio packet doesn't contain Opus data, calling unsupportedAudio callback.");
  59. callback.unsupportedAudio(incomingAudioPacket.target,
  60. incomingAudioPacket.sessionId,
  61. incomingAudioPacket.sequenceNumber,
  62. incomingAudioPacket.audioPayload,
  63. incomingAudioPacket.audioPayloadLength);
  64. }
  65. } catch (mumlib::AudioException &exp) {
  66. logger.error("Audio decode error: %s.", exp.what());
  67. }
  68. return true;
  69. }
  70. private:
  71. bool processIncomingTcpMessage(MessageType messageType, uint8_t *buffer, int length) {
  72. logger.debug("Process incoming message: type %d, length: %d.", messageType, length);
  73. switch (messageType) {
  74. case MessageType::VERSION: {
  75. MumbleProto::Version version;
  76. version.ParseFromArray(buffer, length);
  77. callback.version(
  78. version.version() >> 16,
  79. version.version() >> 8 & 0xff,
  80. version.version() & 0xff,
  81. version.release(),
  82. version.os(),
  83. version.os_version());
  84. }
  85. break;
  86. case MessageType::SERVERSYNC: {
  87. MumbleProto::ServerSync serverSync;
  88. serverSync.ParseFromArray(buffer, length);
  89. sessionId = serverSync.session();
  90. callback.serverSync(
  91. serverSync.welcome_text(),
  92. serverSync.session(),
  93. serverSync.max_bandwidth(),
  94. serverSync.permissions()
  95. );
  96. }
  97. break;
  98. case MessageType::CHANNELREMOVE: {
  99. MumbleProto::ChannelRemove channelRemove;
  100. channelRemove.ParseFromArray(buffer, length);
  101. callback.channelRemove(channelRemove.channel_id());
  102. }
  103. break;
  104. case MessageType::CHANNELSTATE: {
  105. MumbleProto::ChannelState channelState;
  106. channelState.ParseFromArray(buffer, length);
  107. int32_t channel_id = channelState.has_channel_id() ? channelState.channel_id() : -1;
  108. int32_t parent = channelState.has_parent() ? channelState.parent() : -1;
  109. bool temporary = channelState.has_temporary() ? channelState.temporary()
  110. : false; //todo make sure it's correct to assume it's false
  111. int position = channelState.has_position() ? channelState.position() : 0;
  112. vector<uint32_t> links;
  113. for (int i = 0; i < channelState.links_size(); ++i) {
  114. links.push_back(channelState.links(i));
  115. }
  116. vector<uint32_t> links_add;
  117. for (int i = 0; i < channelState.links_add_size(); ++i) {
  118. links_add.push_back(channelState.links_add(i));
  119. }
  120. vector<uint32_t> links_remove;
  121. for (int i = 0; i < channelState.links_remove_size(); ++i) {
  122. links_remove.push_back(channelState.links_remove(i));
  123. }
  124. // this->channelId = channel_id;
  125. callback.channelState(
  126. channelState.name(),
  127. channel_id,
  128. parent,
  129. channelState.description(),
  130. links,
  131. links_add,
  132. links_remove,
  133. temporary,
  134. position
  135. );
  136. }
  137. break;
  138. case MessageType::USERREMOVE: {
  139. MumbleProto::UserRemove user_remove;
  140. user_remove.ParseFromArray(buffer, length);
  141. int32_t actor = user_remove.has_actor() ? user_remove.actor() : -1;
  142. bool ban = user_remove.has_ban() ? user_remove.ban()
  143. : false; //todo make sure it's correct to assume it's false
  144. callback.userRemove(
  145. user_remove.session(),
  146. actor,
  147. user_remove.reason(),
  148. ban
  149. );
  150. }
  151. break;
  152. case MessageType::USERSTATE: {
  153. MumbleProto::UserState userState;
  154. userState.ParseFromArray(buffer, length);
  155. // There are far too many things in this structure. Culling to the ones that are probably important
  156. int32_t session = userState.has_session() ? userState.session() : -1;
  157. int32_t actor = userState.has_actor() ? userState.actor() : -1;
  158. int32_t user_id = userState.has_user_id() ? userState.user_id() : -1;
  159. int32_t channel_id = userState.has_channel_id() ? userState.channel_id() : -1;
  160. int32_t mute = userState.has_mute() ? userState.mute() : -1;
  161. int32_t deaf = userState.has_deaf() ? userState.deaf() : -1;
  162. int32_t suppress = userState.has_suppress() ? userState.suppress() : -1;
  163. int32_t self_mute = userState.has_self_mute() ? userState.self_mute() : -1;
  164. int32_t self_deaf = userState.has_self_deaf() ? userState.self_deaf() : -1;
  165. int32_t priority_speaker = userState.has_priority_speaker() ? userState.priority_speaker() : -1;
  166. int32_t recording = userState.has_recording() ? userState.recording() : -1;
  167. callback.userState(session,
  168. actor,
  169. userState.name(),
  170. user_id,
  171. channel_id,
  172. mute,
  173. deaf,
  174. suppress,
  175. self_mute,
  176. self_deaf,
  177. userState.comment(),
  178. priority_speaker,
  179. recording);
  180. }
  181. break;
  182. case MessageType::BANLIST: {
  183. MumbleProto::BanList ban_list;
  184. ban_list.ParseFromArray(buffer, length);
  185. for (int i = 0; i < ban_list.bans_size(); i++) {
  186. auto ban = ban_list.bans(i);
  187. const uint8_t *ip_data = reinterpret_cast<const uint8_t *>(ban.address().c_str());
  188. uint32_t ip_data_size = ban.address().size();
  189. int32_t duration = ban.has_duration() ? ban.duration() : -1;
  190. callback.banList(
  191. ip_data,
  192. ip_data_size,
  193. ban.mask(),
  194. ban.name(),
  195. ban.hash(),
  196. ban.reason(),
  197. ban.start(),
  198. duration);
  199. }
  200. }
  201. break;
  202. case MessageType::TEXTMESSAGE: {
  203. MumbleProto::TextMessage text_message;
  204. text_message.ParseFromArray(buffer, length);
  205. int32_t actor = text_message.has_actor() ? text_message.actor() : -1;
  206. vector<uint32_t> sessions;
  207. for (int i = 0; i < text_message.session_size(); ++i) {
  208. sessions.push_back(text_message.session(i));
  209. }
  210. vector<uint32_t> channel_ids;
  211. for (int i = 0; i < text_message.channel_id_size(); ++i) {
  212. channel_ids.push_back(text_message.channel_id(i));
  213. }
  214. vector<uint32_t> tree_ids;
  215. for (int i = 0; i < text_message.tree_id_size(); ++i) {
  216. tree_ids.push_back(text_message.tree_id(i));
  217. }
  218. callback.textMessage(actor, sessions, channel_ids, tree_ids, text_message.message());
  219. }
  220. break;
  221. case MessageType::PERMISSIONDENIED: // 12
  222. logger.warn("PermissionDenied Message: support not implemented yet");
  223. break;
  224. case MessageType::ACL: // 13
  225. logger.warn("ACL Message: support not implemented yet.");
  226. break;
  227. case MessageType::QUERYUSERS: // 14
  228. logger.warn("QueryUsers Message: support not implemented yet");
  229. break;
  230. case MessageType::CONTEXTACTIONMODIFY: // 16
  231. logger.warn("ContextActionModify Message: support not implemented yet");
  232. break;
  233. case MessageType::CONTEXTACTION: // 17
  234. logger.warn("ContextAction Message: support not implemented yet");
  235. break;
  236. case MessageType::USERLIST: // 18
  237. logger.warn("UserList Message: support not implemented yet");
  238. break;
  239. case MessageType::VOICETARGET:
  240. logger.warn("VoiceTarget Message: I don't think the server ever sends this structure.");
  241. break;
  242. case MessageType::PERMISSIONQUERY: {
  243. MumbleProto::PermissionQuery permissionQuery;
  244. permissionQuery.ParseFromArray(buffer, length);
  245. int32_t channel_id = permissionQuery.has_channel_id() ? permissionQuery.channel_id() : -1;
  246. uint32_t permissions = permissionQuery.has_permissions() ? permissionQuery.permissions() : 0;
  247. uint32_t flush = permissionQuery.has_flush() ? permissionQuery.flush() : -1;
  248. callback.permissionQuery(channel_id, permissions, flush);
  249. }
  250. break;
  251. case MessageType::CODECVERSION: {
  252. MumbleProto::CodecVersion codecVersion;
  253. codecVersion.ParseFromArray(buffer, length);
  254. int32_t alpha = codecVersion.alpha();
  255. int32_t beta = codecVersion.beta();
  256. uint32_t prefer_alpha = codecVersion.prefer_alpha();
  257. int32_t opus = codecVersion.has_opus() ? codecVersion.opus() : 0;
  258. callback.codecVersion(alpha, beta, prefer_alpha, opus);
  259. }
  260. break;
  261. case MessageType::USERSTATS:
  262. logger.warn("UserStats Message: support not implemented yet");
  263. break;
  264. case MessageType::REQUESTBLOB: // 23
  265. logger.warn("RequestBlob Message: I don't think this is sent by the server.");
  266. break;
  267. case MessageType::SERVERCONFIG: {
  268. MumbleProto::ServerConfig serverConfig;
  269. serverConfig.ParseFromArray(buffer, length);
  270. uint32_t max_bandwidth = serverConfig.has_max_bandwidth() ? serverConfig.max_bandwidth() : 0;
  271. uint32_t allow_html = serverConfig.has_allow_html() ? serverConfig.allow_html() : 0;
  272. uint32_t message_length = serverConfig.has_message_length() ? serverConfig.message_length() : 0;
  273. uint32_t image_message_length = serverConfig.has_image_message_length()
  274. ? serverConfig.image_message_length() : 0;
  275. callback.serverConfig(max_bandwidth, serverConfig.welcome_text(), allow_html, message_length,
  276. image_message_length);
  277. }
  278. break;
  279. case MessageType::SUGGESTCONFIG: // 25
  280. logger.warn("SuggestConfig Message: support not implemented yet");
  281. break;
  282. default:
  283. throw MumlibException("unknown message type: " + to_string(static_cast<int>(messageType)));
  284. }
  285. return true;
  286. }
  287. };
  288. Mumlib::Mumlib(Callback &callback) {
  289. MumlibConfiguration conf;
  290. impl = new _Mumlib_Private(callback, conf);
  291. }
  292. Mumlib::Mumlib(Callback &callback, io_service &ioService) {
  293. MumlibConfiguration conf;
  294. impl = new _Mumlib_Private(callback, ioService, conf);
  295. }
  296. Mumlib::Mumlib(Callback &callback, MumlibConfiguration &configuration)
  297. : impl(new _Mumlib_Private(callback, configuration)) { }
  298. Mumlib::Mumlib(Callback &callback, io_service &ioService, MumlibConfiguration &configuration)
  299. : impl(new _Mumlib_Private(callback, ioService, configuration)) { }
  300. Mumlib::~Mumlib() {
  301. disconnect();
  302. delete impl;
  303. }
  304. ConnectionState Mumlib::getConnectionState() {
  305. return impl->transport.getConnectionState();
  306. }
  307. int Mumlib::getChannelId() {
  308. return impl->channelId;
  309. }
  310. void Mumlib::connect(string host, int port, string user, string password) {
  311. impl->transport.connect(host, port, user, password);
  312. }
  313. void Mumlib::disconnect() {
  314. if (not impl->externalIoService) {
  315. impl->ioService.stop();
  316. }
  317. if (impl->transport.getConnectionState() != ConnectionState::NOT_CONNECTED) {
  318. impl->transport.disconnect();
  319. }
  320. }
  321. void Mumlib::reconnect() {
  322. if (not impl->externalIoService) {
  323. impl->ioService.reset();
  324. }
  325. if (impl->transport.getConnectionState() != ConnectionState::NOT_CONNECTED) {
  326. impl->transport.disconnect();
  327. }
  328. }
  329. void Mumlib::run() {
  330. if (impl->externalIoService) {
  331. throw MumlibException("can't call run() when using external io_service");
  332. }
  333. impl->ioService.run();
  334. }
  335. void Mumlib::sendAudioData(int16_t *pcmData, int pcmLength) {
  336. uint8_t encodedData[5000];
  337. int length = impl->audio.encodeAudioPacket(0, pcmData, pcmLength, encodedData, 5000);
  338. impl->transport.sendEncodedAudioPacket(encodedData, length);
  339. }
  340. void Mumlib::sendAudioDataTarget(int targetId, int16_t *pcmData, int pcmLength) {
  341. uint8_t encodedData[5000];
  342. int length = impl->audio.encodeAudioPacket(targetId, pcmData, pcmLength, encodedData, 5000);
  343. impl->transport.sendEncodedAudioPacket(encodedData, length);
  344. }
  345. void Mumlib::sendTextMessage(string message) {
  346. MumbleProto::TextMessage textMessage;
  347. textMessage.set_actor(impl->sessionId);
  348. textMessage.add_channel_id(impl->channelId);
  349. textMessage.set_message(message);
  350. impl->transport.sendControlMessage(MessageType::TEXTMESSAGE, textMessage);
  351. }
  352. void Mumlib::joinChannel(int channelId) {
  353. MumbleProto::UserState userState;
  354. userState.set_channel_id(channelId);
  355. impl->transport.sendControlMessage(MessageType::USERSTATE, userState);
  356. impl->channelId = channelId;
  357. }
  358. void Mumlib::sendVoiceTarget(int targetId, int channelId) {
  359. MumbleProto::VoiceTarget voiceTarget;
  360. MumbleProto::VoiceTarget_Target voiceTargetTarget;
  361. voiceTargetTarget.set_channel_id(channelId);
  362. voiceTargetTarget.set_children(true);
  363. voiceTarget.set_id(targetId);
  364. voiceTarget.add_targets()->CopyFrom(voiceTargetTarget);
  365. impl->transport.sendControlMessage(MessageType::VOICETARGET, voiceTarget);
  366. }
  367. void Mumlib::sendUserState(mumlib::UserState field, bool val) {
  368. MumbleProto::UserState userState;
  369. switch (field) {
  370. case UserState::MUTE:
  371. userState.set_mute(val);
  372. break;
  373. case UserState::DEAF:
  374. userState.set_deaf(val);
  375. break;
  376. case UserState::SUPPRESS:
  377. userState.set_suppress(val);
  378. break;
  379. case UserState::SELF_MUTE:
  380. userState.set_self_mute(val);
  381. break;
  382. case UserState::SELF_DEAF:
  383. userState.set_self_deaf(val);
  384. break;
  385. case UserState::PRIORITY_SPEAKER:
  386. userState.set_priority_speaker(val);
  387. break;
  388. case UserState::RECORDING:
  389. userState.set_recording(val);
  390. break;
  391. default:
  392. // in any other case, just ignore the command
  393. return;
  394. }
  395. impl->transport.sendControlMessage(MessageType::USERSTATE, userState);
  396. }
  397. void Mumlib::sendUserState(mumlib::UserState field, std::string val) {
  398. MumbleProto::UserState userState;
  399. switch (field) {
  400. case UserState::COMMENT:
  401. // TODO: if comment longer than 128 bytes, we need to set the SHA1 hash
  402. userState.set_comment(val);
  403. break;
  404. default:
  405. // in any other case, just ignore the command
  406. return;
  407. }
  408. impl->transport.sendControlMessage(MessageType::USERSTATE, userState);
  409. }
  410. }