00001
00002
00003
00004
00005
00006
00007
00008
00010
00011
00012 #include <sys/types.h>
00013 #include <sys/time.h>
00014
00015
00016 #include <quuid.h>
00017
00018
00019 #include "upnputil.h"
00020 #include "upnp.h"
00021 #include "compat.h"
00022 #include "mythconfig.h"
00023
00024
00025 #ifndef USING_MINGW
00026 #include <net/if.h>
00027 #endif // USING_MINGW
00028 #ifdef HAVE_GETIFADDRS
00029 #include <ifaddrs.h>
00030 #endif
00031
00033
00035
00036 QString LookupUDN( QString sDeviceType )
00037 {
00038 QStringList sList = QStringList::split( ":", sDeviceType );
00039 QString sLoc = "LookupUDN(" + sDeviceType + ")";
00040 QString sName;
00041 QString sUDN;
00042
00043 if (sList.size() <= 2)
00044 {
00045 VERBOSE(VB_IMPORTANT, sLoc + "- bad device type, not enough tokens");
00046 return QString::null;
00047 }
00048
00049 sName = "UPnP/UDN/" + sList[ sList.size() - 2 ];
00050 sUDN = UPnp::g_pConfig->GetValue( sName, "" );
00051
00052 VERBOSE(VB_UPNP, sLoc + " sName=" + sName + ", sUDN=" + sUDN);
00053
00054 if ( sUDN.length() == 0)
00055 {
00056 sUDN = QUuid::createUuid().toString();
00057
00058 sUDN = sUDN.mid( 1, sUDN.length() - 2);
00059
00060 UPnp::g_pConfig->SetValue( sName, sUDN );
00061
00062 UPnp::g_pConfig->Save();
00063 }
00064
00065 return( sUDN );
00066 }
00067
00069
00070 #ifdef HAVE_GETIFADDRS
00071
00072 long GetIPAddressList(QStringList &sStrList)
00073 {
00074 struct ifaddrs *list, *ifa;
00075
00076
00077 sStrList.clear();
00078
00079 if (getifaddrs(&list) == -1)
00080 {
00081 VERBOSE(VB_UPNP, QString("GetIPAddressList() - getifaddrs failed: ")
00082 + strerror(errno));
00083 return 0;
00084 }
00085
00086 for (ifa=list; ifa; ifa=ifa->ifa_next)
00087 {
00088 if (!ifa->ifa_addr)
00089 continue;
00090 if (ifa->ifa_addr->sa_family != AF_INET)
00091 continue;
00092 if (ifa->ifa_flags & IFF_LOOPBACK)
00093 continue;
00094 if (!(ifa->ifa_flags & IFF_UP))
00095 continue;
00096
00097
00098 char address[15];
00099
00100 if (inet_ntop(ifa->ifa_addr->sa_family,
00101 &((struct sockaddr_in *)ifa->ifa_addr)->sin_addr,
00102 address, sizeof(address)) == NULL)
00103 {
00104 VERBOSE(VB_UPNP, QString("GetIPAddressList() - inet_ntop failed: ")
00105 + strerror(errno));
00106 continue;
00107 }
00108
00109 sStrList.append(address);
00110
00111
00112 }
00113
00114 freeifaddrs(list);
00115
00116 return(sStrList.count());
00117 }
00118
00119 #else // HAVE_GETIFADDRS
00120
00121
00122
00123
00124 long GetIPAddressList( QStringList &sStrList )
00125 {
00126 #ifdef USING_MINGW
00127 VERBOSE(VB_UPNP, QString("GetIPAddressList() not implemented in MinGW"));
00128 return 0;
00129 #else
00130
00131 sStrList.clear();
00132
00133 QSocketDevice socket( QSocketDevice::Datagram );
00134
00135 struct ifreq ifReqs[ 16 ];
00136 struct ifreq ifReq;
00137 struct ifconf ifConf;
00138
00139
00140
00141
00142
00143 ifConf.ifc_len = sizeof( struct ifreq ) * sizeof( ifReqs );
00144 ifConf.ifc_ifcu.ifcu_req = ifReqs;
00145
00146 if ( ioctl( socket.socket(), SIOCGIFCONF, &ifConf ) < 0)
00147 return( 0 );
00148
00149 long nCount = ifConf.ifc_len / sizeof( struct ifreq );
00150
00151
00152
00153
00154
00155 for (long nIdx = 0; nIdx < nCount; nIdx++ )
00156 {
00157
00158
00159
00160
00161 strcpy ( ifReq.ifr_name, ifReqs[ nIdx ].ifr_name );
00162
00163 if (ioctl ( socket.socket(), SIOCGIFFLAGS, &ifReq ) < 0)
00164 continue;
00165
00166
00167
00168
00169
00170 if ((ifReq.ifr_flags & IFF_LOOPBACK) || (!(ifReq.ifr_flags & IFF_UP)))
00171 continue;
00172
00173 if ( ifReqs[ nIdx ].ifr_addr.sa_family == AF_INET)
00174 {
00175 struct sockaddr_in addr;
00176
00177
00178
00179
00180
00181 memcpy (&addr, &(ifReqs[ nIdx ].ifr_addr), sizeof( ifReqs[ nIdx ].ifr_addr ));
00182
00183 if (addr.sin_addr.s_addr != htonl( INADDR_LOOPBACK ))
00184 {
00185 QHostAddress address( htonl( addr.sin_addr.s_addr ));
00186
00187 sStrList.append( address.toString() );
00188 }
00189 }
00190 }
00191
00192 return( sStrList.count() );
00193 #endif // !USING_MINGW
00194 }
00195
00196 #endif // HAVE_GETIFADDRS
00197
00199
00201
00202 bool operator< ( TaskTime t1, TaskTime t2 )
00203 {
00204 if ( (t1.tv_sec < t2.tv_sec) or
00205 ((t1.tv_sec == t2.tv_sec) && (t1.tv_usec < t2.tv_usec)))
00206 {
00207 return true;
00208 }
00209
00210 return false;
00211 }
00212
00214
00216
00217 bool operator== ( TaskTime t1, TaskTime t2 )
00218 {
00219 if ((t1.tv_sec == t2.tv_sec) && (t1.tv_usec == t2.tv_usec))
00220 return true;
00221
00222 return false;
00223 }
00224
00226
00228
00229 void AddMicroSecToTaskTime( TaskTime &t, suseconds_t uSecs )
00230 {
00231 uSecs += t.tv_usec;
00232
00233 t.tv_sec += (uSecs / 1000000);
00234 t.tv_usec = (uSecs % 1000000);
00235 }
00236
00238
00240
00241 void AddSecondsToTaskTime( TaskTime &t, long nSecs )
00242 {
00243 t.tv_sec += nSecs;
00244 }