00001
00002
00003
00004
00005 package javax.jmdns.impl;
00006
00007 import java.net.InetAddress;
00008 import java.util.Set;
00009 import java.util.logging.Level;
00010 import java.util.logging.Logger;
00011
00012 import javax.jmdns.ServiceInfo;
00013 import javax.jmdns.ServiceInfo.Fields;
00014 import javax.jmdns.impl.JmDNSImpl.ServiceTypeEntry;
00015 import javax.jmdns.impl.constants.DNSConstants;
00016 import javax.jmdns.impl.constants.DNSRecordClass;
00017 import javax.jmdns.impl.constants.DNSRecordType;
00018
00024 public class DNSQuestion extends DNSEntry {
00025 private static Logger logger = Logger.getLogger(DNSQuestion.class.getName());
00026
00030 private static class DNS4Address extends DNSQuestion {
00031 DNS4Address(String name, DNSRecordType type, DNSRecordClass recordClass, boolean unique) {
00032 super(name, type, recordClass, unique);
00033 }
00034
00035 @Override
00036 public void addAnswers(JmDNSImpl jmDNSImpl, Set<DNSRecord> answers) {
00037 DNSRecord answer = jmDNSImpl.getLocalHost().getDNSAddressRecord(this.getRecordType(), DNSRecordClass.UNIQUE, DNSConstants.DNS_TTL);
00038 if (answer != null) {
00039 answers.add(answer);
00040 }
00041 }
00042
00043 @Override
00044 public boolean iAmTheOnlyOne(JmDNSImpl jmDNSImpl) {
00045 String name = this.getName().toLowerCase();
00046 return jmDNSImpl.getLocalHost().getName().equals(name) || jmDNSImpl.getServices().keySet().contains(name);
00047 }
00048
00049 }
00050
00054 private static class DNS6Address extends DNSQuestion {
00055 DNS6Address(String name, DNSRecordType type, DNSRecordClass recordClass, boolean unique) {
00056 super(name, type, recordClass, unique);
00057 }
00058
00059 @Override
00060 public void addAnswers(JmDNSImpl jmDNSImpl, Set<DNSRecord> answers) {
00061 DNSRecord answer = jmDNSImpl.getLocalHost().getDNSAddressRecord(this.getRecordType(), DNSRecordClass.UNIQUE, DNSConstants.DNS_TTL);
00062 if (answer != null) {
00063 answers.add(answer);
00064 }
00065 }
00066
00067 @Override
00068 public boolean iAmTheOnlyOne(JmDNSImpl jmDNSImpl) {
00069 String name = this.getName().toLowerCase();
00070 return jmDNSImpl.getLocalHost().getName().equals(name) || jmDNSImpl.getServices().keySet().contains(name);
00071 }
00072
00073 }
00074
00078 private static class HostInformation extends DNSQuestion {
00079 HostInformation(String name, DNSRecordType type, DNSRecordClass recordClass, boolean unique) {
00080 super(name, type, recordClass, unique);
00081 }
00082 }
00083
00087 private static class Pointer extends DNSQuestion {
00088 Pointer(String name, DNSRecordType type, DNSRecordClass recordClass, boolean unique) {
00089 super(name, type, recordClass, unique);
00090 }
00091
00092 @Override
00093 public void addAnswers(JmDNSImpl jmDNSImpl, Set<DNSRecord> answers) {
00094
00095 for (ServiceInfo serviceInfo : jmDNSImpl.getServices().values()) {
00096 this.addAnswersForServiceInfo(jmDNSImpl, answers, (ServiceInfoImpl) serviceInfo);
00097 }
00098 if (this.isServicesDiscoveryMetaQuery()) {
00099 for (String serviceType : jmDNSImpl.getServiceTypes().keySet()) {
00100 ServiceTypeEntry typeEntry = jmDNSImpl.getServiceTypes().get(serviceType);
00101 answers.add(new DNSRecord.Pointer("_services._dns-sd._udp.local.", DNSRecordClass.CLASS_IN, DNSRecordClass.NOT_UNIQUE, DNSConstants.DNS_TTL, typeEntry.getType()));
00102 }
00103 } else if (this.isReverseLookup()) {
00104 String ipValue = this.getQualifiedNameMap().get(Fields.Instance);
00105 if ((ipValue != null) && (ipValue.length() > 0)) {
00106 InetAddress address = jmDNSImpl.getLocalHost().getInetAddress();
00107 String hostIPAddress = (address != null ? address.getHostAddress() : "");
00108 if (ipValue.equalsIgnoreCase(hostIPAddress)) {
00109 if (this.isV4ReverseLookup()) {
00110 answers.add(jmDNSImpl.getLocalHost().getDNSReverseAddressRecord(DNSRecordType.TYPE_A, DNSRecordClass.NOT_UNIQUE, DNSConstants.DNS_TTL));
00111 }
00112 if (this.isV6ReverseLookup()) {
00113 answers.add(jmDNSImpl.getLocalHost().getDNSReverseAddressRecord(DNSRecordType.TYPE_AAAA, DNSRecordClass.NOT_UNIQUE, DNSConstants.DNS_TTL));
00114 }
00115 }
00116 }
00117 } else if (this.isDomainDiscoveryQuery()) {
00118
00119 }
00120 }
00121
00122 }
00123
00127 private static class Service extends DNSQuestion {
00128 Service(String name, DNSRecordType type, DNSRecordClass recordClass, boolean unique) {
00129 super(name, type, recordClass, unique);
00130 }
00131
00132 @Override
00133 public void addAnswers(JmDNSImpl jmDNSImpl, Set<DNSRecord> answers) {
00134 String loname = this.getName().toLowerCase();
00135 if (jmDNSImpl.getLocalHost().getName().equalsIgnoreCase(loname)) {
00136
00137 answers.addAll(jmDNSImpl.getLocalHost().answers(this.isUnique(), DNSConstants.DNS_TTL));
00138 return;
00139 }
00140
00141 if (jmDNSImpl.getServiceTypes().containsKey(loname)) {
00142 DNSQuestion question = new Pointer(this.getName(), DNSRecordType.TYPE_PTR, this.getRecordClass(), this.isUnique());
00143 question.addAnswers(jmDNSImpl, answers);
00144 return;
00145 }
00146
00147 this.addAnswersForServiceInfo(jmDNSImpl, answers, (ServiceInfoImpl) jmDNSImpl.getServices().get(loname));
00148 }
00149
00150 @Override
00151 public boolean iAmTheOnlyOne(JmDNSImpl jmDNSImpl) {
00152 String name = this.getName().toLowerCase();
00153 return jmDNSImpl.getLocalHost().getName().equals(name) || jmDNSImpl.getServices().keySet().contains(name);
00154 }
00155
00156 }
00157
00161 private static class Text extends DNSQuestion {
00162 Text(String name, DNSRecordType type, DNSRecordClass recordClass, boolean unique) {
00163 super(name, type, recordClass, unique);
00164 }
00165
00166 @Override
00167 public void addAnswers(JmDNSImpl jmDNSImpl, Set<DNSRecord> answers) {
00168 this.addAnswersForServiceInfo(jmDNSImpl, answers, (ServiceInfoImpl) jmDNSImpl.getServices().get(this.getName().toLowerCase()));
00169 }
00170
00171 @Override
00172 public boolean iAmTheOnlyOne(JmDNSImpl jmDNSImpl) {
00173 String name = this.getName().toLowerCase();
00174 return jmDNSImpl.getLocalHost().getName().equals(name) || jmDNSImpl.getServices().keySet().contains(name);
00175 }
00176
00177 }
00178
00182 private static class AllRecords extends DNSQuestion {
00183 AllRecords(String name, DNSRecordType type, DNSRecordClass recordClass, boolean unique) {
00184 super(name, type, recordClass, unique);
00185 }
00186
00187 @Override
00188 public boolean isSameType(DNSEntry entry) {
00189
00190 return (entry != null);
00191 }
00192
00193 @Override
00194 public void addAnswers(JmDNSImpl jmDNSImpl, Set<DNSRecord> answers) {
00195 String loname = this.getName().toLowerCase();
00196 if (jmDNSImpl.getLocalHost().getName().equalsIgnoreCase(loname)) {
00197
00198 answers.addAll(jmDNSImpl.getLocalHost().answers(this.isUnique(), DNSConstants.DNS_TTL));
00199 return;
00200 }
00201
00202 if (jmDNSImpl.getServiceTypes().containsKey(loname)) {
00203 DNSQuestion question = new Pointer(this.getName(), DNSRecordType.TYPE_PTR, this.getRecordClass(), this.isUnique());
00204 question.addAnswers(jmDNSImpl, answers);
00205 return;
00206 }
00207
00208 this.addAnswersForServiceInfo(jmDNSImpl, answers, (ServiceInfoImpl) jmDNSImpl.getServices().get(loname));
00209 }
00210
00211 @Override
00212 public boolean iAmTheOnlyOne(JmDNSImpl jmDNSImpl) {
00213 String name = this.getName().toLowerCase();
00214 return jmDNSImpl.getLocalHost().getName().equals(name) || jmDNSImpl.getServices().keySet().contains(name);
00215 }
00216
00217 }
00218
00219 DNSQuestion(String name, DNSRecordType type, DNSRecordClass recordClass, boolean unique) {
00220 super(name, type, recordClass, unique);
00221 }
00222
00236 public static DNSQuestion newQuestion(String name, DNSRecordType type, DNSRecordClass recordClass, boolean unique) {
00237 switch (type) {
00238 case TYPE_A:
00239 return new DNS4Address(name, type, recordClass, unique);
00240 case TYPE_A6:
00241 return new DNS6Address(name, type, recordClass, unique);
00242 case TYPE_AAAA:
00243 return new DNS6Address(name, type, recordClass, unique);
00244 case TYPE_ANY:
00245 return new AllRecords(name, type, recordClass, unique);
00246 case TYPE_HINFO:
00247 return new HostInformation(name, type, recordClass, unique);
00248 case TYPE_PTR:
00249 return new Pointer(name, type, recordClass, unique);
00250 case TYPE_SRV:
00251 return new Service(name, type, recordClass, unique);
00252 case TYPE_TXT:
00253 return new Text(name, type, recordClass, unique);
00254 default:
00255 return new DNSQuestion(name, type, recordClass, unique);
00256 }
00257 }
00258
00262 boolean answeredBy(DNSEntry rec) {
00263 return this.isSameRecordClass(rec) && this.isSameType(rec) && this.getName().equals(rec.getName());
00264 }
00265
00274 public void addAnswers(JmDNSImpl jmDNSImpl, Set<DNSRecord> answers) {
00275
00276 }
00277
00278 protected void addAnswersForServiceInfo(JmDNSImpl jmDNSImpl, Set<DNSRecord> answers, ServiceInfoImpl info) {
00279 if ((info != null) && info.isAnnounced()) {
00280 if (this.getName().equalsIgnoreCase(info.getQualifiedName()) || this.getName().equalsIgnoreCase(info.getType())) {
00281 answers.addAll(jmDNSImpl.getLocalHost().answers(DNSRecordClass.UNIQUE, DNSConstants.DNS_TTL));
00282 answers.addAll(info.answers(DNSRecordClass.UNIQUE, DNSConstants.DNS_TTL, jmDNSImpl.getLocalHost()));
00283 }
00284 if (logger.isLoggable(Level.FINER)) {
00285 logger.finer(jmDNSImpl.getName() + " DNSQuestion(" + this.getName() + ").addAnswersForServiceInfo(): info: " + info + "\n" + answers);
00286 }
00287 }
00288 }
00289
00290
00291
00292
00293
00294 @Override
00295 public boolean isStale(long now) {
00296 return false;
00297 }
00298
00299
00300
00301
00302
00303 @Override
00304 public boolean isExpired(long now) {
00305 return false;
00306 }
00307
00315 public boolean iAmTheOnlyOne(JmDNSImpl jmDNSImpl) {
00316 return false;
00317 }
00318
00319
00320
00321
00322
00323 @Override
00324 public void toString(StringBuilder aLog) {
00325
00326 }
00327
00328 }