📄 client.c
字号:
//
// NetworkOpen
//
// This function is called after the configuration has booted
//
static void NetworkOpen()
{
// Create our local servers
hEcho = TaskCreate( echosrv, "EchoSrv", OS_TASKPRINORM, 0x1400, 0, 0, 0 );
hData = TaskCreate( datasrv, "DataSrv", OS_TASKPRINORM, 0x1400, 0, 0, 0 );
hNull = TaskCreate( nullsrv, "NullSrv", OS_TASKPRINORM, 0x1400, 0, 0, 0 );
hOob = TaskCreate( oobsrv, "OobSrv", OS_TASKPRINORM, 0x1000, 0, 0, 0 );
}
//
// NetworkClose
//
// This function is called when the network is shutting down,
// or when it no longer has any IP addresses assigned to it.
//
static void NetworkClose()
{
fdCloseSession( hOob );
fdCloseSession( hNull );
fdCloseSession( hData );
fdCloseSession( hEcho );
// Kill any active console
ConsoleClose();
// If we opened NETCTRL as NC_PRIORITY_HIGH, we can't
// kill our task threads until we've given them the
// opportunity to shutdown. We do this by manually
// setting our task priority to NC_PRIORITY_LOW.
TaskSetPri( TaskSelf(), NC_PRIORITY_LOW );
TaskDestroy( hOob );
TaskDestroy( hNull );
TaskDestroy( hData );
TaskDestroy( hEcho );
}
#else
//
// System Task Code [ Server Daemon Servers ]
//
static HANDLE hEcho=0,hEchoUdp=0,hData=0,hNull=0,hOob=0;
//
// NetworkOpen
//
// This function is called after the configuration has booted
//
static void NetworkOpen()
{
// Create our local servers
hEcho = DaemonNew( SOCK_STREAMNC, 0, 7, dtask_tcp_echo,
OS_TASKPRINORM, OS_TASKSTKNORM, 0, 3 );
hEchoUdp = DaemonNew( SOCK_DGRAM, 0, 7, dtask_udp_echo,
OS_TASKPRINORM, OS_TASKSTKNORM, 0, 1 );
hData = DaemonNew( SOCK_STREAM, 0, 1000, dtask_tcp_datasrv,
OS_TASKPRINORM, OS_TASKSTKNORM, 0, 3 );
hNull = DaemonNew( SOCK_STREAMNC, 0, 1001, dtask_tcp_nullsrv,
OS_TASKPRINORM, OS_TASKSTKNORM, 0, 3 );
hOob = DaemonNew( SOCK_STREAMNC, 0, 999, dtask_tcp_oobsrv,
OS_TASKPRINORM, OS_TASKSTKNORM, 0, 3 );
}
//
// NetworkClose
//
// This function is called when the network is shutting down,
// or when it no longer has any IP addresses assigned to it.
//
static void NetworkClose()
{
DaemonFree( hOob );
DaemonFree( hNull );
DaemonFree( hData );
DaemonFree( hEchoUdp );
DaemonFree( hEcho );
// Kill any active console
ConsoleClose();
}
#endif
//
// NetworkIPAddr
//
// This function is called whenever an IP address binding is
// added or removed from the system.
//
static void NetworkIPAddr( IPN IPAddr, uint IfIdx, uint fAdd )
{
static uint fAddGroups = 0;
IPN IPTmp;
if( fAdd )
printf("Network Added: ");
else
printf("Network Removed: ");
// Print a message
IPTmp = ntohl( IPAddr );
printf("If-%d:%d.%d.%d.%d\n", IfIdx,
(UINT8)(IPTmp>>24)&0xFF, (UINT8)(IPTmp>>16)&0xFF,
(UINT8)(IPTmp>>8)&0xFF, (UINT8)IPTmp&0xFF );
// This is a good time to join any multicast group we require
if( fAdd && !fAddGroups )
{
fAddGroups = 1;
// IGMPJoinHostGroup( inet_addr("224.1.2.3"), IfIdx );
}
}
//
// DHCP_reset()
//
// Code to reset DHCP client by removing it from the active config,
// and then reinstalling it.
//
// Called with:
// IfIdx set to the interface (1-n) that is using DHCP.
// fOwnTask set when called on a new task thread (via TaskCreate()).
//
void DHCP_reset( uint IfIdx, uint fOwnTask )
{
CI_SERVICE_DHCPC dhcpc;
HANDLE h;
int rc,tmp;
uint idx;
// If we were called from a newly created task thread, allow
// the entity that created us to complete
if( fOwnTask )
TaskSleep(500);
// Find DHCP on the supplied interface
for(idx=1; ; idx++)
{
// Find a DHCP entry
rc = CfgGetEntry( 0, CFGTAG_SERVICE, CFGITEM_SERVICE_DHCPCLIENT,
idx, &h );
if( rc != 1 )
goto RESET_EXIT;
// Get DHCP entry data
tmp = sizeof(dhcpc);
rc = CfgEntryGetData( h, &tmp, (UINT8 *)&dhcpc );
// If not the right entry, continue
if( (rc<=0) || dhcpc.cisargs.IfIdx != IfIdx )
{
CfgEntryDeRef(h);
h = 0;
continue;
}
// This is the entry we want!
// Remove the current DHCP service
CfgRemoveEntry( 0, h );
// Specify DHCP Service on specified IF
bzero( &dhcpc, sizeof(dhcpc) );
dhcpc.cisargs.Mode = CIS_FLG_IFIDXVALID;
dhcpc.cisargs.IfIdx = IfIdx;
dhcpc.cisargs.pCbSrv = &ServiceReport;
CfgAddEntry( 0, CFGTAG_SERVICE, CFGITEM_SERVICE_DHCPCLIENT, 0,
sizeof(dhcpc), (UINT8 *)&dhcpc, 0 );
break;
}
RESET_EXIT:
// If we are a function, return, otherwise, call TaskExit()
if( fOwnTask )
TaskExit();
}
void CheckDHCPOptions();
//
// Service Status Reports
//
// Here's a quick example of using service status updates
//
static char *TaskName[] = { "Telnet","HTTP","NAT","DHCPS","DHCPC","DNS" };
static char *ReportStr[] = { "","Running","Updated","Complete","Fault" };
static char *StatusStr[] = { "Disabled","Waiting","IPTerm","Failed","Enabled" };
static void ServiceReport( uint Item, uint Status, uint Report, HANDLE h )
{
printf( "Service Status: %-9s: %-9s: %-9s: %03d\n",
TaskName[Item-1], StatusStr[Status],
ReportStr[Report/256], Report&0xFF );
//
// Example of adding to the DHCP configuration space
//
// When using the DHCP client, the client has full control over access
// to the first 256 entries in the CFGTAG_SYSINFO space.
//
// Note that the DHCP client will erase all CFGTAG_SYSINFO tags except
// CFGITEM_DHCP_HOSTNAME. If the application needs to keep manual
// entries in the DHCP tag range, then the code to maintain them should
// be placed here.
//
// Here, we want to manually add a DNS server to the configuration, but
// we can only do it once DHCP has finished its programming.
//
if( Item == CFGITEM_SERVICE_DHCPCLIENT &&
Status == CIS_SRV_STATUS_ENABLED &&
(Report == (NETTOOLS_STAT_RUNNING|DHCPCODE_IPADD) ||
Report == (NETTOOLS_STAT_RUNNING|DHCPCODE_IPRENEW)) )
{
IPN IPTmp;
// Manually add the DNS server when specified
IPTmp = inet_addr(DNSServer);
if( IPTmp )
CfgAddEntry( 0, CFGTAG_SYSINFO, CFGITEM_DHCP_DOMAINNAMESERVER,
0, sizeof(IPTmp), (UINT8 *)&IPTmp, 0 );
#if 0
// We can now check on what the DHCP server supplied in
// response to our DHCP option tags.
CheckDHCPOptions();
#endif
}
// Reset DHCP client service on failure
if( Item==CFGITEM_SERVICE_DHCPCLIENT && (Report&~0xFF)==NETTOOLS_STAT_FAULT )
{
CI_SERVICE_DHCPC dhcpc;
int tmp;
// Get DHCP entry data (for index to pass to DHCP_reset).
tmp = sizeof(dhcpc);
CfgEntryGetData( h, &tmp, (UINT8 *)&dhcpc );
// Create the task to reset DHCP on its designated IF
// We must use TaskCreate instead of just calling the function as
// we are in a callback function.
TaskCreate( DHCP_reset, "DHCPreset", OS_TASKPRINORM, 0x1000,
dhcpc.cisargs.IfIdx, 1, 0 );
}
}
void CheckDHCPOptions()
{
char IPString[16];
IPN IPAddr;
int i, rc;
// Now scan for DHCPOPT_SERVER_IDENTIFIER via configuration
printf("\nDHCP Server ID:\n");
for(i=1;;i++)
{
// Try and get a DNS server
rc = CfgGetImmediate( 0, CFGTAG_SYSINFO, DHCPOPT_SERVER_IDENTIFIER,
i, 4, (UINT8 *)&IPAddr );
if( rc != 4 )
break;
// We got something
// Convert IP to a string:
NtIPN2Str( IPAddr, IPString );
printf("DHCP Server %d = '%s'\n", i, IPString);
}
if( i==1 )
printf("None\n\n");
else
printf("\n");
// Now scan for DHCPOPT_ROUTER via the configuration
printf("Router Information:\n");
for(i=1;;i++)
{
// Try and get a DNS server
rc = CfgGetImmediate( 0, CFGTAG_SYSINFO, DHCPOPT_ROUTER,
i, 4, (UINT8 *)&IPAddr );
if( rc != 4 )
break;
// We got something
// Convert IP to a string:
NtIPN2Str( IPAddr, IPString );
printf("Router %d = '%s'\n", i, IPString);
}
if( i==1 )
printf("None\n\n");
else
printf("\n");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -