Dns class in java

Simple *Authoritative DNS Server* in Java

Is there an already written Java DNS Server that only implements authoritative responses. I would like to take the source code and move it into a DNS server we will be developing that will use custom rule sets to decide what TTL to use and what IP address to publish. The server will not be a caching server. It will only return authoritative results and only be published on the WHOIS record for the domains. It will never be called directly. The server will have to publish MX records, A records and SPF/TXT records. The plan is to use DNS to assist in load balancing among gateway-servers on multiple locations (we are aware that DNS has a short reach in this area). Also it will cease to publish IP addesses of gateway-servers when they go down (on purpose or on accident) (granted, DNS will only be able to help during extended outages). We will write the logic for all this ourselves.. but I would very much like to start with a DNS server that has been through a little testing instead of starting from scratch. However, that is only feasible if what we copy from is simple enough. Otherwise,, it could turn out to be a waste of time

Читайте также:  Python несколько графиков рядом

3 Answers 3

I guess what you need is a java library which implements DNS protocol. Take a look at dnsjava

This is very good in terms of complete spec coverage of all types of records and class.

But the issue which you might face with a java based library is performance. DNS servers would be expected to have a high throughput. But yes, you can solve that by throwing more hardware.

If performance is a concern for you , I would suggest to look into unbound

Источник

How to configure hostname resolution to use a custom dns server in java?

When developing or testing a Java application, it may be necessary to use a custom DNS server for hostname resolution. By default, the Java runtime environment uses the system’s configured DNS server for hostname resolution. However, there may be cases where you want to use a different DNS server, such as when testing a network setup or accessing a network that uses a different DNS server. This article outlines a few different ways to configure hostname resolution to use a custom DNS server in Java.

Method 1: Use the java.net.InetAddress class

To configure hostname resolution to use a custom DNS server in Java using the java.net.InetAddress class, you can follow the steps below:

  1. Create an instance of the java.net.InetAddress class using the getByName() method and passing in the hostname you want to resolve:
InetAddress address = InetAddress.getByName("example.com");
  1. Set the system property «sun.net.spi.nameservice.nameservers» to the IP address of your custom DNS server:
System.setProperty("sun.net.spi.nameservice.nameservers", "192.168.1.1");
System.setProperty("sun.net.spi.nameservice.provider.1", "dns,sun");
InetAddress address = InetAddress.getByName("example.com");
import java.net.InetAddress; public class CustomDNSExample  public static void main(String[] args) throws Exception  // Step 1: Create an instance of InetAddress InetAddress address = InetAddress.getByName("example.com"); // Step 2: Set the custom DNS server System.setProperty("sun.net.spi.nameservice.nameservers", "192.168.1.1"); // Step 3: Set the DNS provider System.setProperty("sun.net.spi.nameservice.provider.1", "dns,sun"); // Step 4: Resolve the hostname using the custom DNS server InetAddress customAddress = InetAddress.getByName("example.com"); System.out.println("Default IP address: " + address.getHostAddress()); System.out.println("Custom IP address: " + customAddress.getHostAddress()); > >

Method 2: Use a custom resolver

To configure hostname resolution to use a custom DNS server in Java, you can use a custom resolver. Here’s an example code that shows how to do it:

import org.xbill.DNS.*; public class CustomResolverExample  public static void main(String[] args) throws TextParseException  // create a custom resolver Resolver resolver = new SimpleResolver("mydns.example.com"); // create a lookup object with the custom resolver Lookup lookup = new Lookup("example.com", Type.A); lookup.setResolver(resolver); // perform the lookup Record[] records = lookup.run(); // print the results for (Record record : records)  System.out.println(record); > > >

In this example, we create a custom resolver using the SimpleResolver class from the org.xbill.DNS package. We pass the hostname of our custom DNS server ( mydns.example.com ) to the constructor of the SimpleResolver class.

Next, we create a Lookup object with the domain name we want to resolve ( example.com ) and the record type we’re interested in ( Type.A ). We then set the resolver of the Lookup object to our custom resolver using the setResolver method.

