2
0
mirror of https://gitlab.isc.org/isc-projects/kea synced 2025-08-30 21:45:37 +00:00

more little functions, fewer big ones

git-svn-id: svn://bind10.isc.org/svn/bind10/branches/f2f200910@211 e5f2f494-b856-4b98-b285-d166d9295462
This commit is contained in:
Evan Hunt
2009-10-30 00:41:55 +00:00
parent 537ddbf8cd
commit d9df2fbfe3

View File

@@ -50,6 +50,17 @@ init_db() {
zones.serve("flame.org"); zones.serve("flame.org");
} }
Rdata::RdataPtr ns1, ns2, ns3, a, aaaa;
static void
init_server() {
ns1 = Rdata::RdataPtr(new NS("ns1.parking.com"));
ns2 = Rdata::RdataPtr(new NS("ns2.parking.com"));
ns3 = Rdata::RdataPtr(new NS("ns3.parking.com"));
a = Rdata::RdataPtr(new A("127.0.0.1"));
aaaa = Rdata::RdataPtr(new AAAA("::1"));
}
static int static int
start_server(int port) { start_server(int port) {
int s = socket(AF_INET, SOCK_DGRAM, 0); int s = socket(AF_INET, SOCK_DGRAM, 0);
@@ -71,30 +82,24 @@ start_server(int port) {
} }
static void static void
run_server(int s) { process_message(int s) {
Rdata::RdataPtr ns1, ns2, ns3; Message msg;
ns1 = Rdata::RdataPtr(new NS("ns1.parking.com"));
ns2 = Rdata::RdataPtr(new NS("ns2.parking.com"));
ns3 = Rdata::RdataPtr(new NS("ns3.parking.com"));
struct sockaddr_storage ss; struct sockaddr_storage ss;
socklen_t sa_len = sizeof(ss); socklen_t sa_len = sizeof(ss);
struct sockaddr* sa = static_cast<struct sockaddr*>((void*)&ss); struct sockaddr* sa = static_cast<struct sockaddr*>((void*)&ss);
while (true) {
Message msg;
if (msg.getBuffer().recvFrom(s, sa, &sa_len) > 0) { if (msg.getBuffer().recvFrom(s, sa, &sa_len) > 0) {
try { try {
msg.fromWire(); msg.fromWire();
} catch (...) { } catch (...) {
cerr << "parse failed" << endl; cerr << "parse failed" << endl;
continue; return;
} }
cout << "received a message:\n" << msg.toText() << endl; cout << "received a message:\n" << msg.toText() << endl;
if (msg.getSection(SECTION_QUESTION).size() != 1) if (msg.getSection(SECTION_QUESTION).size() != 1)
continue; return;
msg.makeResponse(); msg.makeResponse();
msg.setAA(true); msg.setAA(true);
@@ -104,7 +109,7 @@ run_server(int s) {
string name = query->getName().toText(true); string name = query->getName().toText(true);
if (zones.contains(name)) { if (zones.contains(name)) {
msg.setRcode(Message::RCODE_NOERROR); msg.setRcode(Message::RCODE_NOERROR);
RRset* nsset = new RRset(query->getName(), query->getClass(), RRset* nsset = new RRset(query->getName(), RRClass::IN,
RRType::NS, TTL(3600)); RRType::NS, TTL(3600));
nsset->addRdata(ns1); nsset->addRdata(ns1);
@@ -117,15 +122,12 @@ run_server(int s) {
msg.addRRset(SECTION_AUTHORITY, RRsetPtr(nsset)); msg.addRRset(SECTION_AUTHORITY, RRsetPtr(nsset));
if (query->getType() == RRType::A) { if (query->getType() == RRType::A) {
RR arr(query->getName(), query->getClass(), RR arr(query->getName(), RRClass::IN, RRType::A, TTL(3600), a);
RRType::A, TTL(3600),
Rdata::RdataPtr(new A("127.0.0.1")));
msg.addRR(SECTION_ANSWER, arr); msg.addRR(SECTION_ANSWER, arr);
} else if (query->getType() == RRType::AAAA) { } else if (query->getType() == RRType::AAAA) {
RR aaaarr(query->getName(), query->getClass(), RR aaaarr(query->getName(), RRClass::IN, RRType::AAAA,
RRType::AAAA, TTL(3600), TTL(3600), aaaa);
Rdata::RdataPtr(new AAAA("::1")));
msg.addRR(SECTION_ANSWER, aaaarr); msg.addRR(SECTION_ANSWER, aaaarr);
} }
} else { } else {
@@ -139,12 +141,17 @@ run_server(int s) {
<< " bytes):\n" << msg.toText() << endl; << " bytes):\n" << msg.toText() << endl;
msg.getBuffer().sendTo(s, *sa, sa_len); msg.getBuffer().sendTo(s, *sa, sa_len);
} }
}
static void
run_server(int s) {
while (true) {
process_message(s);
} }
} }
int int
main(int argc, char* argv[]) main(int argc, char* argv[]) {
{
Message msg; Message msg;
int ch; int ch;
int port = DNSPORT; int port = DNSPORT;
@@ -166,8 +173,9 @@ main(int argc, char* argv[])
exit(1); exit(1);
} }
// initialize DNS database // initialize
init_db(); init_db();
init_server();
// start the server // start the server
int s = start_server(port); int s = start_server(port);