1 // fg_socket.cxx -- Socket I/O routines
3 // Written by Curtis Olson, started November 1999.
5 // Copyright (C) 1999 Curtis L. Olson - curt@flightgear.org
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <Include/compiler.h>
26 #include <sys/time.h> // select()
27 #include <sys/types.h> // socket(), bind(), select(), accept()
28 #include <sys/socket.h> // socket(), bind(), listen(), accept()
29 #include <netinet/in.h> // struct sockaddr_in
30 #include <netdb.h> // gethostbyname()
31 #include <unistd.h> // select(), fsync()/fdatasync()
35 #include <Debug/logstream.hxx>
37 #include "fg_socket.hxx"
42 FGSocket::FGSocket() :
48 FGSocket::~FGSocket() {
52 int FGSocket::make_server_socket () {
53 struct sockaddr_in name;
55 #if defined( __CYGWIN__ ) || defined( __CYGWIN32__ )
62 sock = socket (PF_INET, SOCK_STREAM, 0);
64 FG_LOG( FG_IO, FG_ALERT,
65 "Error: socket() failed in make_server_socket()" );
69 // Give the socket a name.
70 name.sin_family = AF_INET;
71 name.sin_addr.s_addr = INADDR_ANY;
72 name.sin_port = htons(port); // set port to zero to let system pick
73 name.sin_addr.s_addr = htonl (INADDR_ANY);
74 if (bind (sock, (struct sockaddr *) &name, sizeof (name)) < 0) {
75 FG_LOG( FG_IO, FG_ALERT,
76 "Error: bind() failed in make_server_socket()" );
80 // Find the assigned port number
81 length = sizeof(struct sockaddr_in);
82 if ( getsockname(sock, (struct sockaddr *) &name, &length) ) {
83 FG_LOG( FG_IO, FG_ALERT,
84 "Error: getsockname() failed in make_server_socket()" );
87 port = ntohs(name.sin_port);
93 int FGSocket::make_client_socket () {
94 struct sockaddr_in name;
98 sock = socket (PF_INET, SOCK_STREAM, 0);
100 FG_LOG( FG_IO, FG_ALERT,
101 "Error: socket() failed in make_client_socket()" );
105 // specify address family
106 name.sin_family = AF_INET;
108 // get the hosts official name/info
109 hp = gethostbyname( hostname.c_str() );
111 // Connect this socket to the host and the port specified on the
113 #if defined( __CYGWIN__ ) || defined( __CYGWIN32__ )
114 bcopy(hp->h_addr, (char *)(&(name.sin_addr.s_addr)), hp->h_length);
116 bcopy(hp->h_addr, &(name.sin_addr.s_addr), hp->h_length);
118 name.sin_port = htons(port);
120 if ( connect(sock, (struct sockaddr *) &name,
121 sizeof(struct sockaddr_in)) < 0 )
124 FG_LOG( FG_IO, FG_ALERT,
125 "Error: connect() failed in make_client_socket()" );
133 // If specified as a server (out direction for now) open the master
134 // listening socket. If specified as a client, open a connection to a
137 bool FGSocket::open( FGProtocol::fgProtocolDir dir ) {
138 if ( port_str == "" || port_str == "any" ) {
141 port = atoi( port_str.c_str() );
144 client_connections.clear();
146 if ( dir == FGProtocol::out ) {
147 // this means server for now
149 // Setup socket to listen on. Set "port" before making this
150 // call. A port of "0" indicates that we want to let the os
151 // pick any available port.
152 sock = make_server_socket();
153 cout << "socket is connected to port = " << port << endl;
155 // Specify the maximum length of the connection queue
156 listen(sock, FG_MAX_SOCKET_QUEUE);
158 } else if ( dir == FGProtocol::in ) {
159 // this means client for now
161 sock = make_client_socket();
163 FG_LOG( FG_IO, FG_ALERT,
164 "Error: bidirection mode not available yet for sockets." );
169 FG_LOG( FG_IO, FG_ALERT, "Error opening socket: " << hostname
178 // read data from socket (client)
179 // read a block of data of specified size
180 int FGSocket::read( char *buf, int length ) {
183 // check for potential input
186 FD_SET(sock, &ready);
191 // test for any input read on sock (returning immediately, even if
193 select(32, &ready, 0, 0, &tv);
195 if ( FD_ISSET(sock, &ready) ) {
196 result = std::read( sock, buf, length );
197 if ( result != length ) {
198 FG_LOG( FG_IO, FG_INFO,
199 "Warning: read() not enough bytes." );
207 // read a line of data, length is max size of input buffer
208 int FGSocket::readline( char *buf, int length ) {
211 // check for potential input
214 FD_SET(sock, &ready);
219 // test for any input read on sock (returning immediately, even if
221 select(32, &ready, 0, 0, &tv);
223 if ( FD_ISSET(sock, &ready) ) {
224 // read a chunk, keep in the save buffer until we have the
225 // requested amount read
227 char *buf_ptr = save_buf + save_len;
228 result = std::read( sock, buf_ptr, FG_MAX_MSG_SIZE - save_len );
230 cout << "current read = " << buf_ptr << endl;
231 cout << "current save_buf = " << save_buf << endl;
232 cout << "save_len = " << save_len << endl;
235 // look for the end of line in save_buf
237 for ( i = 0; i < save_len && save_buf[i] != '\n'; ++i );
238 if ( save_buf[i] == '\n' ) {
241 // no end of line yet
242 cout << "no eol found" << endl;
245 cout << "line length = " << result << endl;
247 // we found an end of line
249 // copy to external buffer
250 strncpy( buf, save_buf, result );
252 cout << "fg_socket line = " << buf << endl;
255 for ( i = result; i < save_len; ++i ) {
256 save_buf[ i - result ] = save_buf[i];
264 // write data to socket (server)
265 int FGSocket::write( char *buf, int length ) {
267 // check for any new client connection requests
270 FD_SET(sock, &ready);
275 // test for any input on sock (returning immediately, even if
277 select(32, &ready, 0, 0, &tv);
279 // any new connections?
280 if ( FD_ISSET(sock, &ready) ) {
281 int msgsock = accept(sock, 0, 0);
283 FG_LOG( FG_IO, FG_ALERT,
284 "Error: accept() failed in write()" );
287 client_connections.push_back( msgsock );
291 bool error_condition = false;
292 FG_LOG( FG_IO, FG_INFO, "Client connections = " <<
293 client_connections.size() );
294 for ( int i = 0; i < (int)client_connections.size(); ++i ) {
295 int msgsock = client_connections[i];
297 // read and junk any possible incoming messages.
298 // char junk[ FG_MAX_MSG_SIZE ];
299 // std::read( msgsock, junk, FG_MAX_MSG_SIZE );
301 // write the interesting data to the socket
302 if ( std::write(msgsock, buf, length) < 0 ) {
303 FG_LOG( FG_IO, FG_ALERT, "Error writing to socket: " << port );
304 error_condition = true;
306 #ifdef _POSIX_SYNCHRONIZED_IO
307 // fdatasync(msgsock);
314 if ( error_condition ) {
322 // write null terminated string to socket (server)
323 int FGSocket::writestring( char *str ) {
324 int length = strlen( str );
325 return write( str, length );
330 bool FGSocket::close() {
331 for ( int i = 0; i < (int)client_connections.size(); ++i ) {
332 int msgsock = client_connections[i];
333 std::close( msgsock );