001: package CustomDNS;
002:
003: import java.io.*;
004: import java.net.*;
005: import java.util.Properties;
006:
007: import CustomDNS.SimpleConfiguration;
008: import CustomDNS.UpdateProtocol;
009: import CustomDNS.UpdateProtocol.ServerResponse;
010:
011: /*************************************************************************
012: * A simple client for the update server.
013: *************************************************************************
014: * @see CustomDNS.UpdateServer
015: */
016:
017: public class UpdateClient {
018:
019: /*********************************************************************
020: * Update the address of a host.
021: *********************************************************************
022: * @param server The host running the CustomDNS update server.
023: * @param port The port number of the CustomDNS update server.
024: * @param zone The zone in which our hostname resides.
025: * @param username The username to use for the update.
026: * @param password The password to use for the update.
027: * @param hostname The hostname (omit the zone).
028: * @param address The new address. If this is 'null', ask the server
029: * to determine the address automatically.
030: */
031:
032: public static void updateAddress(InetAddress server, short port,
033: String zone, String username, String password,
034: String hostname, InetAddress address) throws IOException,
035: ServerResponse {
036: // Connect to our server.
037: Socket socket = new Socket(server, port);
038:
039: try {
040: // Set up our I/O streams.
041: Reader rawin = new InputStreamReader(socket
042: .getInputStream());
043: BufferedReader in = new BufferedReader(rawin);
044: Writer out = new OutputStreamWriter(socket
045: .getOutputStream());
046:
047: // Figure out what to send as an address string.
048: String addressStr;
049: if (address != null) {
050: addressStr = address.getHostAddress();
051: } else {
052: addressStr = "AUTOMATIC";
053: }
054:
055: // Perform the update.
056: getResponse(in);
057: UpdateProtocol.writeLine(out, "AUTH " + zone + " PASS "
058: + username + " " + password);
059: getResponse(in);
060: UpdateProtocol.writeLine(out, "ADDRESS " + hostname + "."
061: + zone + " " + addressStr);
062: getResponse(in);
063: UpdateProtocol.writeLine(out, "QUIT");
064: getResponse(in);
065: } finally {
066: socket.close();
067: }
068: }
069:
070: // Parse a server response.
071: private static ServerResponse getResponse(BufferedReader in)
072: throws IOException, ServerResponse {
073: String line = in.readLine();
074: ServerResponse response = ServerResponse.parse(line);
075: if (response.isError())
076: throw response;
077: return response;
078: }
079:
080: /*********************************************************************
081: * Update the address of a host.
082: *********************************************************************
083: * @param server The host running the CustomDNS update server.
084: * @param zone The zone in which our hostname resides.
085: * @param username The username to use for the update.
086: * @param password The password to use for the update.
087: * @param hostname The hostname (omit the zone).
088: * @param address The new address. If this is 'null', ask the server
089: * to determine the address automatically.
090: */
091:
092: public static void updateAddress(InetAddress server, String zone,
093: String username, String password, String hostname,
094: InetAddress address) throws IOException, ServerResponse {
095: updateAddress(server, UpdateProtocol.DEFAULT_PORT, zone,
096: username, password, hostname, address);
097: }
098:
099: /*********************************************************************
100: * Update the address of a host.
101: *********************************************************************
102: * @param properties Properties describing the update.
103: */
104:
105: public static void updateAddress(Properties c) throws IOException,
106: ServerResponse, UnknownHostException {
107: // Extract our configuration.
108: InetAddress server = InetAddress
109: .getByName(getProp(c, "server"));
110: String rawport = getProp(c, "port");
111: String zone = getProp(c, "zone");
112: String username = getProp(c, "username");
113: String password = getProp(c, "password");
114: String host = getProp(c, "hostname");
115: String addrStr = getProp(c, "address");
116: InetAddress addr;
117:
118: if (addrStr == null
119: || addrStr.toUpperCase().equals("AUTOMATIC"))
120: addr = null;
121: else
122: addr = InetAddress.getByName(addrStr);
123:
124: // Get our port.
125: if (rawport == null)
126: rawport = Short.toString(UpdateProtocol.DEFAULT_PORT);
127: short port = Short.parseShort(rawport);
128:
129: // Update our address.
130: updateAddress(server, port, zone, username, password, host,
131: addr);
132: }
133:
134: // Get a property.
135: private static String getProp(Properties properties, String key) {
136: return properties.getProperty("customdns.updateclient." + key);
137: }
138:
139: /*********************************************************************
140: * Update the address of a host.
141: *********************************************************************
142: * @param propfile The name of a property file describing the update.
143: */
144:
145: public static void updateAddress(String propfile)
146: throws IOException, ServerResponse, UnknownHostException {
147: // Load our configuration & update our address.
148: Properties c = new SimpleConfiguration(propfile);
149: updateAddress(c);
150: }
151:
152: /*********************************************************************
153: * Update the address of a host.
154: *********************************************************************
155: * @param propfile The name of a property file describing the update.
156: * @param address An address to use instead of the one from the file.
157: */
158:
159: public static void updateAddress(String propfile, String address)
160: throws IOException, ServerResponse, UnknownHostException {
161: // Load our configuration, update our address.
162: Properties c = new SimpleConfiguration(propfile);
163: c.put("customdns.updateclient.address", address);
164: updateAddress(c);
165: }
166:
167: /*********************************************************************
168: * Update an address from the command-line.
169: *********************************************************************
170: * @param args Our command-line arguments. Pass a property file
171: * describing the update to perform.
172: */
173:
174: public static void main(String[] args) {
175: try {
176: if (args.length < 1 || args.length > 2) {
177: System.err.println("Usage: CustomDNS.UpdateClient"
178: + " update.prop [ip address]");
179: System.exit(1);
180: }
181: updateAddress(args[0], (args.length == 2) ? args[1]
182: : "AUTOMATIC");
183: } catch (Exception e) {
184: System.err.println("CustomDNS.UpdateServer: "
185: + e.toString());
186: System.exit(1);
187: }
188: }
189:
190: }
|