1
0
mirror of https://github.com/mfontanini/libtins synced 2026-01-27 20:24:26 +01:00

Code cleanup and use same syntax on the entire project

Initial code cleanup

More code cleanup

Cleanup more code

Cleanup Dot11 code

Fix OSX build issue

Cleanup examples

Fix ref and pointer declaration syntax

Fix braces
This commit is contained in:
Matias Fontanini
2016-01-02 08:17:59 -08:00
parent f5a82b1a17
commit d84f10cf08
177 changed files with 13203 additions and 12272 deletions

View File

@@ -40,6 +40,8 @@
#include "address_range.h"
using std::string;
using std::ostringstream;
using std::ostream;
namespace Tins{
const IPv4Address IPv4Address::broadcast("255.255.255.255");
@@ -54,66 +56,68 @@ const AddressRange<IPv4Address> loopback_range = IPv4Address("127.0.0.0") / 8;
const AddressRange<IPv4Address> multicast_range = IPv4Address("224.0.0.0") / 4;
IPv4Address::IPv4Address(uint32_t ip)
: ip_addr(Endian::be_to_host(ip)) {
: ip_addr_(Endian::be_to_host(ip)) {
}
IPv4Address::IPv4Address(const char *ip) {
ip_addr = ip ? ip_to_int(ip) : 0;
IPv4Address::IPv4Address(const char* ip) {
ip_addr_ = ip ? ip_to_int(ip) : 0;
}
IPv4Address::IPv4Address(const std::string &ip)
: ip_addr(ip_to_int(ip.c_str())) {
IPv4Address::IPv4Address(const std::string& ip)
: ip_addr_(ip_to_int(ip.c_str())) {
}
IPv4Address::operator uint32_t() const {
return Endian::host_to_be(ip_addr);
return Endian::host_to_be(ip_addr_);
}
std::string IPv4Address::to_string() const {
std::ostringstream oss;
oss << *this;
string IPv4Address::to_string() const {
ostringstream oss;
oss <<* this;
return oss.str();
}
uint32_t IPv4Address::ip_to_int(const char* ip) {
#ifdef _WIN32
in_addr addr;
if(InetPtonA(AF_INET, ip, &addr)) {
if (InetPtonA(AF_INET, ip, &addr)) {
return Endian::be_to_host(addr.s_addr);
}
else {
throw std::runtime_error("Invalid ip address");
throw invalid_address();
}
#else // _WIN32
in_addr addr;
if(inet_pton(AF_INET, ip, &addr) == 1) {
if (inet_pton(AF_INET, ip, &addr) == 1) {
return Endian::be_to_host(addr.s_addr);
}
else {
throw std::runtime_error("Invalid ip address");
throw invalid_address();
}
#endif // _WIN32
}
std::ostream &operator<<(std::ostream &output, const IPv4Address &addr) {
ostream& operator<<(ostream& output, const IPv4Address& addr) {
int mask(24);
uint32_t ip_addr = addr.ip_addr;
while(mask >=0) {
output << ((ip_addr >> mask) & 0xff);
if(mask)
uint32_t ip_addr_ = addr.ip_addr_;
while (mask >=0) {
output << ((ip_addr_ >> mask) & 0xff);
if (mask) {
output << '.';
}
mask -= 8;
}
return output;;
}
bool IPv4Address::is_private() const {
const AddressRange<IPv4Address> *iter = private_ranges;
while(iter != private_ranges + 3) {
if(iter->contains(*this))
const AddressRange<IPv4Address>* iter = private_ranges;
while (iter != private_ranges + 3) {
if (iter->contains(*this)) {
return true;
}
++iter;
}
return false;
@@ -132,6 +136,7 @@ bool IPv4Address::is_unicast() const {
}
bool IPv4Address::is_broadcast() const {
return *this == broadcast;
}
return* this == broadcast;
}
} // Tins