root/trunk/pdns/pdns/dynmessenger.cc @ 680

Revision 680, 2.9 KB (checked in by ahu, 7 years ago)

the FSF has moved!

  • Property svn:eol-style set to native
  • Property svn:keywords set to author date id revision
Line 
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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  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
27DynMessenger::DynMessenger(const string &localdir, const string &fname)
28{
29  d_s=socket(AF_UNIX,SOCK_STREAM,0);
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    unlink(d_local.sun_path);
50    throw AhuException("Unable to bind to local temporary file: "+string(strerror(errno)));
51  }
52 
53  if(chmod(d_local.sun_path,0666)<0) { // make sure that pdns can reply!
54    unlink(d_local.sun_path);
55    perror("fchmod");
56    exit(1);
57  }
58
59  memset(&d_remote,0,sizeof(d_remote));
60 
61  d_remote.sun_family=AF_UNIX;
62  strcpy(d_remote.sun_path,fname.c_str());
63  if(connect(d_s,(sockaddr*)&d_remote,sizeof(d_remote))<0) {
64    unlink(d_local.sun_path);
65    throw AhuException("Unable to connect to remote '"+fname+"': "+string(strerror(errno)));
66  }
67 
68}
69
70DynMessenger::~DynMessenger()
71{
72  if(unlink(d_local.sun_path)<0)
73    cerr<<"Warning: unable to unlink local unix domain endpoint: "<<strerror(errno)<<endl;
74  close(d_s);
75}   
76
77int DynMessenger::send(const string &msg) const
78{
79  if(::send(d_s,msg.c_str(),msg.size()+1,0)<0) {
80    perror("sendto");
81    return -1;
82  }
83  return 0;
84}
85
86/*
87       int  recvfrom(int  s,  void  *buf,  size_t len, int flags,
88       struct sockaddr *from, socklen_t *fromlen);
89*/
90
91string DynMessenger::receive() const 
92{
93  char buffer[1500];
94
95  int retlen;
96  string answer;
97  for(;;) {
98    retlen=recv(d_s,buffer,sizeof(buffer),0);
99    if(retlen<0)
100      throw AhuException("Error from remote: "+string(strerror(errno)));
101
102    answer.append(buffer,retlen);
103    if(retlen!=sizeof(buffer))
104      break;
105  }
106
107  return answer;
108}
109
Note: See TracBrowser for help on using the browser.