www.pudn.com > Firewall_PNE_3_3.zip > fwNvIfRam.c, change:2009-03-16,size:5442b


/* fwNvIfRam.c - Firewall Non-Volatile storage interface for RAM */

/* Copyright 2004-2005 Wind River Systems, Inc. */
#include "copyright_wrs.h"

/*
modification history
--------------------
01b,02feb05,myz  added IPV6 support
01a,11feb04,myz  written
*/

/*
DESCRIPTION

This library provides a sample RAM based Non-Volatile storage interface. It
simply tries to demostrate how the rule lists can be saved and restored across
the soft reset. It may also be used directly for a battery backed RAM device.


*/

#include "vxWorks.h"
#include "stdio.h"
#include "ioLib.h"
#include "stdlib.h"
#include "string.h"

/* currently there are only eight independent parameter blocks identified by
 * the eight parameter block Ids. They are "prein", "input", "forwd" and 
 * "output" for each IPV4 or IPV6 stack. In the future, this list may be 
 * expended when MAC filter rules needs to be saved. In that case, 
 * the parameter NUM_OF_RULELISTS should be changed accordingly.
 */
 
#define NUM_OF_RULELISTS   8

LOCAL int ramSize = 0;
LOCAL char * pRamBase = 0;
LOCAL int listSize = 0;

LOCAL char nvRamData[0x10000];

/******************************************************************************
*
* fwNvIfRamParamsGet - Retrieve the parameter block from NV storage
*
* Read the entire parameter block identified by pParamsId into the use supplied
* buffer pointed to by pParamsBuf.
*
* RETURNS: the actual length of retrieved data or 0 if fails
*/

int fwNvIfRamParamsGet
    (
    char * pParamsId,    /* the parameter block ID, 0 terminated less than or
                          * equal 8 byte(including 0 character) string */
    char * pParamsBuf,   /* buffer to store the parameters */
    int    len,          /* the length of the buffer */
    int    offset     /* offset from the start of the storage */
    )
    {
    int i;

    /* first find the parameter ID */

    for (i = 0; i < NUM_OF_RULELISTS; i++)
        {
        if (strcmp(pParamsId,pRamBase + i * listSize) == 0)
            break;
        }

    if (i == NUM_OF_RULELISTS)
        return 0;
 
    /* we have a match, copy the data to user buffer, data always start at
     * offset 8
     */

    bcopy(pRamBase + i * listSize + offset + 8, pParamsBuf, len);

    return len;
    }

/******************************************************************************
*
* fwNvIfRamParamsSet - Set the parameter block to NV storage
*
* Write the entire parameter block pointed to by pParams to the NV storage
* identified by parameter block ID pParamsId.
*
* RETURNS: the actual written length  or 0 if fails
*
*/

int fwNvIfRamParamsSet
    (
    char * pParamsId,    /* the parameter block ID, 0 terminated less than or
                          * equal 8 byte(including 0 character) string */
    char * pParams,   /* parameter block for storing */
    int    len,       /* the length of the parameter block */
    int    offset     /* offset from the start of the storage */
    )
    {
    int i;
    int copySize;

    /* first find the parameter ID */

    for (i = 0; i < NUM_OF_RULELISTS; i++)
        {
        if (*(pRamBase + i * listSize) == 0)
            {
            /* the first time, save the parameter ID */

            strcpy(pRamBase + i * listSize,pParamsId);
        
            /* now copy the data, data always start from offset 8 */ 
       
            copySize = min(listSize - offset, len);
            bcopy(pParams, pRamBase + i * listSize + offset + 8,copySize);
            return copySize;
            }
        else
            {
            if (strcmp(pParamsId,pRamBase + i * listSize) == 0)
                {
                /* we have a match */

                copySize = min(listSize - offset, len);
                bcopy(pParams, pRamBase + i * listSize + offset + 8,copySize);
                return copySize;
                }
            }
        }

    /* run out of space */

    printf("no more space or invalid parameter ID\n");
    return 0;
    }

/*****************************************************************************
*
* fwNvIfRamParamsClose - Close a file
*
*/
int fwNvIfRamParamsClose
    (
    char * pParamsId
    )
    {
    return OK;
    } 
        
/******************************************************************************
*
* fwNvIfRamParamsInit - Initialize the sample NV RAM interface
*
* the parameter pStr has the format "memAddr,memSize"
*
*/

int fwNvIfRamParamsInit
    (
    char * pStr   /* has the format "memAddr,memSize" */
    )
    {
    if (pStr != NULL)
        {
        char * pHolder = NULL;
        char * pToken;
        char * pTmp;

        pTmp = malloc(strlen(pStr) + 1);

        if (pTmp == NULL)
            return ERROR;

        strcpy(pTmp,pStr);
        pStr = pTmp;

        /* parse the parameter string */

        pToken = strtok_r(pStr,",",&pHolder);

        if (pToken != NULL)
            {
            pRamBase = (char *)strtoul (pToken, NULL, 16);
    
            pToken = strtok_r(NULL,":",&pHolder); 

            if (pToken != NULL)
                ramSize = strtoul (pToken, NULL, 16);
            }
        free(pTmp);
        }

    if (pRamBase == NULL || ramSize == 0)
	{
        /* no Random Access Memory is specified, use default */

        pRamBase = nvRamData;
        ramSize = 0x10000; 
        }

    /* each ruleList will have equal amount space. i.e. ramSize/5 */

    listSize = ramSize/NUM_OF_RULELISTS;
    return OK;
    }