OPeNDAP Hyrax Back End Server (BES)  Updated for version 3.8.3
Socket.cc
Go to the documentation of this file.
1 // Socket.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 
33 #include "config.h"
34 
35 #include <cstdio>
36 #include <cerrno>
37 #include <cstring>
38 
39 #ifdef HAVE_UNISTD_H
40 #include <unistd.h>
41 #endif
42 
43 #include <sys/types.h>
44 #include <sys/socket.h>
45 #include <arpa/inet.h>
46 #ifdef HAVE_UNISTD_H
47 #include <unistd.h>
48 #endif
49 
50 #include "Socket.h"
51 #include "BESInternalError.h"
52 
53 Socket::Socket( int socket, struct sockaddr *addr )
54  : _socket( socket ),
55  _connected( true ),
56  _listening( false ),
57  _addr_set( true )
58 {
59  char ip[46];
60  unsigned int port;
61  /* ... */
62  switch (addr->sa_family) {
63  case AF_INET:
64  inet_ntop (AF_INET, &(((struct sockaddr_in *)addr)->sin_addr), ip, sizeof (ip));
65  port = ntohs (((struct sockaddr_in *)addr)->sin_port);
66  break;
67  case AF_INET6:
68  inet_ntop (AF_INET6, &(((struct sockaddr_in6 *)addr)->sin6_addr), ip, sizeof (ip));
69  port = ntohs (((struct sockaddr_in6 *)addr)->sin6_port);
70  break;
71  default:
72  snprintf (ip, sizeof (ip), "UNKNOWN FAMILY: %d", addr->sa_family);
73  port = 0;
74  break;
75  }
76  _port = port ;
77  _ip = ip ;
78 }
79 
80 void
82 {
83  if( _connected )
84  {
85  ::close( _socket ) ;
86  _socket = 0 ;
87  _connected = false ;
88  _listening = false ;
89  }
90 }
91 
92 void
93 Socket::send( const string &str, int start, int end )
94 {
95  string send_str = str.substr( start, end ) ;
96  int bytes_written = write( _socket, send_str.c_str(), send_str.length() ) ;
97  if( bytes_written == -1 )
98  {
99  string err( "socket failure, writing on stream socket" ) ;
100  const char* error_info = strerror( errno ) ;
101  if( error_info )
102  err += " " + (string)error_info ;
103  throw BESInternalError( err, __FILE__, __LINE__ ) ;
104  }
105 }
106 
107 int
108 Socket::receive( char *inBuff, const int inSize )
109 {
110  int bytesRead = 0 ;
111  if( ( bytesRead = read( _socket, inBuff, inSize ) ) < 1 )
112  {
113  string err( "socket failure, reading on stream socket: " ) ;
114  const char *error_info = strerror( errno ) ;
115  if( error_info )
116  err += " " + (string)error_info ;
117  throw BESInternalError( err, __FILE__, __LINE__ ) ;
118  }
119  //inBuff[bytesRead] = '\0' ;
120  return bytesRead ;
121 }
122 
123 #if 0
124 // removed jhrg 5/5/11
125 void
126 Socket::sync()
127 {
128 #if 0
129  // fsync does not work for sockets.
130  fsync( _socket ) ;
131 #endif
132 }
133 #endif
134 
141 void
142 Socket::dump( ostream &strm ) const
143 {
144  strm << BESIndent::LMarg << "Socket::dump - ("
145  << (void *)this << ")" << endl ;
147  strm << BESIndent::LMarg << "socket: " << _socket << endl ;
148  strm << BESIndent::LMarg << "is connected? " << _connected << endl ;
149  strm << BESIndent::LMarg << "is listening? " << _listening << endl ;
150  strm << BESIndent::LMarg << "socket address set? " << _addr_set << endl ;
151  if( _addr_set )
152  {
153  strm << BESIndent::LMarg << "socket port: " << _port << endl;
154  strm << BESIndent::LMarg << "socket ip: " << _ip << endl;
155  }
157 }
158