| 1 | /* |
| 2 | * $Id: libnet_advanced.c,v 1.20 2005/11/29 22:32:53 carlosc Exp $ |
| 3 | * |
| 4 | * libnet |
| 5 | * libnet_advanced.c - Advanced routines |
| 6 | * |
| 7 | * Copyright (c) 1998 - 2004 Mike D. Schiffman <mike@infonexus.com> |
| 8 | * All rights reserved. |
| 9 | * |
| 10 | * Redistribution and use in source and binary forms, with or without |
| 11 | * modification, are permitted provided that the following conditions |
| 12 | * are met: |
| 13 | * 1. Redistributions of source code must retain the above copyright |
| 14 | * notice, this list of conditions and the following disclaimer. |
| 15 | * 2. Redistributions in binary form must reproduce the above copyright |
| 16 | * notice, this list of conditions and the following disclaimer in the |
| 17 | * documentation and/or other materials provided with the distribution. |
| 18 | * |
| 19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
| 20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 29 | * SUCH DAMAGE. |
| 30 | * |
| 31 | */ |
| 32 | |
| 33 | #if (HAVE_CONFIG_H) |
| 34 | #include "../include/config.h" |
| 35 | #endif |
| 36 | #if (!(_WIN32) || (__CYGWIN__)) |
| 37 | #include "../include/libnet.h" |
| 38 | #else |
| 39 | #include "../include/win32/libnet.h" |
| 40 | #endif |
| 41 | |
| 42 | int |
| 43 | libnet_adv_cull_packet(libnet_t *l, u_int8_t **packet, u_int32_t *packet_s) |
| 44 | { |
| 45 | *packet = NULL; |
| 46 | *packet_s = 0; |
| 47 | |
| 48 | if (l->injection_type != LIBNET_LINK_ADV) |
| 49 | { |
| 50 | snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, |
| 51 | "%s(): advanced link mode not enabled\n", __func__); |
| 52 | return (-1); |
| 53 | } |
| 54 | |
| 55 | /* checksums will be written in */ |
| 56 | return (libnet_pblock_coalesce(l, packet, packet_s)); |
| 57 | } |
| 58 | |
| 59 | int |
| 60 | libnet_adv_cull_header(libnet_t *l, libnet_ptag_t ptag, u_int8_t **header, |
| 61 | u_int32_t *header_s) |
| 62 | { |
| 63 | libnet_pblock_t *p; |
| 64 | |
| 65 | *header = NULL; |
| 66 | *header_s = 0; |
| 67 | |
| 68 | if (l->injection_type != LIBNET_LINK_ADV) |
| 69 | { |
| 70 | snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, |
| 71 | "%s(): advanced link mode not enabled\n", __func__); |
| 72 | return (-1); |
| 73 | } |
| 74 | |
| 75 | p = libnet_pblock_find(l, ptag); |
| 76 | if (p == NULL) |
| 77 | { |
| 78 | snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, |
| 79 | "%s(): ptag not found, you sure it exists?\n", __func__); |
| 80 | return (-1); |
| 81 | } |
| 82 | *header = p->buf; |
| 83 | *header_s = p->b_len; |
| 84 | |
| 85 | return (1); |
| 86 | } |
| 87 | |
| 88 | int |
| 89 | libnet_adv_write_link(libnet_t *l, u_int8_t *packet, u_int32_t packet_s) |
| 90 | { |
| 91 | int c; |
| 92 | |
| 93 | if (l->injection_type != LIBNET_LINK_ADV) |
| 94 | { |
| 95 | snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, |
| 96 | "%s(): advanced link mode not enabled\n", __func__); |
| 97 | return (-1); |
| 98 | } |
| 99 | c = libnet_write_link(l, packet, packet_s); |
| 100 | |
| 101 | /* do statistics */ |
| 102 | if (c == packet_s) |
| 103 | { |
| 104 | l->stats.packets_sent++; |
| 105 | l->stats.bytes_written += c; |
| 106 | } |
| 107 | else |
| 108 | { |
| 109 | l->stats.packet_errors++; |
| 110 | /* |
| 111 | * XXX - we probably should have a way to retrieve the number of |
| 112 | * bytes actually written (since we might have written something). |
| 113 | */ |
| 114 | if (c > 0) |
| 115 | { |
| 116 | l->stats.bytes_written += c; |
| 117 | } |
| 118 | } |
| 119 | return (c); |
| 120 | } |
| 121 | |
| 122 | void |
| 123 | libnet_adv_free_packet(libnet_t *l, u_int8_t *packet) |
| 124 | { |
| 125 | /* |
| 126 | * Restore original pointer address so free won't complain about a |
| 127 | * modified chunk pointer. |
| 128 | */ |
| 129 | if (l->aligner > 0) |
| 130 | { |
| 131 | packet = packet - l->aligner; |
| 132 | } |
| 133 | free(packet); |
| 134 | } |
| 135 | |
| 136 | /* EOF */ |