udp: Remove now unecessary code

This commit is contained in:
Sebastian Dröge 2012-01-17 11:18:33 +01:00
parent bb38a849e5
commit 2ea3ef0c1c
3 changed files with 6 additions and 451 deletions

View file

@ -31,11 +31,6 @@
static gboolean
plugin_init (GstPlugin * plugin)
{
#ifdef G_OS_WIN32
if (!gst_udp_net_utils_win32_wsa_startup (GST_OBJECT (plugin)))
return FALSE;
#endif
/* register info of the netaddress metadata so that we can use it from
* multiple threads right away. Note that the plugin loading is always
* serialized */

View file

@ -23,350 +23,11 @@
#include "config.h"
#endif
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <gst/gst.h>
#include <string.h>
#include "gstudpnetutils.h"
/* EAI_ADDRFAMILY was obsoleted in BSD at some point */
#ifndef EAI_ADDRFAMILY
#define EAI_ADDRFAMILY 1
#endif
#ifdef G_OS_WIN32
gboolean
gst_udp_net_utils_win32_wsa_startup (GstObject * obj)
{
WSADATA w;
int error;
error = WSAStartup (0x0202, &w);
if (error) {
GST_WARNING_OBJECT (obj, "WSAStartup error: %d", error);
return FALSE;
}
if (w.wVersion != 0x0202) {
WSACleanup ();
GST_WARNING_OBJECT (obj, "Winsock version wrong : 0x%x", w.wVersion);
return FALSE;
}
return TRUE;
}
#endif
int
gst_udp_get_sockaddr_length (struct sockaddr_storage *addr)
{
/* MacOS is picky about passing precisely the correct length,
* so we calculate it here for the given socket type.
*/
switch (addr->ss_family) {
case AF_INET:
return sizeof (struct sockaddr_in);
case AF_INET6:
return sizeof (struct sockaddr_in6);
default:
/* don't know, Screw MacOS and use the full length */
return sizeof (*addr);
}
}
int
gst_udp_get_addr (const char *hostname, int port, struct sockaddr_storage *addr)
{
struct addrinfo hints, *res = NULL, *nres;
char service[NI_MAXSERV];
int ret;
memset (&hints, 0, sizeof (hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;
g_snprintf (service, sizeof (service) - 1, "%d", port);
service[sizeof (service) - 1] = '\0';
if ((ret = getaddrinfo (hostname, (port == -1) ? NULL : service, &hints,
&res)) < 0) {
goto beach;
}
nres = res;
while (nres) {
if (nres->ai_family == AF_INET || nres->ai_family == AF_INET6)
break;
nres = nres->ai_next;
}
if (nres) {
memcpy (addr, nres->ai_addr, nres->ai_addrlen);
} else {
ret = EAI_ADDRFAMILY;
}
freeaddrinfo (res);
beach:
return ret;
}
int
gst_udp_set_loop (int sockfd, guint16 ss_family, gboolean loop)
{
int ret = -1;
int l = (loop == FALSE) ? 0 : 1;
switch (ss_family) {
case AF_INET:
{
ret = setsockopt (sockfd, IPPROTO_IP, IP_MULTICAST_LOOP, &l, sizeof (l));
if (ret < 0)
return ret;
break;
}
case AF_INET6:
{
ret =
setsockopt (sockfd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &l,
sizeof (l));
if (ret < 0)
return ret;
break;
}
default:
#ifdef G_OS_WIN32
WSASetLastError (WSAEAFNOSUPPORT);
#else
errno = EAFNOSUPPORT;
#endif
}
return ret;
}
int
gst_udp_set_ttl (int sockfd, guint16 ss_family, int ttl, gboolean is_multicast)
{
int optname = -1;
int ret = -1;
switch (ss_family) {
case AF_INET:
{
optname = (is_multicast == TRUE) ? IP_MULTICAST_TTL : IP_TTL;
ret = setsockopt (sockfd, IPPROTO_IP, optname, &ttl, sizeof (ttl));
if (ret < 0)
return ret;
break;
}
case AF_INET6:
{
optname =
(is_multicast == TRUE) ? IPV6_MULTICAST_HOPS : IPV6_UNICAST_HOPS;
ret = setsockopt (sockfd, IPPROTO_IPV6, optname, &ttl, sizeof (ttl));
if (ret < 0)
return ret;
/* When using IPV4 address with IPV6 socket, both TTL values
must be set in order to actually use the given value.
Has no effect when IPV6 address is used. */
optname = (is_multicast == TRUE) ? IP_MULTICAST_TTL : IP_TTL;
ret = setsockopt (sockfd, IPPROTO_IP, optname, &ttl, sizeof (ttl));
if (ret < 0)
return ret;
break;
}
default:
#ifdef G_OS_WIN32
WSASetLastError (WSAEAFNOSUPPORT);
#else
errno = EAFNOSUPPORT;
#endif
}
return ret;
}
/* FIXME: Add interface selection for windows hosts. */
int
gst_udp_join_group (int sockfd, struct sockaddr_storage *addr, gchar * iface)
{
int ret = -1;
switch (addr->ss_family) {
case AF_INET:
{
#ifdef HAVE_IP_MREQN
struct ip_mreqn mreq4;
#else
struct ip_mreq mreq4;
#endif
memset (&mreq4, 0, sizeof (mreq4));
mreq4.imr_multiaddr.s_addr =
((struct sockaddr_in *) addr)->sin_addr.s_addr;
#ifdef HAVE_IP_MREQN
if (iface)
mreq4.imr_ifindex = if_nametoindex (iface);
else
mreq4.imr_ifindex = 0; /* Pick any. */
#else
mreq4.imr_interface.s_addr = INADDR_ANY;
#endif
if ((ret =
setsockopt (sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
(const void *) &mreq4, sizeof (mreq4))) < 0)
return ret;
break;
}
case AF_INET6:
{
struct ipv6_mreq mreq6;
memset (&mreq6, 0, sizeof (mreq6));
memcpy (&mreq6.ipv6mr_multiaddr,
&(((struct sockaddr_in6 *) addr)->sin6_addr),
sizeof (struct in6_addr));
mreq6.ipv6mr_interface = 0;
#if !defined(G_OS_WIN32)
if (iface)
mreq6.ipv6mr_interface = if_nametoindex (iface);
#endif
if ((ret =
setsockopt (sockfd, IPPROTO_IPV6, IPV6_JOIN_GROUP,
(const void *) &mreq6, sizeof (mreq6))) < 0)
return ret;
break;
}
default:
#ifdef G_OS_WIN32
WSASetLastError (WSAEAFNOSUPPORT);
#else
errno = EAFNOSUPPORT;
#endif
}
return ret;
}
int
gst_udp_leave_group (int sockfd, struct sockaddr_storage *addr)
{
int ret = -1;
switch (addr->ss_family) {
case AF_INET:
{
struct ip_mreq mreq4;
memset (&mreq4, 0, sizeof (mreq4));
mreq4.imr_multiaddr.s_addr =
((struct sockaddr_in *) addr)->sin_addr.s_addr;
mreq4.imr_interface.s_addr = INADDR_ANY;
if ((ret =
setsockopt (sockfd, IPPROTO_IP, IP_DROP_MEMBERSHIP,
(const void *) &mreq4, sizeof (mreq4))) < 0)
return ret;
}
break;
case AF_INET6:
{
struct ipv6_mreq mreq6;
memset (&mreq6, 0, sizeof (mreq6));
memcpy (&mreq6.ipv6mr_multiaddr,
&(((struct sockaddr_in6 *) addr)->sin6_addr),
sizeof (struct in6_addr));
mreq6.ipv6mr_interface = 0;
if ((ret =
setsockopt (sockfd, IPPROTO_IPV6, IPV6_LEAVE_GROUP,
(const void *) &mreq6, sizeof (mreq6))) < 0)
return ret;
}
break;
default:
#ifdef G_OS_WIN32
WSASetLastError (WSAEAFNOSUPPORT);
#else
errno = EAFNOSUPPORT;
#endif
}
return ret;
}
int
gst_udp_is_multicast (struct sockaddr_storage *addr)
{
int ret = -1;
switch (addr->ss_family) {
case AF_INET:
{
struct sockaddr_in *addr4 = (struct sockaddr_in *) addr;
ret = IN_MULTICAST (g_ntohl (addr4->sin_addr.s_addr));
}
break;
case AF_INET6:
{
struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *) addr;
ret = IN6_IS_ADDR_MULTICAST (&addr6->sin6_addr);
}
break;
default:
#ifdef G_OS_WIN32
WSASetLastError (WSAEAFNOSUPPORT);
#else
errno = EAFNOSUPPORT;
#endif
}
return ret;
}
void
gst_udp_uri_init (GstUDPUri * uri, const gchar * host, gint port)
{
uri->host = NULL;
uri->port = -1;
gst_udp_uri_update (uri, host, port);
}
int
gst_udp_uri_update (GstUDPUri * uri, const gchar * host, gint port)
{
if (host) {
g_free (uri->host);
uri->host = g_strdup (host);
if (strchr (host, ':'))
uri->is_ipv6 = TRUE;
else
uri->is_ipv6 = FALSE;
}
if (port != -1)
uri->port = port;
return 0;
}
gboolean
gst_udp_parse_uri (const gchar * uristr, gchar ** host, guint16 * port)
{
@ -417,7 +78,9 @@ gst_udp_parse_uri (const gchar * uristr, gchar ** host, guint16 * port)
GST_DEBUG ("host set to '%s'", *host);
if (colptr != NULL) {
*port = atoi (colptr + 1);
*port = g_ascii_strtoll (colptr + 1, NULL, 10);
} else {
*port = 0;
}
g_free (location_start);
@ -443,24 +106,3 @@ wrong_address:
return FALSE;
}
}
gchar *
gst_udp_uri_string (GstUDPUri * uri)
{
gchar *result;
if (uri->is_ipv6) {
result = g_strdup_printf ("udp://[%s]:%d", uri->host, uri->port);
} else {
result = g_strdup_printf ("udp://%s:%d", uri->host, uri->port);
}
return result;
}
void
gst_udp_uri_free (GstUDPUri * uri)
{
g_free (uri->host);
uri->host = NULL;
uri->port = -1;
}

View file

@ -18,93 +18,11 @@
* Boston, MA 02111-1307, USA.
*/
#include <gst/gst.h>
#ifndef __GST_UDP_NET_UTILS_H__
#define __GST_UDP_NET_UTILS_H__
#include <sys/types.h>
/* Needed for G_OS_XXXX */
#include <glib.h>
#ifdef G_OS_WIN32
/* ws2_32.dll has getaddrinfo and freeaddrinfo on Windows XP and later.
* minwg32 headers check WINVER before allowing the use of these */
#ifndef WINVER
#define WINVER 0x0501
#endif
#include <winsock2.h>
#include <ws2tcpip.h>
#ifndef socklen_t
#define socklen_t int
#endif
/* Needed for GstObject and GST_WARNING_OBJECT */
#include <gst/gstobject.h>
#include <gst/gstinfo.h>
#else
#include <sys/time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <net/if.h>
#include <netdb.h>
#include <sys/wait.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <sys/ioctl.h>
#endif
#include <fcntl.h>
#ifdef G_OS_WIN32
#define IOCTL_SOCKET ioctlsocket
#define CLOSE_SOCKET(sock) closesocket(sock)
#define setsockopt(sock,l,opt,val,len) setsockopt(sock,l,opt,(char *)(val),len)
#define WSA_STARTUP(obj) gst_udp_net_utils_win32_wsa_startup(GST_OBJECT(obj))
#define WSA_CLEANUP(obj) WSACleanup ()
#else
#define IOCTL_SOCKET ioctl
#define CLOSE_SOCKET(sock) close(sock)
#define setsockopt(sock,l,opt,val,len) setsockopt(sock,l,opt,(void *)(val),len)
#define WSA_STARTUP(obj)
#define WSA_CLEANUP(obj)
#endif
#ifdef G_OS_WIN32
gboolean gst_udp_net_utils_win32_wsa_startup (GstObject * obj);
#endif
typedef struct {
gchar *host;
guint16 port;
gboolean is_ipv6;
} GstUDPUri;
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, guint16 ss_family, gboolean loop);
int gst_udp_set_ttl (int sockfd, guint16 ss_family, int ttl, gboolean is_multicast);
/* multicast groups */
int gst_udp_join_group (int sockfd, struct sockaddr_storage *addr,
gchar *iface);
int gst_udp_leave_group (int sockfd, struct sockaddr_storage *addr);
/* uri handling */
void gst_udp_uri_init (GstUDPUri *uri, const gchar *host, gint port);
int gst_udp_uri_update (GstUDPUri *uri, const gchar *host, gint port);
gchar * gst_udp_uri_string (GstUDPUri *uri);
void gst_udp_uri_free (GstUDPUri *uri);
gboolean gst_udp_parse_uri (const gchar *uristr, gchar **host, guint16 *port);
#endif /* __GST_UDP_NET_UTILS_H__*/