2 * OLSR ad-hoc routing table management protocol
3 * Copyright (C) 2003 Andreas Tønnesen (andreto@ifi.uio.no)
5 * This file is part of the olsr.org OLSR daemon.
7 * olsr.org is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * olsr.org is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with olsr.org; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * $Id: interfaces.c,v 1.10 2004/11/01 20:13:27 kattemat Exp $
27 #include "interfaces.h"
29 #include "scheduler.h"
33 *Do initialization of various data needed for
34 *network interface management.
35 *This function also tries to set up the given interfaces.
37 *@return the number of interfaces configured
42 struct olsr_if *tmp_if;
49 *Initializing addrsock struct to be
50 *used on all the sockets
52 if(olsr_cnf->ip_version == AF_INET)
55 memset(&addrsock, 0, sizeof (addrsock));
56 addrsock.sin_family = AF_INET;
57 addrsock.sin_port = olsr_udp_port;
58 (addrsock.sin_addr).s_addr = INADDR_ANY;
63 memset(&addrsock6, 0, sizeof (addrsock6));
64 addrsock6.sin6_family = AF_INET6;
65 addrsock6.sin6_port = olsr_udp_port;
66 //(addrsock6.sin6_addr).s_addr = IN6ADDR_ANY_INIT;
69 olsr_printf(1, "\n ---- Interface configuration ---- \n\n");
70 /* Run trough all interfaces immedeatly */
71 for(tmp_if = olsr_cnf->interfaces; tmp_if != NULL; tmp_if = tmp_if->next)
76 /* register network interface update function with scheduler */
77 olsr_register_scheduler_event(&check_interface_updates, NULL, 5.0, 0, NULL);
79 return (ifnet == NULL) ? 0 : 1;
85 *Find the local interface with a given address.
87 *@param addr the address to check.
89 *@return the interface struct representing the interface
90 *that matched the address.
94 if_ifwithaddr(union olsr_ip_addr *addr)
96 struct interface *ifp;
98 for (ifp = ifnet; ifp; ifp = ifp->int_next)
100 if(olsr_cnf->ip_version == AF_INET)
103 //printf("Checking: %s == ", inet_ntoa(((struct sockaddr_in *)&ifp->int_addr)->sin_addr));
104 //printf("%s\n", olsr_ip_to_string(addr));
106 if (COMP_IP(&((struct sockaddr_in *)&ifp->int_addr)->sin_addr, addr))
112 //printf("Checking %s ", olsr_ip_to_string((union olsr_ip_addr *)&ifp->int6_addr.sin6_addr));
113 //printf("== %s\n", olsr_ip_to_string((union olsr_ip_addr *)&((struct sockaddr_in6 *)addr)->sin6_addr));
114 if (COMP_IP(&ifp->int6_addr.sin6_addr, addr))
124 *Find the interface with a given number.
126 *@param nr the number of the interface to find.
128 *@return return the interface struct representing the interface
129 *that matched the number.
132 if_ifwithsock(int fd)
134 struct interface *ifp;
139 if (ifp->olsr_socket == fd)
149 *Create a new interf_name struct using a given
150 *name and insert it into the interface list.
152 *@param name the name of the interface.
160 struct olsr_if *interf_n = olsr_cnf->interfaces;
162 //printf("Adding interface %s\n", name);
164 /* check if the inerfaces already exists */
165 while(interf_n != NULL)
167 if(memcmp(interf_n->name, name, strlen(name)) == 0)
169 fprintf(stderr, "Duplicate interfaces defined... not adding %s\n", name);
172 interf_n = interf_n->next;
175 interf_n = olsr_malloc(sizeof(struct olsr_if), "queue interface");
177 /* strlen () does not return length including terminating /0 */
178 interf_n->name = olsr_malloc(strlen(name) + 1, "queue interface name");
179 interf_n->cnf = NULL;
180 interf_n->interf = NULL;
181 interf_n->configured = 0;
182 interf_n->index = olsr_cnf->ifcnt++;
184 strcpy(interf_n->name, name);
185 interf_n->next = olsr_cnf->interfaces;
186 olsr_cnf->interfaces = interf_n;
193 *Add an ifchange function. These functions are called on all (non-initial)
194 *changes in the interface set.
201 add_ifchgf(int (*f)(struct interface *, int))
204 struct ifchgf *new_ifchgf;
206 new_ifchgf = olsr_malloc(sizeof(struct ifchgf), "Add ifchgfunction");
208 new_ifchgf->next = ifchgf_list;
209 new_ifchgf->function = f;
211 ifchgf_list = new_ifchgf;
219 * Remove an ifchange function
222 del_ifchgf(int (*f)(struct interface *, int))
224 struct ifchgf *tmp_ifchgf, *prev;
226 tmp_ifchgf = ifchgf_list;
231 if(tmp_ifchgf->function == f)
236 ifchgf_list = tmp_ifchgf->next;
241 prev->next = tmp_ifchgf->next;
247 tmp_ifchgf = tmp_ifchgf->next;