Listing 3.3
Parsing NetServerEnum2 Replies


#define NERR_Success 0

#define SV_TYPE_ALL               0xFFFFFFFF
#define SV_TYPE_UNKNOWN           0x1F000000

#define SV_TYPE_DOMAIN_ENUM       0x80000000
#define SV_TYPE_LOCAL_LIST_ONLY   0x40000000
#define SV_TYPE_ALTERNATE_XPORT   0x20000000
#define SV_TYPE_DFS_SERVER        0x00800000
#define SV_TYPE_WIN95_PLUS        0x00400000
#define SV_TYPE_SERVER_VMS        0x00200000
#define SV_TYPE_SERVER_OSF        0x00100000
#define SV_TYPE_DOMAIN_MASTER     0x00080000
#define SV_TYPE_MASTER_BROWSER    0x00040000
#define SV_TYPE_BACKUP_BROWSER    0x00020000
#define SV_TYPE_POTENTIAL_BROWSER 0x00010000
#define SV_TYPE_SERVER_NT         0x00008000
#define SV_TYPE_SERVER_MFPN       0x00004000
#define SV_TYPE_WFW               0x00002000
#define SV_TYPE_NT                0x00001000
#define SV_TYPE_SERVER_UNIX       0x00000800
#define SV_TYPE_DIALIN_SERVER     0x00000400
#define SV_TYPE_PRINTQ_SERVER     0x00000200
#define SV_TYPE_DOMAIN_MEMBER     0x00000100
#define SV_TYPE_NOVELL            0x00000080
#define SV_TYPE_AFP               0x00000040
#define SV_TYPE_TIME_SOURCE       0x00000020
#define SV_TYPE_DOMAIN_BAKCTRL    0x00000010
#define SV_TYPE_DOMAIN_CTRL       0x00000008
#define SV_TYPE_SQLSERVER         0x00000004
#define SV_TYPE_SERVER            0x00000002
#define SV_TYPE_WORKSTATION       0x00000001


typedef struct
  {
  ushort Status;
  ushort Convert;
  ushort EntryCount;
  ushort AvailCount;
  } NSE2_ReplyParams;


