📄 datagram.c
字号:
printf("Can't open net driver '%s'\n", netcfg);
else
{ /* Save ether address */
memcpy(np->mac, ether_addr(dtype), MACLEN);
np->dtype = dtype; /* ..and driver type */
b = (BYTE)(np->ip >> 24);
if (read_cfgstr(fname, "mask", temps, 30))
np->mask = atoip(temps); /* Get netmask */
else
np->mask = b<128 ? 0xff000000L: b<192 ? 0xffff0000L:0xffffff00L;
if (read_cfgstr(fname, "gate", temps, 30))
np->gate = atoip(temps); /* Get gateway IP addr */
else
np->gate = 0;
}
}
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;
if ((rxlen=get_frame(gfp)) > 0) /* Any incoming frames? */
{
ip = getframe_datap(gfp);
if (is_arp(gfp, rxlen))
{ /* ARP response? */
arp = getframe_datap(gfp);
if (arp->op==ARPREQ && arp->dip==locnode.ip)
{ /* ARP request? */
node.ip = arp->sip; /* Make ARP response */
memcpy(node.mac, 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(remnode.mac, arp->smac, MACLEN);
ret = ARP_RX;
}
}
else if ((rxlen=is_ip(gfp, rxlen))!=0 && /* IP datagram? */
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)
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;
char temps[30];
time_t t;
udp = getframe_datap(gfp);
getudp_srce(gfp, &rem); /* Get srce & dest nodes */
getudp_locdest(gfp, &loc);
if (loc.port == locnode.port) /* Client response */
{
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, udp->data, len);
else if (loc.port == DAYPORT) /* Daytime req: get date */
{
time(&t); /* Get standard string */
strcpy(temps, ctime(&t));
strcpy(&temps[24], "\r\n"); /* Patch newline chars */
udp_transmit(gfp, &loc, &rem, (BYTE *)temps, 26);
}
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;
udp = getframe_datap(gfp);
memmove(udp->data, dat, len);
put_frame(gfp, make_udp(gfp, sp, dp, (WORD)maxi(len, 0)));
}
/* Send a UDP datagram, given destination node, data and length */
void udp_transmit_file(GENFRAME *gfp, NODE *sp, NODE *dp, FILE *in)
{
UDPKT *udp;
WORD len;
udp = getframe_datap(gfp);
len = fread(udp->data, 1, sizeof(udp->data), 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 (isdigit(*str))
port = atoi(str);
else if (!stricmp(str, "echo"))
port = ECHOPORT;
else if (!stricmp(str, "daytime"))
port = DAYPORT;
else if (!stricmp(str, "time"))
port = TIMEPORT;
else if (!stricmp(str, "snmp"))
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')
putchar(b);
else
putchar(' ');
}
}
else while (len > 0)
{
n = mini(len, 24);
printf("%04X: ", oset);
for (i=0; i<n; i++)
printf("%02X ", data[i]);
printf("\n ");
for (i=0; i<n; i++, data++)
{
putchar(' ');
putchar(*data>=' ' && *data<='~' ? *data : 0);
putchar(' ');
}
len -= n;
oset += n;
putchar('\n');
}
}
/* Display usage help */
void disp_usage(void)
{
printf("Usage: DATAGRAM [options] [IP_addr [portnum [data]]]\n");
printf(" Default port number is %u\n", DEFPORT);
printf("Options: -b Binary data (hexadecimal data display)\n");
printf(" -i name Input data filename\n");
printf(" -c name Config filename (default %s)\n", cfgfile);
printf(" -u UDP datagram display\n");
printf(" -v Verbose (frame) display\n\n");
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 + -