|
Packit |
03b34a |
/*
|
|
Packit |
03b34a |
* $Id: libnet_advanced.c,v 1.8 2004/11/09 07:05:07 mike Exp $
|
|
Packit |
03b34a |
*
|
|
Packit |
03b34a |
* libnet
|
|
Packit |
03b34a |
* libnet_advanced.c - Advanced routines
|
|
Packit |
03b34a |
*
|
|
Packit |
03b34a |
* Copyright (c) 1998 - 2004 Mike D. Schiffman <mike@infonexus.com>
|
|
Packit |
03b34a |
* All rights reserved.
|
|
Packit |
03b34a |
*
|
|
Packit |
03b34a |
* Redistribution and use in source and binary forms, with or without
|
|
Packit |
03b34a |
* modification, are permitted provided that the following conditions
|
|
Packit |
03b34a |
* are met:
|
|
Packit |
03b34a |
* 1. Redistributions of source code must retain the above copyright
|
|
Packit |
03b34a |
* notice, this list of conditions and the following disclaimer.
|
|
Packit |
03b34a |
* 2. Redistributions in binary form must reproduce the above copyright
|
|
Packit |
03b34a |
* notice, this list of conditions and the following disclaimer in the
|
|
Packit |
03b34a |
* documentation and/or other materials provided with the distribution.
|
|
Packit |
03b34a |
*
|
|
Packit |
03b34a |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
Packit |
03b34a |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
Packit |
03b34a |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
Packit |
03b34a |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
Packit |
03b34a |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
Packit |
03b34a |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
Packit |
03b34a |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
Packit |
03b34a |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
Packit |
03b34a |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
Packit |
03b34a |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
Packit |
03b34a |
* SUCH DAMAGE.
|
|
Packit |
03b34a |
*
|
|
Packit |
03b34a |
*/
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
#if (HAVE_CONFIG_H)
|
|
Packit |
03b34a |
#include "../include/config.h"
|
|
Packit |
03b34a |
#endif
|
|
Packit |
03b34a |
#if (!(_WIN32) || (__CYGWIN__))
|
|
Packit |
03b34a |
#include "../include/libnet.h"
|
|
Packit |
03b34a |
#else
|
|
Packit |
03b34a |
#include "../include/win32/libnet.h"
|
|
Packit |
03b34a |
#endif
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
int
|
|
Packit |
03b34a |
libnet_adv_cull_packet(libnet_t *l, uint8_t **packet, uint32_t *packet_s)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
*packet = NULL;
|
|
Packit |
03b34a |
*packet_s = 0;
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
if (l->injection_type != LIBNET_LINK_ADV)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
|
|
Packit |
03b34a |
"%s(): advanced link mode not enabled\n", __func__);
|
|
Packit |
03b34a |
return (-1);
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
/* checksums will be written in */
|
|
Packit |
03b34a |
return (libnet_pblock_coalesce(l, packet, packet_s));
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
int
|
|
Packit |
03b34a |
libnet_adv_cull_header(libnet_t *l, libnet_ptag_t ptag, uint8_t **header,
|
|
Packit |
03b34a |
uint32_t *header_s)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
libnet_pblock_t *p;
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
*header = NULL;
|
|
Packit |
03b34a |
*header_s = 0;
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
if (l->injection_type != LIBNET_LINK_ADV)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
|
|
Packit |
03b34a |
"%s(): advanced link mode not enabled\n", __func__);
|
|
Packit |
03b34a |
return (-1);
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
p = libnet_pblock_find(l, ptag);
|
|
Packit |
03b34a |
if (p == NULL)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
|
|
Packit |
03b34a |
"%s(): ptag not found, you sure it exists?\n", __func__);
|
|
Packit |
03b34a |
return (-1);
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
*header = p->buf;
|
|
Packit |
03b34a |
*header_s = p->b_len;
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
return (1);
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
int
|
|
Packit |
03b34a |
libnet_adv_write_link(libnet_t *l, const uint8_t *packet, uint32_t packet_s)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
int c;
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
if (l->injection_type != LIBNET_LINK_ADV)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
|
|
Packit |
03b34a |
"%s(): advanced link mode not enabled\n", __func__);
|
|
Packit |
03b34a |
return (-1);
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
c = libnet_write_link(l, packet, packet_s);
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
/* do statistics */
|
|
Packit |
03b34a |
if (c == packet_s)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
l->stats.packets_sent++;
|
|
Packit |
03b34a |
l->stats.bytes_written += c;
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
else
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
l->stats.packet_errors++;
|
|
Packit |
03b34a |
/*
|
|
Packit |
03b34a |
* XXX - we probably should have a way to retrieve the number of
|
|
Packit |
03b34a |
* bytes actually written (since we might have written something).
|
|
Packit |
03b34a |
*/
|
|
Packit |
03b34a |
if (c > 0)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
l->stats.bytes_written += c;
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
return (c);
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
int
|
|
Packit |
03b34a |
libnet_adv_write_raw_ipv4(libnet_t *l, const uint8_t *packet, uint32_t packet_s)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
int c;
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
if (l->injection_type != LIBNET_RAW4_ADV)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
|
|
Packit |
03b34a |
"%s(): advanced raw4 mode not enabled\n", __func__);
|
|
Packit |
03b34a |
return (-1);
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
c = libnet_write_raw_ipv4(l, packet, packet_s);
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
/* do statistics */
|
|
Packit |
03b34a |
if (c == packet_s)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
l->stats.packets_sent++;
|
|
Packit |
03b34a |
l->stats.bytes_written += c;
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
else
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
l->stats.packet_errors++;
|
|
Packit |
03b34a |
/*
|
|
Packit |
03b34a |
* XXX - we probably should have a way to retrieve the number of
|
|
Packit |
03b34a |
* bytes actually written (since we might have written something).
|
|
Packit |
03b34a |
*/
|
|
Packit |
03b34a |
if (c > 0)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
l->stats.bytes_written += c;
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
return (c);
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
void
|
|
Packit |
03b34a |
libnet_adv_free_packet(libnet_t *l, uint8_t *packet)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
/*
|
|
Packit |
03b34a |
* Restore original pointer address so free won't complain about a
|
|
Packit |
03b34a |
* modified chunk pointer.
|
|
Packit |
03b34a |
*/
|
|
Packit |
03b34a |
if (l->aligner > 0)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
packet = packet - l->aligner;
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
free(packet);
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
/* EOF */
|