void PrintBrowserBits( ulong ServerType )
  /* ---------------------------------------------------- **
   * Itemize Browse Service Provider Type Bits.
   * This is boring, and could probably be done better
   * using an array and a for() loop.
   * ---------------------------------------------------- **
   */
  {
  if( SV_TYPE_ALL == ServerType )
    {
    printf( "  All/Any Server types.\n" );
    return;
    }

  if( SV_TYPE_UNKNOWN & ServerType )
    printf( "  Warning: Undefined bits set.\n" );

  if( SV_TYPE_DOMAIN_ENUM & ServerType )
    printf( "  Enumerate Domains\n" );
  if( SV_TYPE_LOCAL_LIST_ONLY & ServerType )
    printf( "  Local List Only\n" );
  if( SV_TYPE_ALTERNATE_XPORT & ServerType )
    printf( "  Alternate Export (Unknown type)\n" );
  if( SV_TYPE_DFS_SERVER & ServerType )
    printf( "  DFS Support\n" );
  if( SV_TYPE_WIN95_PLUS & ServerType )
    printf( "  Windows95+\n" );
  if( SV_TYPE_SERVER_VMS & ServerType )
    printf( "  VMS (Pathworks) Server\n" );
  if( SV_TYPE_SERVER_OSF & ServerType )
    printf( "  OSF Unix Server\n" );
  if( SV_TYPE_DOMAIN_MASTER & ServerType )
    printf( "  Domain Master Browser\n" );
  if( SV_TYPE_MASTER_BROWSER & ServerType )
    printf( "  Local Master Browser\n" );
  if( SV_TYPE_BACKUP_BROWSER & ServerType )
    printf( "  Backup Browser\n" );
  if( SV_TYPE_POTENTIAL_BROWSER & ServerType )
    printf( "  Potential Browser\n" );
  if( SV_TYPE_SERVER_NT & ServerType )
    printf( "  Windows NT (or compatible) Server\n" );
  if( SV_TYPE_SERVER_MFPN & ServerType )
    printf( "  MFPN (Unkown type)\n" );
  if( SV_TYPE_WFW & ServerType )
    printf( "  Windows for Workgroups\n" );
  if( SV_TYPE_NT & ServerType )
    printf( "  Windows NT Workstation\n" );
  if( SV_TYPE_SERVER_UNIX & ServerType )
    printf( "  Unix/Xenix/Samba Server\n" );
  if( SV_TYPE_DIALIN_SERVER & ServerType )
    printf( "  Dialin Server\n" );
  if( SV_TYPE_PRINTQ_SERVER & ServerType )
    printf( "  Print Server\n" );
  if( SV_TYPE_DOMAIN_MEMBER & ServerType )
    printf( "  NT Domain Member Server\n" );
  if( SV_TYPE_NOVELL & ServerType )
    printf( "  Novell Server\n" );
  if( SV_TYPE_AFP & ServerType )
    printf( "  Apple Server\n" );
  if( SV_TYPE_TIME_SOURCE & ServerType )
    printf( "  Time Source\n" );
  if( SV_TYPE_DOMAIN_BAKCTRL & ServerType )
    printf( "  Backup Domain Controller\n" );
  if( SV_TYPE_DOMAIN_CTRL & ServerType )
    printf( "  Domain Controller\n" );
  if( SV_TYPE_SQLSERVER & ServerType )
    printf( "  SQL Server\n" );
  if( SV_TYPE_SERVER & ServerType )
    printf( "  SMB Server\n" );
  if( SV_TYPE_WORKSTATION & ServerType )
    printf( "  Workstation\n" );
  } /* PrintBrowserBits */


void PrintNetServerEnum2Reply( uchar *ParamBlock,
                               int    ParamLen,
                               uchar *DataBlock,
                               int    DataLen )
  /* ---------------------------------------------------- **
   * Parse a NetServerEnum2 Reply and print the contents.
   * ---------------------------------------------------- **
   */
  {
  NSE2_ReplyParams Rep;
  int              i;
  int              offset;
  uchar           *pos;

  /* Check for an obvious error.
   */
  if( ParamLen != 8 )
    Fail( "Error parsing NetServerEnum2 reply.\n" );

  /* Grab all of the parameter words.
   */
  Rep.Status     = smb_GetShort( ParamBlock, 0 );
  Rep.Convert    = smb_GetShort( ParamBlock, 2 );
  Rep.EntryCount = smb_GetShort( ParamBlock, 4 );
  Rep.AvailCount = smb_GetShort( ParamBlock, 6 );

  /* Check for problems (errors and warnings).
   */
  if( Rep.Status != NERR_Success )
    Fail( "NetServerEnum2 Error: %d.\n", Rep.Status );
  if( Rep.EntryCount < Rep.AvailCount )
    printf( "Warning: The list is incomplete.\n" );

  /* Dump the ServerInfo_1 records.
   */
  pos = DataBlock;
  for( i = 0; i < Rep.EntryCount; i++ )
    {
    printf( "%-15s V%d.%d\n", pos, pos[16], pos[17] );
    PrintBrowserBits( smb_GetLong( pos, 18 ) );
    offset  = 0x0000FFFF & smb_GetLong( pos, 22 );
    offset -= Rep.Convert;
    if( offset >= DataLen )
      Fail( "Packet offset error.\n" );
    printf( "  Comment: %s\n", (DataBlock + offset) );
    pos += 26;
    }
  } /* PrintNetServerEnum2Reply */


$Revision: 1.1 $
$Date: 2003/04/22 06:03:37 $
[W3C Validated] Copyright © 1999-2003 Christopher R. Hertel 
Released under the terms of the LGPL