aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/java/sip/communicator/impl/protocol/jabber/extensions/jingle/IceUdpTransportPacketExtension.java
blob: 173701f48e85346271c1ce6ef780bce9fe2c401a (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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/*
 * Jitsi, the OpenSource Java VoIP and Instant Messaging client.
 *
 * Copyright @ 2015 Atlassian Pty Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.java.sip.communicator.impl.protocol.jabber.extensions.jingle;

import java.util.*;

import net.java.sip.communicator.impl.protocol.jabber.extensions.*;

import org.jivesoftware.smack.packet.*;

/**
 * An {@link AbstractPacketExtension} implementation for transport elements.
 *
 * @author Emil Ivov
 * @author Lyubomir Marinov
 */
public class IceUdpTransportPacketExtension
    extends AbstractPacketExtension
{
    /**
     * The name of the "transport" element.
     */
    public static final String NAMESPACE
        = "urn:xmpp:jingle:transports:ice-udp:1";

    /**
     * The name of the "transport" element.
     */
    public static final String ELEMENT_NAME = "transport";

    /**
     * The name of the <tt>pwd</tt> ICE attribute.
     */
    public static final String PWD_ATTR_NAME = "pwd";

    /**
     * The name of the <tt>ufrag</tt> ICE attribute.
     */
    public static final String UFRAG_ATTR_NAME = "ufrag";

    /**
     * A list of one or more candidates representing each of the initiator's
     * higher-priority transport candidates as determined in accordance with
     * the ICE methodology.
     */
    private final List<CandidatePacketExtension> candidateList
        = new ArrayList<CandidatePacketExtension>();

    /**
     * Once the parties have connectivity and therefore the initiator has
     * completed ICE as explained in RFC 5245, the initiator MAY communicate
     * the in-use candidate pair in the signalling channel by sending a
     * transport-info message that contains a "remote-candidate" element
     */
    private RemoteCandidatePacketExtension remoteCandidate;

    /**
     * Creates a new {@link IceUdpTransportPacketExtension} instance.
     */
    public IceUdpTransportPacketExtension()
    {
        super(NAMESPACE, ELEMENT_NAME);
    }

    /**
     * Creates a new {@link IceUdpTransportPacketExtension} instance with the
     * specified <tt>namespace</tt> and <tt>elementName</tt>. The purpose of
     * this method is to allow {@link RawUdpTransportPacketExtension} to
     * extend this class.
     *
     * @param namespace the XML namespace that the instance should belong to.
     * @param elementName the name of the element that we would be representing.
     */
    protected IceUdpTransportPacketExtension(String namespace,
                                             String elementName)
    {
        super(namespace, elementName);
    }

    /**
     * Sets the ICE defined password attribute.
     *
     * @param pwd a password <tt>String</tt> as defined in RFC 5245
     */
    public void setPassword(String pwd)
    {
        super.setAttribute(PWD_ATTR_NAME, pwd);
    }

    /**
     * Returns the ICE defined password attribute.
     *
     * @return a password <tt>String</tt> as defined in RFC 5245
     */
    public String getPassword()
    {
        return super.getAttributeAsString(PWD_ATTR_NAME);
    }

    /**
     * Sets the ICE defined user fragment attribute.
     *
     * @param ufrag a user fragment <tt>String</tt> as defined in RFC 5245
     */
    public void setUfrag(String ufrag)
    {
        super.setAttribute(UFRAG_ATTR_NAME, ufrag);
    }

    /**
     * Returns the ICE defined user fragment attribute.
     *
     * @return a user fragment <tt>String</tt> as defined in RFC 5245
     */
    public String getUfrag()
    {
        return super.getAttributeAsString(UFRAG_ATTR_NAME);
    }

    /**
     * Returns this element's child (local or remote) candidate elements.
     *
     * @return this element's child (local or remote) candidate elements.
     */
    @Override
    public List<? extends PacketExtension> getChildExtensions()
    {
        List<PacketExtension> childExtensions
            = new ArrayList<PacketExtension>();
        List<? extends PacketExtension> superChildExtensions
            = super.getChildExtensions();

        childExtensions.addAll(superChildExtensions);

        synchronized (candidateList)
        {
            if (candidateList.size() > 0)
                childExtensions.addAll(candidateList);
            else if (remoteCandidate != null)
                childExtensions.add(remoteCandidate);
        }

        return childExtensions;
    }

    /**
     * Adds <tt>candidate</tt> to the list of {@link CandidatePacketExtension}s
     * registered with this transport.
     *
     * @param candidate the new {@link CandidatePacketExtension} to add to this
     * transport element.
     */
    public void addCandidate(CandidatePacketExtension candidate)
    {
        synchronized(candidateList)
        {
            candidateList.add(candidate);
        }
    }

    /**
     * Removes <tt>candidate</tt> from the list of
     * {@link CandidatePacketExtension}s registered with this transport.
     *
     * @param candidate the <tt>CandidatePacketExtension</tt> to remove from
     * this transport element
     * @return <tt>true</tt> if the list of <tt>CandidatePacketExtension</tt>s
     * registered with this transport contained the specified <tt>candidate</tt>
     */
    public boolean removeCandidate(CandidatePacketExtension candidate)
    {
        synchronized (candidateList)
        {
            return candidateList.remove(candidate);
        }
    }

    /**
     * Removes given <tt>PacketExtension</tt> from the list of child packet
     * extensions. <tt>CandidatePacketExtension</tt> are not taken into account
     * in this method and {@link #removeCandidate(CandidatePacketExtension)}
     * should be used instead.
     *
     * @param childExtension <tt>PacketExtension</tt> instance to be removed
     *        from child packet extensions list.
     *
     * @return <tt>true</tt> if given <tt>childExtension</tt> has been in the
     *         list and was removed or <tt>false</tt> otherwise.
     */
    public boolean removeChildExtension(PacketExtension childExtension)
    {
        List<? extends PacketExtension> childExtensions
            = super.getChildExtensions();

        return childExtensions != null
            && childExtensions.remove(childExtension);
    }

    /**
     * Returns the list of {@link CandidatePacketExtension}s currently
     * registered with this transport.
     *
     * @return the list of {@link CandidatePacketExtension}s currently
     * registered with this transport.
     */
    public List<CandidatePacketExtension> getCandidateList()
    {
        synchronized(candidateList)
        {
            return new ArrayList<CandidatePacketExtension>(candidateList);
        }
    }

    /**
     * Sets <tt>candidate</tt> as the in-use candidate after ICE has terminated.
     *
     * @param candidate the new {@link CandidatePacketExtension} to set as an
     * in-use candidate for this session.
     */
    public void setRemoteCandidate(RemoteCandidatePacketExtension candidate)
    {
        this.remoteCandidate = candidate;
    }

    /**
     * Returns the in-use <tt>candidate</tt> for this session.
     *
     * @return Returns the in-use <tt>candidate</tt> for this session.
     */
    public RemoteCandidatePacketExtension getRemoteCandidate()
    {
        return remoteCandidate;
    }

    /**
     * Tries to determine whether  <tt>childExtension</tt> is a {@link
     * CandidatePacketExtension}, a {@link RemoteCandidatePacketExtension} or
     * something else and then adds it as such.
     *
     * @param childExtension the extension we'd like to add here.
     */
    @Override
    public void addChildExtension(PacketExtension childExtension)
    {
        //first check for RemoteCandidate because they extend Candidate.
        if(childExtension instanceof RemoteCandidatePacketExtension)
            setRemoteCandidate((RemoteCandidatePacketExtension) childExtension);

        else if(childExtension instanceof CandidatePacketExtension)
            addCandidate((CandidatePacketExtension) childExtension);

        else
            super.addChildExtension(childExtension);
    }

    /**
     * Checks whether an 'rtcp-mux' extension has been added to this
     * <tt>IceUdpTransportPacketExtension</tt>.
     * @return <tt>true</tt> if this <tt>IceUdpTransportPacketExtension</tt>
     * has a child with the 'rtcp-mux' name.
     */
    public boolean isRtcpMux()
    {
        for (PacketExtension packetExtension : getChildExtensions())
        {
            if (RtcpmuxPacketExtension.ELEMENT_NAME
                    .equals(packetExtension.getElementName()))
                return true;
        }
        return false;
    }

    /**
     * Clones a specific <tt>IceUdpTransportPacketExtension</tt> and its
     * candidates.
     *
     * @param src the <tt>IceUdpTransportPacketExtension</tt> to be cloned
     * @return a new <tt>IceUdpTransportPacketExtension</tt> instance which has
     * the same run-time type, attributes, namespace, text and candidates as the
     * specified <tt>src</tt>
     * @throws Exception if an error occurs during the cloning of the specified
     * <tt>src</tt> and its candidates
     */
    public static IceUdpTransportPacketExtension cloneTransportAndCandidates(
            IceUdpTransportPacketExtension src)
    {
        return cloneTransportAndCandidates(src, false);
    }

    /**
     * Clones a specific <tt>IceUdpTransportPacketExtension</tt> and its
     * candidates.
     *
     * @param src the <tt>IceUdpTransportPacketExtension</tt> to be cloned
     * @param copyDtls if <tt>true</tt> will also copy
     *                 {@link DtlsFingerprintPacketExtension}.
     * @return a new <tt>IceUdpTransportPacketExtension</tt> instance which has
     * the same run-time type, attributes, namespace, text and candidates as the
     * specified <tt>src</tt>
     * @throws Exception if an error occurs during the cloning of the specified
     * <tt>src</tt> and its candidates
     */
    public static IceUdpTransportPacketExtension cloneTransportAndCandidates(
            IceUdpTransportPacketExtension src, boolean copyDtls)
    {
        if (src == null)
            return null;

        IceUdpTransportPacketExtension dst = AbstractPacketExtension.clone(src);
        // Copy candidates
        for (CandidatePacketExtension srcCand : src.getCandidateList())
        {
            if (!(srcCand instanceof RemoteCandidatePacketExtension))
                dst.addCandidate(
                    AbstractPacketExtension.clone(srcCand));
        }
        // Copy RTCP MUX
        if (src.isRtcpMux())
        {
            dst.addChildExtension(new RtcpmuxPacketExtension());
        }
        // Optionally copy DTLS
        if (copyDtls)
        {
            for (DtlsFingerprintPacketExtension dtlsFingerprint
                : src.getChildExtensionsOfType(
                DtlsFingerprintPacketExtension.class))
            {
                DtlsFingerprintPacketExtension copy
                    = new DtlsFingerprintPacketExtension();

                copy.setFingerprint(dtlsFingerprint.getFingerprint());
                copy.setHash(dtlsFingerprint.getHash());
                copy.setRequired(dtlsFingerprint.getRequired());
                copy.setSetup(dtlsFingerprint.getSetup());

                dst.addChildExtension(copy);
            }
        }
        return dst;
    }
}