main.cc 22.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// Copyright (C) 2009  Internet Systems Consortium, Inc. ("ISC")
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.

// $Id$

17
#include "config.h"
18

19 20 21 22
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <netdb.h>
Jeremy C. Reed's avatar
Jeremy C. Reed committed
23
#include <netinet/in.h>
24
#include <stdlib.h>
25
#include <errno.h>
26

27
#include <cassert>
28 29 30
#include <iostream>

#include <boost/foreach.hpp>
31
#ifdef HAVE_BOOST_SYSTEM
32 33
#include <boost/bind.hpp>
#include <boost/asio.hpp>
34
#endif  // HAVE_BOOST_SYSTEM
35 36

#include <exceptions/exceptions.h>
37

38 39
#include <dns/buffer.h>
#include <dns/message.h>
40
#include <dns/messagerenderer.h>
41

42 43 44
#include <cc/session.h>
#include <cc/data.h>
#include <config/ccsession.h>
45 46 47

#if defined(HAVE_BOOST_SYSTEM) && defined(HAVE_BOOST_PYTHON)
#define USE_XFROUT
48
#include <xfr/xfrout_client.h>
49
#endif
50

51
#include "spec_config.h"
52
#include "common.h"
53 54 55
#include "auth_srv.h"

using namespace std;
56
#ifdef USE_XFROUT
57
using namespace isc::xfr;
58
#endif
59

60
#ifdef HAVE_BOOST_SYSTEM
61 62 63
using namespace boost::asio;
using ip::udp;
using ip::tcp;
64
#endif  // HAVE_BOOST_SYSTEM
65

66 67 68
using namespace isc::data;
using namespace isc::cc;
using namespace isc::config;
69
using namespace isc::dns;
70

71 72
namespace {

JINMEI Tatuya's avatar
JINMEI Tatuya committed
73 74
bool verbose_mode = false;

75
const string PROGRAM = "Auth";
76
const char* DNSPORT = "5300";
77 78 79 80

/* need global var for config/command handlers.
 * todo: turn this around, and put handlers in the authserver
 * class itself? */
81
AuthSrv *auth_server;
82
#ifdef HAVE_BOOST_SYSTEM
83 84 85
// TODO: this should be a property of AuthSrv, and AuthSrv needs
// a stop() method (so the shutdown command can be handled)
boost::asio::io_service io_service_;
86
#else
87
bool running;
88
#endif  // HAVE_BOOST_SYSTEM
89

JINMEI Tatuya's avatar
JINMEI Tatuya committed
90
ElementPtr
91
my_config_handler(ElementPtr new_config) {
92 93 94
    return auth_server->updateConfig(new_config);
}

JINMEI Tatuya's avatar
JINMEI Tatuya committed
95
ElementPtr
96
my_command_handler(const string& command, const ElementPtr args) {
97
    ElementPtr answer = createAnswer();
98

99
    if (command == "print_message") {
100 101 102
        cout << args << endl;
        /* let's add that message to our answer as well */
        answer->get("result")->add(args);
103
    } else if (command == "shutdown") {
104
#ifdef HAVE_BOOST_SYSTEM
105
        io_service_.stop();
106 107
#else
        running = false;
108
#endif  // HAVE_BOOST_SYSTEM
109
    }
110
    
111 112 113
    return answer;
}

114
#ifdef USE_XFROUT
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
//TODO. The sample way for checking axfr query, the code should be merged to auth server class
static bool
check_axfr_query(char *msg_data, uint16_t msg_len)
{
    if (msg_len < 15)
        return false;

    uint16_t query_type = *(uint16_t *)(msg_data + (msg_len - 4));
    if ( query_type == 0xFC00)
        return true;
    
    return false;
}

//TODO. Send the xfr query to xfrout module, the code should be merged to auth server class
static void
dispatch_axfr_query(int tcp_sock, char axfr_query[], uint16_t query_len)
{
133
    std::string path = string(UNIX_SOCKET_FILE);
134 135 136 137 138 139 140 141 142 143 144
    XfroutClient xfr_client(path);
    try {
        xfr_client.connect();
        xfr_client.sendXfroutRequestInfo(tcp_sock, (uint8_t *)axfr_query, query_len);
        xfr_client.disconnect();
    }
    catch (const std::exception & err) {
        //if (verbose_mode)
            cerr << "error handle xfr query:" << err.what() << endl;
    }
}
145
#endif
146

Jelte Jansen's avatar
Jelte Jansen committed
147
#ifdef HAVE_BOOST_SYSTEM
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
//
// Helper classes for asynchronous I/O using boost::asio
//
class TCPClient {
public:
    TCPClient(io_service& io_service) :
        socket_(io_service),
        response_buffer_(0),
        responselen_buffer_(TCP_MESSAGE_LENGTHSIZE),
        response_renderer_(response_buffer_),
        dns_message_(Message::PARSE)
    {}

