1 /* Farstream unit tests generic utilities
2  *
3  * Copyright (C) 2007 Collabora, Nokia
4  * @author: Olivier Crete <olivier.crete@collabora.co.uk>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
19 */
20 
21 #ifdef HAVE_CONFIG_H
22 # include <config.h>
23 #endif
24 
25 #include <gst/check/gstcheck.h>
26 
27 #include "testutils.h"
28 
29 #ifdef HAVE_GETIFADDRS
30  #include <sys/socket.h>
31  #include <ifaddrs.h>
32  #include <net/if.h>
33  #include <netinet/in.h>
34  #include <arpa/inet.h>
35 #endif
36 
37 gchar *
find_multicast_capable_address(void)38 find_multicast_capable_address (void)
39 {
40 #ifdef HAVE_GETIFADDRS
41   gchar *retval = NULL;
42   struct ifaddrs *ifa, *results;
43 
44   if (getifaddrs (&results) < 0)
45     return NULL;
46 
47   for (ifa = results; ifa; ifa = ifa->ifa_next) {
48     /* no ip address from interface that is down */
49     if ((ifa->ifa_flags & IFF_UP) == 0)
50       continue;
51 
52     if ((ifa->ifa_flags & IFF_MULTICAST) == 0)
53       continue;
54 
55     if ((ifa->ifa_flags & IFF_RUNNING) == 0)
56       continue;
57 
58     if (ifa->ifa_addr == NULL || ifa->ifa_addr->sa_family != AF_INET)
59       continue;
60 
61     if (retval)
62     {
63       g_free (retval);
64       retval = NULL;
65       GST_DEBUG ("Disabling test, more than one multicast capable interface");
66       break;
67     }
68 
69     retval = g_strdup (
70         inet_ntoa (((struct sockaddr_in *) ifa->ifa_addr)->sin_addr));
71     GST_DEBUG ("Sending from %s on interface %s", retval, ifa->ifa_name);
72   }
73 
74   freeifaddrs (results);
75 
76   if (retval == NULL)
77     g_message ("Skipping multicast transmitter tests, "
78         "no multicast capable interface found");
79   return retval;
80 
81 #else
82   g_message ("This system does not have getifaddrs,"
83       " this test will be disabled");
84   return NULL;
85 #endif
86 }
87 
88 gchar *
get_fullpath(const gchar * filename)89 get_fullpath (const gchar *filename)
90 {
91   if (g_getenv ("SRCDIR"))
92     return g_strdup_printf ("%s/%s", g_getenv ("SRCDIR"), filename);
93   else
94     return g_strdup (filename);
95 }
96