]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/dynmessenger.cc
Merge pull request #2260 from rubenk/remove-temporary-socket
[thirdparty/pdns.git] / pdns / dynmessenger.cc
1 /*
2 PowerDNS Versatile Database Driven Nameserver
3 Copyright (C) 2002 - 2008 PowerDNS.COM BV
4
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
8
9 Additionally, the license of this program contains a special
10 exception which allows to distribute the program in binary form when
11 it is linked against OpenSSL.
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 St, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22 #include "dynmessenger.hh"
23 #include <cstdio>
24 #include <cstdlib>
25 #include <cstring>
26 #include <cerrno>
27 #include <iostream>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30
31 DynMessenger::DynMessenger(const string &fname,
32 int timeout_sec,
33 int timeout_usec)
34 {
35 d_s=socket(AF_UNIX,SOCK_STREAM,0);
36 Utility::setCloseOnExec(d_s);
37
38 if(d_s<0) {
39 throw PDNSException(string("socket")+strerror(errno));
40 }
41
42 try {
43 if(makeUNsockaddr(fname, &d_remote))
44 throw PDNSException("Unable to connect to remote '"+fname+"': Path is not a valid UNIX socket path.");
45
46 struct timeval timeout;
47 timeout.tv_sec = timeout_sec;
48 timeout.tv_usec = timeout_usec;
49
50 if (setsockopt (d_s, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout)) < 0)
51 throw PDNSException("Unable to set SO_RCVTIMEO option on socket: " + stringerror());
52
53 if (setsockopt (d_s, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout, sizeof(timeout)) < 0)
54 throw PDNSException("Unable to set SO_SNDTIMEO option on socket: " + stringerror());
55
56 int ret = Utility::timed_connect(d_s,(sockaddr*)&d_remote,sizeof(d_remote), timeout_sec, timeout_usec);
57
58 if (ret == 0)
59 throw TimeoutException("Unable to connect to remote '"+fname+"': "+stringerror());
60 else if (ret < 0)
61 throw PDNSException("Unable to connect to remote '"+fname+"': "+stringerror());
62
63 } catch(...) {
64 close(d_s);
65 d_s=-1;
66 throw;
67 }
68 }
69
70 DynMessenger::DynMessenger(const ComboAddress& remote,
71 const string &secret,
72 int timeout_sec,
73 int timeout_usec)
74 {
75 d_s=socket(AF_INET, SOCK_STREAM,0);
76 Utility::setCloseOnExec(d_s);
77
78 if(d_s<0) {
79 throw PDNSException(string("socket")+strerror(errno));
80 }
81
82 try {
83 struct timeval timeout;
84 timeout.tv_sec = timeout_sec;
85 timeout.tv_usec = timeout_usec;
86
87 if (setsockopt (d_s, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout)) < 0)
88 throw PDNSException("Unable to set SO_RCVTIMEO option on socket: " + stringerror());
89
90 if (setsockopt (d_s, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout, sizeof(timeout)) < 0)
91 throw PDNSException("Unable to set SO_SNDTIMEO option on socket: " + stringerror());
92
93 int ret = Utility::timed_connect(d_s, (sockaddr*)&remote, remote.getSocklen(), timeout_sec, timeout_usec);
94
95 if (ret == 0)
96 throw TimeoutException("Unable to connect to remote '"+remote.toStringWithPort()+"': "+string(strerror(errno)));
97 else if (ret < 0)
98 throw PDNSException("Unable to connect to remote '"+remote.toStringWithPort()+"': "+string(strerror(errno)));
99
100 string login=secret+"\n";
101 writen2(d_s, login);
102 } catch(...) {
103 close(d_s);
104 d_s=-1;
105 throw;
106 }
107 }
108
109 DynMessenger::~DynMessenger()
110 {
111 if (d_s > 0)
112 close(d_s);
113 }
114
115 int DynMessenger::send(const string &msg) const
116 {
117 try {
118 if(writen2(d_s, msg+"\n") < 0) { // sue me
119 perror("sendto");
120 return -1;
121 }
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()));
128 }
129 }
130
131 string DynMessenger::receive() const
132 {
133 char buffer[1500];
134
135 int retlen;
136 string answer;
137 for(;;) {
138 retlen=recv(d_s,buffer,sizeof(buffer),0);
139 if(retlen<0) {
140 if (errno == EAGAIN)
141 throw TimeoutException("Error from remote in receive(): " + string(strerror(errno)));
142 else
143 throw PDNSException("Error from remote in receive(): " + string(strerror(errno)));
144 }
145
146 answer.append(buffer,retlen);
147 if (retlen == 0)
148 break;
149 }
150
151 return answer;
152 }
153