aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/java/sip/communicator/service/protocol/StunServerDescriptor.java
blob: 71e948c9c01b56c43b638194c008921191e3d315 (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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
 * SIP Communicator, the OpenSource Java VoIP and Instant Messaging client.
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */
package net.java.sip.communicator.service.protocol;

import java.util.*;

import net.java.sip.communicator.util.*;
import static net.java.sip.communicator.service.protocol.ProtocolProviderFactory.*;

/**
 * A <tt>StunServerDescriptor</tt> stores information necessary to create a
 * STUN or TURN candidate harvester that we could use with ICE4J. Descriptors
 * are normally initialized by protocol wizards. They are then used to convert
 * the data into a {@link String} form suitable for storage in an accounts
 * properties Map.
 *
 * @author Yana Stamcheva
 * @author Emil Ivov
 */
public class StunServerDescriptor
{
    /**
     * The maximum number of stun servers that we would allow.
     */
    public static final int MAX_STUN_SERVER_COUNT = 100;

    /**
     * The address (IP or FQDN) of the server.
     */
    private String address;

    /**
     * The port of the server.
     */
    private int port;

    /**
     * Indicates if the server can also act as a TURN server (as opposed to a
     * STUN only server).
     */
    private boolean isTurnSupported;

    /**
     * The username that we need to use with the server or <tt>null</tt> if
     * this server does not require a user name.
     */
    private byte[] username;

    /**
     * The password that we need to use when authenticating with the server
     * or <tt>null</tt> if no password is necessary.
     */
    private byte[] password;

    /**
     * Creates an instance of <tt>StunServer</tt> by specifying all parameters.
     *
     * @param address the IP address or FQDN of the STUN server
     * @param port the port of the server
     * @param supportTurn indicates if this STUN server supports TURN
     * @param username the user name for authenticating
     * @param password the password
     */
    public StunServerDescriptor( String  address,
                                 int     port,
                                 boolean supportTurn,
                                 String  username,
                                 String  password)
    {
        this.address = address;
        this.port = port;
        this.isTurnSupported = supportTurn;
        this.username = (username != null) ? StringUtils.getUTF8Bytes(username)
                : "".getBytes();
        this.password = (password != null) ? StringUtils.getUTF8Bytes(password)
                : "".getBytes();
    }

    /**
     * Returns the IP address or FQDN of this server.
     *
     * @return the IP address or FQDN of this server
     */
    public String getAddress()
    {
        return address;
    }

    /**
     * Sets the IP address or FQDN of this server.
     *
     * @param address the IP address or FQDN to set
     */
    public void setAddress(String address)
    {
        this.address = address;
    }

    /**
     * Returns the port of this server.
     *
     * @return the port of this server
     */
    public int getPort()
    {
        return port;
    }

    /**
     * Sets the port corresponding to this server.
     *
     * @param port the port to set
     */
    public void setPort(int port)
    {
        this.port = port;
    }

    /**
     * Indicates if TURN is supported by this server.
     *
     * @return <tt>true</tt> if TURN is supported by this server, otherwise -
     * returns <tt>false</tt>
     */
    public boolean isTurnSupported()
    {
        return isTurnSupported;
    }

    /**
     * Specifies whether this server can also act as a TURN relay.
     *
     * @param turnSupported <tt>true</tt> to indicate that TURN is supported,
     * <tt>false</tt> - otherwise
     */
    public void setTurnSupported(boolean turnSupported)
    {
        this.isTurnSupported = turnSupported;
    }

    /**
     * Returns the username associated to this server.
     *
     * @return the username associated to this server
     */
    public byte[] getUsername()
    {
        return username;
    }

    /**
     * Sets the username associated to this server.
     *
     * @param username the username to set
     */
    public void setUsername(String username)
    {
        this.username = StringUtils.getUTF8Bytes(username);
    }

    /**
     * Returns the password associated to this server username.
     *
     * @return the password associated to this server username
     */
    public byte[] getPassword()
    {
        return password;
    }

    /**
     * Sets the password associated to this server username.
     *
     * @param password the password to set
     */
    public void setPassword(String password)
    {
        this.password = StringUtils.getUTF8Bytes(password);
    }

    /**
     * Stores this descriptor into the specified {@link Map}.The method is meant
     * for use with account property maps. It also allows prepending an account
     * prefix to all property names so that multiple descriptors can be stored
     * in a single {@link Map}.
     *
     * @param props the account properties {@link Map} that we'd like to store
     * this descriptor in.
     * @param namePrefix the prefix that we should prepend to every property
     * name.
     */
    public void storeDescriptor(Map<String, String> props, String namePrefix)
    {
        if(namePrefix == null)
            namePrefix = "";

        props.put(namePrefix + STUN_ADDRESS, getAddress());

        if(getPort() != -1)
            props.put(namePrefix + STUN_PORT, Integer.toString( getPort() ));

        if (getUsername() != null && getUsername().length > 0)
            props.put(namePrefix + STUN_USERNAME,
                      StringUtils.getUTF8String(getUsername()));

        if (getPassword() != null && getPassword().length > 0)
        {
            //props.put(namePrefix + STUN_PASSWORD, new String(getPassword()));
            props.put(namePrefix + "." + STUN_PASSWORD,
                    new String(getPassword()));
        }

        props.put(namePrefix + STUN_IS_TURN_SUPPORTED,
                        Boolean.toString( isTurnSupported() ));
    }

    /**
     * Loads this descriptor from the specified {@link Map}.The method is meant
     * for use with account property maps. It also allows prepending an account
     * prefix to all property names so that multiple descriptors can be read
     * in a single {@link Map}.
     *
     * @param props the account properties {@link Map} that we'd like to load
     * this descriptor from.
     * @param namePrefix the prefix that we should prepend to every property
     * name.
     *
     * @return the newly created descriptor or null if no descriptor was found.
     */
    public static StunServerDescriptor loadDescriptor(
                                        Map<String, String> props,
                                        String              namePrefix)
    {
        if(namePrefix == null)
            namePrefix = "";

        String stunAddress = props.get(namePrefix + STUN_ADDRESS);

        // there doesn't seem to be a stun server with the specified prefix
        if (stunAddress == null)
            return null;

        String stunPortStr = props.get(namePrefix + STUN_PORT);

        int stunPort = -1;

        try
        {
            stunPort = Integer.parseInt(stunPortStr);
        }
        catch(Throwable t)
        {
            //if the port value was wrong we just keep the default: -1
        }

        String stunUsername = props.get(namePrefix + STUN_USERNAME);
        String stunPassword = props.get(namePrefix + STUN_PASSWORD);

        boolean stunIsTurnSupported
            = Boolean.parseBoolean(
                            props.get(namePrefix + STUN_IS_TURN_SUPPORTED));

        StunServerDescriptor stunServer =
                  new StunServerDescriptor( stunAddress,
                                            stunPort,
                                            stunIsTurnSupported,
                                            stunUsername,
                                            stunPassword);

        return stunServer;
    }

    /**
     * Returns a <tt>String</tt> representation of this descriptor
     *
     * @return a <tt>String</tt> representation of this descriptor.
     */
    public String toString()
    {
        return "StunServerDesc: " + getAddress() + "/"
               + getPort()
               + " turnSupp=" + this.isTurnSupported();
    }
}