michael@0: /* michael@0: Copyright (c) 2007, Adobe Systems, Incorporated michael@0: All rights reserved. michael@0: michael@0: Redistribution and use in source and binary forms, with or without michael@0: modification, are permitted provided that the following conditions are michael@0: met: michael@0: michael@0: * Redistributions of source code must retain the above copyright michael@0: notice, this list of conditions and the following disclaimer. michael@0: michael@0: * Redistributions in binary form must reproduce the above copyright michael@0: notice, this list of conditions and the following disclaimer in the michael@0: documentation and/or other materials provided with the distribution. michael@0: michael@0: * Neither the name of Adobe Systems, Network Resonance nor the names of its michael@0: contributors may be used to endorse or promote products derived from michael@0: this software without specific prior written permission. michael@0: michael@0: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR michael@0: A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT michael@0: OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, michael@0: SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT michael@0: LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, michael@0: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY michael@0: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE michael@0: OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: */ michael@0: michael@0: michael@0: #ifndef _STUN_H michael@0: #define _STUN_H michael@0: michael@0: #ifdef WIN32 michael@0: #include michael@0: #else michael@0: #include michael@0: #include michael@0: #include michael@0: #ifndef LINUX michael@0: #if !defined(__OpenBSD__) && !defined(__NetBSD__) michael@0: #include michael@0: #endif michael@0: #include michael@0: #include michael@0: #endif michael@0: #ifndef BSD michael@0: #include michael@0: #endif michael@0: #include michael@0: #ifndef LINUX michael@0: #include michael@0: #endif michael@0: #include michael@0: #include michael@0: #endif michael@0: #include michael@0: michael@0: #include "nr_api.h" michael@0: #include "stun_msg.h" michael@0: #include "stun_build.h" michael@0: #include "stun_codec.h" michael@0: #include "stun_hint.h" michael@0: #include "stun_util.h" michael@0: #include "nr_socket.h" michael@0: #include "stun_client_ctx.h" michael@0: #include "stun_server_ctx.h" michael@0: #include "stun_proc.h" michael@0: michael@0: #define NR_STUN_VERSION "rfc3489bis-11" michael@0: #define NR_STUN_PORT 3478 michael@0: michael@0: /* STUN attributes */ michael@0: #define NR_STUN_ATTR_MAPPED_ADDRESS 0x0001 michael@0: #define NR_STUN_ATTR_USERNAME 0x0006 michael@0: #define NR_STUN_ATTR_MESSAGE_INTEGRITY 0x0008 michael@0: #define NR_STUN_ATTR_ERROR_CODE 0x0009 michael@0: #define NR_STUN_ATTR_UNKNOWN_ATTRIBUTES 0x000A michael@0: #define NR_STUN_ATTR_REALM 0x0014 michael@0: #define NR_STUN_ATTR_NONCE 0x0015 michael@0: #define NR_STUN_ATTR_XOR_MAPPED_ADDRESS 0x0020 michael@0: #define NR_STUN_ATTR_SERVER 0x8022 michael@0: #define NR_STUN_ATTR_ALTERNATE_SERVER 0x8023 michael@0: #define NR_STUN_ATTR_FINGERPRINT 0x8028 michael@0: michael@0: /* for backwards compatibility with obsolete versions of the STUN spec */ michael@0: #define NR_STUN_ATTR_OLD_XOR_MAPPED_ADDRESS 0x8020 michael@0: michael@0: #ifdef USE_STUND_0_96 michael@0: #define NR_STUN_ATTR_OLD_CHANGE_REQUEST 0x0003 michael@0: #endif /* USE_STUND_0_96 */ michael@0: michael@0: #ifdef USE_RFC_3489_BACKWARDS_COMPATIBLE michael@0: /* for backwards compatibility with obsolete versions of the STUN spec */ michael@0: #define NR_STUN_ATTR_OLD_PASSWORD 0x0007 michael@0: #define NR_STUN_ATTR_OLD_RESPONSE_ADDRESS 0x0002 michael@0: #define NR_STUN_ATTR_OLD_SOURCE_ADDRESS 0x0004 michael@0: #define NR_STUN_ATTR_OLD_CHANGED_ADDRESS 0x0005 michael@0: #endif /* USE_RFC_3489_BACKWARDS_COMPATIBLE */ michael@0: michael@0: #ifdef USE_ICE michael@0: /* ICE attributes */ michael@0: #define NR_STUN_ATTR_PRIORITY 0x0024 michael@0: #define NR_STUN_ATTR_USE_CANDIDATE 0x0025 michael@0: #define NR_STUN_ATTR_ICE_CONTROLLED 0x8029 michael@0: #define NR_STUN_ATTR_ICE_CONTROLLING 0x802A michael@0: #endif /* USE_ICE */ michael@0: michael@0: #ifdef USE_TURN michael@0: /* TURN attributes */ michael@0: #define NR_STUN_ATTR_LIFETIME 0x000d michael@0: #define NR_STUN_ATTR_XOR_PEER_ADDRESS 0x0012 michael@0: #define NR_STUN_ATTR_DATA 0x0013 michael@0: #define NR_STUN_ATTR_XOR_RELAY_ADDRESS 0x0016 michael@0: #define NR_STUN_ATTR_REQUESTED_TRANSPORT 0x0019 michael@0: michael@0: #define NR_STUN_ATTR_REQUESTED_TRANSPORT_UDP 17 michael@0: #endif /* USE_TURN */ michael@0: michael@0: /* michael@0: * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+ michael@0: * |M|M|M|M|M|C|M|M|M|C|M|M|M|M| michael@0: * |1|1|9|8|7|1|6|5|4|0|3|2|1|0| michael@0: * |1|0| | | | | | | | | | | | | michael@0: * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+ michael@0: * michael@0: * Figure 3: Format of STUN Message Type Field michael@0: */ michael@0: #define NR_STUN_METHOD_TYPE_BITS(m) \ michael@0: ((((m) & 0xf80) << 2) | (((m) & 0x070) << 1) | ((m) & 0x00f)) michael@0: michael@0: #define NR_STUN_CLASS_TYPE_BITS(c) \ michael@0: ((((c) & 0x002) << 7) | (((c) & 0x001) << 4)) michael@0: michael@0: #define NR_STUN_GET_TYPE_METHOD(t) \ michael@0: ((((t) >> 2) & 0xf80) | (((t) >> 1) & 0x070) | ((t) & 0x00f)) michael@0: michael@0: #define NR_STUN_GET_TYPE_CLASS(t) \ michael@0: ((((t) >> 7) & 0x002) | (((t) >> 4) & 0x001)) michael@0: michael@0: #define NR_STUN_TYPE(m,c) (NR_STUN_METHOD_TYPE_BITS((m)) | NR_STUN_CLASS_TYPE_BITS((c))) michael@0: michael@0: /* building blocks for message types */ michael@0: #define NR_METHOD_BINDING 0x001 michael@0: #define NR_CLASS_REQUEST 0x0 michael@0: #define NR_CLASS_INDICATION 0x1 michael@0: #define NR_CLASS_RESPONSE 0x2 michael@0: #define NR_CLASS_ERROR_RESPONSE 0x3 michael@0: michael@0: /* define types for STUN messages */ michael@0: #define NR_STUN_MSG_BINDING_REQUEST NR_STUN_TYPE(NR_METHOD_BINDING, \ michael@0: NR_CLASS_REQUEST) michael@0: #define NR_STUN_MSG_BINDING_INDICATION NR_STUN_TYPE(NR_METHOD_BINDING, \ michael@0: NR_CLASS_INDICATION) michael@0: #define NR_STUN_MSG_BINDING_RESPONSE NR_STUN_TYPE(NR_METHOD_BINDING, \ michael@0: NR_CLASS_RESPONSE) michael@0: #define NR_STUN_MSG_BINDING_ERROR_RESPONSE NR_STUN_TYPE(NR_METHOD_BINDING, \ michael@0: NR_CLASS_ERROR_RESPONSE) michael@0: michael@0: #ifdef USE_TURN michael@0: /* building blocks for TURN message types */ michael@0: #define NR_METHOD_ALLOCATE 0x003 michael@0: #define NR_METHOD_REFRESH 0x004 michael@0: michael@0: #define NR_METHOD_SEND 0x006 michael@0: #define NR_METHOD_DATA 0x007 michael@0: #define NR_METHOD_CREATE_PERMISSION 0x008 michael@0: #define NR_METHOD_CHANNEL_BIND 0x009 michael@0: michael@0: /* define types for a TURN message */ michael@0: #define NR_STUN_MSG_ALLOCATE_REQUEST NR_STUN_TYPE(NR_METHOD_ALLOCATE, \ michael@0: NR_CLASS_REQUEST) michael@0: #define NR_STUN_MSG_ALLOCATE_RESPONSE NR_STUN_TYPE(NR_METHOD_ALLOCATE, \ michael@0: NR_CLASS_RESPONSE) michael@0: #define NR_STUN_MSG_ALLOCATE_ERROR_RESPONSE NR_STUN_TYPE(NR_METHOD_ALLOCATE, \ michael@0: NR_CLASS_ERROR_RESPONSE) michael@0: #define NR_STUN_MSG_REFRESH_REQUEST NR_STUN_TYPE(NR_METHOD_REFRESH, \ michael@0: NR_CLASS_REQUEST) michael@0: #define NR_STUN_MSG_REFRESH_RESPONSE NR_STUN_TYPE(NR_METHOD_REFRESH, \ michael@0: NR_CLASS_RESPONSE) michael@0: #define NR_STUN_MSG_REFRESH_ERROR_RESPONSE NR_STUN_TYPE(NR_METHOD_REFRESH, \ michael@0: NR_CLASS_ERROR_RESPONSE) michael@0: michael@0: #define NR_STUN_MSG_SEND_INDICATION NR_STUN_TYPE(NR_METHOD_SEND, \ michael@0: NR_CLASS_INDICATION) michael@0: #define NR_STUN_MSG_DATA_INDICATION NR_STUN_TYPE(NR_METHOD_DATA, \ michael@0: NR_CLASS_INDICATION) michael@0: michael@0: #define NR_STUN_MSG_PERMISSION_REQUEST NR_STUN_TYPE(NR_METHOD_CREATE_PERMISSION, \ michael@0: NR_CLASS_REQUEST) michael@0: #define NR_STUN_MSG_PERMISSION_RESPONSE NR_STUN_TYPE(NR_METHOD_CREATE_PERMISSION, \ michael@0: NR_CLASS_RESPONSE) michael@0: #define NR_STUN_MSG_PERMISSION_ERROR_RESPONSE NR_STUN_TYPE(NR_METHOD_CREATE_PERMISSION, \ michael@0: NR_CLASS_ERROR_RESPONSE) michael@0: michael@0: #define NR_STUN_MSG_CHANNEL_BIND_REQUEST NR_STUN_TYPE(NR_METHOD_CHANNEL_BIND, \ michael@0: NR_CLASS_REQUEST) michael@0: #define NR_STUN_MSG_CHANNEL_BIND_RESPONSE NR_STUN_TYPE(NR_METHOD_CHANNEL_BIND, \ michael@0: NR_CLASS_RESPONSE) michael@0: #define NR_STUN_MSG_CHANNEL_BIND_ERROR_RESPONSE NR_STUN_TYPE(NR_METHOD_CHANNEL_BIND, \ michael@0: NR_CLASS_ERROR_RESPONSE) michael@0: michael@0: michael@0: #endif /* USE_TURN */ michael@0: michael@0: michael@0: #define NR_STUN_AUTH_RULE_OPTIONAL (1<<0) michael@0: #define NR_STUN_AUTH_RULE_SHORT_TERM (1<<8) michael@0: #define NR_STUN_AUTH_RULE_LONG_TERM (1<<9) michael@0: michael@0: #endif