⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ip.c

📁 三星公司的ARM44B0的TCPIP测试程序
💻 C
📖 第 1 页 / 共 2 页
字号:
    ip->i.check = ~csum(ip, IPHDRLEN); /* ..then set to calc value */
    memcpy(tempip,(BYTE *)(&(ip->i)),IPHDRLEN);
    ident++;                            /* Increment datagram ident */
    oset = 0;                           /* Clear fragment offset */
    len += IPHDRLEN + sublen;      /* Bump up length */
    //Uart_Printf("\nlen=%x",len);
    return(make_frame(gfp, destp->mac, PCOL_IP, (WORD)len));
}

/* Swap byte order of ints in IP header */
void swap_ip(GENFRAME *gfp)
{
    IPHDR *iph;

    iph=(IPHDR *)&ipkt;
    //iph = getframe_datap(gfp);
    iph->len = swapw(iph->len);
    iph->ident = swapw(iph->ident);
    iph->frags = swapw(iph->frags);
    iph->sip = swapl(iph->sip);
    iph->dip = swapl(iph->dip);
}

/* Return the maximum IP data size for a given frame without fragmentation */
int ip_maxdata(GENFRAME *gfp)
{
    return(maxi(getframe_mtu(gfp)-sizeof(IPHDR), 0));
}

/* Defragment an incoming IP datagram by matching with existing fragments
** This function handles a maximum of 2 fragments per datagram
** Return total IP data length, 0 if datagram is incomplete */
int defrag_ip(IPKT *ip, int dlen)
{
    int n=0, match=0;
    WORD oset;
    FRAG *fp, *fp2=0;

    oset = (ip->i.frags & 0x1fff) << 3; /* Get offset for imcoming frag */
    while (n<NFRAGS && !match)          /* Search for matching half */
    {
        fp = &frags[n++];
        if (fp->tries)
        {                               /* ..by checking ident */
            if (!(match = (ip->i.ident==fp->ident && ip->i.sip==fp->sipp)))
                fp->tries--;            /* If no match, reduce attempts left */
        }
        else
            fp2 = fp;
    }
    if (match)
    {                                   /* Matched: check isn't a duplicate */
        if ((oset+dlen == fp->oset || fp->oset+fp->len == oset) &&
            dlen+fp->len <= MAXGEN)     /* ..and length is OK */
        {
            if (oset)                   /* Move old data as necessary */
                memmove(&ip->data[oset], ip->data, dlen);
            ip->i.len = dlen += fp->len;/* ..and add in new data */
            memcpy(&ip->data[fp->oset], fp->data, fp->len);
            fp->tries = 0;
        }
        else
        {
            if (netdebug)
                Uart_Printf("Mismatched frag oset %u buff len %u\n", oset, fp->len);
            match = 0;
        }
    }
    else if (fp2)                       /* No match, but there is spare space */
    {
        fp2->tries = FRAGTRIES;         /* Save frag for matching later */
        fp2->ident = ip->i.ident;
        fp2->sipp = ip->i.sip;
        fp2->oset = oset;
        fp2->len = dlen;
        memcpy(fp2->data, ip->data, dlen);
    }
    return(match ? dlen : 0);
}

/* Find local node corresponding to given IP addr, return 0 if not found */
NODE *findloc_ip(LWORD locip)
{
    NODE *np=0;
    int n=0;

    while (get_locnode_n && (np=get_locnode_n(n))!=0 && np->ip!=locip)
        n++;
    return(np);
}

/* Get the frame driver type, source IP and Ethernet addresses
** Returned data does not include port number, netmask or gateway addr */
void getip_srce(GENFRAME *gfp, NODE *np)
{
    IPHDR *iph;

    np->dtype = gfp->g.dtype;
    getframe_srce(gfp, np->mac);
    //iph = getframe_datap(gfp);
    iph = &(ipkt.i);
    np->ip = iph->sip;
}

/* Get the frame driver type, destination IP and Ethernet addresses
** Returned data does not include port number, netmask or gateway addr */
void getip_dest(GENFRAME *gfp, NODE *np)
{
    IPHDR *iph;

    np->dtype = gfp->g.dtype;
    getframe_dest(gfp, np->mac);
    //iph = getframe_datap(gfp);
    iph = &(ipkt.i);
    np->ip = iph->dip;
}

