]>
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
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 #include "dynmessenger.hh"
25 #include <sys/types.h>
28 DynMessenger::DynMessenger(const string
&localdir
, const string
&fname
)
30 d_s
=socket(AF_UNIX
,SOCK_STREAM
,0);
31 Utility::setCloseOnExec(d_s
);
34 throw PDNSException(string("socket")+strerror(errno
));
37 memset(&d_local
,0,sizeof(d_local
));
39 string localname
=localdir
;
41 localname
+="/lsockXXXXXX";
42 d_local
.sun_family
=AF_UNIX
;
43 strcpy(d_local
.sun_path
,localname
.c_str());
45 if(mkstemp(d_local
.sun_path
)<0)
46 throw PDNSException("Unable to generate local temporary file: "+string(strerror(errno
)));
48 unlink(d_local
.sun_path
);
50 if(bind(d_s
, (sockaddr
*)&d_local
,sizeof(d_local
))<0) {
51 unlink(d_local
.sun_path
);
52 throw PDNSException("Unable to bind to local temporary file: "+string(strerror(errno
)));
55 if(chmod(d_local
.sun_path
,0666)<0) { // make sure that pdns can reply!
56 unlink(d_local
.sun_path
);
61 memset(&d_remote
,0,sizeof(d_remote
));
63 d_remote
.sun_family
=AF_UNIX
;
64 strcpy(d_remote
.sun_path
,fname
.c_str());
65 if(connect(d_s
,(sockaddr
*)&d_remote
,sizeof(d_remote
))<0) {
66 unlink(d_local
.sun_path
);
67 throw PDNSException("Unable to connect to remote '"+fname
+"': "+string(strerror(errno
)));
72 DynMessenger::DynMessenger(const ComboAddress
& remote
, const string
&secret
)
75 d_s
=socket(AF_INET
, SOCK_STREAM
,0);
76 Utility::setCloseOnExec(d_s
);
79 throw PDNSException(string("socket")+strerror(errno
));
82 if(connect(d_s
, (sockaddr
*)&remote
, remote
.getSocklen())<0) {
84 throw PDNSException("Unable to connect to remote '"+remote
.toStringWithPort()+"': "+string(strerror(errno
)));
87 string login
=secret
+"\n";
91 DynMessenger::~DynMessenger()
93 if(*d_local
.sun_path
&& unlink(d_local
.sun_path
)<0)
94 cerr
<<"Warning: unable to unlink local unix domain endpoint: "<<strerror(errno
)<<endl
;
98 int DynMessenger::send(const string
&msg
) const
100 if(writen2(d_s
, msg
+"\n") < 0) { // sue me
107 string
DynMessenger::receive() const
114 retlen
=recv(d_s
,buffer
,sizeof(buffer
),0);
116 throw PDNSException("Error from remote: "+string(strerror(errno
)));
118 answer
.append(buffer
,retlen
);
119 if(retlen
!=sizeof(buffer
))