]> git.ipfire.org Git - thirdparty/dhcp.git/blob - common/raw.c
Sanitize Solaris build.
[thirdparty/dhcp.git] / 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) 1996-1999 Internet Software Consortium.
20 * Use is subject to license terms which appear in the file named
21 * ISC-LICENSE that should have accompanied this file when you
22 * received it. If a file named ISC-LICENSE did not accompany this
23 * file, or you are not sure the one you have is correct, you may
24 * obtain an applicable copy of the license at:
25 *
26 * http://www.isc.org/isc-license-1.0.html.
27 *
28 * This file is part of the ISC DHCP distribution. The documentation
29 * associated with this file is listed in the file DOCUMENTATION,
30 * included in the top-level directory of this release.
31 *
32 * Support and other services are available for ISC products - see
33 * http://www.isc.org for more information.
34 */
35
36 #ifndef lint
37 static char copyright[] =
38 "$Id: raw.c,v 1.15 1999/03/16 06:37:50 mellon Exp $ Copyright (c) 1995, 1996 The Internet Software Consortium. All rights reserved.\n";
39 #endif /* not lint */
40
41 #include "dhcpd.h"
42
43 #if defined (USE_RAW_SEND)
44 #include <sys/uio.h>
45
46 /* Generic interface registration routine... */
47 void if_register_send (info)
48 struct interface_info *info;
49 {
50 struct sockaddr_in name;
51 int sock;
52 struct socklist *tmp;
53 int flag;
54
55 /* Set up the address we're going to connect to. */
56 name.sin_family = AF_INET;
57 name.sin_port = local_port;
58 name.sin_addr.s_addr = htonl (INADDR_BROADCAST);
59 memset (name.sin_zero, 0, sizeof (name.sin_zero));
60
61 /* List addresses on which we're listening. */
62 if (!quiet_interface_discovery)
63 log_info ("Sending on %s, port %d",
64 piaddr (info -> address), htons (local_port));
65 if ((sock = socket (AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0)
66 log_fatal ("Can't create dhcp socket: %m");
67
68 /* Set the BROADCAST option so that we can broadcast DHCP responses. */
69 flag = 1;
70 if (setsockopt (sock, SOL_SOCKET, SO_BROADCAST,
71 &flag, sizeof flag) < 0)
72 log_fatal ("Can't set SO_BROADCAST option on dhcp socket: %m");
73
74 /* Set the IP_HDRINCL flag so that we can supply our own IP
75 headers... */
76 if (setsockopt (sock, IPPROTO_IP, IP_HDRINCL, &flag, sizeof flag) < 0)
77 log_fatal ("Can't set IP_HDRINCL flag: %m");
78
79 info -> wfdesc = sock;
80 if (!quiet_interface_discovery)
81 log_info ("Sending on Raw/%s%s%s",
82 info -> name,
83 (info -> shared_network ? "/" : ""),
84 (info -> shared_network ?
85 info -> shared_network -> name : ""));
86 }
87
88 size_t send_packet (interface, packet, raw, len, from, to, hto)
89 struct interface_info *interface;
90 struct packet *packet;
91 struct dhcp_packet *raw;
92 size_t len;
93 struct in_addr from;
94 struct sockaddr_in *to;
95 struct hardware *hto;
96 {
97 unsigned char buf [256];
98 int bufp = 0;
99 struct iovec iov [2];
100 int result;
101
102 /* Assemble the headers... */
103 assemble_udp_ip_header (interface, buf, &bufp, from.s_addr,
104 to -> sin_addr.s_addr, to -> sin_port,
105 (unsigned char *)raw, len);
106
107 /* Fire it off */
108 iov [0].iov_base = (char *)buf;
109 iov [0].iov_len = bufp;
110 iov [1].iov_base = (char *)raw;
111 iov [1].iov_len = len;
112
113 result = writev(interface -> wfdesc, iov, 2);
114 if (result < 0)
115 log_error ("send_packet: %m");
116 return result;
117 }
118 #endif /* USE_SOCKET_SEND */