]>
git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/dynmessenger.cc
2 PowerDNS Versatile Database Driven Nameserver
3 Copyright (C) 2002 - 2008 PowerDNS.COM BV
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License version 2
7 as published by the Free Software Foundation
9 Additionally, the license of this program contains a special
10 exception which allows to distribute the program in binary form when
11 it is linked against OpenSSL.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "dynmessenger.hh"
28 #include <sys/types.h>
31 DynMessenger::DynMessenger(const string
&localdir
, const string
&fname
)
33 d_s
=socket(AF_UNIX
,SOCK_STREAM
,0);
34 Utility::setCloseOnExec(d_s
);
37 throw PDNSException(string("socket")+strerror(errno
));
40 string localname
=localdir
;
42 localname
+="/lsockXXXXXX";
43 if (makeUNsockaddr(localname
, &d_local
))
44 throw PDNSException("Unable to bind to local temporary file, path '"+localname
+"' is not a valid UNIX socket path.");
46 if(mkstemp(d_local
.sun_path
)<0)
47 throw PDNSException("Unable to generate local temporary file: "+stringerror());
49 unlink(d_local
.sun_path
);
52 if(bind(d_s
, (sockaddr
*)&d_local
,sizeof(d_local
))<0)
53 throw PDNSException("Unable to bind to local temporary file: "+stringerror());
55 // make sure that pdns can reply!
56 if(chmod(d_local
.sun_path
,0666)<0)
57 throw PDNSException("Unable to chmod local temporary file: "+stringerror());
59 if(makeUNsockaddr(fname
, &d_remote
))
60 throw PDNSException("Unable to connect to remote '"+fname
+"': Path is not a valid UNIX socket path.");
62 if(connect(d_s
,(sockaddr
*)&d_remote
,sizeof(d_remote
))<0)
63 throw PDNSException("Unable to connect to remote '"+fname
+"': "+stringerror());
68 unlink(d_local
.sun_path
);
73 DynMessenger::DynMessenger(const ComboAddress
& remote
, const string
&secret
)
76 d_s
=socket(AF_INET
, SOCK_STREAM
,0);
77 Utility::setCloseOnExec(d_s
);
80 throw PDNSException(string("socket")+strerror(errno
));
83 if(connect(d_s
, (sockaddr
*)&remote
, remote
.getSocklen())<0) {
86 throw PDNSException("Unable to connect to remote '"+remote
.toStringWithPort()+"': "+string(strerror(errno
)));
89 string login
=secret
+"\n";
93 DynMessenger::~DynMessenger()
97 if(*d_local
.sun_path
&& unlink(d_local
.sun_path
)<0)
98 cerr
<<"Warning: unable to unlink local unix domain endpoint: "<<strerror(errno
)<<endl
;
101 int DynMessenger::send(const string
&msg
) const
103 if(writen2(d_s
, msg
+"\n") < 0) { // sue me
110 string
DynMessenger::receive() const
117 retlen
=recv(d_s
,buffer
,sizeof(buffer
),0);
119 throw PDNSException("Error from remote: "+string(strerror(errno
)));
121 answer
.append(buffer
,retlen
);