Commit 972eb35f authored by Olivier Crête's avatar Olivier Crête

stun: Use struct sockaddr_storage * for any pointer that we write to

Some platforms have a larger alignment requirement for struct sockaddr_* than
for plain struct sockaddr, in that case, we need to make sure that the incoming pointer
has the right alignement before taking it in.
parent 23545569
......@@ -2124,7 +2124,7 @@ static gboolean priv_map_reply_to_conn_check_request (NiceAgent *agent, Stream *
if (memcmp (discovery_id, response_id, sizeof(StunTransactionId)) == 0) {
res = stun_usage_ice_conncheck_process (resp,
&sockaddr.addr, &socklen,
&sockaddr.storage, &socklen,
agent_to_ice_compatibility (agent));
nice_debug ("Agent %p : stun_bind_process/conncheck for %p res %d "
"(controlling=%d).", agent, p, (int)res, agent->controlling_mode);
......@@ -2175,7 +2175,7 @@ static gboolean priv_map_reply_to_conn_check_request (NiceAgent *agent, Stream *
" conncheck %p SUCCEEDED.", agent, p);
priv_conn_check_unfreeze_related (agent, stream, p);
} else {
ok_pair = priv_process_response_check_for_peer_reflexive(agent,
ok_pair = priv_process_response_check_for_peer_reflexive (agent,
stream, component, p, sockptr, &sockaddr.addr,
local_candidate, remote_candidate);
}
......@@ -2265,8 +2265,8 @@ static gboolean priv_map_reply_to_discovery_request (NiceAgent *agent, StunMessa
stun_message_id (&d->stun_message, discovery_id);
if (memcmp (discovery_id, response_id, sizeof(StunTransactionId)) == 0) {
res = stun_usage_bind_process (resp, &sockaddr.addr,
&socklen, &alternate.addr, &alternatelen);
res = stun_usage_bind_process (resp, &sockaddr.storage,
&socklen, &alternate.storage, &alternatelen);
nice_debug ("Agent %p : stun_bind_process/disc for %p res %d.",
agent, d, (int)res);
......@@ -2395,9 +2395,9 @@ static gboolean priv_map_reply_to_relay_request (NiceAgent *agent, StunMessage *
if (memcmp (discovery_id, response_id, sizeof(StunTransactionId)) == 0) {
res = stun_usage_turn_process (resp,
&relayaddr.addr, &relayaddrlen,
&sockaddr.addr, &socklen,
&alternate.addr, &alternatelen,
&relayaddr.storage, &relayaddrlen,
&sockaddr.storage, &socklen,
&alternate.storage, &alternatelen,
&bandwidth, &lifetime, agent_to_turn_compatibility (agent));
nice_debug ("Agent %p : stun_turn_process/disc for %p res %d.",
agent, d, (int)res);
......@@ -2956,7 +2956,7 @@ gboolean conn_check_handle_inbound_stun (NiceAgent *agent, Stream *stream,
rbuf_len = sizeof (rbuf);
res = stun_usage_ice_conncheck_create_reply (&agent->stun_agent, &req,
&msg, rbuf, &rbuf_len, &sockaddr.addr, sizeof (sockaddr),
&msg, rbuf, &rbuf_len, &sockaddr.storage, sizeof (sockaddr),
&control, agent->tie_breaker,
agent_to_ice_compatibility (agent));
......
......@@ -642,7 +642,7 @@ socket_send_message (NiceSocket *sock, const NiceAddress *to,
buffer, sizeof(buffer), STUN_IND_SEND))
goto send;
if (stun_message_append_xor_addr (&msg, STUN_ATTRIBUTE_PEER_ADDRESS,
&sa.addr, sizeof(sa)) !=
&sa.storage, sizeof(sa)) !=
STUN_MESSAGE_RETURN_SUCCESS)
goto send;
} else {
......@@ -660,7 +660,7 @@ socket_send_message (NiceSocket *sock, const NiceAddress *to,
goto send;
}
if (stun_message_append_addr (&msg, STUN_ATTRIBUTE_DESTINATION_ADDRESS,
&sa.addr, sizeof(sa)) !=
&sa.storage, sizeof(sa)) !=
STUN_MESSAGE_RETURN_SUCCESS)
goto send;
......@@ -852,7 +852,7 @@ priv_binding_expired_timeout (gpointer data)
/* look up binding associated with peer */
stun_message_find_xor_addr (
&priv->current_binding_msg->message,
STUN_ATTRIBUTE_XOR_PEER_ADDRESS, &sa.addr, &sa_len);
STUN_ATTRIBUTE_XOR_PEER_ADDRESS, &sa.storage, &sa_len);
nice_address_set_from_sockaddr (&to, &sa.addr);
/* If the binding is being refreshed, then move it to
......@@ -1071,7 +1071,7 @@ nice_turn_socket_parse_recv (NiceSocket *sock, NiceSocket **from_sock,
/* look up binding associated with peer */
stun_message_find_xor_addr (
&priv->current_binding_msg->message,
STUN_ATTRIBUTE_XOR_PEER_ADDRESS, &sa.addr, &sa_len);
STUN_ATTRIBUTE_XOR_PEER_ADDRESS, &sa.storage, &sa_len);
nice_address_set_from_sockaddr (&to, &sa.addr);
for (i = priv->channels; i; i = i->next) {
......@@ -1174,7 +1174,7 @@ nice_turn_socket_parse_recv (NiceSocket *sock, NiceSocket **from_sock,
nice_debug ("got response for CreatePermission");
stun_message_find_xor_addr (
&current_create_permission_msg->message,
STUN_ATTRIBUTE_XOR_PEER_ADDRESS, &peer.addr, &peer_len);
STUN_ATTRIBUTE_XOR_PEER_ADDRESS, &peer.storage, &peer_len);
nice_address_set_from_sockaddr (&to, &peer.addr);
/* unathorized => resend with realm and nonce */
......@@ -1255,12 +1255,12 @@ nice_turn_socket_parse_recv (NiceSocket *sock, NiceSocket **from_sock,
if (priv->compatibility == NICE_TURN_SOCKET_COMPATIBILITY_DRAFT9 ||
priv->compatibility == NICE_TURN_SOCKET_COMPATIBILITY_RFC5766) {
if (stun_message_find_xor_addr (&msg, STUN_ATTRIBUTE_REMOTE_ADDRESS,
&sa.addr, &from_len) !=
&sa.storage, &from_len) !=
STUN_MESSAGE_RETURN_SUCCESS)
goto recv;
} else {
if (stun_message_find_addr (&msg, STUN_ATTRIBUTE_REMOTE_ADDRESS,
&sa.addr, &from_len) !=
&sa.storage, &from_len) !=
STUN_MESSAGE_RETURN_SUCCESS)
goto recv;
}
......@@ -1440,7 +1440,7 @@ priv_retransmissions_create_permission_tick_unlocked (TurnPriv *priv, GList *lis
stun_agent_forget_transaction (&priv->agent, id);
stun_message_find_xor_addr (
&current_create_permission_msg->message,
STUN_ATTRIBUTE_XOR_PEER_ADDRESS, &addr.addr, &addr_len);
STUN_ATTRIBUTE_XOR_PEER_ADDRESS, &addr.storage, &addr_len);
nice_address_set_from_sockaddr (&to, &addr.addr);
priv_remove_sent_permission_for_peer (priv, &to);
......@@ -1647,7 +1647,7 @@ priv_send_create_permission(TurnPriv *priv, StunMessage *resp,
priv->password_len,
realm, realm_len,
nonce, nonce_len,
&addr.addr,
&addr.storage,
STUN_USAGE_TURN_COMPATIBILITY_RFC5766);
if (msg_buf_len > 0) {
......@@ -1699,7 +1699,7 @@ priv_send_channel_bind (TurnPriv *priv, StunMessage *resp,
}
if (stun_message_append_xor_addr (&msg->message, STUN_ATTRIBUTE_PEER_ADDRESS,
&sa.addr,
&sa.storage,
sizeof(sa))
!= STUN_MESSAGE_RETURN_SUCCESS) {
g_free (msg);
......@@ -1829,7 +1829,7 @@ priv_add_channel_binding (TurnPriv *priv, const NiceAddress *peer)
if (stun_message_append_addr (&msg->message,
STUN_ATTRIBUTE_DESTINATION_ADDRESS,
&sa.addr, sizeof(sa))
&sa.storage, sizeof(sa))
!= STUN_MESSAGE_RETURN_SUCCESS) {
g_free (msg);
return FALSE;
......
......@@ -223,7 +223,7 @@ stun_message_find_string (const StunMessage *msg, StunAttribute type,
StunMessageReturn
stun_message_find_addr (const StunMessage *msg, StunAttribute type,
struct sockaddr *addr, socklen_t *addrlen)
struct sockaddr_storage *addr, socklen_t *addrlen)
{
const uint8_t *ptr;
uint16_t len = 0;
......@@ -284,8 +284,7 @@ stun_message_find_addr (const StunMessage *msg, StunAttribute type,
StunMessageReturn
stun_message_find_xor_addr (const StunMessage *msg, StunAttribute type,
struct sockaddr *addr,
socklen_t *addrlen)
struct sockaddr_storage *addr, socklen_t *addrlen)
{
StunMessageReturn val = stun_message_find_addr (msg, type, addr, addrlen);
if (val)
......@@ -296,8 +295,7 @@ stun_message_find_xor_addr (const StunMessage *msg, StunAttribute type,
StunMessageReturn
stun_message_find_xor_addr_full (const StunMessage *msg, StunAttribute type,
struct sockaddr *addr, socklen_t *addrlen,
uint32_t magic_cookie)
struct sockaddr_storage *addr, socklen_t *addrlen, uint32_t magic_cookie)
{
StunMessageReturn val = stun_message_find_addr (msg, type, addr, addrlen);
if (val)
......@@ -422,7 +420,7 @@ stun_message_append_string (StunMessage * msg, StunAttribute type,
StunMessageReturn
stun_message_append_addr (StunMessage *msg, StunAttribute type,
const struct sockaddr *addr, socklen_t addrlen)
const struct sockaddr_storage *addr, socklen_t addrlen)
{
const void *pa;
uint8_t *ptr;
......@@ -432,7 +430,7 @@ stun_message_append_addr (StunMessage *msg, StunAttribute type,
if ((size_t) addrlen < sizeof (struct sockaddr))
return STUN_MESSAGE_RETURN_INVALID;
switch (addr->sa_family)
switch (addr->ss_family)
{
case AF_INET:
{
......@@ -475,48 +473,42 @@ stun_message_append_addr (StunMessage *msg, StunAttribute type,
StunMessageReturn
stun_message_append_xor_addr (StunMessage *msg, StunAttribute type,
const struct sockaddr *addr, socklen_t addrlen)
const struct sockaddr_storage *addr, socklen_t addrlen)
{
StunMessageReturn val;
/* Must be big enough to hold any supported address: */
union {
struct sockaddr_storage storage;
struct sockaddr addr;
} xor;
struct sockaddr_storage tmpaddr;
if ((size_t) addrlen > sizeof (xor))
addrlen = sizeof (xor);
memcpy (&xor.storage, addr, addrlen);
if ((size_t) addrlen > sizeof (tmpaddr))
addrlen = sizeof (tmpaddr);
memcpy (&tmpaddr, addr, addrlen);
val = stun_xor_address (msg, &xor.addr, addrlen,
val = stun_xor_address (msg, &tmpaddr, addrlen,
STUN_MAGIC_COOKIE);
if (val)
return val;
return stun_message_append_addr (msg, type, &xor.addr, addrlen);
return stun_message_append_addr (msg, type, &tmpaddr, addrlen);
}
StunMessageReturn
stun_message_append_xor_addr_full (StunMessage *msg, StunAttribute type,
const struct sockaddr *addr, socklen_t addrlen,
const struct sockaddr_storage *addr, socklen_t addrlen,
uint32_t magic_cookie)
{
StunMessageReturn val;
/* Must be big enough to hold any supported address: */
union {
struct sockaddr_storage storage;
struct sockaddr addr;
} xor;
struct sockaddr_storage tmpaddr;
if ((size_t) addrlen > sizeof (xor))
addrlen = sizeof (xor);
memcpy (&xor.storage, addr, addrlen);
if ((size_t) addrlen > sizeof (tmpaddr))
addrlen = sizeof (tmpaddr);
memcpy (&tmpaddr, addr, addrlen);
val = stun_xor_address (msg, &xor.addr, addrlen, magic_cookie);
val = stun_xor_address (msg, &tmpaddr, addrlen, magic_cookie);
if (val)
return val;
return stun_message_append_addr (msg, type, &xor.addr, addrlen);
return stun_message_append_addr (msg, type, &tmpaddr, addrlen);
}
......
......@@ -645,7 +645,7 @@ StunMessageReturn stun_message_find_string (const StunMessage *msg,
* %STUN_MESSAGE_RETURN_UNSUPPORTED_ADDRESS if the address family is unknown.
*/
StunMessageReturn stun_message_find_addr (const StunMessage *msg,
StunAttribute type, struct sockaddr *addr, socklen_t *addrlen);
StunAttribute type, struct sockaddr_storage *addr, socklen_t *addrlen);
/**
* stun_message_find_xor_addr:
......@@ -664,7 +664,7 @@ StunMessageReturn stun_message_find_addr (const StunMessage *msg,
* %STUN_MESSAGE_RETURN_UNSUPPORTED_ADDRESS if the address family is unknown.
*/
StunMessageReturn stun_message_find_xor_addr (const StunMessage *msg,
StunAttribute type, struct sockaddr *addr, socklen_t *addrlen);
StunAttribute type, struct sockaddr_storage *addr, socklen_t *addrlen);
/**
* stun_message_find_xor_addr_full:
......@@ -684,7 +684,7 @@ StunMessageReturn stun_message_find_xor_addr (const StunMessage *msg,
* %STUN_MESSAGE_RETURN_UNSUPPORTED_ADDRESS if the address family is unknown.
*/
StunMessageReturn stun_message_find_xor_addr_full (const StunMessage *msg,
StunAttribute type, struct sockaddr *addr,
StunAttribute type, struct sockaddr_storage *addr,
socklen_t *addrlen, uint32_t magic_cookie);
......@@ -795,7 +795,7 @@ StunMessageReturn stun_message_append_string (StunMessage *msg,
* %STUN_MESSAGE_RETURN_UNSUPPORTED_ADDRESS if the address family is unknown.
*/
StunMessageReturn stun_message_append_addr (StunMessage * msg,
StunAttribute type, const struct sockaddr *addr, socklen_t addrlen);
StunAttribute type, const struct sockaddr_storage *addr, socklen_t addrlen);
/**
* stun_message_append_xor_addr:
......@@ -811,7 +811,7 @@ StunMessageReturn stun_message_append_addr (StunMessage * msg,
* %STUN_MESSAGE_RETURN_UNSUPPORTED_ADDRESS if the address family is unknown.
*/
StunMessageReturn stun_message_append_xor_addr (StunMessage * msg,
StunAttribute type, const struct sockaddr *addr, socklen_t addrlen);
StunAttribute type, const struct sockaddr_storage *addr, socklen_t addrlen);
/**
* stun_message_append_xor_addr_full:
......@@ -828,7 +828,7 @@ StunMessageReturn stun_message_append_xor_addr (StunMessage * msg,
* %STUN_MESSAGE_RETURN_UNSUPPORTED_ADDRESS if the address family is unknown.
*/
StunMessageReturn stun_message_append_xor_addr_full (StunMessage * msg,
StunAttribute type, const struct sockaddr *addr, socklen_t addrlen,
StunAttribute type, const struct sockaddr_storage *addr, socklen_t addrlen,
uint32_t magic_cookie);
/**
......
......@@ -102,7 +102,8 @@ static int listen_dgram (void)
/** Incorrect socket family test */
static void bad_family (void)
{
struct sockaddr addr, dummy;
struct sockaddr addr;
struct sockaddr_storage dummy;
int val;
socklen_t dummylen = sizeof(dummy);
......@@ -121,7 +122,8 @@ static void bad_family (void)
/** Too small socket address test */
static void small_srv_addr (void)
{
struct sockaddr addr, dummy;
struct sockaddr addr;
struct sockaddr_storage dummy;
int val;
socklen_t dummylen = sizeof(dummy);
......@@ -141,7 +143,7 @@ static void small_srv_addr (void)
static void big_srv_addr (void)
{
uint8_t buf[sizeof (struct sockaddr_storage) + 16];
struct sockaddr dummy;
struct sockaddr_storage dummy;
int val;
socklen_t dummylen = sizeof(dummy);
......@@ -156,8 +158,7 @@ static void big_srv_addr (void)
/** Timeout test */
static void timeout (void)
{
struct sockaddr_storage srv;
struct sockaddr dummy;
struct sockaddr_storage srv, dummy;
socklen_t srvlen = sizeof (srv);
socklen_t dummylen = sizeof(dummy);
int val;
......@@ -231,8 +232,7 @@ static void bad_responses (void)
assert (stun_agent_validate (&agent, &msg, buf, len, NULL, NULL)
== STUN_VALIDATION_SUCCESS);
val = stun_usage_bind_process (&msg,
(struct sockaddr *)&addr, &addrlen, (struct sockaddr *)&addr, &addrlen);
val = stun_usage_bind_process (&msg, &addr, &addrlen, &addr, &addrlen);
assert (val == STUN_USAGE_BIND_RETURN_INVALID);
/* Send response with wrong request type */
......@@ -241,8 +241,7 @@ static void bad_responses (void)
/* Send error response without ERROR-CODE */
buf[1] |= 0x10;
val = stun_usage_bind_process (&msg,
(struct sockaddr *)&addr, &addrlen, (struct sockaddr *)&addr, &addrlen);
val = stun_usage_bind_process (&msg, &addr, &addrlen, &addr, &addrlen);
assert (val == STUN_USAGE_BIND_RETURN_INVALID);
close (fd);
......@@ -311,8 +310,7 @@ static void responses (void)
val = getsockname (servfd, (struct sockaddr *)&addr, &addrlen);
assert (val == 0);
val = stun_usage_bind_process (&msg,
(struct sockaddr *)&addr, &addrlen, (struct sockaddr *)&addr, &addrlen);
val = stun_usage_bind_process (&msg, &addr, &addrlen, &addr, &addrlen);
assert (val == STUN_USAGE_BIND_RETURN_ERROR);
/* Send response with a no mapped address at all */
......@@ -342,8 +340,7 @@ static void responses (void)
val = getsockname (servfd, (struct sockaddr *)&addr, &addrlen);
assert (val == 0);
val = stun_usage_bind_process (&msg,
(struct sockaddr *)&addr, &addrlen, (struct sockaddr *)&addr, &addrlen);
val = stun_usage_bind_process (&msg, &addr, &addrlen, &addr, &addrlen);
assert (val == STUN_USAGE_BIND_RETURN_ERROR);
/* Send old-style response */
......@@ -365,7 +362,7 @@ static void responses (void)
stun_agent_init_response (&agent, &msg, buf, sizeof (buf), &msg);
assert (stun_message_append_addr (&msg, STUN_ATTRIBUTE_MAPPED_ADDRESS,
(struct sockaddr *)&addr, addrlen) == STUN_MESSAGE_RETURN_SUCCESS);
&addr, addrlen) == STUN_MESSAGE_RETURN_SUCCESS);
len = stun_agent_finish_message (&agent, &msg, NULL, 0);
assert (len > 0);
......@@ -375,8 +372,7 @@ static void responses (void)
val = getsockname (servfd, (struct sockaddr *)&addr, &addrlen);
assert (val == 0);
val = stun_usage_bind_process (&msg,
(struct sockaddr *)&addr, &addrlen, (struct sockaddr *)&addr, &addrlen);
val = stun_usage_bind_process (&msg, &addr, &addrlen, &addr, &addrlen);
assert (val == STUN_USAGE_BIND_RETURN_SUCCESS);
/* End */
......
......@@ -67,7 +67,11 @@
int main (void)
{
struct sockaddr_in ip4;
union {
struct sockaddr sa;
struct sockaddr_storage storage;
struct sockaddr_in ip4;
} addr;
uint8_t req_buf[STUN_MAX_MESSAGE_SIZE];
uint8_t resp_buf[STUN_MAX_MESSAGE_SIZE];
const const uint64_t tie = 0x8000000000000000LL;
......@@ -95,13 +99,13 @@ int main (void)
STUN_AGENT_USAGE_USE_FINGERPRINT |
STUN_AGENT_USAGE_SHORT_TERM_CREDENTIALS);
memset (&ip4, 0, sizeof (ip4));
ip4.sin_family = AF_INET;
memset (&addr, 0, sizeof (addr));
addr.ip4.sin_family = AF_INET;
#ifdef HAVE_SA_LEN
ip4.sin_len = sizeof (addr);
addr.ip4.sin_len = sizeof (addr);
#endif
ip4.sin_port = htons (12345);
ip4.sin_addr.s_addr = htonl (0x7f000001);
addr.ip4.sin_port = htons (12345);
addr.ip4.sin_addr.s_addr = htonl (0x7f000001);
/* Incorrect message class */
assert (stun_agent_init_request (&agent, &req, req_buf, sizeof(req_buf), STUN_BINDING));
......@@ -112,8 +116,8 @@ int main (void)
len = sizeof (resp_buf);
val2 = stun_usage_ice_conncheck_create_reply (&agent, &req,
&resp, resp_buf, &len, (struct sockaddr *)&ip4,
sizeof (ip4), &control, tie, STUN_USAGE_ICE_COMPATIBILITY_RFC5245);
&resp, resp_buf, &len, &addr.storage,
sizeof (addr.ip4), &control, tie, STUN_USAGE_ICE_COMPATIBILITY_RFC5245);
assert (val2 == STUN_USAGE_ICE_RETURN_INVALID_REQUEST);
assert (len == 0);
......@@ -126,8 +130,8 @@ int main (void)
len = sizeof (resp_buf);
val2 = stun_usage_ice_conncheck_create_reply (&agent, &req,
&resp, resp_buf, &len, (struct sockaddr *)&ip4,
sizeof (ip4), &control, tie, STUN_USAGE_ICE_COMPATIBILITY_RFC5245);
&resp, resp_buf, &len, &addr.storage,
sizeof (addr.ip4), &control, tie, STUN_USAGE_ICE_COMPATIBILITY_RFC5245);
assert (val2 == STUN_USAGE_ICE_RETURN_INVALID_METHOD);
assert (len > 0);
......@@ -181,8 +185,8 @@ int main (void)
len = sizeof (resp_buf);
val2 = stun_usage_ice_conncheck_create_reply (&agent, &req,
&resp, resp_buf, &len, (struct sockaddr *)&ip4,
sizeof (ip4), &control, tie, STUN_USAGE_ICE_COMPATIBILITY_RFC5245);
&resp, resp_buf, &len, &addr.storage,
sizeof (addr.ip4), &control, tie, STUN_USAGE_ICE_COMPATIBILITY_RFC5245);
assert (val2 == STUN_USAGE_ICE_RETURN_SUCCESS);
assert (len > 0);
assert (stun_agent_validate (&agent, &resp, resp_buf, len,
......@@ -198,15 +202,15 @@ int main (void)
rlen = stun_agent_finish_message (&agent, &req, pass, pass_len);
assert (rlen > 0);
ip4.sin_family = AF_UNSPEC;
addr.ip4.sin_family = AF_UNSPEC;
len = sizeof (resp_buf);
val2 = stun_usage_ice_conncheck_create_reply (&agent, &req,
&resp, resp_buf, &len, (struct sockaddr *)&ip4,
sizeof (ip4), &control, tie, STUN_USAGE_ICE_COMPATIBILITY_RFC5245);
&resp, resp_buf, &len, &addr.storage,
sizeof (addr.ip4), &control, tie, STUN_USAGE_ICE_COMPATIBILITY_RFC5245);
assert (val2 == STUN_USAGE_ICE_RETURN_INVALID_ADDRESS);
assert (len == 0);
ip4.sin_family = AF_INET;
addr.ip4.sin_family = AF_INET;
/* Lost role conflict */
assert (stun_agent_init_request (&agent, &req, req_buf, sizeof(req_buf), STUN_BINDING));
......@@ -221,8 +225,8 @@ int main (void)
len = sizeof (resp_buf);
control = true;
val2 = stun_usage_ice_conncheck_create_reply (&agent, &req,
&resp, resp_buf, &len, (struct sockaddr *)&ip4,
sizeof (ip4), &control, tie, STUN_USAGE_ICE_COMPATIBILITY_RFC5245);
&resp, resp_buf, &len, &addr.storage,
sizeof (addr.ip4), &control, tie, STUN_USAGE_ICE_COMPATIBILITY_RFC5245);
assert (val2 == STUN_USAGE_ICE_RETURN_ROLE_CONFLICT);
assert (len > 0);
assert (control == false);
......@@ -242,8 +246,8 @@ int main (void)
len = sizeof (resp_buf);
control = false;
val2 = stun_usage_ice_conncheck_create_reply (&agent, &req,
&resp, resp_buf, &len, (struct sockaddr *)&ip4,
sizeof (ip4), &control, tie, STUN_USAGE_ICE_COMPATIBILITY_RFC5245);
&resp, resp_buf, &len, &addr.storage,
sizeof (addr.ip4), &control, tie, STUN_USAGE_ICE_COMPATIBILITY_RFC5245);
assert (val2 == STUN_USAGE_ICE_RETURN_SUCCESS);
assert (len > 0);
assert (control == false);
......
......@@ -146,29 +146,29 @@ check_af (const char *name, int family, socklen_t addrlen)
stun_agent_init_request (&agent, &msg, buf, sizeof(buf), STUN_BINDING);
if (stun_message_append_addr (&msg, STUN_ATTRIBUTE_MAPPED_ADDRESS,
(struct sockaddr *)&addr, addrlen) !=
&addr, addrlen) !=
STUN_MESSAGE_RETURN_UNSUPPORTED_ADDRESS)
fatal ("Unknown address family test failed");
if (stun_message_append_xor_addr (&msg, STUN_ATTRIBUTE_XOR_MAPPED_ADDRESS,
(struct sockaddr *)&addr, addrlen) !=
&addr, addrlen) !=
STUN_MESSAGE_RETURN_UNSUPPORTED_ADDRESS)
fatal ("Unknown address family xor test failed");
addr.ss_family = family;
if (stun_message_append_addr (&msg, STUN_ATTRIBUTE_MAPPED_ADDRESS,
(struct sockaddr *)&addr, addrlen - 1) != STUN_MESSAGE_RETURN_INVALID)
&addr, addrlen - 1) != STUN_MESSAGE_RETURN_INVALID)
fatal ("Too small %s sockaddr test failed", name);
if (stun_message_append_xor_addr (&msg, STUN_ATTRIBUTE_XOR_MAPPED_ADDRESS,
(struct sockaddr *)&addr, addrlen - 1) != STUN_MESSAGE_RETURN_INVALID)
&addr, addrlen - 1) != STUN_MESSAGE_RETURN_INVALID)
fatal ("Too small %s sockaddr xor test failed", name);
if (stun_message_append_addr (&msg, STUN_ATTRIBUTE_MAPPED_ADDRESS,
(struct sockaddr *)&addr, addrlen) != STUN_MESSAGE_RETURN_SUCCESS)
&addr, addrlen) != STUN_MESSAGE_RETURN_SUCCESS)
fatal ("%s sockaddr test failed", name);
if (stun_message_append_xor_addr (&msg, STUN_ATTRIBUTE_XOR_MAPPED_ADDRESS,
(struct sockaddr *)&addr, addrlen) != STUN_MESSAGE_RETURN_SUCCESS)
&addr, addrlen) != STUN_MESSAGE_RETURN_SUCCESS)
fatal ("%s sockaddr xor test failed", name);
}
......@@ -176,7 +176,10 @@ int main (void)
{
uint8_t buf[100];
size_t len;
struct sockaddr addr;
union {
struct sockaddr_storage storage;
struct sockaddr addr;
} addr;
StunAgent agent;
StunMessage msg;
......@@ -240,11 +243,11 @@ int main (void)
fatal ("String overflow test failed");
memset (&addr, 0, sizeof (addr));
addr.sa_family = AF_INET;
#ifdef HAVE_SA_LEN
addr.sa_len = sizeof (addr);
addr.addr.sa_family = AF_INET;
#ifdef HAVE_SS_LEN
addr.addr.ss_len = sizeof (addr);
#endif
if (stun_message_append_xor_addr (&msg, 0xffff, &addr,
if (stun_message_append_xor_addr (&msg, 0xffff, &addr.storage,
sizeof (addr)) != STUN_MESSAGE_RETURN_NOT_ENOUGH_SPACE)
fatal ("Address overflow test failed");
len = sizeof (msg);
......
......@@ -356,7 +356,7 @@ static void test_attribute (void)
union
{
struct sockaddr sa;
struct sockaddr_storage st;
struct sockaddr_in6 s6;
} addr;
socklen_t addrlen;
......@@ -422,27 +422,27 @@ static void test_attribute (void)
fatal ("String test failed");
addrlen = sizeof (addr);
if (stun_message_find_addr (&msg, 0xff01, &addr.sa, &addrlen) !=
if (stun_message_find_addr (&msg, 0xff01, &addr.st, &addrlen) !=
STUN_MESSAGE_RETURN_INVALID)
fatal ("Too short addres test failed");
addrlen = sizeof (addr);
if (stun_message_find_addr (&msg, 0xff02, &addr.sa, &addrlen) !=
if (stun_message_find_addr (&msg, 0xff02, &addr.st, &addrlen) !=
STUN_MESSAGE_RETURN_UNSUPPORTED_ADDRESS)
fatal ("Unknown address family test failed");
addrlen = sizeof (addr);
if (stun_message_find_addr (&msg, 0xff03, &addr.sa, &addrlen) !=
if (stun_message_find_addr (&msg, 0xff03, &addr.st, &addrlen) !=
STUN_MESSAGE_RETURN_INVALID)
fatal ("Too short IPv6 address test failed");
addrlen = sizeof (addr);
if (stun_message_find_addr (&msg, 0xff04, &addr.sa, &addrlen) !=
if (stun_message_find_addr (&msg, 0xff04, &addr.st, &addrlen) !=
STUN_MESSAGE_RETURN_SUCCESS)
fatal ("IPv4 address test failed");
addrlen = sizeof (addr);
if (stun_message_find_addr (&msg, 0xff05, &addr.sa, &addrlen) !=
if (stun_message_find_addr (&msg, 0xff05, &addr.st, &addrlen) !=
STUN_MESSAGE_RETURN_INVALID)
fatal ("Too big IPv4 address test failed");
addrlen = sizeof (addr);
if (stun_message_find_xor_addr (&msg, 0xff06, &addr.sa, &addrlen) !=
if (stun_message_find_xor_addr (&msg, 0xff06, &addr.st, &addrlen) !=
STUN_MESSAGE_RETURN_SUCCESS ||
memcmp (&addr.s6.sin6_addr, "\x20\x01\x0d\xb8""\xde\xad\xbe\xef"
"\xde\xfa\xce\xd0""\xfa\xce\xde\xed", 16))
......@@ -598,8 +598,11 @@ static void test_vectors (void)
0x80, 0x28, 0x00, 0x04, // FINGERPRINT
0xec, 0x27, 0xae, 0xb7};
struct sockaddr_in ip4;
struct sockaddr_in6 ip6;
union {
struct sockaddr_storage st;
struct sockaddr_in ip4;
struct sockaddr_in6 ip6;
} addr;
socklen_t addrlen;
StunAgent agent;
......@@ -616,8 +619,7 @@ static void test_vectors (void)
STUN_AGENT_USAGE_SHORT_TERM_CREDENTIALS |
STUN_AGENT_USAGE_USE_FINGERPRINT);
memset (&ip4, 0, sizeof (ip4));
memset (&ip6, 0, sizeof (ip6));
memset (&addr, 0, sizeof (addr));
puts ("Checking test vectors...");
......@@ -650,15 +652,15 @@ static void test_vectors (void)
test_vector_validater, (void *) 0) != STUN_VALIDATION_UNMATCHED_RESPONSE)
fatal ("Response ipv4 test vector authentication failed");
addrlen = sizeof (ip4);
addrlen = sizeof (addr.ip4);
if (stun_message_find_xor_addr (&msg, STUN_ATTRIBUTE_XOR_MAPPED_ADDRESS,
(struct sockaddr *)&ip4, &addrlen) != STUN_MESSAGE_RETURN_SUCCESS)
&addr.st, &addrlen) != STUN_MESSAGE_RETURN_SUCCESS)
fatal ("Response test vector IPv4 extraction failed");
if (ip4.sin_family != AF_INET)
if (addr.ip4.sin_family != AF_INET)
fatal ("Response test vector IPv4 family failed");
if (ntohl (ip4.sin_addr.s_addr) != 0xC0000201)
if (ntohl (addr.ip4.sin_addr.s_addr) != 0xC0000201)
fatal ("Response test vector IPv4 address failed");
if (ntohs (ip4.sin_port) != 32853)
if (ntohs (addr.ip4.sin_port) != 32853)
fatal ("Response test vector IPv6 port failed");
if (stun_agent_validate (&agent, &msg, req, sizeof(req),
......@@ -683,16 +685,16 @@ static void test_vectors (void)
test_vector_validater, (void *) 1) != STUN_VALIDATION_SUCCESS)
fatal ("Response ipv6 test vector authentication failed");
addrlen = sizeof (ip6);
addrlen = sizeof (addr.ip6);
if (stun_message_find_xor_addr (&msg, STUN_ATTRIBUTE_XOR_MAPPED_ADDRESS,
(struct sockaddr *)&ip6, &addrlen) != STUN_MESSAGE_RETURN_SUCCESS)
&addr.st, &addrlen) != STUN_MESSAGE_RETURN_SUCCESS)
fatal ("Response test vector IPv6 extraction failed");
if (ip6.sin6_family != AF_INET6)
if (addr.ip6.sin6_family != AF_INET6)
fatal ("Response test vector IPv6 family failed");
if (memcmp (ip6.sin6_addr.s6_addr, "\x20\x01\x0d\xb8\x12\x34\x56\x78"
if (memcmp (addr.ip6.sin6_addr.s6_addr, "\x20\x01\x0d\xb8\x12\x34\x56\x78"
"\x00\x11\x22\x33\x44\x55\x66\x77", 16) != 0)
fatal ("Response test vector IPv6 address failed");
if (ntohs (ip6.sin6_port) != 32853)
if (ntohs (addr.ip6.sin6_port) != 32853)
fatal ("Response test vector IPv6 port failed");
......
......@@ -108,8 +108,8 @@ static int run (int family, const char *hostname, const char *service)
printaddr ("Server address", ptr->ai_addr, ptr->ai_addrlen);
val = stun_usage_bind_run (ptr->ai_addr, ptr->ai_addrlen,
&addr.addr, &addrlen);
val = stun_usage_bind_run (ptr->ai_addr, ptr->ai_addrlen, &addr.storage,
&addrlen);
if (val)
fprintf (stderr, "%d\n", val);
else
......
......@@ -223,10 +223,10 @@ static int dgram_process (int sock, StunAgent *oldagent, StunAgent *newagent)
stun_agent_init_response (agent, &response, buf, sizeof (buf), &request);
if (stun_message_has_cookie (&request))
stun_message_append_xor_addr (&response,
STUN_ATTRIBUTE_XOR_MAPPED_ADDRESS, &addr.addr, addr_len);
STUN_ATTRIBUTE_XOR_MAPPED_ADDRESS, &addr.storage, addr_len);
else
stun_message_append_addr (&response, STUN_ATTRIBUTE_MAPPED_ADDRESS,
&addr.addr, addr_len);
&addr.storage, addr_len);
break;
case STUN_SHARED_SECRET:
......
......@@ -94,8 +94,8 @@ size_t stun_usage_bind_create (StunAgent *agent, StunMessage *msg,
}
StunUsageBindReturn stun_usage_bind_process (StunMessage *msg,
struct sockaddr *addr, socklen_t *addrlen,
struct sockaddr *alternate_server, socklen_t *alternate_server_len)
struct sockaddr_storage *addr, socklen_t *addrlen,
struct sockaddr_storage *alternate_server, socklen_t *alternate_server_len)
{
int code = -1;
StunMessageReturn val;
......@@ -355,7 +355,7 @@ static int stun_err_dequeue (int fd)
static ssize_t
stun_trans_sendto (StunTransport *tr, const uint8_t *buf, size_t len,
const struct sockaddr *dst, socklen_t dstlen)
const struct sockaddr *dst, socklen_t dstlen)
{
static const int flags = MSG_DONTWAIT | MSG_NOSIGNAL;
ssize_t val;
......@@ -375,14 +375,15 @@ stun_trans_sendto (StunTransport *tr, const uint8_t *buf, size_t len,