educoder -- ip分片重组

任务说明

将收到ip分片重组为完整的ip包。

struct IpPkt* CombinIp(struct IpPktList *ip_list)  

注意:参数中给出的分片顺序可能是乱序的,与前一关任务中返回的链表会存在不同。

 #include"network_protocol.h"
 #include <malloc.h>
//ip分片重组
struct IpPkt* CombinIp(struct IpPktList *ip_list){
	struct IpPkt *ip=NULL;
    /********* Begin 1 **********/
    //如果只有一个分片,直接复制ip
    if(ip_list->next == NULL){
        return ip_list->ip;
    }
    //找到最后分片,确定ip的长度,其标志为000,片偏移不为0
    struct IpPktList *t = (struct IpPktList*)malloc(sizeof(struct IpPktList));
    t = ip_list;
    struct IpPkt *tempip;
    short len = t->ip->ip_len;
    while(t!=NULL){
        tempip = t->ip;
        short fragoff = ntohs(tempip->ip_fragoff);
        if((fragoff & 0xe000) == 0 && (fragoff & 0x1fff) > 0){
            // printf("%d\n", fragoff);
            len = (fragoff << 3) + ntohs(tempip->ip_len) - 20;
            break;
        }
        t = t->next;
    }
    // printf("%d\n", len);
    //按片偏移进行重组,注意最后的ip长度,偏移标志字段,校验要重新计算
    ip = (struct IpPkt*)malloc(20 + len);
    *ip = *(t->ip);
    ip->ip_len = htons(len + 20);
    ip->ip_cksum = 0;
    ip->ip_fragoff = 0;
    ip->ip_cksum = htons(ipcksum(ip));
    t = ip_list;
    while(t != NULL){
        tempip = t->ip;
        short fragoff = ntohs(tempip->ip_fragoff);
        short offset = fragoff << 3;
        len = ntohs(tempip->ip_len) - 20;
        for(int i = 0; i < len; i++){
            ip->ip_data[offset + 1] = tempip->ip_data[i];
        }
        t = t->next;
    }
    /********* End 1  **********/
    return ip;
}
 
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值