001: // Copyright (c) 1999-2004 Brian Wellington (bwelling@xbill.org)
002:
003: package org.xbill.DNS;
004:
005: import java.net.*;
006: import java.io.*;
007: import java.util.*;
008:
009: /**
010: * Well Known Services - Lists services offered by this host.
011: *
012: * @author Brian Wellington
013: */
014:
015: public class WKSRecord extends Record {
016:
017: public static class Protocol {
018: /**
019: * IP protocol identifiers. This is basically copied out of RFC 1010.
020: */
021:
022: private Protocol() {
023: }
024:
025: /** Internet Control Message */
026: public static final int ICMP = 1;
027:
028: /** Internet Group Management */
029: public static final int IGMP = 2;
030:
031: /** Gateway-to-Gateway */
032: public static final int GGP = 3;
033:
034: /** Stream */
035: public static final int ST = 5;
036:
037: /** Transmission Control */
038: public static final int TCP = 6;
039:
040: /** UCL */
041: public static final int UCL = 7;
042:
043: /** Exterior Gateway Protocol */
044: public static final int EGP = 8;
045:
046: /** any private interior gateway */
047: public static final int IGP = 9;
048:
049: /** BBN RCC Monitoring */
050: public static final int BBN_RCC_MON = 10;
051:
052: /** Network Voice Protocol */
053: public static final int NVP_II = 11;
054:
055: /** PUP */
056: public static final int PUP = 12;
057:
058: /** ARGUS */
059: public static final int ARGUS = 13;
060:
061: /** EMCON */
062: public static final int EMCON = 14;
063:
064: /** Cross Net Debugger */
065: public static final int XNET = 15;
066:
067: /** Chaos */
068: public static final int CHAOS = 16;
069:
070: /** User Datagram */
071: public static final int UDP = 17;
072:
073: /** Multiplexing */
074: public static final int MUX = 18;
075:
076: /** DCN Measurement Subsystems */
077: public static final int DCN_MEAS = 19;
078:
079: /** Host Monitoring */
080: public static final int HMP = 20;
081:
082: /** Packet Radio Measurement */
083: public static final int PRM = 21;
084:
085: /** XEROX NS IDP */
086: public static final int XNS_IDP = 22;
087:
088: /** Trunk-1 */
089: public static final int TRUNK_1 = 23;
090:
091: /** Trunk-2 */
092: public static final int TRUNK_2 = 24;
093:
094: /** Leaf-1 */
095: public static final int LEAF_1 = 25;
096:
097: /** Leaf-2 */
098: public static final int LEAF_2 = 26;
099:
100: /** Reliable Data Protocol */
101: public static final int RDP = 27;
102:
103: /** Internet Reliable Transaction */
104: public static final int IRTP = 28;
105:
106: /** ISO Transport Protocol Class 4 */
107: public static final int ISO_TP4 = 29;
108:
109: /** Bulk Data Transfer Protocol */
110: public static final int NETBLT = 30;
111:
112: /** MFE Network Services Protocol */
113: public static final int MFE_NSP = 31;
114:
115: /** MERIT Internodal Protocol */
116: public static final int MERIT_INP = 32;
117:
118: /** Sequential Exchange Protocol */
119: public static final int SEP = 33;
120:
121: /** CFTP */
122: public static final int CFTP = 62;
123:
124: /** SATNET and Backroom EXPAK */
125: public static final int SAT_EXPAK = 64;
126:
127: /** MIT Subnet Support */
128: public static final int MIT_SUBNET = 65;
129:
130: /** MIT Remote Virtual Disk Protocol */
131: public static final int RVD = 66;
132:
133: /** Internet Pluribus Packet Core */
134: public static final int IPPC = 67;
135:
136: /** SATNET Monitoring */
137: public static final int SAT_MON = 69;
138:
139: /** Internet Packet Core Utility */
140: public static final int IPCV = 71;
141:
142: /** Backroom SATNET Monitoring */
143: public static final int BR_SAT_MON = 76;
144:
145: /** WIDEBAND Monitoring */
146: public static final int WB_MON = 78;
147:
148: /** WIDEBAND EXPAK */
149: public static final int WB_EXPAK = 79;
150:
151: private static Mnemonic protocols = new Mnemonic("IP protocol",
152: Mnemonic.CASE_LOWER);
153:
154: static {
155: protocols.setMaximum(0xFF);
156: protocols.setNumericAllowed(true);
157:
158: protocols.add(ICMP, "icmp");
159: protocols.add(IGMP, "igmp");
160: protocols.add(GGP, "ggp");
161: protocols.add(ST, "st");
162: protocols.add(TCP, "tcp");
163: protocols.add(UCL, "ucl");
164: protocols.add(EGP, "egp");
165: protocols.add(IGP, "igp");
166: protocols.add(BBN_RCC_MON, "bbn-rcc-mon");
167: protocols.add(NVP_II, "nvp-ii");
168: protocols.add(PUP, "pup");
169: protocols.add(ARGUS, "argus");
170: protocols.add(EMCON, "emcon");
171: protocols.add(XNET, "xnet");
172: protocols.add(CHAOS, "chaos");
173: protocols.add(UDP, "udp");
174: protocols.add(MUX, "mux");
175: protocols.add(DCN_MEAS, "dcn-meas");
176: protocols.add(HMP, "hmp");
177: protocols.add(PRM, "prm");
178: protocols.add(XNS_IDP, "xns-idp");
179: protocols.add(TRUNK_1, "trunk-1");
180: protocols.add(TRUNK_2, "trunk-2");
181: protocols.add(LEAF_1, "leaf-1");
182: protocols.add(LEAF_2, "leaf-2");
183: protocols.add(RDP, "rdp");
184: protocols.add(IRTP, "irtp");
185: protocols.add(ISO_TP4, "iso-tp4");
186: protocols.add(NETBLT, "netblt");
187: protocols.add(MFE_NSP, "mfe-nsp");
188: protocols.add(MERIT_INP, "merit-inp");
189: protocols.add(SEP, "sep");
190: protocols.add(CFTP, "cftp");
191: protocols.add(SAT_EXPAK, "sat-expak");
192: protocols.add(MIT_SUBNET, "mit-subnet");
193: protocols.add(RVD, "rvd");
194: protocols.add(IPPC, "ippc");
195: protocols.add(SAT_MON, "sat-mon");
196: protocols.add(IPCV, "ipcv");
197: protocols.add(BR_SAT_MON, "br-sat-mon");
198: protocols.add(WB_MON, "wb-mon");
199: protocols.add(WB_EXPAK, "wb-expak");
200: }
201:
202: /**
203: * Converts an IP protocol value into its textual representation
204: */
205: public static String string(int type) {
206: return protocols.getText(type);
207: }
208:
209: /**
210: * Converts a textual representation of an IP protocol into its
211: * numeric code. Integers in the range 0..255 are also accepted.
212: * @param s The textual representation of the protocol
213: * @return The protocol code, or -1 on error.
214: */
215: public static int value(String s) {
216: return protocols.getValue(s);
217: }
218: }
219:
220: public static class Service {
221: /**
222: * TCP/UDP services. This is basically copied out of RFC 1010,
223: * with MIT-ML-DEV removed, as it is not unique, and the description
224: * of SWIFT-RVF fixed.
225: */
226:
227: private Service() {
228: }
229:
230: /** Remote Job Entry */
231: public static final int RJE = 5;
232:
233: /** Echo */
234: public static final int ECHO = 7;
235:
236: /** Discard */
237: public static final int DISCARD = 9;
238:
239: /** Active Users */
240: public static final int USERS = 11;
241:
242: /** Daytime */
243: public static final int DAYTIME = 13;
244:
245: /** Quote of the Day */
246: public static final int QUOTE = 17;
247:
248: /** Character Generator */
249: public static final int CHARGEN = 19;
250:
251: /** File Transfer [Default Data] */
252: public static final int FTP_DATA = 20;
253:
254: /** File Transfer [Control] */
255: public static final int FTP = 21;
256:
257: /** Telnet */
258: public static final int TELNET = 23;
259:
260: /** Simple Mail Transfer */
261: public static final int SMTP = 25;
262:
263: /** NSW User System FE */
264: public static final int NSW_FE = 27;
265:
266: /** MSG ICP */
267: public static final int MSG_ICP = 29;
268:
269: /** MSG Authentication */
270: public static final int MSG_AUTH = 31;
271:
272: /** Display Support Protocol */
273: public static final int DSP = 33;
274:
275: /** Time */
276: public static final int TIME = 37;
277:
278: /** Resource Location Protocol */
279: public static final int RLP = 39;
280:
281: /** Graphics */
282: public static final int GRAPHICS = 41;
283:
284: /** Host Name Server */
285: public static final int NAMESERVER = 42;
286:
287: /** Who Is */
288: public static final int NICNAME = 43;
289:
290: /** MPM FLAGS Protocol */
291: public static final int MPM_FLAGS = 44;
292:
293: /** Message Processing Module [recv] */
294: public static final int MPM = 45;
295:
296: /** MPM [default send] */
297: public static final int MPM_SND = 46;
298:
299: /** NI FTP */
300: public static final int NI_FTP = 47;
301:
302: /** Login Host Protocol */
303: public static final int LOGIN = 49;
304:
305: /** IMP Logical Address Maintenance */
306: public static final int LA_MAINT = 51;
307:
308: /** Domain Name Server */
309: public static final int DOMAIN = 53;
310:
311: /** ISI Graphics Language */
312: public static final int ISI_GL = 55;
313:
314: /** NI MAIL */
315: public static final int NI_MAIL = 61;
316:
317: /** VIA Systems - FTP */
318: public static final int VIA_FTP = 63;
319:
320: /** TACACS-Database Service */
321: public static final int TACACS_DS = 65;
322:
323: /** Bootstrap Protocol Server */
324: public static final int BOOTPS = 67;
325:
326: /** Bootstrap Protocol Client */
327: public static final int BOOTPC = 68;
328:
329: /** Trivial File Transfer */
330: public static final int TFTP = 69;
331:
332: /** Remote Job Service */
333: public static final int NETRJS_1 = 71;
334:
335: /** Remote Job Service */
336: public static final int NETRJS_2 = 72;
337:
338: /** Remote Job Service */
339: public static final int NETRJS_3 = 73;
340:
341: /** Remote Job Service */
342: public static final int NETRJS_4 = 74;
343:
344: /** Finger */
345: public static final int FINGER = 79;
346:
347: /** HOSTS2 Name Server */
348: public static final int HOSTS2_NS = 81;
349:
350: /** SU/MIT Telnet Gateway */
351: public static final int SU_MIT_TG = 89;
352:
353: /** MIT Dover Spooler */
354: public static final int MIT_DOV = 91;
355:
356: /** Device Control Protocol */
357: public static final int DCP = 93;
358:
359: /** SUPDUP */
360: public static final int SUPDUP = 95;
361:
362: /** Swift Remote Virtual File Protocol */
363: public static final int SWIFT_RVF = 97;
364:
365: /** TAC News */
366: public static final int TACNEWS = 98;
367:
368: /** Metagram Relay */
369: public static final int METAGRAM = 99;
370:
371: /** NIC Host Name Server */
372: public static final int HOSTNAME = 101;
373:
374: /** ISO-TSAP */
375: public static final int ISO_TSAP = 102;
376:
377: /** X400 */
378: public static final int X400 = 103;
379:
380: /** X400-SND */
381: public static final int X400_SND = 104;
382:
383: /** Mailbox Name Nameserver */
384: public static final int CSNET_NS = 105;
385:
386: /** Remote Telnet Service */
387: public static final int RTELNET = 107;
388:
389: /** Post Office Protocol - Version 2 */
390: public static final int POP_2 = 109;
391:
392: /** SUN Remote Procedure Call */
393: public static final int SUNRPC = 111;
394:
395: /** Authentication Service */
396: public static final int AUTH = 113;
397:
398: /** Simple File Transfer Protocol */
399: public static final int SFTP = 115;
400:
401: /** UUCP Path Service */
402: public static final int UUCP_PATH = 117;
403:
404: /** Network News Transfer Protocol */
405: public static final int NNTP = 119;
406:
407: /** HYDRA Expedited Remote Procedure */
408: public static final int ERPC = 121;
409:
410: /** Network Time Protocol */
411: public static final int NTP = 123;
412:
413: /** Locus PC-Interface Net Map Server */
414: public static final int LOCUS_MAP = 125;
415:
416: /** Locus PC-Interface Conn Server */
417: public static final int LOCUS_CON = 127;
418:
419: /** Password Generator Protocol */
420: public static final int PWDGEN = 129;
421:
422: /** CISCO FNATIVE */
423: public static final int CISCO_FNA = 130;
424:
425: /** CISCO TNATIVE */
426: public static final int CISCO_TNA = 131;
427:
428: /** CISCO SYSMAINT */
429: public static final int CISCO_SYS = 132;
430:
431: /** Statistics Service */
432: public static final int STATSRV = 133;
433:
434: /** INGRES-NET Service */
435: public static final int INGRES_NET = 134;
436:
437: /** Location Service */
438: public static final int LOC_SRV = 135;
439:
440: /** PROFILE Naming System */
441: public static final int PROFILE = 136;
442:
443: /** NETBIOS Name Service */
444: public static final int NETBIOS_NS = 137;
445:
446: /** NETBIOS Datagram Service */
447: public static final int NETBIOS_DGM = 138;
448:
449: /** NETBIOS Session Service */
450: public static final int NETBIOS_SSN = 139;
451:
452: /** EMFIS Data Service */
453: public static final int EMFIS_DATA = 140;
454:
455: /** EMFIS Control Service */
456: public static final int EMFIS_CNTL = 141;
457:
458: /** Britton-Lee IDM */
459: public static final int BL_IDM = 142;
460:
461: /** Survey Measurement */
462: public static final int SUR_MEAS = 243;
463:
464: /** LINK */
465: public static final int LINK = 245;
466:
467: private static Mnemonic services = new Mnemonic(
468: "TCP/UDP service", Mnemonic.CASE_LOWER);
469:
470: static {
471: services.setMaximum(0xFFFF);
472: services.setNumericAllowed(true);
473:
474: services.add(RJE, "rje");
475: services.add(ECHO, "echo");
476: services.add(DISCARD, "discard");
477: services.add(USERS, "users");
478: services.add(DAYTIME, "daytime");
479: services.add(QUOTE, "quote");
480: services.add(CHARGEN, "chargen");
481: services.add(FTP_DATA, "ftp-data");
482: services.add(FTP, "ftp");
483: services.add(TELNET, "telnet");
484: services.add(SMTP, "smtp");
485: services.add(NSW_FE, "nsw-fe");
486: services.add(MSG_ICP, "msg-icp");
487: services.add(MSG_AUTH, "msg-auth");
488: services.add(DSP, "dsp");
489: services.add(TIME, "time");
490: services.add(RLP, "rlp");
491: services.add(GRAPHICS, "graphics");
492: services.add(NAMESERVER, "nameserver");
493: services.add(NICNAME, "nicname");
494: services.add(MPM_FLAGS, "mpm-flags");
495: services.add(MPM, "mpm");
496: services.add(MPM_SND, "mpm-snd");
497: services.add(NI_FTP, "ni-ftp");
498: services.add(LOGIN, "login");
499: services.add(LA_MAINT, "la-maint");
500: services.add(DOMAIN, "domain");
501: services.add(ISI_GL, "isi-gl");
502: services.add(NI_MAIL, "ni-mail");
503: services.add(VIA_FTP, "via-ftp");
504: services.add(TACACS_DS, "tacacs-ds");
505: services.add(BOOTPS, "bootps");
506: services.add(BOOTPC, "bootpc");
507: services.add(TFTP, "tftp");
508: services.add(NETRJS_1, "netrjs-1");
509: services.add(NETRJS_2, "netrjs-2");
510: services.add(NETRJS_3, "netrjs-3");
511: services.add(NETRJS_4, "netrjs-4");
512: services.add(FINGER, "finger");
513: services.add(HOSTS2_NS, "hosts2-ns");
514: services.add(SU_MIT_TG, "su-mit-tg");
515: services.add(MIT_DOV, "mit-dov");
516: services.add(DCP, "dcp");
517: services.add(SUPDUP, "supdup");
518: services.add(SWIFT_RVF, "swift-rvf");
519: services.add(TACNEWS, "tacnews");
520: services.add(METAGRAM, "metagram");
521: services.add(HOSTNAME, "hostname");
522: services.add(ISO_TSAP, "iso-tsap");
523: services.add(X400, "x400");
524: services.add(X400_SND, "x400-snd");
525: services.add(CSNET_NS, "csnet-ns");
526: services.add(RTELNET, "rtelnet");
527: services.add(POP_2, "pop-2");
528: services.add(SUNRPC, "sunrpc");
529: services.add(AUTH, "auth");
530: services.add(SFTP, "sftp");
531: services.add(UUCP_PATH, "uucp-path");
532: services.add(NNTP, "nntp");
533: services.add(ERPC, "erpc");
534: services.add(NTP, "ntp");
535: services.add(LOCUS_MAP, "locus-map");
536: services.add(LOCUS_CON, "locus-con");
537: services.add(PWDGEN, "pwdgen");
538: services.add(CISCO_FNA, "cisco-fna");
539: services.add(CISCO_TNA, "cisco-tna");
540: services.add(CISCO_SYS, "cisco-sys");
541: services.add(STATSRV, "statsrv");
542: services.add(INGRES_NET, "ingres-net");
543: services.add(LOC_SRV, "loc-srv");
544: services.add(PROFILE, "profile");
545: services.add(NETBIOS_NS, "netbios-ns");
546: services.add(NETBIOS_DGM, "netbios-dgm");
547: services.add(NETBIOS_SSN, "netbios-ssn");
548: services.add(EMFIS_DATA, "emfis-data");
549: services.add(EMFIS_CNTL, "emfis-cntl");
550: services.add(BL_IDM, "bl-idm");
551: services.add(SUR_MEAS, "sur-meas");
552: services.add(LINK, "link");
553: }
554:
555: /**
556: * Converts a TCP/UDP service port number into its textual
557: * representation.
558: */
559: public static String string(int type) {
560: return services.getText(type);
561: }
562:
563: /**
564: * Converts a textual representation of a TCP/UDP service into its
565: * port number. Integers in the range 0..65535 are also accepted.
566: * @param s The textual representation of the service.
567: * @return The port number, or -1 on error.
568: */
569: public static int value(String s) {
570: return services.getValue(s);
571: }
572: }
573:
574: private byte[] address;
575: private int protocol;
576: private int[] services;
577:
578: WKSRecord() {
579: }
580:
581: Record getObject() {
582: return new WKSRecord();
583: }
584:
585: /**
586: * Creates a WKS Record from the given data
587: * @param address The IP address
588: * @param protocol The IP protocol number
589: * @param services An array of supported services, represented by port number.
590: */
591: public WKSRecord(Name name, int dclass, long ttl,
592: InetAddress address, int protocol, int[] services) {
593: super (name, Type.WKS, dclass, ttl);
594: if (Address.familyOf(address) != Address.IPv4)
595: throw new IllegalArgumentException("invalid IPv4 address");
596: this .address = address.getAddress();
597: this .protocol = checkU8("protocol", protocol);
598: for (int i = 0; i < services.length; i++) {
599: checkU16("service", services[i]);
600: }
601: this .services = new int[services.length];
602: System
603: .arraycopy(services, 0, this .services, 0,
604: services.length);
605: Arrays.sort(this .services);
606: }
607:
608: void rrFromWire(DNSInput in) throws IOException {
609: address = in.readByteArray(4);
610: protocol = in.readU8();
611: byte[] array = in.readByteArray();
612: List list = new ArrayList();
613: for (int i = 0; i < array.length; i++) {
614: for (int j = 0; j < 8; j++) {
615: int octet = array[i] & 0xFF;
616: if ((octet & (1 << (7 - j))) != 0) {
617: list.add(new Integer(i * 8 + j));
618: }
619: }
620: }
621: services = new int[list.size()];
622: for (int i = 0; i < list.size(); i++) {
623: services[i] = ((Integer) list.get(i)).intValue();
624: }
625: }
626:
627: void rdataFromString(Tokenizer st, Name origin) throws IOException {
628: String s = st.getString();
629: address = Address.toByteArray(s, Address.IPv4);
630: if (address == null)
631: throw st.exception("invalid address");
632:
633: s = st.getString();
634: protocol = Protocol.value(s);
635: if (protocol < 0) {
636: throw st.exception("Invalid IP protocol: " + s);
637: }
638:
639: List list = new ArrayList();
640: while (true) {
641: Tokenizer.Token t = st.get();
642: if (!t.isString())
643: break;
644: int service = Service.value(t.value);
645: if (service < 0) {
646: throw st.exception("Invalid TCP/UDP service: "
647: + t.value);
648: }
649: list.add(new Integer(service));
650: }
651: st.unget();
652: services = new int[list.size()];
653: for (int i = 0; i < list.size(); i++) {
654: services[i] = ((Integer) list.get(i)).intValue();
655: }
656: }
657:
658: /**
659: * Converts rdata to a String
660: */
661: String rrToString() {
662: StringBuffer sb = new StringBuffer();
663: sb.append(Address.toDottedQuad(address));
664: sb.append(" ");
665: sb.append(protocol);
666: for (int i = 0; i < services.length; i++) {
667: sb.append(" " + services[i]);
668: }
669: return sb.toString();
670: }
671:
672: /**
673: * Returns the IP address.
674: */
675: public InetAddress getAddress() {
676: try {
677: return InetAddress.getByAddress(address);
678: } catch (UnknownHostException e) {
679: return null;
680: }
681: }
682:
683: /**
684: * Returns the IP protocol.
685: */
686: public int getProtocol() {
687: return protocol;
688: }
689:
690: /**
691: * Returns the services provided by the host on the specified address.
692: */
693: public int[] getServices() {
694: return services;
695: }
696:
697: void rrToWire(DNSOutput out, Compression c, boolean canonical) {
698: out.writeByteArray(address);
699: out.writeU8(protocol);
700: int highestPort = services[services.length - 1];
701: byte[] array = new byte[highestPort / 8 + 1];
702: for (int i = 0; i < services.length; i++) {
703: int port = services[i];
704: array[port / 8] |= (1 << (7 - port % 8));
705: }
706: out.writeByteArray(array);
707: }
708:
709: }
|