| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
#include "test.h" |
| 21 |
#include <pjsip.h> |
| 22 |
#include <pjlib.h> |
| 23 |
|
| 24 |
#define THIS_FILE "transport_tcp_test.c" |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
#if PJ_HAS_TCP |
| 31 |
|
| 32 |
static pj_status_t multi_listener_test(pjsip_tpfactory *factory[], |
| 33 |
unsigned num_factory, |
| 34 |
pjsip_transport *tp[], |
| 35 |
unsigned *num_tp) |
| 36 |
{ |
| 37 |
pj_status_t status; |
| 38 |
unsigned i = 0; |
| 39 |
pj_str_t s; |
| 40 |
pjsip_transport *tcp; |
| 41 |
pjsip_tpfactory *tpfactory = NULL; |
| 42 |
pj_sockaddr_in rem_addr; |
| 43 |
pjsip_tpselector tp_sel; |
| 44 |
unsigned ntp = 0; |
| 45 |
|
| 46 |
for (;i<num_factory;++i) |
| 47 |
{ |
| 48 |
|
| 49 |
status = pjsip_tcp_transport_start(endpt, NULL, 1, &tpfactory); |
| 50 |
if (status != PJ_SUCCESS) { |
| 51 |
app_perror(" Error: unable to start TCP transport", status); |
| 52 |
return -10; |
| 53 |
} |
| 54 |
|
| 55 |
factory[i] = tpfactory; |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
status = pj_sockaddr_in_init(&rem_addr, &tpfactory->addr_name.host, |
| 60 |
(pj_uint16_t)tpfactory->addr_name.port); |
| 61 |
if (status != PJ_SUCCESS) { |
| 62 |
app_perror(" Error: possibly invalid TCP address name", status); |
| 63 |
return -11; |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
status = pjsip_endpt_acquire_transport(endpt, PJSIP_TRANSPORT_TCP, |
| 68 |
&rem_addr, sizeof(rem_addr), |
| 69 |
NULL, &tcp); |
| 70 |
if (status != PJ_SUCCESS || tcp == NULL) { |
| 71 |
app_perror(" Error: unable to acquire TCP transport", status); |
| 72 |
return -12; |
| 73 |
} |
| 74 |
tp[ntp++] = tcp; |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
if (pj_atomic_get(tcp->ref_cnt) != 1) |
| 80 |
return -13; |
| 81 |
|
| 82 |
|
| 83 |
status = pjsip_endpt_acquire_transport(endpt, PJSIP_TRANSPORT_TCP, |
| 84 |
&rem_addr, sizeof(rem_addr), |
| 85 |
NULL, &tcp); |
| 86 |
if (status != PJ_SUCCESS || tcp == NULL) { |
| 87 |
app_perror(" Error: unable to acquire TCP transport", status); |
| 88 |
return -14; |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
if (tp[ntp-1] != tcp) { |
| 93 |
return -15; |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
if (pj_atomic_get(tcp->ref_cnt) != 2) |
| 99 |
return -16; |
| 100 |
|
| 101 |
|
| 102 |
pjsip_transport_dec_ref(tcp); |
| 103 |
|
| 104 |
|
| 105 |
status = generic_transport_test(tcp); |
| 106 |
if (status != PJ_SUCCESS) |
| 107 |
return status; |
| 108 |
|
| 109 |
|
| 110 |
if (pj_atomic_get(tcp->ref_cnt) != 1) |
| 111 |
return -17; |
| 112 |
|
| 113 |
|
| 114 |
pj_bzero(&tp_sel, sizeof(tp_sel)); |
| 115 |
tp_sel.type = PJSIP_TPSELECTOR_LISTENER; |
| 116 |
tp_sel.u.listener = factory[num_factory/2]; |
| 117 |
pj_sockaddr_in_init(&rem_addr, pj_cstr(&s, "1.1.1.1"), 80); |
| 118 |
status = pjsip_endpt_acquire_transport(endpt, PJSIP_TRANSPORT_TCP, |
| 119 |
&rem_addr, sizeof(rem_addr), |
| 120 |
&tp_sel, &tcp); |
| 121 |
if (status != PJ_SUCCESS) { |
| 122 |
app_perror(" Error: unable to acquire TCP transport", status); |
| 123 |
return -18; |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
if (tcp->factory != factory[num_factory/2]) |
| 128 |
return -19; |
| 129 |
|
| 130 |
|
| 131 |
for (i = 0; i < ntp; ++i) { |
| 132 |
if (tp[i] == tcp) { |
| 133 |
break; |
| 134 |
} |
| 135 |
} |
| 136 |
if (i != ntp) |
| 137 |
return -20; |
| 138 |
|
| 139 |
tp[ntp++] = tcp; |
| 140 |
|
| 141 |
for (i = 0; i<ntp; ++i) { |
| 142 |
if (pj_atomic_get(tp[i]->ref_cnt) != 1) |
| 143 |
return -21; |
| 144 |
} |
| 145 |
*num_tp = ntp; |
| 146 |
|
| 147 |
return PJ_SUCCESS; |
| 148 |
} |
| 149 |
|
| 150 |
int transport_tcp_test(void) |
| 151 |
{ |
| 152 |
enum { SEND_RECV_LOOP = 8 }; |
| 153 |
enum { NUM_LISTENER = 4 }; |
| 154 |
enum { NUM_TP = 8 }; |
| 155 |
pjsip_tpfactory *tpfactory[NUM_LISTENER]; |
| 156 |
pjsip_transport *tcp[NUM_TP]; |
| 157 |
pj_sockaddr_in rem_addr; |
| 158 |
pj_status_t status; |
| 159 |
char host_port_param[PJSIP_MAX_URL_SIZE]; |
| 160 |
char addr[PJ_INET_ADDRSTRLEN]; |
| 161 |
int rtt[SEND_RECV_LOOP], min_rtt; |
| 162 |
int pkt_lost; |
| 163 |
unsigned i; |
| 164 |
unsigned num_listener = NUM_LISTENER; |
| 165 |
unsigned num_tp = NUM_TP; |
| 166 |
|
| 167 |
status = multi_listener_test(tpfactory, num_listener, tcp, &num_tp); |
| 168 |
if (status != PJ_SUCCESS) |
| 169 |
return status; |
| 170 |
|
| 171 |
|
| 172 |
status = pj_sockaddr_in_init(&rem_addr, &tpfactory[0]->addr_name.host, |
| 173 |
(pj_uint16_t)tpfactory[0]->addr_name.port); |
| 174 |
|
| 175 |
pj_ansi_snprintf(host_port_param, sizeof(host_port_param), |
| 176 |
"%s:%d;transport=tcp", |
| 177 |
pj_inet_ntop2(pj_AF_INET(), &rem_addr.sin_addr, addr, |
| 178 |
sizeof(addr)), |
| 179 |
pj_ntohs(rem_addr.sin_port)); |
| 180 |
|
| 181 |
|
| 182 |
for (i=0; i<SEND_RECV_LOOP; ++i) { |
| 183 |
status = transport_send_recv_test(PJSIP_TRANSPORT_TCP, tcp[0], |
| 184 |
host_port_param, &rtt[i]); |
| 185 |
|
| 186 |
if (status != 0) { |
| 187 |
for (i = 0; i < num_tp ; ++i) { |
| 188 |
pjsip_transport_dec_ref(tcp[i]); |
| 189 |
} |
| 190 |
flush_events(500); |
| 191 |
return -72; |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
min_rtt = 0xFFFFFFF; |
| 196 |
for (i=0; i<SEND_RECV_LOOP; ++i) |
| 197 |
if (rtt[i] < min_rtt) min_rtt = rtt[i]; |
| 198 |
|
| 199 |
report_ival("tcp-rtt-usec", min_rtt, "usec", |
| 200 |
"Best TCP transport round trip time, in microseconds " |
| 201 |
"(time from sending request until response is received. " |
| 202 |
"Tests were performed on local machine only, and after " |
| 203 |
"TCP socket has been established by previous test)"); |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
status = transport_rt_test(PJSIP_TRANSPORT_TCP, tcp[0], host_port_param, |
| 208 |
&pkt_lost); |
| 209 |
if (status != 0) { |
| 210 |
for (i = 0; i < num_tp ; ++i) { |
| 211 |
pjsip_transport_dec_ref(tcp[i]); |
| 212 |
} |
| 213 |
return status; |
| 214 |
} |
| 215 |
|
| 216 |
if (pkt_lost != 0) |
| 217 |
PJ_LOG(3,(THIS_FILE, " note: %d packet(s) was lost", pkt_lost)); |
| 218 |
|
| 219 |
|
| 220 |
if ((status=transport_load_test(PJSIP_TRANSPORT_TCP, |
| 221 |
host_port_param)) != 0) |
| 222 |
{ |
| 223 |
return status; |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
for (i = 0; i < num_tp; ++i) { |
| 228 |
if (pj_atomic_get(tcp[i]->ref_cnt) != 1) |
| 229 |
return -80; |
| 230 |
} |
| 231 |
|
| 232 |
for (i = 0; i < num_tp; ++i) { |
| 233 |
|
| 234 |
pjsip_transport_dec_ref(tcp[i]); |
| 235 |
|
| 236 |
|
| 237 |
status = pjsip_transport_destroy(tcp[i]); |
| 238 |
if (status != PJ_SUCCESS) |
| 239 |
return -90; |
| 240 |
} |
| 241 |
|
| 242 |
for (i = 0; i < num_listener; ++i) { |
| 243 |
|
| 244 |
status = pjsip_tpmgr_unregister_tpfactory(pjsip_endpt_get_tpmgr(endpt), |
| 245 |
tpfactory[i]); |
| 246 |
if (status != PJ_SUCCESS) |
| 247 |
return -95; |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
PJ_LOG(3,(THIS_FILE, " Flushing events, 1 second...")); |
| 252 |
flush_events(1000); |
| 253 |
|
| 254 |
|
| 255 |
return 0; |
| 256 |
} |
| 257 |
#else |
| 258 |
int transport_tcp_test(void) |
| 259 |
{ |
| 260 |
return 0; |
| 261 |
} |
| 262 |
#endif |