📄 libnet_build_icmp.c
字号:
p = NULL; /* payload refers to the payload in the original IP header */ if ((payload && !payload_s) || (!payload && payload_s)) { snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, "%s(): payload inconsistency\n", __func__); goto bad; } /* * Build the original IPv4 header in its own pblock. We stick the * payload here. */ ipv4 = libnet_build_ipv4(orig_len, orig_tos, orig_id, orig_frag, orig_ttl, orig_prot, orig_check, orig_src, orig_dst, payload, payload_s, l, 0); if (ipv4 == -1) { /* error set elsewhere */ return (-1); } if (orig_check == 0) { int c = 0; l->aligner = 8 - (l->link_offset % 8); q = libnet_pblock_find(l, ipv4); c = libnet_do_checksum(l, q->buf, libnet_pblock_p2p(q->type), q->h_len); if (c == -1) { /* err msg set in libnet_do_checksum() */ return (-1); } } /* * Now we can build the ICMP part of the packet */ n = LIBNET_ICMPV4_UNREACH_H; /* size of memory block */ /* hl for checksum */ h = LIBNET_ICMPV4_UNREACH_H + LIBNET_IPV4_H + payload_s; /* * Find the existing protocol block if a ptag is specified, or create * a new one. */ p = libnet_pblock_probe(l, ptag, n, LIBNET_PBLOCK_ICMPV4_UNREACH_H); if (p == NULL) { return (-1); } memset(&icmp_hdr, 0, sizeof(icmp_hdr)); icmp_hdr.icmp_type = type; /* packet type */ icmp_hdr.icmp_code = code; /* packet code */ icmp_hdr.icmp_sum = sum; /* checksum */ icmp_hdr.icmp_id = 0; /* must be 0 */ icmp_hdr.icmp_seq = 0; /* must be 0 */ n = libnet_pblock_append(l, p, (u_int8_t *)&icmp_hdr, LIBNET_ICMPV4_UNREACH_H); if (n == -1) { /* error set elsewhere */ goto bad; } if (sum == 0) { /* * If checksum is zero, by default libnet will compute a checksum * for the user. The programmer can override this by calling * libnet_toggle_checksum(l, ptag, 1); */ libnet_pblock_setflags(p, LIBNET_PBLOCK_DO_CHECKSUM); } return (ptag ? ptag : libnet_pblock_update(l, p, h, LIBNET_PBLOCK_ICMPV4_UNREACH_H));bad: libnet_pblock_delete(l, p); return (-1);}libnet_ptag_tlibnet_build_icmpv4_timeexceed(u_int8_t type, u_int8_t code, u_int16_t sum, u_int16_t orig_len, u_int8_t orig_tos, u_int16_t orig_id, u_int16_t orig_frag, u_int8_t orig_ttl, u_int8_t orig_prot, u_int16_t orig_check, u_int32_t orig_src, u_int32_t orig_dst, u_int8_t *payload, u_int32_t payload_s, libnet_t *l, libnet_ptag_t ptag){ u_int32_t n, h; libnet_ptag_t ipv4; libnet_pblock_t *p, *q; struct libnet_icmpv4_hdr icmp_hdr; if (l == NULL) { return (-1); } p = NULL; /* payload refers to the payload in the original IP header */ if ((payload && !payload_s) || (!payload && payload_s)) { snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, "%s(): payload inconsistency\n", __func__); goto bad; } /* * Build the original IPv4 header in its own pblock. We stick the * payload here. */ ipv4 = libnet_build_ipv4(orig_len, orig_tos, orig_id, orig_frag, orig_ttl, orig_prot, orig_check, orig_src, orig_dst, payload, payload_s, l, 0); if (ipv4 == -1) { /* error set elsewhere */ return (-1); } if (orig_check == 0) { int c = 0; l->aligner = 8 - (l->link_offset % 8); q = libnet_pblock_find(l, ipv4); c = libnet_do_checksum(l, q->buf, libnet_pblock_p2p(q->type), q->h_len); if (c == -1) { /* err msg set in libnet_do_checksum() */ return (-1); } } /* size of memory block */ n = LIBNET_ICMPV4_TIMXCEED_H; /* hl for checksum */ h = LIBNET_ICMPV4_TIMXCEED_H + LIBNET_IPV4_H + payload_s; /* * Find the existing protocol block if a ptag is specified, or create * a new one. */ p = libnet_pblock_probe(l, ptag, n, LIBNET_PBLOCK_ICMPV4_TIMXCEED_H); if (p == NULL) { return (-1); } memset(&icmp_hdr, 0, sizeof(icmp_hdr)); icmp_hdr.icmp_type = type; /* packet type */ icmp_hdr.icmp_code = code; /* packet code */ icmp_hdr.icmp_sum = sum; /* checksum */ icmp_hdr.icmp_id = 0; /* must be 0 */ icmp_hdr.icmp_seq = 0; /* must be 0 */ n = libnet_pblock_append(l, p, (u_int8_t *)&icmp_hdr, LIBNET_ICMPV4_TIMXCEED_H); if (n == -1) { goto bad; } if (sum == 0) { /* * If checksum is zero, by default libnet will compute a checksum * for the user. The programmer can override this by calling * libnet_toggle_checksum(l, ptag, 1); */ libnet_pblock_setflags(p, LIBNET_PBLOCK_DO_CHECKSUM); } return (ptag ? ptag : libnet_pblock_update(l, p, h, LIBNET_PBLOCK_ICMPV4_TIMXCEED_H));bad: libnet_pblock_delete(l, p); return (-1);}libnet_ptag_tlibnet_build_icmpv4_redirect(u_int8_t type, u_int8_t code, u_int16_t sum, u_int32_t gateway, u_int16_t orig_len, u_int8_t orig_tos, u_int16_t orig_id, u_int16_t orig_frag, u_int8_t orig_ttl, u_int8_t orig_prot, u_int16_t orig_check, u_int32_t orig_src, u_int32_t orig_dst, u_int8_t *payload, u_int32_t payload_s, libnet_t *l, libnet_ptag_t ptag){ u_int32_t n, h; libnet_ptag_t ipv4; libnet_pblock_t *p, *q; struct libnet_icmpv4_hdr icmp_hdr; if (l == NULL) { return (-1); } p = NULL; /* payload refers to the payload in the original IP header */ if ((payload && !payload_s) || (!payload && payload_s)) { snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, "%s(): payload inconsistency\n", __func__); goto bad; } /* * Build the original IPv4 header in its own pblock. We stick the * payload here. */ ipv4 = libnet_build_ipv4(orig_len, orig_tos, orig_id, orig_frag, orig_ttl, orig_prot, orig_check, orig_src, orig_dst, payload, payload_s, l, 0); if (ipv4 == -1) { /* error set elsewhere */ return (-1); } if (orig_check == 0) { int c = 0; l->aligner = 8 - (l->link_offset % 8); q = libnet_pblock_find(l, ipv4); c = libnet_do_checksum(l, q->buf, libnet_pblock_p2p(q->type), q->h_len); if (c == -1) { /* err msg set in libnet_do_checksum() */ return (-1); } } n = LIBNET_ICMPV4_REDIRECT_H; /* size of memory block */ /* hl for checksum */ h = LIBNET_ICMPV4_REDIRECT_H + LIBNET_IPV4_H + payload_s; /* * Find the existing protocol block if a ptag is specified, or create * a new one. */ p = libnet_pblock_probe(l, ptag, n, LIBNET_PBLOCK_ICMPV4_REDIRECT_H); if (p == NULL) { return (-1); } memset(&icmp_hdr, 0, sizeof(icmp_hdr)); icmp_hdr.icmp_type = type; /* packet type */ icmp_hdr.icmp_code = code; /* packet code */ icmp_hdr.icmp_sum = sum; /* checksum */ icmp_hdr.hun.gateway = htonl(gateway); /* gateway address */ n = libnet_pblock_append(l, p, (u_int8_t *)&icmp_hdr, LIBNET_ICMPV4_REDIRECT_H); if (n == -1) { goto bad; } if (sum == 0) { /* * If checksum is zero, by default libnet will compute a checksum * for the user. The programmer can override this by calling * libnet_toggle_checksum(l, ptag, 1); */ libnet_pblock_setflags(p, LIBNET_PBLOCK_DO_CHECKSUM); } return (ptag ? ptag : libnet_pblock_update(l, p, h, LIBNET_PBLOCK_ICMPV4_REDIRECT_H));bad: libnet_pblock_delete(l, p); return (-1);}/* EOF */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -