--- embedaddon/libnet/src/libnet_write.c 2013/07/22 11:54:42 1.1.1.2 +++ embedaddon/libnet/src/libnet_write.c 2023/09/27 11:11:38 1.1.1.3 @@ -1,12 +1,10 @@ /* - * $Id: libnet_write.c,v 1.1.1.2 2013/07/22 11:54:42 misho Exp $ - * * libnet * libnet_write.c - writes a prebuilt packet to the network * * Copyright (c) 1998 - 2004 Mike D. Schiffman * All rights reserved. - * win32 specific code + * win32 specific code * Copyright (c) 2002 - 2003 Roberto Larcher * * Redistribution and use in source and binary forms, with or without @@ -32,22 +30,8 @@ * */ -#include -#include -#include +#include "common.h" -#if (HAVE_CONFIG_H) -#include "../include/config.h" -#endif -#if (!(_WIN32) || (__CYGWIN__)) -#include "../include/libnet.h" -#else -#include "../include/win32/libnet.h" -#include "../include/win32/config.h" -#include "packet32.h" -#include "Ntddndis.h" -#endif - int libnet_write(libnet_t *l) { @@ -56,7 +40,7 @@ libnet_write(libnet_t *l) uint8_t *packet = NULL; if (l == NULL) - { + { return (-1); } @@ -76,7 +60,7 @@ libnet_write(libnet_t *l) if (len > LIBNET_MAX_PACKET) { snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, - "%s(): packet is too large (%d bytes)\n", + "%s(): packet is too large (%d bytes)", __func__, len); goto done; } @@ -92,7 +76,7 @@ libnet_write(libnet_t *l) break; default: snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, - "%s(): unsuported injection type\n", __func__); + "%s(): unsupported injection type", __func__); goto done; } @@ -129,9 +113,9 @@ done: #if defined (__WIN32__) libnet_ptag_t -libnet_win32_build_fake_ethernet(uint8_t *dst, uint8_t *src, uint16_t type, -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; @@ -140,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 */ @@ -158,28 +142,28 @@ libnet_ptag_t ptag) } libnet_ptag_t -libnet_win32_build_fake_token(uint8_t *dst, uint8_t *src, uint16_t type, -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) { /* @@ -194,43 +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) + packet = (uint8_t*) alloca(packet_s); + + /* 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) { - snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, - "%s(): failed to allocate packet\n", __func__); - return (-1); - } - - /* we have to do the IP checksum */ - if (libnet_do_checksum(l, payload, IPPROTO_IP, LIBNET_IPV4_H) == -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)) { @@ -242,7 +221,7 @@ libnet_win32_write_raw_ipv4(libnet_t *l, const uint8_t } PacketGetNetType(l->lpAdapter, &type); - + switch(type.LinkType) { case NdisMedium802_3: @@ -260,32 +239,11 @@ libnet_win32_write_raw_ipv4(libnet_t *l, const uint8_t case NdisMediumArcnet878_2: default: snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, - "%s(): network type (%d) is not supported\n", __func__, + "%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\n", __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 @@ -311,9 +269,9 @@ libnet_write_raw_ipv4(libnet_t *l, const uint8_t *pack struct libnet_ipv4_hdr *ip_hdr; if (l == NULL) - { + { return (-1); - } + } ip_hdr = (struct libnet_ipv4_hdr *)packet; @@ -321,7 +279,7 @@ libnet_write_raw_ipv4(libnet_t *l, const uint8_t *pack /* * 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); @@ -331,30 +289,6 @@ libnet_write_raw_ipv4(libnet_t *l, const uint8_t *pack 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)); @@ -366,15 +300,9 @@ libnet_write_raw_ipv4(libnet_t *l, const uint8_t *pack if (c != size) { -#if !(__WIN32__) snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, - "%s(): %d bytes written (%s)\n", __func__, c, + "%s(): %d bytes written (%s)", __func__, c, strerror(errno)); -#else /* __WIN32__ */ - snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, - "%s(): %d bytes written (%d)\n", __func__, c, - WSAGetLastError()); -#endif /* !__WIN32__ */ } return (c); } @@ -382,20 +310,19 @@ libnet_write_raw_ipv4(libnet_t *l, const uint8_t *pack 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\n", + 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; @@ -403,22 +330,22 @@ libnet_write_raw_ipv6(libnet_t *l, const uint8_t *pack 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)\n", __func__, c, + "%s(): %d bytes written (%s)", __func__, c, strerror(errno)); -#else /* __WIN32__ */ - snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, - "%s(): %d bytes written (%d)\n", __func__, c, - WSAGetLastError()); -#endif /* !__WIN32__ */ } +#endif /* HAVE_SOLARIS && !HAVE_SOLARIS_IPV6 */ return (c); } -#endif -#endif -/* EOF */ +#endif /* __WIN32__ */ + +/** + * Local Variables: + * indent-tabs-mode: nil + * c-file-style: "stroustrup" + * End: + */