summaryrefslogtreecommitdiff
path: root/src/libnet_write.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/libnet_write.c')
-rw-r--r--src/libnet_write.c154
1 files changed, 41 insertions, 113 deletions
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(&eth_hdr, 0, sizeof(eth_hdr));
+ memset(&eth_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