home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Media Share 9
/
MEDIASHARE_09.ISO
/
hamradio
/
wattcp.zip
/
PCBSD.C
< prev
next >
Wrap
C/C++ Source or Header
|
1992-02-04
|
4KB
|
183 lines
#include <copyright.h>
#include <stdio.h>
#include <wattcp.h>
#include <stdlib.h> /* itoa */
#include <string.h>
#include <elib.h>
/*
* PCBSD - provide some typical BSD UNIX functionality
* Erick Engelke, Feb 22, 1991
*/
/*
* chk_socket - determine whether a real socket or not
*
*/
_chk_socket( tcp_Socket *s )
{
if ( s->ip_type == TCP_PROTO ) {
if ( s->state <= tcp_StateCLOSED) /* skips invalid data */
return( 2 );
}
if ( s->ip_type == UDP_PROTO ) return( 1 );
return( 0 );
}
char *inet_ntoa( char *s, longword x )
{
itoa( x >> 24, s, 10 );
strcat( s, ".");
itoa( (x >> 16) & 0xff, strchr( s, 0), 10);
strcat( s, ".");
itoa( (x >> 8) & 0xff, strchr( s, 0), 10);
strcat( s, ".");
itoa( (x) & 0xff, strchr( s, 0), 10);
return( s );
}
char *psocket( tcp_Socket *s )
{
char buffer[255];
outch( '[' );
outs( inet_ntoa( buffer, s->hisaddr) );
outch( ':' );
itoa( s->hisport, buffer, 10 );
outs( buffer );
outch( ']' );
}
longword inet_addr( char *s )
{
if ( isaddr( s ))
return( aton( s ));
else
return( 0 );
}
char *sockerr( tcp_Socket *s )
{
if ( strlen( s->err_msg ) < 80 )
return( s->err_msg );
}
static char *sock_states[] = {
"Listen","SynSent","SynRec","Established","FinWt1","FinWt2","ClosWt","LastAck"
"TmWt","Closed"};
char *sockstate( tcp_Socket *s )
{
switch ( _chk_socket( s )) {
case 1 : return( "UDP Socket" );
case 2 : return( sock_states[ s->state ] );
default : return( "Not an active socket");
}
}
getpeername( tcp_Socket *s, void *dest, int *len )
{
struct sockaddr temp;
int ltemp;
memset( &temp, 0, sizeof( struct sockaddr ));
temp.s_ip = s->hisaddr;
temp.s_port = s->hisport;
if (!s->hisaddr || !s->hisport || ! _chk_socket( s )) {
if (len) *len = 0;
return( -1 );
}
/* how much do we move */
ltemp = (len) ? *len : sizeof( struct sockaddr );
if (ltemp > sizeof( struct sockaddr)) ltemp = sizeof( struct sockaddr );
qmove( &temp, dest, ltemp );
if (len) *len = ltemp;
return( 0 );
}
getsockname( tcp_Socket *s, void *dest, int *len )
{
struct sockaddr temp;
int ltemp;
memset( &temp, 0, sizeof( struct sockaddr ));
temp.s_ip = s->myaddr;
temp.s_port = s->myport;
if (!s->hisaddr || !s->hisport || ! _chk_socket( s )) {
if (len) *len = 0;
return( -1 );
}
/* how much do we move */
ltemp = (len) ? *len : sizeof( struct sockaddr );
if (ltemp > sizeof( struct sockaddr)) ltemp = sizeof( struct sockaddr );
qmove( &temp, dest, ltemp );
if (len) *len = ltemp;
return( 0 );
}
longword gethostid()
{
return( my_ip_addr );
}
longword sethostid( longword ip )
{
return( my_ip_addr = ip );
}
char *getdomainname( char *name, int length )
{
if ( length ) {
if ( length < strlen( def_domain ))
*name = 0;
else
strcpy( name, def_domain );
return( name );
}
return( ( def_domain && *def_domain ) ? def_domain : NULL );
}
char *setdomainname( char *string )
{
return( def_domain = string );
}
char *gethostname( char *name, int len )
{
if ( len ) {
if (len < strlen( _hostname ))
*name = 0;
else
strcpy( name, _hostname );
return( name );
}
return( ( _hostname && *_hostname ) ? _hostname : NULL );
}
char *sethostname( char *name )
{
return( _hostname = name );
}
word ntohs( word a )
{
return( intel16(a) );
}
word htons( word a )
{
return( intel16(a) );
}
longword ntohl( longword x )
{
return( intel( x ));
}
longword htonl( longword x )
{
return( intel( x ));
}