📄 udp.c
字号:
//-----------------------------------------------------------------------------
// Net UDP.C
//
// This module handles UDP messages
// Refer to RFC 768, 1122
// Also RFC 862 echo, RFC 867 daytime, and RFC 868 time
//-----------------------------------------------------------------------------
#include <string.h>
#include <stdlib.h>
#include <reg89C58.h>
#include <stdio.h>
#include "net.h"
#include "ip.h"
#include "cksum.h"
#include "udp.h"
#include "24c04.h"
extern UCHAR data netsdbuf[10];
extern UCHAR idata debug;
extern ULONG data my_ipaddr;
extern bit udpst;
extern bit oneudpst;
extern bit twoudpst;
extern bit threeudpst;
extern bit fourudpst;
extern bit fiveudpst;
extern UCHAR data sdbuf[8];
extern bit RSS;
UINT xdata sender_udpport;
static ULONG xdata sender_ipaddr;
extern char xdata text[];
void SD_RS(UCHAR x);
void fu_key(UCHAR kk);
void delay(UINT i);
//extern UCHAR wr[3];
//extern UCHAR rd[3];
//extern UCHAR data temp0[5];
//extern UCHAR data temp1[5];
UCHAR xdata aabuf[10]={0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x40};
STORECODE xdata * wwww;
//------------------------------------------------------------------------
// UDP Echo service - see RFC 862
// This simply echos what it received back to the sender
//------------------------------------------------------------------------
void udp_echo_service(UCHAR xdata * inbuf, UINT len)
{
if (debug)
{
// printf("ECHO: Nr chars = ");
memset(text, 0, 10);
itoa(len, text, 10);
//printf(text);
//printf("\n");
}
udp_send(inbuf, ECHO_PORT, len);
}
//------------------------------------------------------------------------
// This handles outgoing UDP messages
// See "TCP/IP Illustrated, Volume 1" Sect 11.1 - 11.3
//------------------------------------------------------------------------
void udp_send(UCHAR xdata * inbuf, UINT port, UINT len)
{
ULONG idata sum;
UINT idata result;
UCHAR xdata * outbuf;
UDP_HEADER xdata * udp;
IP_HEADER xdata * ip;
// Allocate memory for entire outgoing message including
// eth & IP headers. Total ethernet message length is:
// 14 byte eth header + 20 byte IP header + 8 byte UDP header
// + length of this data
outbuf = (UCHAR xdata *)malloc(42 + len);
if (outbuf == NULL)
{
// if (debug) printf("UDP: Oops, out of memory\n");
return;
}
udp = (UDP_HEADER xdata *)(outbuf + 34);
ip = (IP_HEADER xdata *)(outbuf + 14);
// Direct message back to the senders port.
udp->dest_port = sender_udpport;
udp->source_port = port;
udp->length = 8 + len;
udp->checksum = 0;
// Fill in data
// Important do not free receive buffer prior to this
memcpy(&udp->msg_data, (inbuf + 42), len);
// Compute checksum including 12 bytes of pseudoheader
// Must pre-fill 2 items in outbuf to do this
// Direct message back to senders ip address
ip->dest_ipaddr = sender_ipaddr;
ip->source_ipaddr = my_ipaddr;
// Sum source_ipaddr, dest_ipaddr, and entire UDP message
sum = (ULONG)cksum(outbuf + 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));
udp->checksum = ~result;
// if (debug) printf("UDP: Sending msg to IP layer\n");
ip_send(outbuf, sender_ipaddr, UDP_TYPE, udp->length);
}
void udp_qhysend(UCHAR data * sendbuf, UINT hwsource_port,UINT hwremote_port,ULONG hwremote_ipadd, UINT len)
{
ULONG idata sum;
UINT idata result;
UCHAR xdata * outbuf;
UDP_HEADER xdata * udp;
IP_HEADER xdata * ip;
// Allocate memory for entire outgoing message including
// eth & IP headers. Total ethernet message length is:
// 14 byte eth header + 20 byte IP header + 8 byte UDP header
// + length of this data
outbuf = (UCHAR xdata *)malloc(42 + len);
if (outbuf == NULL)
{ return;
}
udp = (UDP_HEADER xdata *)(outbuf + 34);
ip = (IP_HEADER xdata *)(outbuf + 14);
// Direct message back to the senders port.
udp->dest_port = hwremote_port;
udp->source_port = hwsource_port;
udp->length = 8 + len;
udp->checksum = 0;
// Fill in data
// Important do not free receive buffer prior to this
memcpy(&udp->msg_data, sendbuf, len);
// Compute checksum including 12 bytes of pseudoheader
// Must pre-fill 2 items in outbuf to do this
// Direct message back to senders ip address
ip->dest_ipaddr = hwremote_ipadd;
ip->source_ipaddr = my_ipaddr;
// Sum source_ipaddr, dest_ipaddr, and entire UDP message
sum = (ULONG)cksum(outbuf + 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));
udp->checksum = ~result;
// if (debug) printf("UDP: Sending msg to IP layer\n");
// ip_send(outbuf, sender_ipaddr, UDP_TYPE, udp->length);
ip_send(outbuf, ip->dest_ipaddr, UDP_TYPE, udp->length);
}
void sysconfig_udp_send(UINT port, UINT len)
{
//the code is eprom read data
STORECODE xdata * eistore;
UCHAR xdata enet_dcode[28];
// eistore=(STORECODE data *) enet_dcode;
// the code is eprom read complite
ULONG idata sum;
UINT idata result;
UCHAR xdata * outbuf;
UDP_HEADER xdata * udp;
IP_HEADER xdata * ip;
// Allocate memory for entire outgoing message including
// eth & IP headers. Total ethernet message length is:
// 14 byte eth header + 20 byte IP header + 8 byte UDP header
// + length of this data
read_temp(0x00,28,enet_dcode);
eistore=(STORECODE xdata *) enet_dcode;
outbuf = (UCHAR xdata *)malloc(42 + len);
if (outbuf == NULL)
{
// if (debug) printf("UDP: Oops, out of memory\n");
return;
}
udp = (UDP_HEADER xdata *)(outbuf + 34);
ip = (IP_HEADER xdata *)(outbuf + 14);
// Direct message back to the senders port.
udp->dest_port = sender_udpport;
udp->source_port = port;
udp->length = 8 + len;
udp->checksum = 0;
// Fill in data
// Important do not free receive buffer prior to this
memcpy(&udp->msg_data, eistore, len);
// Compute checksum including 12 bytes of pseudoheader
// Must pre-fill 2 items in outbuf to do this
// Direct message back to senders ip address
ip->dest_ipaddr = sender_ipaddr;
ip->source_ipaddr = my_ipaddr;
// Sum source_ipaddr, dest_ipaddr, and entire UDP message
sum = (ULONG)cksum(outbuf + 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));
udp->checksum = ~result;
// if (debug) printf("UDP: Sending msg to IP layer\n");
ip_send(outbuf, sender_ipaddr, UDP_TYPE, udp->length);
}
void onesend_udp(UCHAR data * sendbuf, UINT port, UINT len)
{
// eistore=(STORECODE data *) enet_dcode;
// the code is eprom read complite
ULONG idata sum;
UINT idata result;
UCHAR xdata * outbuf;
UDP_HEADER xdata * udp;
IP_HEADER xdata * ip;
outbuf = (UCHAR xdata *)malloc(42 + len);
if (outbuf == NULL)
{
// if (debug) printf("UDP: Oops, out of memory\n");
return;
}
udp = (UDP_HEADER xdata *)(outbuf + 34);
ip = (IP_HEADER xdata *)(outbuf + 14);
// Direct message back to the senders port.
udp->dest_port = sender_udpport;
udp->source_port = port;
udp->length = 8 + len;
udp->checksum = 0;
// Fill in data
// Important do not free receive buffer prior to this
memcpy(&udp->msg_data, sendbuf, len);
// Compute checksum including 12 bytes of pseudoheader
// Must pre-fill 2 items in outbuf to do this
// Direct message back to senders ip address
ip->dest_ipaddr = sender_ipaddr;
ip->source_ipaddr = my_ipaddr;
// Sum source_ipaddr, dest_ipaddr, and entire UDP message
sum = (ULONG)cksum(outbuf + 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));
udp->checksum = ~result;
// if (debug) printf("UDP: Sending msg to IP layer\n");
ip_send(outbuf, sender_ipaddr, UDP_TYPE, udp->length);
}
//------------------------------------------------------------------------
// This handles incoming UDP messages
// See "TCP/IP Illustrated, Volume 1" Sect 11.1 - 11.3
//------------------------------------------------------------------------
void udp_rcve(UCHAR xdata * inbuf, UINT len)
{ UCHAR idata i;
UINT idata result;
UDP_HEADER xdata * udp;
IP_HEADER xdata * ip;
ULONG idata sum;
UCHAR data QHYNET;
UCHAR data QHYNET2;
UCHAR xdata * mydata;
UCHAR xdata * recive_cdata;
UCHAR data wrtemp[16];
UCHAR data writetemp[28];
// Total of eth & IP headers = 34 bytes
udp = (UDP_HEADER xdata *)(inbuf + 34);
ip = (IP_HEADER xdata *)(inbuf + 14);
mydata=inbuf+42;
recive_cdata=inbuf+42;
// The IP length "len" should be the same as the redundant length
// udp->length. TCP/IP Illustrated, Vol 2, Sect 23.7 says to use the
// UDP length, unless IP length < UDP length, in which case the frame
// should be discarded.
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)
{
// if (debug) printf("UDP: Sender did not compute cksum\n");
}
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)
{
// if (debug) printf("UDP: Error, bad cksum\n");
return;
}
// if (debug) printf("UDP: Msg rcvd with good cksum\n");
}
// Capture sender's port number and ip_addr
// to send return message to
sender_udpport = udp->source_port;
sender_ipaddr = ip->source_ipaddr;
// See if any applications are on any ports
switch (udp->dest_port)
{
case ECHO_PORT:
// Pass it the payload length
udp_echo_service(inbuf, udp->length - 8);
break;
case ONE_PORT:
QHYNET=* mydata;
QHYNET2=* (mydata+1); //this is one client
if(QHYNET==0xff){ sdbuf[1]=QHYNET2;
fu_key(QHYNET2);RSS=1;
}
oneudpst=1;
break;
case TWO_PORT:
QHYNET=* mydata;
QHYNET2=* (mydata+1); //this is two client
if(QHYNET==0xff){ sdbuf[1]=QHYNET2;
fu_key(QHYNET2);RSS=1;
}
twoudpst=1;
break;
case THREE_PORT:
QHYNET=* mydata;
QHYNET2=* (mydata+1); //this is three client
if(QHYNET==0xff){ sdbuf[1]=QHYNET2;
fu_key(QHYNET2);RSS=1;
}
threeudpst=1;
break;
case FOUR_PORT:
QHYNET=* mydata;
QHYNET2=* (mydata+1); //this is four client
if(QHYNET==0xff){ sdbuf[1]=QHYNET2;
fu_key(QHYNET2);RSS=1;
}
fourudpst=1;
break;
case FIVE_PORT:
QHYNET=* mydata;
QHYNET2=* (mydata+1); //this is FIVE client
if(QHYNET==0xff){ sdbuf[1]=QHYNET2;
fu_key(QHYNET2);RSS=1;
}
fiveudpst=1;
break;
case scanip: //询部IP地址
sysconfig_udp_send(scanip, 28);
break;
case configip: //设置IP地址
memcpy(writetemp, recive_cdata, 28);
if(writetemp[27]==0x7f)
{
for(i=0;i<12;i++){wrtemp[i]=writetemp[16+i];}
write_temp(0X00,16,writetemp);
write_temp(0X10,12,wrtemp);
while(1);
}
break;
/*
case QHY_PORT:
QHYNET=* mydata;
QHYNET2=* (mydata+1); //this is key resolution
if(QHYNET==0xff){ sdbuf[1]=QHYNET2;
fu_key(QHYNET2);RSS=1;
}
udpst=1;
break;
*/
default:break;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -