2 * The olsr.org Optimized Link-State Routing daemon(olsrd)
3 * Copyright (c) 2004, Andreas Tønnesen(andreto@olsr.org)
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
16 * * Neither the name of olsr.org, olsrd nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
33 * Visit http://www.olsr.org for more information.
35 * If you find this software useful feel free to make a donation
36 * to the project. For more information see the website or contact
37 * the copyright holders.
39 * $Id: interfaces.c,v 1.24 2005/05/25 14:32:52 br1 Exp $
43 #include "interfaces.h"
45 #include "scheduler.h"
48 static olsr_u32_t if_property_id;
50 /* Ifchange functions */
53 int (*function)(struct interface *, int);
57 static struct ifchgf *ifchgf_list;
60 *Do initialization of various data needed for
61 *network interface management.
62 *This function also tries to set up the given interfaces.
64 *@return the number of interfaces configured
69 struct olsr_if *tmp_if;
76 *Initializing addrsock struct to be
77 *used on all the sockets
79 if(olsr_cnf->ip_version == AF_INET)
82 memset(&addrsock, 0, sizeof (addrsock));
83 addrsock.sin_family = AF_INET;
84 addrsock.sin_port = olsr_udp_port;
85 (addrsock.sin_addr).s_addr = INADDR_ANY;
90 memset(&addrsock6, 0, sizeof (addrsock6));
91 addrsock6.sin6_family = AF_INET6;
92 addrsock6.sin6_port = olsr_udp_port;
93 //(addrsock6.sin6_addr).s_addr = IN6ADDR_ANY_INIT;
96 OLSR_PRINTF(1, "\n ---- Interface configuration ---- \n\n")
97 /* Run trough all interfaces immedeatly */
98 for(tmp_if = olsr_cnf->interfaces; tmp_if != NULL; tmp_if = tmp_if->next)
100 chk_if_up(tmp_if, 1);
103 /* register network interface update function with scheduler */
104 olsr_register_scheduler_event(&check_interface_updates, NULL, IFCHANGES_POLL_INT, 0, NULL);
106 return (ifnet == NULL) ? 0 : 1;
113 return if_property_id++;
117 add_if_geninfo(struct interface *ifp, void *data, olsr_u32_t owner_id)
119 struct if_gen_property *igp;
121 if(get_if_geninfo(ifp, owner_id) != NULL)
124 igp = olsr_malloc(sizeof(struct if_gen_property), __func__);
126 igp->owner_id = owner_id;
130 igp->next = ifp->gen_properties;
131 ifp->gen_properties = igp;
137 get_if_geninfo(struct interface *ifp, olsr_u32_t owner_id)
139 struct if_gen_property *igp_list = ifp->gen_properties;
144 if(igp_list->owner_id == owner_id)
145 return igp_list->data;
147 igp_list = igp_list->next;
154 del_if_geninfo(struct interface *ifp, olsr_u32_t owner_id)
157 struct if_gen_property *igp_list = ifp->gen_properties;
158 struct if_gen_property *igp_prev = NULL;
163 if(igp_list->owner_id == owner_id)
167 igp_list = igp_list->next;
178 ifp->gen_properties = igp_list->next;
182 igp_prev->next = igp_list->next;
184 data = igp_list->data;
192 run_ifchg_cbs(struct interface *ifp, int flag)
194 struct ifchgf *tmp_ifchgf_list = ifchgf_list;
196 while(tmp_ifchgf_list != NULL)
198 tmp_ifchgf_list->function(ifp, flag);
199 tmp_ifchgf_list = tmp_ifchgf_list->next;
205 *Find the local interface with a given address.
207 *@param addr the address to check.
209 *@return the interface struct representing the interface
210 *that matched the address.
214 if_ifwithaddr(union olsr_ip_addr *addr)
216 struct interface *ifp;
221 for (ifp = ifnet; ifp; ifp = ifp->int_next)
223 if(olsr_cnf->ip_version == AF_INET)
226 //printf("Checking: %s == ", inet_ntoa(((struct sockaddr_in *)&ifp->int_addr)->sin_addr));
227 //printf("%s\n", olsr_ip_to_string(addr));
229 if (COMP_IP(&((struct sockaddr_in *)&ifp->int_addr)->sin_addr, addr))
235 //printf("Checking %s ", olsr_ip_to_string((union olsr_ip_addr *)&ifp->int6_addr.sin6_addr));
236 //printf("== %s\n", olsr_ip_to_string((union olsr_ip_addr *)&((struct sockaddr_in6 *)addr)->sin6_addr));
237 if (COMP_IP(&ifp->int6_addr.sin6_addr, addr))
247 *Find the interface with a given number.
249 *@param nr the number of the interface to find.
251 *@return return the interface struct representing the interface
252 *that matched the number.
255 if_ifwithsock(int fd)
257 struct interface *ifp;
262 if (ifp->olsr_socket == fd)
272 *Find the interface with a given label.
274 *@param if_name the label of the interface to find.
276 *@return return the interface struct representing the interface
277 *that matched the label.
280 if_ifwithname(const char *if_name)
282 struct interface *ifp;
287 /* good ol' strcmp should be sufficcient here */
288 if (!strcmp(ifp->int_name, if_name))
298 *Create a new interf_name struct using a given
299 *name and insert it into the interface list.
301 *@param name the name of the interface.
309 struct olsr_if *interf_n = olsr_cnf->interfaces;
311 //printf("Adding interface %s\n", name);
313 /* check if the inerfaces already exists */
314 while(interf_n != NULL)
316 if(memcmp(interf_n->name, name, strlen(name)) == 0)
318 fprintf(stderr, "Duplicate interfaces defined... not adding %s\n", name);
321 interf_n = interf_n->next;
324 interf_n = olsr_malloc(sizeof(struct olsr_if), "queue interface");
326 /* strlen () does not return length including terminating /0 */
327 interf_n->name = olsr_malloc(strlen(name) + 1, "queue interface name");
328 interf_n->cnf = NULL;
329 interf_n->interf = NULL;
330 interf_n->configured = 0;
331 interf_n->index = olsr_cnf->ifcnt++;
333 strncpy(interf_n->name, name, strlen(name) + 1);
334 interf_n->next = olsr_cnf->interfaces;
335 olsr_cnf->interfaces = interf_n;
342 *Add an ifchange function. These functions are called on all (non-initial)
343 *changes in the interface set.
350 add_ifchgf(int (*f)(struct interface *, int))
353 struct ifchgf *new_ifchgf;
355 new_ifchgf = olsr_malloc(sizeof(struct ifchgf), "Add ifchgfunction");
357 new_ifchgf->next = ifchgf_list;
358 new_ifchgf->function = f;
360 ifchgf_list = new_ifchgf;
368 * Remove an ifchange function
371 del_ifchgf(int (*f)(struct interface *, int))
373 struct ifchgf *tmp_ifchgf, *prev;
375 tmp_ifchgf = ifchgf_list;
380 if(tmp_ifchgf->function == f)
385 ifchgf_list = tmp_ifchgf->next;
390 prev->next = tmp_ifchgf->next;
396 tmp_ifchgf = tmp_ifchgf->next;