Finally, we perform the lookup by calling the run method of the Lookup object. This returns an array of Record objects, which we can print out to see the results.

That’s it! By using a custom resolver, we can configure hostname resolution to use a specific DNS server in Java.

Method 3: Set the network address.cache.ttl property

To configure hostname resolution to use a custom DNS server in Java, you can set the networkaddress.cache.ttl property. This property specifies the time-to-live (TTL) value for the DNS name lookup cache. By setting this property to 0, you can disable caching and force the JVM to always use the custom DNS server. Here’s how to do it:

// Set the DNS server address String dnsServer = "8.8.8.8"; // Set the networkaddress.cache.ttl property to 0 java.security.Security.setProperty("networkaddress.cache.ttl", "0"); // Create a new InetAddress instance using the custom DNS server InetAddress addr = InetAddress.getByName("example.com", InetAddress.getByAddress(dnsServer, new byte[]8, 8, 8, 8>)); // Print the IP address of the host System.out.println(addr.getHostAddress());

In the code above, we first set the DNS server address to 8.8.8.8 . Then, we set the networkaddress.cache.ttl property to 0 using the java.security.Security.setProperty() method. This disables caching and forces the JVM to always use the custom DNS server.

Next, we create a new InetAddress instance using the getByName() method and passing in the hostname and the custom DNS server address. Finally, we print the IP address of the host using the getHostAddress() method.

Note that setting the networkaddress.cache.ttl property to 0 may have performance implications, as it disables caching. You should only use this approach if you need to always use a custom DNS server.

Источник

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

Basic DNS system implementation using Java

salimtirit/Domain-Name-System-DNS

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

In this project, I implemented a basic Domain Name System (DNS) mechanism using Java. DNS is a distributed and hierarchical naming system used to translate domain names into corresponding IP addresses for accessing requested services on the internet.

image

DNS is a critical component of the internet infrastructure, and it is essential for the successful operation of web services. In this project, I implemented a simple version of DNS as a tree structure, where the domains are organized in this tree accordingly.

image

The project consists of a single package named «question,» and all the classes should be implemented in this package. I was provided with the empty class files and needed to complete the implementation of these classes.

This class represents each of the nodes in the DNS tree structure. The following fields should be defined for this class:

  • Map childNodeList : This field is used for maintaining the tree structure. The keys are represented as strings, and the values are DNS nodes. Each key is a subdomain name, and the value it is mapped represents a child node in the tree structure.
  • boolean validDomain : It shows whether the current node is a valid domain name or just a subdomain that cannot have any IP address.
  • Set ipAddresses : This set is for storing the IP addresses as a list of a domain name.

The constructor of this class creates a DNS node with an empty childNodeList and ipAddresses. Besides, the validDomain is initialized as “false”. After adding the first IP address to the list of a node, its validDomain should be set to true since it represents a valid domain name with a corresponding IP address.

The Client class represents the client-side and consists of a simple implementation of the cache mechanism used in DNS. Within this class, a nested private class CachedContent is implemented. The CachedContent class has the following fields:

After obtaining an IP address for the requested web domain name, it is stored in the cache by using the necessary information. The cache can store only 10 different domain-IP information on a local device. In order to store the 11th one, an old record should be removed. The cache replacement algorithm removes the cache that is least used. This information is provided through int hitNo , which is incremented by one when the local device uses this information to access a web server without requesting the DNS.

This class represents the main DNS structure. The only field it should have is DnsNode root which is the root of the tree. Correspondingly, the constructor of this class only creates and initializes the root node. This class acts as a DNS server. The server should be able to add a new domain name and its corresponding IP address to the DNS tree and return the IP address of a given domain name.

In this project, I implemented a simple version of the Domain Name System (DNS) mechanism as a tree structure using Java. This implementation allowed me to understand the basic functionalities and structure of DNS and how it can be used to translate domain names into IP addresses.

If you have any suggestions or find any issues with the code, please feel free to contribute to the project by opening a pull request or creating an issue.

This is the third project of CMPE160 Introduction to Object Oriented Programming class in Spring 2020.

About

Basic DNS system implementation using Java

Источник

Оцените статью