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 <stdio.h>
00040 #include <string.h>
00041 #include <stdlib.h>
00042 #include <time.h>
00043 
00044 #ifdef WIN32
00045 #include <winsock.h>
00046 #else
00047 #include <sys/socket.h>
00048 #include <netinet/tcp.h>
00049 #include <netinet/in.h>
00050 #include <arpa/inet.h>
00051 #endif
00052 
00053 #include <sofia-sip/su.h>
00054 #include <sofia-sip/su_localinfo.h>
00055 
00056 SOFIA_BEGIN_DECLS
00057 
00058 /* Define Message Types */
00059 #define BINDING_REQUEST               0x0001
00060 #define BINDING_RESPONSE              0x0101
00061 #define BINDING_ERROR_RESPONSE        0x0111
00062 #define SHARED_SECRET_REQUEST         0x0002
00063 #define SHARED_SECRET_RESPONSE        0x0102
00064 #define SHARED_SECRET_ERROR_RESPONSE  0x0112
00065 
00066 /* Define Attribute Types */
00067 #define MAPPED_ADDRESS                0x0001
00068 #define RESPONSE_ADDRESS              0x0002
00069 #define CHANGE_REQUEST                0x0003
00070 #define SOURCE_ADDRESS                0x0004
00071 #define CHANGED_ADDRESS               0x0005
00072 #define USERNAME                      0x0006
00073 #define PASSWORD                      0x0007
00074 #define MESSAGE_INTEGRITY             0x0008
00075 #define ERROR_CODE                    0x0009
00076 #define UNKNOWN_ATTRIBUTES            0x000a
00077 #define REFLECTED_FROM                0x000b
00078 #define STUN_A_REALM                  0x0014 /* XXX: check value in 3489bis-05+ */
00079 #define STUN_A_NONCE                  0x0015 /* XXX: check value in 3489bis-05+ */
00080 #define STUN_A_XOR_MAPPED_ADDRESS     0x0020
00081 #define STUN_A_FINGERPRINT            0x0023
00082 #define STUN_A_SERVER                 0x8022
00083 #define STUN_A_ALTERNATE_SERVER       0x8023
00084 #define STUN_A_REFRESH_INTERVAL       0x8024
00085 
00086 /* Defines for mandatory and optional attributes */
00087 #define STUN_A_LAST_MANDATORY         0x0023 
00090 #define STUN_A_OPTIONAL               0x7fff
00091 
00092 /* Compability attribute types */
00093 #define STUN_A_ALTERNATE_SERVER_DEP   0x000e 
00094 #define STUN_A_BUGGYSERVER_XORONLY    0x0021 
00095 #define STUN_A_BUGGYSERVER_SERVER     0x0022 
00096 #define LARGEST_ATTRIBUTE             STUN_A_LAST_MANDATORY 
00097 #define OPTIONAL_ATTRIBUTE            STUN_A_OPTIONAL 
00099 /* Stun response codes */
00100 #define STUN_400_BAD_REQUEST             400
00101 #define STUN_401_UNAUTHORIZED            401
00102 #define STUN_420_UNKNOWN_ATTRIBUTE       420
00103 #define STUN_430_STALE_CREDENTIALS       430
00104 #define STUN_431_INTEGRITY_CHECK_FAILURE 431
00105 #define STUN_432_MISSING_USERNAME        432
00106 #define STUN_433_USE_TLS                 433
00107 #define STUN_500_SERVER_ERROR            500
00108 #define STUN_600_GLOBAL_FAILURE          600
00109 
00110 /* flags for CHANGE_REQUEST */
00111 #define STUN_CR_CHANGE_IP               0x0004
00112 #define STUN_CR_CHANGE_PORT             0x0002
00113 
00114 /* mask for ERROR_CODE */
00115 #define STUN_EC_CLASS                   0x0070
00116 #define STUN_EC_NUM                     0x000F
00117 
00118 #define RAND_MAX_16                     65535
00119 
00120 #define STUN_TID_BYTES                  16
00121 
00122 /* other protocol specific parameters */
00123 #define STUN_MAX_RETRX                  5 /* should be 8? */
00124 #define STUN_MAX_RETRX_INT              1600  
00126 #define STUN_DEFAULT_PORT               3478  
00128 /*
00129  * STUN header format
00130  */
00131   /*
00132     0                   1                   2                   3
00133     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
00134    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
00135    |         message type          |       message length          |
00136    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
00137    |                                                               |
00138    |                       Transaction ID                          |
00139    |                                                               |
00140    |                                                               |
00141    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
00142   */
00143 struct stun_buffer_s {
00144   unsigned char *data;      
00145   unsigned size;            
00146 };
00147 
00148 typedef struct stun_buffer_s stun_buffer_t;
00149 
00150 typedef struct {
00151   uint16_t msg_type;        
00152   uint16_t msg_len;         
00153   uint8_t tran_id[16];      
00154 } stun_hdr_t;
00155 
00156 typedef struct stun_attr_s {
00157   uint16_t attr_type;       
00158   void *pattr;              
00159   stun_buffer_t enc_buf;    
00160   struct stun_attr_s *next; 
00161 } stun_attr_t;
00162 
00163 typedef struct {
00164   stun_hdr_t stun_hdr;
00165   stun_attr_t *stun_attr;
00166   stun_buffer_t enc_buf;    
00167 } stun_msg_t;
00168 
00169 /* stun attribute definition */
00170 /* stun_sockaddr_t is used for:
00171    MAPPED_ADDRESS
00172    RESPONSE_ADDRESS
00173    SOURCE_ADDRESS
00174    CHANGED_ADDRESS
00175    REFLECTED_FROM
00176 */
00177 typedef struct sockaddr_in stun_attr_sockaddr_t;
00178 
00179 /* CHANGE_REQUEST attribute */
00180 typedef struct stun_attr_uint32_s {
00181   uint32_t value;
00182 } stun_attr_uint32_t;
00183 
00184 typedef stun_attr_uint32_t stun_attr_changerequest_t;
00185 
00186 /* ERROR_CODE attribute */
00187 typedef struct {
00188   int code;
00189   char *phrase;
00190 } stun_attr_errorcode_t;
00191 
00192 /* USERNAME attribute */
00193 /* typedef struct {
00194   stun_buffer_t *uname;
00195 } stun_attr_username_t;
00196 */
00197 typedef stun_buffer_t stun_attr_username_t;
00198 
00199 /* PASSWORD attribute */
00200 typedef stun_buffer_t stun_attr_password_t;
00201 
00202 /* UNKNOWN_ATTRIBUTES attribute */
00203 typedef struct stun_attr_unknownattributes_s{
00204   uint16_t attr_type[2];
00205   struct stun_attr_unknownattributes_s *next;
00206 } stun_attr_unknownattributes_t;
00207 
00208 /* Common functions */
00209 int stun_parse_message(stun_msg_t *msg);
00210 int stun_parse_attribute(stun_msg_t *msg, unsigned char *p);
00211 int stun_parse_attr_address(stun_attr_t *attr, const unsigned char *p, unsigned len);
00212 int stun_parse_attr_error_code(stun_attr_t *attr, const unsigned char *p, unsigned len);
00213 int stun_parse_attr_unknown_attributes(stun_attr_t *attr, const unsigned char *p, unsigned len);
00214 int stun_parse_attr_uint32(stun_attr_t *attr, const unsigned char *p, unsigned len);
00215 int stun_parse_attr_buffer(stun_attr_t *attr, const unsigned char *p, unsigned len);
00216 
00217 stun_attr_t *stun_get_attr(stun_attr_t *attr, uint16_t attr_type);
00218 
00219 int stun_encode_address(stun_attr_t *attr);
00220 int stun_encode_uint32(stun_attr_t *attr);
00221 int stun_encode_buffer(stun_attr_t *attr);
00222 int stun_encode_error_code(stun_attr_t *attr);
00223 int stun_encode_message_integrity(stun_attr_t *attr, unsigned char *buf, int len, stun_buffer_t *pwd);
00224 int stun_encode_type_len(stun_attr_t *attr, uint16_t len);
00225 int stun_encode_response_address(stun_attr_t *attr);
00226 
00227 int stun_validate_message_integrity(stun_msg_t *msg, stun_buffer_t *pwd); 
00228 
00229 int stun_copy_buffer(stun_buffer_t *p, stun_buffer_t *p2);
00230 void stun_init_buffer(stun_buffer_t *p);
00231 int stun_free_buffer(stun_buffer_t *p);
00232 int stun_free_message(stun_msg_t *msg);
00233 
00234 int stun_init_message(stun_msg_t *msg);
00235 /* int stun_send_message(int sockfd, struct sockaddr_in *to_addr, stun_msg_t *msg, stun_buffer_t *pwd); */
00236 int stun_encode_message(stun_msg_t *msg, stun_buffer_t *pwd);
00237 
00238 char const *stun_response_phrase(int status);
00239 void debug_print(stun_buffer_t *buf);
00240 char const *stun_attr_phrase(uint16_t type);
00241 
00249 char *stun_determine_ip_address(int family);
00250 
00251 SOFIA_END_DECLS
00252 
00253 #endif /* !defined STUN_COMMON_H */

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