Commit 2ad6c8f6 authored by Ghislain MARY's avatar Ghislain MARY

Add IP_PKTINFO socket option activation to be able to get the IP address on...

Add IP_PKTINFO socket option activation to be able to get the IP address on which a packet was received.
parent 6f009177
......@@ -307,6 +307,9 @@ int rtp_session_set_dscp(RtpSession *session, int dscp);
int rtp_session_get_dscp(const RtpSession *session);
/* Packet info */
int rtp_session_set_pktinfo(RtpSession *session, int activate);
/* Multicast methods */
int rtp_session_set_multicast_ttl(RtpSession *session, int ttl);
int rtp_session_get_multicast_ttl(RtpSession *session);
......
......@@ -39,6 +39,7 @@ typedef struct msgb
#if defined(ORTP_TIMESTAMP)
struct timeval timestamp;
#endif
struct in_addr ipi_addr;
} mblk_t;
......
......@@ -308,6 +308,46 @@ rtp_session_set_local_addr (RtpSession * session, const char * addr, int port)
}
/**
*rtp_session_set_pktinfo:
*@session: a rtp session
*@activate: activation flag (0 to deactivate, other value to activate)
*
* (De)activates packet info for incoming and outgoing packets.
*
* Returns: 0 on success.
*
**/
int rtp_session_set_pktinfo(RtpSession *session, int activate)
{
int retval;
// Dont't do anything if socket hasn't been created yet
if (session->rtp.socket == (ortp_socket_t)-1) return 0;
switch (session->rtp.sockfamily) {
case AF_INET:
retval = setsockopt(session->rtp.socket, IPPROTO_IP, IP_PKTINFO, &activate, sizeof(activate));
if (retval < 0) break;
retval = setsockopt(session->rtcp.socket, IPPROTO_IP, IP_PKTINFO, &activate, sizeof(activate));
break;
#ifdef ORTP_INET6
case AF_INET6:
retval = setsockopt(session->rtp.socket, IPPROTO_IPV6, IPV6_PKTINFO, &activate, sizeof(activate));
if (retval < 0) break;
retval = setsockopt(session->rtcp.socket, IPPROTO_IPV6, IPV6_PKTINFO, &activate, sizeof(activate));
break;
#endif
default:
retval = -1;
break;
}
if (retval < 0) ortp_warning("Failed to set packet info on socket.");
return retval;
}
/**
*rtp_session_set_multicast_ttl:
*@session: a rtp session
......@@ -975,13 +1015,11 @@ int rtp_session_rtp_recv_abstract(ortp_socket_t socket, mblk_t *msg, int flags,
#ifndef _WIN32
struct iovec iov;
struct msghdr msghdr;
#if defined(ORTP_TIMESTAMP)
struct cmsghdr *cmsghdr;
struct {
struct cmsghdr cm;
char control[512];
} control;
#endif
memset(&msghdr, 0, sizeof(msghdr));
memset(&iov, 0, sizeof(iov));
iov.iov_base = msg->b_wptr;
......@@ -992,10 +1030,8 @@ int rtp_session_rtp_recv_abstract(ortp_socket_t socket, mblk_t *msg, int flags,
}
msghdr.msg_iov = &iov;
msghdr.msg_iovlen = 1;
#if defined(ORTP_TIMESTAMP)
msghdr.msg_control = &control;
msghdr.msg_controllen = sizeof(control);
#endif
int ret = recvmsg(socket, &msghdr, flags);
if(fromlen != NULL)
......@@ -1009,6 +1045,14 @@ int rtp_session_rtp_recv_abstract(ortp_socket_t socket, mblk_t *msg, int flags,
}
}
#endif
if(ret >= 0) {
for (cmsghdr = CMSG_FIRSTHDR(&msghdr); cmsghdr != NULL ; cmsghdr = CMSG_NXTHDR(&msghdr, cmsghdr)) {
if (((cmsghdr->cmsg_level == IPPROTO_IP) || (cmsghdr->cmsg_level == IPPROTO_IPV6)) && (cmsghdr->cmsg_type == IP_PKTINFO)) {
struct in_pktinfo *pi = (struct in_pktinfo *)CMSG_DATA(cmsghdr);
memcpy(&msg->ipi_addr, &pi->ipi_addr, sizeof(msg->ipi_addr));
}
}
}
return ret;
#else
return recvfrom(socket, (char *)msg->b_wptr, bufsz, flags, from, fromlen);
......
......@@ -273,6 +273,7 @@ mblk_t *copyb(mblk_t *mp)
newm=allocb(len,BPRI_MED);
memcpy(newm->b_wptr,mp->b_rptr,len);
newm->b_wptr+=len;
newm->ipi_addr=mp->ipi_addr;
return newm;
}
......
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