CodeWarrior (OS 9)/OTEndpointInfo.c

/*
    File:       OTEndpointInfo.c
 
    Contains:   Sample to dump out the endpoint info for common OT endpoint types.
 
    Written by: Quinn "The Eskimo!" 
 
    Copyright:  Copyright © 1997-1999 by Apple Computer, Inc., All Rights Reserved.
 
                You may incorporate this Apple sample source code into your program(s) without
                restriction. This Apple sample source code has been provided "AS IS" and the
                responsibility for its operation is yours. You are not permitted to redistribute
                this Apple sample source code as "Apple sample source code" after having made
                changes. If you're going to re-distribute the source, we require that you make
                it clear in the source that the code was descended from Apple sample source
                code, but that you've made changes.
 
    Change History (most recent first):
                7/22/1999   Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
                
 
*/
// The OT debugging macros in <OTDebug.h> require this variable to
// be set.
 
#ifndef qDebug
#define qDebug  1
#endif
 
/////////////////////////////////////////////////////////////////////
// Pick up all the standard OT stuff.
 
#include <OpenTransport.h>
 
/////////////////////////////////////////////////////////////////////
// Pick up device type definitions.
 
#include <OpenTptLinks.h>
 
/////////////////////////////////////////////////////////////////////
// Pick up device type definitions.
 
#include <OpenTptAppleTalk.h>
 
/////////////////////////////////////////////////////////////////////
// Pick up device type definitions.
 
#include <OpenTptInternet.h>
 
/////////////////////////////////////////////////////////////////////
// Pick up the OTDebugBreak and OTAssert macros.
 
#include <OTDebug.h>
 
/////////////////////////////////////////////////////////////////////
// Standard C prototypes.
 
#include <stdio.h>
 
/////////////////////////////////////////////////////////////////////
// OTDebugStr is not defined in any OT header files, but it is
// exported by the libraries, so we define the prototype here.
 
extern pascal void OTDebugStr(const char* str);
 
/////////////////////////////////////////////////////////////////////
 
/*******************************************************************************
** EndPointInfo
********************************************************************************/
 
enum {
    kNumberOfServiceTypeNames = 8
};
 
static char *gServiceTypeConstants[kNumberOfServiceTypeNames] = {
    "unknown",
    "T_COTS",
    "T_COTS_ORD",
    "T_CLTS",
    "unknown",
    "T_TRANS",
    "T_TRANS_ORD",
    "T_TRANS_CLTS"
};
 
static char *gServiceTypeDescriptions[kNumberOfServiceTypeNames] = {
    "unknown",
    "Connection-oriented, transactionless",
    "Connection-oriented, transactionless, orderly release",
    "Connectionless, transactionless",
    "unknown",
    "Connection-oriented, transaction",
    "Connection-oriented, transaction, orderly release",
    "Connectionless, transaction"
};
 
static void PrintFlag(char *flagName, UInt32 flagField, UInt32 flagMask)
    // If the flagMask bit in flagField is set, print the flagName
    // string.
{
  printf("    %s = %d\n", flagName, (flagField & flagMask) != 0);
}
 
static void ValueToString(SInt32 value, char *valueString)
    // Convert value to a string and put it in valueString,
    // formatting the two special case values appropriately.
{
    switch (value) {
        case T_INFINITE:
            sprintf(valueString, "T_INFINITE (%ld)", value);
            break;
        case T_INVALID:
            sprintf(valueString, "T_INVALID  (%ld)", value);
            break;
        default:
            sprintf(valueString, "%ld", value);
            break;
    }
}
 
