1/*
2 * $Id: libbridge_misc.c,v 1.1.1.1 2008/10/15 03:28:31 james26_jang Exp $
3 *
4 * Copyright (C) 2000 Lennert Buytenhek
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21#include <stdio.h>
22#include <stdlib.h>
23#include <sys/time.h>
24#include <asm/param.h>
25#include "libbridge.h"
26#include "libbridge_private.h"
27
28unsigned long __tv_to_jiffies(struct timeval *tv)
29{
30	unsigned long jif;
31
32	jif = 1000000 * tv->tv_sec + tv->tv_usec;
33
34	return (HZ*jif)/1000000;
35}
36
37void __jiffies_to_tv(struct timeval *tv, unsigned long jiffies)
38{
39	unsigned long tvusec;
40
41	tvusec = (1000000*jiffies)/HZ;
42	tv->tv_sec = tvusec/1000000;
43	tv->tv_usec = tvusec - 1000000 * tv->tv_sec;
44}
45
46static char *state_names[5] = {"disabled", "listening", "learning", "forwarding", "blocking"};
47
48char *br_get_state_name(int state)
49{
50	if (state >= 0 && state <= 4)
51		return state_names[state];
52
53	return "<INVALID STATE>";
54}
55
56struct bridge *br_find_bridge(char *brname)
57{
58	struct bridge *b;
59
60	b = bridge_list;
61	while (b != NULL) {
62		if (!strcmp(b->ifname, brname))
63			return b;
64
65		b = b->next;
66	}
67
68	return NULL;
69}
70
71struct port *br_find_port(struct bridge *br, char *portname)
72{
73	char index;
74	struct port *p;
75
76	if (!(index = if_nametoindex(portname)))
77		return NULL;
78
79	p = br->firstport;
80	while (p != NULL) {
81		if (p->ifindex == index)
82			return p;
83
84		p = p->next;
85	}
86
87	return NULL;
88}
89