]>
git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/dynmessenger.cc
2 PowerDNS Versatile Database Driven Nameserver
3 Copyright (C) 2002 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 as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
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"
24 #include <sys/types.h>
27 DynMessenger::DynMessenger(const string
&localdir
, const string
&fname
)
29 d_s
=socket(AF_UNIX
,SOCK_STREAM
,0);
32 throw AhuException(string("socket")+strerror(errno
));
35 memset(&d_local
,0,sizeof(d_local
));
37 string localname
=localdir
;
39 localname
+="/lsockXXXXXX";
40 d_local
.sun_family
=AF_UNIX
;
41 strcpy(d_local
.sun_path
,localname
.c_str());
43 if(mkstemp(d_local
.sun_path
)<0)
44 throw AhuException("Unable to generate local temporary file: "+string(strerror(errno
)));
46 unlink(d_local
.sun_path
);
48 if(bind(d_s
, (sockaddr
*)&d_local
,sizeof(d_local
))<0) {
49 unlink(d_local
.sun_path
);
50 throw AhuException("Unable to bind to local temporary file: "+string(strerror(errno
)));
53 if(chmod(d_local
.sun_path
,0666)<0) { // make sure that pdns can reply!
54 unlink(d_local
.sun_path
);
59 memset(&d_remote
,0,sizeof(d_remote
));
61 d_remote
.sun_family
=AF_UNIX
;
62 strcpy(d_remote
.sun_path
,fname
.c_str());
63 if(connect(d_s
,(sockaddr
*)&d_remote
,sizeof(d_remote
))<0) {
64 unlink(d_local
.sun_path
);
65 throw AhuException("Unable to connect to remote '"+fname
+"': "+string(strerror(errno
)));
70 DynMessenger::~DynMessenger()
72 if(unlink(d_local
.sun_path
)<0)
73 cerr
<<"Warning: unable to unlink local unix domain endpoint: "<<strerror(errno
)<<endl
;
77 int DynMessenger::send(const string
&msg
) const
79 if(::send(d_s
,msg
.c_str(),msg
.size()+1,0)<0) {
87 int recvfrom(int s, void *buf, size_t len, int flags,
88 struct sockaddr *from, socklen_t *fromlen);
91 string
DynMessenger::receive() const
98 retlen
=recv(d_s
,buffer
,sizeof(buffer
),0);
100 throw AhuException("Error from remote: "+string(strerror(errno
)));
102 answer
.append(buffer
,retlen
);
103 if(retlen
!=sizeof(buffer
))