📄 datagram.c
字号:
}
/* 用IP地址,子网掩码和网关的宏定义初始化本地节点
复位并初始化以太网控制器
** Return driver type, 0 if error */
WORD read_netconfig(NODE *np)
{
WORD dtype=0;
np->ip=atoip(LOCALIP); /* Get IP address */
if (!(dtype = open_net(NETCFG))) /* Open net driver */
Uart_Printf("Can't open net driver '%s'\n", NETCFG);
else
{ /* Save ether address */
memcpy((BYTE *)(np->mac), ether_addr(dtype), MACLEN);
np->dtype = dtype; /* ..and driver type */
np->mask = atoip(MASKCFG);/* Get netmask */
np->gate = atoip(GATECFG);/* Get gateway IP addr */
}
return(dtype);
}
/* Return ptr to local node 'n' (n=0 for first), return 0 if doesn't exist
** Used by IP functions to get my netmask & gateway addresses */
NODE *locnode_n(int n)
{
return(n==0 ? &locnode : 0);
}
/* Check for incoming packets, send response if required
** Return state-change value if ARP response or datagram received */
int do_receive(GENFRAME *gfp)
{
NODE node;
ARPKT *arp;
IPKT *ip;
ICMPKT *icmp;
int rxlen, txlen, len, ret=0;
//BYTE i,*p;
if ((rxlen=get_frame(gfp)) > 0) /* Any incoming frames? */
{
ip = getframe_datap(gfp);
if (is_arp(gfp, rxlen))
{ /* ARP response? */
//arp = getframe_datap(gfp);
arp = &arpkt;//////采用全局变量传递指针
if (arp->op==ARPREQ && arp->dip==locnode.ip)
{ /* ARP request? */
node.ip = arp->sip; /* Make ARP response */
memcpy((BYTE *)(node.mac), (BYTE *)(arp->smac), MACLEN);
txlen = make_arp(gfp, &locnode, &node, ARPRESP);
put_frame(gfp, txlen); /* Send packet */
}
if (arp->op==ARPRESP && arp->dip==locnode.ip)
{ /* ARP response? */
memcpy((BYTE *)(remnode.mac), (BYTE *)(arp->smac), MACLEN);
ret = ARP_RX;
}
}
else if ((rxlen=is_ip(gfp, rxlen))!=0)// && /* IP datagram? */
{
ip=&ipkt;/////////用全局变量传递指针
if((ip->i.dip==locnode.ip) ||( ip->i.dip==BCASTIP))
{
getip_srce(gfp, &node);
if ((len=is_icmp(ip, rxlen))!=0) /* ICMP? */
{
icmp = (ICMPKT *)ip;
if (icmp->c.type == ICREQ) /* Echo request? */
{
len = (WORD)maxi(len, 0); /* Make response */
txlen = make_icmp(gfp, &locnode, &node, ICREP,
icmp->c.code, (WORD)len);
put_frame(gfp, txlen); /* Send packet */
}
else if (icmp->c.type == ICUNREACH)
Uart_Printf("ICMP: destination unreachable\n");
}
else if ((len=is_udp(ip, rxlen))!=0) /* UDP? */
{
ret = udp_receive(gfp, maxi(len, 0));
}
}
}
}
return(ret);
}
/* Receive a UDP datagram: return non-0 if client state-change */
int udp_receive(GENFRAME *gfp, int len)
{
UDPKT *udp;
int ret=0;
NODE loc, rem;
udp = getframe_datap(gfp);
swap_udp(udp);
getudp_srce(gfp, &rem); /* Get srce & dest nodes */
getudp_locdest(gfp, &loc);
if (loc.port == locnode.port) /* Client response */
{
Uart_Printf("\n");
disp_data(udp->data, len); /* Display data.. */
ret = CLIENT_DONE; /* ..and exit */
}
else if (loc.port == ECHOPORT) /* Echo req: resend data */
udp_transmit(gfp, &loc, &rem, (BYTE *)(udp->data), len);
else /* Unreachable: send ICMP */
{
swap_udp(udp);
put_frame(gfp, icmp_unreach(gfp, &loc, &rem, UNREACH_PORT));
}
return(ret);
}
/* Send a UDP datagram, given destination node, data and length */
void udp_transmit(GENFRAME *gfp, NODE *sp, NODE *dp, void *dat, int len)
{
UDPKT *udp;
int templen;
udp = getframe_datap(gfp);
memmove((BYTE *)(udp->data), dat, len);
templen=maxi(len,0);
templen=make_udp(gfp, sp, dp, (WORD)templen);
put_frame(gfp, templen);
}
/* Send a UDP datagram, given destination node, data and length */
void udp_transmit_text(GENFRAME *gfp, NODE *sp, NODE *dp, char *in)
{
UDPKT *udp;
WORD len;
int i;
char *p;
udp = getframe_datap(gfp);
len = strlen(in);//fread(udp->data, 1, sizeof(udp->data), in);
p=(char *)udp->data;
for(i=0;i<len;i++)
*p++=*in++;
put_frame(gfp, make_udp(gfp, sp, dp, len));
}
/* Poll the network interface to keep it alive */
void do_poll(GENFRAME *gfp)
{
poll_net(gfp->g.dtype);
}
/* Convert string (numeric or alphabetic) into a port number, 0 if error */
WORD str2service(char *str)
{
WORD port=0;
if ((*str>=0x30)&&(*str<=0x39))//isdigit(*str))
port = atoi(str);
else if (!strncmp(str, "echo",4))
port = ECHOPORT;
else if (!strncmp(str, "daytime",7))
port = DAYPORT;
else if (!strncmp(str, "time",4))
port = TIMEPORT;
else if (!strncmp(str, "snmp",4))
port = SNMPORT;
return(port);
}
/* Display the incoming UDP data */
void disp_data(BYTE *data, int len)
{
BYTE b;
int i, n, oset=0;
if (!binmode)
{
while (len--)
{
b = *data++;
if ((b>=' '&&b<='~') || b=='\n')
Uart_SendByte(b);
else
Uart_SendByte(' ');
}
}
else while (len > 0)
{
n = mini(len, 24);
Uart_Printf("%04X: ", oset);
for (i=0; i<n; i++)
Uart_Printf("%02X ", data[i]);
Uart_Printf("\n ");
for (i=0; i<n; i++, data++)
{
Uart_SendByte(' ');
Uart_SendByte(*data>=' ' && *data<='~' ? *data : 0);
Uart_SendByte(' ');
}
len -= n;
oset += n;
Uart_SendByte('\n');
}
}
/* Display usage help */
void disp_usage(void)
{
Uart_Printf("Usage: DATAGRAM [options] [IP_addr [portnum [data]]]\n");
Uart_Printf(" Default port number is %u\n", DEFPORT);
Uart_Printf("Options: -b Binary data (hexadecimal data display)\n");
Uart_Printf(" -i name Input data filename\n");
//Uart_Printf(" -c name Config filename (default %s)\n", cfgfile);
Uart_Printf(" -u UDP datagram display\n");
Uart_Printf(" -v Verbose (frame) display\n\n");
Uart_Printf("Example: DATAGRAM -c test.cfg 10.1.1.1 echo \"test string\"\n");
}
/* Ctrl-break handler: set flag and return */
void break_handler(int sig)
{
breakflag = sig;
}
/* EOF */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -