Home | History | Annotate | Download | only in stack
      1 /*
      2 * Conditions Of Use
      3 *
      4 * This software was developed by employees of the National Institute of
      5 * Standards and Technology (NIST), an agency of the Federal Government.
      6 * Pursuant to title 15 Untied States Code Section 105, works of NIST
      7 * employees are not subject to copyright protection in the United States
      8 * and are considered to be in the public domain.  As a result, a formal
      9 * license is not needed to use the software.
     10 *
     11 * This software is provided by NIST as a service and is expressly
     12 * provided "AS IS."  NIST MAKES NO WARRANTY OF ANY KIND, EXPRESS, IMPLIED
     13 * OR STATUTORY, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF
     14 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT
     15 * AND DATA ACCURACY.  NIST does not warrant or make any representations
     16 * regarding the use of the software or the results thereof, including but
     17 * not limited to the correctness, accuracy, reliability or usefulness of
     18 * the software.
     19 *
     20 * Permission to use this software is contingent upon your acceptance
     21 * of the terms of this agreement
     22 *
     23 * .
     24 *
     25 */
     26 /*******************************************************************************
     27  * Product of NIST/ITL Advanced Networking Technologies Division (ANTD).        *
     28  *******************************************************************************/
     29 package gov.nist.javax.sip.stack;
     30 
     31 import java.io.Serializable;
     32 import java.util.StringTokenizer;
     33 /*
     34  * IPv6 Support added by Emil Ivov (emil_ivov (at) yahoo.com)<br/>
     35  * Network Research Team (http://www-r2.u-strasbg.fr))<br/>
     36  * Louis Pasteur University - Strasbourg - France<br/>
     37  * Bug fix for correct handling of IPV6 Address added by
     38  * Daniel J. Martinez Manzano <dani (at) dif.um.es>
     39  */
     40 /**
     41  * Routing algorithms return a list of hops to which the request is
     42  * routed.
     43  *
     44  * @version 1.2 $Revision: 1.11 $ $Date: 2009/07/17 18:58:13 $
     45  *
     46  * @author M. Ranganathan   <br/>
     47  *
     48  *
     49  *
     50 
     51  *
     52  */
     53 public final class HopImpl extends Object implements javax.sip.address.Hop, Serializable {
     54     protected String host;
     55     protected int port;
     56     protected String transport;
     57 
     58     protected boolean defaultRoute; // This is generated from the proxy addr
     59     protected boolean uriRoute; // This is extracted from the requestURI.
     60 
     61     /**
     62      * Debugging println.
     63      */
     64     public String toString() {
     65         return host + ":" + port + "/" + transport;
     66     }
     67 
     68     /**
     69      * Create new hop given host, port and transport.
     70      * @param hostName hostname
     71      * @param portNumber port
     72      * @param trans transport
     73      */
     74     public HopImpl(String hostName, int portNumber, String trans) {
     75         host = hostName;
     76 
     77         // Added by Daniel J. Martinez Manzano <dani (at) dif.um.es>
     78         // for correct management of IPv6 addresses.
     79         if(host.indexOf(":") >= 0)
     80             if(host.indexOf("[") < 0)
     81                 host = "[" + host + "]";
     82 
     83         port = portNumber;
     84         transport = trans;
     85     }
     86 
     87 
     88     /**
     89      * Creates new Hop
     90      * @param hop is a hop string in the form of host:port/Transport
     91      * @throws IllegalArgument exception if string is not properly formatted or null.
     92      */
     93     HopImpl(String hop) throws IllegalArgumentException {
     94 
     95         if (hop == null)
     96             throw new IllegalArgumentException("Null arg!");
     97 
     98         // System.out.println("hop = " + hop);
     99         int brack = hop.indexOf(']');
    100         int colon = hop.indexOf(':',brack);
    101         int slash = hop.indexOf('/',colon);
    102 
    103         if (colon>0) {
    104             this.host = hop.substring(0,colon);
    105             String portstr;
    106             if (slash>0) {
    107                 portstr = hop.substring(colon+1,slash);
    108                 this.transport = hop.substring(slash+1);
    109             } else {
    110                 portstr = hop.substring(colon+1);
    111                 this.transport = "UDP";
    112             }
    113             try {
    114                 port = Integer.parseInt(portstr);
    115             } catch (NumberFormatException ex) {
    116                 throw new IllegalArgumentException("Bad port spec");
    117             }
    118         } else {
    119             if (slash>0) {
    120                 this.host = hop.substring(0,slash);
    121                 this.transport = hop.substring(slash+1);
    122                 this.port = transport.equalsIgnoreCase("TLS") ? 5061 : 5060;
    123             } else {
    124                 this.host = hop;
    125                 this.transport = "UDP";
    126                 this.port = 5060;
    127             }
    128         }
    129 
    130         // Validate it
    131         if (host == null || host.length() == 0)
    132             throw new IllegalArgumentException("no host!");
    133 
    134         // normalize
    135         this.host = this.host.trim();
    136         this.transport = this.transport.trim();
    137 
    138         if ((brack>0) && host.charAt(0)!='[') {
    139             throw new IllegalArgumentException("Bad IPv6 reference spec");
    140         }
    141 
    142         if (transport.compareToIgnoreCase("UDP") != 0
    143             && transport.compareToIgnoreCase("TLS") != 0
    144             && transport.compareToIgnoreCase("TCP") != 0) {
    145             System.err.println("Bad transport string " + transport);
    146             throw new IllegalArgumentException(hop);
    147         }
    148     }
    149 
    150     /**
    151      * Retruns the host string.
    152      * @return host String
    153      */
    154     public String getHost() {
    155         return host;
    156     }
    157 
    158     /**
    159      * Returns the port.
    160      * @return port integer.
    161      */
    162     public int getPort() {
    163         return port;
    164     }
    165 
    166     /** returns the transport string.
    167      */
    168     public String getTransport() {
    169         return transport;
    170     }
    171 
    172 
    173 
    174     /** Return true if this is uriRoute
    175      */
    176     public boolean isURIRoute() {
    177         return uriRoute;
    178     }
    179 
    180     /** Set the URIRoute flag.
    181      */
    182     public void setURIRouteFlag() {
    183         uriRoute = true;
    184     }
    185 
    186 
    187 
    188 }
    189