Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/voytechs/jnetstream/primitive/address/IpAddress.java


1   /*
2    * File: IpAddress.java
3    * Auth: Mark Bednarczyk
4    * Date: 2001-08-05
5    *   Id: $Id: IpAddress.java,v 1.1.1.1 2003/09/22 16:32:11 voytechs Exp $
6    ********************************************
7    Copyright (C) 2003  Mark Bednarczyk
8   
9    This program is free software; you can redistribute it and/or
10   modify it under the terms of the GNU General Public License
11   as published by the Free Software Foundation; either version 2
12   of the License, or (at your option) any later version.
13  
14   This program is distributed in the hope that it will be useful,
15   but WITHOUT ANY WARRANTY; without even the implied warranty of
16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   GNU General Public License for more details.
18  
19   You should have received a copy of the GNU General Public License
20   along with this program; if not, write to the Free Software
21   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22   ********************************************
23   * $Log: IpAddress.java,v $
24   * Revision 1.1.1.1  2003/09/22 16:32:11  voytechs
25   * Initial import.
26   *
27   * Revision 1.1  2001/09/04 15:33:55  markbe
28   * Initial revision
29   *
30   */
31  package com.voytechs.jnetstream.primitive.address;
32  
33  import java.util.*;
34  import java.net.*;
35  import java.io.Serializable;
36  
37  /**
38   * A Class for storing IP Addresses
39   * Currently is only designed to utilize IPv4 (32bit) addresses.
40   * Class also contains utility methods for DNS lookups.
41   * There are various functions for converting the IP address to a LONG.
42   * Why would you want to use a long for storage of an IP address, in my
43   * case I store IP address in a database as an UNSIGNED INT, java does not
44   * have unsigned numbers so you have to go to next bigger primitive type to store
45   * it or do like other implementations do store the address in a byte array.
46   */
47  public class IpAddress 
48    extends Address {
49  
50    /* Internal attributes */
51  
52    private InetAddress inetAddress = null;
53    private String hostname = null;
54    private String domainname = null;
55  
56  
57    /**
58     * Main constructor taking the array of bytes as the address.
59     * Automatically sets the separator token between '.' and ':' depending
60     * on number of bytes in the address. 4 bytes IPv4 address with '.' separator 
61     * and 32 bytes for IPv6 address with ':' as separator.
62     *
63     * @param address array of bytes making up the address.
64     * @exception IllegalArgumentException if number of bytes is not 4 or 32.
65     */
66    public IpAddress(byte [] address) {
67      super(address);
68  
69      if(address.length == 4)
70        setSeparator('.');
71  
72      else if(address.length == 16) {
73        setFormat("xx:xx:xx:xx:xx:xx:xx:xx");
74        setRadix(16);
75      }
76  
77      else {
78        throw new IllegalArgumentException("IP Address can only be either 32 or 128 bits long");
79      }
80    }
81  
82    /**
83     * Constructor taking a hostname.
84     * This is a conveniece constructor which converts the hostname to
85     * numerical format internally. Currently only IPv4 addresses can be initialized this way
86     * due to java.net library limitations.
87     *
88     * @param hostname hostname or IP address in '.' notation initialize this address with.
89     * @exception UnknownHostException if hostname can not be converted to an IP address. Addresses
90     * in IPv4 '.' notation format always convert and this exception is never thrown.
91     */
92    public IpAddress(String hostname) 
93      throws java.net.UnknownHostException {
94  
95      inetAddress = InetAddress.getByName(hostname);
96  
97      setByteArrayAddress(inetAddress.getAddress());
98  
99      setSeparator('.');
100   }
101 
102   /**
103    * Do a DNS lookup on our IP address by use of DNS reverse lookup.
104    *
105    * @return DNS name of the IP address if exists or null other wise.
106    */
107   public String getHostname() {
108     if(hostname != null) 
109       return(hostname);
110 
111     if(inetAddress == null) {
112       try {
113         inetAddress = InetAddress.getByName(super.stringValue());
114 
115       }
116       catch(java.net.UnknownHostException e) {}
117     }
118 
119     hostname = inetAddress.getHostName();
120     int i = hostname.indexOf('.');
121     if(i != -1) {
122       domainname = hostname.substring(i +1);
123       hostname = hostname.substring(0, i);
124     }
125     else
126       domainname = "";
127 
128     if(hostname == null)
129       return("");
130 
131     return(hostname);
132   }
133 
134   /**
135    * Do a domain name lookup by use of DNS reverse lookup on the IP addresses
136    * @return domain name part of the hostname or null other wise.
137    */
138   public String getDomainname() {
139     if(domainname == null) {
140       getHostname();
141     }
142 
143     return(domainname);
144   }
145 
146   /**
147    * Convert to dot notation string representation of the address. If hostname is
148    * known then hostname.domain is returned other wise the appropriate numerical
149    * representation of the numerical address is returned as a String.
150    *
151    * @return IP address in the dot notation.
152    */
153   public String toString() {
154     if(hostname != null) {
155       return(hostname + "." + domainname);
156     }
157     else {
158       return(super.toString());
159     }
160   }
161 
162   /**
163    * Test function for IpAddress
164    * @param args command line arguments
165    */
166   public static void main(String [] args) {
167     
168     IpAddress ip = null;
169 
170     try {
171         ip = new IpAddress("128.168.5.10");
172     }
173     catch(UnknownHostException uhe) { 
174       System.err.println(uhe.toString());
175     }
176 
177     ip = new IpAddress(new byte[] {
178       1,2,3,4, 1,2,3,4, 1,2,3,4, 1,2,3,4,
179       1,2,3,4, 1,2,3,4, 1,2,3,4, 1,2,3,4
180       });
181 
182     
183 
184     System.out.println("Converted IP=" + ip );
185   }
186 
187 } /* END OF: IpAddress */