#!/usr/bin/perl -w

# モジュール指定
use IO::Socket;

if(@ARGV <= 2){ # 引数が足りない場合
  print "Usage: client.pl host_name key server [port]\n";
  exit(1);
}
$host = shift;          # 第一の引数:登録するホスト名
$password = shift;      # 第二の引数:登録するホスト名に対するキー
$server = shift;        # 第三の引数:登録するサーバ
$port = shift || "5103";# 第四の引数:登録するサーバのポート(省略時は5103)
$timeout = "60";        # 待ち許容秒数
$salt = &make_salt();   # crypt()で使用される二文字のSALT
$crypt = crypt($password,$salt);

$sock = IO::Socket::INET->new(PeerAddr => $server,# サーバ指定
				PeerPort => $port,# サービスポート番号
				Proto    => "tcp",# プロトコル
				Timeout  => $timeout,
			       );
if ($sock) {
  print $sock "DDNS $host $crypt $salt\r\n";
  print $sock "QUIT\r\n";
  $sock-> flush();# バッファデータ送出
  while (<$sock>){
    print $_;
  }
  $sock -> close();
} else {
  print "$server:$portに接続できません。\n";
}

undef $sock;
undef @ARGV;
exit(0);

###crypt関数のための二文字のSALT作成###
sub make_salt{
  @fred = ("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o",
	   "p","q","r","s","t","u","v","w","x","y","z",
	   "A","B","C","D","E","F","G","H","I","J","K","L","M","N","O",
	   "P","Q","R","S","T","U","V","W","X","Y","Z");
  return  $fred[rand(@fred)] . $fred[rand(@fred)];
}
