1 /* 2 Copyright (c) 2007, Adobe Systems, Incorporated 3 All rights reserved. 4 5 Redistribution and use in source and binary forms, with or without 6 modification, are permitted provided that the following conditions are 7 met: 8 9 * Redistributions of source code must retain the above copyright 10 notice, this list of conditions and the following disclaimer. 11 12 * Redistributions in binary form must reproduce the above copyright 13 notice, this list of conditions and the following disclaimer in the 14 documentation and/or other materials provided with the distribution. 15 16 * Neither the name of Adobe Systems, Network Resonance nor the names of its 17 contributors may be used to endorse or promote products derived from 18 this software without specific prior written permission. 19 20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 34 #ifndef _STUN_H 35 #define _STUN_H 36 37 #ifdef WIN32 38 #include <winsock2.h> 39 #else 40 #include <sys/param.h> 41 #include <sys/socket.h> 42 #ifndef LINUX 43 #include <net/if.h> 44 #ifdef DARWIN 45 #include <net/if_var.h> 46 #endif 47 #include <net/if_dl.h> 48 #include <net/if_types.h> 49 #else 50 #include <linux/if.h> 51 #endif 52 #ifndef BSD 53 #include <net/route.h> 54 #endif 55 #include <netinet/in.h> 56 #ifndef LINUX 57 #include <netinet/in_var.h> 58 #endif 59 #include <arpa/inet.h> 60 #include <netdb.h> 61 #endif 62 #include <time.h> 63 64 #include "nr_api.h" 65 #include "stun_msg.h" 66 #include "stun_build.h" 67 #include "stun_codec.h" 68 #include "stun_hint.h" 69 #include "stun_util.h" 70 #include "nr_socket.h" 71 #include "stun_client_ctx.h" 72 #include "stun_server_ctx.h" 73 #include "stun_proc.h" 74 75 #define NR_STUN_VERSION "rfc3489bis-11" 76 #define NR_STUN_PORT 3478 77 78 /* STUN attributes */ 79 #define NR_STUN_ATTR_MAPPED_ADDRESS 0x0001 80 #define NR_STUN_ATTR_USERNAME 0x0006 81 #define NR_STUN_ATTR_MESSAGE_INTEGRITY 0x0008 82 #define NR_STUN_ATTR_ERROR_CODE 0x0009 83 #define NR_STUN_ATTR_UNKNOWN_ATTRIBUTES 0x000A 84 #define NR_STUN_ATTR_REALM 0x0014 85 #define NR_STUN_ATTR_NONCE 0x0015 86 #define NR_STUN_ATTR_XOR_MAPPED_ADDRESS 0x0020 87 #define NR_STUN_ATTR_SERVER 0x8022 88 #define NR_STUN_ATTR_ALTERNATE_SERVER 0x8023 89 #define NR_STUN_ATTR_FINGERPRINT 0x8028 90 91 /* for backwards compatibility with obsolete versions of the STUN spec */ 92 #define NR_STUN_ATTR_OLD_XOR_MAPPED_ADDRESS 0x8020 93 94 #ifdef USE_STUND_0_96 95 #define NR_STUN_ATTR_OLD_CHANGE_REQUEST 0x0003 96 #endif /* USE_STUND_0_96 */ 97 98 #ifdef USE_RFC_3489_BACKWARDS_COMPATIBLE 99 /* for backwards compatibility with obsolete versions of the STUN spec */ 100 #define NR_STUN_ATTR_OLD_PASSWORD 0x0007 101 #define NR_STUN_ATTR_OLD_RESPONSE_ADDRESS 0x0002 102 #define NR_STUN_ATTR_OLD_SOURCE_ADDRESS 0x0004 103 #define NR_STUN_ATTR_OLD_CHANGED_ADDRESS 0x0005 104 #endif /* USE_RFC_3489_BACKWARDS_COMPATIBLE */ 105 106 #ifdef USE_ICE 107 /* ICE attributes */ 108 #define NR_STUN_ATTR_PRIORITY 0x0024 109 #define NR_STUN_ATTR_USE_CANDIDATE 0x0025 110 #define NR_STUN_ATTR_ICE_CONTROLLED 0x8029 111 #define NR_STUN_ATTR_ICE_CONTROLLING 0x802A 112 #endif /* USE_ICE */ 113 114 #ifdef USE_TURN 115 /* TURN attributes */ 116 #define NR_STUN_ATTR_LIFETIME 0x000d 117 /* from an expired draft defined as optional, but in the required range */ 118 #define NR_STUN_ATTR_BANDWIDTH 0x0010 119 #define NR_STUN_ATTR_XOR_PEER_ADDRESS 0x0012 120 #define NR_STUN_ATTR_DATA 0x0013 121 #define NR_STUN_ATTR_XOR_RELAY_ADDRESS 0x0016 122 #define NR_STUN_ATTR_REQUESTED_TRANSPORT 0x0019 123 124 #define NR_STUN_ATTR_REQUESTED_TRANSPORT_UDP 17 125 #endif /* USE_TURN */ 126 127 /* 128 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 129 * |M|M|M|M|M|C|M|M|M|C|M|M|M|M| 130 * |1|1|9|8|7|1|6|5|4|0|3|2|1|0| 131 * |1|0| | | | | | | | | | | | | 132 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 133 * 134 * Figure 3: Format of STUN Message Type Field 135 */ 136 #define NR_STUN_METHOD_TYPE_BITS(m) \ 137 ((((m) & 0xf80) << 2) | (((m) & 0x070) << 1) | ((m) & 0x00f)) 138 139 #define NR_STUN_CLASS_TYPE_BITS(c) \ 140 ((((c) & 0x002) << 7) | (((c) & 0x001) << 4)) 141 142 #define NR_STUN_GET_TYPE_METHOD(t) \ 143 ((((t) >> 2) & 0xf80) | (((t) >> 1) & 0x070) | ((t) & 0x00f)) 144 145 #define NR_STUN_GET_TYPE_CLASS(t) \ 146 ((((t) >> 7) & 0x002) | (((t) >> 4) & 0x001)) 147 148 #define NR_STUN_TYPE(m,c) (NR_STUN_METHOD_TYPE_BITS((m)) | NR_STUN_CLASS_TYPE_BITS((c))) 149 150 /* building blocks for message types */ 151 #define NR_METHOD_BINDING 0x001 152 #define NR_CLASS_REQUEST 0x0 153 #define NR_CLASS_INDICATION 0x1 154 #define NR_CLASS_RESPONSE 0x2 155 #define NR_CLASS_ERROR_RESPONSE 0x3 156 157 /* define types for STUN messages */ 158 #define NR_STUN_MSG_BINDING_REQUEST NR_STUN_TYPE(NR_METHOD_BINDING, \ 159 NR_CLASS_REQUEST) 160 #define NR_STUN_MSG_BINDING_INDICATION NR_STUN_TYPE(NR_METHOD_BINDING, \ 161 NR_CLASS_INDICATION) 162 #define NR_STUN_MSG_BINDING_RESPONSE NR_STUN_TYPE(NR_METHOD_BINDING, \ 163 NR_CLASS_RESPONSE) 164 #define NR_STUN_MSG_BINDING_ERROR_RESPONSE NR_STUN_TYPE(NR_METHOD_BINDING, \ 165 NR_CLASS_ERROR_RESPONSE) 166 167 #ifdef USE_TURN 168 /* building blocks for TURN message types */ 169 #define NR_METHOD_ALLOCATE 0x003 170 #define NR_METHOD_REFRESH 0x004 171 172 #define NR_METHOD_SEND 0x006 173 #define NR_METHOD_DATA 0x007 174 #define NR_METHOD_CREATE_PERMISSION 0x008 175 #define NR_METHOD_CHANNEL_BIND 0x009 176 177 /* define types for a TURN message */ 178 #define NR_STUN_MSG_ALLOCATE_REQUEST NR_STUN_TYPE(NR_METHOD_ALLOCATE, \ 179 NR_CLASS_REQUEST) 180 #define NR_STUN_MSG_ALLOCATE_RESPONSE NR_STUN_TYPE(NR_METHOD_ALLOCATE, \ 181 NR_CLASS_RESPONSE) 182 #define NR_STUN_MSG_ALLOCATE_ERROR_RESPONSE NR_STUN_TYPE(NR_METHOD_ALLOCATE, \ 183 NR_CLASS_ERROR_RESPONSE) 184 #define NR_STUN_MSG_REFRESH_REQUEST NR_STUN_TYPE(NR_METHOD_REFRESH, \ 185 NR_CLASS_REQUEST) 186 #define NR_STUN_MSG_REFRESH_RESPONSE NR_STUN_TYPE(NR_METHOD_REFRESH, \ 187 NR_CLASS_RESPONSE) 188 #define NR_STUN_MSG_REFRESH_ERROR_RESPONSE NR_STUN_TYPE(NR_METHOD_REFRESH, \ 189 NR_CLASS_ERROR_RESPONSE) 190 191 #define NR_STUN_MSG_SEND_INDICATION NR_STUN_TYPE(NR_METHOD_SEND, \ 192 NR_CLASS_INDICATION) 193 #define NR_STUN_MSG_DATA_INDICATION NR_STUN_TYPE(NR_METHOD_DATA, \ 194 NR_CLASS_INDICATION) 195 196 #define NR_STUN_MSG_PERMISSION_REQUEST NR_STUN_TYPE(NR_METHOD_CREATE_PERMISSION, \ 197 NR_CLASS_REQUEST) 198 #define NR_STUN_MSG_PERMISSION_RESPONSE NR_STUN_TYPE(NR_METHOD_CREATE_PERMISSION, \ 199 NR_CLASS_RESPONSE) 200 #define NR_STUN_MSG_PERMISSION_ERROR_RESPONSE NR_STUN_TYPE(NR_METHOD_CREATE_PERMISSION, \ 201 NR_CLASS_ERROR_RESPONSE) 202 203 #define NR_STUN_MSG_CHANNEL_BIND_REQUEST NR_STUN_TYPE(NR_METHOD_CHANNEL_BIND, \ 204 NR_CLASS_REQUEST) 205 #define NR_STUN_MSG_CHANNEL_BIND_RESPONSE NR_STUN_TYPE(NR_METHOD_CHANNEL_BIND, \ 206 NR_CLASS_RESPONSE) 207 #define NR_STUN_MSG_CHANNEL_BIND_ERROR_RESPONSE NR_STUN_TYPE(NR_METHOD_CHANNEL_BIND, \ 208 NR_CLASS_ERROR_RESPONSE) 209 210 211 #endif /* USE_TURN */ 212 213 214 #define NR_STUN_AUTH_RULE_OPTIONAL (1<<0) 215 #define NR_STUN_AUTH_RULE_SHORT_TERM (1<<8) 216 #define NR_STUN_AUTH_RULE_LONG_TERM (1<<9) 217 218 #endif 219