]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/dynmessenger.cc
Merge branch 'remove-windows-services' of github.com:zeha/pdns into zeha-remove-windo...
[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 &localdir, const string &fname)
32 {
33 d_s=socket(AF_UNIX,SOCK_STREAM,0);
34 Utility::setCloseOnExec(d_s);
35
36 if(d_s<0) {
37 throw PDNSException(string("socket")+strerror(errno));
38 }
39
40 string localname=localdir;
41
42 localname+="/lsockXXXXXX";
43 if (makeUNsockaddr(localname, &d_local))
44 throw PDNSException("Unable to bind to local temporary file, path '"+localname+"' is not a valid UNIX socket path.");
45
46 if(mkstemp(d_local.sun_path)<0)
47 throw PDNSException("Unable to generate local temporary file: "+stringerror());
48
49 unlink(d_local.sun_path);
50
51 try {
52 if(bind(d_s, (sockaddr*)&d_local,sizeof(d_local))<0)
53 throw PDNSException("Unable to bind to local temporary file: "+stringerror());
54
55 // make sure that pdns can reply!
56 if(chmod(d_local.sun_path,0666)<0)
57 throw PDNSException("Unable to chmod local temporary file: "+stringerror());
58
59 if(makeUNsockaddr(fname, &d_remote))
60 throw PDNSException("Unable to connect to remote '"+fname+"': Path is not a valid UNIX socket path.");
61
62 if(connect(d_s,(sockaddr*)&d_remote,sizeof(d_remote))<0)
63 throw PDNSException("Unable to connect to remote '"+fname+"': "+stringerror());
64
65 } catch(...) {
66 close(d_s);
67 d_s=-1;
68 unlink(d_local.sun_path);
69 throw;
70 }
71 }
72
73 DynMessenger::DynMessenger(const ComboAddress& remote, const string &secret)
74 {
75 *d_local.sun_path=0;
76 d_s=socket(AF_INET, SOCK_STREAM,0);
77 Utility::setCloseOnExec(d_s);
78
79 if(d_s<0) {
80 throw PDNSException(string("socket")+strerror(errno));
81 }
82
83 if(connect(d_s, (sockaddr*)&remote, remote.getSocklen())<0) {
84 close(d_s);
85 d_s=-1;
86 throw PDNSException("Unable to connect to remote '"+remote.toStringWithPort()+"': "+string(strerror(errno)));
87 }
88
89 string login=secret+"\n";
90 writen2(d_s, login);
91 }
92
93 DynMessenger::~DynMessenger()
94 {
95 if (d_s > 0)
96 close(d_s);
97 if(*d_local.sun_path && unlink(d_local.sun_path)<0)
98 cerr<<"Warning: unable to unlink local unix domain endpoint: "<<strerror(errno)<<endl;
99 }
100
101 int DynMessenger::send(const string &msg) const
102 {
103 if(writen2(d_s, msg+"\n") < 0) { // sue me
104 perror("sendto");
105 return -1;
106 }
107 return 0;
108 }
109
110 string DynMessenger::receive() const
111 {
112 char buffer[1500];
113
114 int retlen;
115 string answer;
116 for(;;) {
117 retlen=recv(d_s,buffer,sizeof(buffer),0);
118 if(retlen<0)
119 throw PDNSException("Error from remote: "+string(strerror(errno)));
120
121 answer.append(buffer,retlen);
122 if (retlen == 0)
123 break;
124 }
125
126 return answer;
127 }
128