switched stun packet byte layout to network-byte-order.

git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@333 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
drbob 2008-02-04 18:24:15 +00:00
parent 215e3386f2
commit 54063ab434

View File

@ -321,7 +321,7 @@ bool UdpSorter::response(void *stun_pkt, int size, struct sockaddr_in &addr)
return false; return false;
} }
if (((uint16_t *) stun_pkt)[0] != 0x0101) if (htons(((uint16_t *) stun_pkt)[0]) != 0x0101)
{ {
/* not a response */ /* not a response */
return false; return false;
@ -354,13 +354,13 @@ bool UdpSorter::generate_stun_pkt(void *stun_pkt, int *len)
} }
/* just the header */ /* just the header */
((uint16_t *) stun_pkt)[0] = 0x0001; ((uint16_t *) stun_pkt)[0] = (uint16_t) htons(0x0001);
((uint16_t *) stun_pkt)[1] = 20; /* only header */ ((uint16_t *) stun_pkt)[1] = (uint16_t) htons(20); /* only header */
/* transaction id - should be random */ /* transaction id - should be random */
((uint32_t *) stun_pkt)[1] = 0x0020; ((uint32_t *) stun_pkt)[1] = (uint32_t) htonl(0x0020);
((uint32_t *) stun_pkt)[2] = 0x0121; ((uint32_t *) stun_pkt)[2] = (uint32_t) htonl(0x0121);
((uint32_t *) stun_pkt)[3] = 0x0111; ((uint32_t *) stun_pkt)[3] = (uint32_t) htonl(0x0111);
((uint32_t *) stun_pkt)[4] = 0x1010; ((uint32_t *) stun_pkt)[4] = (uint32_t) htonl(0x1010);
*len = 20; *len = 20;
return true; return true;
} }
@ -370,21 +370,23 @@ void *UdpSorter::generate_stun_reply(struct sockaddr_in *stun_addr, int *len)
{ {
/* just the header */ /* just the header */
void *stun_pkt = malloc(28); void *stun_pkt = malloc(28);
((uint16_t *) stun_pkt)[0] = 0x0101; ((uint16_t *) stun_pkt)[0] = (uint16_t) htons(0x0101);
((uint16_t *) stun_pkt)[1] = 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 */ /* transaction id - should be random */
((uint32_t *) stun_pkt)[1] = 0x0020; ((uint32_t *) stun_pkt)[1] = (uint32_t) htonl(0x0f20);
((uint32_t *) stun_pkt)[2] = 0x0121; ((uint32_t *) stun_pkt)[2] = (uint32_t) htonl(0x0f21);
((uint32_t *) stun_pkt)[3] = 0x0111; ((uint32_t *) stun_pkt)[3] = (uint32_t) htonl(0x0f11);
((uint32_t *) stun_pkt)[4] = 0x1010; ((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>
* <inet address> * <inet address>
*/ */
((uint32_t *) stun_pkt)[6] = stun_addr->sin_addr.s_addr; /* THESE SHOULD BE NET ORDER ALREADY */
((uint16_t *) stun_pkt)[10] = AF_INET;
((uint16_t *) stun_pkt)[11] = stun_addr->sin_port; ((uint16_t *) stun_pkt)[11] = stun_addr->sin_port;
((uint32_t *) stun_pkt)[6] = stun_addr->sin_addr.s_addr;
*len = 28; *len = 28;
return stun_pkt; return stun_pkt;
@ -407,7 +409,7 @@ bool UdpSorter::isStunPacket(void *data, int size)
} }
/* match size field */ /* match size field */
uint16_t pktsize = ((uint16_t *) data)[1]; uint16_t pktsize = ntohs(((uint16_t *) data)[1]);
if (size != pktsize) if (size != pktsize)
{ {
#ifdef DEBUG_UDP_SORTER #ifdef DEBUG_UDP_SORTER
@ -417,7 +419,7 @@ bool UdpSorter::isStunPacket(void *data, int size)
return false; return false;
} }
if ((size == 20) && (0x0001 == ((uint16_t *) data)[0])) if ((size == 20) && (0x0001 == ntohs(((uint16_t *) data)[0])))
{ {
#ifdef DEBUG_UDP_SORTER #ifdef DEBUG_UDP_SORTER
std::cerr << "UdpSorter::isStunPacket() (size=20 & data[0]=0x0001) -> true"; std::cerr << "UdpSorter::isStunPacket() (size=20 & data[0]=0x0001) -> true";
@ -427,7 +429,7 @@ bool UdpSorter::isStunPacket(void *data, int size)
return true; return true;
} }
if ((size == 28) && (0x0101 == ((uint16_t *) data)[0])) if ((size == 28) && (0x0101 == ntohs(((uint16_t *) data)[0])))
{ {
#ifdef DEBUG_UDP_SORTER #ifdef DEBUG_UDP_SORTER
std::cerr << "UdpSorter::isStunPacket() (size=28 & data[0]=0x0101) -> true"; std::cerr << "UdpSorter::isStunPacket() (size=28 & data[0]=0x0101) -> true";