]>
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 | 29 | #include <cstring> |
12c86877 BH |
30 | #include <iostream> |
31 | #include <sys/types.h> | |
32 | #include <sys/stat.h> | |
33 | ||
79d65b3b | 34 | DynMessenger::DynMessenger(const string &fname, |
c7c7edb5 IM |
35 | int timeout_sec, |
36 | int timeout_usec) | |
12c86877 | 37 | { |
cc3afe25 | 38 | d_s=socket(AF_UNIX,SOCK_STREAM,0); |
3897b9e1 | 39 | setCloseOnExec(d_s); |
12c86877 BH |
40 | |
41 | if(d_s<0) { | |
a702a96c | 42 | throw PDNSException(string("socket")+stringerror()); |
12c86877 | 43 | } |
12c86877 | 44 | |
76cb4593 | 45 | try { |
76cb4593 CH |
46 | if(makeUNsockaddr(fname, &d_remote)) |
47 | throw PDNSException("Unable to connect to remote '"+fname+"': Path is not a valid UNIX socket path."); | |
48 | ||
c7c7edb5 IM |
49 | struct timeval timeout; |
50 | timeout.tv_sec = timeout_sec; | |
51 | timeout.tv_usec = timeout_usec; | |
52 | ||
53 | if (setsockopt (d_s, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout)) < 0) | |
54 | throw PDNSException("Unable to set SO_RCVTIMEO option on socket: " + stringerror()); | |
55 | ||
56 | if (setsockopt (d_s, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout, sizeof(timeout)) < 0) | |
57 | throw PDNSException("Unable to set SO_SNDTIMEO option on socket: " + stringerror()); | |
58 | ||
59 | int ret = Utility::timed_connect(d_s,(sockaddr*)&d_remote,sizeof(d_remote), timeout_sec, timeout_usec); | |
60 | ||
61 | if (ret == 0) | |
62 | throw TimeoutException("Unable to connect to remote '"+fname+"': "+stringerror()); | |
63 | else if (ret < 0) | |
76cb4593 CH |
64 | throw PDNSException("Unable to connect to remote '"+fname+"': "+stringerror()); |
65 | ||
66 | } catch(...) { | |
67 | close(d_s); | |
68 | d_s=-1; | |
76cb4593 | 69 | throw; |
8d022964 | 70 | } |
12c86877 BH |
71 | } |
72 | ||
c7c7edb5 IM |
73 | DynMessenger::DynMessenger(const ComboAddress& remote, |
74 | const string &secret, | |
75 | int timeout_sec, | |
76 | int timeout_usec) | |
040712e0 | 77 | { |
040712e0 | 78 | d_s=socket(AF_INET, SOCK_STREAM,0); |
3897b9e1 | 79 | setCloseOnExec(d_s); |
42c235e5 | 80 | |
040712e0 | 81 | if(d_s<0) { |
a702a96c | 82 | throw PDNSException(string("socket")+stringerror()); |
040712e0 | 83 | } |
c7c7edb5 IM |
84 | |
85 | try { | |
86 | struct timeval timeout; | |
87 | timeout.tv_sec = timeout_sec; | |
88 | timeout.tv_usec = timeout_usec; | |
89 | ||
90 | if (setsockopt (d_s, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout)) < 0) | |
91 | throw PDNSException("Unable to set SO_RCVTIMEO option on socket: " + stringerror()); | |
92 | ||
93 | if (setsockopt (d_s, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout, sizeof(timeout)) < 0) | |
94 | throw PDNSException("Unable to set SO_SNDTIMEO option on socket: " + stringerror()); | |
95 | ||
96 | int ret = Utility::timed_connect(d_s, (sockaddr*)&remote, remote.getSocklen(), timeout_sec, timeout_usec); | |
97 | ||
98 | if (ret == 0) | |
a702a96c | 99 | throw TimeoutException("Unable to connect to remote '"+remote.toStringWithPort()+"': "+stringerror()); |
c7c7edb5 | 100 | else if (ret < 0) |
a702a96c | 101 | throw PDNSException("Unable to connect to remote '"+remote.toStringWithPort()+"': "+stringerror()); |
c7c7edb5 IM |
102 | |
103 | string login=secret+"\n"; | |
104 | writen2(d_s, login); | |
105 | } catch(...) { | |
040712e0 | 106 | close(d_s); |
76cb4593 | 107 | d_s=-1; |
c7c7edb5 | 108 | throw; |
040712e0 | 109 | } |
040712e0 BH |
110 | } |
111 | ||
12c86877 BH |
112 | DynMessenger::~DynMessenger() |
113 | { | |
76cb4593 CH |
114 | if (d_s > 0) |
115 | close(d_s); | |
12c86877 BH |
116 | } |
117 | ||
118 | int DynMessenger::send(const string &msg) const | |
119 | { | |
c7c7edb5 | 120 | try { |
6c5596a3 | 121 | writen2(d_s, msg+"\n"); |
c7c7edb5 IM |
122 | return 0; |
123 | } catch(std::runtime_error& e) { | |
124 | if (errno == EAGAIN) | |
125 | throw TimeoutException("Error from remote in send(): " + string(e.what())); | |
126 | else | |
127 | throw PDNSException("Error from remote in send(): " + string(e.what())); | |
cc3afe25 | 128 | } |
12c86877 BH |
129 | } |
130 | ||
12c86877 BH |
131 | string DynMessenger::receive() const |
132 | { | |
cc3afe25 | 133 | char buffer[1500]; |
016e4ae9 | 134 | |
12c86877 | 135 | int retlen; |
cc3afe25 BH |
136 | string answer; |
137 | for(;;) { | |
138 | retlen=recv(d_s,buffer,sizeof(buffer),0); | |
c7c7edb5 IM |
139 | if(retlen<0) { |
140 | if (errno == EAGAIN) | |
a702a96c | 141 | throw TimeoutException("Error from remote in receive(): " + stringerror()); |
c7c7edb5 | 142 | else |
a702a96c | 143 | throw PDNSException("Error from remote in receive(): " + stringerror()); |
c7c7edb5 | 144 | } |
803ca684 | 145 | |
cc3afe25 | 146 | answer.append(buffer,retlen); |
be105fb4 | 147 | if (retlen == 0) |
cc3afe25 BH |
148 | break; |
149 | } | |
150 | ||
12c86877 BH |
151 | return answer; |
152 | } | |
153 |