[svn r105] Changed the function that gets the net interface names, added some recoreder functions. trunk
authorrosfran
Thu Nov 23 20:51:44 2006 +0000 (2006-11-23)
branchtrunk
changeset 1041e1f1cb810fe
parent 103 c83e6f69f468
child 105 ba9bf90e7c4b
[svn r105] Changed the function that gets the net interface names, added some recoreder functions.
gmyth/configure.ac
gmyth/src/gmyth_file_transfer.c
gmyth/src/gmyth_recorder.c
gmyth/src/gmyth_recorder.h
gmyth/src/gmyth_socket.c
gmyth/src/gmyth_uri.c
gmyth/src/gmyth_uri.h
     1.1 --- a/gmyth/configure.ac	Wed Nov 22 20:23:03 2006 +0000
     1.2 +++ b/gmyth/configure.ac	Thu Nov 23 20:51:44 2006 +0000
     1.3 @@ -185,6 +185,14 @@
     1.4    AC_MSG_ERROR(you need gstreamer base development packages installed !)
     1.5  fi
     1.6  
     1.7 +##############################
     1.8 +# Checks for Network functions
     1.9 +##############################
    1.10 +
    1.11 +AC_CHECK_FUNCS([socket])
    1.12 +AC_CHECK_FUNCS([inet_ntoa])
    1.13 +AC_CHECK_HEADERS([ifaddrs.h])
    1.14 +AC_CHECK_FUNCS([getifaddrs])
    1.15  
    1.16  #
    1.17  # mysql libraries
    1.18 @@ -205,7 +213,6 @@
    1.19  #dnl Enable gtk-doc
    1.20  #GTK_DOC_CHECK(1.4)
    1.21  
    1.22 -
    1.23  AC_CONFIG_FILES([Makefile
    1.24                  src/Makefile
    1.25          		gmyth.pc])
     2.1 --- a/gmyth/src/gmyth_file_transfer.c	Wed Nov 22 20:23:03 2006 +0000
     2.2 +++ b/gmyth/src/gmyth_file_transfer.c	Thu Nov 23 20:51:44 2006 +0000
     2.3 @@ -439,7 +439,9 @@
     2.4        (*transfer)->sock = sock;
     2.5        strlist = gmyth_string_list_new();
     2.6        
     2.7 -      if ( gmyth_socket_get_protocol_version( (*transfer)->control_sock ) > 26 )      
     2.8 +      g_print( "[%s] Protocol version = %d\n", __FUNCTION__, 
     2.9 +      						gmyth_socket_get_protocol_version( (*transfer)->sock ) );      
    2.10 +      if ( gmyth_socket_get_protocol_version( (*transfer)->sock ) > 26 )      
    2.11        	g_string_printf( base_str, "ANN FileTransfer %s %d %d", hostname->str,
    2.12          		(*transfer)->userreadahead, (*transfer)->retries );
    2.13        else
    2.14 @@ -986,6 +988,7 @@
    2.15  	  			} 
    2.16  	  		}
    2.17      	} else {
    2.18 +    			//continue;
    2.19    				goto cleanup;	      		
    2.20      	} // if
    2.21    	}
     3.1 --- a/gmyth/src/gmyth_recorder.c	Wed Nov 22 20:23:03 2006 +0000
     3.2 +++ b/gmyth/src/gmyth_recorder.c	Thu Nov 23 20:51:44 2006 +0000
     3.3 @@ -206,6 +206,49 @@
     3.4  
     3.5  }
     3.6  
     3.7 +/** Sends the FRONTEND_READY command through Mythtv protocol. This command
     3.8 + * advertises the backend to start capturing TV content.
     3.9 + * 
    3.10 + * @param recorder The GMythRecorder instance.
    3.11 + * @return TRUE if success, FALSE if any error happens.
    3.12 + */
    3.13 +gboolean
    3.14 +gmyth_recorder_send_frontend_ready_command (GMythRecorder *recorder)
    3.15 +{
    3.16 +	GMythStringList *str_list;
    3.17 +	GString *tmp_str;
    3.18 +	
    3.19 +	g_debug ( "[%s] FRONTEND_READY with recorder id = %s", __FUNCTION__, recorder->recorder_num );
    3.20 +	
    3.21 +	str_list = gmyth_string_list_new ();
    3.22 +	
    3.23 +	tmp_str = g_string_new ("QUERY_RECORDER ");
    3.24 +	g_string_append_printf (tmp_str, "%d", recorder->recorder_num); 
    3.25 +	
    3.26 +	gmyth_string_list_append_string (str_list, tmp_str);
    3.27 +	gmyth_string_list_append_string (str_list, g_string_new ("FRONTEND_READY"));
    3.28 +
    3.29 +	gmyth_socket_sendreceive_stringlist (recorder->myth_socket, str_list);
    3.30 +	
    3.31 +  g_string_free (tmp_str, TRUE);
    3.32 +    
    3.33 +  tmp_str = gmyth_string_list_get_string (str_list, 0);
    3.34 +  if (tmp_str == NULL) {
    3.35 +  	g_warning ("[%s] FRONTEND_READY command request couldn't returns, reason: %s", __FUNCTION__, tmp_str->str);
    3.36 +  	return FALSE;
    3.37 +	}
    3.38 +	
    3.39 +  if (g_ascii_strncasecmp (tmp_str->str, "ok", 2)) {
    3.40 +  	g_warning ("[%s] FRONTEND_READY request returned %s", __FUNCTION__, tmp_str->str);
    3.41 +    g_object_unref (str_list);
    3.42 +  	return FALSE;
    3.43 +  }
    3.44 +
    3.45 +  g_object_unref (str_list);
    3.46 +  return TRUE;
    3.47 +
    3.48 +}
    3.49 +
    3.50  /** Send a CHECK_CHANNEL command request to the backend, in order to find if a 
    3.51   * certain channel actually exists.
    3.52   * 
     4.1 --- a/gmyth/src/gmyth_recorder.h	Wed Nov 22 20:23:03 2006 +0000
     4.2 +++ b/gmyth/src/gmyth_recorder.h	Thu Nov 23 20:51:44 2006 +0000
     4.3 @@ -84,6 +84,8 @@
     4.4                                           GString *tvchain_id);
     4.5  gboolean gmyth_recorder_stop_livetv (GMythRecorder *recorder);
     4.6  
     4.7 +gboolean gmyth_recorder_send_frontend_ready_command (GMythRecorder *recorder);
     4.8 +
     4.9  gboolean gmyth_recorder_check_channel (GMythRecorder *recorder, gint channel);
    4.10  
    4.11  gboolean gmyth_recorder_set_channel   (GMythRecorder *recorder,
     5.1 --- a/gmyth/src/gmyth_socket.c	Wed Nov 22 20:23:03 2006 +0000
     5.2 +++ b/gmyth/src/gmyth_socket.c	Thu Nov 23 20:51:44 2006 +0000
     5.3 @@ -37,12 +37,20 @@
     5.4  #include <sys/types.h>
     5.5  #include <sys/socket.h>
     5.6  #include <netdb.h>
     5.7 +#include <net/if.h>
     5.8  #include <errno.h>
     5.9  #include <stdlib.h>
    5.10  
    5.11 +#if defined(HAVE_IFADDRS_H)
    5.12 +	#include <ifaddrs.h>
    5.13 +#else
    5.14 +	#include <sys/ioctl.h>
    5.15 +#endif
    5.16 +
    5.17  #include "gmyth_socket.h"
    5.18  #include "gmyth_stringlist.h"
    5.19  #include "gmyth_context.h"
    5.20 +#include "gmyth_uri.h"
    5.21  
    5.22  #define BUFLEN 				        			512
    5.23  #define MYTH_SEPARATOR 			    		"[]:[]"
    5.24 @@ -106,6 +114,148 @@
    5.25      return errorn;
    5.26  }
    5.27  
    5.28 +static gint
    5.29 +find_match_address_uri( GMythURI* uri, gchar *address ) {
    5.30 +
    5.31 +        if ( g_ascii_strcasecmp( gmyth_uri_gethost( uri ), address ) == 0 ) {
    5.32 +                //g_printerr( "Found URI: %s !!!\n", rui_uri_getvalue(uri) );
    5.33 +                return 0;
    5.34 +        } else {
    5.35 +                return -1;
    5.36 +        }
    5.37 +
    5.38 +}
    5.39 +
    5.40 +#if defined(HAVE_IFADDRS_H)
    5.41 +
    5.42 +GList *
    5.43 +get_local_addrs( GList *current_connections ) {
    5.44 +	
    5.45 +	GList *local_addrs = NULL;
    5.46 +	
    5.47 +	struct ifaddrs *ifaddr;	
    5.48 +	struct ifaddrs *i;
    5.49 +
    5.50 +	gchar addr[NI_MAXHOST+1];
    5.51 +	gchar *ifname;
    5.52 +	gint ifIdx;
    5.53 +	
    5.54 +	if (getifaddrs(&ifaddr) != 0)
    5.55 +	{
    5.56 +		g_printerr("No addresses for interfaces!\n");
    5.57 +		return NULL;
    5.58 +	}
    5.59 +	
    5.60 +	for ( i = ifaddr; i != NULL; i = i->ifa_next ) {
    5.61 +		if (!(i->ifa_flags & IFF_UP))
    5.62 +			continue;
    5.63 +		if (i->ifa_flags & IFF_LOOPBACK)
    5.64 +			continue;
    5.65 +		if ( getnameinfo(i->ifa_addr, sizeof(struct sockaddr), addr, NI_MAXHOST, NULL, 0, NI_NUMERICHOST) == 0 ) {
    5.66 +
    5.67 +			ifname = i->ifa_name;
    5.68 +			ifIdx = if_nametoindex(ifname);
    5.69 +			g_print( "[%s] Interface name: %s, index: %d, address: %s\n", __FUNCTION__, ifname, ifIdx, addr );
    5.70 +			if ( current_connections == NULL || ( current_connections != NULL && 
    5.71 +					g_list_find_custom( current_connections, (gchar *)addr, 
    5.72 +						(GCompareFunc)find_match_address_uri ) == NULL ) )
    5.73 +				local_addrs = g_list_append( local_addrs, g_strdup( addr ) );
    5.74 +
    5.75 +		}
    5.76 +	} // iterates over network interfaces
    5.77 +	
    5.78 +	freeifaddrs(ifaddr);
    5.79 +	
    5.80 +	return local_addrs;
    5.81 +
    5.82 +}
    5.83 +
    5.84 +#else
    5.85 +
    5.86 +static const char *PATH_PROC_NET_DEV = "/proc/net/dev";
    5.87 +
    5.88 +GList *
    5.89 +get_local_addrs( GList *current_connections )
    5.90 +{
    5.91 +
    5.92 +	GList *local_addrs = NULL;
    5.93 +	FILE *fd;
    5.94 +	gint s;
    5.95 +	gchar buffer[256+1];
    5.96 +	gchar ifaddr[20+1];
    5.97 +	gchar *ifname;
    5.98 +	gchar *sep;
    5.99 +	
   5.100 +	s = socket(AF_INET, SOCK_DGRAM, 0);
   5.101 +	if (s < 0)
   5.102 +		return 0;
   5.103 +	fd = fopen(PATH_PROC_NET_DEV, "r");
   5.104 +	fgets(buffer, sizeof(buffer)-1, fd);
   5.105 +	fgets(buffer, sizeof(buffer)-1, fd);
   5.106 +	while (!feof(fd)) {
   5.107 +		ifname = buffer;
   5.108 +		sep;
   5.109 +		if (fgets(buffer, sizeof(buffer)-1, fd) == NULL)
   5.110 +			break;
   5.111 +		sep = strrchr(buffer, ':');
   5.112 +		if (sep)
   5.113 +			*sep = 0;
   5.114 +		while (*ifname == ' ')
   5.115 +			ifname++;
   5.116 +		struct ifreq req;
   5.117 +		strcpy(req.ifr_name, ifname);
   5.118 +		if (ioctl(s, SIOCGIFFLAGS, &req) < 0)
   5.119 +			continue;
   5.120 +		if (!(req.ifr_flags & IFF_UP))
   5.121 +			continue;
   5.122 +		if (req.ifr_flags & IFF_LOOPBACK)
   5.123 +			continue;
   5.124 +		if (ioctl(s, SIOCGIFADDR, &req) < 0)
   5.125 +			continue;
   5.126 +		g_strlcpy( ifaddr, inet_ntoa(((struct sockaddr_in*)&req.ifr_addr)->sin_addr), sizeof(struct ifaddr)-1 );
   5.127 +		local_addrs = g_list_append( local_addrs, g_strdup( ifaddr ) );
   5.128 +
   5.129 +		g_print( "[%s] ( from the /proc/net/dev) Interface name: %s, address: %s\n", __FUNCTION__, 
   5.130 +								ifname, ifaddr );
   5.131 +	}
   5.132 +	fclose(fd);
   5.133 +	close(s);
   5.134 +	return local_addrs;
   5.135 +
   5.136 +}
   5.137 +
   5.138 +#endif
   5.139 +
   5.140 +/**
   5.141 + * Get only the local addresses from the primary interface
   5.142 + */
   5.143 +gchar *
   5.144 +get_primary_addr()
   5.145 +{
   5.146 +	
   5.147 +	gchar *if_eth0 = g_new0( gchar, sizeof(struct ifaddr)-1 );
   5.148 +	GList *if_tmp = NULL;
   5.149 +	
   5.150 +	g_print( "[%s] net if size = %d.\n", __FUNCTION__, sizeof(struct ifaddr)-1 );
   5.151 +	
   5.152 +	GList *interfs = get_local_addrs( NULL );
   5.153 +	
   5.154 +	if ( interfs != NULL && ( g_list_length( interfs ) > 0 ) ) 
   5.155 +	{
   5.156 +		/* get the first occurrence (primary interface) */
   5.157 +		if_tmp = g_list_first( interfs );
   5.158 +		
   5.159 +		if ( if_tmp != NULL )
   5.160 +			g_strlcpy( if_eth0, g_strdup( (gchar *)if_tmp->data ), sizeof(struct ifaddr)-1 );
   5.161 +
   5.162 +	}
   5.163 +	
   5.164 +	if ( interfs != NULL )
   5.165 +		g_list_free( interfs );
   5.166 +	
   5.167 +	return if_eth0;
   5.168 +}
   5.169 +
   5.170  /** This function retrieves the local hostname of the 
   5.171   * client machine.
   5.172   *
   5.173 @@ -114,7 +264,7 @@
   5.174  GString *
   5.175  gmyth_socket_get_local_hostname( )
   5.176  {
   5.177 -    GString *str = g_string_new("");
   5.178 +    GString *str = g_string_sized_new( 1024 );
   5.179  
   5.180      gchar *localhostname = g_strnfill( 1024, ' ' );
   5.181      gchar *localaddr = g_strdup( "127.0.0.1" );
   5.182 @@ -152,10 +302,15 @@
   5.183      if ( found_addr == FALSE ) {
   5.184  	    g_warning("[%s] Could not determine the local hostname address. Setting to %s\n",
   5.185                      __FUNCTION__, localhostname );
   5.186 -    	//if ( localaddr != NULL )
   5.187 -	//        str = g_string_assign( str, localaddr );
   5.188 -    	//else
   5.189 -	str = g_string_assign( str, g_strdup( localhostname ) );
   5.190 +                    
   5.191 +      gchar *prim_addr = get_primary_addr();
   5.192 +      
   5.193 +    	if ( prim_addr != NULL ) {
   5.194 +	        str = g_string_assign( str, g_strdup( prim_addr ) );
   5.195 +	        g_free( prim_addr );
   5.196 +    	} else {
   5.197 +					str = g_string_assign( str, g_strdup( localhostname ) );
   5.198 +    	}
   5.199      }
   5.200  
   5.201      g_static_mutex_unlock( &mutex );
     6.1 --- a/gmyth/src/gmyth_uri.c	Wed Nov 22 20:23:03 2006 +0000
     6.2 +++ b/gmyth/src/gmyth_uri.c	Thu Nov 23 20:51:44 2006 +0000
     6.3 @@ -206,3 +206,11 @@
     6.4  	return uri;
     6.5  
     6.6  }
     6.7 +
     6.8 +gboolean
     6.9 +gmyth_uri_is_equals( GMythURI* uri1, GMythURI* uri2 )
    6.10 +{
    6.11 +	return ( g_ascii_strcasecmp( gmyth_uri_gethost( uri1 ), gmyth_uri_gethost( uri2 ) ) == 0 &&
    6.12 +				gmyth_uri_getport( uri1 ) == gmyth_uri_getport( uri2 ) );
    6.13 +}
    6.14 +
     7.1 --- a/gmyth/src/gmyth_uri.h	Wed Nov 22 20:23:03 2006 +0000
     7.2 +++ b/gmyth/src/gmyth_uri.h	Thu Nov 23 20:51:44 2006 +0000
     7.3 @@ -55,6 +55,8 @@
     7.4  
     7.5  const GMythURI *gmyth_uri_new( gchar *value );
     7.6  
     7.7 +gboolean gmyth_uri_is_equals( GMythURI* uri1, GMythURI* uri2 );
     7.8 +
     7.9  #define gmyth_uri_gethost(urip) (urip->host->str)
    7.10  #define gmyth_uri_getport(urip) (urip->port)
    7.11  #define gmyth_uri_getprotocol(urip) (urip->protocol->str)