Ejemplo n.º 1
0
bool StunClient::allocate(char *realm, char *nonce)
{
    stun_buffer alloc_buff;
    stun_set_allocate_request_str(alloc_buff.buf, &alloc_buff.len, 60 * 5, 
                                  STUN_ATTRIBUTE_REQUESTED_ADDRESS_FAMILY_VALUE_IPV4,
                                  STUN_ATTRIBUTE_TRANSPORT_UDP_VALUE,
                                  STUN_ATTRIBUTE_MOBILITY_EVENT);

    stun_attr_add_str(alloc_buff.buf, &alloc_buff.len, STUN_ATTRIBUTE_USERNAME, (u08bits*)STUN_USERNAME, strlen(STUN_USERNAME));
    // stun_attr_add_str(alloc_buff.buf, &alloc_buff.len, OLD_STUN_ATTRIBUTE_PASSWORD, (u08bits*)STUN_PASSWORD, strlen(STUN_PASSWORD));

    if (realm != NULL && nonce != NULL) {
        m_realm = QByteArray(realm);
        m_nonce = QByteArray(nonce);
    }

    if (!m_realm.isEmpty() && !m_nonce.isEmpty()) {
        stun_attr_add(&alloc_buff, STUN_ATTRIBUTE_REALM, m_realm.data(), m_realm.length());
        stun_attr_add(&alloc_buff, STUN_ATTRIBUTE_NONCE, m_nonce.data(), m_nonce.length());
        stun_attr_add_integrity_by_user_str(alloc_buff.buf, &alloc_buff.len,
                                            (u08bits*)STUN_USERNAME, (u08bits*)m_realm.data(), (u08bits*)STUN_PASSWORD,
                                            (u08bits*)m_nonce.data(), SHATYPE_SHA1);
    }

    QByteArray data = QByteArray((char*)alloc_buff.buf, alloc_buff.len);
    qint64 ret = m_stun_sock->writeDatagram(data, QHostAddress(STUN_SERVER_ADDR), STUN_SERVER_PORT);
    // m_stun_sock->waitForBytesWritten();

    {
        m_sending_udp = true;
        m_sending_data = QByteArray((char*)alloc_buff.buf, alloc_buff.len);
        m_sending_addr = QString("%1:%2").arg(STUN_SERVER_ADDR).arg(STUN_SERVER_PORT);
        m_sending_timer->start();
    }

    qDebug()<<ret;

    return true;
}
Ejemplo n.º 2
0
int stun_attr_add(stun_buffer* buf, u16bits attr, const s08bits* avalue, int alen) {
  return stun_attr_add_str(buf->buf, (size_t*)(&(buf->len)), attr, (const u08bits *)avalue, alen);
}