]>
git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/dynmessenger.cc
2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * In addition, for the avoidance of any doubt, permission is granted to
10 * link this program with OpenSSL and to (re)distribute the binaries
11 * produced as the result of such linking.
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 Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #include "dynmessenger.hh"
32 #include <sys/types.h>
35 DynMessenger::DynMessenger(const string
&fname
,
39 d_s
=socket(AF_UNIX
,SOCK_STREAM
,0);
43 throw PDNSException(string("socket")+strerror(errno
));
47 if(makeUNsockaddr(fname
, &d_remote
))
48 throw PDNSException("Unable to connect to remote '"+fname
+"': Path is not a valid UNIX socket path.");
50 struct timeval timeout
;
51 timeout
.tv_sec
= timeout_sec
;
52 timeout
.tv_usec
= timeout_usec
;
54 if (setsockopt (d_s
, SOL_SOCKET
, SO_RCVTIMEO
, (char *)&timeout
, sizeof(timeout
)) < 0)
55 throw PDNSException("Unable to set SO_RCVTIMEO option on socket: " + stringerror());
57 if (setsockopt (d_s
, SOL_SOCKET
, SO_SNDTIMEO
, (char *)&timeout
, sizeof(timeout
)) < 0)
58 throw PDNSException("Unable to set SO_SNDTIMEO option on socket: " + stringerror());
60 int ret
= Utility::timed_connect(d_s
,(sockaddr
*)&d_remote
,sizeof(d_remote
), timeout_sec
, timeout_usec
);
63 throw TimeoutException("Unable to connect to remote '"+fname
+"': "+stringerror());
65 throw PDNSException("Unable to connect to remote '"+fname
+"': "+stringerror());
74 DynMessenger::DynMessenger(const ComboAddress
& remote
,
79 d_s
=socket(AF_INET
, SOCK_STREAM
,0);
83 throw PDNSException(string("socket")+strerror(errno
));
87 struct timeval timeout
;
88 timeout
.tv_sec
= timeout_sec
;
89 timeout
.tv_usec
= timeout_usec
;
91 if (setsockopt (d_s
, SOL_SOCKET
, SO_RCVTIMEO
, (char *)&timeout
, sizeof(timeout
)) < 0)
92 throw PDNSException("Unable to set SO_RCVTIMEO option on socket: " + stringerror());
94 if (setsockopt (d_s
, SOL_SOCKET
, SO_SNDTIMEO
, (char *)&timeout
, sizeof(timeout
)) < 0)
95 throw PDNSException("Unable to set SO_SNDTIMEO option on socket: " + stringerror());
97 int ret
= Utility::timed_connect(d_s
, (sockaddr
*)&remote
, remote
.getSocklen(), timeout_sec
, timeout_usec
);
100 throw TimeoutException("Unable to connect to remote '"+remote
.toStringWithPort()+"': "+string(strerror(errno
)));
102 throw PDNSException("Unable to connect to remote '"+remote
.toStringWithPort()+"': "+string(strerror(errno
)));
104 string login
=secret
+"\n";
113 DynMessenger::~DynMessenger()
119 int DynMessenger::send(const string
&msg
) const
122 writen2(d_s
, msg
+"\n");
124 } catch(std::runtime_error
& e
) {
126 throw TimeoutException("Error from remote in send(): " + string(e
.what()));
128 throw PDNSException("Error from remote in send(): " + string(e
.what()));
132 string
DynMessenger::receive() const
139 retlen
=recv(d_s
,buffer
,sizeof(buffer
),0);
142 throw TimeoutException("Error from remote in receive(): " + string(strerror(errno
)));
144 throw PDNSException("Error from remote in receive(): " + string(strerror(errno
)));
147 answer
.append(buffer
,retlen
);