static void OpenEndpointAndPrintInfo(char *config)
    // Open an endpoint with the given configuration, call
    // OTGetEndpointInfo to get the endpoint information,
    // and print that out in a nicely formatted manner.
{
    OSStatus err;
    EndpointRef ep;
    TEndpointInfo epInfo;
    char *serviceTypeConstant;
    char *serviceTypeDescription;
    char valueString[256];
        
    ep = OTOpenEndpoint(OTCreateConfiguration(config), 0, &epInfo, &err);
    if (err == noErr) {
        printf("TEndpointInfo for Ò%sÓ\n", config);
        
        ValueToString(epInfo.addr, valueString);
        printf("  addr (max size of address)              = %s\n", valueString);
        
        ValueToString(epInfo.options, valueString);
        printf("  options (max size of options)           = %s\n", valueString);
        
        ValueToString(epInfo.tsdu, valueString);
        printf("  tsdu (max size of data unit)            = %s\n", valueString);
        
        ValueToString(epInfo.etsdu, valueString);
        printf("  etsdu (max size of expedited data unit) = %s\n", valueString);
        
        ValueToString(epInfo.connect, valueString);
        printf("  connect (max size of connect data)      = %s\n", valueString);
        
        ValueToString(epInfo.discon, valueString);
        printf("  discon (max size of disconnect data)    = %s\n", valueString);
        
        if (epInfo.servtype >= 0 && epInfo.servtype < kNumberOfServiceTypeNames) {
            serviceTypeConstant = gServiceTypeConstants[epInfo.servtype];
            serviceTypeDescription = gServiceTypeDescriptions[epInfo.servtype];
        } else {
            serviceTypeConstant = "unknown";
            serviceTypeDescription = "unknown";
        };
        printf("  servtype                                = %s (%ld)\n", serviceTypeConstant, epInfo.servtype);
        printf("    (%s)\n", serviceTypeDescription);
        printf("  flags                                   = $%08x\n", epInfo.flags);
        PrintFlag("T_SENDZERO (can send 0 byte units)        ", epInfo.flags, T_SENDZERO);
        PrintFlag("T_XPG4_1 (supports GetProtAddress)        ", epInfo.flags, T_XPG4_1);
        PrintFlag("CAN_RESOLVE_ADDR (supports ResolveAddress)", epInfo.flags, T_CAN_RESOLVE_ADDR);
        PrintFlag("CAN_SUPPLY_MIB (SNMP)                     ", epInfo.flags, T_CAN_SUPPLY_MIB);
        PrintFlag("T_CAN_SUPPORT_MDATA (support M_DATA mode) ", epInfo.flags, T_CAN_SUPPORT_MDATA);
 
        (void) OTCloseProvider(ep);
    };
    
    if (err != noErr) {
      printf("OpenEndpointAndPrintInfo(%s) = %d\n", config, err);
    };
    printf("\n");
}
 
static void EndpointInfo(void)
{
    OpenEndpointAndPrintInfo(kEnetName);
    
    OpenEndpointAndPrintInfo(kRawIPName);
    OpenEndpointAndPrintInfo(kUDPName);
    OpenEndpointAndPrintInfo(kTCPName);
    
    OpenEndpointAndPrintInfo(kDDPName);
    OpenEndpointAndPrintInfo(kATPName);
    OpenEndpointAndPrintInfo(kPAPName);
    OpenEndpointAndPrintInfo(kADSPName);
}
 
/////////////////////////////////////////////////////////////////////
 
void main(void)
{
    OSStatus err;
    
    printf("OTEndpointInfo\n");
    printf("-- Prints TEndpointInfo for common endpoint configurations.\n");
    printf("-- This is for informational purposes only, don't even think\n");
    printf("-- about hard-coding any of these values!  If you need them you\n");
    printf("-- should fetch them at run time using OTGetEndpointInfo.\n");
    printf("\n");
    
    err = InitOpenTransport();
    
    if (err == noErr) {
    
        EndpointInfo();
        
        CloseOpenTransport();
    }
    
    if (err == noErr) {
        printf("Success.\n");
    } else {
        printf("Failed with error %d.\n", err);
    }
    printf("Done.  Press command-Q to Quit.\n");
}