Tristan Matthews | 0a329cc | 2013-07-17 13:20:14 -0400 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
| 3 | * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com) |
| 4 | * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 | */ |
| 20 | #ifndef __PJNATH_NAT_DETECT_H__ |
| 21 | #define __PJNATH_NAT_DETECT_H__ |
| 22 | |
| 23 | /** |
| 24 | * @file ice_session.h |
| 25 | * @brief ICE session management |
| 26 | */ |
| 27 | #include <pjnath/stun_session.h> |
| 28 | |
| 29 | |
| 30 | PJ_BEGIN_DECL |
| 31 | |
| 32 | |
| 33 | /** |
| 34 | * @defgroup PJNATH_NAT_DETECT NAT Classification/Detection Tool |
| 35 | * @brief NAT Classification/Detection Tool |
| 36 | * @ingroup PJNATH |
| 37 | * @{ |
| 38 | * |
| 39 | * This module provides one function to perform NAT classification and |
| 40 | * detection. NAT type detection is performed by calling |
| 41 | * #pj_stun_detect_nat_type() function. |
| 42 | */ |
| 43 | |
| 44 | |
| 45 | /** |
| 46 | * This enumeration describes the NAT types, as specified by RFC 3489 |
| 47 | * Section 5, NAT Variations. |
| 48 | */ |
| 49 | typedef enum pj_stun_nat_type |
| 50 | { |
| 51 | /** |
| 52 | * NAT type is unknown because the detection has not been performed. |
| 53 | */ |
| 54 | PJ_STUN_NAT_TYPE_UNKNOWN, |
| 55 | |
| 56 | /** |
| 57 | * NAT type is unknown because there is failure in the detection |
| 58 | * process, possibly because server does not support RFC 3489. |
| 59 | */ |
| 60 | PJ_STUN_NAT_TYPE_ERR_UNKNOWN, |
| 61 | |
| 62 | /** |
| 63 | * This specifies that the client has open access to Internet (or |
| 64 | * at least, its behind a firewall that behaves like a full-cone NAT, |
| 65 | * but without the translation) |
| 66 | */ |
| 67 | PJ_STUN_NAT_TYPE_OPEN, |
| 68 | |
| 69 | /** |
| 70 | * This specifies that communication with server has failed, probably |
| 71 | * because UDP packets are blocked. |
| 72 | */ |
| 73 | PJ_STUN_NAT_TYPE_BLOCKED, |
| 74 | |
| 75 | /** |
| 76 | * Firewall that allows UDP out, and responses have to come back to |
| 77 | * the source of the request (like a symmetric NAT, but no |
| 78 | * translation. |
| 79 | */ |
| 80 | PJ_STUN_NAT_TYPE_SYMMETRIC_UDP, |
| 81 | |
| 82 | /** |
| 83 | * A full cone NAT is one where all requests from the same internal |
| 84 | * IP address and port are mapped to the same external IP address and |
| 85 | * port. Furthermore, any external host can send a packet to the |
| 86 | * internal host, by sending a packet to the mapped external address. |
| 87 | */ |
| 88 | PJ_STUN_NAT_TYPE_FULL_CONE, |
| 89 | |
| 90 | /** |
| 91 | * A symmetric NAT is one where all requests from the same internal |
| 92 | * IP address and port, to a specific destination IP address and port, |
| 93 | * are mapped to the same external IP address and port. If the same |
| 94 | * host sends a packet with the same source address and port, but to |
| 95 | * a different destination, a different mapping is used. Furthermore, |
| 96 | * only the external host that receives a packet can send a UDP packet |
| 97 | * back to the internal host. |
| 98 | */ |
| 99 | PJ_STUN_NAT_TYPE_SYMMETRIC, |
| 100 | |
| 101 | /** |
| 102 | * A restricted cone NAT is one where all requests from the same |
| 103 | * internal IP address and port are mapped to the same external IP |
| 104 | * address and port. Unlike a full cone NAT, an external host (with |
| 105 | * IP address X) can send a packet to the internal host only if the |
| 106 | * internal host had previously sent a packet to IP address X. |
| 107 | */ |
| 108 | PJ_STUN_NAT_TYPE_RESTRICTED, |
| 109 | |
| 110 | /** |
| 111 | * A port restricted cone NAT is like a restricted cone NAT, but the |
| 112 | * restriction includes port numbers. Specifically, an external host |
| 113 | * can send a packet, with source IP address X and source port P, |
| 114 | * to the internal host only if the internal host had previously sent |
| 115 | * a packet to IP address X and port P. |
| 116 | */ |
| 117 | PJ_STUN_NAT_TYPE_PORT_RESTRICTED |
| 118 | |
| 119 | } pj_stun_nat_type; |
| 120 | |
| 121 | |
| 122 | /** |
| 123 | * This structure contains the result of NAT classification function. |
| 124 | */ |
| 125 | typedef struct pj_stun_nat_detect_result |
| 126 | { |
| 127 | /** |
| 128 | * Status of the detection process. If this value is not PJ_SUCCESS, |
| 129 | * the detection has failed and \a nat_type field will contain |
| 130 | * PJ_STUN_NAT_TYPE_UNKNOWN. |
| 131 | */ |
| 132 | pj_status_t status; |
| 133 | |
| 134 | /** |
| 135 | * The text describing the status, if the status is not PJ_SUCCESS. |
| 136 | */ |
| 137 | const char *status_text; |
| 138 | |
| 139 | /** |
| 140 | * This contains the NAT type as detected by the detection procedure. |
| 141 | * This value is only valid when the \a status is PJ_SUCCESS. |
| 142 | */ |
| 143 | pj_stun_nat_type nat_type; |
| 144 | |
| 145 | /** |
| 146 | * Text describing that NAT type. |
| 147 | */ |
| 148 | const char *nat_type_name; |
| 149 | |
| 150 | } pj_stun_nat_detect_result; |
| 151 | |
| 152 | |
| 153 | /** |
| 154 | * Type of callback to be called when the NAT detection function has |
| 155 | * completed. |
| 156 | */ |
| 157 | typedef void pj_stun_nat_detect_cb(void *user_data, |
| 158 | const pj_stun_nat_detect_result *res); |
| 159 | |
| 160 | |
| 161 | /** |
| 162 | * Get the NAT name from the specified NAT type. |
| 163 | * |
| 164 | * @param type NAT type. |
| 165 | * |
| 166 | * @return NAT name. |
| 167 | */ |
| 168 | PJ_DECL(const char*) pj_stun_get_nat_name(pj_stun_nat_type type); |
| 169 | |
| 170 | |
| 171 | /** |
| 172 | * Perform NAT classification function according to the procedures |
| 173 | * specified in RFC 3489. Once this function returns successfully, |
| 174 | * the procedure will run in the "background" and will complete |
| 175 | * asynchronously. Application can register a callback to be notified |
| 176 | * when such detection has completed. |
| 177 | * |
| 178 | * @param server STUN server address. |
| 179 | * @param stun_cfg A structure containing various STUN configurations, |
| 180 | * such as the ioqueue and timer heap instance used |
| 181 | * to receive network I/O and timer events. |
| 182 | * @param user_data Application data, which will be returned back |
| 183 | * in the callback. |
| 184 | * @param cb Callback to be registered to receive notification |
| 185 | * about detection result. |
| 186 | * |
| 187 | * @return If this function returns PJ_SUCCESS, the procedure |
| 188 | * will complete asynchronously and callback will be |
| 189 | * called when it completes. For other return |
| 190 | * values, it means that an error has occured and |
| 191 | * the procedure did not start. |
| 192 | */ |
| 193 | PJ_DECL(pj_status_t) pj_stun_detect_nat_type(const pj_sockaddr_in *server, |
| 194 | pj_stun_config *stun_cfg, |
| 195 | void *user_data, |
| 196 | pj_stun_nat_detect_cb *cb); |
| 197 | |
| 198 | |
| 199 | /** |
| 200 | * @} |
| 201 | */ |
| 202 | |
| 203 | |
| 204 | PJ_END_DECL |
| 205 | |
| 206 | |
| 207 | #endif /* __PJNATH_NAT_DETECT_H__ */ |
| 208 | |