001: package CustomDNS;
002:
003: import java.io.Writer;
004: import java.io.IOException;
005:
006: /*************************************************************************
007: * Constants and utility functions used by our update protocol.
008: *************************************************************************
009: * @see CustomDNS.UpdateServer
010: * @see CustomDNS.UpdateClient
011: */
012:
013: public class UpdateProtocol {
014:
015: /** The default port for the update protocol. */
016: public static final short DEFAULT_PORT = (short) 5768;
017:
018: /** The command completed successfully. */
019: public static final int STATUS_OK = 200;
020: /** The connection to the server was successful. */
021: public static final int STATUS_WELCOME = 201;
022: /** The AUTH command was accepted, but the authentication data has
023: not been checked yet. */
024: public static final int STATUS_AUTH_ACCEPTED = 202;
025:
026: /** A syntax error occurred. */
027: public static final int STATUS_SYNTAX_ERROR = 500;
028: /** The last command was unknown. */
029: public static final int STATUS_UNKNOWN_COMMAND = 501;
030: /** The last command referred to an unknown DNS zone. */
031: public static final int STATUS_UNKNOWN_ZONE = 502;
032: /** The last command cannot be handled in this server state. */
033: public static final int STATUS_WRONG_STATE = 502;
034: /** The last command included a malformed IP address. */
035: public static final int STATUS_MALFORMED_ADDRESS = 504;
036: /** The specified update could not be performed. */
037: public static final int STATUS_NO_UPDATE = 510;
038: /** The AUTH command had an unknown authentication type. */
039: public static final int STATUS_UNKNOWN_AUTH_TYPE = 520;
040: /** The authentication could not be performed. */
041: public static final int STATUS_COULD_NOT_AUTH = 521;
042: /** The specified update could not be performed. */
043: public static final int STATUS_NOT_ALLOWED = 522;
044:
045: /*********************************************************************
046: * Write a line in network format (CR/LF terminated).
047: *********************************************************************
048: * We flush the writer immediately.
049: * @param out The output writer.
050: * @param line The string to write.
051: * @exception IOException An error occured while writing.
052: */
053:
054: public static void writeLine(Writer out, String line)
055: throws IOException {
056: out.write(line + "\r\n");
057: out.flush();
058: }
059:
060: /*********************************************************************
061: * An response returned by the update server.
062: *********************************************************************
063: * This class respresents responses of the form:
064: * <pre>500 Error message</pre>
065: * This class is a subclass of Exception, so you can throw it.
066: */
067:
068: static public class ServerResponse extends Exception {
069:
070: private int code;
071: private String text;
072:
073: /*****************************************************************
074: * Create a new server response object.
075: *****************************************************************
076: * @param code A three-digit response code.
077: * @param text The explanatory string returned by the server.
078: */
079:
080: public ServerResponse(int code, String text) {
081: super (code + " " + text);
082: this .code = code;
083: this .text = text;
084: }
085:
086: /*****************************************************************
087: * Parse a server response string.
088: *****************************************************************
089: * @param line The text returned by the server.
090: */
091:
092: static public ServerResponse parse(String line)
093: throws IOException {
094: try {
095: if (line.length() < 5 || line.charAt(3) != ' ')
096: throwParseError();
097: String digits = line.substring(0, 3);
098: String text = line.substring(4);
099: int code = Integer.parseInt(digits);
100: return new ServerResponse(code, text);
101: } catch (NumberFormatException e) {
102: throwParseError();
103: // We never get here.
104: return null;
105: }
106: }
107:
108: // Give up on parsing and throw an error.
109: static private void throwParseError() throws IOException {
110: throw new IOException(
111: "Error parsing response from UpdateServer.");
112: }
113:
114: /** Get the server's response code. */
115: public int getCode() {
116: return this .code;
117: }
118:
119: /** Get the server's response text. */
120: public String getText() {
121: return this .text;
122: }
123:
124: /** Return true if the response represents an error. */
125: public boolean isError() {
126: if (this .code < 200 || this .code > 299)
127: return true;
128: return false;
129: }
130: }
131:
132: }
|