Changeset 4343 for pjproject/trunk/pjlib/src/pj/sock_common.c
- Timestamp:
- Feb 7, 2013 9:35:34 AM (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
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)
Note: See TracChangeset
for help on using the changeset viewer.