Merge from vendor branch BIND:
[dragonfly.git] / contrib / dhcp-3.0 / common / raw.c
1 /* socket.c
2
3    BSD raw socket interface code... */
4
5 /* XXX
6
7    It's not clear how this should work, and that lack of clarity is
8    terribly detrimental to the NetBSD 1.1 kernel - it crashes and
9    burns.
10
11    Using raw sockets ought to be a big win over using BPF or something
12    like it, because you don't need to deal with the complexities of
13    the physical layer, but it appears not to be possible with existing
14    raw socket implementations.  This may be worth revisiting in the
15    future.  For now, this code can probably be considered a curiosity.
16    Sigh. */
17
18 /*
19  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
20  * Copyright (c) 1995-2003 by Internet Software Consortium
21  *
22  * Permission to use, copy, modify, and distribute this software for any
23  * purpose with or without fee is hereby granted, provided that the above
24  * copyright notice and this permission notice appear in all copies.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
27  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
28  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
29  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
30  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
31  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
32  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
33  *
34  *   Internet Systems Consortium, Inc.
35  *   950 Charter Street
36  *   Redwood City, CA 94063
37  *   <info@isc.org>
38  *   http://www.isc.org/
39  *
40  * This software has been written for Internet Systems Consortium
41  * by Ted Lemon in cooperation with Vixie Enterprises and Nominum, Inc.
42  * To learn more about Internet Systems Consortium, see
43  * ``http://www.isc.org/''.  To learn more about Vixie Enterprises,
44  * see ``http://www.vix.com''.   To learn more about Nominum, Inc., see
45  * ``http://www.nominum.com''.
46  */
47
48 #ifndef lint
49 static char copyright[] =
50 "$Id: raw.c,v 1.17.2.2 2004/06/10 17:59:20 dhankins Exp $ Copyright (c) 2004 Internet Systems Consortium.  All rights reserved.\n";
51 #endif /* not lint */
52
53 #include "dhcpd.h"
54
55 #if defined (USE_RAW_SEND)
56 #include <sys/uio.h>
57
58 /* Generic interface registration routine... */
59 void if_register_send (info)
60         struct interface_info *info;
61 {
62         struct sockaddr_in name;
63         int sock;
64         struct socklist *tmp;
65         int flag;
66
67         /* Set up the address we're going to connect to. */
68         name.sin_family = AF_INET;
69         name.sin_port = local_port;
70         name.sin_addr.s_addr = htonl (INADDR_BROADCAST);
71         memset (name.sin_zero, 0, sizeof (name.sin_zero));
72
73         /* List addresses on which we're listening. */
74         if (!quiet_interface_discovery)
75                 log_info ("Sending on %s, port %d",
76                       piaddr (info -> address), htons (local_port));
77         if ((sock = socket (AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0)
78                 log_fatal ("Can't create dhcp socket: %m");
79
80         /* Set the BROADCAST option so that we can broadcast DHCP responses. */
81         flag = 1;
82         if (setsockopt (sock, SOL_SOCKET, SO_BROADCAST,
83                         &flag, sizeof flag) < 0)
84                 log_fatal ("Can't set SO_BROADCAST option on dhcp socket: %m");
85
86         /* Set the IP_HDRINCL flag so that we can supply our own IP
87            headers... */
88         if (setsockopt (sock, IPPROTO_IP, IP_HDRINCL, &flag, sizeof flag) < 0)
89                 log_fatal ("Can't set IP_HDRINCL flag: %m");
90
91         info -> wfdesc = sock;
92         if (!quiet_interface_discovery)
93                 log_info ("Sending on   Raw/%s%s%s",
94                       info -> name,
95                       (info -> shared_network ? "/" : ""),
96                       (info -> shared_network ?
97                        info -> shared_network -> name : ""));
98 }
99
100 void if_deregister_send (info)
101         struct interface_info *info;
102 {
103         close (info -> wfdesc);
104         info -> wfdesc = -1;
105
106         if (!quiet_interface_discovery)
107                 log_info ("Disabling output on Raw/%s%s%s",
108                       info -> name,
109                       (info -> shared_network ? "/" : ""),
110                       (info -> shared_network ?
111                        info -> shared_network -> name : ""));
112 }
113
114 size_t send_packet (interface, packet, raw, len, from, to, hto)
115         struct interface_info *interface;
116         struct packet *packet;
117         struct dhcp_packet *raw;
118         size_t len;
119         struct in_addr from;
120         struct sockaddr_in *to;
121         struct hardware *hto;
122 {
123         unsigned char buf [256];
124         int bufp = 0;
125         struct iovec iov [2];
126         int result;
127
128         /* Assemble the headers... */
129         assemble_udp_ip_header (interface, buf, &bufp, from.s_addr,
130                                 to -> sin_addr.s_addr, to -> sin_port,
131                                 (unsigned char *)raw, len);
132
133         /* Fire it off */
134         iov [0].iov_base = (char *)buf;
135         iov [0].iov_len = bufp;
136         iov [1].iov_base = (char *)raw;
137         iov [1].iov_len = len;
138
139         result = writev(interface -> wfdesc, iov, 2);
140         if (result < 0)
141                 log_error ("send_packet: %m");
142         return result;
143 }
144 #endif /* USE_SOCKET_SEND */