* $Id: libnet_init.c,v 1.20 2005/11/29 22:09:23 carlosc Exp $
* libnet_init.c - Initilization routines.
* Copyright (c) 1998 - 2004 Mike D. Schiffman <mike@infonexus.com>
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* 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
#include "../include/config.h"
#if (!(_WIN32) || (__CYGWIN__))
#include "../include/libnet.h"
#include "../include/win32/libnet.h"
libnet_init(int injection_type
, char *device
, char *err_buf
)
if (getuid() && geteuid())
snprintf(err_buf
, LIBNET_ERRBUF_SIZE
,
"%s(): UID or EUID of 0 required\n", __func__
);
if ((WSAStartup(0x0202, &wsaData
)) != 0)
snprintf(err_buf
, LIBNET_ERRBUF_SIZE
,
"%s(): unable to initialize winsock 2\n", __func__
);
l
= (libnet_t
*)malloc(sizeof (libnet_t
));
snprintf(err_buf
, LIBNET_ERRBUF_SIZE
, "%s(): malloc(): %s\n", __func__
,
memset(l
, 0, sizeof (*l
));
l
->injection_type
= injection_type
;
l
->ptag_state
= LIBNET_PTAG_INITIALIZER
;
l
->device
= (device
? strdup(device
) : NULL
);
strncpy(l
->label
, LIBNET_LABEL_DEFAULT
, LIBNET_LABEL_SIZE
);
l
->label
[sizeof(l
->label
)] = '\0';
switch (l
->injection_type
)
if (libnet_select_device(l
) == -1)
snprintf(err_buf
, LIBNET_ERRBUF_SIZE
, l
->err_buf
);
if (libnet_open_link(l
) == -1)
snprintf(err_buf
, LIBNET_ERRBUF_SIZE
, l
->err_buf
);
if (libnet_open_raw4(l
) == -1)
snprintf(err_buf
, LIBNET_ERRBUF_SIZE
, l
->err_buf
);
if (libnet_open_raw6(l
) == -1)
snprintf(err_buf
, LIBNET_ERRBUF_SIZE
, l
->err_buf
);
snprintf(err_buf
, LIBNET_ERRBUF_SIZE
,
"%s(): unsupported injection type\n", __func__
);
libnet_destroy(libnet_t
*l
)
libnet_clear_packet(libnet_t
*l
)
l
->protocol_blocks
= NULL
;
libnet_stats(libnet_t
*l
, struct libnet_stats
*ls
)
ls
->packets_sent
= l
->stats
.packets_sent
;
ls
->packet_errors
= l
->stats
.packet_errors
;
ls
->bytes_written
= l
->stats
.bytes_written
;
libnet_getfd(libnet_t
*l
)
libnet_getdevice(libnet_t
*l
)
libnet_getpbuf(libnet_t
*l
, libnet_ptag_t ptag
)
p
= libnet_pblock_find(l
, ptag
);
/* err msg set in libnet_pblock_find() */
libnet_getpbuf_size(libnet_t
*l
, libnet_ptag_t ptag
)
p
= libnet_pblock_find(l
, ptag
);
/* err msg set in libnet_pblock_find() */
libnet_getpacket_size(libnet_t
*l
)