Changeset 1053


Ignore:
Timestamp:
Mar 10, 2007 11:19:11 AM (17 years ago)
Author:
bennylp
Message:

Started working on sample TURN client

Location:
pjproject/trunk/pjlib-util
Files:
1 added
10 edited

Legend:

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

    r1037 r1053  
    278278# Begin Source File 
    279279 
     280SOURCE="..\include\pjlib-util\turn_client.h" 
     281# End Source File 
     282# Begin Source File 
     283 
    280284SOURCE="..\include\pjlib-util\types.h" 
    281285# End Source File 
  • pjproject/trunk/pjlib-util/include/pjlib-util.h

    r1037 r1053  
    1 /* $Id */ 
     1/* $Id$ */ 
    22/*  
    33 * Copyright (C)2003-2007 Benny Prijono <benny@prijono.org> 
     
    5555#include <pjlib-util/stun_session.h> 
    5656#include <pjlib-util/stun_transaction.h> 
     57/*#include <pjlib-util/turn_client.h>*/ 
    5758 
    5859/* Old STUN */ 
  • pjproject/trunk/pjlib-util/include/pjlib-util/stun_auth.h

    r1037 r1053  
    1717 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA  
    1818 */ 
    19 #ifndef __PJ_STUN_AUTH_H__ 
    20 #define __PJ_STUN_AUTH_H__ 
     19#ifndef __PJLIB_UTIL_STUN_AUTH_H__ 
     20#define __PJLIB_UTIL_STUN_AUTH_H__ 
    2121 
    2222/** 
     
    272272 
    273273 
    274 #endif  /* __PJ_STUN_AUTH_H__ */ 
    275  
     274#endif  /* __PJLIB_UTIL_STUN_AUTH_H__ */ 
     275 
  • pjproject/trunk/pjlib-util/include/pjlib-util/stun_endpoint.h

    r1034 r1053  
    1717 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA  
    1818 */ 
    19 #ifndef __PJ_STUN_ENDPOINT_H__ 
    20 #define __PJ_STUN_ENDPOINT_H__ 
     19#ifndef __PJLIB_UTIL_STUN_ENDPOINT_H__ 
     20#define __PJLIB_UTIL_STUN_ENDPOINT_H__ 
    2121 
    2222/** 
     
    114114 
    115115 
    116 #endif  /* __PJ_STUN_ENDPOINT_H__ */ 
     116#endif  /* __PJLIB_UTIL_STUN_ENDPOINT_H__ */ 
    117117 
  • pjproject/trunk/pjlib-util/include/pjlib-util/stun_msg.h

    r1052 r1053  
    1717 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA  
    1818 */ 
    19 #ifndef __PJ_STUN_MSG_H__ 
    20 #define __PJ_STUN_MSG_H__ 
     19#ifndef __PJLIB_UTIL_STUN_MSG_H__ 
     20#define __PJLIB_UTIL_STUN_MSG_H__ 
    2121 
    2222/** 
     
    15301530 
    15311531 
    1532 #endif  /* __PJ_STUN_MSG_H__ */ 
    1533  
     1532#endif  /* __PJLIB_UTIL_STUN_MSG_H__ */ 
     1533 
  • pjproject/trunk/pjlib-util/include/pjlib-util/stun_session.h

    r1052 r1053  
    5656     * Callback to be called by the STUN session to send outgoing message. 
    5757     * 
    58      * @param tdata         The STUN transmit data containing the original 
    59      *                      STUN message 
     58     * @param sess          The STUN session. 
    6059     * @param pkt           Packet to be sent. 
    6160     * @param pkt_size      Size of the packet to be sent. 
     61     * @param dst_addr      The destination address. 
    6262     * @param addr_len      Length of destination address. 
    63      * @param dst_addr      The destination address. 
    6463     * 
    6564     * @return              The callback should return the status of the 
  • pjproject/trunk/pjlib-util/include/pjlib-util/stun_transaction.h

    r996 r1053  
    1717 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA  
    1818 */ 
    19 #ifndef __PJ_STUN_TRANSACTION_H__ 
    20 #define __PJ_STUN_TRANSACTION_H__ 
     19#ifndef __PJLIB_UTIL_STUN_TRANSACTION_H__ 
     20#define __PJLIB_UTIL_STUN_TRANSACTION_H__ 
    2121 
    2222/** 
     
    214214 
    215215 
    216 #endif  /* __PJ_STUN_TRANSACTION_H__ */ 
    217  
     216#endif  /* __PJLIB_UTIL_STUN_TRANSACTION_H__ */ 
     217 
  • pjproject/trunk/pjlib-util/src/pjlib-util/stun_msg.c

    r1052 r1053  
    406406    }, 
    407407    { 
    408         /* Unassigned, 0x8025 */ 
     408        /* ID 0x8025 is not assigned*/ 
    409409        NULL, 
    410410        NULL, 
     
    412412    }, 
    413413    { 
    414         /* Padding, 0x8026 */ 
     414        /* PADDING, 0x8026 */ 
    415415        NULL, 
    416416        NULL, 
     
    418418    }, 
    419419    { 
    420         /* Padding, 0x8027 */ 
     420        /* CACHE-TIMEOUT, 0x8027 */ 
    421421        NULL, 
    422422        NULL, 
  • pjproject/trunk/pjlib-util/src/pjstun-client/client_main.c

    r1052 r1053  
    2222 
    2323#define THIS_FILE       "client_main.c" 
     24#define BANDWIDTH       64                  /* -1 to disable */ 
     25#define LIFETIME        30                  /* -1 to disable */ 
     26#define REQ_TRANSPORT   -1                  /* 0: udp, 1: tcp, -1: disable */ 
     27#define REQ_PORT_PROPS  -1                  /* -1 to disable */ 
     28#define REQ_IP          NULL                /* IP address string */ 
     29 
    2430 
    2531static struct global 
     
    3339    pj_thread_t         *thread; 
    3440    pj_bool_t            quit; 
    35  
    36     pj_sockaddr_in       dst_addr;  /**< destination addr */ 
     41    pj_sockaddr_in       peer_addr; 
     42    pj_sockaddr_in       srv_addr;  /**< server addr */ 
    3743 
    3844} g; 
     
    4046static struct options 
    4147{ 
    42     char    *dst_addr; 
    43     char    *dst_port; 
     48    char    *srv_addr; 
     49    char    *srv_port; 
    4450    char    *realm; 
    4551    char    *user_name; 
     
    6167                               const void *pkt, 
    6268                               pj_size_t pkt_size, 
    63                                const pj_sockaddr_t *dst_addr, 
     69                               const pj_sockaddr_t *srv_addr, 
    6470                               unsigned addr_len) 
    6571{ 
     
    6874 
    6975    len = pkt_size; 
    70     status = pj_sock_sendto(g.sock, pkt, &len, 0, dst_addr, addr_len); 
     76    status = pj_sock_sendto(g.sock, pkt, &len, 0, srv_addr, addr_len); 
    7177 
    7278    if (status != PJ_SUCCESS) 
     
    142148    pj_caching_pool_init(&g.cp, &pj_pool_factory_default_policy, 0); 
    143149 
    144     if (o.dst_addr) { 
     150    if (o.srv_addr) { 
    145151        pj_str_t s; 
    146152        pj_uint16_t port; 
    147153 
    148         if (o.dst_port) 
    149             port = (pj_uint16_t) atoi(o.dst_port); 
     154        if (o.srv_port) 
     155            port = (pj_uint16_t) atoi(o.srv_port); 
    150156        else 
    151157            port = PJ_STUN_PORT; 
    152158 
    153         status = pj_sockaddr_in_init(&g.dst_addr, pj_cstr(&s, o.dst_addr), port); 
     159        status = pj_sockaddr_in_init(&g.srv_addr, pj_cstr(&s, o.srv_addr), port); 
    154160        if (status != PJ_SUCCESS) { 
    155161            my_perror("Invalid address", status); 
     
    157163        } 
    158164 
    159         printf("Destination address set to %s:%d\n", o.dst_addr, (int)port); 
     165        printf("Destination address set to %s:%d\n", o.srv_addr, (int)port); 
    160166    } else { 
    161167        printf("Error: address must be specified\n"); 
     
    246252 
    247253    rc = pj_stun_session_send_msg(g.sess, PJ_FALSE,  
    248                                   &g.dst_addr, sizeof(g.dst_addr), 
     254                                  &g.srv_addr, sizeof(g.srv_addr), 
    249255                                  tdata); 
    250256    if (rc != PJ_SUCCESS) 
     
    252258} 
    253259 
    254 static void send_allocate_request(void) 
    255 { 
    256 } 
    257  
    258 static void send_sad_request(void) 
    259 { 
     260static void send_allocate_request(pj_bool_t allocate) 
     261{ 
     262    pj_stun_tx_data *tdata; 
     263    pj_status_t rc; 
     264 
     265    rc = pj_stun_session_create_req(g.sess, PJ_STUN_ALLOCATE_REQUEST, &tdata); 
     266    pj_assert(rc == PJ_SUCCESS); 
     267 
     268 
     269    if (BANDWIDTH != -1) { 
     270        pj_stun_msg_add_uint_attr(tdata->pool, tdata->msg,  
     271                                  PJ_STUN_ATTR_BANDWIDTH, BANDWIDTH); 
     272    } 
     273 
     274    if (!allocate) { 
     275        pj_stun_msg_add_uint_attr(tdata->pool, tdata->msg,  
     276                                  PJ_STUN_ATTR_LIFETIME, 0); 
     277 
     278    } else { 
     279        if (LIFETIME != -1) { 
     280            pj_stun_msg_add_uint_attr(tdata->pool, tdata->msg,  
     281                                      PJ_STUN_ATTR_LIFETIME, LIFETIME); 
     282        } 
     283 
     284        if (REQ_TRANSPORT != -1) { 
     285            pj_stun_msg_add_uint_attr(tdata->pool, tdata->msg,  
     286                                      PJ_STUN_ATTR_REQ_TRANSPORT, REQ_TRANSPORT); 
     287        } 
     288 
     289        if (REQ_PORT_PROPS != -1) { 
     290            pj_stun_msg_add_uint_attr(tdata->pool, tdata->msg,  
     291                                      PJ_STUN_ATTR_REQ_PORT_PROPS, REQ_PORT_PROPS); 
     292        } 
     293 
     294        if (REQ_IP != NULL) { 
     295            pj_sockaddr_in addr; 
     296            pj_str_t tmp; 
     297 
     298            pj_sockaddr_in_init(&addr, pj_cstr(&tmp, REQ_IP), 0); 
     299            pj_stun_msg_add_ip_addr_attr(tdata->pool, tdata->msg, 
     300                                         PJ_STUN_ATTR_REQ_IP, PJ_FALSE, 
     301                                         &addr, sizeof(addr)); 
     302        } 
     303    } 
     304 
     305    rc = pj_stun_session_send_msg(g.sess, PJ_FALSE,  
     306                                  &g.srv_addr, sizeof(g.srv_addr), 
     307                                  tdata); 
     308    pj_assert(rc == PJ_SUCCESS); 
     309} 
     310 
     311static void send_sad_request(pj_bool_t set) 
     312{ 
     313    pj_stun_tx_data *tdata; 
     314    pj_status_t rc; 
     315 
     316    if (g.peer_addr.sin_addr.s_addr == 0 || 
     317        g.peer_addr.sin_port == 0) 
     318    { 
     319        puts("Error: peer address is not set"); 
     320        return; 
     321    } 
     322 
     323    rc = pj_stun_session_create_req(g.sess, PJ_STUN_ALLOCATE_REQUEST, &tdata); 
     324    pj_assert(rc == PJ_SUCCESS); 
     325 
     326    if (set) { 
     327        pj_stun_msg_add_ip_addr_attr(tdata->pool, tdata->msg, 
     328                                     PJ_STUN_ATTR_REMOTE_ADDR, PJ_FALSE, 
     329                                     &g.peer_addr, sizeof(g.peer_addr)); 
     330    } 
     331 
     332    rc = pj_stun_session_send_msg(g.sess, PJ_FALSE,  
     333                                  &g.srv_addr, sizeof(g.srv_addr), 
     334                                  tdata); 
     335    pj_assert(rc == PJ_SUCCESS); 
    260336} 
    261337 
     
    268344} 
    269345 
     346static void set_peer_addr(void) 
     347{ 
     348    char ip_addr[64]; 
     349    pj_str_t tmp; 
     350    pj_sockaddr_in addr; 
     351    int port; 
     352 
     353    printf("Current peer address: %s:%d\n",  
     354           pj_inet_ntoa(g.peer_addr.sin_addr),  
     355           pj_ntohs(g.peer_addr.sin_port)); 
     356 
     357    printf("Input peer address in IP:PORT format: "); 
     358    fflush(stdout); 
     359 
     360    if (scanf("%s:%d", ip_addr, &port) != 2) { 
     361        puts("Error."); 
     362        return; 
     363    } 
     364 
     365    if (pj_sockaddr_in_init(&addr, pj_cstr(&tmp,ip_addr), (pj_uint16_t)port) != PJ_SUCCESS) { 
     366        puts("Error: invalid address"); 
     367        return; 
     368    } 
     369 
     370    g.peer_addr = addr; 
     371} 
     372 
    270373static void menu(void) 
    271374{ 
    272375    puts("Menu:"); 
     376    printf("  pr      Set peer address (currently %s:%d)\n", 
     377           pj_inet_ntoa(g.peer_addr.sin_addr), pj_ntohs(g.peer_addr.sin_port)); 
     378    puts(""); 
    273379    puts("  br      Send Bind request"); 
    274380    puts("  ar      Send Allocate request"); 
     381    puts("  dr      Send de-Allocate request"); 
    275382    puts("  sr      Send Set Active Indication request"); 
    276     puts("  si      Send Send Indication"); 
     383    puts("  cr      Send clear Active Indication request"); 
     384    puts("  si      Send data with Send Indication"); 
    277385    puts("  rw      Send raw data"); 
    278386    puts("  q       Quit"); 
     
    281389} 
    282390 
     391 
    283392static void console_main(void) 
    284393{ 
     
    294403             
    295404        } else if (input[0]=='a' && input[1]=='r') { 
    296             send_allocate_request(); 
     405            send_allocate_request(PJ_TRUE); 
     406             
     407        } else if (input[0]=='d' && input[1]=='r') { 
     408            send_allocate_request(PJ_FALSE); 
    297409             
    298410        } else if (input[0]=='s' && input[1]=='r') { 
    299             send_sad_request(); 
     411            send_sad_request(PJ_TRUE); 
     412             
     413        } else if (input[0]=='c' && input[1]=='r') { 
     414            send_sad_request(PJ_FALSE); 
    300415             
    301416        } else if (input[0]=='s' && input[1]=='i') { 
     
    304419        } else if (input[0]=='r' && input[1]=='w') { 
    305420            send_raw_data(); 
     421             
     422        } else if (input[0]=='p' && input[1]=='r') { 
     423            set_peer_addr(); 
    306424             
    307425        } else if (input[0]=='q') { 
     
    374492 
    375493    if ((pos=pj_ansi_strchr(argv[pj_optind], ':')) != NULL) { 
    376         o.dst_addr = argv[pj_optind]; 
     494        o.srv_addr = argv[pj_optind]; 
    377495        *pos = '\0'; 
    378         o.dst_port = pos+1; 
     496        o.srv_port = pos+1; 
    379497    } else { 
    380         o.dst_addr = argv[pj_optind]; 
     498        o.srv_addr = argv[pj_optind]; 
    381499    } 
    382500 
  • pjproject/trunk/pjlib-util/src/pjstun-srv-test/turn_usage.c

    r1052 r1053  
    9090    pj_stun_session     *session; 
    9191    pj_mutex_t          *mutex; 
     92 
     93    /* Socket and socket address of the allocated port */ 
    9294    int                  sock_type; 
    93  
    94     /* Socket and socket address of the allocated port */ 
    9595    pj_sock_t            sock; 
    9696    pj_ioqueue_key_t    *key; 
     
    504504                                    pj_uint32_t hval); 
    505505 
     506static const char *get_tp_type(int type) 
     507{ 
     508    if (type==0) 
     509        return "udp"; 
     510    else if (type==1) 
     511        return "tcp"; 
     512    else 
     513        return "???"; 
     514} 
     515 
    506516 
    507517/* 
     
    576586        const pj_sockaddr_in *a4 = (const pj_sockaddr_in *)src_addr; 
    577587        pj_ansi_snprintf(client->obj_name, sizeof(client->obj_name), 
    578                          "%s:%d", 
     588                         "%s:%s:%d", 
     589                         get_tp_type(tu->type), 
    579590                         pj_inet_ntoa(a4->sin_addr), 
    580591                         (int)pj_ntohs(a4->sin_port)); 
     
    586597    sess_cb.on_send_msg = &client_sess_on_send_msg; 
    587598    sess_cb.on_rx_request = &client_sess_on_rx_request; 
    588     status = pj_stun_session_create(tu->endpt, "turnc%p", &sess_cb, PJ_FALSE, 
     599    status = pj_stun_session_create(tu->endpt, client->obj_name,  
     600                                    &sess_cb, PJ_FALSE, 
    589601                                    &client->session); 
    590602    if (status != PJ_SUCCESS) { 
     
    599611 
    600612    /* Mutex */ 
    601     status = pj_mutex_create_recursive(client->pool, pool->obj_name,  
     613    status = pj_mutex_create_recursive(client->pool, pool->obj_name, 
    602614                                       &client->mutex); 
    603615    if (status != PJ_SUCCESS) { 
     
    693705{ 
    694706    pj_ioqueue_callback client_ioq_cb; 
     707    int addrlen; 
    695708    pj_status_t status; 
     709 
     710    /* Update address */ 
     711    addrlen = sizeof(pj_sockaddr_in); 
     712    status = pj_sock_getsockname(client->sock, &client->client_addr,  
     713                                 &addrlen); 
     714    if (status != PJ_SUCCESS) { 
     715        pj_sock_close(client->sock); 
     716        client->sock = PJ_INVALID_SOCKET; 
     717        return status; 
     718    } 
    696719 
    697720    /* Register to ioqueue */ 
     
    715738    client_on_read_complete(client->key, &client->pkt_read_key, 0); 
    716739 
     740    PJ_LOG(4,(THIS_FILE, "TURN client %s: relay allocated on %s:%s:%d", 
     741              client->obj_name, 
     742              get_tp_type(client->sock_type), 
     743              pj_inet_ntoa(client->client_addr.sin_addr), 
     744              (int)pj_ntohs(client->client_addr.sin_port))); 
     745 
    717746    return PJ_SUCCESS; 
    718747} 
     
    735764    } 
    736765 
    737     PJ_LOG(4,(THIS_FILE, "TURN client %s: relay allocation %s:%d destroyed", 
     766    PJ_LOG(4,(THIS_FILE, "TURN client %s: relay allocation %s:%s:%d destroyed", 
    738767              client->obj_name, 
     768              get_tp_type(client->sock_type), 
    739769              pj_inet_ntoa(client->client_addr.sin_addr), 
    740770              (int)pj_ntohs(client->client_addr.sin_port))); 
     
    772802 
    773803    PJ_LOG(4,(THIS_FILE, "TURN client %s: peer %s:%s:%d added", 
    774               client->obj_name, "udp", pj_inet_ntoa(peer_addr->sin_addr), 
     804              client->obj_name, get_tp_type(client->sock_type),  
     805              pj_inet_ntoa(peer_addr->sin_addr), 
    775806              (int)pj_ntohs(peer_addr->sin_port))); 
    776807 
     
    825856    pj_sockaddr_in req_addr; 
    826857    int addr_len; 
    827     unsigned rpp_bits; 
     858    unsigned req_bw, rpp_bits; 
    828859    pj_time_val timeout; 
    829860    pj_status_t status; 
     
    849880        return PJ_SUCCESS; 
    850881    } else if (a_bw) { 
    851         client->bw_kbps = a_bw->value; 
     882        client->bw_kbps = req_bw = a_bw->value; 
    852883    } else { 
     884        req_bw = 0; 
    853885        client->bw_kbps = client->tu->max_bw_kbps; 
    854886    } 
     
    900932 
    901933        PJ_LOG(4,(THIS_FILE, "TURN client %s: received initial Allocate " 
    902                              "request, requested type:addr:port=%d:%s:%d, rpp " 
    903                              "bits=%d", 
    904                   client->obj_name, client->sock_type, 
     934                             "request, requested type:addr:port=%s:%s:%d, rpp " 
     935                             "bits=%d, bw=%dkbps, lifetime=%d", 
     936                  client->obj_name, get_tp_type(client->sock_type), 
    905937                  pj_inet_ntoa(req_addr.sin_addr), pj_ntohs(req_addr.sin_port), 
    906                   rpp_bits)); 
     938                  rpp_bits, client->bw_kbps, client->lifetime)); 
    907939 
    908940        status = tu_alloc_port(client->tu, client->sock_type, rpp_bits,  
     
    931963        /* Otherwise check if the port parameter stays the same */ 
    932964        /* TODO */ 
    933         PJ_LOG(4,(THIS_FILE, "TURN client %s: received Allocate refresh", 
    934                   client->obj_name)); 
     965        PJ_LOG(4,(THIS_FILE, "TURN client %s: received Allocate refresh, " 
     966                             "lifetime=%d", 
     967                  client->obj_name, client->lifetime)); 
    935968    } 
    936969 
     
    9701003 
    9711004    PJ_LOG(4,(THIS_FILE, "TURN client %s: relay allocated or refreshed, " 
    972                          "internal address is %s:%d", 
     1005                         "internal address is %s:%s:%d", 
    9731006                         client->obj_name, 
     1007                         get_tp_type(client->sock_type), 
    9741008                         pj_inet_ntoa(req_addr.sin_addr), 
    9751009                         (int)pj_ntohs(req_addr.sin_port))); 
Note: See TracChangeset for help on using the changeset viewer.