home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / hamradio / wattcp.zip / PCBSD.C < prev    next >
C/C++ Source or Header  |  1992-02-04  |  4KB  |  183 lines

  1. #include <copyright.h>
  2. #include <stdio.h>
  3. #include <wattcp.h>
  4. #include <stdlib.h>    /* itoa */
  5. #include <string.h>
  6. #include <elib.h>
  7.  
  8. /*
  9.  * PCBSD - provide some typical BSD UNIX functionality
  10.  * Erick Engelke, Feb 22, 1991
  11.  */
  12.  
  13. /*
  14.  * chk_socket - determine whether a real socket or not
  15.  *
  16.  */
  17. _chk_socket( tcp_Socket *s )
  18. {
  19.     if ( s->ip_type == TCP_PROTO ) {
  20.     if ( s->state <= tcp_StateCLOSED)    /* skips invalid data */
  21.         return( 2 );
  22.     }
  23.     if ( s->ip_type == UDP_PROTO ) return( 1 );
  24.     return( 0 );
  25. }
  26.  
  27. char *inet_ntoa( char *s, longword x )
  28. {
  29.  
  30.     itoa( x >> 24, s, 10 );
  31.     strcat( s, ".");
  32.     itoa( (x >> 16) & 0xff, strchr( s, 0), 10);
  33.     strcat( s, ".");
  34.     itoa( (x >> 8) & 0xff, strchr( s, 0), 10);
  35.     strcat( s, ".");
  36.     itoa( (x) & 0xff, strchr( s, 0), 10);
  37.     return( s );
  38. }
  39. char *psocket( tcp_Socket *s )
  40. {
  41.     char buffer[255];
  42.  
  43.     outch( '[' );
  44.     outs( inet_ntoa( buffer, s->hisaddr) );
  45.     outch( ':' );
  46.     itoa( s->hisport, buffer, 10 );
  47.     outs( buffer );
  48.     outch( ']' );
  49. }
  50.  
  51. longword inet_addr( char *s )
  52. {
  53.     if ( isaddr( s ))
  54.         return( aton( s ));
  55.     else
  56.         return( 0 );
  57. }
  58.  
  59. char *sockerr( tcp_Socket *s )
  60. {
  61.     if ( strlen( s->err_msg ) < 80 )
  62.         return( s->err_msg );
  63. }
  64.  
  65. static char *sock_states[] = {
  66.     "Listen","SynSent","SynRec","Established","FinWt1","FinWt2","ClosWt","LastAck"
  67.     "TmWt","Closed"};
  68.  
  69. char *sockstate( tcp_Socket *s )
  70. {
  71.     switch ( _chk_socket( s )) {
  72.        case  1 : return( "UDP Socket" );
  73.        case  2 : return( sock_states[ s->state ] );
  74.        default : return( "Not an active socket");
  75.     }
  76. }
  77.  
  78. getpeername( tcp_Socket *s, void *dest, int *len )
  79. {
  80.     struct sockaddr temp;
  81.     int ltemp;
  82.  
  83.     memset( &temp, 0, sizeof( struct sockaddr ));
  84.     temp.s_ip = s->hisaddr;
  85.     temp.s_port = s->hisport;
  86.  
  87.     if (!s->hisaddr || !s->hisport || ! _chk_socket( s )) {
  88.     if (len) *len = 0;
  89.     return( -1 );
  90.     }
  91.  
  92.     /* how much do we move */
  93.     ltemp = (len) ? *len : sizeof( struct sockaddr );
  94.     if (ltemp > sizeof( struct sockaddr)) ltemp = sizeof( struct sockaddr );
  95.     qmove( &temp, dest, ltemp );
  96.  
  97.     if (len) *len = ltemp;
  98.     return( 0 );
  99. }
  100.  
  101. getsockname(  tcp_Socket *s, void *dest, int *len )
  102. {
  103.     struct sockaddr temp;
  104.     int ltemp;
  105.  
  106.     memset( &temp, 0, sizeof( struct sockaddr ));
  107.     temp.s_ip = s->myaddr;
  108.     temp.s_port = s->myport;
  109.  
  110.     if (!s->hisaddr || !s->hisport || ! _chk_socket( s )) {
  111.     if (len) *len = 0;
  112.     return( -1 );
  113.     }
  114.  
  115.     /* how much do we move */
  116.     ltemp = (len) ? *len : sizeof( struct sockaddr );
  117.     if (ltemp > sizeof( struct sockaddr)) ltemp = sizeof( struct sockaddr );
  118.     qmove( &temp, dest, ltemp );
  119.  
  120.     if (len) *len = ltemp;
  121.     return( 0 );
  122. }
  123.  
  124. longword gethostid()
  125. {
  126.     return( my_ip_addr );
  127. }
  128.  
  129. longword sethostid( longword ip )
  130. {
  131.     return( my_ip_addr = ip );
  132. }
  133.  
  134. char *getdomainname( char *name, int length )
  135. {
  136.     if ( length ) {
  137.     if ( length < strlen( def_domain ))
  138.         *name = 0;
  139.     else
  140.         strcpy( name, def_domain );
  141.     return( name );
  142.     }
  143.     return( ( def_domain && *def_domain ) ? def_domain : NULL );
  144. }
  145.  
  146. char *setdomainname( char *string )
  147. {
  148.     return( def_domain = string );
  149. }
  150.  
  151. char *gethostname( char *name, int len )
  152. {
  153.     if ( len ) {
  154.     if (len < strlen( _hostname ))
  155.         *name = 0;
  156.     else
  157.         strcpy( name, _hostname );
  158.     return( name );
  159.     }
  160.     return( ( _hostname && *_hostname ) ?  _hostname  : NULL );
  161. }
  162. char *sethostname( char *name )
  163. {
  164.     return( _hostname = name );
  165. }
  166. word ntohs( word a )
  167. {
  168.     return( intel16(a) );
  169. }
  170. word htons( word a )
  171. {
  172.     return( intel16(a) );
  173. }
  174. longword ntohl( longword x )
  175. {
  176.     return( intel( x ));
  177. }
  178. longword htonl( longword x )
  179. {
  180.     return( intel( x ));
  181. }
  182.  
  183.