📄 usrappinit.c
字号:
/* usrAppInit.c - stub application initialization routine */
/* Copyright 1984-1998 Wind River Systems, Inc. */
/*
modification history
--------------------
01a,02jun98,ms written
*/
/*
DESCRIPTION
Initialize user application code.
*/
/******************************************************************************
*
* usrAppInit - initialize the users application
*/
#include <stdio.h>
#include "vxWorks.h"
#include "sockLib.h"
#include "inetLib.h"
#include "udpip.h"
void udp_send(UCHAR *outbuf, UINT len,UINT my_udpport, UINT send_udpport);
void udp_rcve(UCHAR * inbuf, UINT len);
void ip_send(UCHAR * outbuf, UINT len,ULONG my_ipaddr,ULONG send_ipaddr);
void ip_rcve(UCHAR * inbuf);
UINT cksum(UCHAR *check,UINT length);
UINT source_udpport;
ULONG source_ipaddr;
UINT aim_udpport;
ULONG aim_ipaddr;
UINT output_len;
UINT input_len;
UCHAR *input_buf;
UCHAR *output_buf;
char testme;
void usrAppInit (void)
{
#ifdef USER_APPL_INIT
USER_APPL_INIT; /* for backwards compatibility */
#endif
/* add application specific code here */
printf("Hello world.\n");
}
/*
//---------------------------------------------------------------------------
//------------------------------------------------------------------------
// This handles outgoing UDP messages
// See "TCP/IP Illustrated, Volume 1" Sect 11.1 - 11.3
//------------------------------------------------------------------------
*/
void udp_send(UCHAR *outbuf, UINT len,UINT my_udpport, UINT send_udpport)
{
ULONG sum;
UINT result;
UDP_HEADER udp;
UDP_HEADER *udper;
udper = &udp;
udper->source_port = my_udpport;
udper->dest_port = send_udpport;
udper->length = 8 + len;
udper->checksum =1;
/* sum = (ULONG)cksum(outbuf + 26, 8 + udper->length);
// Add in the rest of pseudoheader which is
// zero, protocol id, and UDP length
sum += (ULONG)0x0011;
sum += (ULONG)udper->length;
// In case there was a carry, add it back around
result = (UINT)(sum + (sum >> 16));
udper->checksum = ~result;
*/
output_buf=outbuf+8;
memcpy(output_buf,(const void* )udper, 8);
output_len=len+8;
ip_send(output_buf, output_len, source_ipaddr, aim_ipaddr );
}
/*
//------------------------------------------------------------------------
// This handles incoming UDP messages
// See "TCP/IP Illustrated, Volume 1" Sect 11.1 - 11.3
//------------------------------------------------------------------------
*/
void udp_rcve(UCHAR * inbuf, UINT len)
{
UINT result;
UDP_HEADER * udp;
ULONG sum;
memcpy((void *)udp, (void *)inbuf, 8) ;
if (len <udp->length) return;
if (udp->checksum == 0)
{
return ;
}
else
{
sum = (ULONG)cksum(inbuf + 26, 8 + udp->length);
sum += (ULONG)0x0011;
sum += (ULONG)udp->length;
result = (UINT)(sum + (sum >> 16));
if (result != 0xFFFF)
{
return;
}
input_buf=input_buf-8;
input_len=udp->length-8;
}
}
void ip_send(UCHAR * outbuf, UINT len ,ULONG my_ipaddr,ULONG send_ipaddr)
{
IP_HEADER IPHEAD;
IP_HEADER * ip;
ip=&IPHEAD;
/**** UCHAR xdata * hwaddr;*/
ip->ver_len = 0x45;
ip->type_of_service = 0;
ip->total_length = 20 + len;
ip->identifier =1 ;
ip->fragment_info = 0;
ip->time_to_live = 32;
ip->protocol_id = UDP_TYPE;
ip->header_cksum = 0;
ip->source_ipaddr = source_ipaddr;
ip->dest_ipaddr = aim_ipaddr;
ip->header_cksum = ~cksum(outbuf + 14, 20);
output_buf=outbuf+20;
output_len=len+20;
memcpy((void *)output_buf, (void *)ip , 20) ;
/* if (send_ipaddr == NULL)
{
wait.buf = outbuf;
wait.ipaddr = ip->dest_ipaddr;
wait.proto_id = proto_id;
wait.len = len;
wait.timer = ARP_TIMEOUT;
return;
}
*/
return;
}
/*
//------------------------------------------------------------------------
// This handles incoming IP datagrams from the Ethernet layer
// See "TCP/IP Illustrated, Volume 1" Sect 3.2
//------------------------------------------------------------------------
*/
void ip_rcve(UCHAR * inbuf)
{
IP_HEADER iphead;
IP_HEADER * ip;
UINT header_len, payload_len;
ip=&iphead;
memcpy ((void *)ip,(void *)inbuf,20);
if (ip->dest_ipaddr != source_ipaddr) return;
header_len = 4 * (0x0F & ip->ver_len);
payload_len = ip->total_length - header_len;
if (cksum(inbuf + 14, header_len) != 0xFFFF)
{
return;
}
if ((ip->ver_len >> 4) != 0x04) {
return;
}
if ((ip->fragment_info & 0x3FFF) != 0)
{
return;
}
/* if (header_len > 20)
{
// Use memmove because of overlap
memmove(inbuf + 34, inbuf + 14 + header_len, payload_len);
// Adjust info to reflect the move
header_len = 20;
ip->ver_len = 0x45;
ip->total_length = 20 + payload_len;
}
*/
if(ip->protocol_id!=UDP_TYPE)
{ return;
}
input_buf=inbuf-20;
udp_rcve(input_buf, payload_len);
}
UINT cksum(UCHAR *check,UINT length)
{
LONG sum=0;
UINT i;
UINT *ptr;
ptr=(UINT *)check;
for (i=0;i<(length)/2;i++)
{
sum+=*ptr++;
}
if (length&0x01)
{
sum=sum+((*ptr)&0xff00);
}
sum=(sum&0xffff)+((sum>>16)&0xffff);
if(sum&0xffff0000)
{
sum++;
}
return ( (UINT)((sum)&0xffff));
}
void send_udp()
{
UCHAR Buffer[188*7];
UCHAR *mybuff;
UINT code;
UINT test;
mybuff=Buffer;
for( code=0;code<188*7;code++)
{ Buffer[code]='a';
}
output_len=strlen(Buffer);
output_buf=mybuff+output_len ;
aim_udpport=21;
source_udpport=22;
aim_ipaddr=2000 ;
source_ipaddr=1000 ;
for( test =0; test < 1; test++)
{ udp_send(output_buf, output_len,source_udpport,aim_udpport) ;
}
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -