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.12 2004/11/21 11:28:56 kattemat Exp $
43 #include "interfaces.h"
45 #include "scheduler.h"
48 *Do initialization of various data needed for
49 *network interface management.
50 *This function also tries to set up the given interfaces.
52 *@return the number of interfaces configured
57 struct olsr_if *tmp_if;
64 *Initializing addrsock struct to be
65 *used on all the sockets
67 if(olsr_cnf->ip_version == AF_INET)
70 memset(&addrsock, 0, sizeof (addrsock));
71 addrsock.sin_family = AF_INET;
72 addrsock.sin_port = olsr_udp_port;
73 (addrsock.sin_addr).s_addr = INADDR_ANY;
78 memset(&addrsock6, 0, sizeof (addrsock6));
79 addrsock6.sin6_family = AF_INET6;
80 addrsock6.sin6_port = olsr_udp_port;
81 //(addrsock6.sin6_addr).s_addr = IN6ADDR_ANY_INIT;
84 olsr_printf(1, "\n ---- Interface configuration ---- \n\n");
85 /* Run trough all interfaces immedeatly */
86 for(tmp_if = olsr_cnf->interfaces; tmp_if != NULL; tmp_if = tmp_if->next)
91 /* register network interface update function with scheduler */
92 olsr_register_scheduler_event(&check_interface_updates, NULL, 5.0, 0, NULL);
94 return (ifnet == NULL) ? 0 : 1;
100 *Find the local interface with a given address.
102 *@param addr the address to check.
104 *@return the interface struct representing the interface
105 *that matched the address.
109 if_ifwithaddr(union olsr_ip_addr *addr)
111 struct interface *ifp;
113 for (ifp = ifnet; ifp; ifp = ifp->int_next)
115 if(olsr_cnf->ip_version == AF_INET)
118 //printf("Checking: %s == ", inet_ntoa(((struct sockaddr_in *)&ifp->int_addr)->sin_addr));
119 //printf("%s\n", olsr_ip_to_string(addr));
121 if (COMP_IP(&((struct sockaddr_in *)&ifp->int_addr)->sin_addr, addr))
127 //printf("Checking %s ", olsr_ip_to_string((union olsr_ip_addr *)&ifp->int6_addr.sin6_addr));
128 //printf("== %s\n", olsr_ip_to_string((union olsr_ip_addr *)&((struct sockaddr_in6 *)addr)->sin6_addr));
129 if (COMP_IP(&ifp->int6_addr.sin6_addr, addr))
139 *Find the interface with a given number.
141 *@param nr the number of the interface to find.
143 *@return return the interface struct representing the interface
144 *that matched the number.
147 if_ifwithsock(int fd)
149 struct interface *ifp;
154 if (ifp->olsr_socket == fd)
164 *Create a new interf_name struct using a given
165 *name and insert it into the interface list.
167 *@param name the name of the interface.
175 struct olsr_if *interf_n = olsr_cnf->interfaces;
177 //printf("Adding interface %s\n", name);
179 /* check if the inerfaces already exists */
180 while(interf_n != NULL)
182 if(memcmp(interf_n->name, name, strlen(name)) == 0)
184 fprintf(stderr, "Duplicate interfaces defined... not adding %s\n", name);
187 interf_n = interf_n->next;
190 interf_n = olsr_malloc(sizeof(struct olsr_if), "queue interface");
192 /* strlen () does not return length including terminating /0 */
193 interf_n->name = olsr_malloc(strlen(name) + 1, "queue interface name");
194 interf_n->cnf = NULL;
195 interf_n->interf = NULL;
196 interf_n->configured = 0;
197 interf_n->index = olsr_cnf->ifcnt++;
199 strcpy(interf_n->name, name);
200 interf_n->next = olsr_cnf->interfaces;
201 olsr_cnf->interfaces = interf_n;
208 *Add an ifchange function. These functions are called on all (non-initial)
209 *changes in the interface set.
216 add_ifchgf(int (*f)(struct interface *, int))
219 struct ifchgf *new_ifchgf;
221 new_ifchgf = olsr_malloc(sizeof(struct ifchgf), "Add ifchgfunction");
223 new_ifchgf->next = ifchgf_list;
224 new_ifchgf->function = f;
226 ifchgf_list = new_ifchgf;
234 * Remove an ifchange function
237 del_ifchgf(int (*f)(struct interface *, int))
239 struct ifchgf *tmp_ifchgf, *prev;
241 tmp_ifchgf = ifchgf_list;
246 if(tmp_ifchgf->function == f)
251 ifchgf_list = tmp_ifchgf->next;
256 prev->next = tmp_ifchgf->next;
262 tmp_ifchgf = tmp_ifchgf->next;