    void start() {
        async_read(socket_, boost::asio::buffer(data_, TCP_MESSAGE_LENGTHSIZE),
                   boost::bind(&TCPClient::headerRead, this,
                               placeholders::error,
                               placeholders::bytes_transferred));
    }

    tcp::socket& getSocket() { return (socket_); }

    void headerRead(const boost::system::error_code& error,
                    size_t bytes_transferred)
    {
        if (!error) {
174
            InputBuffer dnsbuffer(data_, bytes_transferred);
175 176 177

            uint16_t msglen = dnsbuffer.readUint16();
            async_read(socket_, boost::asio::buffer(data_, msglen),
178

179 180 181 182 183 184 185 186 187 188 189 190 191
                       boost::bind(&TCPClient::requestRead, this,
                                   placeholders::error,
                                   placeholders::bytes_transferred));
        } else {
            delete this;
        }
    }

    void requestRead(const boost::system::error_code& error,
                     size_t bytes_transferred)
    {
        if (!error) {
            InputBuffer dnsbuffer(data_, bytes_transferred);
192
#ifdef USE_XFROUT
193 194 195 196
            if (check_axfr_query(data_, bytes_transferred)) {
                dispatch_axfr_query(socket_.native(), data_, bytes_transferred); 
                // start to get new query ?
                start();
197 198
            } else {
#endif          
199 200 201 202 203 204 205 206 207 208 209 210
                if (auth_server->processMessage(dnsbuffer, dns_message_,
                                                response_renderer_, false)) {
                    responselen_buffer_.writeUint16(response_buffer_.getLength());
                    async_write(socket_,
                                boost::asio::buffer(
                                    responselen_buffer_.getData(),
                                    responselen_buffer_.getLength()),
                                boost::bind(&TCPClient::responseWrite, this,
                                            placeholders::error));
                } else {
                    delete this;
                }
211
#ifdef USE_XFROUT
212
            }
213
#endif
214 215 216 217 218
        } else {
            delete this;
        }
    }

JINMEI Tatuya's avatar
JINMEI Tatuya committed
219
    void responseWrite(const boost::system::error_code& error) {
220 221 222 223 224 225
        if (!error) {
                async_write(socket_,
                            boost::asio::buffer(response_buffer_.getData(),
                                                response_buffer_.getLength()),
                        boost::bind(&TCPClient::handleWrite, this,
                                    placeholders::error));
226 227
        } else {
            delete this;
228 229 230
        }
    }

JINMEI Tatuya's avatar
JINMEI Tatuya committed
231
    void handleWrite(const boost::system::error_code& error) {
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
        if (!error) {
            start();            // handle next request, if any.
      } else {
            delete this;
      }
    }

private:
    tcp::socket socket_;
    OutputBuffer response_buffer_;
    OutputBuffer responselen_buffer_;
    MessageRenderer response_renderer_;
    Message dns_message_;
    enum { MAX_LENGTH = 65535 };
    static const size_t TCP_MESSAGE_LENGTHSIZE = 2;
    char data_[MAX_LENGTH];
};

JINMEI Tatuya's avatar
JINMEI Tatuya committed
250
class TCPServer {
251 252
public:
    TCPServer(io_service& io_service, int af, short port) :
253
        io_service_(io_service), acceptor_(io_service_),
254
        listening_(new TCPClient(io_service_))
255
    {
256 257
        tcp::endpoint endpoint(af == AF_INET6 ? tcp::v6() : tcp::v4(), port);
        acceptor_.open(endpoint.protocol());
258 259 260
        // Set v6-only (we use a different instantiation for v4,
        // otherwise asio will bind to both v4 and v6
        if (af == AF_INET6) {
261
            acceptor_.set_option(ip::v6_only(true));
262
        }
263
        acceptor_.set_option(tcp::acceptor::reuse_address(true));
264 265
        acceptor_.bind(endpoint);
        acceptor_.listen();
266
        acceptor_.async_accept(listening_->getSocket(),
267
                               boost::bind(&TCPServer::handleAccept, this,
268
                                           listening_, placeholders::error));
269 270
    }

271
    ~TCPServer() { delete listening_; }
272

273 274 275 276
    void handleAccept(TCPClient* new_client,
                      const boost::system::error_code& error)
    {
        if (!error) {
277
            assert(new_client == listening_);
278
            new_client->start();
279
            listening_ = new TCPClient(io_service_);
280
            acceptor_.async_accept(listening_->getSocket(),
281
                                   boost::bind(&TCPServer::handleAccept,
282
                                               this, listening_,
283 284 285 286 287 288 289 290 291
                                               placeholders::error));
        } else {
            delete new_client;
        }
    }

private:
    io_service& io_service_;
    tcp::acceptor acceptor_;
292
    TCPClient* listening_;
293 294 295 296
};

class UDPServer {
public:
297
    UDPServer(io_service& io_service, int af, short port) :
298
        io_service_(io_service),
299
        socket_(io_service, af == AF_INET6 ? udp::v6() : udp::v4()),
300 301 302 303
        response_buffer_(0),
        response_renderer_(response_buffer_),
        dns_message_(Message::PARSE)
    {
304 305 306
        // Set v6-only (we use a different instantiation for v4,
        // otherwise asio will bind to both v4 and v6
        if (af == AF_INET6) {
307
            socket_.set_option(boost::asio::ip::v6_only(true));
308 309 310 311
            socket_.bind(udp::endpoint(udp::v6(), port));
        } else {
            socket_.bind(udp::endpoint(udp::v4(), port));
        }
312 313 314 315 316 317 318 319 320 321 322 323
        startReceive();
    }

