1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
|
/*
* author: Darren Bounds <dbounds@intrusense.com>
* copyright: Copyright (C) 2002 by Darren Bounds
* license: This software is under GPL version 2 of license
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* packit official page at http://packit.sourceforge.net
*/
#include "shape_arp_hdr.h"
libnet_t *
shape_arp_hdr(libnet_t *pkt_d)
{
u_int32_t i, s_paddr, r_paddr;
u_int8_t s_neaddr[6];
u_int8_t r_neaddr[6];
struct libnet_ether_addr *hw_addr;
#ifdef DEBUG
fprintf(stdout, "DEBUG: shape_arp_hdr()\n");
#endif
hw_addr = malloc(sizeof(struct libnet_ether_addr));
memset(hw_addr, 0, sizeof(struct libnet_ether_addr));
s_paddr = r_paddr = 0;
if(ahdr_o.rand_s_paddr)
ahdr_o.s_paddr = retrieve_rand_ipv4_addr(ahdr_o.s_paddr);
if(ahdr_o.rand_r_paddr)
ahdr_o.r_paddr = retrieve_rand_ipv4_addr(ahdr_o.r_paddr);
if(ahdr_o.rand_s_eaddr)
ahdr_o.s_eaddr = retrieve_rand_ethernet_addr(ahdr_o.s_eaddr);
if(ahdr_o.rand_r_eaddr)
ahdr_o.r_eaddr = retrieve_rand_ethernet_addr(ahdr_o.r_eaddr);
if(ahdr_o.s_paddr == NULL)
{
switch(ahdr_o.op_type)
{
case ARPOP_REQUEST: case ARPOP_REVREQUEST:
if((s_paddr = libnet_get_ipaddr4(pkt_d)) == -1)
fatal_error("Unable to retrieve local IP address: %s", libnet_geterror(pkt_d));
ahdr_o.s_paddr = libnet_addr2name4(s_paddr, 0);
break;
default:
ahdr_o.s_paddr = IPV4_DEFAULT;
break;
}
}
if((s_paddr = libnet_name2addr4(pkt_d, ahdr_o.s_paddr, 0)) == -1)
fatal_error("Invalid sender protocol address: %s", ahdr_o.s_paddr);
if(ahdr_o.s_eaddr == NULL)
{
switch(ahdr_o.op_type)
{
case ARPOP_REQUEST: case ARPOP_REVREQUEST:
if((hw_addr = libnet_get_hwaddr(pkt_d)) == NULL)
fatal_error("Unable to determine ethernet address: %s", libnet_geterror(pkt_d));
for(i = 0; i < 6; i++)
s_neaddr[i] = hw_addr->ether_addr_octet[i];
break;
default:
ahdr_o.s_eaddr = ETH_DEFAULT;
break;
}
}
if(format_ethernet_addr(ahdr_o.s_eaddr, s_neaddr) == 0)
fatal_error("Invalid sender ethernet address");
snprintf(ahdr_o.shw_addr, 18, "%0X:%0X:%0X:%0X:%0X:%0X",
s_neaddr[0], s_neaddr[1], s_neaddr[2], s_neaddr[3], s_neaddr[4], s_neaddr[5]);
if(ahdr_o.r_paddr == NULL)
{
switch(ahdr_o.op_type)
{
case ARPOP_REPLY: case ARPOP_REQUEST:
if((r_paddr = libnet_get_ipaddr4(pkt_d)) == -1)
fatal_error("Unable to retrieve local IP address: %s", libnet_geterror(pkt_d));
ahdr_o.r_paddr = libnet_addr2name4(r_paddr, 0);
break;
default:
ahdr_o.r_paddr = IPV4_DEFAULT;
break;
}
}
if((r_paddr = libnet_name2addr4(pkt_d, ahdr_o.r_paddr, 0)) == -1)
fatal_error("Invalid receiver protocol address: %s", ahdr_o.r_paddr);
if(ahdr_o.r_eaddr == NULL)
{
switch(ahdr_o.op_type)
{
case ARPOP_REPLY: case ARPOP_REVREPLY:
if((hw_addr = libnet_get_hwaddr(pkt_d)) == NULL)
fatal_error("Unable to determine ethernet address: %s", libnet_geterror(pkt_d));
for(i = 0; i < 6; i++)
r_neaddr[i] = hw_addr->ether_addr_octet[i];
break;
default:
ahdr_o.r_eaddr = ETH_DEFAULT;
break;
}
}
if(format_ethernet_addr(ahdr_o.r_eaddr, r_neaddr) == 0)
fatal_error("Invalid receiver ethernet address");
snprintf(ahdr_o.rhw_addr, 18, "%0X:%0X:%0X:%0X:%0X:%0X",
r_neaddr[0], r_neaddr[1], r_neaddr[2], r_neaddr[3], r_neaddr[4], r_neaddr[5]);
if(libnet_build_arp(
ARPHRD_ETHER,
ETHERTYPE_IP,
6,
4,
ahdr_o.op_type,
s_neaddr,
(u_int8_t *)&s_paddr,
r_neaddr,
(u_int8_t *)&r_paddr,
payload,
payload_len,
pkt_d,
0) == -1)
{
fatal_error("Unable to build ARP header: %s", libnet_geterror(pkt_d));
}
return pkt_d;
}
|