# HG changeset patch # User rosfran # Date 1164315104 0 # Node ID 1e1f1cb810fe2632b99f891598625ce4915b98e8 # Parent c83e6f69f4683b2a28677c948bd7c19ec9e7efa4 [svn r105] Changed the function that gets the net interface names, added some recoreder functions. diff -r c83e6f69f468 -r 1e1f1cb810fe gmyth/configure.ac --- a/gmyth/configure.ac Wed Nov 22 20:23:03 2006 +0000 +++ b/gmyth/configure.ac Thu Nov 23 20:51:44 2006 +0000 @@ -185,6 +185,14 @@ AC_MSG_ERROR(you need gstreamer base development packages installed !) fi +############################## +# Checks for Network functions +############################## + +AC_CHECK_FUNCS([socket]) +AC_CHECK_FUNCS([inet_ntoa]) +AC_CHECK_HEADERS([ifaddrs.h]) +AC_CHECK_FUNCS([getifaddrs]) # # mysql libraries @@ -205,7 +213,6 @@ #dnl Enable gtk-doc #GTK_DOC_CHECK(1.4) - AC_CONFIG_FILES([Makefile src/Makefile gmyth.pc]) diff -r c83e6f69f468 -r 1e1f1cb810fe gmyth/src/gmyth_file_transfer.c --- a/gmyth/src/gmyth_file_transfer.c Wed Nov 22 20:23:03 2006 +0000 +++ b/gmyth/src/gmyth_file_transfer.c Thu Nov 23 20:51:44 2006 +0000 @@ -439,7 +439,9 @@ (*transfer)->sock = sock; strlist = gmyth_string_list_new(); - if ( gmyth_socket_get_protocol_version( (*transfer)->control_sock ) > 26 ) + g_print( "[%s] Protocol version = %d\n", __FUNCTION__, + gmyth_socket_get_protocol_version( (*transfer)->sock ) ); + if ( gmyth_socket_get_protocol_version( (*transfer)->sock ) > 26 ) g_string_printf( base_str, "ANN FileTransfer %s %d %d", hostname->str, (*transfer)->userreadahead, (*transfer)->retries ); else @@ -986,6 +988,7 @@ } } } else { + //continue; goto cleanup; } // if } diff -r c83e6f69f468 -r 1e1f1cb810fe gmyth/src/gmyth_recorder.c --- a/gmyth/src/gmyth_recorder.c Wed Nov 22 20:23:03 2006 +0000 +++ b/gmyth/src/gmyth_recorder.c Thu Nov 23 20:51:44 2006 +0000 @@ -206,6 +206,49 @@ } +/** Sends the FRONTEND_READY command through Mythtv protocol. This command + * advertises the backend to start capturing TV content. + * + * @param recorder The GMythRecorder instance. + * @return TRUE if success, FALSE if any error happens. + */ +gboolean +gmyth_recorder_send_frontend_ready_command (GMythRecorder *recorder) +{ + GMythStringList *str_list; + GString *tmp_str; + + g_debug ( "[%s] FRONTEND_READY with recorder id = %s", __FUNCTION__, recorder->recorder_num ); + + str_list = gmyth_string_list_new (); + + tmp_str = g_string_new ("QUERY_RECORDER "); + g_string_append_printf (tmp_str, "%d", recorder->recorder_num); + + gmyth_string_list_append_string (str_list, tmp_str); + gmyth_string_list_append_string (str_list, g_string_new ("FRONTEND_READY")); + + gmyth_socket_sendreceive_stringlist (recorder->myth_socket, str_list); + + g_string_free (tmp_str, TRUE); + + tmp_str = gmyth_string_list_get_string (str_list, 0); + if (tmp_str == NULL) { + g_warning ("[%s] FRONTEND_READY command request couldn't returns, reason: %s", __FUNCTION__, tmp_str->str); + return FALSE; + } + + if (g_ascii_strncasecmp (tmp_str->str, "ok", 2)) { + g_warning ("[%s] FRONTEND_READY request returned %s", __FUNCTION__, tmp_str->str); + g_object_unref (str_list); + return FALSE; + } + + g_object_unref (str_list); + return TRUE; + +} + /** Send a CHECK_CHANNEL command request to the backend, in order to find if a * certain channel actually exists. * diff -r c83e6f69f468 -r 1e1f1cb810fe gmyth/src/gmyth_recorder.h --- a/gmyth/src/gmyth_recorder.h Wed Nov 22 20:23:03 2006 +0000 +++ b/gmyth/src/gmyth_recorder.h Thu Nov 23 20:51:44 2006 +0000 @@ -84,6 +84,8 @@ GString *tvchain_id); gboolean gmyth_recorder_stop_livetv (GMythRecorder *recorder); +gboolean gmyth_recorder_send_frontend_ready_command (GMythRecorder *recorder); + gboolean gmyth_recorder_check_channel (GMythRecorder *recorder, gint channel); gboolean gmyth_recorder_set_channel (GMythRecorder *recorder, diff -r c83e6f69f468 -r 1e1f1cb810fe gmyth/src/gmyth_socket.c --- a/gmyth/src/gmyth_socket.c Wed Nov 22 20:23:03 2006 +0000 +++ b/gmyth/src/gmyth_socket.c Thu Nov 23 20:51:44 2006 +0000 @@ -37,12 +37,20 @@ #include #include #include +#include #include #include +#if defined(HAVE_IFADDRS_H) + #include +#else + #include +#endif + #include "gmyth_socket.h" #include "gmyth_stringlist.h" #include "gmyth_context.h" +#include "gmyth_uri.h" #define BUFLEN 512 #define MYTH_SEPARATOR "[]:[]" @@ -106,6 +114,148 @@ return errorn; } +static gint +find_match_address_uri( GMythURI* uri, gchar *address ) { + + if ( g_ascii_strcasecmp( gmyth_uri_gethost( uri ), address ) == 0 ) { + //g_printerr( "Found URI: %s !!!\n", rui_uri_getvalue(uri) ); + return 0; + } else { + return -1; + } + +} + +#if defined(HAVE_IFADDRS_H) + +GList * +get_local_addrs( GList *current_connections ) { + + GList *local_addrs = NULL; + + struct ifaddrs *ifaddr; + struct ifaddrs *i; + + gchar addr[NI_MAXHOST+1]; + gchar *ifname; + gint ifIdx; + + if (getifaddrs(&ifaddr) != 0) + { + g_printerr("No addresses for interfaces!\n"); + return NULL; + } + + for ( i = ifaddr; i != NULL; i = i->ifa_next ) { + if (!(i->ifa_flags & IFF_UP)) + continue; + if (i->ifa_flags & IFF_LOOPBACK) + continue; + if ( getnameinfo(i->ifa_addr, sizeof(struct sockaddr), addr, NI_MAXHOST, NULL, 0, NI_NUMERICHOST) == 0 ) { + + ifname = i->ifa_name; + ifIdx = if_nametoindex(ifname); + g_print( "[%s] Interface name: %s, index: %d, address: %s\n", __FUNCTION__, ifname, ifIdx, addr ); + if ( current_connections == NULL || ( current_connections != NULL && + g_list_find_custom( current_connections, (gchar *)addr, + (GCompareFunc)find_match_address_uri ) == NULL ) ) + local_addrs = g_list_append( local_addrs, g_strdup( addr ) ); + + } + } // iterates over network interfaces + + freeifaddrs(ifaddr); + + return local_addrs; + +} + +#else + +static const char *PATH_PROC_NET_DEV = "/proc/net/dev"; + +GList * +get_local_addrs( GList *current_connections ) +{ + + GList *local_addrs = NULL; + FILE *fd; + gint s; + gchar buffer[256+1]; + gchar ifaddr[20+1]; + gchar *ifname; + gchar *sep; + + s = socket(AF_INET, SOCK_DGRAM, 0); + if (s < 0) + return 0; + fd = fopen(PATH_PROC_NET_DEV, "r"); + fgets(buffer, sizeof(buffer)-1, fd); + fgets(buffer, sizeof(buffer)-1, fd); + while (!feof(fd)) { + ifname = buffer; + sep; + if (fgets(buffer, sizeof(buffer)-1, fd) == NULL) + break; + sep = strrchr(buffer, ':'); + if (sep) + *sep = 0; + while (*ifname == ' ') + ifname++; + struct ifreq req; + strcpy(req.ifr_name, ifname); + if (ioctl(s, SIOCGIFFLAGS, &req) < 0) + continue; + if (!(req.ifr_flags & IFF_UP)) + continue; + if (req.ifr_flags & IFF_LOOPBACK) + continue; + if (ioctl(s, SIOCGIFADDR, &req) < 0) + continue; + g_strlcpy( ifaddr, inet_ntoa(((struct sockaddr_in*)&req.ifr_addr)->sin_addr), sizeof(struct ifaddr)-1 ); + local_addrs = g_list_append( local_addrs, g_strdup( ifaddr ) ); + + g_print( "[%s] ( from the /proc/net/dev) Interface name: %s, address: %s\n", __FUNCTION__, + ifname, ifaddr ); + } + fclose(fd); + close(s); + return local_addrs; + +} + +#endif + +/** + * Get only the local addresses from the primary interface + */ +gchar * +get_primary_addr() +{ + + gchar *if_eth0 = g_new0( gchar, sizeof(struct ifaddr)-1 ); + GList *if_tmp = NULL; + + g_print( "[%s] net if size = %d.\n", __FUNCTION__, sizeof(struct ifaddr)-1 ); + + GList *interfs = get_local_addrs( NULL ); + + if ( interfs != NULL && ( g_list_length( interfs ) > 0 ) ) + { + /* get the first occurrence (primary interface) */ + if_tmp = g_list_first( interfs ); + + if ( if_tmp != NULL ) + g_strlcpy( if_eth0, g_strdup( (gchar *)if_tmp->data ), sizeof(struct ifaddr)-1 ); + + } + + if ( interfs != NULL ) + g_list_free( interfs ); + + return if_eth0; +} + /** This function retrieves the local hostname of the * client machine. * @@ -114,7 +264,7 @@ GString * gmyth_socket_get_local_hostname( ) { - GString *str = g_string_new(""); + GString *str = g_string_sized_new( 1024 ); gchar *localhostname = g_strnfill( 1024, ' ' ); gchar *localaddr = g_strdup( "127.0.0.1" ); @@ -152,10 +302,15 @@ if ( found_addr == FALSE ) { g_warning("[%s] Could not determine the local hostname address. Setting to %s\n", __FUNCTION__, localhostname ); - //if ( localaddr != NULL ) - // str = g_string_assign( str, localaddr ); - //else - str = g_string_assign( str, g_strdup( localhostname ) ); + + gchar *prim_addr = get_primary_addr(); + + if ( prim_addr != NULL ) { + str = g_string_assign( str, g_strdup( prim_addr ) ); + g_free( prim_addr ); + } else { + str = g_string_assign( str, g_strdup( localhostname ) ); + } } g_static_mutex_unlock( &mutex ); diff -r c83e6f69f468 -r 1e1f1cb810fe gmyth/src/gmyth_uri.c --- a/gmyth/src/gmyth_uri.c Wed Nov 22 20:23:03 2006 +0000 +++ b/gmyth/src/gmyth_uri.c Thu Nov 23 20:51:44 2006 +0000 @@ -206,3 +206,11 @@ return uri; } + +gboolean +gmyth_uri_is_equals( GMythURI* uri1, GMythURI* uri2 ) +{ + return ( g_ascii_strcasecmp( gmyth_uri_gethost( uri1 ), gmyth_uri_gethost( uri2 ) ) == 0 && + gmyth_uri_getport( uri1 ) == gmyth_uri_getport( uri2 ) ); +} + diff -r c83e6f69f468 -r 1e1f1cb810fe gmyth/src/gmyth_uri.h --- a/gmyth/src/gmyth_uri.h Wed Nov 22 20:23:03 2006 +0000 +++ b/gmyth/src/gmyth_uri.h Thu Nov 23 20:51:44 2006 +0000 @@ -55,6 +55,8 @@ const GMythURI *gmyth_uri_new( gchar *value ); +gboolean gmyth_uri_is_equals( GMythURI* uri1, GMythURI* uri2 ); + #define gmyth_uri_gethost(urip) (urip->host->str) #define gmyth_uri_getport(urip) (urip->port) #define gmyth_uri_getprotocol(urip) (urip->protocol->str)