SNMP OID Validation in C:

SNMP Object Identifiers (OIDs)

Object identifier or OID is an identifier used to name an object (compare URN). Structurally, an OID consists of a node in a hierarchically-assigned namespace, formally defined using the ITU-T's ASN.1 standard. Successive numbers of the nodes, starting at the root of the tree, identify each node in the tree. Designers set up new nodes by registering them under the node's registration authority. The root of the tree contains the following three arcs:

* 0: ITU-T
* 1: ISO
* 2: joint-iso-itu-t

Samples:

Here is a sample structure of an OID:
Iso (1).org(3).dod(6).internet(1).private(4).transition(868).products(2).chassis(4).card(1).slotCps(2)­
.­cpsSlotSummary(1).cpsModuleTable(1).cpsModuleEntry(1).cpsModuleModel(3).3562.3
or
1.3.6.1.4.868.2.4.1.2.1.1.1.3.3562.3 



//Code :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE (!TRUE)
#endif
#define NUL 0

//commentout the following line to disable the debug prints
#define DEBUG_TRACE 1

#ifdef DEBUG_TRACE
#define TRACE_PRINT(x) printf x
#else
#define TRACE_PRINT(x) /*It does nothing */
#endif

/* Function Prototypes */
int validateNumbricOID(char *oidString);
int countDots(char *inString);
int splitString(const char *string,const char *delimiter ,char *bufs[]);
int IsNumeric(char *input);

int main()
{
    char oidString[255];
    while(TRUE)
    {
      printf("Enter OID String:");
      scanf("%s",oidString);
      if(validateNumbricOID(oidString))
      {
          printf("validated ...correct oid found\n");
      }
      else printf("validated...incorrect oid found\n");
    }

    return 0;
}

/***********************************************************************
* Name  : validateNumbricOID                                           *
*                                                                      *
* input : string or character array                                    *
*                                                                      *
* output: true or false                                                *
*                                                                      *
* Modification History :                                               *
*                                                                      *
*                                                                      *
************************************************************************/
int validateNumbricOID(char *oidString)
{
    int ndots = 0,nvalues = 0,i;
    char *oidNumerics[255];
    char const *delimiter = ".";

    ndots = countDots(oidString);
    TRACE_PRINT(("Init Oid validation Process,Given OID : %s \n",oidString));
    TRACE_PRINT(("Number of dots found                  : %d \n",ndots));
    nvalues = splitString(oidString,delimiter,oidNumerics);
    if(nvalues > NUL)
    {
        for(i = 0; i < nvalues ; i++)
        {
            TRACE_PRINT(("nvalue    : %d \n",(int)oidNumerics[i]));
            TRACE_PRINT(("isNumeric : %d \n",IsNumeric(oidNumerics[i])));
            if(IsNumeric(oidNumerics[i])== 0)
            return FALSE;
        }
    }
    else return FALSE;
    TRACE_PRINT(("Number of numberics found : %d \n",nvalues));
    for(i = 0; i < nvalues ; i++)
    {
        free(oidNumerics[i]);
    }
    return TRUE;
}

/***********************************************************************
* Name  : countDots                                                    *
*                                                                      *
* input : input string                                                 *
*                                                                      *
* output: returns number of dots found                                 *
*                                                                      *
* Modification History :                                               *
*                                                                      *
*                                                                      *
************************************************************************/
int countDots(char *inString)
{
    int i = 0,nDots = 0;
    for(i = 0; inString[i] != '\0'; i++)
    {
        if(inString[i] == '.')
        nDots++;
    }
    return nDots;
}

/***********************************************************************
* Name  : SplitString                                                  *
*                                                                      *
* input : input string to be splitted,delimiter                        *
*                                                                      *
* output: the splitted output strings                                  *
*                                                                      *
* Modification History :                                               *
*                                                                      *
*                                                                      *
************************************************************************/
int splitString(const char *string,const char *delimiter ,char *bufs[])
{
    char *token,*inLine;
    int bufCnt = 0;
    inLine = strdup(string);
    token = strtok(inLine,delimiter);
    bufs[bufCnt] = strdup(token);
    while (token != NULL)
    {
        bufs[bufCnt++] = strdup(token);
        token = strtok(NULL,delimiter);
    }
    return bufCnt;
}

//From the XXXX code
int IsNumeric(char *input)
{
    int rc = TRUE;
    unsigned int i = 0;

    for(i = 0; i < strlen(input) ; i++)
    {
        if(!((input[i] >= '0') && (input[i] <= '9')))
            rc = FALSE;
    }
    return rc;
}









SNMP Engine ID validation in C:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE (!TRUE)
#endif
#define NUL 0

//commentout the following line to disable the debug prints
#define DEBUG_TRACE 1

#ifdef DEBUG_TRACE
#define TRACE_PRINT(x) printf x
#else
#define TRACE_PRINT(x) /*It does nothing */
#endif

enum DELIMITER_USER
{

    eDELIMITER_STOP = 0,
    eDELIMITER_COLON = 1,
    eDELIMITER_INVALID = -1,
};



/* Function Prototypes */
int validateEngineID(char *oidString);
int countSeperator(char *inString);
int splitString(const char *string,const char *delimiter ,char *bufs[]);
int IsNumeric(char *input);
int findDelimiter(char *input);
int IsHexaNumber(char *input);


