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  ::close( descript ) ;
133  string msg = "could not bind to Unix socket " ;
134  msg += _tempSocket ;
135  char *err = strerror( errno ) ;
136  if( err )
137  msg = msg + "\n" + err ;
138  else
139  msg = msg + "\nCould not retrieve error message" ;
140  throw BESInternalError( msg, __FILE__, __LINE__ ) ;
141  }
142  }
143  else
144  {
145  string msg = "could not create a Unix socket" ;
146  char *err = strerror( errno ) ;
147  if( err )
148  msg = msg + "\n" + err ;
149  else
150  msg = msg + "\nCould not retrieve error message" ;
151  throw BESInternalError( msg, __FILE__, __LINE__ ) ;
152  }
153 }
154 
155 void
157 {
158  if( _connected )
159  {
160  string err( "Socket is already connected" ) ;
161  throw BESInternalError( err, __FILE__, __LINE__ ) ;
162  }
163 
164  if( _listening )
165  {
166  string err( "Socket is already listening" ) ;
167  throw BESInternalError( err, __FILE__, __LINE__ ) ;
168  }
169 
170  int on = 1 ;
171  static struct sockaddr_un server_add ;
172  _socket = socket( AF_UNIX,SOCK_STREAM, 0 ) ;
173  if( _socket >= 0 )
174  {
175  server_add.sun_family = AF_UNIX;
176  // Changed the call below to strncpy; sockaddr_un.sun_path is a char[104]
177  // on OS/X. jhrg 5/26/06
178  strncpy( server_add.sun_path, _unixSocket.c_str(), 103) ;
179  server_add.sun_path[103] = '\0';
180 
181  (void)unlink( _unixSocket.c_str() ) ;
182  if( setsockopt( _socket, SOL_SOCKET, SO_REUSEADDR,
183  (char*)&on, sizeof( on ) ) )
184  {
185  string error( "could not set SO_REUSEADDR on Unix socket" ) ;
186  const char *error_info = strerror( errno ) ;
187  if( error_info )
188  error += " " + (string)error_info ;
189  throw BESInternalError( error, __FILE__, __LINE__ ) ;
190  }
191 
192  // we aren't setting the send and receive buffer sizes for a unix
193  // socket. These will default to a set value
194 
195  // Added a +1 to the size computation. jhrg 5/26/05
196  if( bind( _socket, (struct sockaddr*)&server_add, sizeof( server_add.sun_family ) + strlen( server_add.sun_path ) + 1) != -1)
197  {
198  if( ::listen( _socket, 5 ) == 0 )
199  {
200  _listening = true ;
201  }
202  else
203  {
204  string error( "could not listen Unix socket" ) ;
205  const char* error_info = strerror( errno ) ;
206  if( error_info )
207  error += " " + (string)error_info ;
208  throw BESInternalError( error, __FILE__, __LINE__ ) ;
209  }
210  }
211  else
212  {
213  string error( "could not bind Unix socket" ) ;
214  const char* error_info = strerror( errno ) ;
215  if( error_info )
216  error += " " + (string)error_info ;
217  throw BESInternalError( error, __FILE__, __LINE__ ) ;
218  }
219  }
220  else
221  {
222  string error( "could not get Unix socket" ) ;
223  const char *error_info = strerror( errno ) ;
224  if( error_info )
225  error += " " + (string)error_info ;
226  throw BESInternalError( error, __FILE__, __LINE__ ) ;
227  }
228 }
229 
230 void
232 {
233  Socket::close() ;
234  if( _tempSocket != "" )
235  {
236  if( !access( _tempSocket.c_str(), F_OK ) )
237  {
238  (void)remove( _tempSocket.c_str() ) ;
239  }
240  _connected = false ;
241  }
242  if( _listening && _unixSocket != "" )
243  {
244  if( !access( _unixSocket.c_str(), F_OK ) )
245  {
246  (void)remove( _unixSocket.c_str() ) ;
247  }
248  _listening = false ;
249  }
250 }
251 
255 bool
257 {
258  return true ;
259 }
260 
267 void
268 UnixSocket::dump( ostream &strm ) const
269 {
270  strm << BESIndent::LMarg << "UnixSocket::dump - ("
271  << (void *)this << ")" << endl ;
273  strm << BESIndent::LMarg << "unix socket: " << _unixSocket << endl ;
274  strm << BESIndent::LMarg << "temp socket: " << _tempSocket << endl ;
275  Socket::dump( strm ) ;
277 }
278