shar_tbl.c
Upload User: caisha3
Upload Date: 2013-09-21
Package Size: 208739k
Code Size: 12k
Category:

Windows Develop

Development Platform:

Visual C++

  1. /*++
  2. Copyright (c) 1992-1996  Microsoft Corporation
  3. Module Name:
  4.     shar_tbl.c
  5. Abstract:
  6.     All routines to perform operations on the Share Table.
  7. Environment:
  8.     User Mode - Win32
  9. Revision History:
  10.     10-May-1996 DonRyan
  11.         Removed banner from Technology Dynamics, Inc.
  12. --*/
  13. //--------------------------- WINDOWS DEPENDENCIES --------------------------
  14. //--------------------------- STANDARD DEPENDENCIES -- #include<xxxxx.h> ----
  15. #include <stdio.h>
  16. #include <memory.h>
  17. //--------------------------- MODULE DEPENDENCIES -- #include"xxxxx.h" ------
  18. #include <snmp.h>
  19. #include <snmputil.h>
  20. #include "mibfuncs.h"
  21. //--------------------------- SELF-DEPENDENCY -- ONE #include"module.h" -----
  22. #include "shar_tbl.h"
  23. //--------------------------- PUBLIC VARIABLES --(same as in module.h file)--
  24.    // Prefix to the Share table
  25. static UINT                shareSubids[] = { 2, 27, 1 };
  26. static AsnObjectIdentifier MIB_SharePrefix = { 3, shareSubids };
  27. SHARE_TABLE      MIB_ShareTable = { 0, NULL };
  28. //--------------------------- PRIVATE CONSTANTS -----------------------------
  29. #define SHARE_FIELD_SUBID      (MIB_SharePrefix.idLength+MIB_OidPrefix.idLength)
  30. #define SHARE_FIRST_FIELD       SHARE_NAME_FIELD
  31. #define SHARE_LAST_FIELD        SHARE_COMMENT_FIELD
  32. //--------------------------- PRIVATE STRUCTS -------------------------------
  33. //--------------------------- PRIVATE VARIABLES -----------------------------
  34. //--------------------------- PRIVATE PROTOTYPES ----------------------------
  35. UINT MIB_shares_get(
  36.         IN OUT RFC1157VarBind *VarBind
  37. );
  38. int MIB_shares_match(
  39.        IN AsnObjectIdentifier *Oid,
  40.        OUT UINT *Pos
  41.        );
  42. UINT MIB_shares_copyfromtable(
  43.         IN UINT Entry,
  44.         IN UINT Field,
  45.         OUT RFC1157VarBind *VarBind
  46.         );
  47. //--------------------------- PRIVATE PROCEDURES ----------------------------
  48. //--------------------------- PUBLIC PROCEDURES -----------------------------
  49. //
  50. // MIB_shares_func
  51. //    High level routine for handling operations on the Share table
  52. //
  53. // Notes:
  54. //
  55. // Return Codes:
  56. //    None.
  57. //
  58. // Error Codes:
  59. //    None.
  60. //
  61. UINT MIB_shares_func(
  62. IN UINT Action,
  63.         IN MIB_ENTRY *MibPtr,
  64. IN OUT RFC1157VarBind *VarBind
  65. )
  66. {
  67. int     Found;
  68. UINT    Entry;
  69. UINT    Field;
  70. UINT    ErrStat;
  71.    switch ( Action )
  72.       {
  73.       case MIB_ACTION_GETFIRST:
  74.          // Fill the Share Table with the info from server
  75.          MIB_shares_lmget();
  76.          // If no elements in table, then return next MIB var, if one
  77.          if ( MIB_ShareTable.Len == 0 )
  78.             {
  79.             if ( MibPtr->MibNext == NULL )
  80.                {
  81.                ErrStat = SNMP_ERRORSTATUS_NOSUCHNAME;
  82.                goto Exit;
  83.                }
  84.             // Do get first on the next MIB var
  85.             ErrStat = (*MibPtr->MibNext->MibFunc)( Action, MibPtr->MibNext,
  86.                                                    VarBind );
  87.             break;
  88.             }
  89.          //
  90.          // Place correct OID in VarBind
  91.          // Assuming the first field in the first record is the "start"
  92.          {
  93.          UINT temp_subs[] = { SHARE_FIRST_FIELD };
  94.          AsnObjectIdentifier FieldOid = { 1, temp_subs };
  95.          SnmpUtilOidFree( &VarBind->name );
  96.          SnmpUtilOidCpy( &VarBind->name, &MIB_OidPrefix );
  97.          SnmpUtilOidAppend( &VarBind->name, &MIB_SharePrefix );
  98.          SnmpUtilOidAppend( &VarBind->name, &FieldOid );
  99.          SnmpUtilOidAppend( &VarBind->name, &MIB_ShareTable.Table[0].Oid );
  100.          }
  101.          //
  102.          // Let fall through on purpose
  103.          //
  104.       case MIB_ACTION_GET:
  105.          ErrStat = MIB_shares_get( VarBind );
  106.  break;
  107.       case MIB_ACTION_GETNEXT:
  108.          // Fill the Share table with the info from server
  109.          MIB_shares_lmget();
  110.          // Determine which field
  111.          Field = VarBind->name.ids[SHARE_FIELD_SUBID];
  112.         // Lookup OID in table
  113.          if (Field < SHARE_FIRST_FIELD)
  114.          {
  115.              Entry = 0;                 // will take the first entry into the table
  116.              Field = SHARE_FIRST_FIELD;  // and the first column of the table
  117.              Found = MIB_TBL_POS_BEFORE;
  118.          }
  119.          else if (Field > SHARE_LAST_FIELD)
  120.              Found = MIB_TBL_POS_END;
  121.          else
  122.              Found = MIB_shares_match( &VarBind->name, &Entry );
  123.          // Index not found, but could be more fields to base GET on
  124.          if ((Found == MIB_TBL_POS_BEFORE && MIB_ShareTable.Len == 0) ||
  125.               Found == MIB_TBL_POS_END )
  126.             {
  127.             // Index not found in table, get next from field
  128. //            Field ++;
  129.             // Make sure not past last field
  130. //            if ( Field > SHARE_LAST_FIELD )
  131. //               {
  132.                // Get next VAR in MIB
  133.                ErrStat = (*MibPtr->MibNext->MibFunc)( MIB_ACTION_GETFIRST,
  134.                                                       MibPtr->MibNext,
  135.                                                       VarBind );
  136.                break;
  137. //               }
  138.             }
  139.          // Get next TABLE entry
  140.          if ( Found == MIB_TBL_POS_FOUND )
  141.             {
  142.             Entry ++;
  143.             if ( Entry > MIB_ShareTable.Len-1 )
  144.                {
  145.                Entry = 0;
  146.                Field ++;
  147.                if ( Field > SHARE_LAST_FIELD )
  148.                   {
  149.                   // Get next VAR in MIB
  150.                   ErrStat = (*MibPtr->MibNext->MibFunc)( MIB_ACTION_GETFIRST,
  151.                                                          MibPtr->MibNext,
  152.                                                          VarBind );
  153.                   break;
  154.                   }
  155.                }
  156.             }
  157.          //
  158.          // Place correct OID in VarBind
  159.          // Assuming the first field in the first record is the "start"
  160.          {
  161.          UINT temp_subs[1];
  162.          AsnObjectIdentifier FieldOid;
  163.          temp_subs[0]      = Field;
  164.          FieldOid.idLength = 1;
  165.          FieldOid.ids      = temp_subs;
  166.          SnmpUtilOidFree( &VarBind->name );
  167.          SnmpUtilOidCpy( &VarBind->name, &MIB_OidPrefix );
  168.          SnmpUtilOidAppend( &VarBind->name, &MIB_SharePrefix );
  169.          SnmpUtilOidAppend( &VarBind->name, &FieldOid );
  170.          SnmpUtilOidAppend( &VarBind->name, &MIB_ShareTable.Table[Entry].Oid );
  171.          }
  172.          ErrStat = MIB_shares_copyfromtable( Entry, Field, VarBind );
  173.          break;
  174.       case MIB_ACTION_SET:
  175.          ErrStat = SNMP_ERRORSTATUS_NOSUCHNAME;
  176.  break;
  177.       default:
  178.          ErrStat = SNMP_ERRORSTATUS_GENERR;
  179.       }
  180. Exit:
  181.    return ErrStat;
  182. } // MIB_shares_func
  183. //
  184. // MIB_shares_get
  185. //    Retrieve Share table information.
  186. //
  187. // Notes:
  188. //
  189. // Return Codes:
  190. //    None.
  191. //
  192. // Error Codes:
  193. //    None.
  194. //
  195. UINT MIB_shares_get(
  196.         IN OUT RFC1157VarBind *VarBind
  197. )
  198. {
  199. UINT   Entry;
  200. int    Found;
  201. UINT   ErrStat;
  202.    if (VarBind->name.ids[SHARE_FIELD_SUBID] < SHARE_FIRST_FIELD ||
  203.        VarBind->name.ids[SHARE_FIELD_SUBID] > SHARE_LAST_FIELD)
  204.        {
  205.        ErrStat = SNMP_ERRORSTATUS_NOSUCHNAME;
  206.        goto Exit;
  207.        }
  208.    // Fill the Share table with the info from server
  209.    MIB_shares_lmget();
  210.    Found = MIB_shares_match( &VarBind->name, &Entry );
  211.    // Look for a complete OID match
  212.    if ( Found != MIB_TBL_POS_FOUND )
  213.       {
  214.       ErrStat = SNMP_ERRORSTATUS_NOSUCHNAME;
  215.       goto Exit;
  216.       }
  217.    // Copy data from table
  218.    ErrStat = MIB_shares_copyfromtable( Entry, VarBind->name.ids[SHARE_FIELD_SUBID],
  219.                                      VarBind );
  220. Exit:
  221.    return ErrStat;
  222. } // MIB_shares_get
  223. //
  224. // MIB_shares_match
  225. //    Match the target OID with a location in the Share table
  226. //
  227. // Notes:
  228. //
  229. // Return Codes:
  230. //    None.
  231. //
  232. // Error Codes:
  233. //    None
  234. //
  235. int MIB_shares_match(
  236.        IN AsnObjectIdentifier *Oid,
  237.        OUT UINT *Pos
  238.        )
  239. {
  240. AsnObjectIdentifier TempOid;
  241. int                 nResult;
  242.    // Remove prefix including field reference
  243.    TempOid.idLength = Oid->idLength - MIB_OidPrefix.idLength -
  244.                       MIB_SharePrefix.idLength - 1;
  245.    TempOid.ids = &Oid->ids[MIB_OidPrefix.idLength+MIB_SharePrefix.idLength+1];
  246.    *Pos = 0;
  247.    while ( *Pos < MIB_ShareTable.Len )
  248.       {
  249.       nResult = SnmpUtilOidCmp( &TempOid, &MIB_ShareTable.Table[*Pos].Oid );
  250.       if ( !nResult )
  251.          {
  252.          nResult = MIB_TBL_POS_FOUND;
  253.          goto Exit;
  254.          }
  255.       if ( nResult < 0 )
  256.          {
  257.          nResult = MIB_TBL_POS_BEFORE;
  258.          goto Exit;
  259.          }
  260.       (*Pos)++;
  261.       }
  262.    nResult = MIB_TBL_POS_END;
  263. Exit:
  264.    return nResult;
  265. }
  266. //
  267. // MIB_shares_copyfromtable
  268. //    Copy requested data from table structure into Var Bind.
  269. //
  270. // Notes:
  271. //
  272. // Return Codes:
  273. //    None.
  274. //
  275. // Error Codes:
  276. //    None.
  277. //
  278. UINT MIB_shares_copyfromtable(
  279.         IN UINT Entry,
  280.         IN UINT Field,
  281.         OUT RFC1157VarBind *VarBind
  282.         )
  283. {
  284. UINT ErrStat;
  285.    // Get the requested field and save in var bind
  286.    switch( Field )
  287.       {
  288.       case SHARE_NAME_FIELD:
  289.          // Alloc space for string
  290.          VarBind->value.asnValue.string.stream = SnmpUtilMemAlloc( sizeof(char)
  291.                        * MIB_ShareTable.Table[Entry].svShareName.length );
  292.          if ( VarBind->value.asnValue.string.stream == NULL )
  293.             {
  294.             ErrStat = SNMP_ERRORSTATUS_GENERR;
  295.             goto Exit;
  296.             }
  297.          // Copy string into return position
  298.          memcpy( VarBind->value.asnValue.string.stream,
  299.                        MIB_ShareTable.Table[Entry].svShareName.stream,
  300.                        MIB_ShareTable.Table[Entry].svShareName.length );
  301.          // Set string length
  302.          VarBind->value.asnValue.string.length =
  303.                           MIB_ShareTable.Table[Entry].svShareName.length;
  304.          VarBind->value.asnValue.string.dynamic = TRUE;
  305.          // Set type of var bind
  306.          VarBind->value.asnType = ASN_RFC1213_DISPSTRING;
  307.          break;
  308.       case SHARE_PATH_FIELD:
  309.          // Alloc space for string
  310.          VarBind->value.asnValue.string.stream = SnmpUtilMemAlloc( sizeof(char)
  311.                        * MIB_ShareTable.Table[Entry].svSharePath.length );
  312.          if ( VarBind->value.asnValue.string.stream == NULL )
  313.             {
  314.             ErrStat = SNMP_ERRORSTATUS_GENERR;
  315.             goto Exit;
  316.             }
  317.          // Copy string into return position
  318.          memcpy( VarBind->value.asnValue.string.stream,
  319.                        MIB_ShareTable.Table[Entry].svSharePath.stream,
  320.                        MIB_ShareTable.Table[Entry].svSharePath.length );
  321.          // Set string length
  322.          VarBind->value.asnValue.string.length =
  323.                           MIB_ShareTable.Table[Entry].svSharePath.length;
  324.          VarBind->value.asnValue.string.dynamic = TRUE;
  325.          // Set type of var bind
  326.          VarBind->value.asnType = ASN_RFC1213_DISPSTRING;
  327.          break;
  328.       case SHARE_COMMENT_FIELD:
  329.          // Alloc space for string
  330.          VarBind->value.asnValue.string.stream = SnmpUtilMemAlloc( sizeof(char)
  331.                        * MIB_ShareTable.Table[Entry].svShareComment.length );
  332.          if ( VarBind->value.asnValue.string.stream == NULL )
  333.             {
  334.             ErrStat = SNMP_ERRORSTATUS_GENERR;
  335.             goto Exit;
  336.             }
  337.          // Copy string into return position
  338.          memcpy( VarBind->value.asnValue.string.stream,
  339.                        MIB_ShareTable.Table[Entry].svShareComment.stream,
  340.                        MIB_ShareTable.Table[Entry].svShareComment.length );
  341.          // Set string length
  342.          VarBind->value.asnValue.string.length =
  343.                           MIB_ShareTable.Table[Entry].svShareComment.length;
  344.          VarBind->value.asnValue.string.dynamic = TRUE;
  345.          // Set type of var bind
  346.          VarBind->value.asnType = ASN_RFC1213_DISPSTRING;
  347.          break;
  348.       default:
  349.          SNMPDBG(( SNMP_LOG_TRACE, "LMMIB2: Internal Error Share Tablen" ));
  350.          ErrStat = SNMP_ERRORSTATUS_GENERR;
  351.          goto Exit;
  352.       }
  353.    ErrStat = SNMP_ERRORSTATUS_NOERROR;
  354. Exit:
  355.    return ErrStat;
  356. } // MIB_shares_copyfromtable
  357. //-------------------------------- END --------------------------------------