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