From d061fced7e3aa4352f47aa68276e58c18054fba6 Mon Sep 17 00:00:00 2001 From: Matias Fontanini Date: Sun, 30 Apr 2017 12:06:42 -0700 Subject: [PATCH] Move frequency (channel) utils into their own file --- include/tins/utils.h | 15 +------- include/tins/utils/frequency_utils.h | 56 ++++++++++++++++++++++++++++ src/CMakeLists.txt | 1 + src/radiotap.cpp | 2 +- src/utils.cpp | 8 ---- src/utils/frequency_utils.cpp | 44 ++++++++++++++++++++++ 6 files changed, 103 insertions(+), 23 deletions(-) create mode 100644 include/tins/utils/frequency_utils.h create mode 100644 src/utils/frequency_utils.cpp diff --git a/include/tins/utils.h b/include/tins/utils.h index e2a54e2..f445af3 100644 --- a/include/tins/utils.h +++ b/include/tins/utils.h @@ -40,6 +40,7 @@ #include "pdu.h" #include "detail/type_traits.h" #include "utils/checksum_utils.h" +#include "utils/frequency_utils.h" // Fix for Windows interface define on combaseapi.h #undef interface @@ -216,20 +217,6 @@ TINS_API std::vector route_entries(); */ TINS_API std::vector route6_entries(); -/** - * \brief Converts a channel number to its mhz representation. - * \param channel The channel number. - * \return The channel's mhz representation. - */ -TINS_API uint16_t channel_to_mhz(uint16_t channel); - -/** - * \brief Converts mhz units to the appropriate channel number. - * \param mhz The mhz units to be converted. - * \return The channel number. - */ -TINS_API uint16_t mhz_to_channel(uint16_t mhz); - /** * \brief Converts a PDUType to a string. * \param pduType The PDUType to be converted. diff --git a/include/tins/utils/frequency_utils.h b/include/tins/utils/frequency_utils.h new file mode 100644 index 0000000..dfc34d5 --- /dev/null +++ b/include/tins/utils/frequency_utils.h @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2017, Matias Fontanini + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef TINS_FREQUENCY_UTILS_H +#define TINS_FREQUENCY_UTILS_H + +#include +#include "../macros.h" + +namespace Tins { +namespace Utils { + +/** + * \brief Converts a channel number to its mhz representation. + * \param channel The channel number. + * \return The channel's mhz representation. + */ +TINS_API uint16_t channel_to_mhz(uint16_t channel); + +/** + * \brief Converts mhz units to the appropriate channel number. + * \param mhz The mhz units to be converted. + * \return The channel number. + */ +TINS_API uint16_t mhz_to_channel(uint16_t mhz); + +} // Utils +} // Tins + +#endif // TINS_FREQUENCY_UTILS_H diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 07334c0..6ba788f 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -73,6 +73,7 @@ set(SOURCES dot11/dot11_probe.cpp dot11/dot11_control.cpp utils/checksum_utils.cpp + utils/frequency_utils.cpp ) SET(PCAP_DEPENDENT_SOURCES diff --git a/src/radiotap.cpp b/src/radiotap.cpp index 6198fa1..d6a7a04 100644 --- a/src/radiotap.cpp +++ b/src/radiotap.cpp @@ -44,11 +44,11 @@ #include #endif #include "dot11/dot11_base.h" -#include "utils.h" #include "packet_sender.h" #include "exceptions.h" #include "memory_helpers.h" #include "utils/checksum_utils.h" +#include "utils/frequency_utils.h" using std::memcpy; diff --git a/src/utils.cpp b/src/utils.cpp index f815e99..413d713 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -524,14 +524,6 @@ set network_interfaces() { return output; } #endif // _WIN32 - -uint16_t channel_to_mhz(uint16_t channel) { - return 2407 + (channel * 5); -} - -uint16_t mhz_to_channel(uint16_t mhz) { - return (mhz - 2407) / 5; -} string to_string(PDU::PDUType pduType) { #define ENUM_TEXT(p) case(PDU::p): return #p; diff --git a/src/utils/frequency_utils.cpp b/src/utils/frequency_utils.cpp new file mode 100644 index 0000000..9c27d06 --- /dev/null +++ b/src/utils/frequency_utils.cpp @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2017, Matias Fontanini + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include "utils/frequency_utils.h" + +namespace Tins { +namespace Utils { + +uint16_t channel_to_mhz(uint16_t channel) { + return 2407 + (channel * 5); +} + +uint16_t mhz_to_channel(uint16_t mhz) { + return (mhz - 2407) / 5; +} + +} // Utils +} // Tins