servicediscoveryandcontrol/pnp/test/upnp/Client/upnpplugin/src/pnputils.cpp
changeset 0 f5a58ecadc66
equal deleted inserted replaced
-1:000000000000 0:f5a58ecadc66
       
     1 // Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 
       
    17 #include "pnputils.h"
       
    18 #include "cupnpservicediscoveryimpl.h"
       
    19 #include <inetprottextutils.h>
       
    20 #include <uriutils.h>
       
    21 
       
    22 const TUint KHttpDefaultPort = 80;
       
    23 
       
    24 /* Used to resolve the domain name into ipaddress
       
    25  @param aResolver   The RHostResolver reference used to resolve the domain name
       
    26  @param aUri		The url which has to be resolved into ip address
       
    27  @param aSockAddr	The resolved address which will get written to on success
       
    28  */
       
    29 void CUPnPUtils::ResolveHostAddressL ( RHostResolver& aResolver,const TDesC8& aUri, TInetAddr& aSockAddr )
       
    30 	{
       
    31 	TUriParser8 parsedUri;
       
    32 	parsedUri.Parse( aUri );
       
    33 	_LIT8(KScheme,"http");
       
    34 	const TDesC8& scheme = parsedUri.Extract( EUriScheme );
       
    35 	if ( scheme.CompareF( KScheme ))
       
    36 		User::Leave(KErrCorrupt);
       
    37 	// Check if the character followed by the scheme eg. http:// is a numeral. If
       
    38 	// so its already an ip address and hence no need to resolve
       
    39 	//If the first character following http:// is a number then its already
       
    40 	// an ipaddress and hence is returned as is		
       
    41 	const TDesC8& hostname = parsedUri.Extract( EUriHost );
       
    42 	if ( UriUtils::HostType( hostname ) == UriUtils::EIPv4Host )
       
    43 		{
       
    44 		RBuf hostBuf;
       
    45 		hostBuf.CreateL ( hostname.Length ( ) );
       
    46 				
       
    47 		hostBuf.Copy ( hostname );
       
    48 		aSockAddr.Input ( hostBuf );		
       
    49 		hostBuf.Close ( );
       
    50 		}
       
    51 	else if ( UriUtils::HostType( hostname ) == UriUtils::ETextHost )
       
    52 		{
       
    53 		RBuf tempBuf;
       
    54 		tempBuf.CreateL ( hostname.Length ( ) );		
       
    55 		tempBuf.Copy ( hostname );
       
    56 	
       
    57 		TNameEntry dnsHostEntry;
       
    58 		// Get the ip address corresponding to the hostname and write it to tempBuf
       
    59 		aResolver.GetByName( tempBuf, dnsHostEntry );
       
    60 		TInetAddr address = TInetAddr(dnsHostEntry().iAddr);
       
    61 		aSockAddr.SetAddress ( address.Address ( ) );
       
    62 		if ( address.Port () == 0 )
       
    63 			{			
       
    64 			aSockAddr.SetPort ( KHttpDefaultPort );
       
    65 			}
       
    66 		else
       
    67 			{
       
    68 			aSockAddr.SetPort ( address.Port () );
       
    69 			}	
       
    70 		
       
    71 		tempBuf.Close ( );
       
    72 		}
       
    73 	else
       
    74 		User::Leave(KErrCorrupt);
       
    75 	
       
    76 	if ( parsedUri.IsPresent ( EUriPort ) )
       
    77 		{
       
    78 		const TDesC8& portBuf = parsedUri.Extract( EUriPort );
       
    79 		TInt port;
       
    80 		InetProtTextUtils::ConvertDescriptorToInt ( portBuf, port );
       
    81 		aSockAddr.SetPort ( port );
       
    82 		}		
       
    83 	}
       
    84 
       
    85 /* Used to check whether uri conforms to given type. The uri types it checks are
       
    86   1. upnp:rootdevice
       
    87   2. urn:domain-name:service:serviceId
       
    88   3. urn:domain-name:device:deviceId
       
    89   4. uuid:
       
    90   
       
    91   @param aUri	The url whose validity must be checked
       
    92   @param aType	The type of uri to be checked against
       
    93   				If aType == EDevice Then aUri is of deviceType
       
    94   				else if aType == EService Then aUri is of serviceType
       
    95   				else if aType == EAny then aUri is generic
       
    96  */
       
    97 TInt CUPnPUtils::GenericUriValidity ( const TDesC8& aUri , CUPnPUtils::TUriType aType)
       
    98 	{
       
    99 	_LIT8(KUrn,"urn");
       
   100 	_LIT8(KUpnp,"upnp:rootdevice");
       
   101 	_LIT8(KService,"service");
       
   102 	_LIT8(KDevice,"device");
       
   103 	_LIT8(KUuid,"uuid");
       
   104 	if(aUri.Length()<=5)
       
   105 		return KErrCorrupt;
       
   106 	
       
   107 	if((aType == CUPnPUtils::EDevice) || (aType == CUPnPUtils::EAny))
       
   108 		{
       
   109 		if ( aUri.Left(4) == KUuid && aUri[4] == ':')
       
   110 			return KErrNone;
       
   111 		// Test whether uri is "upnp:rootdevice"
       
   112 		if (aUri.CompareF(KUpnp) == NULL)
       
   113 			return KErrNone;
       
   114 		}
       
   115 	TLex8 uriParser(aUri);
       
   116 	TChar ch = ':';
       
   117 	uriParser.Mark();
       
   118 	while((uriParser.Eos() == EFalse) && uriParser.Peek() != ch)
       
   119 		uriParser.Inc();
       
   120 	if(uriParser.MarkedToken().CompareF(KUrn) == NULL)
       
   121 		{
       
   122 		// Reach the first character after the first delimiter ":"
       
   123 		uriParser.Inc();
       
   124 		if( !uriParser.Eos() )
       
   125 			uriParser.Inc();
       
   126 		
       
   127 		// Now move char pointer to the second delimiter
       
   128 		while((uriParser.Eos() == EFalse) && uriParser.Peek() != ch)
       
   129 			uriParser.Inc();
       
   130 		if (uriParser.Eos())
       
   131 			return KErrCorrupt;
       
   132 		
       
   133 		// Reach the the 2nd delimiter ":" and mark it. Now start searching
       
   134 		// from the next character
       
   135 		uriParser.Inc();
       
   136 		uriParser.Mark();
       
   137 		if( !uriParser.Eos() )
       
   138 			uriParser.Inc();
       
   139 		
       
   140 		// Now move char pointer to the 3rd delimiter
       
   141 		while((uriParser.Eos() == EFalse) && uriParser.Peek() != ch)
       
   142 			uriParser.Inc();
       
   143 		
       
   144 		// Extract the token in between it should be either "service" or "device"
       
   145 		const TDesC8& testUri = uriParser.MarkedToken();
       
   146 		TBool flag = EFalse;
       
   147 		if(aType == CUPnPUtils::EService && (testUri.CompareF(KService) == NULL))
       
   148 			flag = ETrue;
       
   149 		else if(aType == CUPnPUtils::EDevice && (testUri.CompareF(KDevice) == NULL))
       
   150 			flag = ETrue;
       
   151 		else if (aType == CUPnPUtils::EAny)
       
   152 			{
       
   153 			if ((testUri.CompareF(KDevice) == NULL) || (testUri.CompareF(KService)== NULL))
       
   154 				flag = ETrue;
       
   155 			}
       
   156 		
       
   157 		if (flag == EFalse )
       
   158 			return KErrCorrupt;
       
   159 		
       
   160 		// Now check for the remaining part of the uri
       
   161 		uriParser.Inc();
       
   162 		uriParser.Mark();
       
   163 		
       
   164 		if( !uriParser.Eos() )
       
   165 			uriParser.Inc();
       
   166 			
       
   167 		// Now move char pointer to the 4th delimiter
       
   168 		while((uriParser.Eos() == EFalse) && uriParser.Peek() != ch)
       
   169 			uriParser.Inc();
       
   170 		if( uriParser.Eos() )
       
   171 			return KErrCorrupt;
       
   172 		// The service/device suffix should be of length <=64
       
   173 		if((uriParser.MarkedToken()).Length() >64)
       
   174 			return KErrCorrupt;
       
   175 		// Now the first character outside the last colon should be a number
       
   176 		uriParser.Inc();
       
   177 		ch = uriParser.Peek();
       
   178 		if( ch >= 48 && ch <= 57 )
       
   179 			return KErrNone;
       
   180 		else
       
   181 			return KErrCorrupt;
       
   182 	
       
   183 		}
       
   184 	else
       
   185 		return KErrCorrupt;
       
   186 		
       
   187 	}