From 7f980d28aa23c64dce423c0c0f994b2d136fc7fe Mon Sep 17 00:00:00 2001 From: Andoni Morales Alastruey Date: Wed, 10 Mar 2010 22:23:43 +0100 Subject: [PATCH] multiudpsink: avoid getting the socket family using getsockname() --- gst/udp/gstmultiudpsink.c | 13 +++++++++---- gst/udp/gstmultiudpsink.h | 1 + gst/udp/gstudpnetutils.c | 22 ++++------------------ gst/udp/gstudpnetutils.h | 4 ++-- 4 files changed, 16 insertions(+), 24 deletions(-) diff --git a/gst/udp/gstmultiudpsink.c b/gst/udp/gstmultiudpsink.c index ae7abfe..0a4d3e7 100644 --- a/gst/udp/gstmultiudpsink.c +++ b/gst/udp/gstmultiudpsink.c @@ -77,6 +77,7 @@ enum #define DEFAULT_CLOSEFD TRUE #define DEFAULT_SOCK -1 #define DEFAULT_CLIENTS NULL +#define DEFAULT_FAMILY 0 /* FIXME, this should be disabled by default, we don't need to join a multicast * group for sending, if this socket is also used for receiving, it should * be configured in the element that does the receive. */ @@ -356,6 +357,7 @@ gst_multiudpsink_init (GstMultiUDPSink * sink) sink->ttl_mc = DEFAULT_TTL_MC; sink->loop = DEFAULT_LOOP; sink->qos_dscp = DEFAULT_QOS_DSCP; + sink->ss_family = DEFAULT_FAMILY; } static void @@ -774,14 +776,14 @@ gst_multiudpsink_configure_client (GstMultiUDPSink * sink, goto join_group_failed; } GST_DEBUG_OBJECT (sink, "setting loop to %d", sink->loop); - if (gst_udp_set_loop (sink->sock, sink->loop) != 0) + if (gst_udp_set_loop (sink->sock, sink->ss_family, sink->loop) != 0) goto loop_failed; GST_DEBUG_OBJECT (sink, "setting ttl to %d", sink->ttl_mc); - if (gst_udp_set_ttl (sink->sock, sink->ttl_mc, TRUE) != 0) + if (gst_udp_set_ttl (sink->sock, sink->ss_family, sink->ttl_mc, TRUE) != 0) goto ttl_failed; } else { GST_DEBUG_OBJECT (sink, "setting unicast ttl to %d", sink->ttl); - if (gst_udp_set_ttl (sink->sock, sink->ttl, FALSE) != 0) + if (gst_udp_set_ttl (sink->sock, sink->ss_family, sink->ttl, FALSE) != 0) goto ttl_failed; } return TRUE; @@ -831,9 +833,12 @@ gst_multiudpsink_init_send (GstMultiUDPSink * sink) if (sink->sockfd == -1) { GST_DEBUG_OBJECT (sink, "creating sockets"); /* create sender socket try IP6, fall back to IP4 */ - if ((sink->sock = socket (AF_INET6, SOCK_DGRAM, 0)) == -1) + sink->ss_family = AF_INET6; + if ((sink->sock = socket (AF_INET6, SOCK_DGRAM, 0)) == -1){ + sink->ss_family = AF_INET; if ((sink->sock = socket (AF_INET, SOCK_DGRAM, 0)) == -1) goto no_socket; + } GST_DEBUG_OBJECT (sink, "have socket"); sink->externalfd = FALSE; diff --git a/gst/udp/gstmultiudpsink.h b/gst/udp/gstmultiudpsink.h index 37787b7..722acf8 100644 --- a/gst/udp/gstmultiudpsink.h +++ b/gst/udp/gstmultiudpsink.h @@ -75,6 +75,7 @@ struct _GstMultiUDPSink { gint ttl_mc; gboolean loop; gint qos_dscp; + ADDRESS_FAMILY ss_family; }; struct _GstMultiUDPSinkClass { diff --git a/gst/udp/gstudpnetutils.c b/gst/udp/gstudpnetutils.c index eb47ad6..bbea308 100644 --- a/gst/udp/gstudpnetutils.c +++ b/gst/udp/gstudpnetutils.c @@ -117,19 +117,12 @@ beach: } int -gst_udp_set_loop (int sockfd, gboolean loop) +gst_udp_set_loop (int sockfd, ADDRESS_FAMILY ss_family, gboolean loop) { - socklen_t socklen; - struct sockaddr_storage addr; int ret = -1; int l = (loop == FALSE) ? 0 : 1; - socklen = sizeof (addr); - if ((ret = getsockname (sockfd, (struct sockaddr *) &addr, &socklen)) < 0) { - return ret; - } - - switch (addr.ss_family) { + switch (ss_family) { case AF_INET: { ret = setsockopt (sockfd, IPPROTO_IP, IP_MULTICAST_LOOP, &l, sizeof (l)); @@ -160,19 +153,12 @@ gst_udp_set_loop (int sockfd, gboolean loop) } int -gst_udp_set_ttl (int sockfd, int ttl, gboolean is_multicast) +gst_udp_set_ttl (int sockfd, ADDRESS_FAMILY ss_family, int ttl, gboolean is_multicast) { - socklen_t socklen; - struct sockaddr_storage addr; int optname = -1; int ret = -1; - socklen = sizeof (addr); - if ((ret = getsockname (sockfd, (struct sockaddr *) &addr, &socklen)) < 0) { - return ret; - } - - switch (addr.ss_family) { + switch (ss_family) { case AF_INET: { optname = (is_multicast == TRUE) ? IP_MULTICAST_TTL : IP_TTL; diff --git a/gst/udp/gstudpnetutils.h b/gst/udp/gstudpnetutils.h index 4b8676f..3fe599a 100644 --- a/gst/udp/gstudpnetutils.h +++ b/gst/udp/gstudpnetutils.h @@ -86,8 +86,8 @@ int gst_udp_get_sockaddr_length (struct sockaddr_storage *addr); int gst_udp_get_addr (const char *hostname, int port, struct sockaddr_storage *addr); int gst_udp_is_multicast (struct sockaddr_storage *addr); -int gst_udp_set_loop (int sockfd, gboolean loop); -int gst_udp_set_ttl (int sockfd, int ttl, gboolean is_multicast); +int gst_udp_set_loop (int sockfd, ADDRESS_FAMILY ss_family, gboolean loop); +int gst_udp_set_ttl (int sockfd, ADDRESS_FAMILY ss_family, int ttl, gboolean is_multicast); /* multicast groups */ int gst_udp_join_group (int sockfd, struct sockaddr_storage *addr, -- 2.7.4