📄 netsrv.c
字号:
/*
* Copyright 2006 by Texas Instruments Incorporated.
* All rights reserved. Property of Texas Instruments Incorporated.
* Restricted rights to use, duplicate or disclose this code are
* granted through contract.
*
* @(#) TCP/IP_Network_Developers_Kit 1.91.00.08 08-22-2006 (ndk-a08)
*/
//--------------------------------------------------------------------------
// Net Configuration Service Provider
//--------------------------------------------------------------------------
// NETSRV.C
//
// This module contains the service provider functions used to
// implement the CONFIG system define in NETCFG, created by
// NETCTRL and used by NETTOOLS.
//
// Author: Michael A. Denio
// Copyright 2000 by Texas Instruments Inc.
//--------------------------------------------------------------------------
#include <netmain.h>
#include <_stack.h>
// Number of system IP Addresses
static int fBooting = 0;
// Copies of the Stack and OS config structures
static IPCONFIG ipcfgcopy;
static OSENVCFG oscfgcopy;
// NETCFG Configuration Tag Service Providers
static int SPService( HANDLE, uint, uint, uint, HANDLE );
static int SPIpNet( HANDLE, uint, uint, uint, HANDLE );
static int SPRoute( HANDLE, uint, uint, uint, HANDLE );
static int SPConfig( HANDLE, uint, uint, uint, HANDLE );
// Functions to spawn and kill NETTOOLS services
static void ServiceCallback( HANDLE hCfgEntry, uint Status );
static void ServiceSpawn( HANDLE hCfg, HANDLE hCfgEntry );
static void ServiceKill( HANDLE hCfg, HANDLE hCfgEntry );
static void ServiceScan( HANDLE hCfg );
// Configuration Initialization/Shutdown Order
static uint OpenOrder[CFGTAG_MAX] =
{ CFGTAG_OS, CFGTAG_IP, CFGTAG_SERVICE,
CFGTAG_IPNET, CFGTAG_ROUTE, CFGTAG_CLIENT,
CFGTAG_SYSINFO, CFGTAG_ACCT, 0, 0, 0, 0, 0, 0, 0, 0 };
static uint CloseOrder[CFGTAG_MAX] =
{ CFGTAG_SERVICE, CFGTAG_ROUTE, CFGTAG_IPNET,
CFGTAG_IP, CFGTAG_OS, CFGTAG_CLIENT,
CFGTAG_SYSINFO, CFGTAG_ACCT, 0, 0, 0, 0, 0, 0, 0, 0 };
//--------------------------------------------------------------------------
// NS_PreBoot()
//
// This function must be called before any system initialization is
// performed.
//--------------------------------------------------------------------------
void NS_PreBoot()
{
// Our config service provider needs original copies of the
// configuration arrays.
ipcfgcopy = _ipcfg;
oscfgcopy = _oscfg;
}
//--------------------------------------------------------------------------
// NS_BootTask()
//
// This function is called as a task created by NETCTRL
// Note the call to TaskExit() at the end of the function.
//--------------------------------------------------------------------------
void NS_BootTask( HANDLE hCfg )
{
// Install the service provider callbacks
CfgSetService( hCfg, CFGTAG_OS, &SPConfig );
CfgSetService( hCfg, CFGTAG_IP, &SPConfig );
CfgSetService( hCfg, CFGTAG_SERVICE, &SPService );
CfgSetService( hCfg, CFGTAG_IPNET, &SPIpNet );
CfgSetService( hCfg, CFGTAG_ROUTE, &SPRoute );
// Set the configuration initialization order
CfgSetExecuteOrder( hCfg, CFGTAG_MAX, OpenOrder, CloseOrder );
// Enable the configuration
fBooting = 1;
CfgExecute( hCfg, 1 );
fBooting = 0;
// Now scan for loadable services
ServiceScan( hCfg );
// Inform NETCTRL of address count
// This also boots the user tasks
NC_BootComplete();
// Exit this task thread
TaskExit();
}
//--------------------------------------------------------------------------
// SPConfig() - CFGTAG_IP and CFGTAG_OS Service Provider
//
//--------------------------------------------------------------------------
static int SPConfig(HANDLE hCfg, uint Tag, uint Item, uint Op, HANDLE hCfgEntry)
{
uint *pi,*pdst,*pdef;
(void)hCfg;
// Get the information
if( CfgEntryInfo( hCfgEntry, 0, (UINT8 **)(&pi) ) < 0 )
return( -1 );
// Setup to handle IP or OS
if( Tag == CFGTAG_IP )
{
// Bound the value of Item
if( Item > CFGITEM_IP_MAX )
return( -1 );
pdst = (uint *)&_ipcfg;
pdef = (uint *)&ipcfgcopy;
}
else if( Tag == CFGTAG_OS )
{
// Bound the value of Item
if( Item > CFGITEM_OS_MAX )
return( -1 );
pdst = (uint *)&_oscfg;
pdef = (uint *)&oscfgcopy;
}
else
return( -1 );
// Verify Item
if( !Item )
return( -1 );
Item--;
// If this is an "add", add the entry
if( Op == CFGOP_ADD )
*(pdst+Item) = *pi;
// Else if "remove", restore the default
else if( Op == CFGOP_REMOVE )
*(pdst+Item) = *(pdef+Item);
// Return success
return(1);
}
//--------------------------------------------------------------------------
// SPService() - CFGTAG_SERVICE Service Provider
//
//--------------------------------------------------------------------------
static int SPService(HANDLE hCfg, uint Tag, uint Item, uint Op, HANDLE hCfgEntry)
{
CISARGS *pa;
(void)Tag;
// If this is an "add", add the entry
if( Op == CFGOP_ADD )
{
// If we can't get the info, just return
if( CfgEntryInfo( hCfgEntry, 0, (UINT8 **)(&pa) ) < 0 )
return(-1);
// Set status to "waiting"
pa->Status = CIS_SRV_STATUS_WAIT;
pa->hService = 0;
pa->ReportCode = 0;
// Keep a local copy of the Item value
pa->Item = Item;
// Most services are have qualification requirements before
// they can be invoked. Since these qualifications must be
// reviewed on every address add and removal, we don't
// attempt to spawn the service here.
//
// An exception to this is the DHCP client service, which
// in an attempt to speed up the boot process, we'll launch
// ASAP.
//
// We don't invoke other services here. We will rescan the
// service table if we're not booting. This will invoke
// the new service if its requirements are met.
if( Item == CFGITEM_SERVICE_DHCPCLIENT )
ServiceSpawn( hCfg, hCfgEntry );
else if( !fBooting )
ServiceScan( hCfg );
}
// Else if this is a "remove", remove the entry
else if( Op == CFGOP_REMOVE )
{
// Not only can tasks be removed here, but they can also
// be removed if they loose their IP address. This is
// done is ServiceScan(). In order to make this a little
// cleaner, we use a single "kill" routine.
// Remove the service
ServiceKill( hCfg, hCfgEntry );
}
// Return success
return(1);
}
//--------------------------------------------------------------------------
// SPIpNet() - CFGTAG_IPNET Service Provider
//
//--------------------------------------------------------------------------
static int SPIpNet(HANDLE hCfg, uint Tag, uint Item, uint Op, HANDLE hCfgEntry)
{
CI_IPNET *pi;
HANDLE hIF;
(void)Tag;
// Get the information
if( CfgEntryInfo( hCfgEntry, 0, (UINT8 **)(&pi) ) < 0 )
return( -1 );
// If this is an "add", add the entry
if( Op == CFGOP_ADD )
{
// Get the interface handle
llEnter();
hIF = IFIndexGetHandle(Item);
llExit();
if( !hIF )
return(-1);
// Add the newtwork
pi->hBind = NtAddNetwork( hIF, pi->IPAddr, pi->IPMask );
// If network not added, return error
if( !pi->hBind )
return(-1);
// Notify NetCtrl
NC_IPUpdate( pi->IPAddr, Item, 1 );
}
// Else if this is a "remove", remove the entry
else if( Op == CFGOP_REMOVE )
{
// If no network, return "pass"
if( !pi->hBind )
return(0);
// Remove the network
NtRemoveNetwork( pi->hBind );
pi->hBind = 0;
// Notify NetCtrl
NC_IPUpdate( pi->IPAddr, Item, 0 );
}
// Perform service maintenance when not in boot process
if( !fBooting )
ServiceScan( hCfg );
// Return success
return(1);
}
//--------------------------------------------------------------------------
// SPRoute() - CFGTAG_ROUTE Service Provider
//
//--------------------------------------------------------------------------
static int SPRoute(HANDLE hCfg, uint Tag, uint Item, uint Op, HANDLE hCfgEntry)
{
CI_ROUTE *pr;
(void)hCfg;
(void)Tag;
(void)Item;
// Get the information
if( CfgEntryInfo( hCfgEntry, 0, (UINT8 **)(&pr) ) < 0 )
return( -1 );
// If this is an "add", add the entry
if( Op == CFGOP_ADD )
{
// Start calling STACK functions
llEnter();
// Create the route and make it STATIC
pr->hRoute = RtCreate( FLG_RTF_REPORT, FLG_RTE_GATEWAY,
pr->IPDestAddr & pr->IPDestMask,
pr->IPDestMask, 0, pr->IPGateAddr, 0 );
// Stop calling STACK functions
llExit();
// If route not added, return error
if( !pr->hRoute )
return(-1);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -