/* Flush all delayed transmision once the socket is connected. */ static void tls_flush_pending_tx(struct tls_transport *tls) { pj_lock_acquire(tls->base.lock); while (!pj_list_empty(&tls->delayed_list)) { struct delayed_tdata *pending_tx; pjsip_tx_data *tdata; pj_ioqueue_op_key_t *op_key; pj_ssize_t size; pj_status_t status; pending_tx = tls->delayed_list.next; pj_list_erase(pending_tx); tdata = pending_tx->tdata_op_key->tdata; op_key = (pj_ioqueue_op_key_t*)pending_tx->tdata_op_key; /* send! */ size = tdata->buf.cur - tdata->buf.start; status = pj_ssl_sock_send(tls->ssock, op_key, tdata->buf.start, &size, 0); if (status != PJ_EPENDING) { on_data_sent(tls->ssock, op_key, size); } } pj_lock_release(tls->base.lock); }
static pj_bool_t ssl_on_data_sent(pj_ssl_sock_t *ssock, pj_ioqueue_op_key_t *op_key, pj_ssize_t sent) { struct test_state *st = (struct test_state*) pj_ssl_sock_get_user_data(ssock); PJ_UNUSED_ARG(op_key); if (sent < 0) { st->err = -sent; } else { st->sent += sent; /* Send more if any */ while (st->sent < st->send_str_len) { pj_ssize_t size; pj_status_t status; size = st->send_str_len - st->sent; status = pj_ssl_sock_send(ssock, (pj_ioqueue_op_key_t*)&st->send_key, st->send_str + st->sent, &size, 0); if (status != PJ_SUCCESS && status != PJ_EPENDING) { app_perror("...ERROR pj_ssl_sock_send()", status); st->err = status; break; } if (status == PJ_SUCCESS) st->sent += size; else break; } } if (st->err != PJ_SUCCESS) { pj_ssl_sock_close(ssock); if (!st->is_server) clients_num--; return PJ_FALSE; } return PJ_TRUE; }
/* Flush all delayed transmision once the socket is connected. */ static void tls_flush_pending_tx(struct tls_transport *tls) { pj_time_val now; pj_gettickcount(&now); pj_lock_acquire(tls->base.lock); while (!pj_list_empty(&tls->delayed_list)) { struct delayed_tdata *pending_tx; pjsip_tx_data *tdata; pj_ioqueue_op_key_t *op_key; pj_ssize_t size; pj_status_t status; pending_tx = tls->delayed_list.next; pj_list_erase(pending_tx); tdata = pending_tx->tdata_op_key->tdata; op_key = (pj_ioqueue_op_key_t*)pending_tx->tdata_op_key; if (pending_tx->timeout.sec > 0 && PJ_TIME_VAL_GT(now, pending_tx->timeout)) { continue; } /* send! */ size = tdata->buf.cur - tdata->buf.start; status = pj_ssl_sock_send(tls->ssock, op_key, tdata->buf.start, &size, 0); if (status != PJ_EPENDING) { pj_lock_release(tls->base.lock); on_data_sent(tls->ssock, op_key, size); pj_lock_acquire(tls->base.lock); } } pj_lock_release(tls->base.lock); }
static pj_bool_t ssl_on_data_read(pj_ssl_sock_t *ssock, void *data, pj_size_t size, pj_status_t status, pj_size_t *remainder) { struct test_state *st = (struct test_state*) pj_ssl_sock_get_user_data(ssock); PJ_UNUSED_ARG(remainder); PJ_UNUSED_ARG(data); if (size > 0) { pj_size_t consumed; /* Set random remainder */ *remainder = pj_rand() % 100; /* Apply zero remainder if: * - remainder is less than size, or * - connection closed/error * - echo/check_eco set */ if (*remainder > size || status != PJ_SUCCESS || st->echo || st->check_echo) *remainder = 0; consumed = size - *remainder; st->recv += consumed; //printf("%.*s", consumed, (char*)data); pj_memmove(data, (char*)data + consumed, *remainder); /* Echo data when specified to */ if (st->echo) { pj_ssize_t size_ = consumed; status = pj_ssl_sock_send(ssock, (pj_ioqueue_op_key_t*)&st->send_key, data, &size_, 0); if (status != PJ_SUCCESS && status != PJ_EPENDING) { app_perror("...ERROR pj_ssl_sock_send()", status); goto on_return; } if (status == PJ_SUCCESS) st->sent += size_; } /* Verify echoed data when specified to */ if (st->check_echo) { if (!st->check_echo_ptr) st->check_echo_ptr = st->send_str; if (pj_memcmp(st->check_echo_ptr, data, consumed)) { status = PJ_EINVAL; app_perror("...ERROR echoed data not exact", status); goto on_return; } st->check_echo_ptr += consumed; /* Echo received completely */ if (st->send_str_len == st->recv) { pj_ssl_sock_info info; char buf[64]; status = pj_ssl_sock_get_info(ssock, &info); if (status != PJ_SUCCESS) { app_perror("...ERROR pj_ssl_sock_get_info()", status); goto on_return; } pj_sockaddr_print((pj_sockaddr_t*)&info.local_addr, buf, sizeof(buf), 1); PJ_LOG(3, ("", "...%s successfully recv %d bytes echo", buf, st->recv)); st->done = PJ_TRUE; } } } if (status != PJ_SUCCESS) { if (status == PJ_EEOF) { status = PJ_SUCCESS; st->done = PJ_TRUE; } else { app_perror("...ERROR ssl_on_data_read()", status); } } on_return: st->err = status; if (st->err != PJ_SUCCESS || st->done) { pj_ssl_sock_close(ssock); if (!st->is_server) clients_num--; return PJ_FALSE; } return PJ_TRUE; }
static pj_bool_t ssl_on_accept_complete(pj_ssl_sock_t *ssock, pj_ssl_sock_t *newsock, const pj_sockaddr_t *src_addr, int src_addr_len) { struct test_state *parent_st = (struct test_state*) pj_ssl_sock_get_user_data(ssock); struct test_state *st; void *read_buf[1]; pj_ssl_sock_info info; char buf[64]; pj_status_t status; PJ_UNUSED_ARG(src_addr_len); /* Duplicate parent test state to newly accepted test state */ st = pj_pool_zalloc(parent_st->pool, sizeof(struct test_state)); *st = *parent_st; pj_ssl_sock_set_user_data(newsock, st); status = pj_ssl_sock_get_info(newsock, &info); if (status != PJ_SUCCESS) { app_perror("...ERROR pj_ssl_sock_get_info()", status); goto on_return; } pj_sockaddr_print(src_addr, buf, sizeof(buf), 1); PJ_LOG(3, ("", "...Accepted connection from %s", buf)); if (st->is_verbose) dump_ssl_info(&info); /* Start reading data */ read_buf[0] = st->read_buf; status = pj_ssl_sock_start_read2(newsock, st->pool, sizeof(st->read_buf), (void**)read_buf, 0); if (status != PJ_SUCCESS) { app_perror("...ERROR pj_ssl_sock_start_read2()", status); goto on_return; } /* Start sending data */ while (st->sent < st->send_str_len) { pj_ssize_t size; size = st->send_str_len - st->sent; status = pj_ssl_sock_send(newsock, (pj_ioqueue_op_key_t*)&st->send_key, st->send_str + st->sent, &size, 0); if (status != PJ_SUCCESS && status != PJ_EPENDING) { app_perror("...ERROR pj_ssl_sock_send()", status); goto on_return; } if (status == PJ_SUCCESS) st->sent += size; else break; } on_return: st->err = status; if (st->err != PJ_SUCCESS) { pj_ssl_sock_close(newsock); return PJ_FALSE; } return PJ_TRUE; }
static pj_bool_t ssl_on_connect_complete(pj_ssl_sock_t *ssock, pj_status_t status) { struct test_state *st = (struct test_state*) pj_ssl_sock_get_user_data(ssock); void *read_buf[1]; pj_ssl_sock_info info; char buf1[64], buf2[64]; if (status != PJ_SUCCESS) { app_perror("...ERROR ssl_on_connect_complete()", status); goto on_return; } status = pj_ssl_sock_get_info(ssock, &info); if (status != PJ_SUCCESS) { app_perror("...ERROR pj_ssl_sock_get_info()", status); goto on_return; } pj_sockaddr_print((pj_sockaddr_t*)&info.local_addr, buf1, sizeof(buf1), 1); pj_sockaddr_print((pj_sockaddr_t*)&info.remote_addr, buf2, sizeof(buf2), 1); PJ_LOG(3, ("", "...Connected %s -> %s!", buf1, buf2)); if (st->is_verbose) dump_ssl_info(&info); /* Start reading data */ read_buf[0] = st->read_buf; status = pj_ssl_sock_start_read2(ssock, st->pool, sizeof(st->read_buf), (void**)read_buf, 0); if (status != PJ_SUCCESS) { app_perror("...ERROR pj_ssl_sock_start_read2()", status); goto on_return; } /* Start sending data */ while (st->sent < st->send_str_len) { pj_ssize_t size; size = st->send_str_len - st->sent; status = pj_ssl_sock_send(ssock, (pj_ioqueue_op_key_t*)&st->send_key, st->send_str + st->sent, &size, 0); if (status != PJ_SUCCESS && status != PJ_EPENDING) { app_perror("...ERROR pj_ssl_sock_send()", status); goto on_return; } if (status == PJ_SUCCESS) st->sent += size; else break; } on_return: st->err = status; if (st->err != PJ_SUCCESS) { pj_ssl_sock_close(ssock); clients_num--; return PJ_FALSE; } return PJ_TRUE; }