    void handleRequest(const boost::system::error_code& error,
                       size_t bytes_recvd)
    {
        if (!error && bytes_recvd > 0) {
            InputBuffer request_buffer(data_, bytes_recvd);

            dns_message_.clear(Message::PARSE);
            response_renderer_.clear();
            if (auth_server->processMessage(request_buffer, dns_message_,
324
                                            response_renderer_, true)) {
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
                socket_.async_send_to(
                    boost::asio::buffer(response_buffer_.getData(),
                                        response_buffer_.getLength()),
                    sender_endpoint_,
                    boost::bind(&UDPServer::sendCompleted,
                                this,
                                placeholders::error,
                                placeholders::bytes_transferred));
            } else {
                startReceive();
            }
        } else {
            startReceive();
        }
    }

341 342
    void sendCompleted(const boost::system::error_code& error UNUSED_PARAM,
                       size_t bytes_sent UNUSED_PARAM)
343
    {
344 345
        // Even if error occurred there's nothing to do.  Simply handle
        // the next request.
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
        startReceive();
    }
private:
    void startReceive() {
        socket_.async_receive_from(
            boost::asio::buffer(data_, MAX_LENGTH), sender_endpoint_,
            boost::bind(&UDPServer::handleRequest, this,
                        placeholders::error,
                        placeholders::bytes_transferred));
    }

private:
    io_service& io_service_;
    udp::socket socket_;
    OutputBuffer response_buffer_;
    MessageRenderer response_renderer_;
    Message dns_message_;
    udp::endpoint sender_endpoint_;
    enum { MAX_LENGTH = 4096 };
    char data_[MAX_LENGTH];
};
367 368 369 370 371

struct ServerSet {
    ServerSet() : udp4_server(NULL), udp6_server(NULL),
                  tcp4_server(NULL), tcp6_server(NULL)
    {}
JINMEI Tatuya's avatar
JINMEI Tatuya committed
372
    ~ServerSet() {
373 374 375 376 377 378 379 380 381 382 383
        delete udp4_server;
        delete udp6_server;
        delete tcp4_server;
        delete tcp6_server;
    }
    UDPServer* udp4_server;
    UDPServer* udp6_server;
    TCPServer* tcp4_server;
    TCPServer* tcp6_server;
};

JINMEI Tatuya's avatar
JINMEI Tatuya committed
384
void
385
run_server(const char* port, const bool use_ipv4, const bool use_ipv6,
386
           AuthSrv* srv UNUSED_PARAM)
387 388 389 390 391
{
    ServerSet servers;
    short portnum = atoi(port);

    if (use_ipv4) {
392 393
        servers.udp4_server = new UDPServer(io_service_, AF_INET, portnum);
        servers.tcp4_server = new TCPServer(io_service_, AF_INET, portnum);
394 395
    }
    if (use_ipv6) {
396 397
        servers.udp6_server = new UDPServer(io_service_, AF_INET6, portnum);
        servers.tcp6_server = new TCPServer(io_service_, AF_INET6, portnum);
398 399 400
    }

    cout << "Server started." << endl;
401
    io_service_.run();
402
}
403
#else  // !HAVE_BOOST_SYSTEM
404 405
struct SocketSet {
    SocketSet() : ups4(-1), tps4(-1), ups6(-1), tps6(-1) {}
JINMEI Tatuya's avatar
JINMEI Tatuya committed
406
    ~SocketSet() {
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
        if (ups4 >= 0) {
            close(ups4);
        }
        if (tps4 >= 0) {
            close(tps4);
        }
        if (ups6 >= 0) {
            close(ups6);
        }
        if (tps4 >= 0) {
            close(tps6);
        }
    }
    int ups4, tps4, ups6, tps6;
};

JINMEI Tatuya's avatar
JINMEI Tatuya committed
423
int
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
getUDPSocket(int af, const char* port) {
    struct addrinfo hints, *res;

    memset(&hints, 0, sizeof(hints));
    hints.ai_family = af;
    hints.ai_socktype = SOCK_DGRAM;
    hints.ai_flags = AI_PASSIVE;
    hints.ai_protocol = IPPROTO_UDP;

    int error = getaddrinfo(NULL, port, &hints, &res);
    if (error != 0) {
        isc_throw(FatalError, "getaddrinfo failed: " << gai_strerror(error));
    }

    int s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
    if (s < 0) {
        isc_throw(FatalError, "failed to open socket");
    }

    if (af == AF_INET6) {
        int on = 1;
        if (setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on)) < 0) {
            cerr << "couldn't set IPV6_V6ONLY socket option" << endl;
            // proceed anyway
        }
    }

    if (bind(s, res->ai_addr, res->ai_addrlen) < 0) {
        isc_throw(FatalError, "binding socket failure");
    }

    return (s);
}

