mirror of
https://github.com/RetroShare/RetroShare.git
synced 2025-02-05 09:35:39 -05:00
udpstunner: don't open code header fields, reference and follow (old) RFC
This commit is contained in:
parent
4095d82a67
commit
e91d877af2
@ -47,6 +47,12 @@ const uint32_t TOU_STUN_MAX_RECV_RATE = 25; /* every 25 seconds */
|
|||||||
const int32_t TOU_STUN_DEFAULT_TARGET_RATE = 15; /* 20 secs is minimum to keep a NAT UDP port open */
|
const int32_t TOU_STUN_DEFAULT_TARGET_RATE = 15; /* 20 secs is minimum to keep a NAT UDP port open */
|
||||||
const double TOU_SUCCESS_LPF_FACTOR = 0.90;
|
const double TOU_SUCCESS_LPF_FACTOR = 0.90;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* based on RFC 3489
|
||||||
|
*/
|
||||||
|
const uint16_t STUN_BINDING_REQUEST = 0x0001;
|
||||||
|
const uint16_t STUN_BINDING_RESPONSE = 0x0101;
|
||||||
|
|
||||||
#define EXCLUSIVE_MODE_TIMEOUT 300
|
#define EXCLUSIVE_MODE_TIMEOUT 300
|
||||||
|
|
||||||
UdpStunner::UdpStunner(UdpPublisher *pub)
|
UdpStunner::UdpStunner(UdpPublisher *pub)
|
||||||
@ -187,7 +193,6 @@ int UdpStunner::releaseExclusiveMode(std::string holder, bool forceStun)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
rstime_t now = time(NULL);
|
|
||||||
mExclusiveMode = false;
|
mExclusiveMode = false;
|
||||||
if (forceStun)
|
if (forceStun)
|
||||||
{
|
{
|
||||||
@ -217,6 +222,7 @@ int UdpStunner::releaseExclusiveMode(std::string holder, bool forceStun)
|
|||||||
}
|
}
|
||||||
|
|
||||||
#ifdef DEBUG_UDP_STUNNER_FILTER
|
#ifdef DEBUG_UDP_STUNNER_FILTER
|
||||||
|
rstime_t now = time(NULL);
|
||||||
std::cerr << "UdpStunner::cancelExclusiveMode() Canceled. Was in ExclusiveMode for: " << now - mExclusiveModeTS;
|
std::cerr << "UdpStunner::cancelExclusiveMode() Canceled. Was in ExclusiveMode for: " << now - mExclusiveModeTS;
|
||||||
std::cerr << " secs";
|
std::cerr << " secs";
|
||||||
std::cerr << std::endl;
|
std::cerr << std::endl;
|
||||||
@ -326,7 +332,7 @@ bool UdpStunner::locked_handleStunPkt(void *data, int size, struct sockaddr_in &
|
|||||||
#endif
|
#endif
|
||||||
/* generate a response */
|
/* generate a response */
|
||||||
int len;
|
int len;
|
||||||
void *pkt = UdpStun_generate_stun_reply(&from, &len);
|
void *pkt = UdpStun_generate_stun_reply(&from, &len, data);
|
||||||
if (!pkt)
|
if (!pkt)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
@ -485,7 +491,7 @@ bool UdpStun_response(void *stun_pkt, int size, struct sockaddr_in &addr)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (htons(((uint16_t *) stun_pkt)[0]) != 0x0101)
|
if (htons(((uint16_t *) stun_pkt)[0]) != STUN_BINDING_RESPONSE)
|
||||||
{
|
{
|
||||||
/* not a response */
|
/* not a response */
|
||||||
return false;
|
return false;
|
||||||
@ -517,19 +523,27 @@ bool UdpStun_generate_stun_pkt(void *stun_pkt, int *len)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* just the header */
|
/* just the header */
|
||||||
((uint16_t *) stun_pkt)[0] = (uint16_t) htons(0x0001);
|
((uint16_t *) stun_pkt)[0] = (uint16_t) htons(STUN_BINDING_REQUEST);
|
||||||
((uint16_t *) stun_pkt)[1] = (uint16_t) htons(20); /* only header */
|
((uint16_t *) stun_pkt)[1] = (uint16_t) htons(20); /* only header */
|
||||||
/* transaction id - should be random */
|
/* RFC 3489
|
||||||
((uint32_t *) stun_pkt)[1] = (uint32_t) htonl(0x0020);
|
* The transaction ID is used to correlate requests and responses.
|
||||||
((uint32_t *) stun_pkt)[2] = (uint32_t) htonl(0x0121);
|
*
|
||||||
((uint32_t *) stun_pkt)[3] = (uint32_t) htonl(0x0111);
|
* RFC 5389 introduces a mmgic cokie at the location where preciously the transaction ID was located:
|
||||||
((uint32_t *) stun_pkt)[4] = (uint32_t) htonl(0x1010);
|
* In RFC 3489 [RFC3489], this field was part of
|
||||||
|
* the transaction ID; placing the magic cookie in this location allows
|
||||||
|
* a server to detect if the client will understand certain attributes
|
||||||
|
* that were added in this revised specification.
|
||||||
|
*/
|
||||||
|
((uint32_t *) stun_pkt)[1] = htonl(RsRandom::random_u32());
|
||||||
|
((uint32_t *) stun_pkt)[2] = htonl(RsRandom::random_u32());
|
||||||
|
((uint32_t *) stun_pkt)[3] = htonl(RsRandom::random_u32());
|
||||||
|
((uint32_t *) stun_pkt)[4] = htonl(RsRandom::random_u32());
|
||||||
*len = 20;
|
*len = 20;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void *UdpStun_generate_stun_reply(struct sockaddr_in *stun_addr, int *len)
|
void *UdpStun_generate_stun_reply(struct sockaddr_in *stun_addr, int *len, const void *data)
|
||||||
{
|
{
|
||||||
/* just the header */
|
/* just the header */
|
||||||
void *stun_pkt = rs_malloc(28);
|
void *stun_pkt = rs_malloc(28);
|
||||||
@ -537,13 +551,13 @@ void *UdpStun_generate_stun_reply(struct sockaddr_in *stun_addr, int *len)
|
|||||||
if(!stun_pkt)
|
if(!stun_pkt)
|
||||||
return NULL ;
|
return NULL ;
|
||||||
|
|
||||||
((uint16_t *) stun_pkt)[0] = (uint16_t) htons(0x0101);
|
((uint16_t *) stun_pkt)[0] = (uint16_t) htons(STUN_BINDING_RESPONSE);
|
||||||
((uint16_t *) stun_pkt)[1] = (uint16_t) htons(28); /* only header + 8 byte addr */
|
((uint16_t *) stun_pkt)[1] = (uint16_t) htons(28); /* only header + 8 byte addr */
|
||||||
/* transaction id - should be random */
|
/* RFC 3489
|
||||||
((uint32_t *) stun_pkt)[1] = (uint32_t) htonl(0x0f20);
|
* The Binding Response MUST contain the same transaction ID contained in the Binding Request.
|
||||||
((uint32_t *) stun_pkt)[2] = (uint32_t) htonl(0x0f21);
|
*/
|
||||||
((uint32_t *) stun_pkt)[3] = (uint32_t) htonl(0x0f11);
|
memcpy(&((uint32_t *) stun_pkt)[1], &((uint32_t *) data)[1], 4 * sizeof (uint32_t));
|
||||||
((uint32_t *) stun_pkt)[4] = (uint32_t) htonl(0x1010);
|
|
||||||
/* now add address
|
/* now add address
|
||||||
* 0 1 2 3
|
* 0 1 2 3
|
||||||
* <INET> <port>
|
* <INET> <port>
|
||||||
@ -586,20 +600,20 @@ bool UdpStun_isStunPacket(void *data, int size)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((size == 20) && (0x0001 == ntohs(((uint16_t *) data)[0])))
|
if ((size == 20) && (STUN_BINDING_REQUEST == ntohs(((uint16_t *) data)[0])))
|
||||||
{
|
{
|
||||||
#ifdef DEBUG_UDP_STUNNER_FILTER
|
#ifdef DEBUG_UDP_STUNNER_FILTER
|
||||||
std::cerr << "UdpStunner::isStunPacket() (size=20 & data[0]=0x0001) -> true";
|
std::cerr << "UdpStunner::isStunPacket() (size=20 & data[0]=STUN_BINDING_REQUEST) -> true";
|
||||||
std::cerr << std::endl;
|
std::cerr << std::endl;
|
||||||
#endif
|
#endif
|
||||||
/* request */
|
/* request */
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((size == 28) && (0x0101 == ntohs(((uint16_t *) data)[0])))
|
if ((size == 28) && (STUN_BINDING_RESPONSE == ntohs(((uint16_t *) data)[0])))
|
||||||
{
|
{
|
||||||
#ifdef DEBUG_UDP_STUNNER_FILTER
|
#ifdef DEBUG_UDP_STUNNER_FILTER
|
||||||
std::cerr << "UdpStunner::isStunPacket() (size=28 & data[0]=0x0101) -> true";
|
std::cerr << "UdpStunner::isStunPacket() (size=28 & data[0]=STUN_BINDING_RESPONSE) -> true";
|
||||||
std::cerr << std::endl;
|
std::cerr << std::endl;
|
||||||
#endif
|
#endif
|
||||||
/* response */
|
/* response */
|
||||||
|
@ -170,7 +170,7 @@ bool locked_checkExternalAddress();
|
|||||||
|
|
||||||
bool UdpStun_isStunPacket(void *data, int size);
|
bool UdpStun_isStunPacket(void *data, int size);
|
||||||
bool UdpStun_response(void *stun_pkt, int size, struct sockaddr_in &addr);
|
bool UdpStun_response(void *stun_pkt, int size, struct sockaddr_in &addr);
|
||||||
void *UdpStun_generate_stun_reply(struct sockaddr_in *stun_addr, int *len);
|
void *UdpStun_generate_stun_reply(struct sockaddr_in *stun_addr, int *len, const void* data);
|
||||||
bool UdpStun_generate_stun_pkt(void *stun_pkt, int *len);
|
bool UdpStun_generate_stun_pkt(void *stun_pkt, int *len);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
x
Reference in New Issue
Block a user