1999-11-20 15:41:48 +00:00
|
|
|
// fg_socket.cxx -- Socket I/O routines
|
|
|
|
//
|
|
|
|
// Written by Curtis Olson, started November 1999.
|
|
|
|
//
|
|
|
|
// Copyright (C) 1999 Curtis L. Olson - curt@flightgear.org
|
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU General Public License as
|
|
|
|
// published by the Free Software Foundation; either version 2 of the
|
|
|
|
// License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful, but
|
|
|
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
//
|
|
|
|
// $Id$
|
|
|
|
|
|
|
|
|
2000-02-15 03:30:01 +00:00
|
|
|
#include <simgear/compiler.h>
|
1999-11-20 15:41:48 +00:00
|
|
|
|
|
|
|
#include <sys/time.h> // select()
|
|
|
|
#include <sys/types.h> // socket(), bind(), select(), accept()
|
|
|
|
#include <sys/socket.h> // socket(), bind(), listen(), accept()
|
|
|
|
#include <netinet/in.h> // struct sockaddr_in
|
|
|
|
#include <netdb.h> // gethostbyname()
|
1999-11-23 03:19:09 +00:00
|
|
|
#include <unistd.h> // select(), fsync()/fdatasync()
|
1999-11-20 15:41:48 +00:00
|
|
|
|
2000-02-04 22:50:04 +00:00
|
|
|
#if defined( sgi )
|
|
|
|
#include <strings.h>
|
|
|
|
#endif
|
|
|
|
|
1999-11-20 15:41:48 +00:00
|
|
|
#include STL_STRING
|
|
|
|
|
2000-02-16 23:01:03 +00:00
|
|
|
#include <simgear/debug/logstream.hxx>
|
1999-11-20 15:41:48 +00:00
|
|
|
|
|
|
|
#include "fg_socket.hxx"
|
|
|
|
|
|
|
|
FG_USING_STD(string);
|
|
|
|
|
|
|
|
|
|
|
|
FGSocket::FGSocket() :
|
|
|
|
save_len(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
FGSocket::~FGSocket() {
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int FGSocket::make_server_socket () {
|
|
|
|
struct sockaddr_in name;
|
1999-11-24 14:14:45 +00:00
|
|
|
|
2000-02-04 22:50:04 +00:00
|
|
|
#if defined( __CYGWIN__ ) || defined( __CYGWIN32__ ) || defined( sgi )
|
1999-11-24 14:14:45 +00:00
|
|
|
int length;
|
|
|
|
#else
|
1999-11-20 15:41:48 +00:00
|
|
|
socklen_t length;
|
1999-11-24 14:14:45 +00:00
|
|
|
#endif
|
1999-11-20 15:41:48 +00:00
|
|
|
|
|
|
|
// Create the socket.
|
|
|
|
sock = socket (PF_INET, SOCK_STREAM, 0);
|
|
|
|
if (sock < 0) {
|
|
|
|
FG_LOG( FG_IO, FG_ALERT,
|
|
|
|
"Error: socket() failed in make_server_socket()" );
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Give the socket a name.
|
|
|
|
name.sin_family = AF_INET;
|
|
|
|
name.sin_addr.s_addr = INADDR_ANY;
|
|
|
|
name.sin_port = htons(port); // set port to zero to let system pick
|
|
|
|
name.sin_addr.s_addr = htonl (INADDR_ANY);
|
|
|
|
if (bind (sock, (struct sockaddr *) &name, sizeof (name)) < 0) {
|
|
|
|
FG_LOG( FG_IO, FG_ALERT,
|
|
|
|
"Error: bind() failed in make_server_socket()" );
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find the assigned port number
|
|
|
|
length = sizeof(struct sockaddr_in);
|
|
|
|
if ( getsockname(sock, (struct sockaddr *) &name, &length) ) {
|
|
|
|
FG_LOG( FG_IO, FG_ALERT,
|
|
|
|
"Error: getsockname() failed in make_server_socket()" );
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
port = ntohs(name.sin_port);
|
|
|
|
|
|
|
|
return sock;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int FGSocket::make_client_socket () {
|
|
|
|
struct sockaddr_in name;
|
|
|
|
struct hostent *hp;
|
|
|
|
|
|
|
|
// Create the socket.
|
|
|
|
sock = socket (PF_INET, SOCK_STREAM, 0);
|
|
|
|
if (sock < 0) {
|
|
|
|
FG_LOG( FG_IO, FG_ALERT,
|
|
|
|
"Error: socket() failed in make_client_socket()" );
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// specify address family
|
|
|
|
name.sin_family = AF_INET;
|
|
|
|
|
|
|
|
// get the hosts official name/info
|
|
|
|
hp = gethostbyname( hostname.c_str() );
|
|
|
|
|
|
|
|
// Connect this socket to the host and the port specified on the
|
|
|
|
// command line
|
1999-11-24 14:14:45 +00:00
|
|
|
#if defined( __CYGWIN__ ) || defined( __CYGWIN32__ )
|
|
|
|
bcopy(hp->h_addr, (char *)(&(name.sin_addr.s_addr)), hp->h_length);
|
|
|
|
#else
|
1999-11-20 15:41:48 +00:00
|
|
|
bcopy(hp->h_addr, &(name.sin_addr.s_addr), hp->h_length);
|
1999-11-24 14:14:45 +00:00
|
|
|
#endif
|
1999-11-20 15:41:48 +00:00
|
|
|
name.sin_port = htons(port);
|
|
|
|
|
|
|
|
if ( connect(sock, (struct sockaddr *) &name,
|
|
|
|
sizeof(struct sockaddr_in)) < 0 )
|
|
|
|
{
|
|
|
|
std::close(sock);
|
|
|
|
FG_LOG( FG_IO, FG_ALERT,
|
|
|
|
"Error: connect() failed in make_client_socket()" );
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return sock;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// If specified as a server (out direction for now) open the master
|
|
|
|
// listening socket. If specified as a client, open a connection to a
|
|
|
|
// server.
|
|
|
|
|
|
|
|
bool FGSocket::open( FGProtocol::fgProtocolDir dir ) {
|
|
|
|
if ( port_str == "" || port_str == "any" ) {
|
|
|
|
port = 0;
|
|
|
|
} else {
|
|
|
|
port = atoi( port_str.c_str() );
|
|
|
|
}
|
|
|
|
|
|
|
|
client_connections.clear();
|
|
|
|
|
|
|
|
if ( dir == FGProtocol::out ) {
|
|
|
|
// this means server for now
|
|
|
|
|
|
|
|
// Setup socket to listen on. Set "port" before making this
|
|
|
|
// call. A port of "0" indicates that we want to let the os
|
|
|
|
// pick any available port.
|
|
|
|
sock = make_server_socket();
|
|
|
|
cout << "socket is connected to port = " << port << endl;
|
|
|
|
|
|
|
|
// Specify the maximum length of the connection queue
|
|
|
|
listen(sock, FG_MAX_SOCKET_QUEUE);
|
|
|
|
|
|
|
|
} else if ( dir == FGProtocol::in ) {
|
|
|
|
// this means client for now
|
|
|
|
|
|
|
|
sock = make_client_socket();
|
|
|
|
} else {
|
|
|
|
FG_LOG( FG_IO, FG_ALERT,
|
|
|
|
"Error: bidirection mode not available yet for sockets." );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( sock < 0 ) {
|
|
|
|
FG_LOG( FG_IO, FG_ALERT, "Error opening socket: " << hostname
|
|
|
|
<< ":" << port );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// read data from socket (client)
|
|
|
|
// read a block of data of specified size
|
|
|
|
int FGSocket::read( char *buf, int length ) {
|
|
|
|
int result = 0;
|
|
|
|
|
|
|
|
// check for potential input
|
|
|
|
fd_set ready;
|
|
|
|
FD_ZERO(&ready);
|
|
|
|
FD_SET(sock, &ready);
|
|
|
|
struct timeval tv;
|
|
|
|
tv.tv_sec = 0;
|
|
|
|
tv.tv_usec = 0;
|
|
|
|
|
|
|
|
// test for any input read on sock (returning immediately, even if
|
|
|
|
// nothing)
|
|
|
|
select(32, &ready, 0, 0, &tv);
|
|
|
|
|
|
|
|
if ( FD_ISSET(sock, &ready) ) {
|
|
|
|
result = std::read( sock, buf, length );
|
|
|
|
if ( result != length ) {
|
|
|
|
FG_LOG( FG_IO, FG_INFO,
|
|
|
|
"Warning: read() not enough bytes." );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// read a line of data, length is max size of input buffer
|
|
|
|
int FGSocket::readline( char *buf, int length ) {
|
|
|
|
int result = 0;
|
|
|
|
|
|
|
|
// check for potential input
|
|
|
|
fd_set ready;
|
|
|
|
FD_ZERO(&ready);
|
|
|
|
FD_SET(sock, &ready);
|
|
|
|
struct timeval tv;
|
|
|
|
tv.tv_sec = 0;
|
|
|
|
tv.tv_usec = 0;
|
|
|
|
|
|
|
|
// test for any input read on sock (returning immediately, even if
|
|
|
|
// nothing)
|
|
|
|
select(32, &ready, 0, 0, &tv);
|
|
|
|
|
|
|
|
if ( FD_ISSET(sock, &ready) ) {
|
|
|
|
// read a chunk, keep in the save buffer until we have the
|
|
|
|
// requested amount read
|
|
|
|
|
|
|
|
char *buf_ptr = save_buf + save_len;
|
|
|
|
result = std::read( sock, buf_ptr, FG_MAX_MSG_SIZE - save_len );
|
|
|
|
save_len += result;
|
1999-11-23 03:19:09 +00:00
|
|
|
cout << "current read = " << buf_ptr << endl;
|
|
|
|
cout << "current save_buf = " << save_buf << endl;
|
|
|
|
cout << "save_len = " << save_len << endl;
|
|
|
|
}
|
1999-11-20 15:41:48 +00:00
|
|
|
|
1999-11-23 03:19:09 +00:00
|
|
|
// look for the end of line in save_buf
|
|
|
|
int i;
|
|
|
|
for ( i = 0; i < save_len && save_buf[i] != '\n'; ++i );
|
|
|
|
if ( save_buf[i] == '\n' ) {
|
|
|
|
result = i + 1;
|
|
|
|
} else {
|
|
|
|
// no end of line yet
|
|
|
|
cout << "no eol found" << endl;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
cout << "line length = " << result << endl;
|
1999-11-20 15:41:48 +00:00
|
|
|
|
1999-11-23 03:19:09 +00:00
|
|
|
// we found an end of line
|
1999-11-20 15:41:48 +00:00
|
|
|
|
1999-11-23 03:19:09 +00:00
|
|
|
// copy to external buffer
|
|
|
|
strncpy( buf, save_buf, result );
|
|
|
|
buf[result] = '\0';
|
|
|
|
cout << "fg_socket line = " << buf << endl;
|
|
|
|
|
|
|
|
// shift save buffer
|
|
|
|
for ( i = result; i < save_len; ++i ) {
|
|
|
|
save_buf[ i - result ] = save_buf[i];
|
1999-11-20 15:41:48 +00:00
|
|
|
}
|
1999-11-23 03:19:09 +00:00
|
|
|
save_len -= result;
|
1999-11-20 15:41:48 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// write data to socket (server)
|
|
|
|
int FGSocket::write( char *buf, int length ) {
|
|
|
|
|
|
|
|
// check for any new client connection requests
|
|
|
|
fd_set ready;
|
|
|
|
FD_ZERO(&ready);
|
|
|
|
FD_SET(sock, &ready);
|
|
|
|
struct timeval tv;
|
|
|
|
tv.tv_sec = 0;
|
|
|
|
tv.tv_usec = 0;
|
|
|
|
|
|
|
|
// test for any input on sock (returning immediately, even if
|
|
|
|
// nothing)
|
|
|
|
select(32, &ready, 0, 0, &tv);
|
|
|
|
|
|
|
|
// any new connections?
|
|
|
|
if ( FD_ISSET(sock, &ready) ) {
|
|
|
|
int msgsock = accept(sock, 0, 0);
|
|
|
|
if ( msgsock < 0 ) {
|
|
|
|
FG_LOG( FG_IO, FG_ALERT,
|
|
|
|
"Error: accept() failed in write()" );
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
client_connections.push_back( msgsock );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool error_condition = false;
|
1999-11-23 03:19:09 +00:00
|
|
|
FG_LOG( FG_IO, FG_INFO, "Client connections = " <<
|
|
|
|
client_connections.size() );
|
1999-11-20 15:41:48 +00:00
|
|
|
for ( int i = 0; i < (int)client_connections.size(); ++i ) {
|
|
|
|
int msgsock = client_connections[i];
|
1999-11-23 03:19:09 +00:00
|
|
|
|
|
|
|
// read and junk any possible incoming messages.
|
|
|
|
// char junk[ FG_MAX_MSG_SIZE ];
|
|
|
|
// std::read( msgsock, junk, FG_MAX_MSG_SIZE );
|
|
|
|
|
|
|
|
// write the interesting data to the socket
|
1999-11-20 15:41:48 +00:00
|
|
|
if ( std::write(msgsock, buf, length) < 0 ) {
|
|
|
|
FG_LOG( FG_IO, FG_ALERT, "Error writing to socket: " << port );
|
|
|
|
error_condition = true;
|
1999-11-23 03:19:09 +00:00
|
|
|
} else {
|
|
|
|
#ifdef _POSIX_SYNCHRONIZED_IO
|
|
|
|
// fdatasync(msgsock);
|
|
|
|
#else
|
|
|
|
// fsync(msgsock);
|
|
|
|
#endif
|
1999-11-20 15:41:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( error_condition ) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-24 14:14:45 +00:00
|
|
|
// write null terminated string to socket (server)
|
|
|
|
int FGSocket::writestring( char *str ) {
|
|
|
|
int length = strlen( str );
|
|
|
|
return write( str, length );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-20 15:41:48 +00:00
|
|
|
// close the port
|
|
|
|
bool FGSocket::close() {
|
|
|
|
for ( int i = 0; i < (int)client_connections.size(); ++i ) {
|
|
|
|
int msgsock = client_connections[i];
|
|
|
|
std::close( msgsock );
|
|
|
|
}
|
|
|
|
|
|
|
|
std::close( sock );
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|