/* $Id$ */ #include #include #include #include #include #include enum { MAX_REQUEST = 3 }; static int stun_timer[] = {1600, 1600, 1600 }; #define THIS_FILE "stunclient" #define LOG_ADDR(addr) pj_inet_ntoa(addr.sin_addr), pj_ntohs(addr.sin_port) PJ_DECL(pj_status_t) pj_stun_get_mapped_addr( pj_pool_factory *pf, int sock_cnt, pj_sock_t sock[], const pj_str_t *srv1, int port1, const pj_str_t *srv2, int port2, pj_sockaddr_in mapped_addr[]) { pj_sockaddr_in srv_addr[2]; int i, j, rc, send_cnt = 0; pj_pool_t *pool; struct { struct { pj_uint32_t mapped_addr; pj_uint32_t mapped_port; } srv[2]; } *rec; void *out_msg; pj_size_t out_msg_len; int wait_resp = 0; int mapped_status = 0; PJ_CHECK_STACK(); /* Create pool. */ pool = pj_pool_create(pf, "stun%p", 1024, 1024, NULL); if (!pool) { mapped_status = PJ_STUN_ERR_MEMORY; return -1; } /* Allocate client records */ rec = pj_pool_calloc(pool, sock_cnt, sizeof(*rec)); if (!rec) { mapped_status = PJ_STUN_ERR_MEMORY; goto on_error; } /* Create the outgoing BIND REQUEST message template */ rc = pj_stun_create_bind_req( pool, &out_msg, &out_msg_len, 0, 0); if (rc != 0) { mapped_status = -1; goto on_error; } /* Resolve servers. */ if (pj_sockaddr_in_init(&srv_addr[0], srv1, (pj_uint16_t)port1) != 0) { mapped_status = PJ_STUN_ERR_RESOLVE; goto on_error; } if (pj_sockaddr_in_init(&srv_addr[1], srv2, (pj_uint16_t)port2) != 0) { mapped_status = PJ_STUN_ERR_RESOLVE; goto on_error; } /* Init mapped addresses to zero */ pj_memset(mapped_addr, 0, sock_cnt * sizeof(pj_sockaddr_in)); /* Main retransmission loop. */ for (send_cnt=0; send_cnttsx[2] = pj_htonl(i); msg_hdr->tsx[3] = pj_htonl(j); /* Send! */ sent_len = out_msg_len; rc = pj_sock_sendto(sock[i], out_msg, &sent_len, 0, (pj_sockaddr_t*)&srv_addr[j], sizeof(pj_sockaddr_in)); if (sent_len != (int)out_msg_len) { PJ_LOG(4,(THIS_FILE, "Error sending STUN request to %s:%d", LOG_ADDR(srv_addr[j]))); mapped_status = PJ_STUN_ERR_TRANSPORT; } else { ++wait_resp; } } } /* All requests sent. * The loop below will wait for responses until all responses have * been received (i.e. wait_resp==0) or timeout occurs, which then * we'll go to the next retransmission iteration. */ /* Calculate time of next retransmission. */ pj_gettimeofday(&next_tx); next_tx.sec += (stun_timer[send_cnt]/1000); next_tx.msec += (stun_timer[send_cnt]%1000); pj_time_val_normalize(&next_tx); for (pj_gettimeofday(&now), select_rc=1; mapped_status==0 && select_rc==1 && wait_resp>0 && PJ_TIME_VAL_LT(now, next_tx); pj_gettimeofday(&now)) { pj_time_val timeout; timeout = next_tx; PJ_TIME_VAL_SUB(timeout, now); for (i=0; itsx[2]); srv_idx = pj_ntohl(msg.hdr->tsx[3]); if (sock_idx<0 || sock_idx>=sock_cnt || srv_idx<0 || srv_idx>=2) { PJ_LOG(4,(THIS_FILE, "Invalid transaction ID from %s:%d", LOG_ADDR(addr))); mapped_status = PJ_STUN_ERR_INVALID_MSG; continue; } if (pj_ntohs(msg.hdr->type) != PJ_STUN_BINDING_RESPONSE) { PJ_LOG(4,(THIS_FILE, "Non binding response %d from %s:%d", pj_ntohs(msg.hdr->type), LOG_ADDR(addr))); mapped_status = PJ_STUN_ERR_INVALID_MSG; continue; } if (pj_stun_msg_find_attr(&msg, PJ_STUN_ATTR_ERROR_CODE) != NULL) { PJ_LOG(4,(THIS_FILE, "Got STUN error attribute from %s:%d", LOG_ADDR(addr))); mapped_status = PJ_STUN_ERR_INVALID_MSG; continue; } attr = (void*)pj_stun_msg_find_attr(&msg, PJ_STUN_ATTR_MAPPED_ADDR); if (!attr) { PJ_LOG(4,(THIS_FILE, "No mapped address in response from %s:%d", LOG_ADDR(addr))); mapped_status = PJ_STUN_ERR_INVALID_MSG; continue; } rec[sock_idx].srv[srv_idx].mapped_addr = attr->addr; rec[sock_idx].srv[srv_idx].mapped_port = attr->port; } } /* The best scenario is if all requests have been replied. * Then we don't need to go to the next retransmission iteration. */ if (wait_resp <= 0) break; } for (i=0; i