sofia-sip/stun_common.h

Go to the documentation of this file.
00001 /*
00002  * This file is part of the Sofia-SIP package
00003  *
00004  * Copyright (C) 2005 Nokia Corporation.
00005  *
00006  * Contact: Pekka Pessi <pekka.pessi@nokia-email.address.hidden>
00007  *
00008  * This library is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU Lesser General Public License
00010  * as published by the Free Software Foundation; either version 2.1 of
00011  * the License, or (at your option) any later version.
00012  *
00013  * This library is distributed in the hope that it will be useful, but
00014  * WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00016  * Lesser General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU Lesser General Public
00019  * License along with this library; if not, write to the Free Software
00020  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
00021  * 02110-1301 USA
00022  *
00023  */
00024 
00025 #ifndef STUN_COMMON_H
00026 
00027 #define STUN_COMMON_H
00028 
00039 #include <sofia-sip/su_localinfo.h>
00040 
00041 SOFIA_BEGIN_DECLS
00042 
00043 /* Define Message Types */
00044 #define BINDING_REQUEST               0x0001
00045 #define BINDING_RESPONSE              0x0101
00046 #define BINDING_ERROR_RESPONSE        0x0111
00047 #define SHARED_SECRET_REQUEST         0x0002
00048 #define SHARED_SECRET_RESPONSE        0x0102
00049 #define SHARED_SECRET_ERROR_RESPONSE  0x0112
00050 
00051 /* Define Attribute Types */
00052 #define MAPPED_ADDRESS                0x0001
00053 #define RESPONSE_ADDRESS              0x0002
00054 #define CHANGE_REQUEST                0x0003
00055 #define SOURCE_ADDRESS                0x0004
00056 #define CHANGED_ADDRESS               0x0005
00057 #define USERNAME                      0x0006
00058 #define PASSWORD                      0x0007
00059 #define MESSAGE_INTEGRITY             0x0008
00060 #define ERROR_CODE                    0x0009
00061 #define UNKNOWN_ATTRIBUTES            0x000a
00062 #define REFLECTED_FROM                0x000b
00063 #define STUN_A_REALM                  0x0014 /* XXX: check value in 3489bis-05+ */
00064 #define STUN_A_NONCE                  0x0015 /* XXX: check value in 3489bis-05+ */
00065 #define STUN_A_XOR_MAPPED_ADDRESS     0x0020
00066 #define STUN_A_FINGERPRINT            0x0023
00067 #define STUN_A_SERVER                 0x8022
00068 #define STUN_A_ALTERNATE_SERVER       0x8023
00069 #define STUN_A_REFRESH_INTERVAL       0x8024
00070 
00071 /* Defines for mandatory and optional attributes */
00072 #define STUN_A_LAST_MANDATORY         0x0023 
00075 #define STUN_A_OPTIONAL               0x7fff
00076 
00077 /* Compability attribute types */
00078 #define STUN_A_ALTERNATE_SERVER_DEP   0x000e 
00079 #define STUN_A_BUGGYSERVER_XORONLY    0x0021 
00080 #define STUN_A_BUGGYSERVER_SERVER     0x0022 
00081 #define LARGEST_ATTRIBUTE             STUN_A_LAST_MANDATORY 
00082 #define OPTIONAL_ATTRIBUTE            STUN_A_OPTIONAL 
00084 /* Stun response codes */
00085 #define STUN_400_BAD_REQUEST             400
00086 #define STUN_401_UNAUTHORIZED            401
00087 #define STUN_420_UNKNOWN_ATTRIBUTE       420
00088 #define STUN_430_STALE_CREDENTIALS       430
00089 #define STUN_431_INTEGRITY_CHECK_FAILURE 431
00090 #define STUN_432_MISSING_USERNAME        432
00091 #define STUN_433_USE_TLS                 433
00092 #define STUN_500_SERVER_ERROR            500
00093 #define STUN_600_GLOBAL_FAILURE          600
00094 
00095 /* flags for CHANGE_REQUEST */
00096 #define STUN_CR_CHANGE_IP               0x0004
00097 #define STUN_CR_CHANGE_PORT             0x0002
00098 
00099 /* mask for ERROR_CODE */
00100 #define STUN_EC_CLASS                   0x0070
00101 #define STUN_EC_NUM                     0x000F
00102 
00103 #define RAND_MAX_16                     65535
00104 
00105 #define STUN_TID_BYTES                  16
00106 
00107 /* other protocol specific parameters */
00108 #define STUN_MAX_RETRX                  5 /* should be 8? */
00109 #define STUN_MAX_RETRX_INT              1600  
00111 #define STUN_DEFAULT_PORT               3478  
00113 /*
00114  * STUN header format
00115  */
00116   /*
00117     0                   1                   2                   3
00118     0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
00119    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
00120    |         message type          |       message length          |
00121    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
00122    |                                                               |
00123    |                       Transaction ID                          |
00124    |                                                               |
00125    |                                                               |
00126    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
00127   */
00128 struct stun_buffer_s {
00129   unsigned char *data;      
00130   unsigned size;            
00131 };
00132 
00133 typedef struct stun_buffer_s stun_buffer_t;
00134 
00135 typedef struct {
00136   uint16_t msg_type;        
00137   uint16_t msg_len;         
00138   uint8_t tran_id[16];      
00139 } stun_hdr_t;
00140 
00141 typedef struct stun_attr_s {
00142   uint16_t attr_type;       
00143   void *pattr;              
00144   stun_buffer_t enc_buf;    
00145   struct stun_attr_s *next; 
00146 } stun_attr_t;
00147 
00148 typedef struct {
00149   stun_hdr_t stun_hdr;
00150   stun_attr_t *stun_attr;
00151   stun_buffer_t enc_buf;    
00152 } stun_msg_t;
00153 
00154 /* stun attribute definition */
00155 /* stun_sockaddr_t is used for:
00156    MAPPED_ADDRESS
00157    RESPONSE_ADDRESS
00158    SOURCE_ADDRESS
00159    CHANGED_ADDRESS
00160    REFLECTED_FROM
00161 */
00162 typedef struct sockaddr_in stun_attr_sockaddr_t;
00163 
00164 /* CHANGE_REQUEST attribute */
00165 typedef struct stun_attr_uint32_s {
00166   uint32_t value;
00167 } stun_attr_uint32_t;
00168 
00169 typedef stun_attr_uint32_t stun_attr_changerequest_t;
00170 
00171 /* ERROR_CODE attribute */
00172 typedef struct {
00173   int code;
00174   char *phrase;
00175 } stun_attr_errorcode_t;
00176 
00177 /* USERNAME attribute */
00178 /* typedef struct {
00179   stun_buffer_t *uname;
00180 } stun_attr_username_t;
00181 */
00182 typedef stun_buffer_t stun_attr_username_t;
00183 
00184 /* PASSWORD attribute */
00185 typedef stun_buffer_t stun_attr_password_t;
00186 
00187 /* UNKNOWN_ATTRIBUTES attribute */
00188 typedef struct stun_attr_unknownattributes_s{
00189   uint16_t attr_type[2];
00190   struct stun_attr_unknownattributes_s *next;
00191 } stun_attr_unknownattributes_t;
00192 
00193 /* Common functions */
00194 int stun_parse_message(stun_msg_t *msg);
00195 int stun_parse_attribute(stun_msg_t *msg, unsigned char *p);
00196 int stun_parse_attr_address(stun_attr_t *attr, const unsigned char *p, unsigned len);
00197 int stun_parse_attr_error_code(stun_attr_t *attr, const unsigned char *p, unsigned len);
00198 int stun_parse_attr_unknown_attributes(stun_attr_t *attr, const unsigned char *p, unsigned len);
00199 int stun_parse_attr_uint32(stun_attr_t *attr, const unsigned char *p, unsigned len);
00200 int stun_parse_attr_buffer(stun_attr_t *attr, const unsigned char *p, unsigned len);
00201 
00202 stun_attr_t *stun_get_attr(stun_attr_t *attr, uint16_t attr_type);
00203 
00204 int stun_encode_address(stun_attr_t *attr);
00205 int stun_encode_uint32(stun_attr_t *attr);
00206 int stun_encode_buffer(stun_attr_t *attr);
00207 int stun_encode_error_code(stun_attr_t *attr);
00208 int stun_encode_message_integrity(stun_attr_t *attr, unsigned char *buf, int len, stun_buffer_t *pwd);
00209 int stun_encode_type_len(stun_attr_t *attr, uint16_t len);
00210 int stun_encode_response_address(stun_attr_t *attr);
00211 
00212 int stun_validate_message_integrity(stun_msg_t *msg, stun_buffer_t *pwd); 
00213 
00214 int stun_copy_buffer(stun_buffer_t *p, stun_buffer_t *p2);
00215 void stun_init_buffer(stun_buffer_t *p);
00216 int stun_free_buffer(stun_buffer_t *p);
00217 int stun_free_message(stun_msg_t *msg);
00218 
00219 int stun_init_message(stun_msg_t *msg);
00220 /* int stun_send_message(int sockfd, struct sockaddr_in *to_addr, stun_msg_t *msg, stun_buffer_t *pwd); */
00221 int stun_encode_message(stun_msg_t *msg, stun_buffer_t *pwd);
00222 
00223 char const *stun_response_phrase(int status);
00224 void debug_print(stun_buffer_t *buf);
00225 char const *stun_attr_phrase(uint16_t type);
00226 
00234 char *stun_determine_ip_address(int family);
00235 
00236 SOFIA_END_DECLS
00237 
00238 #endif /* !defined STUN_COMMON_H */

Sofia-SIP 1.12.7 - Copyright (C) 2006 Nokia Corporation. All rights reserved. Licensed under the terms of the GNU Lesser General Public License.