📄 gprs_ip.cpp
字号:
#include <stdio.h>
#include <string.h>
#include "GPRS_net.h"
#include "GPRS_ip.h"
#include "GPRS_udp.h"
#define IP_TTL 128 /* Time To Live for an outgoing IP datagram */
IPKT * ip_in; // 接收的IP 数据包
IPKT * ip_out; // 要发送的IP 数据包
NODE locnode; //本机的节点信息结构(ip,port)
/* Check frame is IP, checksum & byte-swap, return data len */
short is_ip(short len)
{
short ver, dlen=0, hlen;
unsigned short sum;
if (len>=sizeof(IPHDR))
{
ver = ip_in->i.vhl >> 4; /* Get IP version & hdr len */
hlen = (ip_in->i.vhl & 0xf) << 2;
sum = ~csum(&ip_in->i, (unsigned short)hlen); /* Do checksum */
if (ver==4 && len>=hlen && sum==0) /* If OK.. */
{
dlen = min(ip_in->i.len,len);
dlen-=hlen;
if (hlen > sizeof(IPHDR)) /* If IP options present.. */
{ /* ..delete them, move data down */
memmove(ip_in->ipdata, &ip_in->ipdata[hlen-sizeof(IPHDR)], len);
dlen -= hlen-sizeof(IPHDR);
}
}
}
return(dlen);
}
/* Make an IP packet, if greater than the MTU, also make fragment (subframe) in
** this frame. Return total length of frame and subframes (if any) */
void make_ip(NODE *srcep, NODE *destp, unsigned char pcol, unsigned short dlen)
{
static unsigned short ident=0xf0;
ip_out->i.ident = ident; /* Set datagram ident */
ip_out->i.frags = 0; /* Frag offset in units of 8 bytes */
ip_out->i.vhl = 0x40+(sizeof(IPHDR)>>2); /* Version 4, header len 5 LWORDs */
ip_out->i.service = 0; /* Routine message */
ip_out->i.ttl = IP_TTL; /* Time To Live */
ip_out->i.pcol = pcol; /* Set IP protocol */
ip_out->i.sip = srcep->ip; /* Srce, dest IP addrs */
ip_out->i.dip = destp->ip;
ip_out->i.len = dlen + sizeof(IPHDR); /* Data length */
#ifdef bigend
swap_ip(ip_out);
#endif
ip_out->i.check = 0; /* Clear checksum */
ip_out->i.check = ~csum(ip_out, sizeof(IPHDR)); /* ..then set to calc value */
ident++; /* Increment datagram ident */
}
/* Swap byte order of shorts in IP header */
void swap_ip(IPKT * ip)
{
ip->i.len = swapw(ip->i.len);
ip->i.ident = swapw(ip->i.ident);
ip->i.frags = swapw(ip->i.frags);
ip->i.sip = swapl(ip->i.sip);
ip->i.dip = swapl(ip->i.dip);
}
/* Get the frame driver type, source IP and Ethernet addresses
** Returned data does not include port number, netmask or gateway addr */
void getip_srce(NODE *np)
{
np->ip = ip_in->i.sip;
}
/* Get local node data corresponding to a frame destination IP address
** Data does not include port number. Return 0 if no matching local node */
unsigned short getip_locdest(NODE *np)
{
unsigned short ok=0;
if (ip_in->i.dip==locnode.ip)
{
*np=locnode;
ok=1;
}
return (ok);
}
/* Return ICMP data length (-1 if no data), 0 if not ICMP */
short is_icmp(short len)
{
ICMPKT *icmp;
short dlen=0;
if (len>=sizeof(ICMPHDR))
{
icmp = (ICMPKT *)ip_in;
if ((csum(&icmp->c, (unsigned short)len)) == 0xffff)
dlen = len>sizeof(ICMPHDR) ? len-sizeof(ICMPHDR) : -1;
}
return(dlen);
}
/* Make an ICMP packet */
void make_icmp(NODE *srcep, NODE *destp, unsigned char type, unsigned char codetype,unsigned char *dat,unsigned short dlen)
{
ICMPKT *icmp;
unsigned short len;
icmp = (ICMPKT *)ip_out;
memmove((unsigned char *)&icmp->c.ident,dat,dlen+4);
icmp->c.type = type;
icmp->c.codetype = codetype;
icmp->c.check = 0;
len = (unsigned short)(dlen + sizeof(ICMPHDR));
icmp->c.check = ~csum(&icmp->c, len);
make_ip(srcep, destp, PICMP, len);
}
/* Make ICMP 'destination unreachable' for incoming frame */
/*
short icmp_unreach( NODE *srcep, NODE *destp, unsigned char codetype)
{
unsigned short len;
ICMPKT *icmp;
icmp = (ICMPKT *)ip_out;
len = ((icmp->i.vhl & 0xf) << 2) + 8;
memmove(icmp->icmpdata, icmp, len);
return(make_icmp(srcep, destp, ICUNREACH, codetype, (unsigned short)len));
}
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -