Skip to content

Commit

Permalink
Use nbase u8 as buffer type
Browse files Browse the repository at this point in the history
  • Loading branch information
G10h4ck committed Jul 30, 2015
1 parent aec4aac commit 18b6b9c
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 45 deletions.
44 changes: 29 additions & 15 deletions nmap_dns.cc
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ static void write_evt_handler(nsock_pool nsp, nsock_event evt, void *req_v) {
// the time for the timeout.
static void put_dns_packet_on_wire(request *req) {
const size_t maxlen = 512;
char packet[maxlen];
u8 packet[maxlen];
size_t plen=0;

struct timeval now, timeout;
Expand All @@ -536,7 +536,7 @@ static void put_dns_packet_on_wire(request *req) {

req->tries++;

nsock_write(dnspool, req->curr_server->nsd, write_evt_handler, WRITE_TIMEOUT, req, packet, plen);
nsock_write(dnspool, req->curr_server->nsd, write_evt_handler, WRITE_TIMEOUT, req, reinterpret_cast<const char *>(packet), plen);
}

// Processes DNS packets that have timed out
Expand Down Expand Up @@ -703,7 +703,7 @@ static void read_evt_handler(nsock_pool nsp, nsock_event evt, void *) {
buf = (unsigned char *) nse_readbuf(evt, &buflen);

DNS::Packet p;
size_t readed_bytes = p.parseFromBuffer((char*)buf, buflen);
size_t readed_bytes = p.parseFromBuffer(buf, buflen);
if(readed_bytes < DNS::DATA) return;

// We should have 1+ queries:
Expand Down Expand Up @@ -1415,7 +1415,7 @@ bool DNS::Factory::ptrToIp(const std::string &ptr, sockaddr_storage &ip)
sockaddr_storage_inet_pton(ip_str.c_str(), &ip);
return true;
}
size_t DNS::Factory::buildSimpleRequest(const std::string &name, RECORD_TYPE rt, char *buf, size_t maxlen)
size_t DNS::Factory::buildSimpleRequest(const std::string &name, RECORD_TYPE rt, u8 *buf, size_t maxlen)
{
size_t ret=0 , tmp=0;
DNS_CHECK_ACCUMLATE(ret, tmp, putUnsignedShort(progressiveId++, buf, ID, maxlen)); // Postincrement inmportant here
Expand All @@ -1430,14 +1430,14 @@ size_t DNS::Factory::buildSimpleRequest(const std::string &name, RECORD_TYPE rt,

return ret;
}
size_t DNS::Factory::buildReverseRequest(const sockaddr_storage &ip, char *buf, size_t maxlen)
size_t DNS::Factory::buildReverseRequest(const sockaddr_storage &ip, u8 *buf, size_t maxlen)
{
std::string name;
if(ipToPtr(ip,name))
return buildSimpleRequest(name, PTR, buf, maxlen);
return 0;
}
size_t DNS::Factory::putUnsignedShort(u16 num, char *buf, size_t offset, size_t maxlen)
size_t DNS::Factory::putUnsignedShort(u16 num, u8 *buf, size_t offset, size_t maxlen)
{
size_t max_access = offset+1;
if(buf && (maxlen > max_access))
Expand All @@ -1449,7 +1449,7 @@ size_t DNS::Factory::putUnsignedShort(u16 num, char *buf, size_t offset, size_t

return 0;
}
size_t DNS::Factory::putDomainName(const std::string &name, char *buf, size_t offset, size_t maxlen)
size_t DNS::Factory::putDomainName(const std::string &name, u8 *buf, size_t offset, size_t maxlen)
{
size_t ret=0;
if( !( buf && (maxlen > (offset + name.length() + 1))) ) return ret;
Expand Down Expand Up @@ -1477,31 +1477,31 @@ size_t DNS::Factory::putDomainName(const std::string &name, char *buf, size_t of

return ret;
}
size_t DNS::Factory::parseUnsignedShort(u16 &num, const char *buf, size_t offset, size_t maxlen)
size_t DNS::Factory::parseUnsignedShort(u16 &num, const u8 *buf, size_t offset, size_t maxlen)
{
size_t max_access = offset+1;
if(buf && (maxlen > max_access))
{
const u8 * n = reinterpret_cast<const u8 *>(buf+offset);
const u8 * n = buf + offset;
num = n[1] + (n[0]<<8);
return 2;
}

return 0;
}
size_t DNS::Factory::parseUnsignedInt(u32 &num, const char *buf, size_t offset, size_t maxlen)
size_t DNS::Factory::parseUnsignedInt(u32 &num, const u8 *buf, size_t offset, size_t maxlen)
{
size_t max_access = offset+3;
if(buf && (maxlen > max_access))
{
const u8 * n = reinterpret_cast<const u8 *>(buf + offset);
const u8 * n = buf + offset;
num = n[3] + (n[2]<<8) + (n[1]<<16) + (n[0]<<24);
return 4;
}

return 0;
}
size_t DNS::Factory::parseDomainName(std::string &name, const char *buf, size_t offset, size_t maxlen)
size_t DNS::Factory::parseDomainName(std::string &name, const u8 *buf, size_t offset, size_t maxlen)
{
size_t tmp, ret = 0;

Expand Down Expand Up @@ -1539,7 +1539,21 @@ size_t DNS::Factory::parseDomainName(std::string &name, const char *buf, size_t
return ret;
}

size_t DNS::Query::parseFromBuffer(const char *buf, size_t offset, size_t maxlen)
size_t DNS::A_Record::parseFromBuffer(const u8 *buf, size_t offset, size_t maxlen)
{
size_t tmp, ret = 0;
u32 num;
DNS_CHECK_ACCUMLATE(ret, tmp, Factory::parseUnsignedInt(num, buf, offset, maxlen));

memset(&value, 0, sizeof(value));
struct sockaddr_in * ip4addr = (sockaddr_in *) &value;
ip4addr->sin_family = AF_INET;
ip4addr->sin_addr.s_addr = htonl(num);

return ret;
}

size_t DNS::Query::parseFromBuffer(const u8 *buf, size_t offset, size_t maxlen)
{
size_t ret=0;

Expand All @@ -1554,7 +1568,7 @@ size_t DNS::Query::parseFromBuffer(const char *buf, size_t offset, size_t maxlen
return ret;
}

size_t DNS::Answer::parseFromBuffer(const char * buf, size_t offset, size_t maxlen)
size_t DNS::Answer::parseFromBuffer(const u8 *buf, size_t offset, size_t maxlen)
{
size_t ret=0;

Expand Down Expand Up @@ -1606,7 +1620,7 @@ DNS::Answer& DNS::Answer::operator=(const Answer &r)
return *this;
}

size_t DNS::Packet::parseFromBuffer(const char *buf, size_t maxlen)
size_t DNS::Packet::parseFromBuffer(const u8 *buf, size_t maxlen)
{
if( !buf || maxlen < DATA) return 0;

Expand Down
42 changes: 15 additions & 27 deletions nmap_dns.h
Original file line number Diff line number Diff line change
Expand Up @@ -203,21 +203,21 @@ class Factory
static u16 progressiveId;
static bool ipToPtr(const sockaddr_storage &ip, std::string &ptr);
static bool ptrToIp(const std::string &ptr, sockaddr_storage &ip);
static size_t buildSimpleRequest(const std::string &name, RECORD_TYPE rt, char *buf, size_t maxlen);
static size_t buildReverseRequest(const sockaddr_storage &ip, char *buf, size_t maxlen);
static size_t putUnsignedShort(u16 num, char *buf, size_t offset, size_t maxlen);
static size_t putDomainName(const std::string &name, char *buf, size_t offset, size_t maxlen);
static size_t parseUnsignedShort(u16 &num, const char *buf, size_t offset, size_t maxlen);
static size_t parseUnsignedInt(u32 &num, const char *buf, size_t offset, size_t maxlen);
static size_t parseDomainName(std::string &name, const char *buf, size_t offset, size_t maxlen);
static size_t buildSimpleRequest(const std::string &name, RECORD_TYPE rt, u8 *buf, size_t maxlen);
static size_t buildReverseRequest(const sockaddr_storage &ip, u8 *buf, size_t maxlen);
static size_t putUnsignedShort(u16 num, u8 *buf, size_t offset, size_t maxlen);
static size_t putDomainName(const std::string &name, u8 *buf, size_t offset, size_t maxlen);
static size_t parseUnsignedShort(u16 &num, const u8 *buf, size_t offset, size_t maxlen);
static size_t parseUnsignedInt(u32 &num, const u8 *buf, size_t offset, size_t maxlen);
static size_t parseDomainName(std::string &name, const u8 *buf, size_t offset, size_t maxlen);
};

class Record
{
public:
virtual Record * clone() = 0;
virtual ~Record() {}
virtual size_t parseFromBuffer(const char *buf, size_t offset, size_t maxlen) = 0;
virtual size_t parseFromBuffer(const u8 *buf, size_t offset, size_t maxlen) = 0;
};

class A_Record : public Record
Expand All @@ -226,19 +226,7 @@ class A_Record : public Record
sockaddr_storage value;
Record * clone() { return new A_Record(*this); }
~A_Record() {}
size_t parseFromBuffer(const char *buf, size_t offset, size_t maxlen)
{
size_t tmp, ret = 0;
u32 num;
DNS_CHECK_ACCUMLATE(ret, tmp, Factory::parseUnsignedInt(num, buf, offset, maxlen));

memset(&value, 0, sizeof(value));
struct sockaddr_in * ip4addr = (sockaddr_in *) &value;
ip4addr->sin_family = AF_INET;
ip4addr->sin_addr.s_addr = htonl(num);

return ret;
}
size_t parseFromBuffer(const u8 *buf, size_t offset, size_t maxlen);
};

class PTR_Record : public Record
Expand All @@ -247,7 +235,7 @@ class PTR_Record : public Record
std::string value;
Record * clone() { return new PTR_Record(*this); }
~PTR_Record() {}
size_t parseFromBuffer(const char *buf, size_t offset, size_t maxlen)
size_t parseFromBuffer(const u8 *buf, size_t offset, size_t maxlen)
{
return Factory::parseDomainName(value, buf, offset, maxlen);
}
Expand All @@ -259,7 +247,7 @@ class CNAME_Record : public Record
std::string value;
Record * clone() { return new CNAME_Record(*this); }
~CNAME_Record() {}
size_t parseFromBuffer(const char *buf, size_t offset, size_t maxlen)
size_t parseFromBuffer(const u8 *buf, size_t offset, size_t maxlen)
{
return Factory::parseDomainName(value, buf, offset, maxlen);
}
Expand All @@ -272,7 +260,7 @@ class Query
u16 record_type;
u16 record_class;

size_t parseFromBuffer(const char *buf, size_t offset, size_t maxlen);
size_t parseFromBuffer(const u8 *buf, size_t offset, size_t maxlen);
};

class Answer
Expand All @@ -292,7 +280,7 @@ class Answer
Record * record;

// Populate the object reading from buffer and returns "consumed" bytes
size_t parseFromBuffer(const char *buf, size_t offset, size_t maxlen);
size_t parseFromBuffer(const u8 *buf, size_t offset, size_t maxlen);
Answer& operator=(const Answer &r);
};

Expand All @@ -305,8 +293,8 @@ class Packet
void addFlags(FLAGS fl){ flags |= fl; }
void removeFlags(FLAGS fl){ flags &= ~fl; }
void resetFlags() { flags = 0; }
size_t writeToBuffer(char *buf, size_t maxlen);
size_t parseFromBuffer(const char *buf, size_t maxlen);
size_t writeToBuffer(u8 *buf, size_t maxlen);
size_t parseFromBuffer(const u8 *buf, size_t maxlen);

u16 id;
u16 flags;
Expand Down
6 changes: 3 additions & 3 deletions tests/nmap_dns_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ int main()
std::string target = "scanme.nmap.org";
DNS::RECORD_TYPE rt = DNS::A;
const size_t buflen = 1500;
char buf[buflen];
u8 buf[buflen];
size_t reqlen = DNS::Factory::buildSimpleRequest(target, rt, buf, buflen);

DNS::Packet p;
Expand Down Expand Up @@ -177,7 +177,7 @@ int main()
0x00, 0x04, // Record Lenght
0x2d, 0x21, 0x20, 0x9c }; // 45.33.32.156

plen = p.parseFromBuffer((const char*)answere_buf, 49);
plen = p.parseFromBuffer(answere_buf, answere_len);
TEST_INCR(answere_len == plen, ret);

q = &*p.queries.begin();
Expand Down Expand Up @@ -234,7 +234,7 @@ int main()
0x6f, 0x72, 0x67, // "org"
0x00 }; // Name terminator

plen = p.parseFromBuffer((const char*)ptr_answere, ptr_answere_len);
plen = p.parseFromBuffer(ptr_answere, ptr_answere_len);
TEST_INCR(plen == ptr_answere_len, ret);
TEST_INCR(p.id == 0x08f2, ret);
TEST_INCR(p.flags == 0x8180, ret);
Expand Down

0 comments on commit 18b6b9c

Please sign in to comment.