?? 復件 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;
// UINT ip_ident;
// ip_ident = 0;
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;
// testme=(UCHAR)*(sendout_buf-9);
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 udphead;
UDP_HEADER * udp;
// * udp=&udphead;
ULONG sum;
memcpy((void *)udp, (void *)inbuf, 8) ;
if (len <udp->length) return;
// If the checksum is zero it means that the sender did not compute
// it and we should not try to check it.
if (udp->checksum == 0)
{
return ;// printf("UDP: Sender did not compute cksum\r");
}
else
{
// Compute UDP checksum including 12 byte pseudoheader
// Sum source_ipaddr, dest_ipaddr, and entire UDP message
sum = (ULONG)cksum(inbuf + 26, 8 + udp->length);
// Add in the rest of pseudoheader which is
// zero, protocol id, and UDP length
sum += (ULONG)0x0011;
sum += (ULONG)udp->length;
// In case there was a carry, add it back around
result = (UINT)(sum + (sum >> 16));
if (result != 0xFFFF)
{
return;
}
input_buf=input_buf-8;
input_len=udp->length-8;
}
}
//------------------------------------------------------------------------
// This handles outgoing IP datagrams. It adds the 20 byte IP header
// and checksum then forwards the IP datagram to the Ethernet layer
// for sending. See "TCP/IP Illustrated, Volume 1" Sect 3.2
//------------------------------------------------------------------------
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 = (IP_HEADER xdata *)(outbuf + 14);
ip->ver_len = 0x45; // IPv4 with 20 byte header
ip->type_of_service = 0;
ip->total_length = 20 + len;
ip->identifier =1 ;// ip_ident++; // sequential identifier
ip->fragment_info = 0; // not fragmented
ip->time_to_live = 32; // max hops
ip->protocol_id = UDP_TYPE; // type of payload
ip->header_cksum = 0;
ip->source_ipaddr = source_ipaddr;
// Outgoing IP address
ip->dest_ipaddr = aim_ipaddr;
// Compute and insert complement of checksum of ip header
// Outgoing ip header length is always 20 bytes
ip->header_cksum = ~cksum(outbuf + 14, 20);
// Use ARP to get hardware address to send this to
// hwaddr = arp_resolve(ip->dest_ipaddr);
//************************
output_buf=outbuf+20;
output_len=len+20;
// testme=*(sendout_buf-30);
memcpy((void *)output_buf, (void *)ip , 20) ;
// Null means that the ARP resolver did not find the IP address
// in its cache so had to send an ARP request
if (send_ipaddr == NULL)
{
// Fill in the destination information so ehrn the ARP response
// arrives we can identify it and know what to do when we get it
/* wait.buf = outbuf;
wait.ipaddr = ip->dest_ipaddr;
wait.proto_id = proto_id;
wait.len = len;
wait.timer = ARP_TIMEOUT;
*/
return;
}
// printf("sender_ipaddr is NULL\n");
// eth_send(outbuf, ipaddr, IP_PACKET, 20 + len);
// testme=len+20;
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;
// *ip=&iphead;
UINT header_len, payload_len;
memcpy ((void *)ip,(void *)inbuf,20);
// ip = (IP_HEADER xdata *)(inbuf + 14);
// Make sure it is addressed to my IP address
if (ip->dest_ipaddr != source_ipaddr) return;
// Validate checksum of ip header
header_len = 4 * (0x0F & ip->ver_len);
payload_len = ip->total_length - header_len;
if (cksum(inbuf + 14, header_len) != 0xFFFF)
{
return;
}
// Make sure incoming message is IP version 4
if ((ip->ver_len >> 4) != 0x04) {
return;
}
if ((ip->fragment_info & 0x3FFF) != 0)
{
return;
}
input_buf=inbuf-20;
/* 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;
}
*/
// for values for various protocols
if (ip->protocol_id!=UDP_TYPE)
{ return;
}
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);//高16位和低16位相加
if(sum&0xffff0000)
{//表示有進位
sum++;
}
return ( (UINT)((sum)&0xffff));
// return ( (UINT)(~((sum)&0xffff)));
}
//---------------------------------------------------------------------------
void send_udp()
{
UCHAR Buffer[188*7]; //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 + -