Annotation of embedaddon/libnet/src/libnet_build_ethernet.c, revision 1.1
1.1 ! misho 1: /*
! 2: * $Id: libnet_build_ethernet.c,v 1.8 2004/01/03 20:31:01 mike Exp $
! 3: *
! 4: * libnet
! 5: * libnet_build_ethernet.c - ethernet packet assembler
! 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_ptag_t
! 43: libnet_build_ethernet(u_int8_t *dst, u_int8_t *src, u_int16_t type, u_int8_t *payload,
! 44: u_int32_t payload_s, libnet_t *l, libnet_ptag_t ptag)
! 45: {
! 46: u_int32_t n, h;
! 47: libnet_pblock_t *p;
! 48: struct libnet_ethernet_hdr eth_hdr;
! 49:
! 50: if (l == NULL)
! 51: {
! 52: return (-1);
! 53: }
! 54:
! 55: /* sanity check injection type if we're not in advanced mode */
! 56: if (l->injection_type != LIBNET_LINK &&
! 57: !(((l->injection_type) & LIBNET_ADV_MASK)))
! 58: {
! 59: snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
! 60: "%s(): called with non-link layer wire injection primitive",
! 61: __func__);
! 62: p = NULL;
! 63: goto bad;
! 64: }
! 65:
! 66: n = LIBNET_ETH_H + payload_s;
! 67: h = 0;
! 68:
! 69: /*
! 70: * Find the existing protocol block if a ptag is specified, or create
! 71: * a new one.
! 72: */
! 73: p = libnet_pblock_probe(l, ptag, n, LIBNET_PBLOCK_ETH_H);
! 74: if (p == NULL)
! 75: {
! 76: return (-1);
! 77: }
! 78:
! 79: memset(ð_hdr, 0, sizeof(eth_hdr));
! 80: memcpy(eth_hdr.ether_dhost, dst, ETHER_ADDR_LEN); /* destination address */
! 81: memcpy(eth_hdr.ether_shost, src, ETHER_ADDR_LEN); /* source address */
! 82: eth_hdr.ether_type = htons(type); /* packet type */
! 83:
! 84: n = libnet_pblock_append(l, p, (u_int8_t *)ð_hdr, LIBNET_ETH_H);
! 85: if (n == -1)
! 86: {
! 87: goto bad;
! 88: }
! 89:
! 90: if ((payload && !payload_s) || (!payload && payload_s))
! 91: {
! 92: snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
! 93: "%s(): payload inconsistency\n", __func__);
! 94: goto bad;
! 95: }
! 96:
! 97: if (payload && payload_s)
! 98: {
! 99: n = libnet_pblock_append(l, p, payload, payload_s);
! 100: if (n == -1)
! 101: {
! 102: goto bad;
! 103: }
! 104: }
! 105:
! 106: return (ptag ? ptag : libnet_pblock_update(l, p, h, LIBNET_PBLOCK_ETH_H));
! 107: bad:
! 108: libnet_pblock_delete(l, p);
! 109: return (-1);
! 110: }
! 111:
! 112:
! 113: libnet_ptag_t
! 114: libnet_autobuild_ethernet(u_int8_t *dst, u_int16_t type, libnet_t *l)
! 115: {
! 116: u_int32_t n, h;
! 117: struct libnet_ether_addr *src;
! 118: libnet_pblock_t *p;
! 119: libnet_ptag_t ptag;
! 120: struct libnet_ethernet_hdr eth_hdr;
! 121:
! 122: if (l == NULL)
! 123: {
! 124: return (-1);
! 125: }
! 126:
! 127: /* sanity check injection type if we're not in advanced mode */
! 128: if (l->injection_type != LIBNET_LINK &&
! 129: !(((l->injection_type) & LIBNET_ADV_MASK)))
! 130: {
! 131: snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
! 132: "libnet_autobuild_ethernet() called with non-link layer wire"
! 133: " injection primitive");
! 134: p = NULL;
! 135: goto bad;
! 136: }
! 137:
! 138: n = LIBNET_ETH_H;
! 139: h = 0;
! 140: ptag = LIBNET_PTAG_INITIALIZER;
! 141: src = libnet_get_hwaddr(l);
! 142: if (src == NULL)
! 143: {
! 144: /* err msg set in libnet_get_hwaddr() */
! 145: return (-1);
! 146: }
! 147:
! 148: /*
! 149: * Create a new pblock.
! 150: */
! 151: p = libnet_pblock_probe(l, ptag, n, LIBNET_PBLOCK_ETH_H);
! 152: if (p == NULL)
! 153: {
! 154: return (-1);
! 155: }
! 156:
! 157: memset(ð_hdr, 0, sizeof(eth_hdr));
! 158: memcpy(eth_hdr.ether_dhost, dst, ETHER_ADDR_LEN); /* destination address */
! 159: memcpy(eth_hdr.ether_shost, src, ETHER_ADDR_LEN); /* source address */
! 160: eth_hdr.ether_type = htons(type); /* packet type */
! 161:
! 162: n = libnet_pblock_append(l, p, (u_int8_t *)ð_hdr, LIBNET_ETH_H);
! 163: if (n == -1)
! 164: {
! 165: goto bad;
! 166: }
! 167:
! 168: return (libnet_pblock_update(l, p, h, LIBNET_PBLOCK_ETH_H));
! 169: bad:
! 170: libnet_pblock_delete(l, p);
! 171: return (-1);
! 172: }
! 173: /* EOF */
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>