summaryrefslogtreecommitdiffstats
path: root/dom/media/webrtc/transport/third_party/nICEr/src/stun/addrs.c
blob: 362b7d828e4951a6309d0be206ee43a3ddd7b837 (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
/*
Copyright (c) 2007, Adobe Systems, Incorporated
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

* Redistributions of source code must retain the above copyright
  notice, this list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright
  notice, this list of conditions and the following disclaimer in the
  documentation and/or other materials provided with the distribution.

* Neither the name of Adobe Systems, Network Resonance nor the names of its
  contributors may be used to endorse or promote products derived from
  this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include <csi_platform.h>
#include <assert.h>
#include <string.h>

#ifdef WIN32
#include "addrs-win32.h"
#elif defined(BSD) || defined(DARWIN)
#include "addrs-bsd.h"
#else
#include "addrs-netlink.h"
#endif

#include "stun.h"
#include "addrs.h"
#include "util.h"

static int
nr_stun_is_duplicate_addr(nr_local_addr addrs[], int count, nr_local_addr *addr)
{
    int i;
    int different;

    for (i = 0; i < count; ++i) {
        different = nr_transport_addr_cmp(&addrs[i].addr, &(addr->addr),
          NR_TRANSPORT_ADDR_CMP_MODE_ALL);
        if (!different)
            return 1;  /* duplicate */
    }

    return 0;
}

int
nr_stun_filter_addrs(nr_local_addr addrs[], int remove_loopback, int remove_link_local, int *count)
{
    int r, _status;
    nr_local_addr *tmp = 0;
    int i;
    int n;
    /* We prefer temp ipv6 for their privacy properties. If we cannot get
     * that, we prefer ipv6 that are not based on mac address. */
    int filter_mac_ipv6 = 0;
    int filter_teredo_ipv6 = 0;
    int filter_non_temp_ipv6 = 0;

    tmp = RMALLOC(*count * sizeof(*tmp));
    if (!tmp)
        ABORT(R_NO_MEMORY);

    for (i = 0; i < *count; ++i) {
      if (addrs[i].addr.ip_version == NR_IPV6) {
        if (nr_transport_addr_is_teredo(&addrs[i].addr)) {
            addrs[i].interface.type |= NR_INTERFACE_TYPE_TEREDO;
            /* Prefer teredo over mac-based address. Probably will never see
             * both. */
            filter_mac_ipv6 = 1;
        } else {
          filter_teredo_ipv6 = 1;
        }

        if (!nr_transport_addr_is_mac_based(&addrs[i].addr)) {
          filter_mac_ipv6 = 1;
        }

        if (addrs[i].flags & NR_ADDR_FLAG_TEMPORARY) {
          filter_non_temp_ipv6 = 1;
        }
      }
    }

    n = 0;
    for (i = 0; i < *count; ++i) {
        if (nr_stun_is_duplicate_addr(tmp, n, &addrs[i])) {
            /* skip addrs[i], it's a duplicate */
        }
        else if (remove_loopback && nr_transport_addr_is_loopback(&addrs[i].addr)) {
            /* skip addrs[i], it's a loopback */
        }
        else if (remove_link_local &&
                 nr_transport_addr_is_link_local(&addrs[i].addr)) {
            /* skip addrs[i], it's a link-local address */
        }
        else if (filter_mac_ipv6 &&
                 nr_transport_addr_is_mac_based(&addrs[i].addr)) {
            /* skip addrs[i], it's MAC based */
        }
        else if (filter_teredo_ipv6 &&
                 nr_transport_addr_is_teredo(&addrs[i].addr)) {
            /* skip addrs[i], it's a Teredo address */
        }
        else if (filter_non_temp_ipv6 &&
                 (addrs[i].addr.ip_version == NR_IPV6) &&
                 !(addrs[i].flags & NR_ADDR_FLAG_TEMPORARY)) {
            /* skip addrs[i], it's a non-temporary ipv6, and we have a temporary */
        }
        else {
            /* otherwise, copy it to the temporary array */
            if ((r=nr_local_addr_copy(&tmp[n], &addrs[i])))
                ABORT(r);
            ++n;
        }
    }

    *count = n;

    memset(addrs, 0, *count * sizeof(*addrs));
    /* copy temporary array into passed in/out array */
    for (i = 0; i < *count; ++i) {
        if ((r=nr_local_addr_copy(&addrs[i], &tmp[i])))
            ABORT(r);
    }

    _status = 0;
  abort:
    RFREE(tmp);
    return _status;
}

#ifndef USE_PLATFORM_NR_STUN_GET_ADDRS

int
nr_stun_get_addrs(nr_local_addr addrs[], int maxaddrs, int *count)
{
    int _status=0;
    int i;
    char typestr[100];

    // Ensure output records are always fully defined.  See bug 1589990.
    if (maxaddrs > 0) {
       memset(addrs, 0, maxaddrs * sizeof(nr_local_addr));
    }

    _status = stun_getaddrs_filtered(addrs, maxaddrs, count);

    for (i = 0; i < *count; ++i) {
      nr_local_addr_fmt_info_string(addrs+i,typestr,sizeof(typestr));
      r_log(NR_LOG_STUN, LOG_DEBUG, "Address %d: %s on %s, type: %s\n",
            i,addrs[i].addr.as_string,addrs[i].addr.ifname,typestr);
    }

    return _status;
}

#endif