main.cc 21 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_BOOSTLIB
32 33
#include <boost/bind.hpp>
#include <boost/asio.hpp>
JINMEI Tatuya's avatar
JINMEI Tatuya committed
34
#endif  // HAVE_BOOSTLIB
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
#include "spec_config.h"
47
#include "common.h"
48 49 50
#include "auth_srv.h"

using namespace std;
51

52
#ifdef HAVE_BOOSTLIB
53 54 55
using namespace boost::asio;
using ip::udp;
using ip::tcp;
JINMEI Tatuya's avatar
JINMEI Tatuya committed
56
#endif  // HAVE_BOOSTLIB
57

58 59 60
using namespace isc::data;
using namespace isc::cc;
using namespace isc::config;
61
using namespace isc::dns;
62

63 64
namespace {

JINMEI Tatuya's avatar
JINMEI Tatuya committed
65 66
bool verbose_mode = false;

67
const string PROGRAM = "Auth";
68
const char* DNSPORT = "5300";
69 70 71 72

/* need global var for config/command handlers.
 * todo: turn this around, and put handlers in the authserver
 * class itself? */
73
AuthSrv *auth_server;
74
#ifdef HAVE_BOOSTLIB
75 76 77
// 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_;
78
#else
79
bool running;
JINMEI Tatuya's avatar
JINMEI Tatuya committed
80
#endif  // HAVE_BOOSTLIB
81

JINMEI Tatuya's avatar
JINMEI Tatuya committed
82
ElementPtr
83
my_config_handler(ElementPtr new_config) {
84 85 86
    return auth_server->updateConfig(new_config);
}

JINMEI Tatuya's avatar
JINMEI Tatuya committed
87
ElementPtr
88
my_command_handler(const string& command, const ElementPtr args) {
89
    ElementPtr answer = createAnswer();
90

91
    if (command == "print_message") {
92 93 94
        cout << args << endl;
        /* let's add that message to our answer as well */
        answer->get("result")->add(args);
95
    } else if (command == "shutdown") {
96
#ifdef HAVE_BOOSTLIB
97
        io_service_.stop();
98 99
#else
        running = false;
JINMEI Tatuya's avatar
JINMEI Tatuya committed
100
#endif  // HAVE_BOOSTLIB
101
    }
102
    
103 104 105 106
    return answer;
}

#ifdef HAVE_BOOSTLIB
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
//
// 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) {
133
            InputBuffer dnsbuffer(data_, bytes_transferred);
134 135 136

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

138 139 140 141 142 143 144 145 146 147 148 149 150 151
                       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);
            if (auth_server->processMessage(dnsbuffer, dns_message_,
152
                                            response_renderer_, false)) {
153 154 155 156 157
                responselen_buffer_.writeUint16(response_buffer_.getLength());
                async_write(socket_,
                            boost::asio::buffer(
                                responselen_buffer_.getData(),
                                responselen_buffer_.getLength()),
JINMEI Tatuya's avatar
JINMEI Tatuya committed
158 159
                            boost::bind(&TCPClient::responseWrite, this,
                                        placeholders::error));
160 161 162 163 164 165 166 167
            } else {
                delete this;
            }
        } else {
            delete this;
        }
    }

JINMEI Tatuya's avatar
JINMEI Tatuya committed
168
    void responseWrite(const boost::system::error_code& error) {
169 170 171 172 173 174
        if (!error) {
                async_write(socket_,
                            boost::asio::buffer(response_buffer_.getData(),
                                                response_buffer_.getLength()),
                        boost::bind(&TCPClient::handleWrite, this,
                                    placeholders::error));
175 176
        } else {
            delete this;
177 178 179
        }
    }

JINMEI Tatuya's avatar
JINMEI Tatuya committed
180
    void handleWrite(const boost::system::error_code& error) {
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
        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
199
class TCPServer {
200 201
public:
    TCPServer(io_service& io_service, int af, short port) :
202
        io_service_(io_service), acceptor_(io_service_),
203
        listening_(new TCPClient(io_service_))
204
    {
205 206
        tcp::endpoint endpoint(af == AF_INET6 ? tcp::v6() : tcp::v4(), port);
        acceptor_.open(endpoint.protocol());
207 208 209
        // Set v6-only (we use a different instantiation for v4,
        // otherwise asio will bind to both v4 and v6
        if (af == AF_INET6) {
210
            acceptor_.set_option(ip::v6_only(true));
211
        }
212
        acceptor_.set_option(tcp::acceptor::reuse_address(true));
213 214
        acceptor_.bind(endpoint);
        acceptor_.listen();
215
        acceptor_.async_accept(listening_->getSocket(),
216
                               boost::bind(&TCPServer::handleAccept, this,
217
                                           listening_, placeholders::error));
218 219
    }

220
    ~TCPServer() { delete listening_; }
221

222 223 224 225
    void handleAccept(TCPClient* new_client,
                      const boost::system::error_code& error)
    {
        if (!error) {
226
            assert(new_client == listening_);
227
            new_client->start();
228
            listening_ = new TCPClient(io_service_);
229
            acceptor_.async_accept(listening_->getSocket(),
230
                                   boost::bind(&TCPServer::handleAccept,
231
                                               this, listening_,
232 233 234 235 236 237 238 239 240
                                               placeholders::error));
        } else {
            delete new_client;
        }
    }

private:
    io_service& io_service_;
    tcp::acceptor acceptor_;
241
    TCPClient* listening_;
242 243 244 245
};

class UDPServer {
public:
246
    UDPServer(io_service& io_service, int af, short port) :
247
        io_service_(io_service),
248
        socket_(io_service, af == AF_INET6 ? udp::v6() : udp::v4()),
249 250 251 252
        response_buffer_(0),
        response_renderer_(response_buffer_),
        dns_message_(Message::PARSE)
    {
253 254 255
        // Set v6-only (we use a different instantiation for v4,
        // otherwise asio will bind to both v4 and v6
        if (af == AF_INET6) {
256
            socket_.set_option(boost::asio::ip::v6_only(true));
257 258 259 260
            socket_.bind(udp::endpoint(udp::v6(), port));
        } else {
            socket_.bind(udp::endpoint(udp::v4(), port));
        }
261 262 263 264 265 266 267 268 269 270 271 272
        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_,
273
                                            response_renderer_, true)) {
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
                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();
        }
    }

290 291
    void sendCompleted(const boost::system::error_code& error UNUSED_PARAM,
                       size_t bytes_sent UNUSED_PARAM)
292
    {
293 294
        // Even if error occurred there's nothing to do.  Simply handle
        // the next request.
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
        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];
};
316 317 318 319 320

struct ServerSet {
    ServerSet() : udp4_server(NULL), udp6_server(NULL),
                  tcp4_server(NULL), tcp6_server(NULL)
    {}
JINMEI Tatuya's avatar
JINMEI Tatuya committed
321
    ~ServerSet() {
322 323 324 325 326 327 328 329 330 331 332
        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
333
void
334
run_server(const char* port, const bool use_ipv4, const bool use_ipv6,
335
           AuthSrv* srv)
336 337 338 339 340
{
    ServerSet servers;
    short portnum = atoi(port);

    if (use_ipv4) {
341 342
        servers.udp4_server = new UDPServer(io_service_, AF_INET, portnum);
        servers.tcp4_server = new TCPServer(io_service_, AF_INET, portnum);
343 344
    }
    if (use_ipv6) {
345 346
        servers.udp6_server = new UDPServer(io_service_, AF_INET6, portnum);
        servers.tcp6_server = new TCPServer(io_service_, AF_INET6, portnum);
347 348 349
    }

    cout << "Server started." << endl;
350
    io_service_.run();
351 352 353 354
}
#else  // !HAVE_BOOSTLIB
struct SocketSet {
    SocketSet() : ups4(-1), tps4(-1), ups6(-1), tps6(-1) {}
JINMEI Tatuya's avatar
JINMEI Tatuya committed
355
    ~SocketSet() {
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
        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
372
int
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
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
407
int
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
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;
        }
432
        // proceed anyway
433 434 435 436 437 438 439 440 441 442
    }

    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");
    }

443 444 445
    if (listen(s, 100) < 0) {
        isc_throw(FatalError, "failed to listen on a TCP socket");
    }
446
    return (s);
447 448
}

JINMEI Tatuya's avatar
JINMEI Tatuya committed
449
void
450 451 452 453 454 455 456 457 458 459 460 461 462 463
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,
464
                                        true)) {
465 466 467 468 469
            cc = sendto(fd, response_renderer.getData(),
                        response_renderer.getLength(), 0, sa, sa_len);
            if (cc != response_renderer.getLength()) {
                cerr << "UDP send error" << endl;
            }
470
        }
471 472
    } else if (verbose_mode) {
        cerr << "UDP receive error" << endl;
473
    }
474 475
}

476 477
// 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
478
void
479 480
processMessageTCP(const int fd, Message& dns_message,
                  MessageRenderer& response_renderer)
