#!/bin/sh

. /lib/functions.sh

KEY_FILE="/etc/dropbear/authorized_keys"
CONFIG="dropbear"

get_pid_file()
{
	local ServerName="$1"

	# this is the way instances are named in dropbear init file
	echo "/var/run/$CONFIG.$ServerName.pid"
}

get_session_pids()
{
	local PidFile="$1"

	echo "$(ps -w | grep "$PidFile" | grep -v grep | awk '{print $1}')"
}

get_network_info()
{
	local session_pid="$1"
	local server_pid="$2"

	# get this session's ppid
	session_ppid="$(grep PPid /proc/$session_pid/status | awk '{print $2}')"

	# if session's parent is this server
	if [ "$session_ppid" -eq "$server_pid" ]; then
		session="$(netstat -ntp | grep "$session_pid/" | awk '{print $5,$7}' | cut -d'/' -f 1)"
		if [ -n "$session" ]; then
			ip=$(echo "$session" | cut -d ':' -f 1)
			port=$(echo "$session" | cut -d ':' -f 2 | cut -d ' ' -f 1)
			echo "$ip $port"
			return 0
		fi
	fi
	return 1
}
