From e0d4a48ae76680bea71a01e9875badfdc19dc06c Mon Sep 17 00:00:00 2001 From: Wim Taymans Date: Wed, 10 Nov 2010 10:53:41 +0100 Subject: [PATCH] sdp: add method to check for multicast addresses Expose a previously internal method to check for multicast addresses. See #634093 --- gst-libs/gst/sdp/gstsdpmessage.c | 59 ++++++++++++++++++++++---------- gst-libs/gst/sdp/gstsdpmessage.h | 3 ++ win32/common/libgstsdp.def | 1 + 3 files changed, 45 insertions(+), 18 deletions(-) diff --git a/gst-libs/gst/sdp/gstsdpmessage.c b/gst-libs/gst/sdp/gstsdpmessage.c index 9231cb7b1a..c2de249c93 100644 --- a/gst-libs/gst/sdp/gstsdpmessage.c +++ b/gst-libs/gst/sdp/gstsdpmessage.c @@ -322,33 +322,56 @@ gst_sdp_message_free (GstSDPMessage * msg) return GST_SDP_OK; } -static gboolean -is_multicast_address (const gchar * host_name, guint * family) +/** + * gst_sdp_address_is_multicast: + * @nettype: a network type + * @addrtype: an address type + * @addr: an address + * + * Check if the given @addr is a multicast address. + * + * Returns: TRUE when @addr is multicast. + * + * Since: 0.10.32 + */ +gboolean +gst_sdp_address_is_multicast (const gchar * nettype, const gchar * addrtype, + const gchar * addr) { struct addrinfo hints; struct addrinfo *ai; struct addrinfo *res; gboolean ret = FALSE; + g_return_val_if_fail (addr, FALSE); + + /* we only support IN */ + if (nettype && strcmp (nettype, "IN") != 0) + return FALSE; + memset (&hints, 0, sizeof (hints)); hints.ai_socktype = SOCK_DGRAM; - g_return_val_if_fail (host_name, FALSE); + /* set the address type as a hint */ + if (addrtype) { + if (!strcmp (addrtype, "IP4")) + hints.ai_family = AF_INET; + else if (!strcmp (addrtype, "IP6")) + hints.ai_family = AF_INET6; + } - if (getaddrinfo (host_name, NULL, &hints, &res) < 0) + if (getaddrinfo (addr, NULL, &hints, &res) < 0) return FALSE; for (ai = res; !ret && ai; ai = ai->ai_next) { if (ai->ai_family == AF_INET) ret = - IN_MULTICAST (ntohl (((struct sockaddr_in *) ai->ai_addr)->sin_addr. - s_addr)); + IN_MULTICAST (ntohl (((struct sockaddr_in *) ai->ai_addr)-> + sin_addr.s_addr)); else ret = - IN6_IS_ADDR_MULTICAST (&((struct sockaddr_in6 *) ai->ai_addr)-> - sin6_addr); - if (ret && family) - *family = ai->ai_family; + IN6_IS_ADDR_MULTICAST (&((struct sockaddr_in6 *) ai-> + ai_addr)->sin6_addr); } freeaddrinfo (res); @@ -408,12 +431,12 @@ gst_sdp_message_as_text (const GstSDPMessage * msg) if (msg->connection.nettype && msg->connection.addrtype && msg->connection.address) { - guint family; - g_string_append_printf (lines, "c=%s %s %s", msg->connection.nettype, msg->connection.addrtype, msg->connection.address); - if (is_multicast_address (msg->connection.address, &family)) { - if (family == AF_INET) + if (gst_sdp_address_is_multicast (msg->connection.nettype, + msg->connection.addrtype, msg->connection.address)) { + /* only add ttl for IP4 */ + if (strcmp (msg->connection.addrtype, "IP4") == 0) g_string_append_printf (lines, "/%u", msg->connection.ttl); if (msg->connection.addr_number > 1) g_string_append_printf (lines, "/%u", msg->connection.addr_number); @@ -1341,12 +1364,12 @@ gst_sdp_media_as_text (const GstSDPMedia * media) const GstSDPConnection *conn = gst_sdp_media_get_connection (media, i); if (conn->nettype && conn->addrtype && conn->address) { - guint family; - g_string_append_printf (lines, "c=%s %s %s", conn->nettype, conn->addrtype, conn->address); - if (is_multicast_address (conn->address, &family)) { - if (family == AF_INET) + if (gst_sdp_address_is_multicast (conn->nettype, conn->addrtype, + conn->address)) { + /* only add ttl for IP4 multicast */ + if (strcmp (conn->addrtype, "IP4") == 0) g_string_append_printf (lines, "/%u", conn->ttl); if (conn->addr_number > 1) g_string_append_printf (lines, "/%u", conn->addr_number); diff --git a/gst-libs/gst/sdp/gstsdpmessage.h b/gst-libs/gst/sdp/gstsdpmessage.h index bd61b15626..069eb38d70 100644 --- a/gst-libs/gst/sdp/gstsdpmessage.h +++ b/gst-libs/gst/sdp/gstsdpmessage.h @@ -286,6 +286,9 @@ gchar* gst_sdp_message_as_text (const GstSDPMessage GstSDPResult gst_sdp_message_parse_uri (const gchar *uri, GstSDPMessage *msg); gchar* gst_sdp_message_as_uri (const gchar *scheme, const GstSDPMessage *msg); +/* utils */ +gboolean gst_sdp_address_is_multicast (const gchar *nettype, const gchar *addrtype, + const gchar *addr); /* v=.. */ const gchar* gst_sdp_message_get_version (const GstSDPMessage *msg); GstSDPResult gst_sdp_message_set_version (GstSDPMessage *msg, const gchar *version); diff --git a/win32/common/libgstsdp.def b/win32/common/libgstsdp.def index 4201fd0643..186e4ffaef 100644 --- a/win32/common/libgstsdp.def +++ b/win32/common/libgstsdp.def @@ -1,4 +1,5 @@ EXPORTS + gst_sdp_address_is_multicast gst_sdp_media_add_attribute gst_sdp_media_add_bandwidth gst_sdp_media_add_connection