) {
if (/^nodename:\s*(\S+)/) {
$server = $1;
} elsif (/^domain:\s*(\S+)/) {
$domain = $1;
} elsif (/^port:\s*(\d+)/) {
$port = $1;
} elsif (/^parents:\s*(.*)/) {
$parents = $1;
} elsif (/^children:\s*(.*)/) {
$children = $1;
}
}
# ¼³Á¤ÀÌ ½ÇÆÐÇßÀ» °æ¿ì ±âº»°ªÀ¸·Î ÁöÁ¤ÇÑ´Ù.
$localhost .= ".$domain";
$server = $localhost unless $server;
$requester = $localhost unless $requester;
# debug¸¦ À§ÇÑ Ãâ·ÂÄÚµå
#print "Debug code - \n";
#print "Server:$server\n";
#print "Port:$port\n";
#print "Parent nodes:$parents\n";
#print "Child nodes:$children\n";
#print "Keyword:$keyword\n";
#print "Requester:$requester\n";
#print "Maxhop:$maxhop\n";
# CGI FORMÀÇ Ãâ·Â(¼ºê·çƾ È£Ãâ)
&print_form($keyword);
# ÀÌ¿ôÇÏ´Â »óÀ§ ¼¹ö¿Í ÇÏÀ§ ¼¹öÀÇ À̸§À» Ãâ·ÂÇÏ°í ¸µÅ©¸¦ ºÙÀδÙ.
print "»óÀ§ ¼¹ö : \n";
foreach $node (split /\s+/, $parents) {
print "- ¼¹ö $node\n";
}
print "
\n\n";
print "ÇÏÀ§ ¼¹ö :
\n";
foreach $node (split /\s+/, $children) {
print "- ¼¹ö $node\n";
}
print "
\n\n";
# °Ë»ö °á°ú°¡ ¸¹À» °æ¿ì °¢°¢ÀÇ ¼¹ö¿¡¼ ³ª¿Â °á°ú¿¡ ºÙ¿© ³õÀº ű׸¦
# »ç¿ëÇϱâ À§ÇÑ '¹Ù·Î °¡´Â ¸µÅ©'¸¦ ºÙÀδÙ. ±×·¯³ª ÀÌ ÇÁ·Î±×·¥¿¡¼´Â
# ±¸ÇöÀº µÇ¾úÀ¸³ª Á÷°üÀûÀÎ ÀÎÅÍÆäÀ̽º¸¦ À§ÇØ »ç¿ëÇÏÁö ¾Ê´Â´Ù.
#print "°Ë»ö°á°ú·Î ¹Ù·Î °¡´Â ¸µÅ© :
\n";
#print "- $server·ÎºÎÅÍÀÇ °á°ú\n";
#foreach $node (split /\s+/, $parents) {
# print "
- $node·ÎºÎÅÍÀÇ °á°ú\n ";
#}
#foreach $node (split /\s+/, $children) {
# print "
- $node·ÎºÎÅÍÀÇ °á°ú\n ";
#}
#print "
\n\n";
# ù¹ø°·Î ·ÎÄà ¼¹ö¿¡¼ °Ë»öÀ» ½ÃµµÇÑ´Ù.
# ·ÎÄà ¼¹ö¿¡ HTTP 80 Æ÷Æ®·Î ¿¬°áÀ» ¼³Á¤ÇÑ´Ù.(¼ºê·çƾ È£Ãâ)
&connection($server, $keyword, 'LOCAL', $server);
# max hopÀ» °Ë»çÇÏ¿© ÀÏÁ¤ Ƚ¼ö ÀÌ»ó °Ë»ö ¿äûÀÌ ÁøÇàµÇ¸é ÁߴܽÃŲ´Ù.
# ÀÌ ±â´ÉÀº µÎ °³ÀÇ ¼¹ö°¡ À߸øµÈ ¼³Á¤À¸·Î ÀÎÇØ °Ë»ö ¿äûÀ» ¹«ÇÑÈ÷
# ¹Ýº¹ÇÏ´Â °ÍÀ» ¸·±â À§ÇÑ º¸Á¶ ÀåÄ¡ÀÌ´Ù.
if ($maxhop > 4) {
print "³Ê¹« ¸¹Àº ¼¹ö¸¦ Ž»öÇÏ¿´À¸¹Ç·Î ¿©±â¼ Áß´ÜÇÕ´Ï´Ù.
\n";
&print_tail;
exit(0);
}
# µÎ¹ø°·Î´Â ÇÏÀ§ ¼¹ö·ÎºÎÅÍ °Ë»öÀ» ½ÃµµÇÑ´Ù. ÇÏÀ§ ¼¹ö´Â 2°³ ÀÌ»óÀÏ ¼ö ÀÖ´Ù.
foreach $node (split /\s+/, $children) {
if ($requester eq $node) {
print "ÇÏÀ§ ¼¹ö $node´Â °Ë»öÀ» ¿äûÇÑ ¼¹öÀ̹ǷΠ¼¹ö $server´Â ÇÏÀ§ ¼¹öÀÎ $node¿¡°Ô °Ë»öÀ» ¿äûÇÏÁö ¾Ê½À´Ï´Ù.
\n";
} else {
print "¼¹ö $server°¡ ÇÏÀ§ ¼¹ö $node¿¡ °Ë»öÀ» ¿äûÇÕ´Ï´Ù.
\n";
&connection($node, $keyword, 'REMOTE', $server);
}
}
# ¼¼¹ø°·Î´Â »óÀ§ ¼¹ö¿¡°Ô °Ë»öÀ» ¿äûÇÑ´Ù. »óÀ§ ¼¹ö ¶ÇÇÑ 2°³ ÀÌ»ó ³ª¿µÉ
# ¼ö ÀÖ´Ù.
foreach $node (split /\s+/, $parents) {
if ($requester eq $node) {
print "»óÀ§ ¼¹ö $node´Â °Ë»öÀ» ¿äûÇÑ ¼¹öÀ̹ǷΠ¼¹ö $server´Â »óÀ§ ¼¹öÀÎ $node¿¡°Ô °Ë»öÀ» ¿äûÇÏÁö ¾Ê½À´Ï´Ù.
\n";
} else {
print "¼¹ö $server°¡ »óÀ§ ¼¹ö $node¿¡ °Ë»öÀ» ¿äûÇÕ´Ï´Ù.
\n";
&connection($node, $keyword, 'REMOTE', $server);
}
}
# HTMLÀÇ ³ª¸ÓÁö tag¸¦ Ãâ·ÂÇÏ°í Á¾·áÇÑ´Ù.
&print_tail;
exit;
# ¼ºê·çƾ Á¤ÀÇ
# Çì´õ Ãâ·Â
sub print_head {
print "Content-type: text/html\n\n";
print "\n";
print "
\n";
}
# FORM Ãâ·Â
sub print_form {
# keyword´Â FORMÀÇ °Ë»ö¾î ºÎºÐ¿¡ ¹Ì¸® ä¿ö ³ÖÀ» °Ë»ö¾î¸¦ ³Ñ°Ü ¹Þ´Â º¯¼ö
my ($keyword) = @_;
print "Meta search
\n";
print "\n";
}
# tail tag Ãâ·Â
sub print_tail {
print "\n";
print "\n";
}
# SocketÀ» ÅëÇÑ connection
sub connection {
# °Ë»öÇÒ À¥¼¹ö À̸§, °Ë»ö¾î, ·ÎÄÃÀΰ¡ ¸®¸ðÆ®Àΰ¡ÀÇ ¿©ºÎ, ¿äûÇÑ ¼¹ö À̸§
my ($webserver, $word, $whereabout, $req) = @_;
my ($iaddr, $paddr, $proto, $state, $line);
# ¸®¸ðÆ®ÀÏ ¶§¿Í ·ÎÄÃÀÏ ¶§ °¢°¢ÀÇ °Ë»öÀ» ¹Þ¾ÆµéÀÏ CGIÇÁ·Î±×·¥ÀÇ À§Ä¡ ÁöÁ¤
if ($whereabout eq 'REMOTE') {
# In remote hosts, 'm_search.cgi' call another 'm_search.cgi'.
$maxhop++;
$location = "/~terzeron/meta/m_search.cgi?keyword=$word&requester=$req&maxhop=$maxhop"; # remotehost file location
} else {
# In localhost, 'm_search.cgi' use 'htdig'.
$location = "/cgi-bin/htsearch?config=htdig&restrict=&exclue=&method=and&format=builtin-long&words=$word"; # localhost file location
}
# '¹Ù·Î °¡´Â ¸µÅ©'¸¦ À§ÇÑ ÅÂ±× Ãâ·Â(»ç¿ëµÇÁö ¾Ê´Â´Ù)
print "\n";
print "$webserver¿¡¼ÀÇ $word¿¡ ´ëÇÑ °Ë»ö °á°ú
\n";
# socket ¼³Á¤
if ($port =~ /\D/) {
$port = getservbyname($port, 'tcp');
}
print "No port" unless $port;
$iaddr = inet_aton($webserver) or print "no host: $webserver";
$paddr = sockaddr_in($port, $iaddr);
print "No port address" unless $paddr;
$proto = getprotobyname('tcp');
print "No protocol" unless $proto;
socket(SOCK, PF_INET, SOCK_STREAM, $proto) or print "socket: $!";
connect(SOCK, $paddr) or print "connect: $!";
send SOCK, "GET $location\n", MSG_DONTROUTE or print "can't send message to web server\n";
# htdigÀÇ htsearch¿¡¼ ³ª¿À´Â HTML ű׸¦ °¡Áö°í °Ë»ö °á°ú¸¦ Ãß·Á³½´Ù.
$state = 0;
while () {
if ($whereabout eq 'LOCAL') {
$state = 2 if /\
°æ°í! ";
print "$webserverÀÇ Meta search engineÀÌ ";
print "$req·ÎºÎÅÍÀÇ " if $whereabout eq 'REMOTE';
print "°Ë»ö¾î \"$word\"¿¡ °ü·ÃµÈ ";
print "¾î¶² ¹®¼µµ ãÁö ¸øÇß½À´Ï´Ù.\n";
}
print "
\n";
}