本文整理汇总了C++中IPH_CHKSUM_SET函数的典型用法代码示例。如果您正苦于以下问题:C++ IPH_CHKSUM_SET函数的具体用法?C++ IPH_CHKSUM_SET怎么用?C++ IPH_CHKSUM_SET使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了IPH_CHKSUM_SET函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: send_ping
void send_ping(router_t *router, addr_ip_t dest_ip, addr_ip_t src_ip, uint16_t id, uint16_t count) {
int len = IPV4_HEADER_LENGTH+8;
byte *payload = malloc_or_die(len*sizeof(byte)); //Free'd (below)
struct ip_hdr *iphdr = (void *)payload;
IPH_VHLTOS_SET(iphdr, 4, 5, 0);
IPH_LEN_SET(iphdr, htons(len));
IPH_ID_SET(iphdr, 0);
IPH_OFFSET_SET(iphdr, 0);
IPH_TTL_SET(iphdr, 64);
IPH_PROTO_SET(iphdr, 1);
iphdr->src.addr = src_ip;
iphdr->dest.addr = dest_ip;
IPH_CHKSUM_SET(iphdr, 0);
IPH_CHKSUM_SET(iphdr, htons(calc_checksum(payload, IPV4_HEADER_LENGTH)));
struct icmp_echo_hdr *pihdr = (void *)payload+IPV4_HEADER_LENGTH;
ICMPH_TYPE_SET(pihdr, ICMP_TYPE_ECHO_REQUEST);
ICMPH_CODE_SET(pihdr, 0);
pihdr->id = id;
pihdr->seqno = count;
ICMPH_CHKSUM_SET(pihdr, 0);
ICMPH_CHKSUM_SET(pihdr, htons(calc_checksum(payload+IPV4_HEADER_LENGTH, 8)));
send_packet(payload, src_ip, dest_ip, len, FALSE, FALSE);
free(payload);
}
示例2: ip_output_if
/*-----------------------------------------------------------------------------------*/
err_t
ip_output_if(struct pbuf *p, struct ip_addr *src, struct ip_addr *dest,
u8_t ttl,
u8_t proto, struct netif *netif)
{
static struct ip_hdr *iphdr;
static u16_t ip_id = 0;
if(dest != IP_HDRINCL) {
if(pbuf_header(p, IP_HLEN)) {
DEBUGF(IP_DEBUG, ("ip_output: not enough room for IP header in pbuf\n"));
#ifdef IP_STATS
++stats.ip.err;
#endif /* IP_STATS */
pbuf_free(p);
return ERR_BUF;
}
iphdr = p->payload;
IPH_TTL_SET(iphdr, ttl);
IPH_PROTO_SET(iphdr, proto);
ip_addr_set(&(iphdr->dest), dest);
IPH_VHLTOS_SET(iphdr, 4, IP_HLEN / 4, 0);
IPH_LEN_SET(iphdr, htons(p->tot_len));
IPH_OFFSET_SET(iphdr, htons(IP_DF));
IPH_ID_SET(iphdr, htons(++ip_id));
if(ip_addr_isany(src)) {
ip_addr_set(&(iphdr->src), &(netif->ip_addr));
} else {
ip_addr_set(&(iphdr->src), src);
}
IPH_CHKSUM_SET(iphdr, 0);
#if CHECKSUM_GEN_IP
IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, IP_HLEN));
#endif
} else {
iphdr = p->payload;
dest = &(iphdr->dest);
}
#ifdef IP_STATS
stats.ip.xmit++;
#endif /* IP_STATS */
DEBUGF(IP_DEBUG, ("ip_output_if: %c%c ", netif->name[0], netif->name[1]));
#if IP_DEBUG
ip_debug_print(p);
#endif /* IP_DEBUG */
return netif->output(netif, p, dest);
}
示例3: pbuf_header
// remove MTD_FLASHMEM to speedup routing
err_t MTD_FLASHMEM Router::netif_input(pbuf *p, netif *inp)
{
eth_hdr* ethdr = (eth_hdr*)p->payload;
if (ntohs(ethdr->type) == ETHTYPE_IP)
{
// move buffer pointer to start of IP header
pbuf_header(p, -sizeof(eth_hdr));
ip_hdr* iphdr = (ip_hdr*)(p->payload);
// needs to route?
// 1. check match of source interface IP/netmask and destination IP
bool route = ((iphdr->dest.addr & inp->netmask.addr) != (inp->ip_addr.addr & inp->netmask.addr));
// 2. check if not multicast or broadcast (>=224.0.0.0 up to 255.255.255.255)
route = route && ((iphdr->dest.addr & 0xE0) != 0xE0);
if (route)
{
/*
debug("netif_input intf=%d len=%d id=%d prot=%d src=%s dst=%s route?=%c\r\n",
inp->num, p->tot_len, IPH_ID(iphdr), IPH_PROTO(iphdr),
(char const*)IPAddress(iphdr->src.addr).get_str(),
(char const*)IPAddress(iphdr->dest.addr).get_str(),
route?'Y':'N');
*/
// find destination interface
ip_addr_t ipdest;
ipdest.addr = iphdr->dest.addr;
netif* destIntf = ip_route(&ipdest);
// decrement TTL
IPH_TTL_SET(iphdr, IPH_TTL(iphdr) - 1);
if (IPH_TTL(iphdr) > 0)
{
// update IP checksum
if (IPH_CHKSUM(iphdr) >= PP_HTONS(0xffffU - 0x100))
IPH_CHKSUM_SET(iphdr, IPH_CHKSUM(iphdr) + PP_HTONS(0x100) + 1);
else
IPH_CHKSUM_SET(iphdr, IPH_CHKSUM(iphdr) + PP_HTONS(0x100));
// send the packet
ip_output_if(p, NULL, IP_HDRINCL, 0, 0, 0, destIntf);
}
pbuf_free(p);
return ERR_OK;
}
// restore buffer pointer to start of Ethernet header
pbuf_header(p, +sizeof(eth_hdr));
}
return (Router::s_prevInput[inp->num])(p, inp);
}
示例4: ip_forward
/**
* Forwards an IP packet. It finds an appropriate route for the
* packet, decrements the TTL value of the packet, adjusts the
* checksum and outputs the packet on the appropriate interface.
*
* @param p the packet to forward (p->payload points to IP header)
* @param iphdr the IP header of the input packet
* @param inp the netif on which this packet was received
* @return the netif on which the packet was sent (NULL if it wasn't sent)
*/
static struct netif *
ip_forward(struct pbuf *p, struct ip_hdr *iphdr, struct netif *inp)
{
struct netif *netif;
PERF_START;
/* Find network interface where to forward this IP packet to. */
netif = ip_route((struct ip_addr *)&(iphdr->dest));
if (netif == NULL) {
LWIP_DEBUGF(IP_DEBUG, ("ip_forward: no forwarding route for 0x%"X32_F" found\n",
iphdr->dest.addr));
snmp_inc_ipoutnoroutes();
return (struct netif *)NULL;
}
/* Do not forward packets onto the same network interface on which
* they arrived. */
if (netif == inp) {
LWIP_DEBUGF(IP_DEBUG, ("ip_forward: not bouncing packets back on incoming interface.\n"));
snmp_inc_ipoutnoroutes();
return (struct netif *)NULL;
}
/* decrement TTL */
IPH_TTL_SET(iphdr, IPH_TTL(iphdr) - 1);
/* send ICMP if TTL == 0 */
if (IPH_TTL(iphdr) == 0) {
snmp_inc_ipinhdrerrors();
#if LWIP_ICMP
/* Don't send ICMP messages in response to ICMP messages */
if (IPH_PROTO(iphdr) != IP_PROTO_ICMP) {
icmp_time_exceeded(p, ICMP_TE_TTL);
}
#endif /* LWIP_ICMP */
return (struct netif *)NULL;
}
/* Incrementally update the IP checksum. */
if (IPH_CHKSUM(iphdr) >= htons(0xffff - 0x100)) {
IPH_CHKSUM_SET(iphdr, IPH_CHKSUM(iphdr) + htons(0x100) + 1);
} else {
IPH_CHKSUM_SET(iphdr, IPH_CHKSUM(iphdr) + htons(0x100));
}
LWIP_DEBUGF(IP_DEBUG, ("ip_forward: forwarding packet to 0x%"X32_F"\n",
iphdr->dest.addr));
IP_STATS_INC(ip.fw);
IP_STATS_INC(ip.xmit);
snmp_inc_ipforwdatagrams();
PERF_STOP("ip_forward");
/* transmit pbuf on chosen interface */
netif->output(netif, p, (struct ip_addr *)&(iphdr->dest));
return netif;
}
示例5: ip_output_if
/**
* Sends an IP packet on a network interface. This function constructs
* the IP header and calculates the IP header checksum. If the source
* IP address is NULL, the IP address of the outgoing network
* interface is filled in as source address.
* If the destination IP address is IP_HDRINCL, p is assumed to already
* include an IP header and p->payload points to it instead of the data.
*
* @param p the packet to send (p->payload points to the data, e.g. next
protocol header; if dest == IP_HDRINCL, p already includes an IP
header and p->payload points to that IP header)
* @param src the source IP address to send from (if src == IP_ADDR_ANY, the
* IP address of the netif used to send is used as source address)
* @param dest the destination IP address to send the packet to
* @param ttl the TTL value to be set in the IP header
* @param tos the TOS value to be set in the IP header
* @param proto the PROTOCOL to be set in the IP header
* @param netif the netif on which to send this packet
* @return ERR_OK if the packet was sent OK
* ERR_BUF if p doesn't have enough space for IP/LINK headers
* returns errors returned by netif->output
*
* @note ip_id: RFC791 "some host may be able to simply use
* unique identifiers independent of destination"
*/
uint8 ip_output_if(PBUF *p, IP_ADDR *src, IP_ADDR *dest,
uint8 ttl, uint8 tos,
uint8 proto, NETIF *netif)
{
IP_HDR *iphdr;
static uint16 ip_id = 0;
uint8 ret;
/* Should the IP header be generated or is it already included in p? */
if (dest != IP_HDRINCL)
{
uint16 ip_hlen = IP_HLEN;
if (p != NULL)
{
pbuf_header(p, IP_HLEN);
}
iphdr = p->payload;
IPH_TTL_SET(iphdr, ttl);
IPH_PROTO_SET(iphdr, proto);
ip_addr_set(&(iphdr->dest), dest);
IPH_VHLTOS_SET(iphdr, 4, ip_hlen / 4, tos);
IPH_LEN_SET(iphdr, htons(p->len));
IPH_OFFSET_SET(iphdr, 0);
IPH_ID_SET(iphdr, htons(ip_id));
++ip_id;
if (ip_addr_isany(src))
{
ip_addr_set(&(iphdr->src), &(netif->ip_addr));
}
else
{
ip_addr_set(&(iphdr->src), src);
}
IPH_CHKSUM_SET(iphdr, 0);
#if CHECKSUM_GEN_IP
IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, ip_hlen)); //wp
#endif
}
else
{
/* IP header already included in p */
iphdr = p->payload;
dest = &(iphdr->dest);
}
//printf("fle--> nefif output\n");
ret = netif->output(netif, p, dest); // etharp_output
return ret;
}
示例6: ip_forward
/*-----------------------------------------------------------------------------------*/
static void
ip_forward(struct pbuf *p, struct ip_hdr *iphdr, struct netif *inp)
{
static struct netif *netif;
PERF_START;
if((netif = ip_route((struct ip_addr *)&(iphdr->dest))) == NULL) {
DEBUGF(IP_DEBUG, ("ip_forward: no forwarding route for 0x%lx found\n",
iphdr->dest.addr));
return;
}
/* Don't forward packets onto the same network interface on which
they arrived. */
if(netif == inp) {
DEBUGF(IP_DEBUG, ("ip_forward: not forward packets back on incoming interface.\n"));
return;
}
/* Decrement TTL and send ICMP if ttl == 0. */
IPH_TTL_SET(iphdr, IPH_TTL(iphdr) - 1);
if(IPH_TTL(iphdr) == 0) {
/* Don't send ICMP messages in response to ICMP messages */
if(IPH_PROTO(iphdr) != IP_PROTO_ICMP) {
icmp_time_exceeded(p, ICMP_TE_TTL);
}
return;
}
/* Incremental update of the IP checksum. */
if(IPH_CHKSUM(iphdr) >= htons(0xffff - 0x100)) {
IPH_CHKSUM_SET(iphdr, IPH_CHKSUM(iphdr) + htons(0x100) + 1);
} else {
IPH_CHKSUM_SET(iphdr, IPH_CHKSUM(iphdr) + htons(0x100));
}
DEBUGF(IP_DEBUG, ("ip_forward: forwarding packet to 0x%lx\n",
iphdr->dest.addr));
#ifdef IP_STATS
++stats.ip.fw;
++stats.ip.xmit;
#endif /* IP_STATS */
PERF_STOP("ip_forward");
netif->output(netif, p, (struct ip_addr *)&(iphdr->dest));
}
示例7: _low_level_dhcp_send
static rt_err_t _low_level_dhcp_send(struct netif *netif,
const void *buffer,
rt_size_t size)
{
struct pbuf *p;
struct eth_hdr *ethhdr;
struct ip_hdr *iphdr;
struct udp_hdr *udphdr;
p = pbuf_alloc(PBUF_LINK,
SIZEOF_ETH_HDR + sizeof(struct ip_hdr)
+ sizeof(struct udp_hdr) + size,
PBUF_RAM);
if (p == RT_NULL) return -RT_ENOMEM;
ethhdr = (struct eth_hdr *)p->payload;
iphdr = (struct ip_hdr *)((char *)ethhdr + SIZEOF_ETH_HDR);
udphdr = (struct udp_hdr *)((char *)iphdr + sizeof(struct ip_hdr));
ETHADDR32_COPY(ðhdr->dest, (struct eth_addr *)ðbroadcast);
ETHADDR16_COPY(ðhdr->src, netif->hwaddr);
ethhdr->type = PP_HTONS(ETHTYPE_IP);
iphdr->src.addr = 0x00000000; /* src: 0.0.0.0 */
iphdr->dest.addr = 0xFFFFFFFF; /* src: 255.255.255.255 */
IPH_VHL_SET(iphdr, 4, IP_HLEN / 4);
IPH_TOS_SET(iphdr, 0x00);
IPH_LEN_SET(iphdr, htons(IP_HLEN + sizeof(struct udp_hdr) + size));
IPH_ID_SET(iphdr, htons(2));
IPH_OFFSET_SET(iphdr, 0);
IPH_TTL_SET(iphdr, 255);
IPH_PROTO_SET(iphdr, IP_PROTO_UDP);
IPH_CHKSUM_SET(iphdr, 0);
IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, IP_HLEN));
udphdr->src = htons(DHCP_SERVER_PORT);
udphdr->dest = htons(DHCP_CLIENT_PORT);
udphdr->len = htons(sizeof(struct udp_hdr) + size);
udphdr->chksum = 0;
memcpy((char *)udphdr + sizeof(struct udp_hdr),
buffer, size);
return netif->linkoutput(netif, p);
}
示例8: handle_not_repsponding_to_arp
void handle_not_repsponding_to_arp(byte *payload, unsigned len) {
debug_println("Not responding to arp:\n");
packet_info_t *pi = malloc_or_die(sizeof(packet_info_t)); //Free'd (below).
pi->packet = malloc_or_die((IPV4_HEADER_OFFSET+len)*sizeof(uint8_t)); //Free'd (below).
pi->len = len;
memcpy(pi->packet+IPV4_HEADER_OFFSET, payload, len);
struct eth_hdr *ethhdr = (void *)pi->packet;
ETH_DEST_SET(ethhdr, make_mac_addr(0, 0, 0, 0, 0, 0));
ETH_SRC_SET(ethhdr, make_mac_addr(0, 0, 0, 0, 0, 0));
ETH_TYPE_SET(ethhdr, 0);
//Reverse soruce and destination again.
struct ip_hdr *iphdr;/* = (void *)pi->packet+IPV4_HEADER_OFFSET;
swap_bytes(&IPH_SRC(iphdr), &IPH_DEST(iphdr), 4);*/
unsigned i;
for (i = 0; i < pi->len; i += 2)
printf("%02X%02X ", *(pi->packet+i),*(pi->packet+i+1));
printf("\n");
if (generate_response_ICMP_packet(pi, 3, 1)) return;
iphdr = (void *)pi->packet+IPV4_HEADER_OFFSET; //pi->packet have moved in memory, so re-define.
IPH_CHKSUM_SET(iphdr, 0);
IPH_CHKSUM_SET(iphdr, htons(calc_checksum(pi->packet+IPV4_HEADER_OFFSET, IPV4_HEADER_LENGTH)));
for (i = 0; i < pi->len; i += 2)
printf("%02X%02X ", *(pi->packet+i),*(pi->packet+i+1));
printf("\n");
addr_ip_t target_ip = sr_integ_findnextip(IPH_DEST(iphdr));
char target_ip_str[STRLEN_IP];
ip_to_string(target_ip_str, target_ip);
debug_println("target_ip=%s", target_ip_str);
//send_packet(pi->packet+IPV4_HEADER_OFFSET, IPH_SRC(iphdr), target_ip, pi->len-IPV4_HEADER_OFFSET, FALSE, FALSE);
free(pi->packet);
free(pi);
}
示例9: tcp_create_segment
/** Create a TCP segment usable for passing to tcp_input */
struct pbuf*
tcp_create_segment(ip_addr_t* src_ip, ip_addr_t* dst_ip,
u16_t src_port, u16_t dst_port, void* data, size_t data_len,
u32_t seqno, u32_t ackno, u8_t headerflags)
{
struct pbuf* p;
struct ip_hdr* iphdr;
struct tcp_hdr* tcphdr;
u16_t pbuf_len = (u16_t)(sizeof(struct ip_hdr) + sizeof(struct tcp_hdr) + data_len);
p = pbuf_alloc(PBUF_RAW, pbuf_len, PBUF_POOL);
EXPECT_RETNULL(p != NULL);
EXPECT_RETNULL(p->next == NULL);
memset(p->payload, 0, p->len);
iphdr = p->payload;
/* fill IP header */
iphdr->dest.addr = dst_ip->addr;
iphdr->src.addr = src_ip->addr;
IPH_VHLTOS_SET(iphdr, 4, IP_HLEN / 4, 0);
IPH_LEN_SET(iphdr, htons(p->tot_len));
IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, IP_HLEN));
pbuf_header(p, -(s16_t)sizeof(struct ip_hdr));
tcphdr = p->payload;
tcphdr->src = htons(src_port);
tcphdr->dest = htons(dst_port);
tcphdr->seqno = htonl(seqno);
tcphdr->ackno = htonl(ackno);
TCPH_HDRLEN_SET(tcphdr, sizeof(struct tcp_hdr)/4);
TCPH_FLAGS_SET(tcphdr, headerflags);
tcphdr->wnd = htons(TCP_WND);
/* copy data */
memcpy((char*)tcphdr + sizeof(struct tcp_hdr), data, data_len);
/* calculate checksum */
tcphdr->chksum = inet_chksum_pseudo(p, src_ip, dst_ip,
IP_PROTO_TCP, p->tot_len);
pbuf_header(p, sizeof(struct ip_hdr));
return p;
}
示例10: test_udp_create_test_packet
static struct pbuf *
test_udp_create_test_packet(u16_t length, u16_t port, u32_t dst_addr)
{
err_t err;
u8_t ret;
struct udp_hdr *uh;
struct ip_hdr *ih;
struct pbuf *p;
const u8_t test_data[16] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf};
p = pbuf_alloc(PBUF_TRANSPORT, length, PBUF_POOL);
fail_unless(p != NULL);
if (p == NULL) {
return NULL;
}
fail_unless(p->next == NULL);
err = pbuf_take(p, test_data, length);
fail_unless(err == ERR_OK);
/* add UDP header */
ret = pbuf_add_header(p, sizeof(struct udp_hdr));
fail_unless(!ret);
uh = (struct udp_hdr *)p->payload;
uh->chksum = 0;
uh->dest = uh->src = lwip_htons(port);
uh->len = lwip_htons(p->tot_len);
/* add IPv4 header */
ret = pbuf_add_header(p, sizeof(struct ip_hdr));
fail_unless(!ret);
ih = (struct ip_hdr *)p->payload;
memset(ih, 0, sizeof(*ih));
ih->dest.addr = dst_addr;
ih->_len = lwip_htons(p->tot_len);
ih->_ttl = 32;
ih->_proto = IP_PROTO_UDP;
IPH_VHL_SET(ih, 4, sizeof(struct ip_hdr) / 4);
IPH_CHKSUM_SET(ih, inet_chksum(ih, sizeof(struct ip_hdr)));
return p;
}
示例11: icmp_input
//.........这里部分代码省略.........
if (pbuf_header(r, -hlen)) {
LWIP_ASSERT("icmp_input: restoring original p->payload failed\n", 0);
goto memerr;
}
/* free the original p */
pbuf_free(p);
/* we now have an identical copy of p that has room for link headers */
p = r;
} else {
/* restore p->payload to point to icmp header */
if (pbuf_header(p, -(s16_t)(PBUF_IP_HLEN + PBUF_LINK_HLEN))) {
LWIP_ASSERT("icmp_input: restoring original p->payload failed\n", 0);
goto memerr;
}
}
#endif /* LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN */
/* At this point, all checks are OK. */
/* We generate an answer by switching the dest and src ip addresses,
* setting the icmp type to ECHO_RESPONSE and updating the checksum. */
/* 校验完成,调整ICMP回显请求的相关字段,生成回显应答
* 交换IP数据报的源IP和目的IP地址,填写ICMP报文的类型字段,并重新计算ICMP的校验和
*/
/* 获取ICMP报头指针 */
iecho = p->payload;
/* 交换IP报头的源IP地址和目的IP地址 */
tmpaddr.addr = iphdr->src.addr;
iphdr->src.addr = iphdr->dest.addr;
iphdr->dest.addr = tmpaddr.addr;
/* 设置ICMP报文类型为回显应答 */
ICMPH_TYPE_SET(iecho, ICMP_ER);
/* adjust the checksum */
/* 调整ICMP的校验和 */
if (iecho->chksum >= htons(0xffff - (ICMP_ECHO << 8))) {
iecho->chksum += htons(ICMP_ECHO << 8) + 1;
} else {
iecho->chksum += htons(ICMP_ECHO << 8);
}
/* Set the correct TTL and recalculate the header checksum. */
/* 设置IP首部的TTL */
IPH_TTL_SET(iphdr, ICMP_TTL);
/* 计算IP首部校验和 */
IPH_CHKSUM_SET(iphdr, 0);
#if CHECKSUM_GEN_IP
IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, IP_HLEN));
#endif /* CHECKSUM_GEN_IP */
/* 注意:这里没有修改IP首部的标识字段,所以ICMP回显应答的IP首部的标识字段和
* ICMP回显请求的标识字段是相同的。理论上来说ICMP回显应答的IP首部标识字段应该
* 被修改,但为什么不修改?
* 对Linux主机进行ping,Linux主机回复的ICMP回显应答的IP首部的标识字段就修改了
*/
ICMP_STATS_INC(icmp.xmit);
/* increase number of messages attempted to send */
snmp_inc_icmpoutmsgs();
/* increase number of echo replies attempted to send */
snmp_inc_icmpoutechoreps();
/* pbuf的payload由ICMP的报头移动到IP的报头,hlen保存了IP报头的长度 */
if(pbuf_header(p, hlen)) {
LWIP_ASSERT("Can't move over header in packet", 0);
/* 移动失败 */
} else {
/* 移动成功 */
err_t ret;
/* 调用ip_output_if发送IP数据报,IP_HDRINCL表示IP首部已经填写好,并且
* pbuf的payload指向IP数据报首部,而不是IP载荷首部
*/
ret = ip_output_if(p, &(iphdr->src), IP_HDRINCL,
ICMP_TTL, 0, IP_PROTO_ICMP, inp);
if (ret != ERR_OK) {
LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: ip_output_if returned an error: %c.\n", ret));
}
}
break;
/* 如果ICMP类型不是ICMP回显请求,则直接忽略 */
default:
LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: ICMP type %"S16_F" code %"S16_F" not supported.\n",
(s16_t)type, (s16_t)code));
/* 更新统计量 */
ICMP_STATS_INC(icmp.proterr);
ICMP_STATS_INC(icmp.drop);
}
/* 释放pbuf */
pbuf_free(p);
return;
lenerr:
pbuf_free(p);
ICMP_STATS_INC(icmp.lenerr);
snmp_inc_icmpinerrors();
return;
#if LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN
memerr:
pbuf_free(p);
ICMP_STATS_INC(icmp.err);
snmp_inc_icmpinerrors();
return;
#endif /* LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN */
}
示例12: ip_input
//.........这里部分代码省略.........
}
}
#endif /* IP_OPTIONS_SEND */
/* generate IP header */
if (pbuf_header(p, IP_HLEN)) {
LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip_output: not enough room for IP header in pbuf\n"));
IP_STATS_INC(ip.err);
snmp_inc_ipoutdiscards();
return ERR_BUF;
}
iphdr = p->payload;
LWIP_ASSERT("check that first pbuf can hold struct ip_hdr",
(p->len >= sizeof(struct ip_hdr)));
IPH_TTL_SET(iphdr, ttl);
IPH_PROTO_SET(iphdr, proto);
ip_addr_set(&(iphdr->dest), dest);
IPH_VHLTOS_SET(iphdr, 4, ip_hlen / 4, tos);
IPH_LEN_SET(iphdr, htons(p->tot_len));
IPH_OFFSET_SET(iphdr, 0);
IPH_ID_SET(iphdr, htons(ip_id));
++ip_id;
if (ip_addr_isany(src)) {
ip_addr_set(&(iphdr->src), &(netif->ip_addr));
} else {
ip_addr_set(&(iphdr->src), src);
}
IPH_CHKSUM_SET(iphdr, 0);
#if CHECKSUM_GEN_IP
IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, ip_hlen));
#endif
} else {
/* IP header already included in p */
iphdr = p->payload;
dest = &(iphdr->dest);
}
IP_STATS_INC(ip.xmit);
LWIP_DEBUGF(IP_DEBUG, ("ip_output_if: %c%c%"U16_F"\n", netif->name[0], netif->name[1], netif->num));
ip_debug_print(p);
#if ENABLE_LOOPBACK
if (ip_addr_cmp(dest, &netif->ip_addr)) {
/* Packet to self, enqueue it for loopback */
LWIP_DEBUGF(IP_DEBUG, ("netif_loop_output()"));
return netif_loop_output(netif, p, dest);
}
#endif /* ENABLE_LOOPBACK */
#if IP_FRAG
/* don't fragment if interface has mtu set to 0 [loopif] */
if (netif->mtu && (p->tot_len > netif->mtu)) {
return ip_frag(p,netif,dest);
}
#endif
LWIP_DEBUGF(IP_DEBUG, ("netif->output()"));
return netif->output(netif, p, dest);
}
示例13: tcp_create_segment_wnd
/** Create a TCP segment usable for passing to tcp_input */
static struct pbuf*
tcp_create_segment_wnd(ip_addr_t* src_ip, ip_addr_t* dst_ip,
u16_t src_port, u16_t dst_port, void* data, size_t data_len,
u32_t seqno, u32_t ackno, u8_t headerflags, u16_t wnd)
{
struct pbuf *p, *q;
struct ip_hdr* iphdr;
struct tcp_hdr* tcphdr;
u16_t pbuf_len = (u16_t)(sizeof(struct ip_hdr) + sizeof(struct tcp_hdr) + data_len);
LWIP_ASSERT("data_len too big", data_len <= 0xFFFF);
p = pbuf_alloc(PBUF_RAW, pbuf_len, PBUF_POOL);
EXPECT_RETNULL(p != NULL);
/* first pbuf must be big enough to hold the headers */
EXPECT_RETNULL(p->len >= (sizeof(struct ip_hdr) + sizeof(struct tcp_hdr)));
if (data_len > 0) {
/* first pbuf must be big enough to hold at least 1 data byte, too */
EXPECT_RETNULL(p->len > (sizeof(struct ip_hdr) + sizeof(struct tcp_hdr)));
}
for(q = p; q != NULL; q = q->next) {
memset(q->payload, 0, q->len);
}
iphdr = (struct ip_hdr*)p->payload;
/* fill IP header */
iphdr->dest.addr = ip_2_ip4(dst_ip)->addr;
iphdr->src.addr = ip_2_ip4(src_ip)->addr;
IPH_VHL_SET(iphdr, 4, IP_HLEN / 4);
IPH_TOS_SET(iphdr, 0);
IPH_LEN_SET(iphdr, htons(p->tot_len));
IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, IP_HLEN));
/* let p point to TCP header */
pbuf_header(p, -(s16_t)sizeof(struct ip_hdr));
tcphdr = (struct tcp_hdr*)p->payload;
tcphdr->src = htons(src_port);
tcphdr->dest = htons(dst_port);
tcphdr->seqno = htonl(seqno);
tcphdr->ackno = htonl(ackno);
TCPH_HDRLEN_SET(tcphdr, sizeof(struct tcp_hdr)/4);
TCPH_FLAGS_SET(tcphdr, headerflags);
tcphdr->wnd = htons(wnd);
if (data_len > 0) {
/* let p point to TCP data */
pbuf_header(p, -(s16_t)sizeof(struct tcp_hdr));
/* copy data */
pbuf_take(p, data, (u16_t)data_len);
/* let p point to TCP header again */
pbuf_header(p, sizeof(struct tcp_hdr));
}
/* calculate checksum */
tcphdr->chksum = ip_chksum_pseudo(p,
IP_PROTO_TCP, p->tot_len, src_ip, dst_ip);
pbuf_header(p, sizeof(struct ip_hdr));
return p;
}
示例14: icmp_input
//.........这里部分代码省略.........
/* allocate new packet buffer with space for link headers */
r = pbuf_alloc(PBUF_LINK, p->tot_len, PBUF_RAM);
if (r == NULL) {
LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: allocating new pbuf failed\n"));
goto memerr;
}
LWIP_ASSERT("check that first pbuf can hold struct the ICMP header",
(r->len >= hlen + sizeof(struct icmp_echo_hdr)));
/* copy the whole packet including ip header */
if (pbuf_copy(r, p) != ERR_OK) {
LWIP_ASSERT("icmp_input: copying to new pbuf failed\n", 0);
goto memerr;
}
iphdr = r->payload;
/* switch r->payload back to icmp header */
if (pbuf_header(r, -hlen)) {
LWIP_ASSERT("icmp_input: restoring original p->payload failed\n", 0);
goto memerr;
}
/* free the original p */
pbuf_free(p);
/* we now have an identical copy of p that has room for link headers */
p = r;
} else {
/* restore p->payload to point to icmp header */
if (pbuf_header(p, -(s16_t)(PBUF_IP_HLEN + PBUF_LINK_HLEN))) {
LWIP_ASSERT("icmp_input: restoring original p->payload failed\n", 0);
goto memerr;
}
}
#endif /* LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN */
/* At this point, all checks are OK. */
/* We generate an answer by switching the dest and src ip addresses,
* setting the icmp type to ECHO_RESPONSE and updating the checksum. */
iecho = p->payload;
tmpaddr.addr = iphdr->src.addr;
iphdr->src.addr = iphdr->dest.addr;
iphdr->dest.addr = tmpaddr.addr;
ICMPH_TYPE_SET(iecho, ICMP_ER);
/* This part of code has been modified by ST's MCD Application Team */
/* To use the Checksum Offload Engine for the putgoing ICMP packets,
the ICMP checksum field should be set to 0, this is required only for Tx ICMP*/
#ifdef CHECKSUM_BY_HARDWARE
iecho->chksum = 0;
#else
/* adjust the checksum */
if (iecho->chksum >= htons(0xffff - (ICMP_ECHO << 8))) {
iecho->chksum += htons(ICMP_ECHO << 8) + 1;
} else {
iecho->chksum += htons(ICMP_ECHO << 8);
}
#endif
/* Set the correct TTL and recalculate the header checksum. */
IPH_TTL_SET(iphdr, ICMP_TTL);
IPH_CHKSUM_SET(iphdr, 0);
#if CHECKSUM_GEN_IP
IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, IP_HLEN));
#endif /* CHECKSUM_GEN_IP */
ICMP_STATS_INC(icmp.xmit);
/* increase number of messages attempted to send */
snmp_inc_icmpoutmsgs();
/* increase number of echo replies attempted to send */
snmp_inc_icmpoutechoreps();
if(pbuf_header(p, hlen)) {
LWIP_ASSERT("Can't move over header in packet", 0);
} else {
err_t ret;
ret = ip_output_if(p, &(iphdr->src), IP_HDRINCL,
ICMP_TTL, 0, IP_PROTO_ICMP, inp);
if (ret != ERR_OK) {
LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: ip_output_if returned an error: %c.\n", ret));
}
}
break;
default:
LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: ICMP type %"S16_F" code %"S16_F" not supported.\n",
(s16_t)type, (s16_t)code));
ICMP_STATS_INC(icmp.proterr);
ICMP_STATS_INC(icmp.drop);
}
pbuf_free(p);
return;
lenerr:
pbuf_free(p);
ICMP_STATS_INC(icmp.lenerr);
snmp_inc_icmpinerrors();
return;
#if LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN
memerr:
pbuf_free(p);
ICMP_STATS_INC(icmp.err);
snmp_inc_icmpinerrors();
return;
#endif /* LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN */
}
示例15: ip_frag
/**
* Fragment an IP datagram if too large for the netif.
*
* Chop the datagram in MTU sized chunks and send them in order
* by using a fixed size static memory buffer (PBUF_ROM)
*/
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 = 0;
u16_t left, cop;
u16_t mtu = netif->mtu;
u16_t ofo, omf;
u16_t last;
u16_t poff = IP_HLEN;
u16_t tmp;
/* Get a RAM based MTU sized pbuf */
rambuf = pbuf_alloc(PBUF_LINK, 0, PBUF_REF);
if (rambuf == NULL) {
return ERR_MEM;
}
rambuf->tot_len = rambuf->len = mtu;
rambuf->payload = MEM_ALIGN((void *)buf);
/* Copy the IP header in it */
iphdr = rambuf->payload;
memcpy(iphdr, p->payload, IP_HLEN);
/* Save original offset */
tmp = ntohs(IPH_OFFSET(iphdr));
ofo = tmp & IP_OFFMASK;
omf = tmp & IP_MF;
left = p->tot_len - IP_HLEN;
while (left) {
last = (left <= mtu - IP_HLEN);
/* Set new offset and MF flag */
ofo += nfb;
tmp = omf | (IP_OFFMASK & (ofo));
if (!last)
tmp = tmp | IP_MF;
IPH_OFFSET_SET(iphdr, htons(tmp));
/* Fill this fragment */
nfb = (mtu - IP_HLEN) / 8;
cop = last ? left : nfb * 8;
p = copy_from_pbuf(p, &poff, (u8_t *) iphdr + IP_HLEN, cop);
/* Correct header */
IPH_LEN_SET(iphdr, htons(cop + IP_HLEN));
IPH_CHKSUM_SET(iphdr, 0);
IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, IP_HLEN));
if (last)
pbuf_realloc(rambuf, left + IP_HLEN);
/* This part is ugly: we alloc a RAM based pbuf for
* the link level header for each chunk and then
* free it.A PBUF_ROM style pbuf for which pbuf_header
* worked would make things simpler.
*/
header = pbuf_alloc(PBUF_LINK, 0, PBUF_RAM);
if (header != NULL) {
pbuf_chain(header, rambuf);
netif->output(netif, header, dest);
IPFRAG_STATS_INC(ip_frag.xmit);
pbuf_free(header);
} else {
pbuf_free(rambuf);
return ERR_MEM;
}
left -= cop;
}
pbuf_free(rambuf);
return ERR_OK;
}