home *** CD-ROM | disk | FTP | other *** search
/ Network Support Encyclopedia 96-1 / novell-nsepro-1996-1-cd2.iso / download / netware / dax1.exe / CP / CPS / CPCONN.C next >
Text File  |  1992-07-15  |  7KB  |  187 lines

  1. //   ╔════════════════════════════════════════════════════════════════════╗
  2. //   ║                                                                    ║
  3. //   ║ module:      cpconn.c                                              ║
  4. //   ║ abstract:    This module contains the support APIs for handling    ║
  5. //   ║              connection requests.  It also houses the client       ║
  6. //   ║              array and manipulation APIs.                          ║
  7. //   ║                                                                    ║
  8. //   ║ environment: NetWare 3.x v3.11                                     ║
  9. //   ║              Network C for NLMs SDK                                ║
  10. //   ║              CLib v3.11                                            ║
  11. //   ║                                                                    ║
  12. //   ║  This software is provided as is and carries no warranty           ║
  13. //   ║  whatsoever.  Novell disclaims and excludes any and all implied    ║
  14. //   ║  warranties of merchantability, title and fitness for a particular ║
  15. //   ║  purpose.  Novell does not warrant that the software will satisfy  ║
  16. //   ║  your requirements or that the software is without defect or error ║
  17. //   ║  or that operation of the software will be uninterrupted.  You are ║
  18. //   ║  using the software at your risk.  The software is not a product   ║
  19. //   ║  of Novell, Inc. or any of subsidiaries.                           ║
  20. //   ║                                                                    ║
  21. //   ╟────────────────────────────────────────────────────────────────────╢
  22. //   ║ maintenance history:                                               ║
  23. //   ║ level    date      pi   description                                ║
  24. //   ╟────────────────────────────────────────────────────────────────────╢
  25. //   ║  001   02/05/92    kl   initial release.                           ║
  26. //   ╚════════════════════════════════════════════════════════════════════╝
  27.  
  28. #include <string.h>
  29. #include "cp/cpsys.h"
  30.  
  31. //------------------------------------------------------------------------
  32.  
  33. //
  34. //  __clients is the array that is used to maintain session data for the
  35. //  communication protocol layer of the application server.
  36. //
  37.  
  38. STATIC  CPDATA  __clients[CPSDEFNUMCLNTS];
  39.  
  40. //
  41. //  This API returns a pointer to a given clients session data.  It is
  42. //  used by the sending thread to get info needed to send the request.
  43. //
  44.  
  45. CPDATA  *CPGetClientInfo(LONG index)
  46. {
  47.         if( index > CPSDEFNUMCLNTS )      return NULL;
  48.         if( __clients[index].sip != TRUE) return NULL;
  49.  
  50.         return &__clients[index];
  51. }
  52.  
  53. #define     NOTFOUND                -1
  54.  
  55. STATIC  UINT32  CPAddressAlreadyInTable(InternetAddress *ipxaddr)
  56. {
  57.         UINT32  i;
  58.  
  59.         for(i=0; i < CPSDEFNUMCLNTS; ++i){
  60.             if( __clients[i].sip == TRUE ){
  61.                 if( !memcmp(&__clients[i].ipxaddr,ipxaddr,sizeof *ipxaddr) ){
  62.                     xDIAG4(CPprintf("Client %d re-sent allocate session\n",i));
  63.                     return i;
  64.                 }
  65.             }
  66.         }
  67.         return NOTFOUND;
  68. }
  69.  
  70. STATIC  UINT32  CPAllocateSlot()
  71. {
  72.         UINT32  i;
  73.  
  74.         for(i=0; i < CPSDEFNUMCLNTS; ++i){
  75.             if( __clients[i].sip == FALSE ){
  76.                 __clients[i].sip = TRUE;
  77.                 return i;
  78.             }
  79.         }
  80.         DIAG3("CPAllocateSlot() failed because table is full");
  81.         return -1;
  82. }
  83.  
  84. STATIC  T_RC    CPDeAllocateSlot(UINT32 slot)
  85. {
  86.         if( __clients[slot].sip == TRUE ){
  87.             __clients[slot].sip = FALSE;
  88.         }
  89.         else{
  90.             DIAG3("CPDeAllocateSlot() passed slot id which was not in use");
  91.         }
  92.         return CP_SUCCESS;
  93. }
  94.  
  95.  
  96. T_RC    CPClearSessionID(UINT32 sessionID)
  97. {
  98.         return CPDeAllocateSlot(sessionID);
  99. }
  100.  
  101. //
  102. //  This API locates the session ID for a given client.  It is called
  103. //  by the receive message thread, to determine which client has sent
  104. //  the request.  This API will allocate a new session ID if the client
  105. //  is not found in the array.
  106. //
  107.  
  108. UINT32  CPGetSessionID(IPX_HEADER *IPXhdr, CPMESSAGE *CPmsg)
  109. {
  110.         CPDATA  *cpd;
  111.         UINT32  serverID = CPmsg->cphdr.serverID - 1;
  112.         //
  113.         //  Do authentication of signature in packet.  If not valid,
  114.         //  print a message and ignore this request.
  115.         //
  116.         if(CPmsg->cphdr.signature != CPPKTSIG ){
  117.             HEXDIAG1("Invalid CP packet signature on request", CPmsg);
  118.             return -1;
  119.         }
  120.         if( (cpd = CPGetClientInfo(serverID)) != NULL ){
  121.             //
  122.             //  Get here if the index in the CP header is valid and
  123.             //  there is a session in progress there.
  124.             //
  125.             if( !memcmp(&__clients[serverID].ipxaddr,&IPXhdr->sourceNet,sizeof(InternetAddress)) ){
  126.                 //!!
  127.                 //  Enhance by acknowledging the duplicate underneath
  128.                 //!!
  129.                 // 
  130.                 //  Get here if it came from the same address that we have
  131.                 //  registered in the client array
  132.                 // 
  133.                 return serverID;
  134.             }
  135.             HEXDIAG1("Client authentication failed for session ",serverID);
  136.         }
  137.         //
  138.         //  See if client is already registered on a different session ID.
  139.         //  This can occur if client resends an AllocateSession DAP, since
  140.         //  the serverID will still be zero.
  141.         //
  142.         //!!
  143.         //  This will still be a problem if the client reboots, or tries
  144.         //  a different socket number.
  145.         //
  146.         //  Need to implement some sort of WATCHDOG on the engine, to
  147.         //  detect these failed connections.
  148.         //!!
  149.         switch( serverID = CPAddressAlreadyInTable((InternetAddress *)&IPXhdr->sourceNet) ){
  150.         case NOTFOUND:
  151.             if( (serverID = CPAllocateSlot()) == -1 ) return -1;
  152.             break;
  153.         default:
  154.             //
  155.             //  We found his serverID.
  156.             //
  157.             xDIAG4(CPprintf("Returning with serverID = %d\r\n",serverID));
  158.             return serverID;
  159.             break;
  160.         }
  161.         xDIAG4(CPprintf("Falling thru with serverID = %d\r\n",serverID));
  162.         //
  163.         //  Save what we need to for communication with this client.
  164.         //  We need the internet address, and put the index in there
  165.         //  so he can use it on subsequent calls.  Remember, the 'cphdr'
  166.         //  element will be copied into the reply packet on the way out.
  167.         //
  168.         if( (cpd = CPGetClientInfo(serverID)) == NULL){
  169.             DIAG1("cpd is NULL, and it shouldn't be!\n");
  170.             return -1;
  171.         }
  172.         cpd->cphdr = CPmsg->cphdr;      // xfer client header
  173.         cpd->ipxaddr = *(InternetAddress *)&IPXhdr->sourceNet;
  174.         cpd->cphdr.serverID = serverID + 1;
  175.  
  176.         return serverID;
  177. }
  178.  
  179. //------------------------------------------------------------------------
  180. //  Here are the APIs to init and deinit the connection logic
  181. //
  182. //
  183.  
  184. T_RC    CPInitializeConnLogic(){ return 0;}
  185. T_RC    CPDeInitializeConnLogic(){return 0;}
  186. 
  187.