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

  1. /*
  2.  * TAP Support
  3.  *
  4.  * To add TAP support, you must call the tap_add() function at least once.
  5.  * The first or successive calls can be used to change the default userid or
  6.  * specific userids on a per-socket basis.
  7.  *
  8.  * tap_add( NULL, "joe" )   adds the default userid of joe
  9.  * tap_add( NULL, NULL )    reverts the default userid back to the default
  10.  * tap_add( socket, "joe" ) sets the specific socket to have userid joe
  11.  * tap_add( socket, NULL )  tells the specific socket to keep current with the
  12.  *                          default userid (and change when it does)
  13.  *
  14.  * NOTE: this is a system level service, it only includes <wattcp.h> because
  15.  *       it resides BELOW the application layer.  Applications must always
  16.  *       include <tcp.h> instead!
  17.  */
  18.  
  19. #include <wattcp.h>
  20.  
  21. #define TAPPORT 113
  22. #define S_INIT  0
  23. #define S_WAIT  1
  24. #define S_CONN  2   /* connected and talking */
  25. #define S_CLOS  3
  26.  
  27. static tcp_Socket *taps = NULL;
  28. static int tapstatus = S_INIT;
  29. static char *root = "root";
  30. static char *userid;
  31.  
  32.  
  33. tap_tick()
  34. {
  35.     word hisport, myport;
  36.     tcp_Socket *t;
  37.     switch ( tapstatus ) {
  38.         case S_INIT :   tcp_listen( taps, TAPPORT, 0L, 0, NULL, 0 );
  39.                         sock_mode( taps, TCP_MODE_ASCII );
  40.                         tapstatus = S_WAIT;
  41.                         break;
  42.         case S_WAIT :   if ( sock_established( taps )) tapstatus = S_CONN;
  43.                         if ( ! tcp_tick( taps )) s->status = S_CLOS;
  44.                         break;
  45.         case S_CONN :   if ( sock_dataready( taps )) {
  46.                             sock_scanf( taps, "%u,%u", &myport, &hisport );
  47.                             /* must find appropriate socket */
  48.                             if ( t = _tcp_lookup( taps->hisaddr, hisport, myport ))
  49.                                 sock_printf( taps,
  50.                                     "%u, %u : USERID : OTHER : %s",
  51.                                         myport, hisport,
  52.                                         (t->usr_name)? t->usr_name : userid );
  53.                             else
  54.                                 sock_printf( taps,
  55.                                     "%u, %u : ERROR : UNKNOWN-ERROR",
  56.                                         myport, hisport );
  57.                             sock_close( taps );
  58.                             s->status = S_CLOS;
  59.                         }
  60.                         if (!tcp_tick( taps )) {
  61.                             sock_close( taps );
  62.                             s->status = S_CLOS;
  63.                         }
  64.                         break;
  65.             case S_CLOS :
  66.                         sock_mode( taps, TCP_MODE_BINARY );
  67.                         if ( sock_dataready( taps ))
  68.                             sock_fastread( taps, NULL, 512 );
  69.                         if ( !tcp_tick( t ) ) {
  70.                             for ( count = i = 0; i < fingersessions; ++i )
  71.                                 if (fingers[i]->status != S_WAIT ) count++;
  72.                             printf("CLOS : session %u --- %u capacity\n",
  73.                                 sess, (--count*100)/fingersessions );
  74.                             s->status = S_INIT;
  75.                         }
  76.                         break;
  77.         }
  78.     }
  79. }
  80.  
  81. tap_add( tcp_Socket *s, char *localuserid )
  82. {
  83.     char *p;
  84.     /* check if we are initialized */
  85.     if ( taps == NULL ) {
  86.         taps = calloc( 1, sizeof( tcp_Socket ));
  87.         addwattcpd( tap_tick );
  88.     }
  89.  
  90.     /* check userid */
  91.     if ( localuserid ) {
  92.         for ( p = localuserid ; *p ; p++ ) {
  93.             if ( (*p < 33) || (*p > 126)) {
  94.                 /* revert to default */
  95.                 if ( s ) localuserid = userid;
  96.                 else localuserid = root;
  97.             }
  98.         }
  99.     }
  100.  
  101.     /* change system default userid or session based userid */
  102.     if ( s == NULL ) userid = (localuserid) ? localuserid : root;
  103.     else s->usr_name = (localuserid) ? localuserid : userid;
  104. }
  105.  
  106.