📄 libnet_build_icmp.c
字号:
/* * $Id: libnet_build_icmp.c,v 1.6 2003/09/23 22:36:55 mike Exp $ * * libnet * libnet_build_icmp.c - ICMP packet assemblers * * Copyright (c) 1998 - 2003 Mike D. Schiffman <mike@infonexus.com> * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * */#if (HAVE_CONFIG_H)#include "../include/config.h"#endif#if (!(_WIN32) || (__CYGWIN__))
#include "../include/libnet.h"
#else
#include "../include/win32/libnet.h"
#endiflibnet_ptag_tlibnet_build_icmpv4_echo(u_int8_t type, u_int8_t code, u_int16_t sum, u_int16_t id, u_int16_t seq, u_int8_t *payload, u_int32_t payload_s, libnet_t *l, libnet_ptag_t ptag){ u_int32_t n, h; libnet_pblock_t *p; struct libnet_icmpv4_hdr icmp_hdr; if (l == NULL) { return (-1); } n = LIBNET_ICMPV4_ECHO_H + payload_s; /* size of memory block */ h = LIBNET_ICMPV4_ECHO_H + payload_s; /* hl for checksum */ /* * 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_ECHO_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 */ /* * XXX - Does this need to be Big Endian or what? I was testing on * 04/23/2002 and stuff was breaking with these htons()'d. */ icmp_hdr.icmp_id = id; /* packet id */ icmp_hdr.icmp_seq = seq; /* packet seq */ n = libnet_pblock_append(l, p, (u_int8_t *)&icmp_hdr, LIBNET_ICMPV4_ECHO_H); if (n == -1) { goto bad; } if ((payload && !payload_s) || (!payload && payload_s)) { snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, "%s(): payload inconsistency\n", __func__); goto bad; } if (payload && payload_s) { n = libnet_pblock_append(l, p, payload, payload_s); 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_ECHO_H));bad: libnet_pblock_delete(l, p); return (-1);}libnet_ptag_tlibnet_build_icmpv4_mask(u_int8_t type, u_int8_t code, u_int16_t sum, u_int16_t id, u_int16_t seq, u_int32_t mask, u_int8_t *payload, u_int32_t payload_s, libnet_t *l, libnet_ptag_t ptag){ u_int32_t n, h; libnet_pblock_t *p; struct libnet_icmpv4_hdr icmp_hdr; if (l == NULL) { return (-1); } n = LIBNET_ICMPV4_MASK_H + payload_s; /* size of memory block */ h = LIBNET_ICMPV4_MASK_H + payload_s; /* hl for checksum */ /* * 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_MASK_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 = htons(id); /* packet id */ icmp_hdr.icmp_seq = htons(seq); /* packet seq */ icmp_hdr.icmp_mask = htonl(mask); /* address mask */ n = libnet_pblock_append(l, p, (u_int8_t *)&icmp_hdr, LIBNET_ICMPV4_MASK_H); if (n == -1) { goto bad; } if ((payload && !payload_s) || (!payload && payload_s)) { snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, "%s(): payload inconsistency\n", __func__); goto bad; } if (payload && payload_s) { n = libnet_pblock_append(l, p, payload, payload_s); 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_MASK_H));bad: libnet_pblock_delete(l, p); return (-1);}libnet_ptag_tlibnet_build_icmpv4_timestamp(u_int8_t type, u_int8_t code, u_int16_t sum, u_int16_t id, u_int16_t seq, n_time otime, n_time rtime, n_time ttime, u_int8_t *payload, u_int32_t payload_s, libnet_t *l, libnet_ptag_t ptag){ u_int32_t n, h; libnet_pblock_t *p; struct libnet_icmpv4_hdr icmp_hdr; if (l == NULL) { return (-1); } n = LIBNET_ICMPV4_TS_H + payload_s; /* size of memory block */ h = LIBNET_ICMPV4_TS_H + payload_s; /* hl for checksum */ /* * 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_TS_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 = htons(id); /* packet id */ icmp_hdr.icmp_seq = htons(seq); /* packet seq */ icmp_hdr.icmp_otime = htonl(otime); /* original timestamp */ icmp_hdr.icmp_rtime = htonl(rtime); /* receive timestamp */ icmp_hdr.icmp_ttime = htonl(ttime); /* transmit timestamp */ n = libnet_pblock_append(l, p, (u_int8_t *)&icmp_hdr, LIBNET_ICMPV4_TS_H); if (n == -1) { goto bad; } if ((payload && !payload_s) || (!payload && payload_s)) { snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, "%s(): payload inconsistency\n", __func__); goto bad; } if (payload && payload_s) { n = libnet_pblock_append(l, p, payload, payload_s); 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_TS_H));bad: libnet_pblock_delete(l, p); return (-1);}libnet_ptag_tlibnet_build_icmpv4_unreach(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); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -