]>
Commit | Line | Data |
---|---|---|
12c86877 BH |
1 | /* |
2 | PowerDNS Versatile Database Driven Nameserver | |
3 | Copyright (C) 2002 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 as published by | |
7 | the Free Software Foundation; either version 2 of the License, or | |
8 | (at your option) any later version. | |
9 | ||
10 | This program is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | GNU General Public License for more details. | |
14 | ||
15 | You should have received a copy of the GNU General Public License | |
16 | along with this program; if not, write to the Free Software | |
17 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 | */ | |
19 | #include "dynmessenger.hh" | |
20 | #include <cstdio> | |
21 | #include <cstring> | |
22 | #include <cerrno> | |
23 | #include <iostream> | |
24 | #include <sys/types.h> | |
25 | #include <sys/stat.h> | |
26 | ||
27 | DynMessenger::DynMessenger(const string &localdir, const string &fname) | |
28 | { | |
cc3afe25 | 29 | d_s=socket(AF_UNIX,SOCK_STREAM,0); |
12c86877 BH |
30 | |
31 | if(d_s<0) { | |
32 | throw AhuException(string("socket")+strerror(errno)); | |
33 | } | |
34 | ||
35 | memset(&d_local,0,sizeof(d_local)); | |
36 | ||
37 | string localname=localdir; | |
38 | ||
39 | localname+="/lsockXXXXXX"; | |
40 | d_local.sun_family=AF_UNIX; | |
41 | strcpy(d_local.sun_path,localname.c_str()); | |
42 | ||
43 | if(mkstemp(d_local.sun_path)<0) | |
44 | throw AhuException("Unable to generate local temporary file: "+string(strerror(errno))); | |
45 | ||
46 | unlink(d_local.sun_path); | |
47 | ||
48 | if(bind(d_s, (sockaddr*)&d_local,sizeof(d_local))<0) | |
49 | throw AhuException("Unable to bind to local temporary file: "+string(strerror(errno))); | |
50 | ||
51 | if(chmod(d_local.sun_path,0666)<0) { // make sure that pdns can reply! | |
52 | perror("fchmod"); | |
53 | exit(1); | |
54 | } | |
55 | ||
56 | memset(&d_remote,0,sizeof(d_remote)); | |
57 | ||
58 | d_remote.sun_family=AF_UNIX; | |
59 | strcpy(d_remote.sun_path,fname.c_str()); | |
cc3afe25 BH |
60 | if(connect(d_s,(sockaddr*)&d_remote,sizeof(d_remote))<0) |
61 | throw AhuException("Unable to connect to remote '"+fname+"': "+string(strerror(errno))); | |
62 | ||
12c86877 BH |
63 | } |
64 | ||
65 | DynMessenger::~DynMessenger() | |
66 | { | |
67 | if(unlink(d_local.sun_path)<0) | |
68 | cerr<<"Warning: unable to unlink local unix domain endpoint: "<<strerror(errno)<<endl; | |
69 | close(d_s); | |
70 | } | |
71 | ||
72 | int DynMessenger::send(const string &msg) const | |
73 | { | |
cc3afe25 BH |
74 | if(::send(d_s,msg.c_str(),msg.size()+1,0)<0) { |
75 | perror("sendto"); | |
76 | return -1; | |
77 | } | |
12c86877 | 78 | return 0; |
12c86877 BH |
79 | } |
80 | ||
81 | /* | |
82 | int recvfrom(int s, void *buf, size_t len, int flags, | |
83 | struct sockaddr *from, socklen_t *fromlen); | |
84 | */ | |
85 | ||
86 | string DynMessenger::receive() const | |
87 | { | |
cc3afe25 | 88 | char buffer[1500]; |
12c86877 BH |
89 | struct sockaddr_un dontcare; |
90 | unsigned int len=sizeof(dontcare); | |
91 | int retlen; | |
cc3afe25 BH |
92 | string answer; |
93 | for(;;) { | |
94 | retlen=recv(d_s,buffer,sizeof(buffer),0); | |
803ca684 BH |
95 | if(retlen<0) |
96 | throw AhuException("Error from remote: "+string(strerror(errno))); | |
97 | ||
cc3afe25 BH |
98 | answer.append(buffer,retlen); |
99 | if(retlen!=sizeof(buffer)) | |
100 | break; | |
101 | } | |
102 | ||
12c86877 BH |
103 | return answer; |
104 | } | |
105 |