trap_retval RemoteGet( void *data, trap_elen len ) { unsigned_16 rec_len; len = len; _DBG_NET(("RemoteGet\r\n")); if( IS_VALID_SOCKET( data_socket ) ) { int size; size = recvData( &rec_len, sizeof( rec_len ) ); #ifdef __RDOS__ while( size == 0 ) { if( !IS_VALID_SOCKET( data_socket ) ) return( REQUEST_FAILED ); size = recvData( &rec_len, sizeof( rec_len ) ); } #endif if( size == sizeof( rec_len ) ) { CONV_LE_16( rec_len ); #ifdef __RDOS__ if( rec_len && recvData( data, rec_len ) == rec_len ) { #else if( rec_len == 0 || recvData( data, rec_len ) == rec_len ) { #endif _DBG_NET(("Got a packet - size=%d\r\n", rec_len)); return( rec_len ); } } } return( REQUEST_FAILED ); } trap_retval RemotePut( void *data, trap_elen len ) { unsigned_16 send_len; int snd; _DBG_NET(("RemotePut\r\n")); if( IS_VALID_SOCKET( data_socket ) ) { send_len = len; CONV_LE_16( send_len ); snd = send( data_socket, (void *)&send_len, sizeof( send_len ), 0 ); if( IS_RET_OK( snd ) ) { if( len != 0 ) snd = send( data_socket, data, len, 0 ); if( len == 0 || IS_RET_OK( snd ) ) { #ifdef __RDOS__ RdosPushTcpConnection( data_socket ); #endif _DBG_NET(("RemotePut...OK\r\n")); return( len ); } } } return( REQUEST_FAILED ); }
bool RemoteConnect( void ) { #ifdef SERVER #ifdef __RDOS__ void *obj; obj = (void *)RdosWaitTimeout( wait_handle, 250 ); if( obj != NULL ) { data_socket = RdosGetTcpListen( listen_handle ); if( IS_VALID_SOCKET( data_socket ) ) { _DBG_NET(("Found a connection\r\n")); return( true ); } } #else struct timeval timeout; fd_set ready; struct sockaddr dummy; trp_socklen dummy_len = sizeof( dummy ); int rc; FD_ZERO( &ready ); FD_SET( control_socket, &ready ); timeout.tv_sec = 0; timeout.tv_usec = 10000; rc = select( control_socket + 1, &ready, 0, 0, &timeout ); if( IS_RET_OK( rc ) && rc > 0 ) { data_socket = accept( control_socket, &dummy, &dummy_len ); if( IS_VALID_SOCKET( data_socket ) ) { nodelay(); _DBG_NET(("Found a connection\r\n")); return( true ); } } #endif #else #ifdef __RDOS__ // todo: Add code for connect! #else int rc; data_socket = socket( AF_INET, SOCK_STREAM, 0 ); if( IS_VALID_SOCKET( data_socket ) ) { rc = connect( data_socket, (LPSOCKADDR)&socket_address, sizeof( socket_address ) ); if( IS_RET_OK( rc ) ) { nodelay(); return( true ); } } #endif #endif return( false ); }
void RemoteDisco( void ) { _DBG_NET(("RemoteDisco\r\n")); if( IS_VALID_SOCKET( data_socket ) ) { soclose( data_socket ); data_socket = INVALID_SOCKET; } }
const char *RemoteLink( const char *parms, bool server ) { unsigned short port; #ifndef __RDOS__ struct servent *sp; #endif #ifdef SERVER #if !defined( __RDOS__ ) trp_socklen length; char buff[128]; #endif _DBG_NET(("SERVER: Calling into RemoteLink\r\n")); #if defined(__NT__) || defined(__WINDOWS__) { WSADATA data; if( WSAStartup( 0x101, &data ) != 0 ) { return( TRP_ERR_unable_to_initialize_TCPIP ); } } #endif port = 0; #ifdef __RDOS__ while( isdigit( *parms ) ) { port = port * 10 + (*parms - '0'); ++parms; } if( port == 0 ) port = DEFAULT_PORT; wait_handle = RdosCreateWait( ); listen_handle = RdosCreateTcpListen( port, 1, SOCKET_BUFFER ); RdosAddWaitForTcpListen( wait_handle, listen_handle, (int)(&listen_handle) ); #else if( *parms == '\0' ) parms = "tcplink"; sp = getservbyname( parms, "tcp" ); if( sp != NULL ) { port = sp->s_port; } else { while( isdigit( *parms ) ) { port = port * 10 + (*parms - '0'); ++parms; } if( port == 0 ) port = DEFAULT_PORT; port = htons( port ); } control_socket = socket(AF_INET, SOCK_STREAM, 0); if( !IS_VALID_SOCKET( control_socket ) ) { return( TRP_ERR_unable_to_open_stream_socket ); } #ifdef GUISERVER if( *ServParms == '\0' ) { sprintf( ServParms, "%u", ntohs( port ) ); } #endif /* Name socket using wildcards */ socket_address.sin_family = AF_INET; socket_address.sin_addr.s_addr = INADDR_ANY; socket_address.sin_port = port; if( bind( control_socket, (LPSOCKADDR)&socket_address, sizeof( socket_address ) ) ) { return( TRP_ERR_unable_to_bind_stream_socket ); } /* Find out assigned port number and print it out */ length = sizeof( socket_address ); if( getsockname( control_socket, (LPSOCKADDR)&socket_address, &length ) ) { return( TRP_ERR_unable_to_get_socket_name ); } sprintf( buff, "%s%d", TRP_TCP_socket_number, ntohs( socket_address.sin_port ) ); ServMessage( buff ); _DBG_NET(("TCP: ")); _DBG_NET((buff)); _DBG_NET(("\r\n")); #ifdef LIST_INTERFACES // TODO: need rework to POSIX if_nameindex in <net/if.h> /* Find and print TCP/IP interface addresses, ignore aliases */ { struct ifi_info *ifi, *ifihead; struct sockaddr *sa; ifihead = get_ifi_info( AF_INET, false ); for( ifi = ifihead; ifi != NULL; ifi = ifi->ifi_next ) { /* Ignore loopback interfaces */ if( ifi->flags & IFI_LOOP ) continue; if( (sa = ifi->ifi_addr) != NULL ) { sprintf( buff, "%s%s", TRP_TCP_ip_address, inet_ntoa( ((struct sockaddr_in *)sa)->sin_addr ) ); ServMessage( buff ); } } free_ifi_info( ifihead ); } #endif #endif _DBG_NET(("Start accepting connections\r\n")); /* Start accepting connections */ #ifndef __RDOS__ listen( control_socket, 5 ); #endif #else #ifdef __RDOS__ // Todo: handle connect #else const char *sock; char buff[128]; char *p; #if defined(__NT__) || defined(__WINDOWS__) { WSADATA data; if( WSAStartup( 0x101, &data ) != 0 ) { return( TRP_ERR_unable_to_initialize_TCPIP ); } } #endif /* get port number out of name */ p = buff; for( sock = parms; *sock != '\0'; ++sock ) { if( *sock == ':' ) { ++sock; break; } *p++ = *sock; } *p = '\0'; if( sock[0] == '\0' ) { sp = getservbyname( "tcplink", "tcp" ); } else { sp = getservbyname( sock, "tcp" ); } if( sp != NULL ) { port = sp->s_port; } else { port = 0; while( isdigit( *sock ) ) { port = port * 10 + (*sock - '0'); ++sock; } if( *sock != '\0' ) { return( TRP_ERR_unable_to_parse_port_number ); } if( port == 0 ) port = DEFAULT_PORT; port = htons( port ); } parms = buff; /* Setup for socket connect using parms specified by command line. */ socket_address.sin_family = AF_INET; /* OS/2's TCP/IP gethostbyname doesn't handle numeric addresses */ socket_address.sin_addr.s_addr = inet_addr( parms ); if( socket_address.sin_addr.s_addr == (unsigned long)-1L ) { struct hostent *hp; hp = gethostbyname( parms ); if( hp != 0 ) { memcpy( &socket_address.sin_addr, hp->h_addr, hp->h_length ); } else { return( TRP_ERR_unknown_host ); } } socket_address.sin_port = port; #endif #endif server = server; return( NULL ); }
trap_retval RemoteGet( byte *rec, trap_elen len ) { unsigned_16 rec_len; len = len; _DBG_NET(("RemoteGet\r\n")); #ifdef __RDOS__ if( !IS_INVALID_SOCKET( data_socket ) && !RdosIsTcpConnectionClosed( data_socket ) ) { #else if( !IS_INVALID_SOCKET( data_socket ) ) { #endif if( FullGet( &rec_len, sizeof( rec_len ) ) == sizeof( rec_len ) ) { CONV_LE_16( rec_len ); if( rec_len == 0 || FullGet( rec, rec_len ) == rec_len ) { _DBG_NET(("Got a packet - size=%d\r\n", rec_len)); return( rec_len ); } } } return( REQUEST_FAILED ); } trap_retval RemotePut( byte *rec, trap_elen len ) { unsigned_16 send_len; _DBG_NET(("RemotePut\r\n")); #ifdef __RDOS__ if( !IS_INVALID_SOCKET( data_socket ) && !RdosIsTcpConnectionClosed( data_socket ) ) { #else if( !IS_INVALID_SOCKET( data_socket ) ) { #endif send_len = len; CONV_LE_16( send_len ); if( !IS_SOCK_ERROR( send( data_socket, (void *)&send_len, sizeof( send_len ), 0 ) ) ) { if( len == 0 || !IS_SOCK_ERROR( send( data_socket, (void *)rec, len, 0 ) ) ) { #ifdef __RDOS__ RdosPushTcpConnection( data_socket ); #endif _DBG_NET(("RemotePut...OK\r\n")); return( len ); } } } return( REQUEST_FAILED ); } #ifndef __RDOS__ static void nodelay( void ) { struct protoent *proto; int delayoff; int p; delayoff = 1; proto = getprotobyname( "tcp" ); p = proto ? proto->p_proto : IPPROTO_TCP; setsockopt( data_socket, p, TCP_NODELAY, (void *)&delayoff, sizeof( delayoff ) ); } #endif bool RemoteConnect( void ) { #ifdef SERVER #ifdef __RDOS__ void *obj; obj = RdosWaitTimeout( wait_handle, 250 ); if( obj != NULL ) { data_socket = RdosGetTcpListen( listen_handle ); if( !IS_INVALID_SOCKET( data_socket ) ) { _DBG_NET(("Found a connection\r\n")); return( TRUE ); } } #else struct timeval timeout; fd_set ready; struct sockaddr dummy; trp_socklen dummy_len = sizeof( dummy ); FD_ZERO( &ready ); FD_SET( control_socket, &ready ); timeout.tv_sec = 0; timeout.tv_usec = 10000; if( select( control_socket + 1, &ready, 0, 0, &timeout ) > 0 ) { data_socket = accept( control_socket, &dummy, &dummy_len ); if( !IS_INVALID_SOCKET( data_socket ) ) { nodelay(); _DBG_NET(("Found a connection\r\n")); return( TRUE ); } } #endif #else #ifdef __RDOS__ // todo: Add code for connect! #else data_socket = socket( AF_INET, SOCK_STREAM, 0 ); if( !IS_INVALID_SOCKET( data_socket ) ) { if( connect( data_socket, (struct sockaddr TRAPFAR *)&socket_address, sizeof( socket_address ) ) >= 0 ) { nodelay(); return( TRUE ); } } #endif #endif return( FALSE ); }