6cc4e12f3a0f09726c0d57b04afcd56de801f90a
[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     
41     /**
42      * Special IPv4 'wildcard' address asking java.net.ServerSocket#bind to bind to all interfaces.
43      */    
44     public static final  InetAddress IPv4_WILDCARD;
45     /**
46      * Multicast IPv4 address the applications use to discover each other.
47      */
48     public static final InetAddress MULTICAST_IP;
49     /**
50      * Unicast IPv4 address for testing network connectivity. 
51      */
52     public static final InetAddress PING_IP;
53
54     /**
55      * Initialisation of class-static final constants.
56      * 
57      * Required in order to initialise static final constant values.
58      * Only runs once when the class is created, not when object instances are constructed.
59      */
60     static {
61         InetAddress temp;
62         /* temporary used to avoid the compiler error:
63          *
64          * variable IPv4_WILDCARD might already have been assigned
65          *
66          * that will be caused if the value was directly assigned to the 
67          * 'static final' variable in both the try and catch clauses.
68          */
69         try {
70             temp = InetAddress.getByAddress(new byte[] {0,0,0,0});
71         } catch (UnknownHostException ex) {
72             temp = null; //FIXME: variable IPv4_WILDCARD might already have been assigned
73         }
74         IPv4_WILDCARD = temp;
75         
76         try {
77             temp  = InetAddress.getByAddress(new byte[] {(byte)239, (byte)192, (byte)3, (byte)4});
78         } catch (UnknownHostException ex) {
79             temp = null; // FIXME: variable MULTICAST_IP might already have been assigned
80         }
81         MULTICAST_IP = temp;
82         try {
83             temp       = InetAddress.getByAddress(new byte[] {(byte)176, (byte)58, (byte)127, (byte)210});
84         } catch (UnknownHostException ex) {
85             temp = null; // FIXME: variable PING_IP might already have been assigned
86         }
87         PING_IP = temp;
88     }
89
90     public static final InetAddress getDefaultRoutedInetAddress() {
91         InetAddress result = null;
92         try  {
93             WSYD_SocketAddress sa = new WSYD_SocketAddress(PING_IP);
94             // TODO: getDefaultRoutedInetAddress() - implement route detection logic
95         } catch(Exception e) {
96             // TODO: getDefaultRoutedInetAddress() handle all Exceptions
97         }
98         return result;
99     }
100 }