int main()
{
    char engineIdString[64];
    while(TRUE)
    {
      printf("\nEnter Engine ID String:");
      scanf("%s",engineIdString);
      if(validateEngineID(engineIdString))
      {
          printf("validated ...correct engine ID found\n");
      }
      else printf("validated...incorrect engine ID found\n");
    }

    return 0;
}

/***********************************************************************
* Name  : validateEngineID                                             *
*                                                                      *
* input : string or character array                                    *
*                                                                      *
* output: true or false                                                *
*                                                                      *
* Modification History :                                               *
*                                                                      *
*                                                                      *
************************************************************************/
int validateEngineID(char *engineIdString)
{
    int rc = TRUE;
    int nseperators = 0,nvalues = 0,i;
    char *engineIdNumerics[64];
    char const *delimiter[2]  = {".",":"};
    nseperators = countSeperator(engineIdString);
    TRACE_PRINT(("Init Oid validation Process,Given OID : %s \n",engineIdString));
    TRACE_PRINT(("Number of dots found                  : %d \n",nseperators));
    if(findDelimiter(engineIdString)!= eDELIMITER_INVALID )
    {
        printf("Find delimiter output:%d ,delimiter = %s\n",findDelimiter(engineIdString),delimiter[findDelimiter(engineIdString)]);
        nvalues = splitString(engineIdString,delimiter[findDelimiter(engineIdString)],engineIdNumerics);
        if(nvalues > NUL)
        {
            for(i = 0; i < nvalues ; i++)
            {
                TRACE_PRINT(("nvalue    : %d \n",(int)engineIdNumerics[i]));
                TRACE_PRINT(("isNumeric : %d \n",IsNumeric(engineIdNumerics[i])));
                if(IsHexaNumber(engineIdNumerics[i]) == 0)
                rc = FALSE;
            }
        }
        else rc = FALSE;
    }
    else rc = FALSE;
    TRACE_PRINT(("Number of numberics found : %d \n",nvalues));
    for(i = 0; i < nvalues ; i++)
    {
        free(engineIdNumerics[i]);
    }
    return rc;
}

/***********************************************************************
* Name  : countSeperator                                               *
*                                                                      *
* input : input string                                                 *
*                                                                      *
* output: returns number of seperators found                           *
*                                                                      *
* Modification History :                                               *
*                                                                      *
*                                                                      *
************************************************************************/
int countSeperator(char *inString)
{
    int i = 0,nSeperators = 0;
    for(i = 0; inString[i] != '\0'; i++)
    {
        if(inString[i] == '.' || inString[i] == ':')
        nSeperators++;
    }
    return nSeperators;
}


/***********************************************************************
* Name  : SplitString                                                  *
*                                                                      *
* input : input string to be splitted,delimiter                        *
*                                                                      *
* output: the splitted output strings                                  *
*                                                                      *
* Modification History :                                               *
*                                                                      *
*                                                                      *
************************************************************************/
int splitString(const char *string,const char *delimiter ,char *bufs[])
{
    char *token,*inLine;
    int bufCnt = 0;
    inLine = strdup(string);
    token = strtok(inLine,delimiter);
    bufs[bufCnt] = strdup(token);
    while (token != NULL)
    {
        bufs[bufCnt++] = strdup(token);
        token = strtok(NULL,delimiter);
    }
    return bufCnt;
}

//From the XXXX code
int IsNumeric(char *input)
{
    int rc = TRUE;
    unsigned int i = 0;

    for(i = 0; i < strlen(input) ; i++)
    {
        if(!((input[i] >= '0') && (input[i] <= '9')))
            rc = FALSE;
    }
    return rc;
}

/***********************************************************************
* Name  : IsHexaNumber                                                 *
*                                                                      *
* input : input string                                                 *
*                                                                      *
* output: true or false                                                *
*                                                                      *
* Modification History :                                               *
*                                                                      *
*                                                                      *
************************************************************************/
/* An hexadecimal digit is one of the following characters:
   0 1 2 3 4 5 6 7 8 9 A B C D E F a b c d e f
   */
int IsHexaNumber(char *input)
{
    int rc = TRUE;
    unsigned int i = 0;

    for(i = 0; i < strlen(input) ; i++)
    {
        if(!((input[i] >= '0') && (input[i] <= '9')) && !((input[i] >= 'A') && (input[i] <= 'F')) \
          && !((input[i] >= 'a') && (input[i] <= 'f')))
            rc = FALSE;
    }
    return rc;
}

/***********************************************************************
* Name  : findDelimiter                                                 *
*                                                                      *
* input : input string                                                 *
*                                                                      *
* output: delimiter enum output                                        *
*                                                                      *
* Modification History :                                               *
*                                                                      *
*                                                                      *
************************************************************************/
int findDelimiter(char *input)
{
    int rc = eDELIMITER_INVALID, len = strlen(input),sCount = 0,cCount = 0,i = 0;

    for(i = 0; i < len ; i++)
    {
        if(input[i] == '.')
        {
            sCount++;

        }
        else if(input[i] == ':')
        {
            cCount++;
        }
    }
    if(cCount == 0 && sCount == 0)
    rc = eDELIMITER_INVALID;
    else if(cCount == 0)
    rc = eDELIMITER_STOP;
    else if(sCount == 0)
    rc = eDELIMITER_COLON;
    return rc;
}


It is very helpful to improve the postings if you give some comments on this post
Thanks -

No comments: