📄 host-multicast.c
字号:
// 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 );
}
// 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;
// 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");
}
//
// UDP Server task
//
static void udp_test()
{
SOCKET recv = INVALID_SOCKET; // Receiver socket
SOCKET send = INVALID_SOCKET; // Sender socket.
struct sockaddr_in sin1; // Receiver socket address structure
struct sockaddr_in sout1; // Sender socket address structure
struct timeval timeout; // Timeout struct for select
int tmp,cnt;
int sentCnt;
int reply = 0;
fd_set ibits;
while(!IPAddrCnt)
TSK_sleep(100);
// Join the Mulicast Group for MulticastAddr on interface "1"
if (IGMPJoinHostGroup( inet_addr(MulticastAddr), 1 ))
printf("Successfully joined the multicast group %s\n", MulticastAddr);
// Allocate the file environment for this task
fdOpenSession( TaskSelf() );
// Create the multicast receive socket
recv = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if( recv == INVALID_SOCKET )
goto leave;
// Create the multicast receive socket
send = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if( recv == INVALID_SOCKET )
goto leave;
// Receive socket: set Port = 5001, IP address = ANY
bzero( &sin1, sizeof(struct sockaddr_in) );
sin1.sin_family = AF_INET;
sin1.sin_len = sizeof( sin1 );
sin1.sin_port = htons(5001);
// Send socket: set Port = 5000, IP address = ANY
bzero( &sout1, sizeof(struct sockaddr_in) );
sout1.sin_family = AF_INET;
sout1.sin_len = sizeof( sout1 );
sout1.sin_port = htons(5000);
// Bind receive socket
if ( bind( recv, (PSA) &sin1, sizeof(sin1) ) < 0 )
{
printf("Receive: %d",fdError());
goto leave;
}
// Bind send socket
if ( bind( send, (PSA) &sout1, sizeof(sout1) ) < 0 )
{
printf("Send: %d",fdError());
goto leave;
}
// Set timeout to 1 seconds
timeout.tv_sec = 1;
timeout.tv_usec = 0;
strcpy( sendBuffer, "Are you there?" );
for(;;) {
sout1.sin_addr.s_addr = inet_addr(MulticastAddr);
sout1.sin_port = htons(5000);
// Send to multicast group/port every 5 sec until successful
do {
TSK_sleep(5000);
sentCnt = sendto( send, sendBuffer, 15, 0, &sout1, sizeof(sout1) );
} while (sentCnt < 0);
printf (" Message sent to multicast group \n");
// Check for network data on port 5001
FD_ZERO(&ibits);
FD_SET(recv, &ibits);
tmp = fdSelect( 1, &ibits, 0, 0, &timeout );
// See if there is data on the UDP socket...
if( FD_ISSET(recv, &ibits) ) {
// Read the data
tmp = sizeof( sin1 );
cnt = (int)recvfrom( recv, recvBuffer, 1500, 0, &sin1, &tmp );
// See if the message was sent back
if( cnt==11 && !strcmp( (char *)recvBuffer, "I am here!" ) ) {
printf( "Reply #%d from: %d.%d.%d.%d\n", ++reply,
sin1.sin_addr.s_addr & 0xFF,
(sin1.sin_addr.s_addr >> 8) & 0xFF,
(sin1.sin_addr.s_addr >> 16) & 0xFF,
sin1.sin_addr.s_addr >> 24 );
}
}
}
leave:
// We only get here on a fatal error - close the sockets
if( recv != INVALID_SOCKET )
fdClose( recv );
if( send != INVALID_SOCKET )
fdClose( send );
printf("Fatal Error\n");
// This task is killed by the system - here, we block
TaskBlock( TaskSelf() );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -