~ubuntu-branches/ubuntu/maverick/pdns/maverick-updates

« back to all changes in this revision

Viewing changes to pdns/nameserver.cc

  • Committer: Bazaar Package Importer
  • Author(s): Christoph Haas
  • Date: 2009-02-25 23:25:51 UTC
  • mfrom: (1.1.7 upstream) (12.1.3 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090225232551-ts3d9k9q0ti442i9
Tags: 2.9.22-1
New upstream version (closes: #513409).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
    Copyright (C) 2002 - 2005  PowerDNS.COM BV
 
2
    Copyright (C) 2002 - 2007  PowerDNS.COM BV
3
3
 
4
4
    This program is free software; you can redistribute it and/or modify
5
5
    it under the terms of the GNU General Public License version 2 as 
23
23
#include <iostream>
24
24
#include <string>
25
25
#include <sys/types.h>
26
 
 
 
26
#include <boost/shared_ptr.hpp>
27
27
#include "dns.hh"
28
28
#include "dnsbackend.hh"
29
29
#include "dnspacket.hh"
33
33
#include "arguments.hh"
34
34
#include "statbag.hh"
35
35
 
 
36
using namespace boost;
 
37
 
36
38
extern StatBag S;
37
39
 
38
40
/** \mainpage 
40
42
    own backend, see the documentation for the DNSBackend class.
41
43
 
42
44
    \section copyright Copyright and License
43
 
    PowerDNS is (C) 2005 PowerDNS.COM BV. It is distributed according to the terms of the General Public License version 2.
 
45
    PowerDNS is (C) 2001-2008 PowerDNS.COM BV. It is distributed according to the terms of the General Public License version 2.
44
46
 
45
47
    \section overview High level overview
46
48
 
77
79
void UDPNameserver::bindIPv4()
78
80
{
79
81
  vector<string>locals;
80
 
  stringtok(locals,arg()["local-address"]," ,");
 
82
  stringtok(locals,::arg()["local-address"]," ,");
81
83
 
82
84
  if(locals.empty())
83
85
    throw AhuException("No local address specified");
91
93
    if(s<0)
92
94
      throw AhuException("Unable to acquire a UDP socket: "+string(strerror(errno)));
93
95
  
 
96
    if(locals.size() > 1 && !Utility::setNonBlocking(s))
 
97
      throw AhuException("Unable to set UDP socket to non-blocking: "+stringerror());
 
98
  
94
99
    memset(&locala,0,sizeof(locala));
95
100
    locala.sin_family=AF_INET;
96
101
 
109
114
      locala.sin_addr.s_addr=*(int*)h->h_addr;
110
115
    }
111
116
 
112
 
    locala.sin_port=htons(arg().asNum("local-port"));
 
117
    locala.sin_port=htons(::arg().asNum("local-port"));
113
118
    
114
 
    if(bind(s, (sockaddr*)&locala,sizeof(locala))<0) {
115
 
      L<<Logger::Error<<"binding UDP socket to '"+localname+"': "<<strerror(errno)<<endl;
 
119
    if(::bind(s, (sockaddr*)&locala,sizeof(locala))<0) {
 
120
      L<<Logger::Error<<"binding UDP socket to '"+localname+"' port "+lexical_cast<string>(ntohs(locala.sin_port))+": "<<strerror(errno)<<endl;
116
121
      throw AhuException("Unable to bind to UDP socket");
117
122
    }
118
123
    d_highfd=max(s,d_highfd);
119
124
    d_sockets.push_back(s);
120
 
    L<<Logger::Error<<"UDP server bound to "<<inet_ntoa(locala.sin_addr)<<":"<<arg().asNum("local-port")<<endl;
 
125
    L<<Logger::Error<<"UDP server bound to "<<inet_ntoa(locala.sin_addr)<<":"<<::arg().asNum("local-port")<<endl;
121
126
    FD_SET(s, &d_rfds);
122
127
  }
123
128
}
126
131
{
127
132
#if !WIN32 && HAVE_IPV6
128
133
  vector<string>locals;
129
 
  stringtok(locals,arg()["local-ipv6"]," ,");
 
134
  stringtok(locals,::arg()["local-ipv6"]," ,");
130
135
 
131
136
  if(locals.empty())
132
137
    return;
143
148
      L<<Logger::Warning<<"It is advised to bind to explicit addresses with the --local-ipv6 option"<<endl;
144
149
    }
145
150
    
146
 
    ComboAddress locala(localname, arg().asNum("local-port"));
 
151
    ComboAddress locala(localname, ::arg().asNum("local-port"));
147
152
 
148
 
    if(bind(s, (sockaddr*)&locala, sizeof(locala))<0) {
 
153
    if(::bind(s, (sockaddr*)&locala, sizeof(locala))<0) {
149
154
      L<<Logger::Error<<"binding to UDP ipv6 socket: "<<strerror(errno)<<endl;
150
155
      throw AhuException("Unable to bind to UDP ipv6 socket");
151
156
    }
152
157
    d_highfd=max(s,d_highfd);
153
158
    d_sockets.push_back(s);
154
 
    L<<Logger::Error<<"UDPv6 server bound to ["<<localname<<"]:"<<arg().asNum("local-port")<<endl;
 
159
    L<<Logger::Error<<"UDPv6 server bound to ["<<localname<<"]:"<<::arg().asNum("local-port")<<endl;
155
160
    FD_SET(s, &d_rfds);
156
161
  }
157
162
#endif // WIN32
161
166
{
162
167
  d_highfd=0;
163
168
  FD_ZERO(&d_rfds);  
164
 
  if(!arg()["local-address"].empty())
 
169
  if(!::arg()["local-address"].empty())
165
170
    bindIPv4();
166
 
  if(!arg()["local-ipv6"].empty())
 
171
  if(!::arg()["local-ipv6"].empty())
167
172
    bindIPv6();
168
173
 
169
 
  if(arg()["local-address"].empty() && arg()["local-ipv6"].empty()) 
 
174
  if(::arg()["local-address"].empty() && ::arg()["local-ipv6"].empty()) 
170
175
    L<<Logger::Critical<<"PDNS is deaf and mute! Not listening on any interfaces"<<endl;
171
176
    
172
177
}
175
180
{
176
181
  const char *buffer=p->getData();
177
182
  DLOG(L<<Logger::Notice<<"Sending a packet to "<< p->remote.toString() <<" ("<<p->len<<" octets)"<<endl);
178
 
  if(p->len>512) {
179
 
    p=new DNSPacket(*p);
180
 
    p->truncate(512);
181
 
    buffer=p->getData();
182
 
    if(sendto(p->getSocket(),buffer,p->len,0,(struct sockaddr *)(&p->remote),p->remote.getSocklen())<0) 
183
 
      L<<Logger::Error<<"Error sending reply with sendto (socket="<<p->getSocket()<<"): "<<strerror(errno)<<endl;
184
 
    delete p;
 
183
  if(p->len > p->getMaxReplyLen()) {
 
184
    shared_ptr<DNSPacket> sharedp(new DNSPacket(*p));
 
185
    sharedp->truncate(p->getMaxReplyLen());
 
186
    buffer=sharedp->getData();
 
187
    if(sendto(sharedp->getSocket(),buffer,sharedp->len,0,(struct sockaddr *)(&sharedp->remote), sharedp->remote.getSocklen())<0) 
 
188
      L<<Logger::Error<<"Error sending reply with sendto (socket="<<sharedp->getSocket()<<"): "<<strerror(errno)<<endl;
185
189
  }
186
190
  else {
187
191
    if(sendto(p->getSocket(),buffer,p->len,0,(struct sockaddr *)(&p->remote),p->remote.getSocklen())<0)