libnet4r 0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG +15 -0
- data/LICENSE +28 -0
- data/README +187 -0
- data/Rakefile +110 -0
- data/ext/extconf.rb +60 -0
- data/ext/libnet.c +705 -0
- data/ext/libnet_arp.c +152 -0
- data/ext/libnet_ethernet.c +115 -0
- data/ext/libnet_ipv4.c +140 -0
- data/ext/libnet_ipv6.c +138 -0
- data/ext/libnet_udp.c +112 -0
- data/ext/libnet_vlan.c +131 -0
- data/lib/libnet4r/header.rb +609 -0
- data/lib/libnet4r/helpers.rb +19 -0
- data/lib/libnet4r/libnet.rb +39 -0
- data/libnet/CVS/Entries +24 -0
- data/libnet/CVS/Repository +1 -0
- data/libnet/CVS/Root +1 -0
- data/libnet/Makefile.am +15 -0
- data/libnet/Makefile.am.common +10 -0
- data/libnet/Makefile.in +443 -0
- data/libnet/README +21 -0
- data/libnet/VERSION +1 -0
- data/libnet/acconfig.h +36 -0
- data/libnet/acinclude.m4 +463 -0
- data/libnet/aclocal.m4 +1337 -0
- data/libnet/autom4te.cache/CVS/Entries +1 -0
- data/libnet/autom4te.cache/CVS/Repository +1 -0
- data/libnet/autom4te.cache/CVS/Root +1 -0
- data/libnet/autom4te.cache/output.0 +6225 -0
- data/libnet/autom4te.cache/requests +111 -0
- data/libnet/autom4te.cache/traces.0 +272 -0
- data/libnet/config.guess +1314 -0
- data/libnet/config.sub +1410 -0
- data/libnet/configure +6225 -0
- data/libnet/configure.in +235 -0
- data/libnet/doc/BUGS +19 -0
- data/libnet/doc/CHANGELOG +527 -0
- data/libnet/doc/CONTRIB +53 -0
- data/libnet/doc/COPYING +31 -0
- data/libnet/doc/CVS/Entries +12 -0
- data/libnet/doc/CVS/Repository +1 -0
- data/libnet/doc/CVS/Root +1 -0
- data/libnet/doc/DESIGN_NOTES +134 -0
- data/libnet/doc/MIGRATION +172 -0
- data/libnet/doc/PACKET_BUILDING +161 -0
- data/libnet/doc/PORTED +45 -0
- data/libnet/doc/RAWSOCKET_NON_SEQUITUR +41 -0
- data/libnet/doc/TODO +96 -0
- data/libnet/doc/html/acconfig_8h-source.html +49 -0
- data/libnet/doc/html/annotated.html +17 -0
- data/libnet/doc/html/bpf_8h-source.html +277 -0
- data/libnet/doc/html/config_8h-source.html +125 -0
- data/libnet/doc/html/doxygen.css +169 -0
- data/libnet/doc/html/doxygen.png +0 -0
- data/libnet/doc/html/files.html +29 -0
- data/libnet/doc/html/functions.html +26 -0
- data/libnet/doc/html/functions_vars.html +26 -0
- data/libnet/doc/html/getopt_8h-source.html +97 -0
- data/libnet/doc/html/globals.html +219 -0
- data/libnet/doc/html/globals_defs.html +104 -0
- data/libnet/doc/html/globals_func.html +136 -0
- data/libnet/doc/html/gnuc_8h-source.html +56 -0
- data/libnet/doc/html/graph_legend.dot +22 -0
- data/libnet/doc/html/graph_legend.html +75 -0
- data/libnet/doc/html/ifaddrlist_8h-source.html +65 -0
- data/libnet/doc/html/in__systm_8h-source.html +90 -0
- data/libnet/doc/html/index.html +17 -0
- data/libnet/doc/html/libnet-asn1_8h-source.html +268 -0
- data/libnet/doc/html/libnet-functions_8h-source.html +742 -0
- data/libnet/doc/html/libnet-functions_8h.html +8844 -0
- data/libnet/doc/html/libnet-headers_8h-source.html +1655 -0
- data/libnet/doc/html/libnet-headers_8h.html +3053 -0
- data/libnet/doc/html/libnet-macros_8h-source.html +161 -0
- data/libnet/doc/html/libnet-macros_8h.html +358 -0
- data/libnet/doc/html/libnet-structures_8h-source.html +233 -0
- data/libnet/doc/html/libnet-types_8h-source.html +58 -0
- data/libnet/doc/html/libnet_8h-source.html +119 -0
- data/libnet/doc/html/libnet_8h.html +24 -0
- data/libnet/doc/html/structlibnet__802__1q__hdr.html +145 -0
- data/libnet/doc/html/structlibnet__802__1x__hdr.html +97 -0
- data/libnet/doc/libnet.doxygen.conf +1102 -0
- data/libnet/doc/man/CVS/Entries +1 -0
- data/libnet/doc/man/CVS/Repository +1 -0
- data/libnet/doc/man/CVS/Root +1 -0
- data/libnet/doc/man/man3/libnet-functions.h.3 +3136 -0
- data/libnet/doc/man/man3/libnet-headers.h.3 +1872 -0
- data/libnet/doc/man/man3/libnet-macros.h.3 +172 -0
- data/libnet/doc/man/man3/libnet.h.3 +17 -0
- data/libnet/doc/man/man3/libnet_802_1q_hdr.3 +53 -0
- data/libnet/doc/man/man3/libnet_802_1x_hdr.3 +41 -0
- data/libnet/include/CVS/Entries +10 -0
- data/libnet/include/CVS/Repository +1 -0
- data/libnet/include/CVS/Root +1 -0
- data/libnet/include/Makefile.am +7 -0
- data/libnet/include/Makefile.in +395 -0
- data/libnet/include/bpf.h +264 -0
- data/libnet/include/config.h.in +118 -0
- data/libnet/include/gnuc.h +43 -0
- data/libnet/include/ifaddrlist.h +52 -0
- data/libnet/include/libnet/CVS/Entries +9 -0
- data/libnet/include/libnet/CVS/Repository +1 -0
- data/libnet/include/libnet/CVS/Root +1 -0
- data/libnet/include/libnet/Makefile.am +12 -0
- data/libnet/include/libnet/Makefile.in +294 -0
- data/libnet/include/libnet/libnet-asn1.h +255 -0
- data/libnet/include/libnet/libnet-functions.h +2157 -0
- data/libnet/include/libnet/libnet-headers.h +1662 -0
- data/libnet/include/libnet/libnet-macros.h +186 -0
- data/libnet/include/libnet/libnet-structures.h +222 -0
- data/libnet/include/libnet/libnet-types.h +45 -0
- data/libnet/include/libnet.h.in +132 -0
- data/libnet/include/stamp-h.in +1 -0
- data/libnet/include/win32/CVS/Entries +5 -0
- data/libnet/include/win32/CVS/Repository +1 -0
- data/libnet/include/win32/CVS/Root +1 -0
- data/libnet/include/win32/config.h +112 -0
- data/libnet/include/win32/getopt.h +84 -0
- data/libnet/include/win32/in_systm.h +77 -0
- data/libnet/include/win32/libnet.h +106 -0
- data/libnet/install-sh +250 -0
- data/libnet/libnet-config.in +62 -0
- data/libnet/libnet.doxygen.conf +1102 -0
- data/libnet/man/CVS/Entries +1 -0
- data/libnet/man/CVS/Repository +1 -0
- data/libnet/man/CVS/Root +1 -0
- data/libnet/missing +283 -0
- data/libnet/mkinstalldirs +40 -0
- data/libnet/sample/CVS/Entries +47 -0
- data/libnet/sample/CVS/Repository +1 -0
- data/libnet/sample/CVS/Root +1 -0
- data/libnet/sample/Makefile.am +63 -0
- data/libnet/sample/Makefile.in +729 -0
- data/libnet/sample/arp-new.c +144 -0
- data/libnet/sample/arp.c +151 -0
- data/libnet/sample/bgp4_hdr.c +225 -0
- data/libnet/sample/bgp4_notification.c +242 -0
- data/libnet/sample/bgp4_open.c +266 -0
- data/libnet/sample/bgp4_update.c +337 -0
- data/libnet/sample/cdp.c +187 -0
- data/libnet/sample/dhcp_discover.c +257 -0
- data/libnet/sample/dns.c +260 -0
- data/libnet/sample/dot1x.c +113 -0
- data/libnet/sample/fddi_tcp1.c +213 -0
- data/libnet/sample/fddi_tcp2.c +209 -0
- data/libnet/sample/get_addr.c +112 -0
- data/libnet/sample/gre.c +410 -0
- data/libnet/sample/icmp6_echoreq.c +184 -0
- data/libnet/sample/icmp_echo_cq.c +201 -0
- data/libnet/sample/icmp_redirect.c +200 -0
- data/libnet/sample/icmp_timeexceed.c +190 -0
- data/libnet/sample/icmp_timestamp.c +157 -0
- data/libnet/sample/icmp_unreach.c +204 -0
- data/libnet/sample/ieee.c +177 -0
- data/libnet/sample/ip_link.c +201 -0
- data/libnet/sample/ip_raw.c +180 -0
- data/libnet/sample/isl.c +167 -0
- data/libnet/sample/libnet_test.h +60 -0
- data/libnet/sample/mpls.c +251 -0
- data/libnet/sample/ntp.c +193 -0
- data/libnet/sample/ospf_hello.c +179 -0
- data/libnet/sample/ospf_lsa.c +190 -0
- data/libnet/sample/ping_of_death.c +171 -0
- data/libnet/sample/rpc_tcp.c +214 -0
- data/libnet/sample/rpc_udp.c +213 -0
- data/libnet/sample/sebek.c +299 -0
- data/libnet/sample/smurf.c +194 -0
- data/libnet/sample/stp.c +227 -0
- data/libnet/sample/synflood.c +200 -0
- data/libnet/sample/synflood6.c +209 -0
- data/libnet/sample/synflood6_frag.c +234 -0
- data/libnet/sample/tcp1.c +227 -0
- data/libnet/sample/tcp2.c +192 -0
- data/libnet/sample/tftp.c +207 -0
- data/libnet/sample/tring_tcp1.c +214 -0
- data/libnet/sample/tring_tcp2.c +210 -0
- data/libnet/sample/udp1.c +223 -0
- data/libnet/sample/udp2.c +232 -0
- data/libnet/sample/win32/CVS/Entries +9 -0
- data/libnet/sample/win32/CVS/Repository +1 -0
- data/libnet/sample/win32/CVS/Root +1 -0
- data/libnet/sample/win32/arp/CVS/Entries +2 -0
- data/libnet/sample/win32/arp/CVS/Repository +1 -0
- data/libnet/sample/win32/arp/CVS/Root +1 -0
- data/libnet/sample/win32/arp/arp.vcproj +136 -0
- data/libnet/sample/win32/cdp/CVS/Entries +2 -0
- data/libnet/sample/win32/cdp/CVS/Repository +1 -0
- data/libnet/sample/win32/cdp/CVS/Root +1 -0
- data/libnet/sample/win32/cdp/cdp.vcproj +136 -0
- data/libnet/sample/win32/dhcp_discover/CVS/Entries +2 -0
- data/libnet/sample/win32/dhcp_discover/CVS/Repository +1 -0
- data/libnet/sample/win32/dhcp_discover/CVS/Root +1 -0
- data/libnet/sample/win32/dhcp_discover/dhcp_discover.vcproj +139 -0
- data/libnet/sample/win32/dns/CVS/Entries +2 -0
- data/libnet/sample/win32/dns/CVS/Repository +1 -0
- data/libnet/sample/win32/dns/CVS/Root +1 -0
- data/libnet/sample/win32/dns/dns.vcproj +139 -0
- data/libnet/sample/win32/get_addr/CVS/Entries +2 -0
- data/libnet/sample/win32/get_addr/CVS/Repository +1 -0
- data/libnet/sample/win32/get_addr/CVS/Root +1 -0
- data/libnet/sample/win32/get_addr/get_addr.vcproj +139 -0
- data/libnet/sample/win32/getopt.c +121 -0
- data/libnet/sample/win32/icmp_echo_cq/CVS/Entries +2 -0
- data/libnet/sample/win32/icmp_echo_cq/CVS/Repository +1 -0
- data/libnet/sample/win32/icmp_echo_cq/CVS/Root +1 -0
- data/libnet/sample/win32/icmp_echo_cq/icmp_echo_cq.vcproj +139 -0
- data/libnet/sample/win32/tcp1/CVS/Entries +2 -0
- data/libnet/sample/win32/tcp1/CVS/Repository +1 -0
- data/libnet/sample/win32/tcp1/CVS/Root +1 -0
- data/libnet/sample/win32/tcp1/tcp1.vcproj +142 -0
- data/libnet/sample/win32/udp1/CVS/Entries +3 -0
- data/libnet/sample/win32/udp1/CVS/Repository +1 -0
- data/libnet/sample/win32/udp1/CVS/Root +1 -0
- data/libnet/sample/win32/udp1/dns.vcproj +125 -0
- data/libnet/sample/win32/udp1/udp1.vcproj +139 -0
- data/libnet/src/CVS/Entries +60 -0
- data/libnet/src/CVS/Repository +1 -0
- data/libnet/src/CVS/Root +1 -0
- data/libnet/src/Makefile.am +71 -0
- data/libnet/src/Makefile.in +428 -0
- data/libnet/src/libnet_advanced.c +136 -0
- data/libnet/src/libnet_asn1.c +436 -0
- data/libnet/src/libnet_build_802.1q.c +115 -0
- data/libnet/src/libnet_build_802.1x.c +103 -0
- data/libnet/src/libnet_build_802.2.c +167 -0
- data/libnet/src/libnet_build_802.3.c +101 -0
- data/libnet/src/libnet_build_arp.c +169 -0
- data/libnet/src/libnet_build_bgp.c +350 -0
- data/libnet/src/libnet_build_cdp.c +191 -0
- data/libnet/src/libnet_build_data.c +90 -0
- data/libnet/src/libnet_build_dhcp.c +156 -0
- data/libnet/src/libnet_build_dns.c +129 -0
- data/libnet/src/libnet_build_ethernet.c +173 -0
- data/libnet/src/libnet_build_fddi.c +191 -0
- data/libnet/src/libnet_build_gre.c +427 -0
- data/libnet/src/libnet_build_icmp.c +437 -0
- data/libnet/src/libnet_build_igmp.c +112 -0
- data/libnet/src/libnet_build_ip.c +892 -0
- data/libnet/src/libnet_build_ipsec.c +232 -0
- data/libnet/src/libnet_build_isl.c +113 -0
- data/libnet/src/libnet_build_link.c +90 -0
- data/libnet/src/libnet_build_mpls.c +112 -0
- data/libnet/src/libnet_build_ntp.c +121 -0
- data/libnet/src/libnet_build_ospf.c +699 -0
- data/libnet/src/libnet_build_rip.c +108 -0
- data/libnet/src/libnet_build_rpc.c +138 -0
- data/libnet/src/libnet_build_sebek.c +108 -0
- data/libnet/src/libnet_build_snmp.c +52 -0
- data/libnet/src/libnet_build_stp.c +222 -0
- data/libnet/src/libnet_build_tcp.c +365 -0
- data/libnet/src/libnet_build_token_ring.c +193 -0
- data/libnet/src/libnet_build_udp.c +111 -0
- data/libnet/src/libnet_build_vrrp.c +117 -0
- data/libnet/src/libnet_checksum.c +376 -0
- data/libnet/src/libnet_cq.c +395 -0
- data/libnet/src/libnet_crc.c +113 -0
- data/libnet/src/libnet_dll.c +47 -0
- data/libnet/src/libnet_error.c +55 -0
- data/libnet/src/libnet_if_addr.c +415 -0
- data/libnet/src/libnet_init.c +234 -0
- data/libnet/src/libnet_internal.c +310 -0
- data/libnet/src/libnet_link_bpf.c +348 -0
- data/libnet/src/libnet_link_dlpi.c +785 -0
- data/libnet/src/libnet_link_linux.c +310 -0
- data/libnet/src/libnet_link_nit.c +125 -0
- data/libnet/src/libnet_link_none.c +66 -0
- data/libnet/src/libnet_link_pf.c +179 -0
- data/libnet/src/libnet_link_snit.c +149 -0
- data/libnet/src/libnet_link_snoop.c +166 -0
- data/libnet/src/libnet_link_win32.c +346 -0
- data/libnet/src/libnet_pblock.c +511 -0
- data/libnet/src/libnet_port_list.c +295 -0
- data/libnet/src/libnet_prand.c +106 -0
- data/libnet/src/libnet_raw.c +257 -0
- data/libnet/src/libnet_resolve.c +414 -0
- data/libnet/src/libnet_version.c +54 -0
- data/libnet/src/libnet_write.c +502 -0
- data/libnet/version.h.in +6 -0
- data/libnet/win32/CVS/Entries +15 -0
- data/libnet/win32/CVS/Repository +1 -0
- data/libnet/win32/CVS/Root +1 -0
- data/libnet/win32/Libnet-1.1.1-2002.sln +77 -0
- data/libnet/win32/Libnet-1.1.1-2003.ncb +0 -0
- data/libnet/win32/Libnet-1.1.1-2003.sln +84 -0
- data/libnet/win32/Libnet-1.1.1-2003.suo +0 -0
- data/libnet/win32/Libnet-1.1.1.vcproj +311 -0
- data/libnet/win32/Libnet-latest.ncb +0 -0
- data/libnet/win32/Libnet-latest.opt +0 -0
- data/libnet/win32/Libnet-latest.sln +77 -0
- data/libnet/win32/Libnet-latest.suo +0 -0
- data/libnet/win32/Libnet-latest.vcproj +311 -0
- data/libnet/win32/Libnet.dsp +337 -0
- data/libnet/win32/Libnet.dsw +32 -0
- data/libnet/win32/README.txt +57 -0
- data/libnet/win32/libnet_dll.def +164 -0
- data/test/tc_arp.rb +169 -0
- data/test/tc_class.rb +219 -0
- data/test/tc_ethernet.rb +97 -0
- data/test/tc_header.rb +163 -0
- data/test/tc_init.rb +25 -0
- data/test/tc_ipv4.rb +219 -0
- data/test/tc_ipv6.rb +235 -0
- data/test/tc_udp.rb +229 -0
- data/test/tc_vlan.rb +118 -0
- data/test/ts_all.rb +15 -0
- metadata +401 -0
@@ -0,0 +1,415 @@
|
|
1
|
+
/*
|
2
|
+
* $Id: libnet_if_addr.c,v 1.22 2004/03/04 20:51:07 kkuehl Exp $
|
3
|
+
*
|
4
|
+
* libnet
|
5
|
+
* libnet_if_addr.c - interface selection code
|
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
|
+
#ifdef HAVE_SYS_SOCKIO_H
|
42
|
+
#include <sys/sockio.h>
|
43
|
+
#endif
|
44
|
+
#include "../include/ifaddrlist.h"
|
45
|
+
|
46
|
+
#define MAX_IPADDR 512
|
47
|
+
|
48
|
+
#if !(__WIN32__)
|
49
|
+
|
50
|
+
/*
|
51
|
+
* By testing if we can retrieve the FLAGS of an iface
|
52
|
+
* we can know if it exists or not and if it is up.
|
53
|
+
*/
|
54
|
+
int
|
55
|
+
libnet_check_iface(libnet_t *l)
|
56
|
+
{
|
57
|
+
struct ifreq ifr;
|
58
|
+
int fd, res;
|
59
|
+
|
60
|
+
fd = socket(AF_INET, SOCK_DGRAM, 0);
|
61
|
+
if (fd < 0)
|
62
|
+
{
|
63
|
+
snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, "%s() socket: %s\n", __func__,
|
64
|
+
strerror(errno));
|
65
|
+
return (-1);
|
66
|
+
}
|
67
|
+
|
68
|
+
strncpy(ifr.ifr_name, l->device, sizeof(ifr.ifr_name) -1);
|
69
|
+
ifr.ifr_name[sizeof(ifr.ifr_name) - 1] = '\0';
|
70
|
+
|
71
|
+
res = ioctl(fd, SIOCGIFFLAGS, (int8_t *)&ifr);
|
72
|
+
|
73
|
+
if (res < 0)
|
74
|
+
{
|
75
|
+
snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, "%s() ioctl: %s\n", __func__,
|
76
|
+
strerror(errno));
|
77
|
+
}
|
78
|
+
else
|
79
|
+
{
|
80
|
+
if ((ifr.ifr_flags & IFF_UP) == 0)
|
81
|
+
{
|
82
|
+
snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, "%s(): %s is down\n",
|
83
|
+
__func__, l->device);
|
84
|
+
res = -1;
|
85
|
+
}
|
86
|
+
}
|
87
|
+
close(fd);
|
88
|
+
return (res);
|
89
|
+
}
|
90
|
+
|
91
|
+
|
92
|
+
/*
|
93
|
+
* Return the interface list
|
94
|
+
*/
|
95
|
+
|
96
|
+
#ifdef HAVE_SOCKADDR_SA_LEN
|
97
|
+
#define NEXTIFR(i) \
|
98
|
+
((struct ifreq *)((u_char *)&i->ifr_addr + i->ifr_addr.sa_len))
|
99
|
+
#else
|
100
|
+
#define NEXTIFR(i) (i + 1)
|
101
|
+
#endif
|
102
|
+
|
103
|
+
#ifndef BUFSIZE
|
104
|
+
#define BUFSIZE 2048
|
105
|
+
#endif
|
106
|
+
|
107
|
+
#ifdef HAVE_LINUX_PROCFS
|
108
|
+
#define PROC_DEV_FILE "/proc/net/dev"
|
109
|
+
#endif
|
110
|
+
|
111
|
+
int
|
112
|
+
libnet_ifaddrlist(register struct libnet_ifaddr_list **ipaddrp, int8_t *dev,
|
113
|
+
register int8_t *errbuf)
|
114
|
+
{
|
115
|
+
register struct libnet_ifaddr_list *al;
|
116
|
+
struct ifreq *ifr, *lifr, *pifr, nifr;
|
117
|
+
int8_t device[sizeof(nifr.ifr_name)];
|
118
|
+
static struct libnet_ifaddr_list ifaddrlist[MAX_IPADDR];
|
119
|
+
|
120
|
+
char *p;
|
121
|
+
struct ifconf ifc;
|
122
|
+
struct ifreq ibuf[MAX_IPADDR];
|
123
|
+
register int fd, nipaddr;
|
124
|
+
|
125
|
+
#ifdef HAVE_LINUX_PROCFS
|
126
|
+
FILE *fp;
|
127
|
+
char buf[2048];
|
128
|
+
#endif
|
129
|
+
|
130
|
+
fd = socket(AF_INET, SOCK_DGRAM, 0);
|
131
|
+
if (fd < 0)
|
132
|
+
{
|
133
|
+
snprintf(errbuf, LIBNET_ERRBUF_SIZE, "%s(): socket error: %s\n",
|
134
|
+
__func__, strerror(errno));
|
135
|
+
return (-1);
|
136
|
+
}
|
137
|
+
|
138
|
+
#ifdef HAVE_LINUX_PROCFS
|
139
|
+
if ((fp = fopen(PROC_DEV_FILE, "r")) == NULL)
|
140
|
+
{
|
141
|
+
snprintf(errbuf, LIBNET_ERRBUF_SIZE,
|
142
|
+
"%s(): fopen(proc_dev_file) failed: %s\n", __func__,
|
143
|
+
strerror(errno));
|
144
|
+
return (-1);
|
145
|
+
}
|
146
|
+
#endif
|
147
|
+
|
148
|
+
memset(&ifc, 0, sizeof(ifc));
|
149
|
+
ifc.ifc_len = sizeof(ibuf);
|
150
|
+
ifc.ifc_buf = (caddr_t)ibuf;
|
151
|
+
|
152
|
+
if(ioctl(fd, SIOCGIFCONF, &ifc) < 0)
|
153
|
+
{
|
154
|
+
snprintf(errbuf, LIBNET_ERRBUF_SIZE,
|
155
|
+
"%s(): ioctl(SIOCGIFCONF) error: %s\n",
|
156
|
+
__func__, strerror(errno));
|
157
|
+
return(-1);
|
158
|
+
}
|
159
|
+
|
160
|
+
pifr = NULL;
|
161
|
+
lifr = (struct ifreq *)&ifc.ifc_buf[ifc.ifc_len];
|
162
|
+
|
163
|
+
al = ifaddrlist;
|
164
|
+
nipaddr = 0;
|
165
|
+
|
166
|
+
#ifdef HAVE_LINUX_PROCFS
|
167
|
+
while (fgets(buf, sizeof(buf), fp))
|
168
|
+
{
|
169
|
+
if ((p = strchr(buf, ':')) == NULL)
|
170
|
+
{
|
171
|
+
continue;
|
172
|
+
}
|
173
|
+
*p = '\0';
|
174
|
+
for(p = buf; *p == ' '; p++) ;
|
175
|
+
|
176
|
+
strncpy(nifr.ifr_name, p, sizeof(nifr.ifr_name) - 1);
|
177
|
+
nifr.ifr_name[sizeof(nifr.ifr_name) - 1] = '\0';
|
178
|
+
|
179
|
+
#else /* !HAVE_LINUX_PROCFS */
|
180
|
+
|
181
|
+
for (ifr = ifc.ifc_req; ifr < lifr; ifr = NEXTIFR(ifr))
|
182
|
+
{
|
183
|
+
/* XXX LINUX SOLARIS ifalias */
|
184
|
+
if((p = strchr(ifr->ifr_name, ':')))
|
185
|
+
{
|
186
|
+
*p='\0';
|
187
|
+
}
|
188
|
+
if (pifr && strcmp(ifr->ifr_name, pifr->ifr_name) == 0)
|
189
|
+
{
|
190
|
+
continue;
|
191
|
+
}
|
192
|
+
strncpy(nifr.ifr_name, ifr->ifr_name, sizeof(nifr.ifr_name) - 1);
|
193
|
+
nifr.ifr_name[sizeof(nifr.ifr_name) - 1] = '\0';
|
194
|
+
#endif
|
195
|
+
|
196
|
+
/* save device name */
|
197
|
+
strncpy(device, nifr.ifr_name, sizeof(device) - 1);
|
198
|
+
device[sizeof(device) - 1] = '\0';
|
199
|
+
|
200
|
+
if (ioctl(fd, SIOCGIFFLAGS, &nifr) < 0)
|
201
|
+
{
|
202
|
+
pifr = ifr;
|
203
|
+
continue;
|
204
|
+
}
|
205
|
+
if ((nifr.ifr_flags & IFF_UP) == 0)
|
206
|
+
{
|
207
|
+
pifr = ifr;
|
208
|
+
continue;
|
209
|
+
}
|
210
|
+
|
211
|
+
if (dev == NULL && LIBNET_ISLOOPBACK(&nifr))
|
212
|
+
{
|
213
|
+
pifr = ifr;
|
214
|
+
continue;
|
215
|
+
}
|
216
|
+
|
217
|
+
strncpy(nifr.ifr_name, device, sizeof(device) - 1);
|
218
|
+
nifr.ifr_name[sizeof(nifr.ifr_name) - 1] = '\0';
|
219
|
+
if (ioctl(fd, SIOCGIFADDR, (int8_t *)&nifr) < 0)
|
220
|
+
{
|
221
|
+
if (errno != EADDRNOTAVAIL)
|
222
|
+
{
|
223
|
+
snprintf(errbuf, LIBNET_ERRBUF_SIZE,
|
224
|
+
"%s(): SIOCGIFADDR: dev=%s: %s\n", __func__, device,
|
225
|
+
strerror(errno));
|
226
|
+
close(fd);
|
227
|
+
return (-1);
|
228
|
+
}
|
229
|
+
else /* device has no IP address => set to 0 */
|
230
|
+
{
|
231
|
+
al->addr = 0;
|
232
|
+
}
|
233
|
+
}
|
234
|
+
else
|
235
|
+
{
|
236
|
+
al->addr = ((struct sockaddr_in *)&nifr.ifr_addr)->sin_addr.s_addr;
|
237
|
+
}
|
238
|
+
|
239
|
+
if ((al->device = strdup(device)) == NULL)
|
240
|
+
{
|
241
|
+
snprintf(errbuf, LIBNET_ERRBUF_SIZE,
|
242
|
+
"%s(): strdup not enough memory\n", __func__);
|
243
|
+
return(-1);
|
244
|
+
}
|
245
|
+
|
246
|
+
++al;
|
247
|
+
++nipaddr;
|
248
|
+
|
249
|
+
#ifndef HAVE_LINUX_PROCFS
|
250
|
+
pifr = ifr;
|
251
|
+
#endif
|
252
|
+
|
253
|
+
} /* while|for */
|
254
|
+
|
255
|
+
#ifdef HAVE_LINUX_PROCFS
|
256
|
+
if (ferror(fp))
|
257
|
+
{
|
258
|
+
snprintf(errbuf, LIBNET_ERRBUF_SIZE,
|
259
|
+
"%s(): ferror: %s\n", __func__, strerror(errno));
|
260
|
+
return (-1);
|
261
|
+
}
|
262
|
+
fclose(fp);
|
263
|
+
#endif
|
264
|
+
|
265
|
+
*ipaddrp = ifaddrlist;
|
266
|
+
return (nipaddr);
|
267
|
+
}
|
268
|
+
#else
|
269
|
+
/* From tcptraceroute, convert a numeric IP address to a string */
|
270
|
+
#define IPTOSBUFFERS 12
|
271
|
+
static int8_t *iptos(u_int32_t in)
|
272
|
+
{
|
273
|
+
static int8_t output[IPTOSBUFFERS][ 3 * 4 + 3 + 1];
|
274
|
+
static int16_t which;
|
275
|
+
u_int8_t *p;
|
276
|
+
|
277
|
+
p = (u_int8_t *)∈
|
278
|
+
which = (which + 1 == IPTOSBUFFERS ? 0 : which + 1);
|
279
|
+
snprintf(output[which], IPTOSBUFFERS, "%d.%d.%d.%d",
|
280
|
+
p[0], p[1], p[2], p[3]);
|
281
|
+
return output[which];
|
282
|
+
}
|
283
|
+
|
284
|
+
int
|
285
|
+
libnet_ifaddrlist(register struct libnet_ifaddr_list **ipaddrp, int8_t *dev,
|
286
|
+
register int8_t *errbuf)
|
287
|
+
{
|
288
|
+
int nipaddr = 0; int i = 0;
|
289
|
+
|
290
|
+
static struct libnet_ifaddr_list ifaddrlist[MAX_IPADDR];
|
291
|
+
pcap_if_t *alldevs;
|
292
|
+
pcap_if_t *d;
|
293
|
+
int8_t err[PCAP_ERRBUF_SIZE];
|
294
|
+
|
295
|
+
/* Retrieve the interfaces list */
|
296
|
+
if (pcap_findalldevs(&alldevs, err) == -1)
|
297
|
+
{
|
298
|
+
snprintf(errbuf, LIBNET_ERRBUF_SIZE,
|
299
|
+
"%s(): error in pcap_findalldevs: %s\n", __func__, err);
|
300
|
+
return (-1);
|
301
|
+
}
|
302
|
+
|
303
|
+
/* Scan the list printing every entry */
|
304
|
+
for (d = alldevs; d; d = d->next)
|
305
|
+
{
|
306
|
+
if((!d->addresses) || (d->addresses->addr->sa_family != AF_INET))
|
307
|
+
continue;
|
308
|
+
if(d->flags & PCAP_IF_LOOPBACK)
|
309
|
+
continue;
|
310
|
+
|
311
|
+
/* XXX - strdup */
|
312
|
+
ifaddrlist[i].device = strdup(d->name);
|
313
|
+
ifaddrlist[i].addr = (u_int32_t)
|
314
|
+
strdup(iptos(((struct sockaddr_in *)
|
315
|
+
d->addresses->addr)->sin_addr.s_addr));
|
316
|
+
++i;
|
317
|
+
++nipaddr;
|
318
|
+
}
|
319
|
+
|
320
|
+
*ipaddrp = ifaddrlist;
|
321
|
+
return (nipaddr);
|
322
|
+
}
|
323
|
+
#endif /* __WIN32__ */
|
324
|
+
|
325
|
+
int
|
326
|
+
libnet_select_device(libnet_t *l)
|
327
|
+
{
|
328
|
+
int c, i;
|
329
|
+
int8_t err_buf[LIBNET_ERRBUF_SIZE];
|
330
|
+
struct libnet_ifaddr_list *address_list, *al;
|
331
|
+
u_int32_t addr;
|
332
|
+
|
333
|
+
|
334
|
+
if (l == NULL)
|
335
|
+
{
|
336
|
+
return (-1);
|
337
|
+
}
|
338
|
+
|
339
|
+
if (l->device && !isdigit(l->device[0]))
|
340
|
+
{
|
341
|
+
#if !(__WIN32__)
|
342
|
+
if (libnet_check_iface(l) < 0)
|
343
|
+
{
|
344
|
+
/* err msg set in libnet_check_iface() */
|
345
|
+
return (-1);
|
346
|
+
}
|
347
|
+
#endif
|
348
|
+
return (1);
|
349
|
+
}
|
350
|
+
|
351
|
+
/*
|
352
|
+
* Number of interfaces.
|
353
|
+
*/
|
354
|
+
c = libnet_ifaddrlist(&address_list, l->device, err_buf);
|
355
|
+
if (c < 0)
|
356
|
+
{
|
357
|
+
/* err msg set in libnet_ifaddrlist() */
|
358
|
+
return (-1);
|
359
|
+
}
|
360
|
+
else if (c == 0)
|
361
|
+
{
|
362
|
+
snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
|
363
|
+
"%s(): no network interface found\n", __func__);
|
364
|
+
return (-1);
|
365
|
+
}
|
366
|
+
|
367
|
+
al = address_list;
|
368
|
+
if (l->device)
|
369
|
+
{
|
370
|
+
/*
|
371
|
+
* Then we have an IP address in l->device => do lookup
|
372
|
+
*/
|
373
|
+
addr = libnet_name2addr4(l, l->device, 0);
|
374
|
+
|
375
|
+
for (i = c; i; --i, ++address_list)
|
376
|
+
{
|
377
|
+
if (((addr == -1) && !(strncmp(l->device, address_list->device,
|
378
|
+
strlen(l->device)))) ||
|
379
|
+
(address_list->addr == addr))
|
380
|
+
{
|
381
|
+
/* free the "user supplied device" - see libnet_init() */
|
382
|
+
free(l->device);
|
383
|
+
l->device = strdup(address_list->device);
|
384
|
+
goto good;
|
385
|
+
}
|
386
|
+
}
|
387
|
+
if (i <= 0)
|
388
|
+
{
|
389
|
+
snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
|
390
|
+
"%s(): can't find interface for IP %s\n", __func__,
|
391
|
+
l->device);
|
392
|
+
goto bad;
|
393
|
+
}
|
394
|
+
}
|
395
|
+
else
|
396
|
+
{
|
397
|
+
l->device = strdup(address_list->device);
|
398
|
+
}
|
399
|
+
|
400
|
+
good:
|
401
|
+
for (i = 0; i < c; i++)
|
402
|
+
{
|
403
|
+
free(al[i].device);
|
404
|
+
}
|
405
|
+
return (1);
|
406
|
+
|
407
|
+
bad:
|
408
|
+
for (i = 0; i < c; i++)
|
409
|
+
{
|
410
|
+
free(al[i].device);
|
411
|
+
}
|
412
|
+
return (-1);
|
413
|
+
}
|
414
|
+
|
415
|
+
/* EOF */
|
@@ -0,0 +1,234 @@
|
|
1
|
+
/*
|
2
|
+
* $Id: libnet_init.c,v 1.17 2004/03/16 18:40:59 mike Exp $
|
3
|
+
*
|
4
|
+
* libnet
|
5
|
+
* libnet_init.c - Initilization 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
|
+
libnet_t *
|
43
|
+
libnet_init(int injection_type, char *device, char *err_buf)
|
44
|
+
{
|
45
|
+
libnet_t *l = NULL;
|
46
|
+
|
47
|
+
/* corey: remove UID/EUID of 0 restriction */
|
48
|
+
#if defined(__WIN32__)
|
49
|
+
WSADATA wsaData;
|
50
|
+
|
51
|
+
if ((WSAStartup(0x0202, &wsaData)) != 0)
|
52
|
+
{
|
53
|
+
snprintf(err_buf, LIBNET_ERRBUF_SIZE,
|
54
|
+
"%s(): unable to initialize winsock 2\n", __func__);
|
55
|
+
goto bad;
|
56
|
+
}
|
57
|
+
#endif
|
58
|
+
|
59
|
+
l = (libnet_t *)malloc(sizeof (libnet_t));
|
60
|
+
if (l == NULL)
|
61
|
+
{
|
62
|
+
snprintf(err_buf, LIBNET_ERRBUF_SIZE, "%s(): malloc(): %s\n", __func__,
|
63
|
+
strerror(errno));
|
64
|
+
goto bad;
|
65
|
+
}
|
66
|
+
|
67
|
+
memset(l, 0, sizeof (*l));
|
68
|
+
|
69
|
+
l->injection_type = injection_type;
|
70
|
+
l->ptag_state = LIBNET_PTAG_INITIALIZER;
|
71
|
+
l->device = (device ? strdup(device) : NULL);
|
72
|
+
|
73
|
+
strncpy(l->label, LIBNET_LABEL_DEFAULT, LIBNET_LABEL_SIZE);
|
74
|
+
l->label[sizeof(l->label)] = '\0';
|
75
|
+
|
76
|
+
/* corey: move socket operations to libnet_write() */
|
77
|
+
l->fd = -1;
|
78
|
+
|
79
|
+
return (l);
|
80
|
+
|
81
|
+
bad:
|
82
|
+
if (l)
|
83
|
+
{
|
84
|
+
libnet_destroy(l);
|
85
|
+
}
|
86
|
+
return (NULL);
|
87
|
+
}
|
88
|
+
|
89
|
+
void
|
90
|
+
libnet_destroy(libnet_t *l)
|
91
|
+
{
|
92
|
+
if (l)
|
93
|
+
{
|
94
|
+
if (l->fd != -1) {
|
95
|
+
close(l->fd);
|
96
|
+
}
|
97
|
+
if (l->device)
|
98
|
+
{
|
99
|
+
free(l->device);
|
100
|
+
}
|
101
|
+
libnet_clear_packet(l);
|
102
|
+
free(l);
|
103
|
+
}
|
104
|
+
}
|
105
|
+
|
106
|
+
void
|
107
|
+
libnet_clear_packet(libnet_t *l)
|
108
|
+
{
|
109
|
+
libnet_pblock_t *p;
|
110
|
+
libnet_pblock_t *next;
|
111
|
+
|
112
|
+
if (l)
|
113
|
+
{
|
114
|
+
p = l->protocol_blocks;
|
115
|
+
if (p)
|
116
|
+
{
|
117
|
+
for (; p; p = next)
|
118
|
+
{
|
119
|
+
next = p->next;
|
120
|
+
if (p->buf)
|
121
|
+
{
|
122
|
+
free(p->buf);
|
123
|
+
}
|
124
|
+
free(p);
|
125
|
+
}
|
126
|
+
}
|
127
|
+
l->protocol_blocks = NULL;
|
128
|
+
l->total_size = 0;
|
129
|
+
}
|
130
|
+
}
|
131
|
+
|
132
|
+
void
|
133
|
+
libnet_stats(libnet_t *l, struct libnet_stats *ls)
|
134
|
+
{
|
135
|
+
if (l == NULL)
|
136
|
+
{
|
137
|
+
return;
|
138
|
+
}
|
139
|
+
|
140
|
+
ls->packets_sent = l->stats.packets_sent;
|
141
|
+
ls->packet_errors = l->stats.packet_errors;
|
142
|
+
ls->bytes_written = l->stats.bytes_written;
|
143
|
+
}
|
144
|
+
|
145
|
+
int
|
146
|
+
libnet_getfd(libnet_t *l)
|
147
|
+
{
|
148
|
+
if (l == NULL)
|
149
|
+
{
|
150
|
+
return (-1);
|
151
|
+
}
|
152
|
+
|
153
|
+
return (l->fd);
|
154
|
+
}
|
155
|
+
|
156
|
+
int8_t *
|
157
|
+
libnet_getdevice(libnet_t *l)
|
158
|
+
{
|
159
|
+
if (l == NULL)
|
160
|
+
{
|
161
|
+
return (NULL);
|
162
|
+
}
|
163
|
+
|
164
|
+
return (l->device);
|
165
|
+
}
|
166
|
+
|
167
|
+
u_int8_t *
|
168
|
+
libnet_getpbuf(libnet_t *l, libnet_ptag_t ptag)
|
169
|
+
{
|
170
|
+
libnet_pblock_t *p;
|
171
|
+
|
172
|
+
if (l == NULL)
|
173
|
+
{
|
174
|
+
return (NULL);
|
175
|
+
}
|
176
|
+
|
177
|
+
p = libnet_pblock_find(l, ptag);
|
178
|
+
if (p == NULL)
|
179
|
+
{
|
180
|
+
/* err msg set in libnet_pblock_find() */
|
181
|
+
return (NULL);
|
182
|
+
}
|
183
|
+
else
|
184
|
+
{
|
185
|
+
return (p->buf);
|
186
|
+
}
|
187
|
+
}
|
188
|
+
|
189
|
+
u_int32_t
|
190
|
+
libnet_getpbuf_size(libnet_t *l, libnet_ptag_t ptag)
|
191
|
+
{
|
192
|
+
libnet_pblock_t *p;
|
193
|
+
|
194
|
+
if (l == NULL)
|
195
|
+
{
|
196
|
+
return (0);
|
197
|
+
}
|
198
|
+
|
199
|
+
p = libnet_pblock_find(l, ptag);
|
200
|
+
if (p == NULL)
|
201
|
+
{
|
202
|
+
/* err msg set in libnet_pblock_find() */
|
203
|
+
return (0);
|
204
|
+
}
|
205
|
+
else
|
206
|
+
{
|
207
|
+
return (p->b_len);
|
208
|
+
}
|
209
|
+
}
|
210
|
+
|
211
|
+
u_int32_t
|
212
|
+
libnet_getpacket_size(libnet_t *l)
|
213
|
+
{
|
214
|
+
libnet_pblock_t *p;
|
215
|
+
u_int32_t n;
|
216
|
+
|
217
|
+
if (l == NULL)
|
218
|
+
{
|
219
|
+
return (0);
|
220
|
+
}
|
221
|
+
|
222
|
+
n = 0;
|
223
|
+
p = l->protocol_blocks;
|
224
|
+
if (p)
|
225
|
+
{
|
226
|
+
for (; p; p = p->next)
|
227
|
+
{
|
228
|
+
n += p->b_len;
|
229
|
+
}
|
230
|
+
}
|
231
|
+
return (n);
|
232
|
+
}
|
233
|
+
|
234
|
+
/* EOF */
|