mirror of
https://github.com/mfontanini/libtins
synced 2026-01-23 02:35:57 +01:00
Move routing related functions into their own header file
This commit is contained in:
@@ -32,8 +32,6 @@
|
||||
|
||||
#include "macros.h"
|
||||
#include <string>
|
||||
#include <set>
|
||||
#include <vector>
|
||||
#include <stdint.h>
|
||||
#include "ip_address.h"
|
||||
#include "ipv6_address.h"
|
||||
@@ -41,6 +39,7 @@
|
||||
#include "detail/type_traits.h"
|
||||
#include "utils/checksum_utils.h"
|
||||
#include "utils/frequency_utils.h"
|
||||
#include "utils/routing_utils.h"
|
||||
|
||||
// Fix for Windows interface define on combaseapi.h
|
||||
#undef interface
|
||||
@@ -63,66 +62,6 @@ class HWAddress;
|
||||
*/
|
||||
namespace Utils {
|
||||
|
||||
/**
|
||||
* Struct that represents an entry the routing table
|
||||
*/
|
||||
struct RouteEntry {
|
||||
/**
|
||||
* This interface's name.
|
||||
*/
|
||||
std::string interface;
|
||||
|
||||
/**
|
||||
* This route entry's destination.
|
||||
*/
|
||||
IPv4Address destination;
|
||||
|
||||
/**
|
||||
* This route entry's gateway.
|
||||
*/
|
||||
IPv4Address gateway;
|
||||
|
||||
/**
|
||||
* This route entry's subnet mask.
|
||||
*/
|
||||
IPv4Address mask;
|
||||
|
||||
/**
|
||||
* This route entry's metric.
|
||||
*/
|
||||
int metric;
|
||||
};
|
||||
|
||||
/**
|
||||
* Struct that represents an entry the IPv6 routing table
|
||||
*/
|
||||
struct Route6Entry {
|
||||
/**
|
||||
* This interface's name.
|
||||
*/
|
||||
std::string interface;
|
||||
|
||||
/**
|
||||
* This route entry's destination.
|
||||
*/
|
||||
IPv6Address destination;
|
||||
|
||||
/**
|
||||
* This route entry's subnet mask.
|
||||
*/
|
||||
IPv6Address mask;
|
||||
|
||||
/**
|
||||
* This route entry's next hop.
|
||||
*/
|
||||
IPv6Address gateway;
|
||||
|
||||
/**
|
||||
* This route entry's metric.
|
||||
*/
|
||||
int metric;
|
||||
};
|
||||
|
||||
/**
|
||||
* \brief Resolves a domain name and returns its corresponding ip address.
|
||||
*
|
||||
@@ -173,14 +112,6 @@ TINS_API HWAddress<6> resolve_hwaddr(const NetworkInterface& iface,
|
||||
*/
|
||||
TINS_API HWAddress<6> resolve_hwaddr(IPv4Address ip, PacketSender& sender);
|
||||
|
||||
/** \brief List all network interfaces.
|
||||
*
|
||||
* Returns a set of strings, each of them representing the name
|
||||
* of a network interface. These names can be used as the input
|
||||
* interface for Utils::interface_ip, Utils::interface_hwaddr, etc.
|
||||
*/
|
||||
TINS_API std::set<std::string> network_interfaces();
|
||||
|
||||
/**
|
||||
* \brief Finds the gateway's IP address for the given IP
|
||||
* address.
|
||||
@@ -194,29 +125,6 @@ TINS_API std::set<std::string> network_interfaces();
|
||||
*/
|
||||
TINS_API bool gateway_from_ip(IPv4Address ip, IPv4Address& gw_addr);
|
||||
|
||||
|
||||
/**
|
||||
* \brief Retrieves entries in the routing table.
|
||||
*
|
||||
* \brief output ForwardIterator in which entries will be stored.
|
||||
*/
|
||||
template<typename ForwardIterator>
|
||||
void route_entries(ForwardIterator output);
|
||||
|
||||
/**
|
||||
* \brief Retrieves entries in the routing table.
|
||||
*
|
||||
* \return a vector which contains all of the route entries.
|
||||
*/
|
||||
TINS_API std::vector<RouteEntry> route_entries();
|
||||
|
||||
/**
|
||||
* \brief Retrieves entries in the routing table.
|
||||
*
|
||||
* \return a vector which contains all of the route entries.
|
||||
*/
|
||||
TINS_API std::vector<Route6Entry> route6_entries();
|
||||
|
||||
/**
|
||||
* \brief Converts a PDUType to a string.
|
||||
* \param pduType The PDUType to be converted.
|
||||
@@ -259,13 +167,4 @@ dereference_until_pdu(T& value) {
|
||||
} // Utils
|
||||
} // Tins
|
||||
|
||||
template<typename ForwardIterator>
|
||||
void Tins::Utils::route_entries(ForwardIterator output) {
|
||||
std::vector<RouteEntry> entries = route_entries();
|
||||
for (size_t i = 0; i < entries.size(); ++i) {
|
||||
*output = entries[i];
|
||||
++output;
|
||||
}
|
||||
}
|
||||
|
||||
#endif // TINS_UTILS_H
|
||||
|
||||
148
include/tins/utils/routing_utils.h
Normal file
148
include/tins/utils/routing_utils.h
Normal file
@@ -0,0 +1,148 @@
|
||||
/*
|
||||
* 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_ROUTING_UTILS_H
|
||||
#define TINS_ROUTING_UTILS_H
|
||||
|
||||
#include <vector>
|
||||
#include <set>
|
||||
#include "../macros.h"
|
||||
#include "../ip_address.h"
|
||||
#include "../ipv6_address.h"
|
||||
|
||||
// Fix for Windows interface define on combaseapi.h
|
||||
#undef interface
|
||||
|
||||
namespace Tins {
|
||||
namespace Utils {
|
||||
|
||||
/**
|
||||
* Struct that represents an entry the routing table
|
||||
*/
|
||||
struct RouteEntry {
|
||||
/**
|
||||
* This interface's name.
|
||||
*/
|
||||
std::string interface;
|
||||
|
||||
/**
|
||||
* This route entry's destination.
|
||||
*/
|
||||
IPv4Address destination;
|
||||
|
||||
/**
|
||||
* This route entry's gateway.
|
||||
*/
|
||||
IPv4Address gateway;
|
||||
|
||||
/**
|
||||
* This route entry's subnet mask.
|
||||
*/
|
||||
IPv4Address mask;
|
||||
|
||||
/**
|
||||
* This route entry's metric.
|
||||
*/
|
||||
int metric;
|
||||
};
|
||||
|
||||
/**
|
||||
* Struct that represents an entry the IPv6 routing table
|
||||
*/
|
||||
struct Route6Entry {
|
||||
/**
|
||||
* This interface's name.
|
||||
*/
|
||||
std::string interface;
|
||||
|
||||
/**
|
||||
* This route entry's destination.
|
||||
*/
|
||||
IPv6Address destination;
|
||||
|
||||
/**
|
||||
* This route entry's subnet mask.
|
||||
*/
|
||||
IPv6Address mask;
|
||||
|
||||
/**
|
||||
* This route entry's next hop.
|
||||
*/
|
||||
IPv6Address gateway;
|
||||
|
||||
/**
|
||||
* This route entry's metric.
|
||||
*/
|
||||
int metric;
|
||||
};
|
||||
|
||||
/**
|
||||
* \brief Retrieves entries in the routing table.
|
||||
*
|
||||
* \brief output ForwardIterator in which entries will be stored.
|
||||
*/
|
||||
template<typename ForwardIterator>
|
||||
void route_entries(ForwardIterator output);
|
||||
|
||||
/**
|
||||
* \brief Retrieves entries in the routing table.
|
||||
*
|
||||
* \return a vector which contains all of the route entries.
|
||||
*/
|
||||
TINS_API std::vector<RouteEntry> route_entries();
|
||||
|
||||
/**
|
||||
* \brief Retrieves entries in the routing table.
|
||||
*
|
||||
* \return a vector which contains all of the route entries.
|
||||
*/
|
||||
TINS_API std::vector<Route6Entry> route6_entries();
|
||||
|
||||
/**
|
||||
* \brief List all network interfaces.
|
||||
*
|
||||
* Returns a set of strings, each of them representing the name
|
||||
* of a network interface. These names can be used as the input
|
||||
* interface for Utils::interface_ip, Utils::interface_hwaddr, etc.
|
||||
*/
|
||||
TINS_API std::set<std::string> network_interfaces();
|
||||
|
||||
} // Utils
|
||||
} // Tins
|
||||
|
||||
template<typename ForwardIterator>
|
||||
void Tins::Utils::route_entries(ForwardIterator output) {
|
||||
std::vector<RouteEntry> entries = route_entries();
|
||||
for (size_t i = 0; i < entries.size(); ++i) {
|
||||
*output = entries[i];
|
||||
++output;
|
||||
}
|
||||
}
|
||||
|
||||
#endif // TINS_ROUTING_UTILS_H
|
||||
Reference in New Issue
Block a user