-
Notifications
You must be signed in to change notification settings - Fork 923
/
Copy pathdynmessenger.cc
153 lines (129 loc) · 4.41 KB
/
dynmessenger.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
* This file is part of PowerDNS or dnsdist.
* Copyright -- PowerDNS.COM B.V. and its contributors
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of version 2 of the GNU General Public License as
* published by the Free Software Foundation.
*
* In addition, for the avoidance of any doubt, permission is granted to
* link this program with OpenSSL and to (re)distribute the binaries
* produced as the result of such linking.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "dynmessenger.hh"
#include <cstdio>
#include "utility.hh"
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <sys/types.h>
#include <sys/stat.h>
DynMessenger::DynMessenger(const string &fname,
int timeout_sec,
int timeout_usec)
{
d_s=socket(AF_UNIX,SOCK_STREAM,0);
setCloseOnExec(d_s);
if(d_s<0) {
throw PDNSException(string("socket")+stringerror());
}
try {
if(makeUNsockaddr(fname, &d_remote))
throw PDNSException("Unable to connect to remote '"+fname+"': Path is not a valid UNIX socket path.");
struct timeval timeout;
timeout.tv_sec = timeout_sec;
timeout.tv_usec = timeout_usec;
if (setsockopt (d_s, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout)) < 0)
throw PDNSException("Unable to set SO_RCVTIMEO option on socket: " + stringerror());
if (setsockopt (d_s, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout, sizeof(timeout)) < 0)
throw PDNSException("Unable to set SO_SNDTIMEO option on socket: " + stringerror());
int ret = Utility::timed_connect(d_s,(sockaddr*)&d_remote,sizeof(d_remote), timeout_sec, timeout_usec);
if (ret == 0)
throw TimeoutException("Unable to connect to remote '"+fname+"': "+stringerror());
else if (ret < 0)
throw PDNSException("Unable to connect to remote '"+fname+"': "+stringerror());
} catch(...) {
close(d_s);
d_s=-1;
throw;
}
}
DynMessenger::DynMessenger(const ComboAddress& remote,
const string &secret,
int timeout_sec,
int timeout_usec)
{
d_s=socket(AF_INET, SOCK_STREAM,0);
setCloseOnExec(d_s);
if(d_s<0) {
throw PDNSException(string("socket")+stringerror());
}
try {
struct timeval timeout;
timeout.tv_sec = timeout_sec;
timeout.tv_usec = timeout_usec;
if (setsockopt (d_s, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout)) < 0)
throw PDNSException("Unable to set SO_RCVTIMEO option on socket: " + stringerror());
if (setsockopt (d_s, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout, sizeof(timeout)) < 0)
throw PDNSException("Unable to set SO_SNDTIMEO option on socket: " + stringerror());
int ret = Utility::timed_connect(d_s, (sockaddr*)&remote, remote.getSocklen(), timeout_sec, timeout_usec);
if (ret == 0)
throw TimeoutException("Unable to connect to remote '"+remote.toStringWithPort()+"': "+stringerror());
else if (ret < 0)
throw PDNSException("Unable to connect to remote '"+remote.toStringWithPort()+"': "+stringerror());
string login=secret+"\n";
writen2(d_s, login);
} catch(...) {
close(d_s);
d_s=-1;
throw;
}
}
DynMessenger::~DynMessenger()
{
if (d_s > 0)
close(d_s);
}
int DynMessenger::send(const string &msg) const
{
try {
writen2(d_s, msg+"\n");
return 0;
} catch(std::runtime_error& e) {
if (errno == EAGAIN)
throw TimeoutException("Error from remote in send(): " + string(e.what()));
else
throw PDNSException("Error from remote in send(): " + string(e.what()));
}
}
string DynMessenger::receive() const
{
char buffer[1500];
int retlen;
string answer;
for(;;) {
retlen=recv(d_s,buffer,sizeof(buffer),0);
if(retlen<0) {
if (errno == EAGAIN)
throw TimeoutException("Error from remote in receive(): " + stringerror());
else
throw PDNSException("Error from remote in receive(): " + stringerror());
}
answer.append(buffer,retlen);
if (retlen == 0)
break;
}
return answer;
}