home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2007 April / PCpro_2007_04.ISO / files / dsl / jNetTool.exe / org / xbill / DNS / KEYBase.class (.txt) < prev    next >
Encoding:
Java Class File  |  2005-06-05  |  3.1 KB  |  115 lines

  1. package org.xbill.DNS;
  2.  
  3. import java.io.IOException;
  4. import org.xbill.DNS.utils.base64;
  5.  
  6. abstract class KEYBase extends Record {
  7.    protected int flags;
  8.    protected int proto;
  9.    protected int alg;
  10.    protected byte[] key;
  11.    protected int footprint = -1;
  12.  
  13.    protected KEYBase() {
  14.    }
  15.  
  16.    public KEYBase(Name name, int type, int dclass, long ttl, int flags, int proto, int alg, byte[] key) {
  17.       super(name, type, dclass, ttl);
  18.       this.flags = Record.checkU16("flags", flags);
  19.       this.proto = Record.checkU8("proto", proto);
  20.       this.alg = Record.checkU8("alg", alg);
  21.       this.key = key;
  22.    }
  23.  
  24.    void rrFromWire(DNSInput in) throws IOException {
  25.       this.flags = in.readU16();
  26.       this.proto = in.readU8();
  27.       this.alg = in.readU8();
  28.       if (in.remaining() > 0) {
  29.          this.key = in.readByteArray();
  30.       }
  31.  
  32.    }
  33.  
  34.    String rrToString() {
  35.       StringBuffer sb = new StringBuffer();
  36.       sb.append(this.flags);
  37.       sb.append(" ");
  38.       sb.append(this.proto);
  39.       sb.append(" ");
  40.       sb.append(this.alg);
  41.       if (this.key != null) {
  42.          if (Options.check("multiline")) {
  43.             sb.append(" (\n");
  44.             sb.append(base64.formatString(this.key, 64, "\t", true));
  45.             sb.append(" ; key_tag = ");
  46.             sb.append(this.getFootprint());
  47.          } else {
  48.             sb.append(" ");
  49.             sb.append(base64.toString(this.key));
  50.          }
  51.       }
  52.  
  53.       return sb.toString();
  54.    }
  55.  
  56.    public int getFlags() {
  57.       return this.flags;
  58.    }
  59.  
  60.    public int getProtocol() {
  61.       return this.proto;
  62.    }
  63.  
  64.    public int getAlgorithm() {
  65.       return this.alg;
  66.    }
  67.  
  68.    public byte[] getKey() {
  69.       return this.key;
  70.    }
  71.  
  72.    public int getFootprint() {
  73.       if (this.footprint >= 0) {
  74.          return this.footprint;
  75.       } else {
  76.          int foot = 0;
  77.          DNSOutput out = new DNSOutput();
  78.          this.rrToWire(out, (Compression)null, false);
  79.          byte[] rdata = out.toByteArray();
  80.          if (this.alg == 1) {
  81.             int d1 = rdata[rdata.length - 3] & 255;
  82.             int d2 = rdata[rdata.length - 2] & 255;
  83.             foot = (d1 << 8) + d2;
  84.          } else {
  85.             int i;
  86.             for(i = 0; i < rdata.length - 1; i += 2) {
  87.                int d1 = rdata[i] & 255;
  88.                int d2 = rdata[i + 1] & 255;
  89.                foot += (d1 << 8) + d2;
  90.             }
  91.  
  92.             if (i < rdata.length) {
  93.                int d1 = rdata[i] & 255;
  94.                foot += d1 << 8;
  95.             }
  96.  
  97.             foot += foot >> 16 & '\uffff';
  98.          }
  99.  
  100.          this.footprint = foot & '\uffff';
  101.          return this.footprint;
  102.       }
  103.    }
  104.  
  105.    void rrToWire(DNSOutput out, Compression c, boolean canonical) {
  106.       out.writeU16(this.flags);
  107.       out.writeU8(this.proto);
  108.       out.writeU8(this.alg);
  109.       if (this.key != null) {
  110.          out.writeByteArray(this.key);
  111.       }
  112.  
  113.    }
  114. }
  115.