LwIP之IP协议
IP是TCP/IP協(xié)議族中最為核心的協(xié)議,TCP、UDP、ICMP及IGMP數(shù)據(jù)都以IP數(shù)據(jù)報(bào)格式傳輸。IP主要功能是尋址與路由和分片與重組。
?
對(duì)于主機(jī)來說,IP路由選擇是非常簡單的。如果目的主機(jī)與源主機(jī)直接相連或都在一個(gè)共享網(wǎng)絡(luò)上,那么IP數(shù)據(jù)報(bào)就直接送到目的主機(jī)上。否則,主機(jī)把數(shù)據(jù)報(bào)發(fā)往一默認(rèn)的路由器上,由路由器來轉(zhuǎn)發(fā)該數(shù)據(jù)報(bào)。大多數(shù)的主機(jī)都是采用這種簡單機(jī)制,IP是不可靠的數(shù)據(jù)報(bào)傳送服務(wù),不能保證IP數(shù)據(jù)報(bào)能成功地到達(dá)目的地。
?
當(dāng)路由器從一個(gè)大MTU值的網(wǎng)絡(luò)上接收數(shù)據(jù),并將數(shù)據(jù)報(bào)轉(zhuǎn)發(fā)到具有較小MTU值的網(wǎng)絡(luò)上時(shí),就需要對(duì)數(shù)據(jù)報(bào)進(jìn)行分片處理,并在最終的目的地進(jìn)行重組。IP是無連接的數(shù)據(jù)報(bào)傳送服務(wù),每個(gè)數(shù)據(jù)報(bào)的處理都是相互獨(dú)立的,IP數(shù)據(jù)報(bào)可以不按發(fā)送順序接收。
?
IP首部格式
?
/* IP頭部長度 */ #define IP_HLEN 20/* 協(xié)議類型 */ #define IP_PROTO_ICMP 1 #define IP_PROTO_UDP 17 #define IP_PROTO_UDPLITE 136 #define IP_PROTO_TCP 6struct ip_hdr {PACK_STRUCT_FIELD(u16_t _v_hl_tos); //版本號(hào)+首部長度+服務(wù)類型PACK_STRUCT_FIELD(u16_t _len); //總長度(IP首部+數(shù)據(jù)區(qū))PACK_STRUCT_FIELD(u16_t _id); //數(shù)據(jù)包標(biāo)識(shí)(編號(hào))PACK_STRUCT_FIELD(u16_t _offset); //標(biāo)志+片偏移/* IP首部標(biāo)志定義 */ #define IP_RF 0x8000 //保留 #define IP_DF 0x4000 //是否允許分片 #define IP_MF 0x2000 //后續(xù)是否還有更多分片 #define IP_OFFMASK 0x1fff //片偏移域掩碼PACK_STRUCT_FIELD(u16_t _ttl_proto); //生存時(shí)間(最大轉(zhuǎn)發(fā)次數(shù))+協(xié)議類型(IGMP:1、UDP:17、TCP:6)PACK_STRUCT_FIELD(u16_t _chksum); //校驗(yàn)和(IP首部)PACK_STRUCT_FIELD(struct ip_addr src); //源IP地址PACK_STRUCT_FIELD(struct ip_addr dest); //目的IP地址 } PACK_STRUCT_STRUCT;?
先看IP數(shù)據(jù)報(bào)輸出
/* 發(fā)送IP數(shù)據(jù)包 */ err_t ip_output(struct pbuf *p, struct ip_addr *src, struct ip_addr *dest, u8_t ttl, u8_t tos, u8_t proto) {struct netif *netif;/* 根據(jù)IP地址選擇一個(gè)合適(和目的主機(jī)處于同一子網(wǎng))的網(wǎng)絡(luò)接口 */if ((netif = ip_route(dest)) == NULL) {return ERR_RTE;}/* 指定網(wǎng)絡(luò)接口發(fā)送IP數(shù)據(jù)包 */return ip_output_if(p, src, dest, ttl, tos, proto, netif); }/* 指定網(wǎng)絡(luò)接口發(fā)送IP數(shù)據(jù)包 */ err_t ip_output_if(struct pbuf *p, struct ip_addr *src, struct ip_addr *dest, u8_t ttl, u8_t tos, u8_t proto, struct netif *netif) {struct ip_hdr *iphdr;static u16_t ip_id = 0;snmp_inc_ipoutrequests();/* 生成IP頭部 */if (dest != IP_HDRINCL) {u16_t ip_hlen = IP_HLEN;/* 向前調(diào)整出IP頭部空間 */if (pbuf_header(p, IP_HLEN)) {return ERR_BUF;}/* IP頭部指針 */iphdr = p->payload;/* 設(shè)置生存時(shí)間(最大轉(zhuǎn)發(fā)次數(shù)) */IPH_TTL_SET(iphdr, ttl);/* 設(shè)置協(xié)議類型(IGMP:1、UDP:17、TCP:6) */IPH_PROTO_SET(iphdr, proto);/* 設(shè)置目的IP地址 */ip_addr_set(&(iphdr->dest), dest);/* 設(shè)置版本號(hào)+首部長度+服務(wù)類型 */IPH_VHLTOS_SET(iphdr, 4, ip_hlen / 4, tos);/* 設(shè)置總長度(IP首部+數(shù)據(jù)區(qū)) */IPH_LEN_SET(iphdr, htons(p->tot_len));/* 設(shè)置標(biāo)志+片偏移 */IPH_OFFSET_SET(iphdr, 0);/* 設(shè)置數(shù)據(jù)包標(biāo)識(shí)(編號(hào)) */IPH_ID_SET(iphdr, htons(ip_id));/* 每發(fā)送一個(gè)數(shù)據(jù)包,編號(hào)加一 */++ip_id;/* 沒有指定源IP地址 */if (ip_addr_isany(src)) {/* 將當(dāng)前網(wǎng)絡(luò)接口IP地址設(shè)置為源IP地址 */ip_addr_set(&(iphdr->src), &(netif->ip_addr));} /* 設(shè)置源IP地址 */else {ip_addr_set(&(iphdr->src), src);}/* 設(shè)置校驗(yàn)和 */IPH_CHKSUM_SET(iphdr, 0);IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, ip_hlen));}/* IP頭部已經(jīng)包含在pbuf中 */else {iphdr = p->payload;dest = &(iphdr->dest);}/* 如果數(shù)據(jù)包總長度大于MTU,則分片發(fā)送 */if (netif->mtu && (p->tot_len > netif->mtu)) {return ip_frag(p,netif,dest);}/* 如果數(shù)據(jù)包總長度不大于MTU,則直接發(fā)送 */return netif->output(netif, p, dest); }在上述函數(shù)中,ip_route函數(shù)用于IP選路,ip_frag函數(shù)用于IP分片
先看ip_route
/* 根據(jù)IP地址選擇一個(gè)合適(和目的主機(jī)處于同一子網(wǎng))的網(wǎng)絡(luò)接口 */ struct netif *ip_route(struct ip_addr *dest) {struct netif *netif;/* 遍歷網(wǎng)絡(luò)接口鏈表 */for(netif = netif_list; netif != NULL; netif = netif->next) {/* 網(wǎng)絡(luò)接口已經(jīng)啟動(dòng) */if (netif_is_up(netif)) {/* 該網(wǎng)絡(luò)接口和目的主機(jī)處于同一子網(wǎng) */if (ip_addr_netcmp(dest, &(netif->ip_addr), &(netif->netmask))) {return netif;}}}/* 沒有設(shè)置默認(rèn)網(wǎng)絡(luò)接口或默認(rèn)網(wǎng)絡(luò)接口沒有啟動(dòng),直接返回錯(cuò)誤 */if ((netif_default == NULL) || (!netif_is_up(netif_default))) {return NULL;}/* 找不到合適的網(wǎng)絡(luò)接口,返回默認(rèn)網(wǎng)絡(luò)接口 */return netif_default; }再看ip_frag,每個(gè)數(shù)據(jù)分片包含一個(gè)IP首部,該首部基本復(fù)制了原始的數(shù)據(jù)報(bào)IP首部(僅改變 標(biāo)志+片偏移量),首部后面是數(shù)據(jù)分片攜帶的數(shù)據(jù),每個(gè)分片的總長度應(yīng)該小于底層網(wǎng)絡(luò)的MTU值。
如下圖,顯示了一個(gè)原始數(shù)據(jù)報(bào)被分片后的場(chǎng)景
? ? ? ? ? ? ?
/* IP分片pbuf的有效數(shù)據(jù)緩沖區(qū) */ static u8_t buf[LWIP_MEM_ALIGN_SIZE(IP_FRAG_MAX_MTU + MEM_ALIGNMENT - 1)];/* IP數(shù)據(jù)報(bào)分片發(fā)送 */ err_t ip_frag(struct pbuf *p, struct netif *netif, struct ip_addr *dest) {struct pbuf *rambuf;struct pbuf *header;struct ip_hdr *iphdr;u16_t nfb;u16_t left, cop;u16_t mtu = netif->mtu;u16_t ofo, omf;u16_t last;u16_t poff = IP_HLEN;u16_t tmp;/* 為IP數(shù)據(jù)報(bào)分片申請(qǐng)pbuf */rambuf = pbuf_alloc(PBUF_LINK, 0, PBUF_REF);if (rambuf == NULL) {return ERR_MEM;}/* 初始化分片的len和tot_len為該網(wǎng)絡(luò)接口的mtu值 */rambuf->tot_len = rambuf->len = mtu;/* 將分片的有效數(shù)據(jù)緩沖區(qū)指向靜態(tài)數(shù)據(jù)緩沖區(qū)buf */rambuf->payload = LWIP_MEM_ALIGN((void *)buf);/* 分片的IP頭部指針 */iphdr = rambuf->payload;/* 將IP頭部從原pbuf中拷貝到分片pbuf中 */SMEMCPY(iphdr, p->payload, IP_HLEN);/* 原數(shù)據(jù)報(bào)中的標(biāo)志和片偏移 */tmp = ntohs(IPH_OFFSET(iphdr));ofo = tmp & IP_OFFMASK;omf = tmp & IP_MF;/* 原IP數(shù)據(jù)報(bào)中數(shù)據(jù)總長度 */left = p->tot_len - IP_HLEN;/* 分片中可以存放的最大數(shù)據(jù)量(8字節(jié)為單位) */nfb = (mtu - IP_HLEN) / 8;/* 將原數(shù)據(jù)報(bào)循環(huán)分片發(fā)送 */while (left) {/* 檢查當(dāng)前分片是不是最后一片 */last = (left <= mtu - IP_HLEN);/* 重新合成當(dāng)前分片標(biāo)志+片偏移字段 */tmp = omf | (IP_OFFMASK & (ofo));if (!last) //最后一個(gè)分片tmp = tmp | IP_MF;/* 計(jì)算當(dāng)前分片數(shù)據(jù)總長度 */cop = last ? left : nfb * 8;/* 從原始數(shù)據(jù)報(bào)中將分片數(shù)據(jù)拷貝出來 */poff += pbuf_copy_partial(p, (u8_t *)iphdr + IP_HLEN, cop, poff);/* 更新分片標(biāo)志+片偏移字段 */IPH_OFFSET_SET(iphdr, htons(tmp));/* 更新分片總長度(IP首部+數(shù)據(jù)區(qū)) */IPH_LEN_SET(iphdr, htons(cop + IP_HLEN));/* 更新分片檢驗(yàn)和 */IPH_CHKSUM_SET(iphdr, 0);IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, IP_HLEN));/* 最后一個(gè)分片,調(diào)整數(shù)據(jù)長度(len和tot_len) */if (last)pbuf_realloc(rambuf, left + IP_HLEN);/* 為當(dāng)前的分片申請(qǐng)以太網(wǎng)首部空間 */header = pbuf_alloc(PBUF_LINK, 0, PBUF_RAM);if (header != NULL) {/* 將以太網(wǎng)首部和當(dāng)前IP分片拼接起來 */pbuf_chain(header, rambuf);/* 調(diào)用函數(shù)(etharp_output)將當(dāng)前分片數(shù)據(jù)報(bào)(以太網(wǎng)首部+IP分片)發(fā)送 */netif->output(netif, header, dest);/* 發(fā)送完成將以太網(wǎng)首部+IP分片釋放 */pbuf_free(header);}/* 申請(qǐng)失敗 */else {/* 釋放當(dāng)前IP分片 */pbuf_free(rambuf);return ERR_MEM;}/* 計(jì)算剩余數(shù)據(jù)總長度 */left -= cop;/* 計(jì)算片偏移量 */ofo += nfb;}/* 釋放分片pbuf結(jié)構(gòu)體 */pbuf_free(rambuf);return ERR_OK; }?
IP數(shù)據(jù)報(bào)輸入
/* 當(dāng)前接收數(shù)據(jù)包的網(wǎng)絡(luò)接口指針 */ struct netif *current_netif; /* 當(dāng)前接收數(shù)據(jù)包的IP頭部指針 */ const struct ip_hdr *current_header;/* IP數(shù)據(jù)包輸入處理 */ err_t ip_input(struct pbuf *p, struct netif *inp) {struct ip_hdr *iphdr;struct netif *netif;u16_t iphdr_hlen;u16_t iphdr_len;/* IP頭部指針 */iphdr = p->payload;/* IP版本不是IPv4 */if (IPH_V(iphdr) != 4) {/* 釋放數(shù)據(jù)包,不做任何處理 */pbuf_free(p);return ERR_OK;}/* IP首部字節(jié)數(shù) */iphdr_hlen = IPH_HL(iphdr);iphdr_hlen *= 4;/* IP數(shù)據(jù)包總長度(IP首部+數(shù)據(jù)區(qū)) */iphdr_len = ntohs(IPH_LEN(iphdr));/* IP首部長度大于第一個(gè)pbuf數(shù)據(jù)長度,或IP數(shù)據(jù)包總長度大于pbuf鏈表數(shù)據(jù)總長度 */if ((iphdr_hlen > p->len) || (iphdr_len > p->tot_len)) {/* 釋放數(shù)據(jù)包,不做任何處理 */pbuf_free(p);return ERR_OK;}/* 檢查校驗(yàn)和出錯(cuò) */if (inet_chksum(iphdr, iphdr_hlen) != 0) {/* 釋放數(shù)據(jù)包,不做任何處理 */pbuf_free(p);return ERR_OK;}/* 收縮pbuf數(shù)據(jù)區(qū),剝離IP頭部 */pbuf_realloc(p, iphdr_len);{int first = 1;/* 遍歷所有網(wǎng)絡(luò)接口,判斷該數(shù)據(jù)包是不是發(fā)送給自己 */netif = inp; //首先判斷是不是接收數(shù)據(jù)包的網(wǎng)絡(luò)接口,這個(gè)概率較大do {/* 網(wǎng)絡(luò)接口已啟動(dòng)且已經(jīng)配置IP */if ((netif_is_up(netif)) && (!ip_addr_isany(&(netif->ip_addr)))) {/* IP數(shù)據(jù)包目的IP和該網(wǎng)絡(luò)接口IP相同,或該IP數(shù)據(jù)包為廣播包 */if (ip_addr_cmp(&(iphdr->dest), &(netif->ip_addr)) || ip_addr_isbroadcast(&(iphdr->dest), netif)) {/* 退出循環(huán) */break;}}/* 如果不是接收到數(shù)據(jù)包的網(wǎng)絡(luò)接口,就挨個(gè)遍歷鏈表 */if (first) {first = 0;netif = netif_list;} else {netif = netif->next;}if (netif == inp) {netif = netif->next;}} while(netif != NULL);}/* 如果該數(shù)據(jù)包源IP地址是廣播地址或組播地址 */if ((ip_addr_isbroadcast(&(iphdr->src), inp)) || (ip_addr_ismulticast(&(iphdr->src)))) {/* 釋放數(shù)據(jù)包,不做任何處理 */pbuf_free(p);return ERR_OK;}/* 數(shù)據(jù)包不是發(fā)送給自己 */if (netif == NULL) {/* 釋放數(shù)據(jù)包,不做任何處理 */pbuf_free(p);return ERR_OK;}/* 數(shù)據(jù)包是發(fā)送給自己的 *//* 片偏移量不為0或不是最后一個(gè)分片(即該數(shù)據(jù)包分片發(fā)送) */if ((IPH_OFFSET(iphdr) & htons(IP_OFFMASK | IP_MF)) != 0) {/* IP數(shù)據(jù)包重組 */p = ip_reass(p);if (p == NULL) {return ERR_OK;}iphdr = p->payload;}/* 記錄當(dāng)前接收數(shù)據(jù)包的網(wǎng)絡(luò)接口指針 */current_netif = inp;/* 記錄當(dāng)前接收數(shù)據(jù)包的IP頭部指針 */current_header = iphdr;/* 為用戶預(yù)留的原始IP數(shù)據(jù)包輸入處理接口 */if (raw_input(p, inp) == 0){/* 判斷協(xié)議類型 */switch (IPH_PROTO(iphdr)) {/* UDP協(xié)議 */case IP_PROTO_UDP:/* UDP數(shù)據(jù)包輸入處理 */udp_input(p, inp);break;/* TCP協(xié)議 */case IP_PROTO_TCP:/* TCP數(shù)據(jù)包輸入處理 */tcp_input(p, inp);break;/* ICMP協(xié)議 */case IP_PROTO_ICMP:/* ICMP數(shù)據(jù)包輸入處理 */icmp_input(p, inp);break;/* 不支持的協(xié)議 */default:/* 目的IP不是廣播地址且不是組播地址 */if (!ip_addr_isbroadcast(&(iphdr->dest), inp) && !ip_addr_ismulticast(&(iphdr->dest))) {p->payload = iphdr;/* 發(fā)送ICMP目的不可達(dá)報(bào)文(協(xié)議不可達(dá)) */icmp_dest_unreach(p, ICMP_DUR_PROTO);}/* 釋放數(shù)據(jù)包 */pbuf_free(p);}}/* 數(shù)據(jù)包處理完成,清空當(dāng)前接收數(shù)據(jù)包的網(wǎng)絡(luò)接口指針 */current_netif = NULL;/* 數(shù)據(jù)包處理完成,清空當(dāng)前接收數(shù)據(jù)包的IP頭部指針 */current_header = NULL;return ERR_OK; }其中,ip_reass函數(shù)用于重組IP分片,先看一個(gè)重裝結(jié)構(gòu)體
/* 重組IP數(shù)據(jù)報(bào)結(jié)構(gòu)體 */ struct ip_reassdata {struct ip_reassdata *next; //用于將重組IP數(shù)據(jù)報(bào)結(jié)構(gòu)體連接成鏈表struct pbuf *p; //該IP數(shù)據(jù)報(bào)的所有數(shù)據(jù)struct ip_hdr iphdr; //該數(shù)據(jù)報(bào)的IP頭部u16_t datagram_len; //該數(shù)據(jù)報(bào)的數(shù)據(jù)總長度u8_t flags; //是否收到最后一個(gè)分片u8_t timer; //重裝數(shù)據(jù)報(bào)剩余生存時(shí)間 };/* 重組IP數(shù)據(jù)報(bào)鏈表 */ static struct ip_reassdata *reassdatagrams;?最終,所有的重裝結(jié)構(gòu)體,會(huì)通過next字段連接成一個(gè)鏈表,如下圖所示
? ? ? ? ? ? ? ? ??
?上圖中,每一個(gè)pbuf鏈都表示一個(gè)IP分片。LwIP把分片IP首部的前八個(gè)字節(jié)重新組織起來,替換成分片的相關(guān)信息。最終,如上圖所示,通過next_pbuf字段將所有的分片按照片偏移從小到大的順序連接起來。
/* IP分片結(jié)構(gòu)體 */ struct ip_reass_helper {PACK_STRUCT_FIELD(struct pbuf *next_pbuf); //用于將IP分片連接成鏈表(重組IP數(shù)據(jù)報(bào))PACK_STRUCT_FIELD(u16_t start); //分片中數(shù)據(jù)的起始位置PACK_STRUCT_FIELD(u16_t end); //分片中數(shù)據(jù)的結(jié)束位置 } PACK_STRUCT_STRUCT; /* IP數(shù)據(jù)報(bào)重組 */ struct pbuf *ip_reass(struct pbuf *p) {struct pbuf *r;struct ip_hdr *fraghdr;struct ip_reassdata *ipr;struct ip_reass_helper *iprh;u16_t offset, len;u8_t clen;struct ip_reassdata *ipr_prev = NULL;/* 分片數(shù)據(jù)報(bào)IP頭部 */fraghdr = (struct ip_hdr*)p->payload;/* 對(duì)IP頭部長度進(jìn)行檢驗(yàn),不支持選項(xiàng)字段 */if ((IPH_HL(fraghdr) * 4) != IP_HLEN) {goto nullreturn;}/* 計(jì)算片偏移(字節(jié)為單位) */offset = (ntohs(IPH_OFFSET(fraghdr)) & IP_OFFMASK) * 8;/* 計(jì)算分片中的數(shù)據(jù)長度 */len = ntohs(IPH_LEN(fraghdr)) - IPH_HL(fraghdr) * 4;/* 分片中的pbuf個(gè)數(shù) */clen = pbuf_clen(p);/* 所有IP分片pbuf總個(gè)數(shù)超過上限 */if ((ip_reass_pbufcount + clen) > IP_REASS_MAX_PBUFS) {goto nullreturn;}/* 遍歷所有重組IP數(shù)據(jù)報(bào)鏈表 */for (ipr = reassdatagrams; ipr != NULL; ipr = ipr->next) {/* 檢查該分片是否屬于該重組IP數(shù)據(jù)報(bào)(源地址+目的地址+數(shù)據(jù)報(bào)標(biāo)識(shí)(編號(hào))) */if (IP_ADDRESSES_AND_ID_MATCH(&ipr->iphdr, fraghdr)) {break;}ipr_prev = ipr;}/* 沒有匹配到重組IP數(shù)據(jù)報(bào),說明是一個(gè)新的重組IP數(shù)據(jù)報(bào) */if (ipr == NULL) {/* 新建一個(gè)重組IP數(shù)據(jù)報(bào),并插入鏈表 */ipr = ip_reass_enqueue_new_datagram(fraghdr, clen);if(ipr == NULL) {goto nullreturn;}} /* 匹配到重組IP數(shù)據(jù)報(bào) */else {/* 收到第一個(gè)分片 */if (((ntohs(IPH_OFFSET(fraghdr)) & IP_OFFMASK) == 0) && ((ntohs(IPH_OFFSET(&ipr->iphdr)) & IP_OFFMASK) != 0)) {/* 設(shè)置重組IP數(shù)據(jù)報(bào)的IP頭部 */SMEMCPY(&ipr->iphdr, fraghdr, IP_HLEN);}}/* 更新所有IP分片重組pbuf總個(gè)數(shù) */ip_reass_pbufcount += clen;/* 收到最后一個(gè)分片 */if ((ntohs(IPH_OFFSET(fraghdr)) & IP_MF) == 0) {/* 設(shè)置重組IP數(shù)據(jù)的標(biāo)志位為已收到最后一個(gè)分片 */ipr->flags |= IP_REASS_FLAG_LASTFRAG;/* 設(shè)置重組IP數(shù)據(jù)的數(shù)據(jù)總長度 */ipr->datagram_len = offset + len;}/* 將IP分片插入合適的重組IP數(shù)據(jù)報(bào),判斷所有分片都已經(jīng)收到 */if (ip_reass_chain_frag_into_datagram_and_validate(ipr, p)) {/* 分片接收完成,數(shù)據(jù)總長度更新為IP首部長度+數(shù)據(jù)長度 */ipr->datagram_len += IP_HLEN;/* 第二個(gè)分片指針 */r = ((struct ip_reass_helper *)ipr->p->payload)->next_pbuf;/* 將第一個(gè)IP分片結(jié)構(gòu)還原成IP首部結(jié)構(gòu)體 */fraghdr = (struct ip_hdr *)(ipr->p->payload);SMEMCPY(fraghdr, &ipr->iphdr, IP_HLEN);IPH_LEN_SET(fraghdr, htons(ipr->datagram_len));IPH_OFFSET_SET(fraghdr, 0);IPH_CHKSUM_SET(fraghdr, 0);IPH_CHKSUM_SET(fraghdr, inet_chksum(fraghdr, IP_HLEN));/* 遍歷所有IP分片 */p = ipr->p;while(r != NULL) {iprh = (struct ip_reass_helper*)r->payload;/* 向后剝掉各個(gè)分片的IP頭部 */pbuf_header(r, -IP_HLEN);/* 將各個(gè)分片拼接起來 */pbuf_cat(p, r);r = iprh->next_pbuf;}/* 將該IP重組數(shù)據(jù)報(bào)從鏈表中移除 */ip_reass_dequeue_datagram(ipr, ipr_prev);/* 更新IP分片重組pbuf總個(gè)數(shù) */ip_reass_pbufcount -= pbuf_clen(p);/* 返回重組后的IP數(shù)據(jù)報(bào)pbuf鏈表 */return p;}return NULL;nullreturn:/* 釋放該分片空間 */pbuf_free(p);return NULL; }/* 新建一個(gè)重組IP數(shù)據(jù)報(bào),并插入鏈表 */ static struct ip_reassdata *ip_reass_enqueue_new_datagram(struct ip_hdr *fraghdr, int clen) {struct ip_reassdata* ipr;/* 為重組IP數(shù)據(jù)報(bào)結(jié)構(gòu)體分配空間 */ipr = memp_malloc(MEMP_REASSDATA);/* 分配失敗 */if (ipr == NULL) {return NULL;}/* 清空重組IP數(shù)據(jù)報(bào)結(jié)構(gòu)體 */memset(ipr, 0, sizeof(struct ip_reassdata));/* 重組IP數(shù)據(jù)報(bào)最大生存時(shí)間 */ipr->timer = IP_REASS_MAXAGE;/* 將重組IP數(shù)據(jù)報(bào)結(jié)構(gòu)體插入鏈表 */ipr->next = reassdatagrams;reassdatagrams = ipr;/* 設(shè)置重組IP數(shù)據(jù)報(bào)的IP頭部數(shù)據(jù) */SMEMCPY(&(ipr->iphdr), fraghdr, IP_HLEN);return ipr; }/* 將IP重組數(shù)據(jù)報(bào)從鏈表中移除 */ static void ip_reass_dequeue_datagram(struct ip_reassdata *ipr, struct ip_reassdata *prev) {if (reassdatagrams == ipr) {reassdatagrams = ipr->next;} else {prev->next = ipr->next;}/* 釋放該重IP數(shù)據(jù)報(bào)結(jié)構(gòu)體 */memp_free(MEMP_REASSDATA, ipr); }/* 將IP分片插入合適的重組IP數(shù)據(jù)報(bào),并判斷是否所有分片都已經(jīng)收到 */ static int ip_reass_chain_frag_into_datagram_and_validate(struct ip_reassdata *ipr, struct pbuf *new_p) {struct ip_reass_helper *iprh, *iprh_tmp, *iprh_prev=NULL;struct pbuf *q;u16_t offset, len;struct ip_hdr *fraghdr;int valid = 1;/* IP分片的IP頭部指針 */fraghdr = (struct ip_hdr*)new_p->payload; /* IP分片的數(shù)據(jù)長度 */len = ntohs(IPH_LEN(fraghdr)) - IPH_HL(fraghdr) * 4;/* IP分片的片偏移(字節(jié)為單位) */offset = (ntohs(IPH_OFFSET(fraghdr)) & IP_OFFMASK) * 8;/* IP分片結(jié)構(gòu)體指針 */iprh = (struct ip_reass_helper *)new_p->payload;/* 下一個(gè)分片指針 */iprh->next_pbuf = NULL;/* 分片中數(shù)據(jù)的起始位置和結(jié)束位置 */iprh->start = offset;iprh->end = offset + len;/* 按照片偏移從小到大將IP分片插入重組IP數(shù)據(jù)報(bào) */for (q = ipr->p; q != NULL;) {iprh_tmp = (struct ip_reass_helper *)q->payload;/* iprh片偏移小于iprh_tmp */if (iprh->start < iprh_tmp->start) {/* 將iprh插到iprh_tmp前面 */iprh->next_pbuf = q;/* iprh不是片偏移最小的分片 */if (iprh_prev != NULL) {/* 將iprh插到iprh_prev后面 */iprh_prev->next_pbuf = new_p;}/* iprh是片偏移最小的分片 */else {/* 將iprh插到頭部 */ipr->p = new_p;}break;} /* 收到重復(fù)分片,不做處理,直接刪除 */else if(iprh->start == iprh_tmp->start) {goto freepbuf;} /* iprh片偏移大于iprh_tmp */else {/* iprh_prev不是片偏移最小的分片 */if (iprh_prev != NULL) {/* iprh_prev和iprh_tmp不連續(xù) */if (iprh_prev->end != iprh_tmp->start) {valid = 0; //分片不連續(xù),還有分片沒有收到}}}q = iprh_tmp->next_pbuf;iprh_prev = iprh_tmp;}/* 鏈表為空或者已經(jīng)遍歷到鏈表尾部 */if (q == NULL) {/* 已經(jīng)遍歷到鏈表尾部 */if (iprh_prev != NULL) {/* 將分片插到鏈表尾部 */iprh_prev->next_pbuf = new_p;/* 分片不連續(xù),還有分片沒有收到 */if (iprh_prev->end != iprh->start) {valid = 0;}} /* 鏈表為空 */else {/* 將分片插入鏈表首部 */ipr->p = new_p;}}/* 已經(jīng)收到最后一個(gè)分片 */if ((ipr->flags & IP_REASS_FLAG_LASTFRAG) != 0) {/* 暫未發(fā)現(xiàn)還有分片沒收到,進(jìn)一步判斷 */if (valid) {/* 第一個(gè)分片沒有收到 */if (((struct ip_reass_helper *)ipr->p->payload)->start != 0) {valid = 0; //第一個(gè)分片沒有收到} /* 已經(jīng)收到第一個(gè)分片 */else {/* 遍歷整個(gè)重組IP數(shù)據(jù)報(bào),查看所有分片是否連續(xù) */iprh_prev = iprh;q = iprh->next_pbuf;while (q != NULL) {iprh = (struct ip_reass_helper*)q->payload;if (iprh_prev->end != iprh->start) {valid = 0; /* 分片不連續(xù),還有分片沒有收到 */break;}iprh_prev = iprh;q = iprh->next_pbuf;}}}/* 返回重組IP是否已經(jīng)收到所有分片 */return valid;}/* 還有分片沒有收到 */return 0; }如果傳輸?shù)倪^程中有IP分片丟失,協(xié)議棧是不可能無限期等待重組完成的。LwIP的做法是等待1秒鐘,如果還沒有重組完成,則刪除該重組IP數(shù)據(jù)報(bào)。
/* 重組IP數(shù)據(jù)報(bào)定時(shí)器回調(diào)函數(shù)(周期1秒) */ void ip_reass_tmr(void) {struct ip_reassdata *r, *prev = NULL;/* 遍歷所有重組IP數(shù)據(jù)報(bào) */r = reassdatagrams;while (r != NULL) {/* 沒有超時(shí),將生存周期減一 */if (r->timer > 0) {r->timer--;prev = r;r = r->next;} /* 超時(shí) */else {struct ip_reassdata *tmp;tmp = r;r = r->next;/* 釋放重組IP數(shù)據(jù)報(bào)的所有分片,并將重組IP數(shù)據(jù)報(bào)從鏈表中移除 */ip_reass_free_complete_datagram(tmp, prev);}} }/* 釋放重組IP數(shù)據(jù)報(bào)的所有分片,并將重組IP數(shù)據(jù)報(bào)從鏈表中移除 */ static int ip_reass_free_complete_datagram(struct ip_reassdata *ipr, struct ip_reassdata *prev) {int pbufs_freed = 0;struct pbuf *p;struct ip_reass_helper *iprh;/* 向源主機(jī)發(fā)送ICMP分片重組超時(shí)報(bào)文 */iprh = (struct ip_reass_helper *)ipr->p->payload;if (iprh->start == 0) {p = ipr->p;ipr->p = iprh->next_pbuf;SMEMCPY(p->payload, &ipr->iphdr, IP_HLEN);icmp_time_exceeded(p, ICMP_TE_FRAG);pbufs_freed += pbuf_clen(p);pbuf_free(p);}/* 第一個(gè)分片pbuf鏈表 */p = ipr->p;/* 遍歷該IP重組數(shù)據(jù)報(bào)中的所有分片 */while (p != NULL) {struct pbuf *pcur;iprh = (struct ip_reass_helper *)p->payload;pcur = p;p = iprh->next_pbuf;/* 統(tǒng)計(jì)該IP重組數(shù)據(jù)報(bào)中pbuf個(gè)數(shù) */pbufs_freed += pbuf_clen(pcur);/* 釋放該分片pbuf鏈表 */pbuf_free(pcur); }/* 將IP重組數(shù)據(jù)報(bào)從鏈表中移除 */ip_reass_dequeue_datagram(ipr, prev);/* 更新所有IP分片重組pbuf總個(gè)數(shù) */ip_reass_pbufcount -= pbufs_freed;/* 返回釋放的pbuf個(gè)數(shù) */return pbufs_freed; }?
總結(jié)
- 上一篇: STM32之独立看门狗例程
- 下一篇: 数据分析六部曲