JINMEI Tatuya's avatar
JINMEI Tatuya committed
458
int
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
getTCPSocket(int af, const char* port) {
    struct addrinfo hints, *res;

    memset(&hints, 0, sizeof(hints));
    hints.ai_family = af;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_flags = AI_PASSIVE;
    hints.ai_protocol = IPPROTO_TCP;

    int error = getaddrinfo(NULL, port, &hints, &res);
    if (error != 0) {
        isc_throw(FatalError, "getaddrinfo failed: " << gai_strerror(error));
    }

    int s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
    if (s < 0) {
        isc_throw(FatalError, "failed to open socket");
    }

    int on = 1;
    if (af == AF_INET6) {
        if (setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on)) < 0) {
            cerr << "couldn't set IPV6_V6ONLY socket option" << endl;
        }
483
        // proceed anyway
484 485 486 487 488 489 490 491 492 493
    }

    if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) {
        cerr << "couldn't set SO_REUSEADDR socket option" << endl;
    }

    if (bind(s, res->ai_addr, res->ai_addrlen) < 0) {
        isc_throw(FatalError, "binding socket failure");
    }

494 495 496
    if (listen(s, 100) < 0) {
        isc_throw(FatalError, "failed to listen on a TCP socket");
    }
497
    return (s);
498 499
}

JINMEI Tatuya's avatar
JINMEI Tatuya committed
500
void
501 502 503 504 505 506 507 508 509 510 511 512 513 514
processMessageUDP(const int fd, Message& dns_message,
                  MessageRenderer& response_renderer)
{
    struct sockaddr_storage ss;
    socklen_t sa_len = sizeof(ss);
    struct sockaddr* sa = static_cast<struct sockaddr*>((void*)&ss);
    char recvbuf[4096];
    int cc;

    dns_message.clear(Message::PARSE);
    response_renderer.clear();
    if ((cc = recvfrom(fd, recvbuf, sizeof(recvbuf), 0, sa, &sa_len)) > 0) {
        InputBuffer buffer(recvbuf, cc);
        if (auth_server->processMessage(buffer, dns_message, response_renderer,
515
                                        true)) {
516 517 518 519 520
            cc = sendto(fd, response_renderer.getData(),
                        response_renderer.getLength(), 0, sa, sa_len);
            if (cc != response_renderer.getLength()) {
                cerr << "UDP send error" << endl;
            }
521
        }
522 523
    } else if (verbose_mode) {
        cerr << "UDP receive error" << endl;
524
    }
525 526
}

