0f028975f75c16db00da13c410187c26a097e4db
[WeStealzYourDataz.git] / src / uk / ac / ntu / n0521366 / wsyd / libs / net / Network.java
1 /*
2  * The MIT License
3  *
4  * Copyright 2015 TJ <hacker@iam.tj>.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 package uk.ac.ntu.n0521366.wsyd.libs.net;
25
26 import java.net.InetAddress;
27 import java.net.UnknownHostException;
28
29 /**
30  *
31  * @author TJ <hacker@iam.tj>
32  */
33 public class Network {
34     public static final int PORTS_SERVER_SOCIAL = 50000;
35     public static final int PORTS_SERVER_CHAT = 50001;
36     public static final int PORTS_SERVER_LOG = 50002;
37     public static final int PORTS_MULTICAST_DISCOVERY = 50003;
38     public static final int PORTS_CLIENT_CONTROL_FIRST = 50010;
39     public static final int PORTS_CLIENT_CHAT_FIRST = 51000;
40     public static final int PORTS_EPHEMERAL = 0;
41     
42     /**
43      * Special IPv4 'wildcard' address asking java.net.ServerSocket#bind to bind to all interfaces.
44      */    
45     public static final  InetAddress IPv4_WILDCARD;
46     /**
47      * Multicast IPv4 address the applications use to discover each other.
48      */
49     public static final InetAddress MULTICAST_IP;
50     /**
51      * Unicast IPv4 address for testing network connectivity. 
52      */
53     public static final InetAddress PING_IP;
54
55     /**
56      * Initialisation of class-static final constants.
57      * 
58      * Required in order to initialise static final constant values.
59      * Only runs once when the class is created, not when object instances are constructed.
60      */
61     static {
62         InetAddress temp;
63         /* temporary used to avoid the compiler error:
64          *
65          * variable IPv4_WILDCARD might already have been assigned
66          *
67          * that will be caused if the value was directly assigned to the 
68          * 'static final' variable in both the try and catch clauses.
69          */
70         try {
71             temp = InetAddress.getByAddress(new byte[] {0,0,0,0});
72         } catch (UnknownHostException ex) {
73             temp = null; //FIXME: variable IPv4_WILDCARD might already have been assigned
74         }
75         IPv4_WILDCARD = temp;
76         
77         try {
78             temp  = InetAddress.getByAddress(new byte[] {(byte)239, (byte)192, (byte)3, (byte)4});
79         } catch (UnknownHostException ex) {
80             temp = null; // FIXME: variable MULTICAST_IP might already have been assigned
81         }
82         MULTICAST_IP = temp;
83         try {
84             temp       = InetAddress.getByAddress(new byte[] {(byte)176, (byte)58, (byte)127, (byte)210});
85         } catch (UnknownHostException ex) {
86             temp = null; // FIXME: variable PING_IP might already have been assigned
87         }
88         PING_IP = temp;
89     }
90
91     public static final InetAddress getDefaultRoutedInetAddress() {
92         InetAddress result = null;
93         try  {
94             WSYD_SocketAddress sa = new WSYD_SocketAddress(PING_IP);
95             // TODO: getDefaultRoutedInetAddress() - implement route detection logic
96         } catch(Exception e) {
97             // TODO: getDefaultRoutedInetAddress() handle all Exceptions
98         }
99         return result;
100     }
101 }