/* Get local node data corresponding to a frame destination IP address
** Data does not include port number. Return 0 if no matching local node */
int getip_locdest(GENFRAME *gfp, NODE *np)
{
    IPHDR *iph;
    NODE *locp;
    int ok=0;

    //iph = getframe_datap(gfp);
    iph=(IPHDR *)&ipkt;
    ok = (locp = findloc_ip(iph->dip)) != 0;
    if (ok)
        *np = *locp;
    return(ok);
}

/* Check a remote address to see if it is on the local subnet.
** If so (or no gateway), return it. If not, return the gateway IP address */
LWORD gate_ip(NODE *remp, NODE *locp)
{
    return((locp->gate==0||on_subnet(remp->ip, locp)) ? remp->ip : locp->gate);
}

/* Check an IP address to see if it is on a subnet, return 0 if not */
int on_subnet(LWORD remip, NODE *locp)
{
    return(((remip ^ locp->ip) & locp->mask) == 0);
}

/* Return ICMP data length (-1 if no data), 0 if not ICMP */
int is_icmp(IPKT *ip, int len)
{
    ICMPKT *icmp;
    WORD sum;
    int dlen=0;

    //icmp=&icmpkt;
    
    if (ip->i.pcol==PICMP && len>=ICMPHDRLEN)//sizeof(ICMPHDR))
    {
        icmp = (ICMPKT *)ip;
        if ((sum=csum(&icmp->c, (WORD)len)) == 0xffff)
        {
            swap_icmp(icmp);
            dlen = len>sizeof(ICMPHDR) ? len-sizeof(ICMPHDR) : -1;
        }
        else
            Uart_Printf("\nICMP checksum error: %04X\n", sum);
    }
    return(dlen);
}

/* Make an ICMP packet */
int make_icmp(GENFRAME *gfp, NODE *srcep, NODE *destp, BYTE type, BYTE code,
    WORD dlen)
{
    ICMPKT *icmp;
    WORD len;
    BYTE *tempicmp;

    //icmp = getframe_datap(gfp);
    tempicmp=(BYTE *)getframe_datap(gfp);
    icmp=&icmpkt;
    icmp->c.type = type;
    icmp->c.code = code;
    icmp->c.check = 0;
    swap_icmp(icmp);
    len = (WORD)(dlen + ICMPHDRLEN);//sizeof(ICMPHDR));
    
    memcpy((BYTE *)(&(icmp->c.ident)),&tempicmp[24],sizeof(WORD));
    memcpy((BYTE *)(&(icmp->c.seq)),&tempicmp[26],sizeof(WORD));
    memcpy((icmp->data),&tempicmp[28],dlen);
    
    icmp->c.check = ~csum(&icmp->c, len);
    
    memcpy(&tempicmp[20],(BYTE *)(&(icmp->c.type)),sizeof(BYTE));
    memcpy(&tempicmp[21],(BYTE *)(&(icmp->c.code)),sizeof(BYTE));
    memcpy(&tempicmp[22],(BYTE *)(&(icmp->c.check)),sizeof(WORD));
    memcpy(&tempicmp[24],(BYTE *)(&(icmp->c.ident)),sizeof(WORD));
    memcpy(&tempicmp[26],(BYTE *)(&(icmp->c.seq)),sizeof(WORD));
    return(make_ip(gfp, srcep, destp, PICMP, len));
}

/* Make ICMP 'destination unreachable' for incoming frame */
int icmp_unreach(GENFRAME *gfp, NODE *srcep, NODE *destp, BYTE code)
{
    int len;
    ICMPKT *icmp;

    icmp = getframe_datap(gfp);
    len = ((icmp->i.vhl & 0xf) << 2) + 8;
    swap_ip(gfp);
    memmove(icmp->data, icmp, len);
    return(make_icmp(gfp, srcep, destp, ICUNREACH, code, (WORD)len));
}

/* Swap byte order of ints in ICMP header */
void swap_icmp(ICMPKT *icmp)
{
    icmp->c.ident = swapw(icmp->c.ident);
    icmp->c.seq = swapw(icmp->c.seq);
}

/* Return the maximum ICMP data size for a given frame without fragmentation */
int icmp_maxdata(GENFRAME *gfp)
{
    return(maxi(ip_maxdata(gfp)-sizeof(ICMPHDR), 0));
}

/* EOF */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -