diff --git a/include/utils.h b/include/utils.h index cf1fa4b..4f44505 100644 --- a/include/utils.h +++ b/include/utils.h @@ -11,7 +11,9 @@ namespace Tins { uint32_t ip_to_int(const std::string &ip); std::string ip_to_string(uint32_t ip); - inline uint32_t ntohl(uint32_t address) { + uint32_t resolve_ip(const std::string &to_resolve); + + inline uint32_t net_to_host_l(uint32_t address) { return (((address & 0xff000000) >> 24) | ((address & 0x00ff0000) >> 8) | ((address & 0x0000ff00) << 8) | diff --git a/src/ip.cpp b/src/ip.cpp index 19306fe..6297999 100644 --- a/src/ip.cpp +++ b/src/ip.cpp @@ -14,9 +14,9 @@ using namespace std; Tins::IP::IP(const string &ip_dst, const string &ip_src) : PDU(IPPROTO_IP) { init_ip_fields(); if(ip_dst.size()) - _ip.daddr = Utils::ip_to_int(ip_dst); + _ip.daddr = Utils::resolve_ip(ip_dst); if(ip_src.size()) - _ip.saddr = Utils::ip_to_int(ip_src); + _ip.saddr = Utils::resolve_ip(ip_src); } @@ -64,7 +64,7 @@ void Tins::IP::check(uint16_t new_check) { } void Tins::IP::source_address(const string &ip) { - _ip.saddr = Utils::ip_to_int(ip); + _ip.saddr = Utils::resolve_ip(ip); } void Tins::IP::source_address(uint32_t ip) { @@ -72,7 +72,7 @@ void Tins::IP::source_address(uint32_t ip) { } void Tins::IP::dest_address(const string &ip) { - _ip.daddr = Utils::ip_to_int(ip); + _ip.daddr = Utils::resolve_ip(ip); } void Tins::IP::dest_address(uint32_t ip) { @@ -86,23 +86,25 @@ uint32_t Tins::IP::header_size() const { } bool Tins::IP::send(PacketSender* sender) { - struct sockaddr_in link_addr; - link_addr.sin_family = AF_INET; link_addr.sin_port = 0; link_addr.sin_addr.s_addr = _ip.daddr; return sender->send_l3(this, (const struct sockaddr*)&link_addr, sizeof(link_addr)); - } void Tins::IP::write_serialization(uint8_t *buffer, uint32_t total_sz) { uint32_t my_sz = header_size() + trailer_size(); - uint32_t new_flag = inner_pdu()? inner_pdu()->flag() : 255; + uint32_t new_flag; assert(total_sz >= my_sz); - if(new_flag == IPPROTO_IP) - new_flag = IPPROTO_IPIP; + if(inner_pdu()) { + new_flag = inner_pdu()->flag(); + if(new_flag == IPPROTO_IP) + new_flag = IPPROTO_IPIP; + } + else + new_flag = IPPROTO_IP; flag(new_flag); _ip.protocol = new_flag; _ip.tot_len = total_sz; diff --git a/src/utils.cpp b/src/utils.cpp index b27b544..abfd8f6 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -1,5 +1,9 @@ #include #include +#include +#ifndef WIN32 + #include +#endif #include "utils.h" using namespace std; @@ -22,13 +26,13 @@ uint32_t Tins::Utils::ip_to_int(const string &ip) { } if(bytes_found < 4 || (i < ip.size() && bytes_found == 4)) throw std::runtime_error("Invalid ip address"); - return ntohl(result); + return net_to_host_l(result); } string Tins::Utils::ip_to_string(uint32_t ip) { ostringstream oss; int mask(24); - ip = ntohl(ip); + ip = net_to_host_l(ip); while(mask >=0) { oss << ((ip >> mask) & 0xff); if(mask) @@ -37,3 +41,10 @@ string Tins::Utils::ip_to_string(uint32_t ip) { } return oss.str(); } + +uint32_t Tins::Utils::resolve_ip(const string &to_resolve) { + struct hostent *data = gethostbyname(to_resolve.c_str()); + if(!data) + return 0; + return ((struct in_addr**)data->h_addr_list)[0]->s_addr; +}