527 528
// XXX: this function does not handle partial reads or partial writes,
//      and is VERY UNSAFE - will probably be removed or rewritten
JINMEI Tatuya's avatar
JINMEI Tatuya committed
529
void
530 531
processMessageTCP(const int fd, Message& dns_message,
                  MessageRenderer& response_renderer)
532
{
533 534 535 536 537
    struct sockaddr_storage ss;
    socklen_t sa_len = sizeof(ss);
    struct sockaddr* sa = static_cast<struct sockaddr*>((void*)&ss);
    char sizebuf[2];
    int cc;
538

539
    int ts = accept(fd, sa, &sa_len);
540 541 542 543 544 545
    if (ts < 0) {
        if (verbose_mode) {
            cerr << "[XX] TCP accept failure:" << endl;
            return;
        }
    }
546

547 548 549
    if (verbose_mode) {
        cerr << "[XX] process TCP" << endl;
    }
550
    cc = recv(ts, sizebuf, 2, 0);
551 552 553 554 555 556 557
    if (cc < 0) {
        if (verbose_mode) {
            cerr << "[XX] TCP recv failure:" << endl;
        }
        close(ts);
        return;
    }
558 559 560
    if (verbose_mode) {
        cerr << "[XX] got: " << cc << endl;
    }
561 562 563
    uint16_t size, size_n;
    memcpy(&size_n, sizebuf, 2);
    size = ntohs(size_n);
564 565 566
    if (verbose_mode) {
        cerr << "[XX] got: " << size << endl;
    }
567 568 569 570 571

    vector<char> message_buffer;
    message_buffer.reserve(size);
    cc = 0;
    while (cc < size) {
572 573 574
        if (verbose_mode) {
            cerr << "[XX] cc now: " << cc << " of " << size << endl;
        }
575 576 577 578
        const int cc0 = recv(ts, &message_buffer[0] + cc, size - cc, 0);
        if (cc0 < 0) {
            if (verbose_mode) {
                cerr << "TCP receive error" << endl;
579 580
                close(ts);
                return;
581 582
            }
        }
583 584 585 586 587
        if (cc0 == 0) {
            // client closed connection
            close(ts);
            return;
        }
588
        cc += cc0;
589 590
    }

591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609
    InputBuffer buffer(&message_buffer[0], size);
    dns_message.clear(Message::PARSE);
    response_renderer.clear();
    if (auth_server->processMessage(buffer, dns_message, response_renderer,
                                    false)) {
        size = response_renderer.getLength();
        size_n = htons(size);
        if (send(ts, &size_n, 2, 0) == 2) {
            cc = send(ts, response_renderer.getData(),
                      response_renderer.getLength(), 0);
            if (cc == -1) {
                if (verbose_mode) {
                    cerr << "[AuthSrv] error in sending TCP response message" <<
                        endl;
                }
            } else {
                if (verbose_mode) {
                    cerr << "[XX] sent TCP response: " << cc << " bytes"
                         << endl;
610
                }
611
            }
612 613 614 615
        } else {
            if (verbose_mode) {
                cerr << "TCP send error" << endl;
            }
616 617 618 619 620
        }
    }
 
   // TODO: we don't check for more queries on the stream atm
    close(ts);
621 622
}

JINMEI Tatuya's avatar
JINMEI Tatuya committed
623
void
624
run_server(const char* port, const bool use_ipv4, const bool use_ipv6,
625
           AuthSrv* srv)
