home *** CD-ROM | disk | FTP | other *** search
/ Chip 2005 June / ccd0605.iso / Software / Shareware / Comunicatii / nat32 / identd.tcl < prev    next >
Text File  |  2001-10-12  |  2KB  |  99 lines

  1. #!tcl
  2.  
  3. #
  4. # IDENTD Forwarder
  5. #
  6.  
  7. set port 113
  8.  
  9. # Listen at Port for connection requests
  10.  
  11. set s [socket -p $port 1]
  12.  
  13. while {1} {
  14.  
  15. # accept an incoming request
  16.  
  17.     set ns [$s accept]
  18.  
  19. # where did it come from
  20.  
  21. set sip [$ns getaddr]
  22.  
  23. # got one, now read it
  24.  
  25.     set data [$ns gets 512]
  26.  
  27. #    echo IDENTD: got $data from $sip
  28.     
  29. # parse the request
  30.  
  31.     set lport [lindex $data 0]
  32.     set lport [string trim $lport ,] 
  33.     set fport [lindex $data 1]
  34.     
  35. # fetch the Port Map from NAT32
  36.  
  37.     set pmap [exec "pmap"]
  38.         
  39. # parse the pmap
  40.  
  41.     set addr "not found"
  42.     set pmap [split $pmap "\n"]
  43.  
  44.     foreach v $pmap {
  45.         set ipsrc [lindex $v 1]
  46.         set ipdst [lindex $v 2]
  47.         set prot  [lindex $v 3]
  48.         set dp    [lindex $v 4]
  49.         set sp    [lindex $v 5]
  50.         set nsp   [lindex $v 6]
  51.  
  52.         if {$ipdst == $sip && $prot == "6" && $lport == $dp && $fport == $nsp} {
  53.             set addr $ipsrc
  54.             break
  55.         }
  56.     }
  57.  
  58.     if {$addr == "not found"} {
  59.         $ns puts "$lport, $fport : USERID : UNIX : NAT32\n"
  60.         $ns shutdown 
  61.         rename $ns {}
  62.         continue
  63.     }
  64.  
  65. # open a socket to the private machine
  66.  
  67.     set cs [socket $addr $port]
  68.  
  69.     if {[lindex $cs 0] == "can't"} {
  70.         $ns shutdown
  71.         rename $ns {}
  72.         continue
  73.     }
  74.  
  75. # send the request to that machine
  76.  
  77.     $cs puts $data
  78.  
  79. # read the response
  80.  
  81.     set data [$cs gets 512]
  82.  
  83. # close the socket
  84.  
  85.     rename $cs {}
  86.  
  87. # return the response to the orginal server
  88.  
  89.     $ns puts $data
  90.  
  91. # close the socket in the send direction
  92.  
  93.     $ns shutdown
  94.     
  95. #close the socket and loop for the next request
  96.  
  97.     rename $ns {}
  98. }
  99.