#!/bin/sh
#This script is created by ssparser automatically. The parser first created by MaoShouyan
printf "Content-type: text/html
Cache-Control: no-cache

"
echo -n ""; 
. ../common/common.sh 
myself="/cgi-bin/Setup/`basename $0`"
[ ! -e ${PGETC}/arp.txt ] && touch ${PGETC}/arp.txt

echo -n "
<script type=\"text/javascript\" src=\"/img/common.js\"></script>
<script language=\"javascript\">
function onAddArp(ipaddr, macaddr)
{
	window.location.href = \"${myself}?action=add&ipaddr=\" + ipaddr + \"&macaddr=\" + macaddr;
}
function onAddArpEx()
{
	window.location.href = \"/cgi-bin/Setup/iparp_add\";
}
function onDeleteArp(ipaddr)
{
	if (confirm(\"ȷҪARP?\")) 
		window.location.href = \"${myself}?action=delete&ipaddr=\" + ipaddr;
}
function onAddAll()
{
	if (confirm(\"ȷҪȫ?\")) 
		window.location.href = \"${myself}?action=addall\";
}
function onQueryArp()
{
	window.location.href = \"/cgi-bin/Setup/iparp_query\";
}
</script>
";
if [ "${CGI_action}" = "delete" ]; then
	tmpfile="/var/tmp/arp.tmp"
	[ -f ${tmpfile} ] && rm -f ${tmpfile}
	touch ${tmpfile}
	exec 0<${PGETC}/arp.txt
	while read ipaddr macaddr theothers
	do
		if [ "${CGI_ipaddr}" != "${ipaddr}" ]; then
			echo "${ipaddr} ${macaddr}" >> ${tmpfile}
		fi
	done
	errmsg=`arp -d ${CGI_ipaddr} 2>&1`
	mv ${tmpfile} ${PGETC}/arp.txt
	afm_dialog_msg "ɹ!"
fi
if [ "${CGI_action}" = "add" ]; then
	tmpfile="/var/tmp/arp.tmp"
	[ -f ${tmpfile} ] && rm -f ${tmpfile}
	touch ${tmpfile}
	yes=0
	exec 0<${PGETC}/arp.txt
	while read ipaddr macaddr theothers
	do
		if [ "${CGI_ipaddr}" = "${ipaddr}" -a "${CGI_macaddr}" = "${macaddr}" ]; then
			yes=1
		else
			echo "${ipaddr} ${macaddr}" >> ${tmpfile}
		fi
	done
	if [ ${yes} -eq 0 ]; then
		echo "${CGI_ipaddr} ${CGI_macaddr}" >> ${tmpfile}
	fi
	errmsg=`arp -s ${CGI_ipaddr} ${CGI_macaddr} 2>&1`
	if [ "$?" = "0" ]; then
		mv ${tmpfile} ${PGETC}/arp.txt
		afm_dialog_msg "ɹ!"
	else
		rm -f ${tmpfile}
		afm_dialog_msg "ʧ!"
	fi
fi
if [ "${CGI_action}" = "addall" ]; then
	tmpfile="/var/tmp/arp.tmp"
	[ -f ${tmpfile} ] && rm -f ${tmpfile}
	touch ${tmpfile}
	arp -an | sed -e "s/(//g" | sed -e "s/)//g" | while read col1 ipaddr at macaddr on intf perm theothers
	do
    		[ "${macaddr}" = "incomplete" ] && continue
		echo "${ipaddr} ${macaddr}" >> ${tmpfile}
		errmsg=`arp -s ${ipaddr} ${macaddr} 2>&1`
	done
	mv ${tmpfile} ${PGETC}/arp.txt
	afm_dialog_msg "ɹ!"
fi

echo -n "
<body>
"; cgi_show_title "·->ARP" 
echo -n "
<br>
<table class=bgurl width=800 border=0 cellspacing=1 cellpadding=1 bgcolor=\"#ffffff\">
<tr id=tblhdr height=22>
	<td width=50  align=center></td>
	<td width=220 align=center>IPַ</td>
	<td width=220 align=center>MACַ</td>
	<td width=* align=center>
		<input type=button value=\"ȫ\" style=\"width:90px;height:21px\" onclick=\"onAddAll()\"></input>
		&nbsp;&nbsp;
		<input type=button value=\"Ӱ>>\" style=\"width:90px;height:21px\" onclick=\"onAddArpEx()\"></input>
	</td>
</tr>
";
idname="row1"
no=1
arp -an | sed -e "s/(//g" | sed -e "s/)//g" | while read col1 ipaddr at macaddr on intf perm theothers
do
    [ "${macaddr}" = "incomplete" ] && continue
    echo "<tr id=${idname}>"
    echo "<td align=center>${no}</td>"
    echo "<td align=center>${ipaddr}</td>"
    echo "<td align=center>${macaddr}</td>"
    if [ "${perm}" = "permanent" ]; then
        echo "<td align=center>ARP&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a style=\"color:#0000ff\" href=\"javascript:onDeleteArp('${ipaddr}')\"></a></td>"
    else
        echo "<td align=center><a style=\"color:#0000ff\" href=\"javascript:onAddArp('${ipaddr}', '${macaddr}')\">ARP</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>"
    fi
    echo "</tr>"
    if [ "${idname}" = "row1" ]; then
        idname="row2"
    else
        idname="row1"
    fi
    no=`expr ${no} + 1`
done
echo "</table>"

echo -n "
</body>
</html>
";