Changeset 929 for pjproject


Ignore:
Timestamp:
Feb 3, 2007 8:31:33 PM (18 years ago)
Author:
bennylp
Message:

ICE branch: initial server prototype

Location:
pjproject/branches/iceproject/pjlib-util
Files:
2 added
8 edited

Legend:

Unmodified
Added
Removed
  • pjproject/branches/iceproject/pjlib-util/build/pjlib_util.dsp

    r913 r929  
    208208# Begin Source File 
    209209 
     210SOURCE="..\include\pjlib-util\stun_doc.h" 
     211# End Source File 
     212# Begin Source File 
     213 
    210214SOURCE="..\include\pjlib-util\stun_endpoint.h" 
    211215# End Source File 
     
    213217 
    214218SOURCE="..\include\pjlib-util\stun_msg.h" 
     219# End Source File 
     220# Begin Source File 
     221 
     222SOURCE="..\include\pjlib-util\stun_server.h" 
    215223# End Source File 
    216224# Begin Source File 
  • pjproject/branches/iceproject/pjlib-util/include/pjlib-util/stun_endpoint.h

    r913 r929  
    1 /* $Id */ 
     1/* $Id$ */ 
    22/*  
    33 * Copyright (C) 2003-2005 Benny Prijono <benny@prijono.org> 
     
    2121 
    2222/** 
    23  * @file stun_msg.h 
    24  * @brief STUN message components. 
     23 * @file stun_endpoint.h 
     24 * @brief STUN endpoint. 
    2525 */ 
    2626 
  • pjproject/branches/iceproject/pjlib-util/include/pjlib-util/stun_msg.h

    r913 r929  
    1 /* $Id */ 
     1/* $Id$ */ 
    22/*  
    33 * Copyright (C) 2003-2005 Benny Prijono <benny@prijono.org> 
     
    2828#include <pj/sock.h> 
    2929 
    30 /** 
    31  * @defgroup PJLIB_UTIL_STUN Simple Traversal Underneath NAT (STUN) 
    32  * @ingroup PJLIB_UTIL 
    33  */ 
    3430 
    3531PJ_BEGIN_DECL 
  • pjproject/branches/iceproject/pjlib-util/include/pjlib-util/stun_simple.h

    r913 r929  
    1 /* $Id */ 
     1/* $Id$ */ 
    22/*  
    33 * Copyright (C) 2003-2005 Benny Prijono <benny@prijono.org> 
     
    2828#include <pj/sock.h> 
    2929 
    30 /** 
    31  * @defgroup PJLIB_UTIL_STUN_CLIENT Mini/Tiny STUN Client 
    32  * @ingroup PJLIB_UTIL 
    33  * @{ 
    34  */ 
    3530 
    3631PJ_BEGIN_DECL 
    3732 
    38 /** 
     33/* 
    3934 * This enumeration describes STUN message types. 
    4035 */ 
     
    5045 
    5146 
    52 /** 
     47/* 
    5348 * This enumeration describes STUN attribute types. 
    5449 */ 
     
    6964 
    7065 
    71 /** 
     66/* 
    7267 * This structre describes STUN message header. 
    7368 */ 
     
    8075 
    8176 
    82 /** 
     77/* 
    8378 * This structre describes STUN attribute header. 
    8479 */ 
     
    9085 
    9186 
    92 /** 
     87/* 
    9388 * This structre describes STUN MAPPED-ADDR attribute. 
    9489 */ 
     
    147142PJ_DECL(void*) pj_stun_msg_find_attr( pj_stun_msg *msg, pj_stun_attr_type t); 
    148143 
     144 
     145/** 
     146 * @defgroup PJLIB_UTIL_STUN_CLIENT Simple STUN Helper 
     147 * @ingroup PJLIB_UTIL_STUN 
     148 * @brief A simple and small footprint STUN resolution helper 
     149 * @{ 
     150 * 
     151 * This is the older implementation of STUN client, with only one function 
     152 * provided (pj_stun_get_mapped_addr()) to retrieve the public IP address 
     153 * of multiple sockets. 
     154 */ 
    149155 
    150156/** 
  • pjproject/branches/iceproject/pjlib-util/include/pjlib-util/stun_transaction.h

    r913 r929  
    1 /* $Id */ 
     1/* $Id$ */ 
    22/*  
    33 * Copyright (C) 2003-2005 Benny Prijono <benny@prijono.org> 
     
    3838 * @ingroup PJLIB_UTIL_STUN 
    3939 * @{ 
     40 * 
     41 The @ref PJLIB_UTIL_STUN_TRANSACTION is used to manage outgoing STUN request, 
     42 for example to retransmit the request and to notify application about the 
     43 completion of the request. 
     44 
     45 The @ref PJLIB_UTIL_STUN_TRANSACTION does not use any networking operations, 
     46 but instead application must supply the transaction with a callback to 
     47 be used by the transaction to send outgoing requests. This way the STUN 
     48 transaction is made more generic and can work with different types of 
     49 networking codes in application. 
     50 
     51 
    4052 */ 
    4153 
     
    5062typedef struct pj_stun_tsx_cb 
    5163{ 
     64    /** 
     65     * This callback is called when the STUN transaction completed. 
     66     * 
     67     * @param tsx           The STUN transaction. 
     68     * @param status        Status of the transaction. Status PJ_SUCCESS 
     69     *                      means that the request has received a successful 
     70     *                      response. 
     71     * @param response      The STUN response, which value may be NULL if 
     72     *                      \a status is not PJ_SUCCESS. 
     73     */ 
    5274    void        (*on_complete)(pj_stun_client_tsx *tsx, 
    5375                               pj_status_t status,  
    5476                               pj_stun_msg *response); 
     77 
     78    /** 
     79     * This callback is called by the STUN transaction when it wants to send 
     80     * outgoing message. 
     81     * 
     82     * @param tsx           The STUN transaction instance. 
     83     * @param stun_pkt      The STUN packet to be sent. 
     84     * @param pkt_size      Size of the STUN packet. 
     85     * 
     86     * @return              If return value of the callback is not PJ_SUCCESS, 
     87     *                      the transaction will fail. 
     88     */ 
    5589    pj_status_t (*on_send_msg)(pj_stun_client_tsx *tsx, 
    5690                               const void *stun_pkt, 
     
    6296 
    6397/** 
    64  * Create a STUN client transaction. 
     98 * Create an instance of STUN client transaction. The STUN client  
     99 * transaction is used to transmit outgoing STUN request and to  
     100 * ensure the reliability of the request by periodically retransmitting 
     101 * the request, if necessary. 
     102 * 
     103 * @param endpt         The STUN endpoint, which will be used to retrieve 
     104 *                      various settings for the transaction. 
     105 * @param cb            Callback structure, to be used by the transaction 
     106 *                      to send message and to notify the application about 
     107 *                      the completion of the transaction. 
     108 * @param p_tsx         Pointer to receive the transaction instance. 
     109 * 
     110 * @return              PJ_SUCCESS on success, or the appropriate error code. 
    65111 */ 
    66112PJ_DECL(pj_status_t) pj_stun_client_tsx_create( pj_stun_endpoint *endpt, 
     
    69115 
    70116/** 
    71  * . 
     117 * Destroy a STUN client transaction. 
     118 * 
     119 * @param tsx           The STUN transaction. 
     120 * 
     121 * @return              PJ_SUCCESS on success, or the appropriate error code. 
    72122 */ 
    73123PJ_DECL(pj_status_t) pj_stun_client_tsx_destroy(pj_stun_client_tsx *tsx); 
     
    75125 
    76126/** 
    77  * . 
     127 * Associate an arbitrary data with the STUN transaction. This data 
     128 * can be then retrieved later from the transaction, by using 
     129 * pj_stun_client_tsx_get_data() function. 
     130 * 
     131 * @param tsx           The STUN client transaction. 
     132 * @param data          Application data to be associated with the 
     133 *                      STUN transaction. 
     134 * 
     135 * @return              PJ_SUCCESS on success. 
    78136 */ 
    79137PJ_DECL(pj_status_t) pj_stun_client_tsx_set_data(pj_stun_client_tsx *tsx, 
     
    82140 
    83141/** 
    84  * . 
     142 * Get the user data that was previously associated with the STUN  
     143 * transaction. 
     144 * 
     145 * @param tsx           The STUN client transaction. 
     146 * 
     147 * @return              The user data. 
    85148 */ 
    86149PJ_DECL(void*) pj_stun_client_tsx_get_data(pj_stun_client_tsx *tsx); 
     
    88151 
    89152/** 
    90  * . 
     153 * Start the STUN client transaction by sending STUN request using 
     154 * this transaction. If reliable transport such as TCP or TLS is used, 
     155 * the retransmit flag should be set to PJ_FALSE because reliablity 
     156 * will be assured by the transport layer. 
     157 * 
     158 * @param tsx           The STUN client transaction. 
     159 * @param retransmit    Should this message be retransmitted by the 
     160 *                      STUN transaction. 
     161 * @param msg           The STUN message. 
     162 * 
     163 * @return              PJ_SUCCESS on success or the appropriate error code. 
    91164 */ 
    92165PJ_DECL(pj_status_t) pj_stun_client_tsx_send_msg(pj_stun_client_tsx *tsx, 
     166                                                 pj_bool_t retransmit, 
    93167                                                 const pj_stun_msg *msg); 
    94168 
    95169 
    96170/** 
    97  * . 
     171 * Notify the STUN transaction about the arrival of STUN response. 
     172 * If the STUN response contains a final error (300 and greater), the 
     173 * transaction will be terminated and callback will be called. If the 
     174 * STUN response contains response code 100-299, retransmission 
     175 * will  cease, but application must still call this function again 
     176 * with a final response later to allow the transaction to complete. 
     177 * 
     178 * @param tsx           The STUN client transaction instance. 
     179 * @param packet        The incoming packet. 
     180 * @param pkt_size      Size of the incoming packet. 
     181 * @param parsed_len    Optional pointer to receive the number of bytes 
     182 *                      that have been parsed from the incoming packet 
     183 *                      for the STUN message. This is useful if the 
     184 *                      STUN transaction is running over stream oriented 
     185 *                      socket such as TCP or TLS. 
     186 * 
     187 * @return              PJ_SUCCESS on success or the appropriate error code. 
    98188 */ 
    99189PJ_DECL(pj_status_t) pj_stun_client_tsx_on_rx_msg(pj_stun_client_tsx *tsx, 
  • pjproject/branches/iceproject/pjlib-util/src/pjlib-util/stun_endpoint.c

    r913 r929  
    1 /* $Id */ 
     1/* $Id$ */ 
    22/*  
    33 * Copyright (C) 2003-2005 Benny Prijono <benny@prijono.org> 
  • pjproject/branches/iceproject/pjlib-util/src/pjlib-util/stun_msg.c

    r913 r929  
    1 /* $Id */ 
     1/* $Id$ */ 
    22/*  
    33 * Copyright (C) 2003-2005 Benny Prijono <benny@prijono.org> 
  • pjproject/branches/iceproject/pjlib-util/src/pjlib-util/stun_transaction.c

    r913 r929  
    1 /* $Id */ 
     1/* $Id$ */ 
    22/*  
    33 * Copyright (C) 2003-2005 Benny Prijono <benny@prijono.org> 
     
    3939    pj_uint32_t          tsx_id[4]; 
    4040 
     41    pj_bool_t            require_retransmit; 
    4142    pj_timer_entry       timer; 
    4243    unsigned             transmit_count; 
     
    109110 
    110111/* 
    111  * . 
     112 * Set user data. 
    112113 */ 
    113114PJ_DEF(pj_status_t) pj_stun_client_tsx_set_data(pj_stun_client_tsx *tsx, 
     
    121122 
    122123/* 
    123  * . 
     124 * Get the user data 
    124125 */ 
    125126PJ_DEF(void*) pj_stun_client_tsx_get_data(pj_stun_client_tsx *tsx) 
     
    139140    PJ_ASSERT_RETURN(tsx->timer.id == 0, PJ_EBUSY); 
    140141 
    141     /* Calculate retransmit/timeout delay */ 
    142     if (tsx->transmit_count == 0) { 
    143         tsx->retransmit_time.sec = 0; 
    144         tsx->retransmit_time.msec = tsx->endpt->rto_msec; 
    145  
    146     } else if (tsx->transmit_count < PJ_STUN_MAX_RETRANSMIT_COUNT) { 
    147         unsigned msec; 
    148  
    149         msec = PJ_TIME_VAL_MSEC(tsx->retransmit_time); 
    150         msec = (msec >> 1) + 100; 
    151         tsx->retransmit_time.sec = msec / 1000; 
    152         tsx->retransmit_time.msec = msec % 100; 
    153  
    154     } else { 
    155         tsx->retransmit_time.sec = PJ_STUN_TIMEOUT_VALUE / 1000; 
    156         tsx->retransmit_time.msec = PJ_STUN_TIMEOUT_VALUE % 1000; 
    157     } 
    158  
    159     /* Schedule timer first because when send_msg() failed we can 
    160      * cancel it (as opposed to when schedule_timer() failed we cannot 
    161      * cancel transmission). 
    162      */ 
    163     status = pj_timer_heap_schedule(tsx->endpt->timer_heap, &tsx->timer, 
    164                                     &tsx->retransmit_time); 
    165     if (status != PJ_SUCCESS) { 
    166         tsx->timer.id = 0; 
    167         return status; 
     142    if (tsx->require_retransmit) { 
     143        /* Calculate retransmit/timeout delay */ 
     144        if (tsx->transmit_count == 0) { 
     145            tsx->retransmit_time.sec = 0; 
     146            tsx->retransmit_time.msec = tsx->endpt->rto_msec; 
     147 
     148        } else if (tsx->transmit_count < PJ_STUN_MAX_RETRANSMIT_COUNT) { 
     149            unsigned msec; 
     150 
     151            msec = PJ_TIME_VAL_MSEC(tsx->retransmit_time); 
     152            msec = (msec >> 1) + 100; 
     153            tsx->retransmit_time.sec = msec / 1000; 
     154            tsx->retransmit_time.msec = msec % 100; 
     155 
     156        } else { 
     157            tsx->retransmit_time.sec = PJ_STUN_TIMEOUT_VALUE / 1000; 
     158            tsx->retransmit_time.msec = PJ_STUN_TIMEOUT_VALUE % 1000; 
     159        } 
     160 
     161        /* Schedule timer first because when send_msg() failed we can 
     162         * cancel it (as opposed to when schedule_timer() failed we cannot 
     163         * cancel transmission). 
     164         */ 
     165        status = pj_timer_heap_schedule(tsx->endpt->timer_heap, &tsx->timer, 
     166                                        &tsx->retransmit_time); 
     167        if (status != PJ_SUCCESS) { 
     168            tsx->timer.id = 0; 
     169            return status; 
     170        } 
    168171    } 
    169172 
     
    172175    status = tsx->cb.on_send_msg(tsx, tsx->last_pkt, tsx->last_pkt_size); 
    173176    if (status != PJ_SUCCESS) { 
    174         pj_timer_heap_cancel(tsx->endpt->timer_heap, &tsx->timer); 
    175         tsx->timer.id = 0; 
     177        if (tsx->timer.id != 0) { 
     178            pj_timer_heap_cancel(tsx->endpt->timer_heap, &tsx->timer); 
     179            tsx->timer.id = 0; 
     180        } 
    176181        stun_perror(tsx, "STUN error sending message", status); 
    177182        return status; 
     
    185190} 
    186191 
    187 /* 
    188  * . 
     192 
     193/* 
     194 * Send outgoing message and start STUN transaction. 
    189195 */ 
    190196PJ_DEF(pj_status_t) pj_stun_client_tsx_send_msg(pj_stun_client_tsx *tsx, 
     197                                                pj_bool_t retransmit, 
    191198                                                const pj_stun_msg *msg) 
    192199{ 
     
    208215    pj_memcpy(&tsx->tsx_id[1], msg->hdr.tsx_id, 12); 
    209216 
     217    /* Update STUN retransmit flag */ 
     218    tsx->require_retransmit = retransmit; 
     219 
     220    /* Send the message */ 
    210221    return tsx_transmit_msg(tsx); 
    211222} 
     
    243254 
    244255/* 
    245  *  
     256 * Notify the STUN transaction about the arrival of STUN response. 
    246257 */ 
    247258PJ_DEF(pj_status_t) pj_stun_client_tsx_on_rx_msg(pj_stun_client_tsx *tsx, 
     
    292303    err_attr = (pj_stun_error_code_attr*)  
    293304                pj_stun_msg_find_attr(msg, PJ_STUN_ATTR_ERROR_CODE, 0); 
    294     if (err_attr->err_class == 1) { 
     305 
     306    if (err_attr && err_attr->err_class <= 2) { 
     307        /* draft-ietf-behave-rfc3489bis-05.txt Section 8.3.2: 
     308         * Any response between 100 and 299 MUST result in the cessation 
     309         * of request retransmissions, but otherwise is discarded. 
     310         */ 
    295311        PJ_LOG(4,(tsx->obj_name,  
    296312                  "STUN rx_msg() error: received provisional %d code (%.*s)", 
     
    301317    } 
    302318 
    303     if (err_attr==NULL || err_attr->err_class == 2) { 
     319    if (err_attr == NULL) { 
    304320        status = PJ_SUCCESS; 
    305321    } else { 
Note: See TracChangeset for help on using the changeset viewer.