diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/Makefile.am | 24 | ||||
-rw-r--r-- | src/common.h | 25 | ||||
-rw-r--r-- | src/libnet-9.def | 176 | ||||
-rw-r--r-- | src/libnet_dll_info.rc | 59 | ||||
-rw-r--r-- | src/libnet_link_bpf.c | 2 | ||||
-rw-r--r-- | src/libnet_link_none.c | 2 | ||||
-rw-r--r-- | src/libnet_link_win32.c | 172 | ||||
-rw-r--r-- | src/libnet_prand.c | 46 | ||||
-rw-r--r-- | src/libnet_resolve.c | 2 | ||||
-rw-r--r-- | src/libnet_write.c | 154 |
10 files changed, 441 insertions, 221 deletions
diff --git a/src/Makefile.am b/src/Makefile.am index f2874a5..d9cadd0 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,4 +1,3 @@ -# $Id: Makefile.am,v 1.6 2004/11/09 07:05:07 mike Exp $ # # Libnet automake information file # Copyright (c) 1998 - 2004 Mike D. Schiffman <mike@infonexus.com> @@ -6,9 +5,8 @@ # # Process this file with automake to produce a Makefile.in script. -include $(top_srcdir)/Makefile.am.common - EXTRA_DIST = libnet_dll.c common.h +AM_CPPFLAGS = -I$(top_srcdir)/include -I$(builddir)/../include lib_LTLIBRARIES = libnet.la @@ -65,7 +63,8 @@ libnet_la_LIBADD = @LTLIBOBJS@ # Note: this version-info should NOT simply be the version of the libnet # package. See: -# http://www.sourceware.org/autobook/autobook/autobook_91.html +# https://www.sourceware.org/autobook/autobook/autobook_61.html#Library-Versioning +# http://www.gnu.org/software/libtool/manual/html_node/Updating-version-info.html # for the rules. # # Summary: @@ -91,9 +90,22 @@ libnet_la_LIBADD = @LTLIBOBJS@ # 1.1.2.1-fork is 5:0:4 # 1.1.3 will be 6:0:5 -> new interfaces, backwards compatible # 1.1.4 was 6:0:5 -> probably an error on my part -# 1.1.5 is 7:0:6 -> new APIs, backwards compat -# 1.1.6 is 8:0:7 -> new APIs, backwards compat +# 1.1.5 is 7:0:6 -> new APIs, backwards compatible +# 1.1.6 is 8:0:7 -> new APIs, backwards compatible # 1.2 is 9:0:0 -> new APIs, removed __libnet_print_vers (internal, should not have been used, but linkable) APIs libnet_la_LDFLAGS = -version-info 9:0:0 +## Windows stuff + +if WIN32 +libnet_la_LDFLAGS += -no-undefined +# don't do any of this if we're not building shared libnet +if COND_SHARED +libnet_la_SOURCES += libnet_dll_info.rc +libnet_la_LDFLAGS += -Wl,--output-def, -Wl,libnet-9.def + +.rc.lo: + $(AM_V_GEN)$(LIBTOOL) --mode=compile --silent $(WINDRES) $< -o $@ +endif +endif
\ No newline at end of file diff --git a/src/common.h b/src/common.h index 358db77..32b36cd 100644 --- a/src/common.h +++ b/src/common.h @@ -29,15 +29,28 @@ #if (_WIN32) || (__CYGWIN__) -/* MSVC warns about snprintf */ -#define _CRT_SECURE_NO_WARNINGS + /* MSVC warns about snprintf */ + #define _CRT_SECURE_NO_WARNINGS + /* don't pull in pcap/bpf.h */ + #define PCAP_DONT_INCLUDE_PCAP_BPF_H + #include <pcap/pcap.h> + + /* don't pull in Packet32's locol bpf definitions */ + // #define BPF_MAJOR_VERSION + #include <Packet32.h> + #include <malloc.h> /* alloca() */ + +#ifdef __MINGW32__ + #include <ntddndis.h> #else + #include <Ntddndis.h> +#endif -#include <assert.h> +#else +#include <assert.h> #include <sys/types.h> - #include <netinet/in.h> /* TODO - should ../include/gnuc.h be included here? */ @@ -46,7 +59,7 @@ #endif -#include "../include/config.h" +#include <config.h> #include "../include/libnet.h" /* IPPROTO_ and sockaddr_ definitions are here. They are often @@ -55,4 +68,4 @@ */ #ifndef IPPROTO_MH #define IPPROTO_MH 135 /* IPv6 mobility header */ -#endif +#endif
\ No newline at end of file diff --git a/src/libnet-9.def b/src/libnet-9.def new file mode 100644 index 0000000..746eb8e --- /dev/null +++ b/src/libnet-9.def @@ -0,0 +1,176 @@ +EXPORTS
+ all_lists @1 DATA
+ gethostbyname2 @2
+ in6addr_error @3 DATA
+ libnet_addr2name4 @4
+ libnet_addr2name4_r @5
+ libnet_addr2name6_r @6
+ libnet_adv_cull_header @7
+ libnet_adv_cull_packet @8
+ libnet_adv_free_packet @9
+ libnet_adv_write_link @10
+ libnet_adv_write_raw_ipv4 @11
+ libnet_autobuild_arp @12
+ libnet_autobuild_ethernet @13
+ libnet_autobuild_fddi @14
+ libnet_autobuild_ipv4 @15
+ libnet_autobuild_ipv6 @16
+ libnet_autobuild_link @17
+ libnet_autobuild_token_ring @18
+ libnet_build_802_1q @19
+ libnet_build_802_1x @20
+ libnet_build_802_2 @21
+ libnet_build_802_2snap @22
+ libnet_build_802_3 @23
+ libnet_build_arp @24
+ libnet_build_asn1_bitstring @25
+ libnet_build_asn1_header @26
+ libnet_build_asn1_int @27
+ libnet_build_asn1_length @28
+ libnet_build_asn1_null @29
+ libnet_build_asn1_objid @30
+ libnet_build_asn1_sequence @31
+ libnet_build_asn1_string @32
+ libnet_build_asn1_uint @33
+ libnet_build_bgp4_header @34
+ libnet_build_bgp4_notification @35
+ libnet_build_bgp4_open @36
+ libnet_build_bgp4_update @37
+ libnet_build_bootpv4 @38
+ libnet_build_cdp @39
+ libnet_build_cdp_value @40
+ libnet_build_data @41
+ libnet_build_dhcpv4 @42
+ libnet_build_dnsv4 @43
+ libnet_build_egre @44
+ libnet_build_ethernet @45
+ libnet_build_fddi @46
+ libnet_build_gre @47
+ libnet_build_gre_last_sre @48
+ libnet_build_gre_sre @49
+ libnet_build_hsrp @50
+ libnet_build_icmpv4_echo @51
+ libnet_build_icmpv4_mask @52
+ libnet_build_icmpv4_redirect @53
+ libnet_build_icmpv4_timeexceed @54
+ libnet_build_icmpv4_timestamp @55
+ libnet_build_icmpv4_unreach @56
+ libnet_build_icmpv6 @57
+ libnet_build_icmpv6_common @58
+ libnet_build_icmpv6_echo @59
+ libnet_build_icmpv6_ndp_nadv @60
+ libnet_build_icmpv6_ndp_nsol @61
+ libnet_build_icmpv6_ndp_opt @62
+ libnet_build_icmpv6_unreach @63
+ libnet_build_igmp @64
+ libnet_build_ipsec_ah @65
+ libnet_build_ipsec_esp_ftr @66
+ libnet_build_ipsec_esp_hdr @67
+ libnet_build_ipv4 @68
+ libnet_build_ipv4_options @69
+ libnet_build_ipv6 @70
+ libnet_build_ipv6_destopts @71
+ libnet_build_ipv6_frag @72
+ libnet_build_ipv6_hbhopts @73
+ libnet_build_ipv6_routing @74
+ libnet_build_isl @75
+ libnet_build_link @76
+ libnet_build_mpls @77
+ libnet_build_ntp @78
+ libnet_build_ospfv2 @79
+ libnet_build_ospfv2_dbd @80
+ libnet_build_ospfv2_hello @81
+ libnet_build_ospfv2_lsa @82
+ libnet_build_ospfv2_lsa_as @83
+ libnet_build_ospfv2_lsa_net @84
+ libnet_build_ospfv2_lsa_rtr @85
+ libnet_build_ospfv2_lsa_sum @86
+ libnet_build_ospfv2_lsr @87
+ libnet_build_ospfv2_lsu @88
+ libnet_build_rip @89
+ libnet_build_rpc_call @90
+ libnet_build_sebek @91
+ libnet_build_snmp @92
+ libnet_build_stp_conf @93
+ libnet_build_stp_tcn @94
+ libnet_build_tcp @95
+ libnet_build_tcp_options @96
+ libnet_build_token_ring @97
+ libnet_build_udp @98
+ libnet_build_vrrp @99
+ libnet_clear_packet @100
+ libnet_close_link_interface @101
+ libnet_close_raw4 @102
+ libnet_close_raw6 @103
+ libnet_compute_crc @104
+ libnet_cq_add @105
+ libnet_cq_destroy @106
+ libnet_cq_end_loop @107
+ libnet_cq_find_by_label @108
+ libnet_cq_getlabel @109
+ libnet_cq_head @110
+ libnet_cq_last @111
+ libnet_cq_next @112
+ libnet_cq_remove @113
+ libnet_cq_remove_by_label @114
+ libnet_cq_size @115
+ libnet_destroy @116
+ libnet_diag_dump_context @117
+ libnet_diag_dump_hex @118
+ libnet_diag_dump_pblock @119
+ libnet_diag_dump_pblock_type @120
+ libnet_do_checksum @121
+ libnet_get_hwaddr @122
+ libnet_get_ipaddr4 @123
+ libnet_get_ipaddr6 @124
+ libnet_get_prand @125
+ libnet_getdevice @126
+ libnet_geterror @127
+ libnet_getfd @128
+ libnet_getgre_length @129
+ libnet_getpacket_size @130
+ libnet_getpbuf @131
+ libnet_getpbuf_size @132
+ libnet_hex_aton @133
+ libnet_ifaddrlist @134
+ libnet_in6_is_error @135
+ libnet_in_cksum @136
+ libnet_inet_checksum @137
+ libnet_init @138
+ libnet_ip_check @139
+ libnet_name2addr4 @140
+ libnet_name2addr6 @141
+ libnet_open_link @142
+ libnet_open_raw4 @143
+ libnet_open_raw6 @144
+ libnet_pblock_append @145
+ libnet_pblock_coalesce @146
+ libnet_pblock_delete @147
+ libnet_pblock_find @148
+ libnet_pblock_insert_before @149
+ libnet_pblock_new @150
+ libnet_pblock_p2p @151
+ libnet_pblock_probe @152
+ libnet_pblock_record_ip_offset @153
+ libnet_pblock_setflags @154
+ libnet_pblock_swap @155
+ libnet_pblock_update @156
+ libnet_plist_chain_dump @157
+ libnet_plist_chain_dump_string @158
+ libnet_plist_chain_free @159
+ libnet_plist_chain_new @160
+ libnet_plist_chain_next_pair @161
+ libnet_seed_prand @162
+ libnet_select_device @163
+ libnet_stats @164
+ libnet_toggle_checksum @165
+ libnet_version @166
+ libnet_win32_build_fake_ethernet @167
+ libnet_win32_build_fake_token @168
+ libnet_win32_get_remote_mac @169
+ libnet_win32_read_arp_table @170
+ libnet_win32_write_raw_ipv4 @171
+ libnet_write @172
+ libnet_write_link @173
+ libnet_write_raw_ipv4 @174
+ libnet_write_raw_ipv6 @175
diff --git a/src/libnet_dll_info.rc b/src/libnet_dll_info.rc new file mode 100644 index 0000000..23f20f3 --- /dev/null +++ b/src/libnet_dll_info.rc @@ -0,0 +1,59 @@ +/* + * + * libnet + * libnet_dll_info.rc - DLL version-information resource + * + * Copyright (c) 2017 Ali Abdulkadir <autostart.ini@gmail.com> + * + * 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. + * + */ + + /* + * FIXME all this needs to be manually updated. Unless someone bothers to + * update libnet_version.c... + */ + +#include <winver.h> + + VS_VERSION_INFO VERSIONINFO + FILEVERSION 1,2,0,0 + PRODUCTVERSION 1,2,0,0 + FILEFLAGSMASK 0x3fL + FILEOS VOS__WINDOWS32 + FILETYPE VFT_DLL + FILESUBTYPE VFT2_DRV_NETWORK + FILEFLAGS 0 + BEGIN + BLOCK "StringFileInfo" + BEGIN + BLOCK "040904b0" + BEGIN + VALUE "Comments", "Provided under a BSD-Style License\0" + VALUE "CompanyName", "https://github.com/sgeto/libnet\0" + VALUE "FileDescription", "Portable Packet Construction and Injection API\0" + VALUE "FileVersion", "1,2,0,0" + VALUE "InternalName", "Libnet\0" + VALUE "LegalCopyright", "Copyright (c) The libnet Developer Community.\0" + VALUE "LegalTrademarks", "\0" + VALUE "OriginalFilename", "libnet-9.dll\0" + VALUE "ProductName", "libnet\0" + VALUE "ProductVersion", "1.2-rc3" + END + END + BLOCK "VarFileInfo" + BEGIN + VALUE "Translation", 0x0, 1200 + END + END + /* EOF */
\ No newline at end of file diff --git a/src/libnet_link_bpf.c b/src/libnet_link_bpf.c index 3743c83..fbdb642 100644 --- a/src/libnet_link_bpf.c +++ b/src/libnet_link_bpf.c @@ -46,7 +46,7 @@ #include <net/if_types.h> #include "../include/gnuc.h" -#include <bpf.h> +#include "../include/bpf.h" #ifdef HAVE_OS_PROTO_H #include "../include/os-proto.h" diff --git a/src/libnet_link_none.c b/src/libnet_link_none.c index 8be5fb4..faf7bff 100644 --- a/src/libnet_link_none.c +++ b/src/libnet_link_none.c @@ -16,7 +16,7 @@ * distributions including binary code include the above copyright notice and * this paragraph in its entirety in the documentation or other materials * provided with the distribution, and (3) all advertising materials mentioning - * features or use of this software display the following acknowledgement: + * features or use of this software display the following acknowledgment: * ``This product includes software developed by the University of California, * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of * the University nor the names of its contributors may be used to endorse diff --git a/src/libnet_link_win32.c b/src/libnet_link_win32.c index 738b10b..5f89680 100644 --- a/src/libnet_link_win32.c +++ b/src/libnet_link_win32.c @@ -1,6 +1,4 @@ /* - * $Id: libnet_link_win32.c,v 1.16 2004/02/18 18:19:00 mike Exp $ - * * libnet * libnet_link_win32.c - low-level win32 libwpcap routines * @@ -32,21 +30,78 @@ * */ -/* MSVC warns about snprintf. This needs to be defined before the declaration of _snprintf is seen. */ -#define _CRT_SECURE_NO_WARNINGS - /* Libnet's unnamespaced ICMP6_ macros stomp on the enumerated versions of these names in the MS headers, so pre-include this header. */ + #include <winsock2.h> #include <iphlpapi.h> /* From the Microsoft Platform SDK */ +#include <iprtrmib.h> +#include <assert.h> #include "common.h" -#include <winsock2.h> -#include <assert.h> -#include <Packet32.h> -#include <Ntddndis.h> -#include "iprtrmib.h" +/* + * These are the types that are the same on all platforms, and that + * have been defined by <net/bpf.h> for ages. + */ + +#ifndef DLT_NULL +#define DLT_NULL 0 /* BSD loopback encapsulation */ +#endif + +#ifndef DLT_EN10MB +#define DLT_EN10MB 1 /* Ethernet (10Mb) */ +#endif + +#ifndef DLT_EN3MB +#define DLT_EN3MB 2 /* Experimental Ethernet (3Mb) */ +#endif + +#ifndef DLT_AX25 +#define DLT_AX25 3 /* Amateur Radio AX.25 */ +#endif + +#ifndef DLT_PRONET +#define DLT_PRONET 4 /* Proteon ProNET Token Ring */ +#endif + +#ifndef DLT_CHAOS +#define DLT_CHAOS 5 /* Chaos */ +#endif + +#ifndef DLT_IEEE802 +#define DLT_IEEE802 6 /* 802.5 Token Ring */ +#endif + +#ifndef DLT_ARCNET +#define DLT_ARCNET 7 /* ARCNET, with BSD-style header */ +#endif + +#ifndef DLT_SLIP +#define DLT_SLIP 8 /* Serial Line IP */ +#endif + +#ifndef DLT_PPP +#define DLT_PPP 9 /* Point-to-point Protocol */ +#endif + +#ifndef DLT_FDDI +#define DLT_FDDI 10 /* FDDI */ +#endif + +/* + * These are types that are different on some platforms, and that + * have been defined by <net/bpf.h> for ages. We use #ifdefs to + * detect the BSDs that define them differently from the traditional + * libpcap <net/bpf.h> + * + * XXX - DLT_ATM_RFC1483 is 13 in BSD/OS, and DLT_RAW is 14 in BSD/OS, + * but I don't know what the right #define is for BSD/OS. + */ + +#ifndef DLT_ATM_RFC1483 +#define DLT_ATM_RFC1483 11 /* LLC-encapsulated ATM */ +#endif int libnet_open_link(libnet_t *l) @@ -55,9 +110,9 @@ libnet_open_link(libnet_t *l) NetType IFType; if (l == NULL) - { + { return (-1); - } + } if (l->device == NULL) { @@ -75,10 +130,10 @@ libnet_open_link(libnet_t *l) dwErrorCode=GetLastError(); snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, "%s(): unable to open the driver, error Code : %lx", - __func__, dwErrorCode); + __func__, dwErrorCode); return (-1); } - + /* increase the send buffer */ PacketSetBuff(l->lpAdapter, 512000); @@ -144,29 +199,18 @@ libnet_close_link_interface(libnet_t *l) } int -libnet_write_link(libnet_t *l, const uint8_t *packet, uint32_t size) +libnet_write_link(libnet_t *l, const uint8_t *data, uint32_t size) { - LPPACKET lpPacket; - DWORD BytesTransfered; + PACKET pkt; + DWORD BytesTransfered = -1; - BytesTransfered = -1; + /* Packet* arguments aren't const, but aren't actually modified. + */ + PacketInitPacket(&pkt, (PVOID)data, size); - if ((lpPacket = PacketAllocatePacket()) == NULL) - { - snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, - "%s(): failed to allocate the LPPACKET structure", __func__); - return (-1); - } - /* FIXME Packet* arguments aren't const, are they actually modified? That would be a problem, we can't modify our input */ - PacketInitPacket(lpPacket, packet, size); + if (PacketSendPacket(l->lpAdapter, &pkt, TRUE)) + BytesTransfered = size; - /* PacketSendPacket returns a BOOLEAN */ - if(PacketSendPacket(l->lpAdapter, lpPacket, TRUE)) - { - BytesTransfered = size; - } - - PacketFreePacket(lpPacket); return (BytesTransfered); } @@ -177,18 +221,18 @@ libnet_get_hwaddr(libnet_t *l) ULONG IoCtlBufferLength = (sizeof(PACKET_OID_DATA) + sizeof(ULONG) - 1); PPACKET_OID_DATA OidData; - + int i = 0; if (l == NULL) - { + { return (NULL); - } + } if (l->device == NULL) - { + { if (libnet_select_device(l) == -1) - { + { snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, "%s(): can't figure out a device to use", __func__); return (NULL); @@ -196,7 +240,7 @@ libnet_get_hwaddr(libnet_t *l) } OidData = (struct _PACKET_OID_DATA *) malloc(IoCtlBufferLength); - + if (OidData == NULL) { snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, @@ -206,13 +250,13 @@ libnet_get_hwaddr(libnet_t *l) if (l->link_type == DLT_IEEE802) { - OidData->Oid = OID_802_5_CURRENT_ADDRESS; + OidData->Oid = OID_802_5_CURRENT_ADDRESS; } else { - OidData->Oid = OID_802_3_CURRENT_ADDRESS; + OidData->Oid = OID_802_3_CURRENT_ADDRESS; } - + OidData->Length = 6; if((PacketRequest(l->lpAdapter, FALSE, OidData)) == FALSE) { @@ -243,12 +287,12 @@ libnet_win32_get_remote_mac(libnet_t *l, DWORD DestIP) static BYTE bcastmac[]= {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF}; BYTE *MAC = libnet_win32_read_arp_table(DestIP); - + if (MAC==NULL) { memset(pulMac, 0xff, sizeof (pulMac)); memset(&sin, 0, sizeof(sin)); - + if((hr = SendARP (DestIP, 0, pulMac, &ulLen)) != NO_ERROR) { *(int32_t *)&sin.sin_addr = DestIP; @@ -272,7 +316,7 @@ libnet_win32_get_remote_mac(libnet_t *l, DWORD DestIP) return(bcastmac); /* ff:ff:ff:ff:ff:ff */ } } - + pbHexMac = (PBYTE) pulMac; return (pbHexMac); @@ -286,26 +330,23 @@ libnet_win32_get_remote_mac(libnet_t *l, DWORD DestIP) BYTE * libnet_win32_read_arp_table(DWORD DestIP) { static BYTE buffMAC[6]; - BOOL fOrder=TRUE; - BYTE *MAC=NULL; - DWORD status, i, ci; - - PMIB_IPNETTABLE pIpNetTable = NULL; + BOOL fOrder = TRUE; + DWORD status; + + MIB_IPNETTABLE *pIpNetTable = NULL; DWORD Size = 0; - - memset(buffMAC, 0, sizeof(buffMAC)); - if((status = GetIpNetTable(pIpNetTable, &Size, fOrder)) == ERROR_INSUFFICIENT_BUFFER) + memset(buffMAC, 0, sizeof(buffMAC)); + status = GetIpNetTable(NULL, &Size, fOrder); + if (status == ERROR_INSUFFICIENT_BUFFER) { - pIpNetTable = (PMIB_IPNETTABLE) malloc(Size); - assert(pIpNetTable); + pIpNetTable = alloca(Size); status = GetIpNetTable(pIpNetTable, &Size, fOrder); } - if(status == NO_ERROR) + if (status == NO_ERROR) { - /* set current interface */ - ci = pIpNetTable->table[0].dwIndex; + DWORD i, ci = pIpNetTable->table[0].dwIndex; /* set current interface */ for (i = 0; i < pIpNetTable->dwNumEntries; ++i) { @@ -315,23 +356,10 @@ BYTE * libnet_win32_read_arp_table(DWORD DestIP) if(pIpNetTable->table[i].dwAddr == DestIP) /* found IP in arp cache */ { memcpy(buffMAC, pIpNetTable->table[i].bPhysAddr, sizeof(buffMAC)); - free(pIpNetTable); return buffMAC; - } + } } - - if (pIpNetTable) - free (pIpNetTable); - return(NULL); } - else - { - if (pIpNetTable) - { - free (pIpNetTable); - } - MAC=NULL; - } return(NULL); } diff --git a/src/libnet_prand.c b/src/libnet_prand.c index d62a24c..915a4d6 100644 --- a/src/libnet_prand.c +++ b/src/libnet_prand.c @@ -1,6 +1,4 @@ /* - * $Id: libnet_prand.c,v 1.7 2004/01/28 19:45:00 mike Exp $ - * * libnet * libnet_prand.c - pseudo-random number generation * @@ -29,24 +27,33 @@ * SUCH DAMAGE. * */ - + #include "common.h" +#ifdef _WIN32 +#include <wincrypt.h> +#include <time.h> + +#else +#include <sys/time.h> /* gettimeofday() */ +#endif + + int libnet_seed_prand(libnet_t *l) { - #if !(__WIN32__) +#ifndef WIN32 struct timeval seed; - #endif +#endif if (l == NULL) - { + { return (-1); - } + } - #if __WIN32__ +#ifdef WIN32 srand((unsigned)time(NULL)); - #else +#else if (gettimeofday(&seed, NULL) == -1) { snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, @@ -58,7 +65,7 @@ libnet_seed_prand(libnet_t *l) * More entropy then just seeding with time(2). */ srandom((unsigned)(seed.tv_sec ^ seed.tv_usec)); - #endif +#endif return (1); } @@ -71,17 +78,16 @@ uint32_t libnet_get_prand(int mod) { uint32_t n; /* 0 to 4,294,967,295 */ -#ifndef _WIN32 +#ifndef WIN32 n = random(); #else - HCRYPTPROV hProv = 0; - CryptAcquireContext(&hProv, - 0, 0, PROV_RSA_FULL, - CRYPT_VERIFYCONTEXT); - - CryptGenRandom(hProv, - sizeof(n), (BYTE*)&n); - CryptReleaseContext(hProv, 0); + HCRYPTPROV hProv = 0; + + CryptAcquireContext(&hProv, + 0, 0, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT); + + CryptGenRandom(hProv, sizeof(n), (BYTE*)&n); + CryptReleaseContext(hProv, 0); #endif switch (mod) { @@ -101,4 +107,4 @@ libnet_get_prand(int mod) return (0); /* NOTTREACHED */ } -/* EOF */ +/* EOF */
\ No newline at end of file diff --git a/src/libnet_resolve.c b/src/libnet_resolve.c index a654520..2f9040b 100644 --- a/src/libnet_resolve.c +++ b/src/libnet_resolve.c @@ -1,6 +1,4 @@ /* - * $Id: libnet_resolve.c,v 1.21 2004/11/09 07:05:07 mike Exp $ - * * libnet * libnet_resolve.c - various name resolution type routines * diff --git a/src/libnet_write.c b/src/libnet_write.c index 5c447ae..f9a60e8 100644 --- a/src/libnet_write.c +++ b/src/libnet_write.c @@ -1,12 +1,10 @@ /* - * $Id: libnet_write.c,v 1.14 2004/11/09 07:05:07 mike Exp $ - * * libnet * libnet_write.c - writes a prebuilt packet to the network * * Copyright (c) 1998 - 2004 Mike D. Schiffman <mike@infonexus.com> * All rights reserved. - * win32 specific code + * win32 specific code * Copyright (c) 2002 - 2003 Roberto Larcher <roberto.larcher@libero.it> * * Redistribution and use in source and binary forms, with or without @@ -34,11 +32,6 @@ #include "common.h" -#if (_WIN32) || (__CYGWIN__) -#include "packet32.h" -#include "Ntddndis.h" -#endif - int libnet_write(libnet_t *l) { @@ -47,7 +40,7 @@ libnet_write(libnet_t *l) uint8_t *packet = NULL; if (l == NULL) - { + { return (-1); } @@ -120,9 +113,9 @@ done: #if defined (__WIN32__) libnet_ptag_t -libnet_win32_build_fake_ethernet(uint8_t *dst, uint8_t *src, uint16_t type, -const uint8_t *payload, uint32_t payload_s, uint8_t *packet, libnet_t *l, -libnet_ptag_t ptag) +libnet_win32_build_fake_ethernet (uint8_t *dst, uint8_t *src, uint16_t type, + const uint8_t *payload, uint32_t payload_s, + uint8_t *packet, libnet_t *l, libnet_ptag_t ptag) { struct libnet_ethernet_hdr eth_hdr; @@ -131,7 +124,7 @@ libnet_ptag_t ptag) return (-1); } - memset(ð_hdr, 0, sizeof(eth_hdr)); + memset(ð_hdr, 0, sizeof(eth_hdr)); eth_hdr.ether_type = htons(type); memcpy(eth_hdr.ether_dhost, dst, ETHER_ADDR_LEN); /* destination address */ memcpy(eth_hdr.ether_shost, src, ETHER_ADDR_LEN); /* source address */ @@ -149,28 +142,28 @@ libnet_ptag_t ptag) } libnet_ptag_t -libnet_win32_build_fake_token(uint8_t *dst, uint8_t *src, uint16_t type, -const uint8_t *payload, uint32_t payload_s, uint8_t *packet, libnet_t *l, -libnet_ptag_t ptag) +libnet_win32_build_fake_token (uint8_t *dst, uint8_t *src, uint16_t type, + const uint8_t *payload, uint32_t payload_s, + uint8_t *packet, libnet_t *l, libnet_ptag_t ptag) { struct libnet_token_ring_hdr token_ring_hdr; - + if (!packet) { return (-1); } - memset(&token_ring_hdr, 0, sizeof(token_ring_hdr)); + memset(&token_ring_hdr, 0, sizeof(token_ring_hdr)); token_ring_hdr.token_ring_access_control = 0x10; token_ring_hdr.token_ring_frame_control = 0x40; token_ring_hdr.token_ring_llc_dsap = 0xaa; token_ring_hdr.token_ring_llc_ssap = 0xaa; token_ring_hdr.token_ring_llc_control_field = 0x03; token_ring_hdr.token_ring_type = htons(type); - memcpy(token_ring_hdr.token_ring_dhost, dst, ETHER_ADDR_LEN); - memcpy(token_ring_hdr.token_ring_shost, libnet_get_hwaddr(l), - ETHER_ADDR_LEN); - + memcpy(token_ring_hdr.token_ring_dhost, dst, ETHER_ADDR_LEN); + memcpy(token_ring_hdr.token_ring_shost, libnet_get_hwaddr(l), + ETHER_ADDR_LEN); + if (payload && payload_s) { /* @@ -185,45 +178,38 @@ libnet_ptag_t ptag) int libnet_win32_write_raw_ipv4(libnet_t *l, const uint8_t *payload, uint32_t payload_s) -{ +{ static BYTE dst[ETHER_ADDR_LEN]; static BYTE src[ETHER_ADDR_LEN]; - - uint8_t *packet = NULL; + + uint8_t *packet; uint32_t packet_s; - LPPACKET lpPacket = NULL; - DWORD remoteip = 0; - DWORD BytesTransfered; + DWORD remoteip = 0; NetType type; struct libnet_ipv4_hdr *ip_hdr = NULL; - + memset(dst, 0, sizeof(dst)); memset(src, 0, sizeof(src)); packet_s = payload_s + l->link_offset; - packet = (uint8_t *)malloc(packet_s); - if (packet == NULL) - { - snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, - "%s(): failed to allocate packet", __func__); - return (-1); - } - /* FIXME all the return paths below, except the last, leak 'packet' */ + packet = (uint8_t*) alloca(packet_s); - /* we have to do the IP checksum */ - /* FIXME MSVC warning is correct, checksum modifies its input. Fix is to build checksum inside the allocated 'packet' */ - if (libnet_inet_checksum(l, payload, IPPROTO_IP, LIBNET_IPV4_H, payload, payload+payload_s) == -1) + /* we have to do the IP checksum + * FIXME: warning is correct, checksum modifies its input. + * Fix is to build checksum inside the allocated 'packet' + */ + if (libnet_inet_checksum(l, (uint8_t*)payload, IPPROTO_IP, LIBNET_IPV4_H, payload, payload+payload_s) == -1) { /* error msg set in libnet_do_checksum */ return (-1); - } + } /* MACs, IPs and other stuff... */ ip_hdr = (struct libnet_ipv4_hdr *)payload; memcpy(src, libnet_get_hwaddr(l), sizeof(src)); remoteip = ip_hdr->ip_dst.S_un.S_addr; - + /* check if the remote station is the local station */ if (remoteip == libnet_get_ipaddr4(l)) { @@ -235,7 +221,7 @@ libnet_win32_write_raw_ipv4(libnet_t *l, const uint8_t *payload, uint32_t payloa } PacketGetNetType(l->lpAdapter, &type); - + switch(type.LinkType) { case NdisMedium802_3: @@ -256,29 +242,8 @@ libnet_win32_write_raw_ipv4(libnet_t *l, const uint8_t *payload, uint32_t payloa "%s(): network type (%d) is not supported", __func__, type.LinkType); return (-1); - break; - } - - BytesTransfered = -1; - if ((lpPacket = PacketAllocatePacket()) == NULL) - { - snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, - "%s(): failed to allocate the LPPACKET structure", __func__); - return (-1); - } - - PacketInitPacket(lpPacket, packet, packet_s); - - /* PacketSendPacket returns a BOOLEAN */ - if (PacketSendPacket(l->lpAdapter, lpPacket, TRUE)) - { - BytesTransfered = packet_s; } - - PacketFreePacket(lpPacket); - free(packet); - - return (BytesTransfered); + return libnet_write_link (l, packet, packet_s); } int @@ -304,9 +269,9 @@ libnet_write_raw_ipv4(libnet_t *l, const uint8_t *packet, uint32_t size) struct libnet_ipv4_hdr *ip_hdr; if (l == NULL) - { + { return (-1); - } + } ip_hdr = (struct libnet_ipv4_hdr *)packet; @@ -314,7 +279,7 @@ libnet_write_raw_ipv4(libnet_t *l, const uint8_t *packet, uint32_t size) /* * For link access, we don't need to worry about the inconsistencies of * certain BSD kernels. However, raw socket nuances abound. Certain - * BSD implmentations require the ip_len and ip_off fields to be in host + * BSD implementations require the ip_len and ip_off fields to be in host * byte order. */ ip_hdr->ip_len = FIX(ip_hdr->ip_len); @@ -324,30 +289,6 @@ libnet_write_raw_ipv4(libnet_t *l, const uint8_t *packet, uint32_t size) memset(&sin, 0, sizeof(sin)); sin.sin_family = AF_INET; sin.sin_addr.s_addr = ip_hdr->ip_dst.s_addr; -#if (__WIN32__) - /* set port for TCP */ - /* - * XXX - should first check to see if there's a pblock for a TCP - * header, if not we can use a dummy value for the port. - */ - if (ip_hdr->ip_p == 6) - { - struct libnet_tcp_hdr *tcph_p = - (struct libnet_tcp_hdr *)(packet + (ip_hdr->ip_hl << 2)); - sin.sin_port = tcph_p->th_dport; - } - /* set port for UDP */ - /* - * XXX - should first check to see if there's a pblock for a UDP - * header, if not we can use a dummy value for the port. - */ - else if (ip_hdr->ip_p == 17) - { - struct libnet_udp_hdr *udph_p = - (struct libnet_udp_hdr *)(packet + (ip_hdr->ip_hl << 2)); - sin.sin_port = udph_p->uh_dport; - } -#endif /* __WIN32__ */ c = sendto(l->fd, packet, size, 0, (struct sockaddr *)&sin, sizeof(sin)); @@ -359,15 +300,9 @@ libnet_write_raw_ipv4(libnet_t *l, const uint8_t *packet, uint32_t size) if (c != size) { -#if !(__WIN32__) snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, "%s(): %d bytes written (%s)", __func__, c, strerror(errno)); -#else /* __WIN32__ */ - snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, - "%s(): %d bytes written (%d)", __func__, c, - WSAGetLastError()); -#endif /* !__WIN32__ */ } return (c); } @@ -375,20 +310,19 @@ libnet_write_raw_ipv4(libnet_t *l, const uint8_t *packet, uint32_t size) int libnet_write_raw_ipv6(libnet_t *l, const uint8_t *packet, uint32_t size) { + int c = -1; + #if defined HAVE_SOLARIS && !defined HAVE_SOLARIS_IPV6 snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, "%s(): no IPv6 support", __func__, strerror(errno)); - return (-1); -} #else - int c; struct sockaddr_in6 sin; struct libnet_ipv6_hdr *ip_hdr; if (l == NULL) - { + { return (-1); - } + } ip_hdr = (struct libnet_ipv6_hdr *)packet; @@ -396,22 +330,16 @@ libnet_write_raw_ipv6(libnet_t *l, const uint8_t *packet, uint32_t size) sin.sin6_family = AF_INET6; memcpy(sin.sin6_addr.s6_addr, ip_hdr->ip_dst.libnet_s6_addr, sizeof(ip_hdr->ip_dst.libnet_s6_addr)); - + c = sendto(l->fd, packet, size, 0, (struct sockaddr *)&sin, sizeof(sin)); if (c != size) { -#if !(__WIN32__) snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, "%s(): %d bytes written (%s)", __func__, c, strerror(errno)); -#else /* __WIN32__ */ - snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, - "%s(): %d bytes written (%d)", __func__, c, - WSAGetLastError()); -#endif /* !__WIN32__ */ } +#endif /* HAVE_SOLARIS && !HAVE_SOLARIS_IPV6 */ return (c); } -#endif -#endif -/* EOF */ +#endif /* __WIN32__ */ +/* EOF */
\ No newline at end of file |