481
{
482 483 484 485 486
    struct sockaddr_storage ss;
    socklen_t sa_len = sizeof(ss);
    struct sockaddr* sa = static_cast<struct sockaddr*>((void*)&ss);
    char sizebuf[2];
    int cc;
487

488
    int ts = accept(fd, sa, &sa_len);
489 490 491 492 493 494
    if (ts < 0) {
        if (verbose_mode) {
            cerr << "[XX] TCP accept failure:" << endl;
            return;
        }
    }
495

496 497 498
    if (verbose_mode) {
        cerr << "[XX] process TCP" << endl;
    }
499
    cc = recv(ts, sizebuf, 2, 0);
500 501 502 503 504 505 506
    if (cc < 0) {
        if (verbose_mode) {
            cerr << "[XX] TCP recv failure:" << endl;
        }
        close(ts);
        return;
    }
507 508 509
    if (verbose_mode) {
        cerr << "[XX] got: " << cc << endl;
    }
510 511 512
    uint16_t size, size_n;
    memcpy(&size_n, sizebuf, 2);
    size = ntohs(size_n);
513 514 515
    if (verbose_mode) {
        cerr << "[XX] got: " << size << endl;
    }
516 517 518 519 520

    vector<char> message_buffer;
    message_buffer.reserve(size);
    cc = 0;
    while (cc < size) {
521 522 523
        if (verbose_mode) {
            cerr << "[XX] cc now: " << cc << " of " << size << endl;
        }
524 525 526 527
        const int cc0 = recv(ts, &message_buffer[0] + cc, size - cc, 0);
        if (cc0 < 0) {
            if (verbose_mode) {
                cerr << "TCP receive error" << endl;
528 529
                close(ts);
                return;
530 531
            }
        }
532 533 534 535 536
        if (cc0 == 0) {
            // client closed connection
            close(ts);
            return;
        }
537
        cc += cc0;
538 539 540 541 542 543
    }

    InputBuffer buffer(&message_buffer[0], size);
    dns_message.clear(Message::PARSE);
    response_renderer.clear();
    if (auth_server->processMessage(buffer, dns_message, response_renderer,
544
                                    false)) {
545 546 547 548 549 550
        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) {
551 552 553 554
                if (verbose_mode) {
                    cerr << "[AuthSrv] error in sending TCP response message" <<
                        endl;
                }
555
            } else {
556 557 558 559
                if (verbose_mode) {
                    cerr << "[XX] sent TCP response: " << cc << " bytes"
                         << endl;
                }
560
            }
561 562 563 564
        } else {
            if (verbose_mode) {
                cerr << "TCP send error" << endl;
            }
565 566 567 568 569
        }
    }
 
   // TODO: we don't check for more queries on the stream atm
    close(ts);
570 571
}

JINMEI Tatuya's avatar
JINMEI Tatuya committed
572
void
573
run_server(const char* port, const bool use_ipv4, const bool use_ipv6,
574
           AuthSrv* srv)
575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
{
    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;
598

599 600
    cout << "Server started." << endl;
    
601 602 603 604 605
    if (srv->configSession() == NULL) {
        isc_throw(FatalError, "Config session not initalized");
    }

    int ss = srv->configSession()->getSocket();
606 607 608 609
    Message dns_message(Message::PARSE);
    OutputBuffer resonse_buffer(0);
    MessageRenderer response_renderer(resonse_buffer);

610 611
    running = true;
    while (running) {
612 613
        fd_set fds = fds_base;
        FD_SET(ss, &fds);
614
        ++nfds;
615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636

        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)) {
637
            srv->configSession()->checkCommand();
638
        }
639
    }
640 641 642
}
#endif // HAVE_BOOSTLIB

JINMEI Tatuya's avatar
JINMEI Tatuya committed
643
void
644 645 646
usage() {
    cerr << "Usage: b10-auth [-p port] [-4|-6]" << endl;
    exit(1);
647
}
JINMEI Tatuya's avatar
JINMEI Tatuya committed
648
} // end of anonymous namespace
649 650 651 652

int
main(int argc, char* argv[]) {
    int ch;
653
    const char* port = DNSPORT;
654
    bool use_ipv4 = true, use_ipv6 = true;
655

656
    while ((ch = getopt(argc, argv, "46p:v")) != -1) {
657
        switch (ch) {
658
        case '4':
659 660 661 662 663
            // 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;
664 665
            break;
        case '6':
666 667
            // The same note as -4 applies.
            use_ipv4 = false;
668
            break;
669
        case 'p':
670
            port = optarg;
671
            break;
672 673 674
        case 'v':
            verbose_mode = true;
            break;
675 676 677 678 679 680
        case '?':
        default:
            usage();
        }
    }

681
    if (argc - optind > 0) {
682
        usage();
683 684
    }

685
    if (!use_ipv4 && !use_ipv6) {
686 687 688 689
        cerr << "-4 and -6 can't coexist" << endl;
        usage();
    }

690
    // initialize command channel
691
    int ret = 0;
692
    try {
693
        string specfile;
694
        if (getenv("B10_FROM_SOURCE")) {
695 696
            specfile = string(getenv("B10_FROM_SOURCE")) +
                "/src/bin/auth/auth.spec";
697
        } else {
698
            specfile = string(AUTH_SPECFILE_LOCATION);
699
        }
700

701 702 703 704 705 706 707 708 709 710 711 712 713 714
        auth_server = new AuthSrv;
        auth_server->setVerbose(verbose_mode);

#ifdef HAVE_BOOSTLIB
        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);
715 716
    } catch (const std::exception& ex) {
        cerr << ex.what() << endl;
717
        ret = 1;
718
    }
719 720 721

    delete auth_server;
    return (ret);
722
}