aboutsummaryrefslogtreecommitdiff
path: root/net.BSD/ifconfig.sh
blob: 68a3875f53cf4570a0c4dd960b4b66701b4140f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# Copyright 2004-2007 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2

ifconfig_depend() {
	program /sbin/ifconfig
	provide interface
}

_exists() {
	[ -e /dev/net/"${IFACE}" ]
}

_get_mac_address() {
	local mac=$(LC_ALL=C ifconfig "${IFACE}" | \
	sed -n -e 's/^[[:space:]]*ether \(..:..:..:..:..:..\).*/\1/p')

	case "${mac}" in
		00:00:00:00:00:00) ;;
		44:44:44:44:44:44) ;;
		FF:FF:FF:FF:FF:FF) ;;
		*) echo "${mac}"; return 0 ;;
	esac

	return 1
}

_up () {
	ifconfig "${IFACE}" up
}

_down () {
	ifconfig "${IFACE}" down
}

_ifindex() {
	local x=
	for x in /dev/net[0-9]* ; do
		if [ "${x}" -ef /dev/net/"${IFACE}" ] ; then
			echo "${x#/dev/net}"
			return 0
		fi
	done
	return 1
}

_is_wireless() {
	LC_ALL=C ifconfig "${IFACE}" 2>/dev/null | \
		grep -q "^[[:space:]]*media: IEEE 802.11 Wireless"
}

_get_inet_address() {
	set -- $(LC_ALL=C ifconfig "${IFACE}" |
	sed -n -e 's/^[[:space:]]*inet \([^ ]*\) netmask 0x\(..\)\(..\)\(..\)\(..\).*/\1 0x\2.0x\3.0x\4/p')
	[ -z "$1" ] && return 1

	echo -n "$1"
	shift
	echo "/$(_netmask2cidr "$1")"
}

_add_address() {
	if [ "${metric:-0}" != "0" ] ; then
		set -- "$@" metric ${metric}
	fi

	case "$@" in
		*:*) ifconfig "${IFACE}" inet6 add "$@" ;;
		*)   ifconfig "${IFACE}"       add "$@" ;;
	esac
}

_add_route() {
	if [ $# -gt 3 ] ; then
		if [ "$3" = "gw" -o "$3" = "via" ] ; then
			local one=$1 two=$2
			shift ; shift; shift
			set -- "${one}" "${two}" "$@"
		fi
	fi

	case "$@" in
		*:*) route add -inet6 "$@" ;;
		*)   route add        "$@" ;;
	esac
}

_delete_addresses() {
	# We don't remove addresses from aliases
	case "${IFACE}" in
		*:*) return 0 ;;
	esac

	einfo "Removing addresses"
	eindent
	local addr=
	for addr in $(LC_ALL=C ifconfig "${IFACE}" |
		sed -n -e 's/^[[:space:]]*inet \([^ ]*\).*/\1/p') ; do
		if [ "${addr}" = "127.0.0.1" ] ; then
			# Don't delete the loopback address
			[ "$1" = "lo" -o "$1" = "lo0" ] && continue
		fi
		einfo "${addr}"
		ifconfig "$1" delete "${addr}"
		eend $?
	done

	# Remove IPv6 addresses
	for addr in $(LC_ALL=C ifconfig "${IFACE}" | \
		sed -n -e 's/^[[:space:]]*inet6 \([^ ]*\).*/\1/p') ; do
		case "${addr}" in
			*"%${IFACE}") continue ;;
			::1) continue ;;
		esac
		einfo "${addr}"
		ifconfig "${IFACE}" inet6 delete "${addr}"
		eend $?
	done
	
	return 0
}

_show_address() {
	einfo "received address $(_get_inet_address "${IFACE}")"
}

_has_carrier() {
	local s=$(LC_ALL=C ifconfig "${IFACE}" | \
	sed -n -e 's/^[[:space:]]status: \(.*\)$/\1/p')
	[ -z "${s}" -o "${s}" = "active" -o "${s}" = "associated" ] 
}

# vim: set ts=4 :