TURN Server  1.5
 All Classes Namespaces Files Functions Variables Typedefs Friends Defines
src/client/ns_turn_msg.h
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2012, 2013 Citrix Systems
00003  *
00004  * All rights reserved.
00005  *
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions
00008  * are met:
00009  * 1. Redistributions of source code must retain the above copyright
00010  *    notice, this list of conditions and the following disclaimer.
00011  * 2. Redistributions in binary form must reproduce the above copyright
00012  *    notice, this list of conditions and the following disclaimer in the
00013  *    documentation and/or other materials provided with the distribution.
00014  * 3. Neither the name of the project nor the names of its contributors
00015  *    may be used to endorse or promote products derived from this software
00016  *    without specific prior written permission.
00017  *
00018  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
00019  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00020  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00021  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
00022  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00023  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00024  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00025  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00026  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00027  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00028  * SUCH DAMAGE.
00029  */
00030 
00031 #ifndef __LIB_TURN_MSG__
00032 #define __LIB_TURN_MSG__
00033 
00034 #include "ns_turn_ioaddr.h"
00035 #include "ns_turn_msg_defs.h"
00036 
00037 #ifdef __cplusplus
00038 extern "C" {
00039 #endif
00040 
00042 
00046 typedef struct {
00050   uint8_t tsx_id[12];
00051 } stun_tid;
00052 
00054 
00055 typedef const void* stun_attr_ref;
00056 
00058 
00059 int stun_tid_equals(const stun_tid *id1, const stun_tid *id2);
00060 void stun_tid_cpy(stun_tid *id1, const stun_tid *id2);
00061 void stun_tid_generate(stun_tid* id);
00062 
00064 
00065 u16bits stun_make_type(u16bits method);
00066 u16bits stun_make_request(u16bits method);
00067 u16bits stun_make_indication(u16bits method);
00068 u16bits stun_make_success_response(u16bits method);
00069 u16bits stun_make_error_response(u16bits method);
00070 
00072 
00073 u32bits stun_adjust_allocate_lifetime(u32bits lifetime);
00074 
00076 
00077 int stun_get_message_len_str(u08bits *buf, size_t len);
00078 
00079 void stun_init_buffer_str(u08bits *buf, size_t *len);
00080 void stun_init_command_str(u16bits message_type, u08bits* buf, size_t *len);
00081 void stun_init_request_str(u16bits method, u08bits* buf, size_t *len);
00082 void stun_init_indication_str(u16bits method, u08bits* buf, size_t *len);
00083 void stun_init_success_response_str(u16bits method, u08bits* buf, size_t *len, stun_tid* id);
00084 void stun_init_error_response_str(u16bits method, u08bits* buf, size_t *len, u16bits error_code, const u08bits *reason, stun_tid* id);
00085 int stun_init_channel_message_str(u16bits chnumber, u08bits* buf, size_t *len, int length);
00086 
00087 u08bits* stun_get_app_data_ptr_str(u08bits* buf, int *olength);
00088 
00089 int stun_is_command_message_str(const u08bits* buf, size_t blen);
00090 int stun_is_command_message_full_check_str(const u08bits* buf, size_t blen, int must_check_fingerprint);
00091 int stun_is_command_message_offset_str(const u08bits* buf, size_t blen, int offset);
00092 int stun_is_request_str(const u08bits* buf, size_t len);
00093 int stun_is_success_response_str(const u08bits* buf, size_t len);
00094 int stun_is_error_response_str(const u08bits* buf, size_t len, int *err_code, u08bits *err_msg, size_t err_msg_size);
00095 int stun_is_challenge_response_str(const u08bits* buf, size_t len, int *err_code, u08bits *err_msg, size_t err_msg_size, u08bits *realm, u08bits *nonce);
00096 int stun_is_response_str(const u08bits* buf, size_t len);
00097 int stun_is_indication_str(const u08bits* buf, size_t len);
00098 u16bits stun_get_method_str(const u08bits *buf, size_t len);
00099 u16bits stun_get_msg_type_str(const u08bits *buf, size_t len);
00100 int stun_is_channel_message_str(const u08bits *buf, size_t len, u16bits* chnumber);
00101 int is_channel_msg_str(const u08bits* buf, size_t blen);
00102 
00103 void stun_set_binding_request_str(u08bits* buf, size_t *len);
00104 int stun_set_binding_response_str(u08bits* buf, size_t *len, stun_tid* tid, 
00105                                   const ioa_addr *reflexive_addr, int error_code,
00106                                   const u08bits *reason);
00107 int stun_is_binding_request_str(const u08bits* buf, size_t len, size_t offset);
00108 int stun_is_binding_response_str(const u08bits* buf, size_t len);
00109 
00110 void stun_tid_from_message_str(const u08bits* buf, size_t len, stun_tid* id);
00111 void stun_tid_message_cpy(u08bits *buf, const stun_tid* id);
00112 void stun_tid_generate_in_message_str(u08bits* buf, stun_tid* id);
00113 
00114 int stun_get_command_message_len_str(const u08bits* buf, size_t len);
00115 
00116 int stun_attr_is_addr(stun_attr_ref attr);
00117 int stun_attr_get_type(stun_attr_ref attr);
00118 int stun_attr_get_len(stun_attr_ref attr);
00119 const u08bits* stun_attr_get_value(stun_attr_ref attr);
00120 u16bits stun_attr_get_channel_number(stun_attr_ref attr);
00121 u08bits stun_attr_get_even_port(stun_attr_ref attr);
00122 u64bits stun_attr_get_reservation_token_value(stun_attr_ref attr);
00123 stun_attr_ref stun_attr_get_first_by_type_str(const u08bits* buf, size_t len, u16bits attr_type);
00124 stun_attr_ref stun_attr_get_first_str(const u08bits* buf, size_t len);
00125 stun_attr_ref stun_attr_get_next_str(const u08bits* buf, size_t len, stun_attr_ref prev);
00126 int stun_attr_add_str(u08bits* buf, size_t *len, u16bits attr, const u08bits* avalue, int alen);
00127 int stun_attr_add_addr_str(u08bits *buf, size_t *len, u16bits attr_type, const ioa_addr* ca);
00128 int stun_attr_get_addr_str(const u08bits *buf, size_t len, stun_attr_ref attr, ioa_addr* ca, const ioa_addr *default_addr);
00129 int stun_attr_get_first_addr_str(const u08bits *buf, size_t len, u16bits attr_type, ioa_addr* ca, const ioa_addr *default_addr);
00130 int stun_attr_add_channel_number_str(u08bits* buf, size_t *len, u16bits chnumber);
00131 u16bits stun_attr_get_first_channel_number_str(const u08bits *buf, size_t len);
00132 
00133 int stun_get_channel_message_len_str(const u08bits* buf);
00134 int stun_is_specific_channel_message_str(const u08bits* buf, size_t len, u16bits chnumber);
00135 
00136 int stun_set_allocate_request_str(u08bits* buf, size_t *len, u32bits lifetime, int address_family);
00137 int stun_set_allocate_response_str(u08bits* buf, size_t *len, stun_tid* tid, 
00138                                    const ioa_addr *relayed_addr,
00139                                    const ioa_addr *reflexive_addr,
00140                                    u32bits lifetime, int error_code, const u08bits *reason,
00141                                    u64bits reservation_token);
00142 
00143 u16bits stun_set_channel_bind_request_str(u08bits* buf, size_t *len,
00144                                           const ioa_addr* peer_addr, u16bits channel_number);
00145 void stun_set_channel_bind_response_str(u08bits* buf, size_t *len, stun_tid* tid, int error_code, const u08bits *reason);
00146 
00147 int stun_get_requested_address_family(stun_attr_ref attr);
00148 
00149 int stun_attr_add_fingerprint_str(u08bits *buf, size_t *len);
00150 
00151 int SASLprep(u08bits *s);
00152 
00153 #define print_bin(str, len, field) print_bin_func(str,len,field,__FUNCTION__)
00154 void print_bin_func(const char *name, size_t len, const void *s, const char *func);
00155 
00156 /*
00157  * Return -1 if failure, 0 if the integrity is not correct, 1 if OK
00158  */
00159 int stun_check_message_integrity_by_key_str(u08bits *buf, size_t len, u08bits *key);
00160 int stun_check_message_integrity_str(u08bits *buf, size_t len, u08bits *uname, u08bits *realm, u08bits *upwd);
00161 int stun_attr_add_integrity_str(u08bits *buf, size_t *len, u08bits *key);
00162 int stun_attr_add_integrity_by_user_str(u08bits *buf, size_t *len, u08bits *uname, u08bits *realm, u08bits *upwd, u08bits *nonce);
00163 
00164 /*
00165  * To be implemented by application
00166  */
00167 
00168 int stun_produce_integrity_key_str(u08bits *uname, u08bits *realm, u08bits *upwd, u08bits *key);
00169 int stun_calculate_hmac(u08bits *buf, size_t len, u08bits *key, u08bits *hmac);
00170 
00171 /* RFC 5780 */
00172 int stun_attr_get_change_request_str(stun_attr_ref attr, int *change_ip, int *change_port);
00173 int stun_attr_add_change_request_str(u08bits *buf, size_t *len, int change_ip, int change_port);
00174 int stun_attr_get_response_port_str(stun_attr_ref attr);
00175 int stun_attr_add_response_port_str(u08bits *buf, size_t *len, u16bits port);
00176 int stun_attr_get_padding_len_str(stun_attr_ref attr);
00177 int stun_attr_add_padding_str(u08bits *buf, size_t *len, u16bits padding_len);
00178 
00180 
00181 #ifdef __cplusplus
00182 }
00183 #endif
00184 
00185 #endif //__LIB_TURN_MSG__