本文整理汇总了C++中pj_sockaddr_get_len函数的典型用法代码示例。如果您正苦于以下问题:C++ pj_sockaddr_get_len函数的具体用法?C++ pj_sockaddr_get_len怎么用?C++ pj_sockaddr_get_len使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了pj_sockaddr_get_len函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: natclient_send_data
/*
* Send application data to remote agent.
*/
void natclient_send_data(struct ice_trans_s* icetrans, unsigned comp_id, const char *data)
{
pj_status_t status;
if (icetrans->icest == NULL) {
PJ_LOG(1,(THIS_FILE, "Error: No ICE instance, create it first"));
return;
}
if (!pj_ice_strans_has_sess(icetrans->icest)) {
PJ_LOG(1,(THIS_FILE, "Error: No ICE session, initialize first"));
return;
}
/*
if (!pj_ice_strans_sess_is_complete(icetrans->icest)) {
PJ_LOG(1,(THIS_FILE, "Error: ICE negotiation has not been started or is in progress"));
return;
}
*/
if (comp_id<1||comp_id>pj_ice_strans_get_running_comp_cnt(icetrans->icest)) {
PJ_LOG(1,(THIS_FILE, "Error: invalid component ID"));
return;
}
status = pj_ice_strans_sendto(icetrans->icest, comp_id, data, strlen(data),
&icetrans->rem.def_addr[comp_id-1],
pj_sockaddr_get_len(&icetrans->rem.def_addr[comp_id-1]));
if (status != PJ_SUCCESS)
natclient_perror("Error sending data", status);
else
PJ_LOG(3,(THIS_FILE, "Data sent"));
}
示例2: PJ_DEF
/*
* Bind socket at random port.
*/
PJ_DEF(pj_status_t) pj_sock_bind_random( pj_sock_t sockfd,
const pj_sockaddr_t *addr,
pj_uint16_t port_range,
pj_uint16_t max_try)
{
pj_sockaddr bind_addr;
int addr_len;
pj_uint16_t base_port;
pj_status_t status = PJ_SUCCESS;
PJ_CHECK_STACK();
PJ_ASSERT_RETURN(addr, PJ_EINVAL);
pj_sockaddr_cp(&bind_addr, addr);
addr_len = pj_sockaddr_get_len(addr);
base_port = pj_sockaddr_get_port(addr);
if (base_port == 0 || port_range == 0) {
return pj_sock_bind(sockfd, &bind_addr, addr_len);
}
for (; max_try; --max_try) {
pj_uint16_t port;
port = (pj_uint16_t)(base_port + pj_rand() % (port_range + 1));
pj_sockaddr_set_port(&bind_addr, port);
status = pj_sock_bind(sockfd, &bind_addr, addr_len);
if (status == PJ_SUCCESS)
break;
}
return status;
}
示例3: get_mapped_addr
/* Start sending STUN Binding request */
static pj_status_t get_mapped_addr(pj_stun_sock *stun_sock)
{
pj_stun_tx_data *tdata;
pj_status_t status;
/* Increment request counter and create STUN Binding request */
++stun_sock->tsx_id[5];
status = pj_stun_session_create_req(stun_sock->stun_sess,
PJ_STUN_BINDING_REQUEST,
PJ_STUN_MAGIC,
(const pj_uint8_t*)stun_sock->tsx_id,
&tdata);
if (status != PJ_SUCCESS)
goto on_error;
/* Send request */
status=pj_stun_session_send_msg(stun_sock->stun_sess, INTERNAL_MSG_TOKEN,
PJ_FALSE, PJ_TRUE, &stun_sock->srv_addr,
pj_sockaddr_get_len(&stun_sock->srv_addr),
tdata);
if (status != PJ_SUCCESS)
goto on_error;
return PJ_SUCCESS;
on_error:
sess_fail(stun_sock, PJ_STUN_SOCK_BINDING_OP, status);
return status;
}
示例4: PJ_DEF
/* Get the default IP interface */
PJ_DEF(pj_status_t) pj_getdefaultipinterface(int af, pj_sockaddr *addr)
{
pj_sock_t fd;
pj_str_t cp;
pj_sockaddr a;
int len;
pj_uint8_t zero[64];
pj_status_t status;
addr->addr.sa_family = (pj_uint16_t)af;
status = pj_sock_socket(af, pj_SOCK_DGRAM(), 0, &fd);
if (status != PJ_SUCCESS) {
return status;
}
PJ_LOG(4, ("sock_common.c", "pj_getdefaultipinterface() pj_sock_socket."));
if (af == PJ_AF_INET) {
cp = pj_str("1.1.1.1");
} else {
cp = pj_str("1::1");
}
status = pj_sockaddr_init(af, &a, &cp, 53);
if (status != PJ_SUCCESS) {
pj_sock_close(fd);
return status;
}
PJ_LOG(4, ("sock_common.c", "pj_getdefaultipinterface() pj_sockaddr_init."));
status = pj_sock_connect(fd, &a, pj_sockaddr_get_len(&a));
if (status != PJ_SUCCESS) {
pj_sock_close(fd);
return status;
}
PJ_LOG(4, ("sock_common.c", "pj_getdefaultipinterface() pj_sock_connect."));
len = sizeof(a);
status = pj_sock_getsockname(fd, &a, &len);
if (status != PJ_SUCCESS) {
pj_sock_close(fd);
return status;
}
PJ_LOG(4, ("sock_common.c", "pj_getdefaultipinterface() pj_sock_getsockname."));
pj_sock_close(fd);
/* Check that the address returned is not zero */
pj_bzero(zero, sizeof(zero));
if (pj_memcmp(pj_sockaddr_get_addr(&a), zero,
pj_sockaddr_get_addr_len(&a))==0)
{
PJ_LOG(4, ("sock_common.c", "pj_getdefaultipinterface() interface not found."));
return PJ_ENOTFOUND;
}
pj_sockaddr_copy_addr(addr, &a);
/* Success */
return PJ_SUCCESS;
}
示例5: send_allocate_response
/* Respond to ALLOCATE request */
static pj_status_t send_allocate_response(pj_turn_allocation *alloc,
pj_stun_session *srv_sess,
pj_turn_transport *transport,
const pj_stun_rx_data *rdata)
{
pj_stun_tx_data *tdata;
pj_status_t status;
/* Respond the original ALLOCATE request */
status = pj_stun_session_create_res(srv_sess, rdata, 0, NULL, &tdata);
if (status != PJ_SUCCESS)
return status;
/* Add XOR-RELAYED-ADDRESS attribute */
pj_stun_msg_add_sockaddr_attr(tdata->pool, tdata->msg,
PJ_STUN_ATTR_XOR_RELAYED_ADDR, PJ_TRUE,
&alloc->relay.hkey.addr,
pj_sockaddr_get_len(&alloc->relay.hkey.addr));
/* Add LIFETIME. */
pj_stun_msg_add_uint_attr(tdata->pool, tdata->msg,
PJ_STUN_ATTR_LIFETIME,
(unsigned)alloc->relay.lifetime);
/* Add BANDWIDTH */
pj_stun_msg_add_uint_attr(tdata->pool, tdata->msg,
PJ_STUN_ATTR_BANDWIDTH,
alloc->bandwidth);
/* Add RESERVATION-TOKEN */
PJ_TODO(ADD_RESERVATION_TOKEN);
/* Add XOR-MAPPED-ADDRESS */
pj_stun_msg_add_sockaddr_attr(tdata->pool, tdata->msg,
PJ_STUN_ATTR_XOR_MAPPED_ADDR, PJ_TRUE,
&alloc->hkey.clt_addr,
pj_sockaddr_get_len(&alloc->hkey.clt_addr));
/* Send the response */
return pj_stun_session_send_msg(srv_sess, transport, PJ_TRUE,
PJ_FALSE, &alloc->hkey.clt_addr,
pj_sockaddr_get_len(&alloc->hkey.clt_addr),
tdata);
}
示例6: srv_resolver_cb
/* Callback to be called by DNS SRV resolution */
static void srv_resolver_cb(void *user_data,
pj_status_t status,
const pj_dns_srv_record *rec)
{
struct query *query = (struct query*) user_data;
pjsip_server_addresses srv;
unsigned i;
if (status != PJ_SUCCESS) {
char errmsg[PJ_ERR_MSG_SIZE];
/* Log error */
pj_strerror(status, errmsg, sizeof(errmsg));
PJ_LOG(4,(query->objname, "DNS A/AAAA record resolution failed: %s",
errmsg));
/* Call the callback */
(*query->cb)(status, query->token, NULL);
return;
}
/* Build server addresses and call callback */
srv.count = 0;
for (i=0; i<rec->count; ++i) {
const pj_dns_addr_record *s = &rec->entry[i].server;
unsigned j;
for (j = 0; j < s->addr_count &&
srv.count < PJSIP_MAX_RESOLVED_ADDRESSES; ++j)
{
srv.entry[srv.count].type = query->naptr[0].type;
srv.entry[srv.count].priority = rec->entry[i].priority;
srv.entry[srv.count].weight = rec->entry[i].weight;
pj_sockaddr_init(s->addr[j].af,
&srv.entry[srv.count].addr,
0, (pj_uint16_t)rec->entry[i].port);
if (s->addr[j].af == pj_AF_INET6())
srv.entry[srv.count].addr.ipv6.sin6_addr = s->addr[j].ip.v6;
else
srv.entry[srv.count].addr.ipv4.sin_addr = s->addr[j].ip.v4;
srv.entry[srv.count].addr_len =
pj_sockaddr_get_len(&srv.entry[srv.count].addr);
/* Update transport type if this is IPv6 */
if (s->addr[j].af == pj_AF_INET6())
srv.entry[srv.count].type |= PJSIP_TRANSPORT_IPV6;
++srv.count;
}
}
/* Call the callback */
(*query->cb)(PJ_SUCCESS, query->token, &srv);
}
示例7: PJ_DEF
/* Set local peer address */
PJ_DEF(void) pj_tcp_session_set_local_addr( pj_tcp_session *sess,
const pj_sockaddr_t *addr)
{
char buf[PJ_INET6_ADDRSTRLEN+20];
if (!sess)
return;
if (!sess->local_addr) {
sess->local_addr = (pj_sockaddr_t *)
pj_pool_calloc(sess->pool, 1,
pj_sockaddr_get_len(addr));
}
sess->local_addr_len = pj_sockaddr_get_len(addr);
pj_sockaddr_cp(sess->local_addr, addr);
//pj_sockaddr_set_str_addr(pj_AF_INET(), (pj_sockaddr *)sess->peer_addr, &str_addr);
PJ_LOG(4, (THIS_FILE, "pj_tcp_session_set_remote_peer_addr() %s",
pj_sockaddr_print(sess->local_addr, buf, sizeof(buf), 3)));
}
示例8: create_success_response
static pj_stun_msg* create_success_response(test_server *test_srv,
turn_allocation *alloc,
pj_stun_msg *req,
pj_pool_t *pool,
unsigned lifetime,
pj_str_t *auth_key)
{
pj_stun_msg *resp;
pj_str_t tmp;
pj_status_t status;
/* Create response */
status = pj_stun_msg_create_response(pool, req, 0, NULL, &resp);
if (status != PJ_SUCCESS) {
return NULL;
}
/* Add TURN_NONCE */
pj_stun_msg_add_string_attr(pool, resp, PJ_STUN_ATTR_NONCE, pj_cstr(&tmp, TURN_NONCE));
/* Add LIFETIME */
pj_stun_msg_add_uint_attr(pool, resp, PJ_STUN_ATTR_LIFETIME, lifetime);
if (lifetime != 0) {
/* Add XOR-RELAYED-ADDRESS */
pj_stun_msg_add_sockaddr_attr(pool, resp, PJ_STUN_ATTR_XOR_RELAYED_ADDR, PJ_TRUE, &alloc->alloc_addr,
pj_sockaddr_get_len(&alloc->alloc_addr));
/* Add XOR-MAPPED-ADDRESS */
pj_stun_msg_add_sockaddr_attr(pool, resp, PJ_STUN_ATTR_XOR_MAPPED_ADDR, PJ_TRUE, &alloc->client_addr,
pj_sockaddr_get_len(&alloc->client_addr));
}
/* Add blank MESSAGE-INTEGRITY */
pj_stun_msg_add_msgint_attr(pool, resp);
/* Set auth key */
pj_stun_create_key(pool, auth_key, &test_srv->domain, &test_srv->username,
PJ_STUN_PASSWD_PLAIN, &test_srv->passwd);
return resp;
}
示例9: send_reply_err
/* Create and send error response */
static void send_reply_err(pj_turn_allocation *alloc,
const pj_stun_rx_data *rdata,
pj_bool_t cache,
int code, const char *errmsg)
{
pj_status_t status;
status = pj_stun_session_respond(alloc->sess, rdata, code, errmsg, NULL,
cache, &alloc->hkey.clt_addr,
pj_sockaddr_get_len(&alloc->hkey.clt_addr.addr));
if (status != PJ_SUCCESS) {
alloc_err(alloc, "Error sending STUN error response", status);
return;
}
}
示例10: PJ_DEF
PJ_DEF(pj_status_t) pj_activesock_create_udp( pj_pool_t *pool,
const pj_sockaddr *addr,
const pj_activesock_cfg *opt,
pj_ioqueue_t *ioqueue,
const pj_activesock_cb *cb,
void *user_data,
pj_activesock_t **p_asock,
pj_sockaddr *bound_addr)
{
pj_sock_t sock_fd;
pj_sockaddr default_addr;
pj_status_t status;
if (addr == NULL) {
pj_sockaddr_init(pj_AF_INET(), &default_addr, NULL, 0);
addr = &default_addr;
}
status = pj_sock_socket(addr->addr.sa_family, pj_SOCK_DGRAM(), 0,
&sock_fd);
if (status != PJ_SUCCESS) {
return status;
}
status = pj_sock_bind(sock_fd, addr, pj_sockaddr_get_len(addr));
if (status != PJ_SUCCESS) {
pj_sock_close(sock_fd);
return status;
}
status = pj_activesock_create(pool, sock_fd, pj_SOCK_DGRAM(), opt,
ioqueue, cb, user_data, p_asock);
if (status != PJ_SUCCESS) {
pj_sock_close(sock_fd);
return status;
}
if (bound_addr) {
int addr_len = sizeof(*bound_addr);
status = pj_sock_getsockname(sock_fd, bound_addr, &addr_len);
if (status != PJ_SUCCESS) {
pj_activesock_close(*p_asock);
return status;
}
}
return PJ_SUCCESS;
}
示例11: send_reply_ok
/* Create and send successful response */
static void send_reply_ok(pj_turn_allocation *alloc,
const pj_stun_rx_data *rdata)
{
pj_status_t status;
unsigned interval;
pj_stun_tx_data *tdata;
status = pj_stun_session_create_res(alloc->sess, rdata, 0, NULL, &tdata);
if (status != PJ_SUCCESS) {
alloc_err(alloc, "Error creating STUN success response", status);
return;
}
/* Calculate time to expiration */
if (alloc->relay.lifetime != 0) {
pj_time_val now;
pj_gettimeofday(&now);
interval = alloc->relay.expiry.sec - now.sec;
} else {
interval = 0;
}
/* Add LIFETIME if this is not ChannelBind. */
if (PJ_STUN_GET_METHOD(tdata->msg->hdr.type)!=PJ_STUN_CHANNEL_BIND_METHOD){
pj_stun_msg_add_uint_attr(tdata->pool, tdata->msg,
PJ_STUN_ATTR_LIFETIME, interval);
/* Add BANDWIDTH if lifetime is not zero */
if (interval != 0) {
pj_stun_msg_add_uint_attr(tdata->pool, tdata->msg,
PJ_STUN_ATTR_BANDWIDTH,
alloc->bandwidth);
}
}
status = pj_stun_session_send_msg(alloc->sess, NULL, PJ_TRUE,
PJ_FALSE, &alloc->hkey.clt_addr,
pj_sockaddr_get_len(&alloc->hkey.clt_addr),
tdata);
if (status != PJ_SUCCESS) {
alloc_err(alloc, "Error sending STUN success response", status);
return;
}
}
示例12: send_refresh
/*
* Send REFRESH
*/
static void send_refresh(pj_tcp_session *sess, int lifetime)
{
pj_stun_tx_data *tdata;
pj_status_t status;
PJ_ASSERT_ON_FAIL(sess->state==PJ_TCP_STATE_READY, return);
/* Create a bare REFRESH request */
status = pj_stun_session_create_req(sess->stun, PJ_STUN_REFRESH_REQUEST,
PJ_STUN_MAGIC, NULL, &tdata);
if (status != PJ_SUCCESS)
goto on_error;
/* Add LIFETIME */
if (lifetime >= 0) {
pj_stun_msg_add_uint_attr(tdata->pool, tdata->msg,
PJ_STUN_ATTR_LIFETIME, lifetime);
}
#if TCP_TODO
/* Send request */
if (lifetime == 0) {
pj_tcp_session_set_state(sess, PJ_TCP_STATE_DISCONNECTING);
}
status = pj_stun_session_send_msg(sess->stun, NULL, PJ_FALSE,
(sess->conn_type==PJ_TCP_TP_UDP),
sess->srv_addr,
pj_sockaddr_get_len(sess->srv_addr),
tdata);
if (status != PJ_SUCCESS)
goto on_error;
#endif
return;
on_error:
if (lifetime == 0) {
pj_tcp_session_set_state(sess, PJ_TCP_STATE_DISCONNECTED);
sess_shutdown(sess, status);
}
}
示例13: get_local_interface
static pj_status_t get_local_interface(const pj_sockaddr *server,
pj_sockaddr *local_addr)
{
pj_sock_t sock;
pj_sockaddr tmp, local;
int addr_len;
pj_status_t status;
status = pj_sock_socket(server->addr.sa_family, pj_SOCK_DGRAM(),
0, &sock);
if (status != PJ_SUCCESS)
return status;
addr_len = pj_sockaddr_get_len(server);
pj_sockaddr_init(server->addr.sa_family, &local, NULL, 0);
status = pj_sock_bind(sock, &local, addr_len);
if (status != PJ_SUCCESS) {
pj_sock_close(sock);
return status;
}
status = pj_sock_connect(sock, server, addr_len);
if (status != PJ_SUCCESS) {
pj_sock_close(sock);
return status;
}
status = pj_sock_getsockname(sock, &tmp, &addr_len);
if (status != PJ_SUCCESS) {
pj_sock_close(sock);
return status;
}
pj_sockaddr_cp(local_addr, &tmp);
pj_sock_close(sock);
return PJ_SUCCESS;
}
示例14: stun_on_rx_indication
/*
* Callback notification from STUN session when it receives STUN
* indications. This callback was trigger by STUN incoming message
* processing in pj_turn_allocation_on_rx_client_pkt().
*/
static pj_status_t stun_on_rx_indication(pj_stun_session *sess,
const pj_uint8_t *pkt,
unsigned pkt_len,
const pj_stun_msg *msg,
void *token,
const pj_sockaddr_t *src_addr,
unsigned src_addr_len)
{
pj_stun_xor_peer_addr_attr *peer_attr;
pj_stun_data_attr *data_attr;
pj_turn_allocation *alloc;
pj_turn_permission *perm;
pj_ssize_t len;
PJ_UNUSED_ARG(pkt);
PJ_UNUSED_ARG(pkt_len);
PJ_UNUSED_ARG(token);
PJ_UNUSED_ARG(src_addr);
PJ_UNUSED_ARG(src_addr_len);
alloc = (pj_turn_allocation*) pj_stun_session_get_user_data(sess);
/* Only expect Send Indication */
if (msg->hdr.type != PJ_STUN_SEND_INDICATION) {
/* Ignore */
return PJ_SUCCESS;
}
/* Get XOR-PEER-ADDRESS attribute */
peer_attr = (pj_stun_xor_peer_addr_attr*)
pj_stun_msg_find_attr(msg, PJ_STUN_ATTR_XOR_PEER_ADDR, 0);
/* MUST have XOR-PEER-ADDRESS attribute */
if (!peer_attr)
return PJ_SUCCESS;
/* Get DATA attribute */
data_attr = (pj_stun_data_attr*)
pj_stun_msg_find_attr(msg, PJ_STUN_ATTR_DATA, 0);
/* Create/update/refresh the permission */
perm = lookup_permission_by_addr(alloc, &peer_attr->sockaddr,
pj_sockaddr_get_len(&peer_attr->sockaddr));
if (perm == NULL) {
perm = create_permission(alloc, &peer_attr->sockaddr,
pj_sockaddr_get_len(&peer_attr->sockaddr));
}
refresh_permission(perm);
/* Return if we don't have data */
if (data_attr == NULL)
return PJ_SUCCESS;
/* Relay the data to peer */
len = data_attr->length;
pj_sock_sendto(alloc->relay.tp.sock, data_attr->data,
&len, 0, &peer_attr->sockaddr,
pj_sockaddr_get_len(&peer_attr->sockaddr));
return PJ_SUCCESS;
}
示例15: stun_on_rx_request
//.........这里部分代码省略.........
resched_timeout(alloc);
/* Send reply */
send_reply_ok(alloc, rdata);
}
} else if (msg->hdr.type == PJ_STUN_CHANNEL_BIND_REQUEST) {
/*
* ChannelBind request.
*/
pj_stun_channel_number_attr *ch_attr;
pj_stun_xor_peer_addr_attr *peer_attr;
pj_turn_permission *p1, *p2;
ch_attr = (pj_stun_channel_number_attr*)
pj_stun_msg_find_attr(msg, PJ_STUN_ATTR_CHANNEL_NUMBER, 0);
peer_attr = (pj_stun_xor_peer_addr_attr*)
pj_stun_msg_find_attr(msg, PJ_STUN_ATTR_XOR_PEER_ADDR, 0);
if (!ch_attr || !peer_attr) {
send_reply_err(alloc, rdata, PJ_TRUE,
PJ_STUN_SC_BAD_REQUEST, NULL);
return PJ_SUCCESS;
}
/* Find permission with the channel number */
p1 = lookup_permission_by_chnum(alloc, PJ_STUN_GET_CH_NB(ch_attr->value));
/* If permission is found, this is supposed to be a channel bind
* refresh. Make sure it's for the same peer.
*/
if (p1) {
if (pj_sockaddr_cmp(&p1->hkey.peer_addr, &peer_attr->sockaddr)) {
/* Address mismatch. Send 400 */
send_reply_err(alloc, rdata, PJ_TRUE,
PJ_STUN_SC_BAD_REQUEST,
"Peer address mismatch");
return PJ_SUCCESS;
}
/* Refresh permission */
refresh_permission(p1);
/* Send response */
send_reply_ok(alloc, rdata);
/* Done */
return PJ_SUCCESS;
}
/* If permission is not found, create a new one. Make sure the peer
* has not alreadyy assigned with a channel number.
*/
p2 = lookup_permission_by_addr(alloc, &peer_attr->sockaddr,
pj_sockaddr_get_len(&peer_attr->sockaddr));
if (p2 && p2->channel != PJ_TURN_INVALID_CHANNEL) {
send_reply_err(alloc, rdata, PJ_TRUE, PJ_STUN_SC_BAD_REQUEST,
"Peer address already assigned a channel number");
return PJ_SUCCESS;
}
/* Create permission if it doesn't exist */
if (!p2) {
p2 = create_permission(alloc, &peer_attr->sockaddr,
pj_sockaddr_get_len(&peer_attr->sockaddr));
if (!p2)
return PJ_SUCCESS;
}
/* Assign channel number to permission */
p2->channel = PJ_STUN_GET_CH_NB(ch_attr->value);
/* Register to hash table */
pj_assert(sizeof(p2->channel)==2);
pj_hash_set(alloc->pool, alloc->ch_table, &p2->channel,
sizeof(p2->channel), 0, p2);
/* Update */
refresh_permission(p2);
/* Reply */
send_reply_ok(alloc, rdata);
return PJ_SUCCESS;
} else if (msg->hdr.type == PJ_STUN_ALLOCATE_REQUEST) {
/* Respond with 437 (section 6.3 turn-07) */
send_reply_err(alloc, rdata, PJ_TRUE, PJ_STUN_SC_ALLOCATION_MISMATCH,
NULL);
} else {
/* Respond with Bad Request? */
send_reply_err(alloc, rdata, PJ_TRUE, PJ_STUN_SC_BAD_REQUEST, NULL);
}
return PJ_SUCCESS;
}