Sync with HEAD.
[dragonfly.git] / contrib / dhcp-3.0 / common / ethernet.c
blobdd7af2b9876df2f72f8b1ae1f2f335d7674eb519
1 /* ethernet.c
3 Packet assembly code, originally contributed by Archie Cobbs. */
5 /*
6 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
7 * Copyright (c) 1996-2003 by Internet Software Consortium
9 * Permission to use, copy, modify, and distribute this software for any
10 * purpose with or without fee is hereby granted, provided that the above
11 * copyright notice and this permission notice appear in all copies.
13 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
19 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 * Internet Systems Consortium, Inc.
22 * 950 Charter Street
23 * Redwood City, CA 94063
24 * <info@isc.org>
25 * http://www.isc.org/
27 * This software has been written for Internet Systems Consortium
28 * by Ted Lemon in cooperation with Vixie Enterprises and Nominum, Inc.
29 * To learn more about Internet Systems Consortium, see
30 * ``http://www.isc.org/''. To learn more about Vixie Enterprises,
31 * see ``http://www.vix.com''. To learn more about Nominum, Inc., see
32 * ``http://www.nominum.com''.
35 #ifndef lint
36 static char copyright[] =
37 "$Id: ethernet.c,v 1.6.2.3 2004/06/10 17:59:17 dhankins Exp $ Copyright (c) 2004 Internet Systems Consortium. All rights reserved.\n";
38 #endif /* not lint */
40 #include "dhcpd.h"
42 #if defined (PACKET_ASSEMBLY) || defined (PACKET_DECODING)
43 #include "includes/netinet/if_ether.h"
44 #endif /* PACKET_ASSEMBLY || PACKET_DECODING */
46 #if defined (PACKET_ASSEMBLY)
47 /* Assemble an hardware header... */
49 void assemble_ethernet_header (interface, buf, bufix, to)
50 struct interface_info *interface;
51 unsigned char *buf;
52 unsigned *bufix;
53 struct hardware *to;
55 struct isc_ether_header eh;
57 if (to && to -> hlen == 7) /* XXX */
58 memcpy (eh.ether_dhost, &to -> hbuf [1],
59 sizeof eh.ether_dhost);
60 else
61 memset (eh.ether_dhost, 0xff, sizeof (eh.ether_dhost));
62 if (interface -> hw_address.hlen - 1 == sizeof (eh.ether_shost))
63 memcpy (eh.ether_shost, &interface -> hw_address.hbuf [1],
64 sizeof (eh.ether_shost));
65 else
66 memset (eh.ether_shost, 0x00, sizeof (eh.ether_shost));
68 eh.ether_type = htons (ETHERTYPE_IP);
70 memcpy (&buf [*bufix], &eh, ETHER_HEADER_SIZE);
71 *bufix += ETHER_HEADER_SIZE;
73 #endif /* PACKET_ASSEMBLY */
75 #ifdef PACKET_DECODING
76 /* Decode a hardware header... */
78 ssize_t decode_ethernet_header (interface, buf, bufix, from)
79 struct interface_info *interface;
80 unsigned char *buf;
81 unsigned bufix;
82 struct hardware *from;
84 struct isc_ether_header eh;
86 memcpy (&eh, buf + bufix, ETHER_HEADER_SIZE);
88 #ifdef USERLAND_FILTER
89 if (ntohs (eh.ether_type) != ETHERTYPE_IP)
90 return -1;
91 #endif
92 memcpy (&from -> hbuf [1], eh.ether_shost, sizeof (eh.ether_shost));
93 from -> hbuf [0] = ARPHRD_ETHER;
94 from -> hlen = (sizeof eh.ether_shost) + 1;
96 return ETHER_HEADER_SIZE;
98 #endif /* PACKET_DECODING */