626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648
{
    SocketSet socket_set;
    fd_set fds_base;
    int nfds = -1;

    FD_ZERO(&fds_base);
    if (use_ipv4) {
        socket_set.ups4 = getUDPSocket(AF_INET, port);
        FD_SET(socket_set.ups4, &fds_base);
        nfds = max(nfds, socket_set.ups4);
        socket_set.tps4 = getTCPSocket(AF_INET, port);
        FD_SET(socket_set.tps4, &fds_base);
        nfds = max(nfds, socket_set.tps4);
    }
    if (use_ipv6) {
        socket_set.ups6 = getUDPSocket(AF_INET6, port);
        FD_SET(socket_set.ups6, &fds_base);
        nfds = max(nfds, socket_set.ups6);
        socket_set.tps6 = getTCPSocket(AF_INET6, port);
        FD_SET(socket_set.tps6, &fds_base);
        nfds = max(nfds, socket_set.tps6);
    }
    ++nfds;
649

650 651
    cout << "Server started." << endl;
    
652 653 654 655 656
    if (srv->configSession() == NULL) {
        isc_throw(FatalError, "Config session not initalized");
    }

    int ss = srv->configSession()->getSocket();
657 658 659 660
    Message dns_message(Message::PARSE);
    OutputBuffer resonse_buffer(0);
    MessageRenderer response_renderer(resonse_buffer);

661 662
    running = true;
    while (running) {
663 664
        fd_set fds = fds_base;
        FD_SET(ss, &fds);
665
        ++nfds;
666

Jelte Jansen's avatar
Jelte Jansen committed
667 668 669
        if (srv->configSession()->hasQueuedMsgs()) {
            srv->configSession()->checkCommand();
        }
670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690
        int n = select(nfds, &fds, NULL, NULL, NULL);
        if (n < 0) {
            if (errno != EINTR) {
                isc_throw(FatalError, "select error");
            }
            continue;
        }

        if (socket_set.ups4 >= 0 && FD_ISSET(socket_set.ups4, &fds)) {
            processMessageUDP(socket_set.ups4, dns_message, response_renderer);
        }
        if (socket_set.ups6 >= 0 && FD_ISSET(socket_set.ups6, &fds)) {
            processMessageUDP(socket_set.ups6, dns_message, response_renderer);
        }
        if (socket_set.tps4 >= 0 && FD_ISSET(socket_set.tps4, &fds)) {
            processMessageTCP(socket_set.tps4, dns_message, response_renderer);
        }
        if (socket_set.tps6 >= 0 && FD_ISSET(socket_set.tps6, &fds)) {
            processMessageTCP(socket_set.tps6, dns_message, response_renderer);
        }
        if (FD_ISSET(ss, &fds)) {
691
            srv->configSession()->checkCommand();
692
        }
693
    }
694
}
695
#endif // HAVE_BOOST_SYSTEM
696

JINMEI Tatuya's avatar
JINMEI Tatuya committed
697
void
698 699 700
usage() {
    cerr << "Usage: b10-auth [-p port] [-4|-6]" << endl;
    exit(1);
701
}
JINMEI Tatuya's avatar
JINMEI Tatuya committed
702
} // end of anonymous namespace
703 704 705 706

int
main(int argc, char* argv[]) {
    int ch;
707
    const char* port = DNSPORT;
708
    bool use_ipv4 = true, use_ipv6 = true;
709

710
    while ((ch = getopt(argc, argv, "46p:v")) != -1) {
711
        switch (ch) {
712
        case '4':
713 714 715 716 717
            // Note that -4 means "ipv4 only", we need to set "use_ipv6" here,
            // not "use_ipv4".  We could use something like "ipv4_only", but
            // we found the negatively named variable could confuse the code
            // logic.
            use_ipv6 = false;
718 719
            break;
        case '6':
720 721
            // The same note as -4 applies.
            use_ipv4 = false;
722
            break;
723
        case 'p':
724
            port = optarg;
725
            break;
726 727 728
        case 'v':
            verbose_mode = true;
            break;
729 730 731 732 733 734
        case '?':
        default:
            usage();
        }
    }

735
    if (argc - optind > 0) {
736
        usage();
737 738
    }

739
    if (!use_ipv4 && !use_ipv6) {
740 741 742 743
        cerr << "-4 and -6 can't coexist" << endl;
        usage();
    }

744
    // initialize command channel
745
    int ret = 0;
746
    try {
747
        string specfile;
748
        if (getenv("B10_FROM_SOURCE")) {
749 750
            specfile = string(getenv("B10_FROM_SOURCE")) +
                "/src/bin/auth/auth.spec";
751
        } else {
752
            specfile = string(AUTH_SPECFILE_LOCATION);
753
        }
754

755 756 757
        auth_server = new AuthSrv;
        auth_server->setVerbose(verbose_mode);

758
#ifdef HAVE_BOOST_SYSTEM
759 760 761 762 763 764 765 766 767 768
        ModuleCCSession cs(specfile, io_service_, my_config_handler,
                           my_command_handler);
#else
        ModuleCCSession cs(specfile, my_config_handler, my_command_handler);
#endif

        auth_server->setConfigSession(&cs);
        auth_server->updateConfig(ElementPtr());

        run_server(port, use_ipv4, use_ipv6, auth_server);
769 770
    } catch (const std::exception& ex) {
        cerr << ex.what() << endl;
771
        ret = 1;
772
    }
773 774 775

    delete auth_server;
    return (ret);
776
}