NBP.c

/*------------------------------------------------------------------------------
#
#   NBP.c : this module contains all the Name Binding Protocol functions
#
#
#   Versions:   1.0                 10/91
#   C.Buttin - Apple Computer Europe            
#
------------------------------------------------------------------------------*/
#include <Limits.h>
#include <errors.h>
#include <Types.h>
#include <Memory.h>
#include <stdIO.h>
#include <String.h>
#include <Strings.h>
#include <Devices.h>
#include <AppleTalk.h>
 
 
/* functions */
pascal  Boolean     InitNBP(Boolean SelfOn);        /* SelfSend mode */
pascal  void        CloseNBP(Boolean SelfOn);       /* SelfSend mode */
/* SetSelfSend :  Set self mode */
pascal  void        SetSelfSend(Boolean flagOn);
 
/* NBPGetList : Get the List of entities with a specific entity name into a buffer
   Input : 
        - Ptr on a buffer to receive the data
        - size of the buffer
        - pointer on the names of entities to be looked for 
        - maximum number of entities to be looked for
    Output :
        - total number of entities or an error code */ 
pascal  short       NBPGetList(Ptr buffer,
                               short bufSize,
                               EntityPtr entity,
                               short maxEntities);
 
/* NBPGetAddress : Extract the AppleTalk address of an entity
   Input : 
        - Ptr on a buffer containing the data
        - position of the AppleTalk address to be found
        - total numver of entities 
        - NBP name of the entity
        - AppleTalk address of the entity
    Output :
        - true if the address is found */ 
pascal  Boolean     NBPGetAddress(Ptr buffer,
                                  short tuplenum,
                                  short numEntities,
                                  EntityPtr entity,
                                  AddrBlock *address);
 
/* NBPRegisterEntity : creates an entry in the Name Binding Table
    Input : 
        - socket number
        - entity to be associated with this socket
    OutPut :
        - Pointer on the name table entry. This pointer must be available as
          long as the entity has not been removed from the table. */ 
pascal  Ptr         NBPRegisterEntity(unsigned char theSocket,
                                 EntityPtr entity);
 
/* NBPRemoveEntity : remove an entity from the Name Binding Table
    Input : 
        - entity    */
pascal  OSErr       NBPRemoveEntity(EntityPtr entity);
 
 
 
                            /**     CODE        **/
                            
 
pascal Boolean InitNBP(Boolean SelfOn)
{
 
    if (MPPOpen() != noErr)
        return false;
    if (SelfOn)
        SetSelfSend(true);                  
 
} /* InitNBP */
 
 
pascal void CloseNBP(Boolean SelfOn)
{
    SetSelfSend(SelfOn);        /* disable SelfSend for other applications (chooser...) */
} /* CloseNBP */
 
/* Set self mode */
pascal void SetSelfSend(Boolean flagOn)     /* true to enabled it, false to disable it */
{
    MPPParamBlock MPPBlock;
    
    MPPBlock.SETSELF.newSelfFlag = flagOn;           /* self-send toggle flag */
    PSetSelfSend((MPPPBPtr)&MPPBlock,false);
 
} /* SetSelfSend */
 
/* Get the names and addresses of the entities defined by a NBP name.
   Return the items in the buffer */
pascal short NBPGetList(Ptr buffer,short bufSize,EntityPtr entity,short maxEntities)
{   
    MPPParamBlock   MPPBlock;           /* to build parameter block for NBP */
    OSErr           error;
    
    if (bufSize < (sizeof(EntityName)+sizeof(AddrBlock)) * maxEntities)
        return(paramErr);
    /* set NBP record */
    MPPBlock.NBP.ioCompletion = nil;                /* no completion routine */
    MPPBlock.NBP.interval = 8;                      /* retry interval(8-ticks units) */
    MPPBlock.NBP.count = 3;                         /* retry count */
    MPPBlock.NBP.NBPPtrs.entityPtr = (Ptr)entity;   /* entity name to be looked at */
    MPPBlock.NBP.parm.Lookup.retBuffPtr = buffer;   /* reception buffer */
    MPPBlock.NBP.parm.Lookup.retBuffSize = bufSize;
    MPPBlock.NBP.parm.Lookup.maxToGet  = maxEntities;
            
    if ((error = PLookupName((MPPPBPtr) &MPPBlock,false)) != noErr) 
        return error;
 
    /* Return the actual number of entities */
    return(MPPBlock.NBP.parm.Lookup.numGotten);
} /* NBPGetList */
 
/* Get the AppleTalk address of the nth entity  */
pascal Boolean NBPGetAddress(Ptr buffer,short tuplenum,short numEntities,EntityPtr entity,AddrBlock *address)
{
    
    if (tuplenum > numEntities)
        return false;
    if (NBPExtract(buffer,numEntities,tuplenum,entity, address) != noErr) {
        return false;
        }
    else return true;
} /* NBPGetAddress */
 
/* register an an entity in the Name Binding Table. */
pascal Ptr NBPRegisterEntity(unsigned char theSocket,EntityPtr entity)
{
    MPPParamBlock   MPPBlock;   
    Ptr             NTEPtr;
    
    /* Builds up entity name  */
    if (!(NTEPtr = NewPtr(sizeof(EntityName)+9)))
        return nil;
    NBPSetNTE(NTEPtr,entity->objStr,entity->typeStr,entity->zoneStr,theSocket); 
 
    /* set NBP record */
    MPPBlock.NBP.ioCompletion = nil;        /* no completion routine */
    MPPBlock.NBP.interval = 8;              /* retry interval (8-tick unit) */
    MPPBlock.NBP.count = 1;                 /* retry count */
    MPPBlock.NBP.NBPPtrs.ntQElPtr = NTEPtr; /* entity name + socket to be registered at */
    MPPBlock.NBP.parm.verifyFlag = 1;       /* verify if name already exists */
    if (PRegisterName((MPPPBPtr)&MPPBlock,false) != noErr) {
        DisposPtr(NTEPtr);
        return nil;
        }
    else return NTEPtr;
} /* NBPRegisterEntity */
 
/* remove an entity from the binding table */
pascal OSErr NBPRemoveEntity(EntityPtr entity)          
{
    MPPParamBlock MPPBlock;     
 
    /* set up NBP ParamBlock */
    MPPBlock.NBP.ioCompletion = nil;                /* no completion routine */
    MPPBlock.NBP.NBPPtrs.entityPtr = (Ptr)entity;   /* entity name to be removed */
 
    return (PRemoveName((MPPPBPtr)&MPPBlock,false));
} /* NBPRemoveEntity */