OPeNDAP Hyrax Back End Server (BES)  Updated for version 3.8.3
UnixSocket.cc
Go to the documentation of this file.
1 // UnixSocket.cc
2 
3 // This file is part of bes, A C++ back-end server implementation framework
4 // for the OPeNDAP Data Access Protocol.
5 
6 // Copyright (c) 2004-2009 University Corporation for Atmospheric Research
7 // Author: Patrick West <pwest@ucar.edu> and Jose Garcia <jgarcia@ucar.edu>
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
18 //
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 //
23 // You can contact University Corporation for Atmospheric Research at
24 // 3080 Center Green Drive, Boulder, CO 80301
25 
26 // (c) COPYRIGHT University Corporation for Atmospheric Research 2004-2005
27 // Please read the full copyright statement in the file COPYRIGHT_UCAR.
28 //
29 // Authors:
30 // pwest Patrick West <pwest@ucar.edu>
31 // jgarcia Jose Garcia <jgarcia@ucar.edu>
32 // szednik Stephan Zednik <zednik@ucar.edu>
33 
34 #include <unistd.h> // for unlink
35 #include <sys/un.h>
36 #include <sys/socket.h>
37 #include <sys/types.h>
38 
39 #include <cstdio>
40 #include <cerrno>
41 #include <cstring>
42 
43 #include "UnixSocket.h"
44 #include "BESInternalError.h"
45 #include "SocketUtilities.h"
46 
47 void
49 {
50  if( _listening )
51  {
52  string err( "Socket is already listening" ) ;
53  throw BESInternalError( err, __FILE__, __LINE__ ) ;
54  }
55 
56  if( _connected )
57  {
58  string err( "Socket is already connected" ) ;
59  throw BESInternalError( err, __FILE__, __LINE__ ) ;
60  }
61 
62  struct sockaddr_un client_addr ;
63  struct sockaddr_un server_addr ;
64 
65  // what is the max size of the path to the unix socket
66  unsigned int max_len = sizeof( client_addr.sun_path ) ;
67 
68  char path[107] = "" ;
69  getcwd( path, sizeof( path ) ) ;
70  _tempSocket = path ;
71  _tempSocket += "/" ;
72  _tempSocket += SocketUtilities::create_temp_name() ;
73  _tempSocket += ".unixSocket" ;
74  // maximum path for struct sockaddr_un.sun_path is 108
75  // get sure we will not exceed to max for creating sockets
76  // 107 characters in pathname + '\0'
77  if( _tempSocket.length() > max_len - 1 )
78  {
79  string msg = "path to temporary unix socket " ;
80  msg += _tempSocket + " is too long" ;
81  throw( BESInternalError( msg, __FILE__, __LINE__ ) ) ;
82  }
83  if( _unixSocket.length() > max_len - 1 )
84  {
85  string msg = "path to unix socket " ;
86  msg += _unixSocket + " is too long" ;
87  throw( BESInternalError( msg, __FILE__, __LINE__ ) ) ;
88  }
89 
90  strncpy(server_addr.sun_path, _unixSocket.c_str(), _unixSocket.size());
91  server_addr.sun_path[_unixSocket.size()] = '\0';
92  server_addr.sun_family = AF_UNIX ;
93 
94  int descript = socket( AF_UNIX, SOCK_STREAM, 0 ) ;
95  if( descript != -1 )
96  {
97  strncpy( client_addr.sun_path, _tempSocket.c_str(), _tempSocket.size());
98  client_addr.sun_path[_tempSocket.size()] = '\0';
99  client_addr.sun_family = AF_UNIX ;
100 
101  int clen = sizeof( client_addr.sun_family ) ;
102  clen += strlen( client_addr.sun_path ) + 1;
103 
104  if( bind( descript, (struct sockaddr*)&client_addr, clen + 1) != -1 )
105  {
106  int slen = sizeof( server_addr.sun_family ) ;
107  slen += strlen( server_addr.sun_path) + 1;
108 
109  // we aren't setting the send and receive buffer sizes for a
110  // unix socket. These will default to a set value
111 
112  if( ::connect( descript, (struct sockaddr*)&server_addr, slen ) != -1)
113  {
114  _socket = descript ;
115  _connected = true ;
116  }
117  else
118  {
119  ::close( descript ) ;
120  string msg = "could not connect via " ;
121  msg += _unixSocket ;
122  char *err = strerror( errno ) ;
123  if( err )
124  msg = msg + "\n" + err ;
125  else
126  msg = msg + "\nCould not retrieve error message" ;
127  throw BESInternalError( msg, __FILE__, __LINE__ ) ;
128  }
129  }
130  else
131  {
132  string msg = "could not bind to Unix socket " ;
133  msg += _tempSocket ;
134  char *err = strerror( errno ) ;
135  if( err )
136  msg = msg + "\n" + err ;
137  else
138  msg = msg + "\nCould not retrieve error message" ;
139  throw BESInternalError( msg, __FILE__, __LINE__ ) ;
140  }
141  }
142  else
143  {
144  string msg = "could not create a Unix socket" ;
145  char *err = strerror( errno ) ;
146  if( err )
147  msg = msg + "\n" + err ;
148  else
149  msg = msg + "\nCould not retrieve error message" ;
150  throw BESInternalError( msg, __FILE__, __LINE__ ) ;
151  }
152 }
153 
154 void
156 {
157  if( _connected )
158  {
159  string err( "Socket is already connected" ) ;
160  throw BESInternalError( err, __FILE__, __LINE__ ) ;
161  }
162 
163  if( _listening )
164  {
165  string err( "Socket is already listening" ) ;
166  throw BESInternalError( err, __FILE__, __LINE__ ) ;
167  }
168 
169  int on = 1 ;
170  static struct sockaddr_un server_add ;
171  _socket = socket( AF_UNIX,SOCK_STREAM, 0 ) ;
172  if( _socket >= 0 )
173  {
174  server_add.sun_family = AF_UNIX;
175  // Changed the call below to strncpy; sockaddr_un.sun_path is a char[104]
176  // on OS/X. jhrg 5/26/06
177  strncpy( server_add.sun_path, _unixSocket.c_str(), 103) ;
178  server_add.sun_path[103] = '\0';
179 
180  (void)unlink( _unixSocket.c_str() ) ;
181  if( setsockopt( _socket, SOL_SOCKET, SO_REUSEADDR,
182  (char*)&on, sizeof( on ) ) )
183  {
184  string error( "could not set SO_REUSEADDR on Unix socket" ) ;
185  const char *error_info = strerror( errno ) ;
186  if( error_info )
187  error += " " + (string)error_info ;
188  throw BESInternalError( error, __FILE__, __LINE__ ) ;
189  }
190 
191  // we aren't setting the send and receive buffer sizes for a unix
192  // socket. These will default to a set value
193 
194  // Added a +1 to the size computation. jhrg 5/26/05
195  if( bind( _socket, (struct sockaddr*)&server_add, sizeof( server_add.sun_family ) + strlen( server_add.sun_path ) + 1) != -1)
196  {
197  if( ::listen( _socket, 5 ) == 0 )
198  {
199  _listening = true ;
200  }
201  else
202  {
203  string error( "could not listen Unix socket" ) ;
204  const char* error_info = strerror( errno ) ;
205  if( error_info )
206  error += " " + (string)error_info ;
207  throw BESInternalError( error, __FILE__, __LINE__ ) ;
208  }
209  }
210  else
211  {
212  string error( "could not bind Unix socket" ) ;
213  const char* error_info = strerror( errno ) ;
214  if( error_info )
215  error += " " + (string)error_info ;
216  throw BESInternalError( error, __FILE__, __LINE__ ) ;
217  }
218  }
219  else
220  {
221  string error( "could not get Unix socket" ) ;
222  const char *error_info = strerror( errno ) ;
223  if( error_info )
224  error += " " + (string)error_info ;
225  throw BESInternalError( error, __FILE__, __LINE__ ) ;
226  }
227 }
228 
229 void
231 {
232  Socket::close() ;
233  if( _tempSocket != "" )
234  {
235  if( !access( _tempSocket.c_str(), F_OK ) )
236  {
237  (void)remove( _tempSocket.c_str() ) ;
238  }
239  _connected = false ;
240  }
241  if( _listening && _unixSocket != "" )
242  {
243  if( !access( _unixSocket.c_str(), F_OK ) )
244  {
245  (void)remove( _unixSocket.c_str() ) ;
246  }
247  _listening = false ;
248  }
249 }
250 
254 bool
256 {
257  return true ;
258 }
259 
266 void
267 UnixSocket::dump( ostream &strm ) const
268 {
269  strm << BESIndent::LMarg << "UnixSocket::dump - ("
270  << (void *)this << ")" << endl ;
272  strm << BESIndent::LMarg << "unix socket: " << _unixSocket << endl ;
273  strm << BESIndent::LMarg << "temp socket: " << _tempSocket << endl ;
274  Socket::dump( strm ) ;
276 }
277 
virtual void connect()
Definition: UnixSocket.cc:48
exception thrown if inernal error encountered
int _socket
Definition: Socket.h:47
static void Indent()
Definition: BESIndent.cc:38
bool _listening
Definition: Socket.h:49
static ostream & LMarg(ostream &strm)
Definition: BESIndent.cc:73
virtual bool allowConnection()
is there any wrapper code for unix sockets
Definition: UnixSocket.cc:255
virtual void close()
Definition: Socket.cc:81
virtual void dump(ostream &strm) const
dumps information about this object
Definition: Socket.cc:136
virtual void listen()
Definition: UnixSocket.cc:155
static void UnIndent()
Definition: BESIndent.cc:44
bool _connected
Definition: Socket.h:48
virtual void close()
Definition: UnixSocket.cc:230
static string create_temp_name()
Create a uniq name which is used to create a unique name for a Unix socket in a client ...
virtual void dump(ostream &strm) const
dumps information about this object
Definition: UnixSocket.cc:267