aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/fil/libre/repwifiapp/helpers/DhcpSettings.java
blob: c0587a30b248fb834122df390e2a76041b2570e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
//
// Copyright 2017 Filippo "Fil" Bergamo <fil.bergamo@riseup.net>
// 
// This file is part of RepWifiApp.
//
// RepWifiApp is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// RepWifiApp is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with RepWifiApp.  If not, see <http://www.gnu.org/licenses/>.
// 
// ********************************************************************

package fil.libre.repwifiapp.helpers;

import org.apache.http.conn.util.InetAddressUtils;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.Serializable;
import android.nfc.FormatException;

public class DhcpSettings implements Serializable {

    private static final long serialVersionUID = 1L;
    public boolean useDhcp;
    private String _staticIP;
    private int _mask;
    private String _defGw;

    private static final String JSONKEY_DHCP = "DHCP";
    private static final String JSONKEY_STATIC_IP = "StaticIP";
    private static final String JSONKEY_GW = "Gateway";
    
    public DhcpSettings(boolean useDhcp, String staticIP, String subnetMask, String defaultGateway)
                    throws FormatException {
        this(useDhcp, staticIP, Utils.netmaskStringToInt(subnetMask), defaultGateway);
    }

    public DhcpSettings(boolean useDhcp, String staticIP, int subnetMask, String defaultGatweay)
                    throws FormatException {

        this.useDhcp = useDhcp;

        if (!useDhcp) {

            if (!validateParams(staticIP, defaultGatweay, subnetMask)) {
                throw new FormatException("Invalid dhcp parameters!");
            }

            this._staticIP = staticIP;
            this._mask = subnetMask;
            this._defGw = defaultGatweay;

        }

    }

    private DhcpSettings(){
        // inner use
    }
    
    public static DhcpSettings parseSavedSettings(String staticIPwithMask, String defaultGateway){
        
        try {

            String[] ipm = staticIPwithMask.split("/");
            String ip = ipm[0];
            int mask = Integer.parseInt(ipm[1]);

            return new DhcpSettings(false, ip, mask, defaultGateway);
            

        } catch (Exception e) {
            Utils.logError("Exception while parsing DhcpSettings for saved network. Reverting to dhcp.", e);
            return null;
        }
        
    }
    
    public static DhcpSettings getDefault(){
        try {
            return new DhcpSettings(true, null, 24, null);
        } catch (FormatException e) {
            //no format exception can happen.
            return null;
        }
    }
    
    private boolean validateParams(String ip, String gateway, int mask) {

        if (isValidAddress(ip) && isValidAddress(gateway) && isValidMask(mask)) {
            return true;
        } else {
            return false;
        }

    }
    
    public static boolean isValidAddress(String ipAddress){
        return InetAddressUtils.isIPv4Address(ipAddress);
    }
    
    public static boolean isValidMaks(String mask){
        int m = Utils.netmaskStringToInt(mask);
        return isValidMask(m);
    }

    public static boolean isValidMask(int mask){
        if (mask >= 8 && mask <= 32){
            return true;
        }else{
            return false;
        }
    }
    
    public String getStaticIP() {
        if (_staticIP == null){
            return "";
        }
        return _staticIP;
    }

    public String getStaticIPwithMask() {
        return getStaticIP() + "/" + String.valueOf(getSubnetMaskInt());
    }

    public int getSubnetMaskInt() {
        return _mask;
    }
    
    public String getSubnetMaskString(){
        String v = Utils.netmaskIntToString(_mask);
        if (v == null){
            return "";
        }
        return v;
    }
    
    public String getDefaultGateway() {
        if (_defGw == null){
            return "";
        }
        return _defGw;
    }

    public JSONObject toJson(){
        
        JSONObject j = new JSONObject();
        
        try {
            j.put(JSONKEY_DHCP, useDhcp);
            j.put(JSONKEY_GW, getDefaultGateway());
            j.put(JSONKEY_STATIC_IP, getStaticIPwithMask());
            
            return j;
            
        } catch (JSONException e) {
            Utils.logError("Exception while converting DhcpSettings to JSON.", e);
            return null;
        }
        
        
    }
    
    public static DhcpSettings fromJsonObject(JSONObject json){
        
        if (json == null){
            return null;
        }
        
        DhcpSettings sets = new DhcpSettings();
        
        try {
            
            sets.useDhcp = json.getBoolean(JSONKEY_DHCP);
            
            if (json.has(JSONKEY_GW) && ! json.isNull(JSONKEY_GW)){
                sets._defGw = json.getString(JSONKEY_GW);
            }
            
            if (json.has(JSONKEY_STATIC_IP) && !json.isNull(JSONKEY_STATIC_IP)){
                
                String[] splt = json.getString(JSONKEY_STATIC_IP).split("/");
                sets._staticIP = splt[0];
                sets._mask = Integer.parseInt(splt[1]);
                
            }
            
            return sets;
            
        } catch (Exception e) {
            Utils.logError("Exception while parsing json object to DhcpSettings", e);
            return null;
        }
        
    }
    
}