1
0
mirror of https://github.com/mfontanini/libtins synced 2026-01-29 04:54:28 +01:00

Merge pull request #333 from DDoSolitary/patch-radiotap-writer

Overhaul RadioTapWriter
This commit is contained in:
Matias Fontanini
2019-03-14 18:36:31 -07:00
committed by GitHub
2 changed files with 27 additions and 3 deletions

View File

@@ -41,11 +41,12 @@ namespace Tins {
namespace Utils {
uint32_t calculate_padding(uint32_t alignment, uint32_t offset) {
return offset % alignment;
uint32_t extra = offset % alignment;
return extra == 0 ? 0 : alignment - extra;
}
uint32_t get_bit(uint32_t value) {
return log(value) / log(2);
return round(log2(value));
}
RadioTapWriter::RadioTapWriter(vector<uint8_t>& buffer)
@@ -54,7 +55,7 @@ RadioTapWriter::RadioTapWriter(vector<uint8_t>& buffer)
void RadioTapWriter::write_option(const RadioTap::option& option) {
const uint32_t bit = get_bit(option.option());
if (bit > RadioTapParser::MAX_RADIOTAP_FIELD) {
if (bit >= RadioTapParser::MAX_RADIOTAP_FIELD) {
throw malformed_option();
}
const bool is_empty = buffer_.empty();

View File

@@ -759,4 +759,27 @@ TEST_F(RadioTapTest, RadioTapWritingEmptyBuffer) {
}
TEST_F(RadioTapTest, RadioTapWritingInvalidOption) {
vector<uint8_t> buffer(4, 0);
RadioTapWriter writer(buffer);
uint8_t foo = 0;
RadioTap::option option((RadioTap::PresentFlags)(1 << RadioTapParser::MAX_RADIOTAP_FIELD), sizeof(foo), &foo);
EXPECT_THROW(writer.write_option(option), malformed_option);
}
TEST_F(RadioTapTest, RadioTapWriterAlignment) {
vector<uint8_t> buffer(4, 0);
RadioTapWriter writer(buffer);
uint8_t flags = 10;
uint8_t xchannel[sizeof(RadioTap::xchannel_type)] = {
1, 2, 3, 4, 5, 6, 7, 8
};
writer.write_option(RadioTap::option(RadioTap::FLAGS, sizeof(flags), &flags));
writer.write_option(RadioTap::option(RadioTap::XCHANNEL, sizeof(xchannel), xchannel));
vector<uint8_t> expected = {
2, 0, 4, 0, 10, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8
};
EXPECT_EQ(buffer, expected);
}
#endif // TINS_HAVE_DOT11