Project: IPFire
Code Location: git://git.ipfire.org/network.gitmaster
Browse
/
Download File
functions.dhclient
#!/bin/bash
###############################################################################
#                                                                             #
# IPFire.org - A linux based firewall                                         #
# Copyright (C) 2012  IPFire Network Development Team                         #
#                                                                             #
# This program is free software: you can redistribute it and/or modify        #
# it under the terms of the GNU General Public License as published by        #
# the Free Software Foundation, either version 3 of the License, or           #
# (at your option) any later version.                                         #
#                                                                             #
# This program is distributed in the hope that it will be useful,             #
# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
# GNU General Public License for more details.                                #
#                                                                             #
# You should have received a copy of the GNU General Public License           #
# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
#                                                                             #
###############################################################################

dhclient_start() {
	local interface=${1}
	local proto=${2}

	assert isset interface
	assert device_exists ${interface}

	local service=$(dhclient_proto2service ${proto} ${interface})
	service_start ${service}
}

dhclient_stop() {
	local interface=${1}
	local proto=${2}

	local service=$(dhclient_proto2service ${proto} ${interface})
	service_stop ${service}
}

dhclient_status() {
	local interface=${1}
	local proto=${2}

	local service=$(dhclient_proto2service ${proto} ${interface})
	service_status ${service}
}

dhclient_proto2service() {
	local proto=${1}
	assert isset proto

	local interface=${2}
	assert isset interface

	local service

	case "${proto}" in
		ipv4)
			service="dhclient4@${interface}.service"
			;;
		ipv6)
			service="dhclient6@${interface}.service"
			;;
		*)
			return ${EXIT_ERROR}
			;;
	esac

	assert isset service

	echo "${service}"
	return ${EXIT_OK}
}

dhclient_write_config() {
	local interface=${1}
	local file=${2}
	shift 2

	assert isset interface
	assert isset file

	local hostname=${HOSTNAME%%.*}
	local prefix_delegation="false"
	local vendor=$(distro_get_pretty_name)

	while [ $# -gt 0 ]; do
		case "${1}" in
			--hostname=*)
				hostname=$(cli_get_val ${1})
				;;
			--prefix-delegation=*)
				prefix_delegation="$(cli_get_bool "${1}")"
				;;
			--vendor=*)
				vendor=$(cli_get_val ${1})
				;;
			*)
				log WARNING $"Unknown configuration option passed: ${1}."
				;;
		esac
		shift
	done

	# Clear configuration file (if any).
	mkdir -p $(dirname ${file}) 2>/dev/null
	: > ${file}

	# Print the header.
	(	echo "#"
		echo "# This is a dhclient daemon configuration file for ${interface}."
		echo "# THIS FILE IS AUTOMATICALLY GENERATED AND WILL OVERWRITE"
		echo "# ANY CUSTOM CHANGES!"
		echo "#"
		echo "# $(date -u)"
		echo "#"
		echo
	) >>${file}

	# Global options.
	echo "send vendor-class-identifier \"${vendor}\";" >>${file}
	echo

	# Interface options.
	(
		echo "interface \"${interface}\" {"

		if isset hostname; then
			echo "	send host-name \"${hostname}\";"
			print
		fi

		# Prefix delegation (IPv6).
		if enabled prefix_delegation; then
			print "	# Prefix delegation"
			print "	also request dhcp6.ia-pd 1;"
			print "	send dhcp6.ia-na 1;"
			print
		fi

		echo "}"
	) >>${file}

	return ${EXIT_OK}
}