]>
Commit | Line | Data |
---|---|---|
12c86877 | 1 | /* |
12471842 PL |
2 | * This file is part of PowerDNS or dnsdist. |
3 | * Copyright -- PowerDNS.COM B.V. and its contributors | |
4 | * | |
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. | |
8 | * | |
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. | |
12 | * | |
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. | |
17 | * | |
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. | |
21 | */ | |
870a0fe4 AT |
22 | #ifdef HAVE_CONFIG_H |
23 | #include "config.h" | |
24 | #endif | |
12c86877 BH |
25 | #include "dynmessenger.hh" |
26 | #include <cstdio> | |
1bc3ebb6 | 27 | #include "utility.hh" |
8b3cfcd3 | 28 | #include <cstdlib> |
12c86877 BH |
29 | #include <cstring> |
30 | #include <cerrno> | |
31 | #include <iostream> | |
32 | #include <sys/types.h> | |
33 | #include <sys/stat.h> | |
34 | ||
79d65b3b | 35 | DynMessenger::DynMessenger(const string &fname, |
c7c7edb5 IM |
36 | int timeout_sec, |
37 | int timeout_usec) | |
12c86877 | 38 | { |
cc3afe25 | 39 | d_s=socket(AF_UNIX,SOCK_STREAM,0); |
3897b9e1 | 40 | setCloseOnExec(d_s); |
12c86877 BH |
41 | |
42 | if(d_s<0) { | |
3f81d239 | 43 | throw PDNSException(string("socket")+strerror(errno)); |
12c86877 | 44 | } |
12c86877 | 45 | |
76cb4593 | 46 | try { |
76cb4593 CH |
47 | if(makeUNsockaddr(fname, &d_remote)) |
48 | throw PDNSException("Unable to connect to remote '"+fname+"': Path is not a valid UNIX socket path."); | |
49 | ||
c7c7edb5 IM |
50 | struct timeval timeout; |
51 | timeout.tv_sec = timeout_sec; | |
52 | timeout.tv_usec = timeout_usec; | |
53 | ||
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()); | |
56 | ||
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()); | |
59 | ||
60 | int ret = Utility::timed_connect(d_s,(sockaddr*)&d_remote,sizeof(d_remote), timeout_sec, timeout_usec); | |
61 | ||
62 | if (ret == 0) | |
63 | throw TimeoutException("Unable to connect to remote '"+fname+"': "+stringerror()); | |
64 | else if (ret < 0) | |
76cb4593 CH |
65 | throw PDNSException("Unable to connect to remote '"+fname+"': "+stringerror()); |
66 | ||
67 | } catch(...) { | |
68 | close(d_s); | |
69 | d_s=-1; | |
76cb4593 | 70 | throw; |
8d022964 | 71 | } |
12c86877 BH |
72 | } |
73 | ||
c7c7edb5 IM |
74 | DynMessenger::DynMessenger(const ComboAddress& remote, |
75 | const string &secret, | |
76 | int timeout_sec, | |
77 | int timeout_usec) | |
040712e0 | 78 | { |
040712e0 | 79 | d_s=socket(AF_INET, SOCK_STREAM,0); |
3897b9e1 | 80 | setCloseOnExec(d_s); |
42c235e5 | 81 | |
040712e0 | 82 | if(d_s<0) { |
3f81d239 | 83 | throw PDNSException(string("socket")+strerror(errno)); |
040712e0 | 84 | } |
c7c7edb5 IM |
85 | |
86 | try { | |
87 | struct timeval timeout; | |
88 | timeout.tv_sec = timeout_sec; | |
89 | timeout.tv_usec = timeout_usec; | |
90 | ||
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()); | |
93 | ||
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()); | |
96 | ||
97 | int ret = Utility::timed_connect(d_s, (sockaddr*)&remote, remote.getSocklen(), timeout_sec, timeout_usec); | |
98 | ||
99 | if (ret == 0) | |
100 | throw TimeoutException("Unable to connect to remote '"+remote.toStringWithPort()+"': "+string(strerror(errno))); | |
101 | else if (ret < 0) | |
102 | throw PDNSException("Unable to connect to remote '"+remote.toStringWithPort()+"': "+string(strerror(errno))); | |
103 | ||
104 | string login=secret+"\n"; | |
105 | writen2(d_s, login); | |
106 | } catch(...) { | |
040712e0 | 107 | close(d_s); |
76cb4593 | 108 | d_s=-1; |
c7c7edb5 | 109 | throw; |
040712e0 | 110 | } |
040712e0 BH |
111 | } |
112 | ||
12c86877 BH |
113 | DynMessenger::~DynMessenger() |
114 | { | |
76cb4593 CH |
115 | if (d_s > 0) |
116 | close(d_s); | |
12c86877 BH |
117 | } |
118 | ||
119 | int DynMessenger::send(const string &msg) const | |
120 | { | |
c7c7edb5 | 121 | try { |
6c5596a3 | 122 | writen2(d_s, msg+"\n"); |
c7c7edb5 IM |
123 | return 0; |
124 | } catch(std::runtime_error& e) { | |
125 | if (errno == EAGAIN) | |
126 | throw TimeoutException("Error from remote in send(): " + string(e.what())); | |
127 | else | |
128 | throw PDNSException("Error from remote in send(): " + string(e.what())); | |
cc3afe25 | 129 | } |
12c86877 BH |
130 | } |
131 | ||
12c86877 BH |
132 | string DynMessenger::receive() const |
133 | { | |
cc3afe25 | 134 | char buffer[1500]; |
016e4ae9 | 135 | |
12c86877 | 136 | int retlen; |
cc3afe25 BH |
137 | string answer; |
138 | for(;;) { | |
139 | retlen=recv(d_s,buffer,sizeof(buffer),0); | |
c7c7edb5 IM |
140 | if(retlen<0) { |
141 | if (errno == EAGAIN) | |
142 | throw TimeoutException("Error from remote in receive(): " + string(strerror(errno))); | |
143 | else | |
144 | throw PDNSException("Error from remote in receive(): " + string(strerror(errno))); | |
145 | } | |
803ca684 | 146 | |
cc3afe25 | 147 | answer.append(buffer,retlen); |
be105fb4 | 148 | if (retlen == 0) |
cc3afe25 BH |
149 | break; |
150 | } | |
151 | ||
12c86877 BH |
152 | return answer; |
153 | } | |
154 |