Listing 1.6
Read and Write Name Service Headers

NS_Header.c
#include <netinet/in.h>     /* htons(), ntohs(), etc. */

#include "NS_Header.h"      /* From listing 1.5.      */


void Put_NS_TID( ushort hdr[], ushort TrnID )
  /* ---------------------------------------------------- **
   * Store the transaction ID in the Name Service header.
   * ---------------------------------------------------- **
   */
  {
  hdr[0] = htons( TrnID );
  } /* Put_NS_TID */

void Put_NS_Hdr_Flags( ushort hdr[], ushort flags )
  /* ---------------------------------------------------- **
   * Store the flags in the NBT Name Service header.
   * ---------------------------------------------------- **
   */
  {
  hdr[1] = htons( flags );
  } /* Put_NS_Hdr_Flags */

void Put_NS_Hdr_Rec_Counts( ushort hdr[], int reccount )
  /* ---------------------------------------------------- **
   * Place (ushort)1 into each record count field for
   * which the matching flag bit is set in reccount.
   * ---------------------------------------------------- **
   */
  {
  ushort one;

  one = htons( 1 );

  hdr[2] = ( QUERYREC & reccount ) ? one : 0;
  hdr[3] = ( ANSREC   & reccount ) ? one : 0;
  hdr[4] = ( NSREC    & reccount ) ? one : 0;
  hdr[5] = ( ADDREC   & reccount ) ? one : 0;
  } /* Put_NS_Hdr_Rec_Counts */


ushort Get_NS_Hdr_TID( ushort hdr[] )
  /* ---------------------------------------------------- **
   * Read and return the transaction ID.
   * ---------------------------------------------------- **
   */
  {
  return( ntohs( hdr[0] ) );
  } /* Get_NS_Hdr_TID */

ushort Get_NS_Hdr_Flags( ushort hdr[] )
  /* ---------------------------------------------------- **
   * Read and return the flags field.
   * ---------------------------------------------------- **
   */
  {
  return( ntohs( hdr[1] ) );
  } /* Get_NS_Hdr_Flags */

int Get_NS_Hdr_Rec_Counts( ushort hdr[] )
  /* ---------------------------------------------------- **
   * Convert the four record count fields into a single
   * flagset.
   * ---------------------------------------------------- **
   */
  {
  int tmp = 0;

  if( hdr[2] )
    tmp |= QUERYREC;
  if( hdr[3] )
    tmp |= ANSREC;
  if( hdr[4] )
    tmp |= NSREC;
  if( hdr[5] )
    tmp |= ADDREC;

  return( tmp );
  } /* Get_NS_Hdr_Rec_Counts */


$Revision: 1.14 $
$Date: 2003/02/18 21:43:58 $
[W3C Validated] Copyright © 1999-2003 Christopher R. Hertel 
Released under the terms of the LGPL