Commit 161c4314 authored by Pekka Pessi's avatar Pekka Pessi

Added msg_get_address() and msg_set_address() functions to <msg_addr.h>

Trying to solve ai_addrlen problem.

darcs-hash:20060512144731-65a35-ab57c9da2634bc6ff45ef9c87d7ceea0fe6b28fc.gz
parent c4ae2449
......@@ -238,18 +238,51 @@ void msg_addr_zero(msg_t *msg)
msg->m_addrinfo.ai_addr = &msg->m_addr->su_sa;
}
/** Get pointer to address length. */
size_t *msg_addrlen(msg_t *msg)
/** Get pointer to address length.
*
* @deprecated Use msg_get_address() or msg_set_address() instead.
*/
socklen_t *msg_addrlen(msg_t *msg)
{
return &msg->m_addrinfo.ai_addrlen;
}
/** Get socket address structure. */
/** Get pointer to socket address structure.
*
* @deprecated Use msg_get_address() or msg_set_address() instead.
*/
su_sockaddr_t *msg_addr(msg_t *msg)
{
return msg ? msg->m_addr : 0;
}
/** Get message address. */
int msg_get_address(msg_t *msg, su_sockaddr_t *su, socklen_t *return_len)
{
if (msg && return_len && *return_len >= msg->m_addrinfo.ai_addrlen) {
*return_len = msg->m_addrinfo.ai_addrlen;
if (su)
memcpy(su, msg->m_addr, *return_len = msg->m_addrinfo.ai_addrlen);
return 0;
}
if (msg)
msg->m_errno = EFAULT;
return -1;
}
/** Set message address. */
int msg_set_address(msg_t *msg, su_sockaddr_t const *su, socklen_t sulen)
{
if (sulen < (sizeof msg->m_addr) && msg && su) {
memcpy(msg->m_addr, su, msg->m_addrinfo.ai_addrlen = sulen);
msg->m_addrinfo.ai_family = su->su_family;
return 0;
}
if (msg)
msg->m_errno = EFAULT;
return -1;
}
/** Get addrinfo structure. */
su_addrinfo_t *msg_addrinfo(msg_t *msg)
{
......
......@@ -46,8 +46,13 @@ SOFIA_BEGIN_DECLS
SOFIAPUBFUN void msg_addr_zero(msg_t *msg);
SOFIAPUBFUN su_addrinfo_t *msg_addrinfo(msg_t *msg);
SOFIAPUBFUN su_sockaddr_t *msg_addr(msg_t *msg);
SOFIAPUBFUN size_t *msg_addrlen(msg_t *msg);
SOFIAPUBFUN socklen_t *msg_addrlen(msg_t *msg);
SOFIAPUBFUN int msg_get_address(msg_t *msg, su_sockaddr_t *, socklen_t *);
SOFIAPUBFUN int msg_set_address(msg_t *msg, su_sockaddr_t const *, socklen_t);
SOFIAPUBFUN void msg_addr_copy(msg_t *dst, msg_t const *src);
SOFIAPUBFUN int msg_errno(msg_t const *msg);
......
......@@ -152,7 +152,9 @@ struct tport_comp_vtable_s {
int (*vsc_recv_comp)(tport_t const *self,
tport_compressor_t *sc,
msg_t **in_out_msg);
msg_t **in_out_msg,
su_sockaddr_t *from,
socklen_t fromlen);
int (*vsc_send_comp)(tport_t const *self,
msg_t *msg,
......
......@@ -2819,11 +2819,11 @@ tport_delivered_with_comp(tport_t *tp, msg_t const *msg,
* return message buffer as a iovec
*/
int tport_recv_iovec(tport_t const *self,
msg_t **mmsg,
msg_t **in_out_msg,
msg_iovec_t iovec[msg_n_fragments], int N,
int exact)
{
msg_t *msg = *mmsg;
msg_t *msg = *in_out_msg;
int veclen, fresh;
if (N == 0)
......@@ -2835,7 +2835,7 @@ int tport_recv_iovec(tport_t const *self,
* Allocate a new message if needed
*/
if (!msg) {
if (!(*mmsg = msg = tport_msg_alloc(self, N))) {
if (!(*in_out_msg = msg = tport_msg_alloc(self, N))) {
SU_DEBUG_7(("%s(%p): cannot allocate msg for %u bytes "
"from (%s/%s:%s)\n",
__func__, self, N,
......@@ -3273,12 +3273,12 @@ int tport_send_error(tport_t *self, msg_t *msg,
}
#if SU_HAVE_IN6
else if (self->tp_addrinfo->ai_family == AF_INET6) {
su_sockaddr_t const *su = msg_addr(msg);
su_sockaddr_t const *su = (su_sockaddr_t const *)ai->ai_addr;
SU_DEBUG_3(("tport_vsend(%p): %s with "
"(s=%d, IP6=%s/%s:%s%s (scope=%i) addrlen=%zd)\n",
self, su_strerror(error), self->tp_socket,
tpn->tpn_proto, tpn->tpn_host, tpn->tpn_port, comp,
su->su_scope_id, *msg_addrlen(msg)));
su->su_scope_id, ai->ai_addrlen));
}
#endif
else {
......
......@@ -480,7 +480,8 @@ typedef int const *(tport_set_f)(tport_master_t *mr,
int tport_init_stun_server(tport_master_t *mr, tagi_t const *tags);
void tport_deinit_stun_server(tport_master_t *mr);
int tport_recv_stun_dgram(tport_t const *self, msg_t **in_out_msg);
int tport_recv_stun_dgram(tport_t const *self, msg_t **in_out_msg,
su_sockaddr_t *from, socklen_t fromlen);
int tport_stun_server_add_socket(tport_t *tp);
int tport_stun_server_remove_socket(tport_t *tp);
......@@ -506,7 +507,9 @@ void tport_sigcomp_accept_incomplete(tport_t *self, msg_t *msg);
int tport_recv_comp_dgram(tport_t const *self,
tport_compressor_t *sc,
msg_t **in_out_msg);
msg_t **in_out_msg,
su_sockaddr_t *from,
socklen_t fromlen);
int tport_send_comp(tport_t const *self,
msg_t *msg,
......
......@@ -437,13 +437,10 @@ static int tport_recv_sigcomp_r(tport_t *self,
if (msg) {
/* Message address */
if (self->tp_addrinfo->ai_socktype == SOCK_STREAM) {
*msg_addr(msg) = *self->tp_addr;
*msg_addrlen(msg) = self->tp_addrlen;
} else {
*msg_addr(msg) = *su;
*msg_addrlen(msg) = su_size;
}
if (self->tp_addrinfo->ai_socktype == SOCK_STREAM)
msg_set_address(msg, self->tp_addr, self->tp_addrlen);
else
msg_set_address(msg, su, su_size);
SU_DEBUG_5(("%s(%p): sigcomp recv = %u => %u %s\n", __func__, self,
N, dlen, eos ? " (complete)" : ""));
......
......@@ -258,12 +258,14 @@ struct sigcomp_udvm **tport_get_udvm_slot(tport_t *self)
/** Receive data from datagram using SigComp. */
int tport_recv_comp_dgram(tport_t const *self,
tport_compressor_t *sc,
msg_t **in_out_msg)
msg_t **in_out_msg,
su_sockaddr_t *from,
socklen_t fromlen)
{
tport_comp_vtable_t const *vsc = tport_comp_vtable;
if (vsc)
return vsc->vsc_recv_comp(self, sc, in_out_msg);
return vsc->vsc_recv_comp(self, sc, in_out_msg, from, fromlen);
msg_destroy(*in_out_msg), *in_out_msg = NULL;
......
......@@ -126,14 +126,14 @@ int tport_stun_server_remove_socket(tport_t *tp)
* @retval 3 stun message received, ignore
*/
int tport_recv_stun_dgram(tport_t const *self,
msg_t **in_out_msg)
msg_t **in_out_msg,
su_sockaddr_t *from,
socklen_t fromlen)
{
int retval = -1;
msg_t *msg;
uint8_t *request;
size_t n;
su_sockaddr_t *from;
socklen_t fromlen;
assert(in_out_msg); assert(*in_out_msg);
......@@ -141,8 +141,6 @@ int tport_recv_stun_dgram(tport_t const *self,
request = msg_buf_committed_data(msg);
n = msg_buf_committed(msg);
from = msg_addr(msg);
fromlen = *msg_addrlen(msg);
if (n < 20 || request == NULL) {
su_seterrno(EBADMSG);
......
......@@ -248,9 +248,7 @@ int tport_recv_sctp(tport_t *self)
assert(veclen == 1); assert(iovec[0].mv_len == N);
msg = self->tp_msg;
/* Message address */
*msg_addr(msg) = *self->tp_addr;
*msg_addrlen(msg) = su_sockaddr_size(self->tp_addr);
msg_set_address(msg, self->tp_addr, self->tp_addrlen);
memcpy(iovec[0].mv_base, sctp_buf, iovec[0].mv_len);
......
......@@ -237,9 +237,7 @@ int tport_recv_stream(tport_t *self)
msg = self->tp_msg;
/* Message address */
*msg_addr(msg) = *self->tp_addr;
*msg_addrlen(msg) = self->tp_addrlen;
msg_set_address(msg, self->tp_addr, self->tp_addrlen);
n = su_vrecv(self->tp_socket, iovec, veclen, 0, NULL, NULL);
if (n == SOCKET_ERROR)
......
......@@ -374,9 +374,7 @@ int tport_tls_recv(tport_t *self)
tls_buf = tls_read_buffer(tlstp->tlstp_context, N);
/* Message address */
*msg_addr(msg) = *self->tp_addr;
*msg_addrlen(msg) = self->tp_addrlen;
msg_set_address(msg, self->tp_addr, self->tp_addrlen);
for (i = 0, n = 0; i < veclen; i++) {
m = iovec[i].mv_len; assert(N >= n + m);
......
......@@ -271,10 +271,11 @@ int tport_recv_dgram(tport_t *self)
if ((sample[0] & 0xf8) == 0xf8)
/* SigComp */
return tport_recv_comp_dgram(self, self->tp_comp, &self->tp_msg);
return tport_recv_comp_dgram(self, self->tp_comp, &self->tp_msg,
from, fromlen);
else if (sample[0] == 0 || sample[0] == 1)
/* STUN request or response */
return tport_recv_stun_dgram(self, &self->tp_msg);
return tport_recv_stun_dgram(self, &self->tp_msg, from, fromlen);
else
return 0;
}
......@@ -284,20 +285,18 @@ int tport_send_dgram(tport_t const *self, msg_t *msg,
msg_iovec_t iov[],
int iovused)
{
su_sockaddr_t *su;
int sulen;
#if SU_HAVE_IN6 && defined(IN6_INADDR_TO_V4MAPPED)
su_sockaddr_t su0[1];
#endif
su_sockaddr_t su[1];
socklen_t sulen = sizeof su;
if (tport_is_connection_oriented(self))
return su_vsend(self->tp_socket, iov, iovused, MSG_NOSIGNAL, NULL, 0);
su = msg_addr(msg);
sulen = *msg_addrlen(msg);
msg_get_address(msg, su, &sulen);
#if SU_HAVE_IN6 && defined(IN6_INADDR_TO_V4MAPPED)
if (su->su_family == AF_INET && self->tp_addrinfo->ai_family == AF_INET6) {
su_sockaddr_t su0[1];
memset(su0, 0, sizeof su0);
su0->su_family = self->tp_addrinfo->ai_family;
......@@ -305,7 +304,7 @@ int tport_send_dgram(tport_t const *self, msg_t *msg,
IN6_INADDR_TO_V4MAPPED(&su->su_sin.sin_addr, &su0->su_sin6.sin6_addr);
su = su0, sulen = sizeof(su0->su_sin6);
memcpy(su, su0, sulen = sizeof(su0->su_sin6));
}
#endif
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment