revert between 56095 -> 55830 in arch
[AROS.git] / workbench / network / stacks / AROSTCP / dhcp / common / socket.c
blob30162777594d61c13ba5ba56ad3044b5b3fcc90c
1 /* socket.c
3 BSD socket interface code... */
5 /*
6 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
7 * Copyright (c) 1995-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 /* SO_BINDTODEVICE support added by Elliot Poger (poger@leland.stanford.edu).
36 * This sockopt allows a socket to be bound to a particular interface,
37 * thus enabling the use of DHCPD on a multihomed host.
38 * If SO_BINDTODEVICE is defined in your system header files, the use of
39 * this sockopt will be automatically enabled.
40 * I have implemented it under Linux; other systems should be doable also.
43 #if 0
44 static char copyright[] =
45 "$Id$ Copyright (c) 2004 Internet Systems Consortium. All rights reserved.\n";
46 #endif
48 #include "dhcpd.h"
50 #ifdef USE_SOCKET_FALLBACK
51 # if !defined (USE_SOCKET_SEND)
52 # define if_register_send if_register_fallback
53 # define send_packet send_fallback
54 # define if_reinitialize_send if_reinitialize_fallback
55 # endif
56 #endif
58 static int once = 0;
60 /* Reinitializes the specified interface after an address change. This
61 is not required for packet-filter APIs. */
63 #if defined (USE_SOCKET_SEND) || defined (USE_SOCKET_FALLBACK)
64 void if_reinitialize_send (info)
65 struct interface_info *info;
67 #if 0
68 #ifndef USE_SOCKET_RECEIVE /* ***CHECK HERE*** */
69 once = 0;
70 #ifdef SOCKET_IS_NOT_A_FILE
71 CloseSocket (info -> wfdesc);
72 #else
73 close (info -> wfdesc);
74 #endif
75 #endif
76 if_register_send (info);
77 #endif
79 #endif
81 #ifdef USE_SOCKET_RECEIVE
82 void if_reinitialize_receive (info)
83 struct interface_info *info;
85 #if 0
86 once = 0;
87 #ifdef SOCKET_IS_NOT_A_FILE
88 CloseSocket (info -> rfdesc);
89 #else
90 close (info -> rfdesc);
91 #endif
92 if_register_receive (info);
93 #endif
95 #endif
97 #if defined (USE_SOCKET_SEND) || \
98 defined (USE_SOCKET_RECEIVE) || \
99 defined (USE_SOCKET_FALLBACK)
100 /* Generic interface registration routine... */
101 int if_register_socket (info)
102 struct interface_info *info;
104 struct sockaddr_in name;
105 int sock;
106 int flag;
108 #if !defined (HAVE_SO_BINDTODEVICE) && !defined (USE_FALLBACK)
109 /* Make sure only one interface is registered. */
110 if (once)
111 log_fatal ("The standard socket API can only support %s",
112 "hosts with a single network interface.");
113 once = 1;
114 #endif
116 memset (&name, 0, sizeof (name));
117 /* Set up the address we're going to bind to. */
118 name.sin_family = AF_INET;
119 name.sin_port = local_port;
120 name.sin_addr = local_address;
122 /* Make a socket... */
123 if ((sock = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
124 log_fatal ("Can't create dhcp socket: %m");
126 /* Set the REUSEADDR option so that we don't fail to start if
127 we're being restarted. */
128 flag = 1;
129 if (setsockopt (sock, SOL_SOCKET, SO_REUSEADDR,
130 (char *)&flag, sizeof flag) < 0)
131 log_fatal ("Can't set SO_REUSEADDR option on dhcp socket: %m");
133 /* Set the BROADCAST option so that we can broadcast DHCP responses.
134 We shouldn't do this for fallback devices, and we can detect that
135 a device is a fallback because it has no ifp structure. */
136 if (info -> ifp &&
137 (setsockopt (sock, SOL_SOCKET, SO_BROADCAST,
138 (char *)&flag, sizeof flag) < 0))
139 log_fatal ("Can't set SO_BROADCAST option on dhcp socket: %m");
141 /* Bind the socket to this interface's IP address. */
142 if (bind (sock, (struct sockaddr *)&name, sizeof name) < 0) {
143 log_error ("Can't bind to dhcp address: %m");
144 log_error ("Please make sure there is no other dhcp server");
145 log_error ("running and that there's no entry for dhcp or");
146 log_error ("bootp in /etc/inetd.conf. Also make sure you");
147 log_error ("are not running HP JetAdmin software, which");
148 log_fatal ("includes a bootp server.");
151 #if defined (HAVE_SO_BINDTODEVICE)
152 /* Bind this socket to this interface. */
153 if (info -> ifp &&
154 setsockopt (sock, SOL_SOCKET, SO_BINDTODEVICE,
155 (char *)(info -> ifp), sizeof *(info -> ifp)) < 0) {
156 log_fatal ("setsockopt: SO_BINDTODEVICE: %m");
158 #endif
160 return sock;
162 #endif /* USE_SOCKET_SEND || USE_SOCKET_RECEIVE || USE_SOCKET_FALLBACK */
164 #if defined (USE_SOCKET_SEND) || defined (USE_SOCKET_FALLBACK)
165 void if_register_send (info)
166 struct interface_info *info;
168 #ifndef USE_SOCKET_RECEIVE
169 info -> wfdesc = if_register_socket (info);
170 #if defined (USE_SOCKET_FALLBACK)
171 /* Fallback only registers for send, but may need to receive as
172 well. */
173 info -> rfdesc = info -> wfdesc;
174 #endif
175 #else
176 info -> wfdesc = info -> rfdesc;
177 #endif
178 if (!quiet_interface_discovery)
179 log_info ("Sending on Socket/%s%s%s",
180 info -> name,
181 (info -> shared_network ? "/" : ""),
182 (info -> shared_network ?
183 info -> shared_network -> name : ""));
186 #if defined (USE_SOCKET_SEND)
187 void if_deregister_send (info)
188 struct interface_info *info;
190 #ifndef USE_SOCKET_RECEIVE
191 #ifdef SOCKET_IS_NOT_A_FILE /* ***CHECK HERE*** */
192 CloseSocket(info -> wfdesc);
193 #else
194 close (info -> wfdesc);
195 #endif
196 #endif
197 info -> wfdesc = -1;
199 if (!quiet_interface_discovery)
200 log_info ("Disabling output on Socket/%s%s%s",
201 info -> name,
202 (info -> shared_network ? "/" : ""),
203 (info -> shared_network ?
204 info -> shared_network -> name : ""));
206 #endif /* USE_SOCKET_SEND */
207 #endif /* USE_SOCKET_SEND || USE_SOCKET_FALLBACK */
209 #ifdef USE_SOCKET_RECEIVE
210 void if_register_receive (info)
211 struct interface_info *info;
213 /* If we're using the socket API for sending and receiving,
214 we don't need to register this interface twice. */
215 info -> rfdesc = if_register_socket (info);
216 if (!quiet_interface_discovery)
217 log_info ("Listening on Socket/%s%s%s",
218 info -> name,
219 (info -> shared_network ? "/" : ""),
220 (info -> shared_network ?
221 info -> shared_network -> name : ""));
224 void if_deregister_receive (info)
225 struct interface_info *info;
227 #ifdef SOCKET_IS_NOT_A_FILE
228 CloseSocket (info -> rfdesc);
229 #else
230 close (info -> rfdesc);
231 #endif
232 info -> rfdesc = -1;
234 if (!quiet_interface_discovery)
235 log_info ("Disabling input on Socket/%s%s%s",
236 info -> name,
237 (info -> shared_network ? "/" : ""),
238 (info -> shared_network ?
239 info -> shared_network -> name : ""));
241 #endif /* USE_SOCKET_RECEIVE */
243 #if defined (USE_SOCKET_SEND) || defined (USE_SOCKET_FALLBACK)
244 ssize_t send_packet (interface, packet, raw, len, from, to, hto)
245 struct interface_info *interface;
246 struct packet *packet;
247 struct dhcp_packet *raw;
248 size_t len;
249 struct in_addr from;
250 struct sockaddr_in *to;
251 struct hardware *hto;
253 int result;
254 #ifdef IGNORE_HOSTUNREACH
255 int retry = 0;
256 do {
257 #endif
258 result = sendto (interface -> wfdesc, (char *)raw, len, 0,
259 (struct sockaddr *)to, sizeof *to);
260 #ifdef IGNORE_HOSTUNREACH
261 } while (to -> sin_addr.s_addr == htonl (INADDR_BROADCAST) &&
262 result < 0 &&
263 (errno == EHOSTUNREACH ||
264 errno == ECONNREFUSED) &&
265 retry++ < 10);
266 #endif
267 if (result < 0) {
268 log_error ("send_packet: %m");
269 if (errno == ENETUNREACH)
270 log_error ("send_packet: please consult README file%s",
271 " regarding broadcast address.");
273 return result;
275 #endif /* USE_SOCKET_SEND || USE_SOCKET_FALLBACK */
277 #ifdef USE_SOCKET_RECEIVE
278 ssize_t receive_packet (interface, buf, len, from, hfrom)
279 struct interface_info *interface;
280 unsigned char *buf;
281 size_t len;
282 struct sockaddr_in *from;
283 struct hardware *hfrom;
285 SOCKLEN_T flen = sizeof *from;
286 int result;
288 #ifdef IGNORE_HOSTUNREACH
289 int retry = 0;
290 do {
291 #endif
292 result = recvfrom (interface -> rfdesc, (char *)buf, len, 0,
293 (struct sockaddr *)from, &flen);
294 #ifdef IGNORE_HOSTUNREACH
295 } while (result < 0 &&
296 (errno == EHOSTUNREACH ||
297 errno == ECONNREFUSED) &&
298 retry++ < 10);
299 #endif
300 return result;
302 #endif /* USE_SOCKET_RECEIVE */
304 #if defined (USE_SOCKET_FALLBACK)
305 /* This just reads in a packet and silently discards it. */
307 isc_result_t fallback_discard (object)
308 omapi_object_t *object;
310 char buf [1540];
311 struct sockaddr_in from;
312 SOCKLEN_T flen = sizeof from;
313 int status;
314 struct interface_info *interface;
316 if (object -> type != dhcp_type_interface)
317 return ISC_R_INVALIDARG;
318 interface = (struct interface_info *)object;
320 status = recvfrom (interface -> wfdesc, buf, sizeof buf, 0,
321 (struct sockaddr *)&from, &flen);
322 #if defined (DEBUG)
323 /* Only report fallback discard errors if we're debugging. */
324 if (status < 0) {
325 log_error ("fallback_discard: %m");
326 return ISC_R_UNEXPECTED;
328 #endif
329 return ISC_R_SUCCESS;
331 #endif /* USE_SOCKET_FALLBACK */
333 #if defined (USE_SOCKET_SEND)
334 int can_unicast_without_arp (ip)
335 struct interface_info *ip;
337 return 0;
340 int can_receive_unicast_unconfigured (ip)
341 struct interface_info *ip;
343 #if defined (SOCKET_CAN_RECEIVE_UNICAST_UNCONFIGURED)
344 return 1;
345 #else
346 return 0;
347 #endif
350 int supports_multiple_interfaces (ip)
351 struct interface_info *ip;
353 #if defined (SO_BINDTODEVICE)
354 return 1;
355 #else
356 return 0;
357 #endif
360 /* If we have SO_BINDTODEVICE, set up a fallback interface; otherwise,
361 do not. */
363 void maybe_setup_fallback ()
365 #if defined (USE_SOCKET_FALLBACK)
366 isc_result_t status;
367 struct interface_info *fbi = (struct interface_info *)0;
368 if (setup_fallback (&fbi, MDL)) {
369 fbi -> wfdesc = if_register_socket (fbi);
370 fbi -> rfdesc = fbi -> wfdesc;
371 log_info ("Sending on Socket/%s%s%s",
372 fbi -> name,
373 (fbi -> shared_network ? "/" : ""),
374 (fbi -> shared_network ?
375 fbi -> shared_network -> name : ""));
377 status = omapi_register_io_object ((omapi_object_t *)fbi,
378 if_readsocket, 0,
379 fallback_discard, 0, 0);
380 if (status != ISC_R_SUCCESS)
381 log_fatal ("Can't register I/O handle for %s: %s",
382 fbi -> name, isc_result_totext (status));
383 interface_dereference (&fbi, MDL);
385 #endif
387 #endif /* USE_SOCKET_SEND */