1 package socks.server; 2 import java.io.BufferedReader ; 3 import java.io.IOException ; 4 import java.io.InputStreamReader ; 5 import java.io.InterruptedIOException ; 6 import java.net.ConnectException ; 7 import java.net.InetSocketAddress ; 8 import java.net.Socket ; 9 import java.util.StringTokenizer ; 10 11 23 public class Ident{ 24 25 26 public String errorMessage; 27 28 public String hostType; 29 30 public String userName; 31 32 36 public boolean successful; 37 38 public int errorCode; 39 40 public static final int ERR_NO_CONNECT = 1; 41 42 public static final int ERR_TIMEOUT = 2; 43 46 public static final int ERR_PROTOCOL = 3; 47 50 public static final int ERR_PROTOCOL_INCORRECT = 4; 51 52 53 57 public static final int connectionTimeout = 10000; 58 59 60 75 public Ident(Socket s ){ 76 Socket sock = null; 77 successful = false; 79 try{ 80 sock = new Socket (); 81 sock.setSoTimeout(connectionTimeout); 82 sock.connect(new InetSocketAddress (s.getInetAddress(),113), connectionTimeout); 83 byte[] request = (""+s.getPort()+" , "+ 84 s.getLocalPort()+"\r\n").getBytes(); 85 86 sock.getOutputStream().write(request); 87 88 BufferedReader in = new BufferedReader ( 89 new InputStreamReader (sock.getInputStream())); 90 91 parseResponse(in.readLine()); 92 93 }catch(InterruptedIOException iioe){ 94 errorCode = ERR_TIMEOUT; 95 errorMessage = "Connection to identd timed out."; 96 }catch(ConnectException ce){ 97 errorCode = ERR_NO_CONNECT; 98 errorMessage = "Connection to identd server failed."; 99 100 }catch(IOException ioe){ 101 errorCode = ERR_NO_CONNECT; 102 errorMessage = ""+ioe; 103 }finally{ 104 try{ if(sock!=null) sock.close();}catch(IOException ioe){}; 105 } 106 } 107 108 private void parseResponse(String response){ 109 if(response == null){ 110 errorCode = ERR_PROTOCOL_INCORRECT; 111 errorMessage = "Identd server closed connection."; 112 return; 113 } 114 115 StringTokenizer st = new StringTokenizer (response,":"); 116 if(st.countTokens() < 3){ 117 errorCode = ERR_PROTOCOL_INCORRECT; 118 errorMessage = "Can't parse server response."; 119 return; 120 } 121 122 st.nextToken(); String command = st.nextToken().trim().toUpperCase(); 124 125 if(command.equals("USERID") && st.countTokens() >= 2){ 126 successful = true; 127 hostType = st.nextToken().trim(); 128 userName = st.nextToken().trim(); }else if(command.equals("ERROR")){ 130 errorCode = ERR_PROTOCOL; 131 errorMessage = st.nextToken(); 132 }else{ 133 errorCode = ERR_PROTOCOL_INCORRECT; 134 System.out.println("Opa!"); 135 errorMessage = "Can't parse server response."; 136 } 137 138 139 } 140 141 162 163 } 164 | Popular Tags |