1 /* 2 * Copyright (C) 2019 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #pragma once 18 19 #include <cstdint> 20 #include <optional> 21 #include <string_view> 22 #include <vector> 23 24 struct STUNMessage { 25 explicit STUNMessage(uint16_t type, const uint8_t transactionID[12]); 26 explicit STUNMessage(const void *data, size_t size); 27 28 bool isValid() const; 29 30 uint16_t type() const; 31 addAttributeSTUNMessage32 void addAttribute(uint16_t type) { 33 addAttribute(type, nullptr, 0); 34 } 35 36 void addAttribute(uint16_t type, const void *data, size_t size); 37 void addMessageIntegrityAttribute(std::string_view password); 38 void addFingerprint(); 39 40 bool findAttribute(uint16_t type, const void **data, size_t *size) const; 41 42 const uint8_t *data(); 43 size_t size() const; 44 45 void dump(std::optional<std::string_view> password = std::nullopt) const; 46 47 private: 48 bool mIsValid; 49 std::vector<uint8_t> mData; 50 bool mAddedMessageIntegrity; 51 52 void validate(); 53 54 bool verifyMessageIntegrity(size_t offset, std::string_view password) const; 55 bool verifyFingerprint(size_t offset) const; 56 }; 57