Changeset 4343
- Timestamp:
- Feb 7, 2013 9:35:34 AM (12 years ago)
- Location:
- pjproject/trunk
- Files:
-
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
pjproject/trunk/pjlib/include/pj/sock.h
r3841 r4343 1167 1167 pj_uint16_t port); 1168 1168 1169 /** 1170 * Bind the IP socket sockfd to the given address and a random port in the 1171 * specified range. 1172 * 1173 * @param sockfd The socket desriptor. 1174 * @param addr The local address and port to bind the socket to. 1175 * @param port_range The port range, relative the to start port number 1176 * specified in port field in #addr. Note that if the 1177 * port is zero, this param will be ignored. 1178 * @param max_try Maximum retries. 1179 * 1180 * @return Zero on success. 1181 */ 1182 PJ_DECL(pj_status_t) pj_sock_bind_random( pj_sock_t sockfd, 1183 const pj_sockaddr_t *addr, 1184 pj_uint16_t port_range, 1185 pj_uint16_t max_try); 1186 1169 1187 #if PJ_HAS_TCP 1170 1188 /** -
pjproject/trunk/pjlib/src/pj/sock_common.c
r4218 r4343 25 25 #include <pj/os.h> 26 26 #include <pj/addr_resolv.h> 27 #include <pj/rand.h> 27 28 #include <pj/string.h> 28 29 #include <pj/compat/socket.h> … … 1041 1042 1042 1043 1044 /* 1045 * Bind socket at random port. 1046 */ 1047 PJ_DEF(pj_status_t) pj_sock_bind_random( pj_sock_t sockfd, 1048 const pj_sockaddr_t *addr, 1049 pj_uint16_t port_range, 1050 pj_uint16_t max_try) 1051 { 1052 pj_sockaddr bind_addr; 1053 int addr_len; 1054 pj_uint16_t base_port; 1055 pj_status_t status = PJ_SUCCESS; 1056 1057 PJ_CHECK_STACK(); 1058 1059 PJ_ASSERT_RETURN(addr, PJ_EINVAL); 1060 1061 pj_sockaddr_cp(&bind_addr, addr); 1062 addr_len = pj_sockaddr_get_len(addr); 1063 base_port = pj_sockaddr_get_port(addr); 1064 1065 if (base_port == 0 || port_range == 0) { 1066 return pj_sock_bind(sockfd, &bind_addr, addr_len); 1067 } 1068 1069 for (; max_try; --max_try) { 1070 pj_uint16_t port; 1071 port = (pj_uint16_t)(base_port + pj_rand() % (port_range + 1)); 1072 pj_sockaddr_set_port(&bind_addr, port); 1073 status = pj_sock_bind(sockfd, &bind_addr, addr_len); 1074 if (status == PJ_SUCCESS) 1075 break; 1076 } 1077 1078 return status; 1079 } 1080 1081 1043 1082 /* Only need to implement these in DLL build */ 1044 1083 #if defined(PJ_DLL) -
pjproject/trunk/pjnath/include/pjnath/stun_sock.h
r4197 r4343 239 239 * is non-zero, socket will be bound to this address only, and the 240 240 * transport will have only one address alias (the \a alias_cnt field 241 * in #pj_stun_sock_info structure. 241 * in #pj_stun_sock_info structure. If the port is set to zero, the 242 * socket will bind at any port (chosen by the OS). 242 243 */ 243 244 pj_sockaddr bound_addr; 245 246 /** 247 * Specify the port range for STUN socket binding, relative to the start 248 * port number specified in \a bound_addr. Note that this setting is only 249 * applicable when the start port number is non zero. 250 * 251 * Default value is zero. 252 */ 253 pj_uint16_t port_range; 244 254 245 255 /** -
pjproject/trunk/pjnath/include/pjnath/turn_sock.h
r4197 r4343 141 141 */ 142 142 pj_bool_t qos_ignore_error; 143 144 /** 145 * Specify the interface where the socket should be bound to. If the 146 * address is zero, socket will be bound to INADDR_ANY. If the address 147 * is non-zero, socket will be bound to this address only. If the port is 148 * set to zero, the socket will bind at any port (chosen by the OS). 149 */ 150 pj_sockaddr bound_addr; 151 152 /** 153 * Specify the port range for TURN socket binding, relative to the start 154 * port number specified in \a bound_addr. Note that this setting is only 155 * applicable when the start port number is non zero. 156 * 157 * Default value is zero. 158 */ 159 pj_uint16_t port_range; 143 160 144 161 } pj_turn_sock_cfg; -
pjproject/trunk/pjnath/src/pjnath/stun_sock.c
r4324 r4343 33 33 34 34 35 enum { MAX_BIND_RETRY = 100 }; 36 35 37 struct pj_stun_sock 36 38 { … … 163 165 pj_stun_sock *stun_sock; 164 166 pj_stun_sock_cfg default_cfg; 167 pj_sockaddr bound_addr; 165 168 unsigned i; 169 pj_uint16_t max_bind_retry; 166 170 pj_status_t status; 167 171 … … 212 216 213 217 /* Bind socket */ 214 if (pj_sockaddr_has_addr(&cfg->bound_addr)) {215 status = pj_sock_bind(stun_sock->sock_fd, &cfg->bound_addr, 216 pj_sockaddr_get_len(&cfg->bound_addr));217 } else {218 pj_sockaddr bound_addr; 219 220 pj_sockaddr_init(af, &bound_addr, NULL, 0); 221 status = pj_sock_bind(stun_sock->sock_fd, &bound_addr,222 pj_sockaddr_get_len(&bound_addr)); 223 }224 218 max_bind_retry = MAX_BIND_RETRY; 219 if (cfg->port_range && cfg->port_range < max_bind_retry) 220 max_bind_retry = cfg->port_range; 221 pj_sockaddr_init(af, &bound_addr, NULL, 0); 222 if (cfg->bound_addr.addr.sa_family == pj_AF_INET() || 223 cfg->bound_addr.addr.sa_family == pj_AF_INET6()) 224 { 225 pj_sockaddr_cp(&bound_addr, &cfg->bound_addr); 226 } 227 status = pj_sock_bind_random(stun_sock->sock_fd, &bound_addr, 228 cfg->port_range, max_bind_retry); 225 229 if (status != PJ_SUCCESS) 226 230 goto on_error; -
pjproject/trunk/pjnath/src/pjnath/turn_sock.c
r4207 r4343 33 33 }; 34 34 35 36 enum { MAX_BIND_RETRY = 100 }; 37 38 35 39 #define INIT 0x1FFFFFFF 36 40 … … 103 107 } 104 108 109 105 110 /* 106 111 * Create. … … 726 731 pj_sock_t sock; 727 732 pj_activesock_cb asock_cb; 733 pj_sockaddr bound_addr, *cfg_bind_addr; 734 pj_uint16_t max_bind_retry; 728 735 729 736 /* Close existing connection, if any. This happens when … … 751 758 } 752 759 753 /* Apply QoS, if specified */ 760 /* Bind socket */ 761 cfg_bind_addr = &turn_sock->setting.bound_addr; 762 max_bind_retry = MAX_BIND_RETRY; 763 if (turn_sock->setting.port_range && 764 turn_sock->setting.port_range < max_bind_retry) 765 { 766 max_bind_retry = turn_sock->setting.port_range; 767 } 768 pj_sockaddr_init(turn_sock->af, &bound_addr, NULL, 0); 769 if (cfg_bind_addr->addr.sa_family == pj_AF_INET() || 770 cfg_bind_addr->addr.sa_family == pj_AF_INET6()) 771 { 772 pj_sockaddr_cp(&bound_addr, cfg_bind_addr); 773 } 774 status = pj_sock_bind_random(sock, &bound_addr, 775 turn_sock->setting.port_range, 776 max_bind_retry); 777 if (status != PJ_SUCCESS) { 778 pj_turn_sock_destroy(turn_sock); 779 return; 780 } 781 782 /* Apply QoS, if specified */ 754 783 status = pj_sock_apply_qos2(sock, turn_sock->setting.qos_type, 755 784 &turn_sock->setting.qos_params, -
pjproject/trunk/pjsip/include/pjsua-lib/pjsua.h
r4342 r4343 2154 2154 2155 2155 /** 2156 * Specify the port range for socket binding, relative to the start 2157 * port number specified in \a port. Note that this setting is only 2158 * applicable when the start port number is non zero. 2159 * 2160 * Default value is zero. 2161 */ 2162 unsigned port_range; 2163 2164 /** 2156 2165 * Optional address to advertise as the address of this transport. 2157 2166 * Application can specify any address or hostname for this field, -
pjproject/trunk/pjsip/src/pjsua-lib/pjsua_media.c
r4342 r4343 719 719 ice_cfg.stun.max_host_cands = acc_cfg->ice_cfg.ice_max_host_cands; 720 720 721 /* Copy binding port setting to STUN setting */ 722 pj_sockaddr_init(ice_cfg.af, &ice_cfg.stun.cfg.bound_addr, 723 &cfg->bound_addr, cfg->port); 724 ice_cfg.stun.cfg.port_range = cfg->port_range; 725 if (cfg->port != 0 && ice_cfg.stun.cfg.port_range == 0) 726 ice_cfg.stun.cfg.port_range = pjsua_var.ua_cfg.max_calls * 10; 727 721 728 /* Copy QoS setting to STUN setting */ 722 729 ice_cfg.stun.cfg.qos_type = cfg->qos_type; … … 744 751 pj_memcpy(&ice_cfg.turn.cfg.qos_params, &cfg->qos_params, 745 752 sizeof(cfg->qos_params)); 753 754 /* Copy binding port setting to TURN setting */ 755 pj_sockaddr_init(ice_cfg.af, &ice_cfg.turn.cfg.bound_addr, 756 &cfg->bound_addr, cfg->port); 757 ice_cfg.turn.cfg.port_range = cfg->port_range; 758 if (cfg->port != 0 && ice_cfg.turn.cfg.port_range == 0) 759 ice_cfg.turn.cfg.port_range = pjsua_var.ua_cfg.max_calls * 10; 746 760 } 747 761
Note: See TracChangeset
for help on using the changeset viewer.