Skip to content

Commit

Permalink
Added receiving socket (issue #1).
Browse files Browse the repository at this point in the history
  • Loading branch information
crondaemon committed Aug 8, 2013
1 parent dff81ee commit a769625
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
2 changes: 1 addition & 1 deletion dns_header.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ void DnsHeader::fuzz()

if (_fuzzFlags == true) {
uint16_t v = rand() % 65535;
_flags = *(DnsHeaderFlags*)&v;
_flags = *(DnsHeaderFlags*)(&v);
}

if (_fuzzNRecord[DnsPacket::R_QUESTION] == true) {
Expand Down
26 changes: 25 additions & 1 deletion dns_packet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,19 @@ DnsPacket::DnsPacket()
_udpHdr.dest = 0;
_udpHdr.len = sizeof(_udpHdr);
_udpHdr.check = 0;

_socket = -1;
_recvSocket = -1;
}

void DnsPacket::_socketCreate()
{
int on = 1;
struct sockaddr_in servaddr;

if (_socket > 0 && _recvSocket > 0) {
return;
}

_socket = socket(PF_INET, SOCK_RAW, IPPROTO_UDP);

Expand All @@ -50,6 +58,19 @@ void DnsPacket::_socketCreate()

if (setsockopt(_socket, IPPROTO_IP, IP_HDRINCL, &on, sizeof(on)) < 0)
throw runtime_error(string(__func__) + ": unable to set option _IPHDRINCL");

_recvSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (_recvSocket == -1)
throw runtime_error("Can't create listening socket");


memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = _udpHdr.source;

if (bind(_recvSocket, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0 )
throw runtime_error("Can't bind() listening socket");
}

string DnsPacket::data() const
Expand Down Expand Up @@ -158,14 +179,17 @@ void DnsPacket::sendNet(bool doCksum)
output += dns_dgram;

if (send(_socket, output.data(), output.length(), 0) < 0) {
// if (sendto(_socket, output.data(), output.length(), 0, (struct sockaddr*)&sin,
// sizeof(sin)) < 0) {
if (errno == 22) {
cout << "Invalid parameter (probably fuzzer is shaking it).\n";
} else {
throw runtime_error("sendto() error: " + string(strerror(errno)));
throw runtime_error("send() error: " + string(strerror(errno)));
}
}
}


string DnsPacket::ipFrom() const
{
char buf[INET_ADDRSTRLEN];
Expand Down
1 change: 1 addition & 0 deletions dns_packet.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

class DnsPacket {
int _socket;
int _recvSocket;

//! Creates the socket
void _socketCreate();
Expand Down

0 comments on commit a769625

Please sign in to comment.