common/ethernet.c

Go to the documentation of this file.
00001 /* ethernet.c
00002 
00003    Packet assembly code, originally contributed by Archie Cobbs. */
00004 
00005 /*
00006  * Copyright (c) 2004,2007,2009,2014 by Internet Systems Consortium, Inc. ("ISC")
00007  * Copyright (c) 1996-2003 by Internet Software Consortium
00008  *
00009  * Permission to use, copy, modify, and distribute this software for any
00010  * purpose with or without fee is hereby granted, provided that the above
00011  * copyright notice and this permission notice appear in all copies.
00012  *
00013  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
00014  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
00015  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
00016  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
00017  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
00018  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
00019  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
00020  *
00021  *   Internet Systems Consortium, Inc.
00022  *   950 Charter Street
00023  *   Redwood City, CA 94063
00024  *   <info@isc.org>
00025  *   https://www.isc.org/
00026  *
00027  */
00028 
00029 #include "dhcpd.h"
00030 
00031 #if defined (PACKET_ASSEMBLY) || defined (PACKET_DECODING)
00032 #include "includes/netinet/if_ether.h"
00033 #endif /* PACKET_ASSEMBLY || PACKET_DECODING */
00034 
00035 #if defined (PACKET_ASSEMBLY)
00036 /* Assemble an hardware header... */
00037 
00038 void assemble_ethernet_header (interface, buf, bufix, to)
00039         struct interface_info *interface;
00040         unsigned char *buf;
00041         unsigned *bufix;
00042         struct hardware *to;
00043 {
00044         struct isc_ether_header eh;
00045 
00046         if (to && to -> hlen == 7) /* XXX */
00047                 memcpy (eh.ether_dhost, &to -> hbuf [1],
00048                         sizeof eh.ether_dhost);
00049         else
00050                 memset (eh.ether_dhost, 0xff, sizeof (eh.ether_dhost));
00051         if (interface -> hw_address.hlen - 1 == sizeof (eh.ether_shost))
00052                 memcpy (eh.ether_shost, &interface -> hw_address.hbuf [1],
00053                         sizeof (eh.ether_shost));
00054         else
00055                 memset (eh.ether_shost, 0x00, sizeof (eh.ether_shost));
00056 
00057         eh.ether_type = htons (ETHERTYPE_IP);
00058 
00059         memcpy (&buf [*bufix], &eh, ETHER_HEADER_SIZE);
00060         *bufix += ETHER_HEADER_SIZE;
00061 }
00062 #endif /* PACKET_ASSEMBLY */
00063 
00064 #ifdef PACKET_DECODING
00065 /* Decode a hardware header... */
00066 
00067 ssize_t decode_ethernet_header (interface, buf, bufix, from)
00068      struct interface_info *interface;
00069      unsigned char *buf;
00070      unsigned bufix;
00071      struct hardware *from;
00072 {
00073   struct isc_ether_header eh;
00074 
00075   memcpy (&eh, buf + bufix, ETHER_HEADER_SIZE);
00076 
00077 #ifdef USERLAND_FILTER
00078   if (ntohs (eh.ether_type) != ETHERTYPE_IP)
00079           return -1;
00080 #endif
00081   memcpy (&from -> hbuf [1], eh.ether_shost, sizeof (eh.ether_shost));
00082   from -> hbuf [0] = ARPHRD_ETHER;
00083   from -> hlen = (sizeof eh.ether_shost) + 1;
00084 
00085   return ETHER_HEADER_SIZE;
00086 }
00087 #endif /* PACKET_DECODING */

Generated on 5 Apr 2014 for ISC DHCP by  doxygen 1.6.1