#!/bin/sh
#
# run-branch-network
#
# Starts the Raddle network emulator daemons
#
# We require the OS snmp daemon to start first as it will not start after Raddle does
# bcause the script sees existing snmpd processes.
#
# The LSB way:
#
### BEGIN INIT INFO
# Provides:            raddle
# Required-Start:      snmp
# Should-Start:        $syslog $network
# Required-Stop:       
# Should-Stop:         $syslog $network
# Default-Start:       3 5
# Default-Stop:        0 1 6
# Description:         raddle
# Short-Description:   raddle
### END INIT INFO
#
# The old way:
#
# chkconfig: - 51 7
# description: SNMP emulator daemons
#
# Andrew Findlay
# http://raddle.sourceforge.net/
#
# $Id: run-branch-network,v 1.15 2009/10/31 16:16:26 afindlay Exp $

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
export PATH

# PID file prefix
PIDFILEPREFIX="/var/run/snmpd.pid."

# Log file prefix
LOGFILEPREFIX="/var/log/raddle/snmpd."

CONFIGDIR=/usr/local/lib/raddle/branch-network

# Where is honeyd?
HONEYD=/usr/bin/honeyd

# The interface to tap
INTERFACE=lo

# Use this set of args if starting snmpd manually for debugging
# SNMPD_ARGS="-f -L -V -C -I vacm_vars -M +${CONFIGDIR}/mibs -m +BRIDGE-MIB"
#
# Normal snmpd args
# We have our own copy of BRIDGE-MIB because recent versions of net-snmpd do not install one
#
SNMPD_ARGS="-C -I vacm_conf -M +${CONFIGDIR}/mibs -m +BRIDGE-MIB"

# Daemons to start:
DAEMONS="r1 r2 r3 a1 s1 s2"

start() {
	for d in ${DAEMONS}
	do
		echo -n "Starting snmp emulator for ${d}"
		snmpd $SNMPD_ARGS -p ${PIDFILEPREFIX}${d} \
			-Lf ${LOGFILEPREFIX}${d}.log -A -c ${CONFIGDIR}/${d}.conf
		echo ''
	done

	case "$INTERFACE" in
		lo*)
			# No need for arpd, but we do want some routes
			$CONFIGDIR/make-route-commands | sh
			;;
		*)
			# We need arpd for the real ethernet interface
			echo -n 'Arpd: '
			arpd `$CONFIGDIR/find-r1-addr` $INTERFACE
			echo ''
			# No point in adding routes in this case, but
			# they should be added on another machine.
			;;
	esac

	# We start honeyd with the -N (ignore incoming checksums)
	# so that it will work properly on loopback interfaces
	echo -n 'Honeyd: '
	( cd $CONFIGDIR; ./make-honeyd-config > honeyd.conf )
	$HONEYD -N \
		-f $CONFIGDIR/honeyd.conf \
		-l /var/log/raddle/honeyd.log \
		-i $INTERFACE \
		`$CONFIGDIR/make-honeyd-bind-args`
	echo ''
	return 0
}

stop() {
	echo -n "Stopping $prog: "
	for d in ${DAEMONS}
	do
		if test -f ${PIDFILEPREFIX}${d}
		then
			kill `cat ${PIDFILEPREFIX}${d}`
			rm -f ${PIDFILEPREFIX}${d}
			echo -n ${d}" "
		fi
	done
	echo ''

	case "$INTERFACE" in
		lo*)
			$CONFIGDIR/make-route-commands del | sh
			;;
		*)
			echo -n "Stopping Arpd: "
			killall arpd
			echo ''
			;;
	esac

	echo -n "Stopping Honeyd: "
	killall honeyd
	echo ''
	return 0
}

restart(){
        stop
        start
}

case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  restart)
        restart
        ;;
  reload)
        restart
        ;;
  *)
        echo $"Usage: $0 {start|stop|status|restart|reload}"
        RETVAL=1
esac

exit $RETVAL


