networkcontrol/qoslib/src/qosselector.cpp
changeset 0 af10295192d8
equal deleted inserted replaced
-1:000000000000 0:af10295192d8
       
     1 // Copyright (c) 2003-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 #include "qoslib.h"
       
    17 
       
    18 
       
    19 const TUint KMaxPort = 65535;
       
    20 
       
    21 //
       
    22 // TQoSSelector
       
    23 //
       
    24 
       
    25 /**
       
    26 Constructor. 
       
    27 
       
    28 Initialises all variables to specified, but null, values. 
       
    29 @publishedPartner
       
    30 @released
       
    31 @capability NetworkControl Restrict QoS policy operations because they may 
       
    32 affect several data flows.
       
    33 */
       
    34 //lint -e{1927} would like some of the following to be in initializer list!
       
    35 EXPORT_C TQoSSelector::TQoSSelector()
       
    36 	{
       
    37 	iSrc.SetAddress(KInet6AddrNone);
       
    38 	iDst.SetAddress(KInet6AddrNone);
       
    39 	iSrcMask.SetAddress(KInet6AddrNone);
       
    40 	iDstMask.SetAddress(KInet6AddrNone);
       
    41 	iProtocol = 0;
       
    42 	iSrcPortMax = 0;
       
    43 	iDstPortMax = 0;
       
    44 	iIapId = 0;
       
    45 	}
       
    46 
       
    47 /** 
       
    48 Compares if the selectors are equal.
       
    49 
       
    50 @publishedPartner
       
    51 @released
       
    52 @capability NetworkControl Restrict QoS policy operations because they may 
       
    53 affect several data flows.
       
    54 @param aSelector TQoSSelector object that is compared with this object.
       
    55 @return ETrue, if selectors are equal to this object; otherwise, EFalse. 
       
    56 */
       
    57 EXPORT_C TInt TQoSSelector::operator==(const TQoSSelector& aSelector) const
       
    58 	{
       
    59 	if (iProtocol == aSelector.Protocol() && iSrcPortMax == aSelector.MaxPortSrc() &&
       
    60 		iDstPortMax == aSelector.MaxPortDst() && iIapId == (TUint)aSelector.IapId() &&
       
    61 		iSrc.CmpAddr(aSelector.GetSrc()) && iDst.CmpAddr(aSelector.GetDst()))
       
    62 		return TRUE;
       
    63 	else
       
    64 		return FALSE;
       
    65 	}
       
    66 
       
    67 /** 
       
    68 Compares if the selector matches aSocket. 
       
    69 
       
    70 @publishedPartner
       
    71 @released
       
    72 @capability NetworkControl Restrict QoS policy operations because they may 
       
    73 affect several data flows.
       
    74 @param aSocket RSocket that is compared with this object.
       
    75 @return ETrue, if selector created from aSocket is equal to this object; 
       
    76 otherwise, EFalse. 
       
    77 */
       
    78 EXPORT_C TBool TQoSSelector::Match(RSocket& aSocket) const
       
    79 	{
       
    80 	TQoSSelector sel;
       
    81 	TInt ret = sel.SetAddr(aSocket);
       
    82 	if (ret!=KErrNone)
       
    83 		return EFalse;
       
    84 	return (*this == sel);
       
    85 	}
       
    86 
       
    87 /**
       
    88 Sets the addresses for selector.
       
    89 
       
    90 @publishedPartner
       
    91 @released
       
    92 @capability NetworkControl Restrict QoS policy operations because they may 
       
    93 affect several data flows.
       
    94 @param aSrcAddr Source address. Port must have value <= 65535 
       
    95 (0 is used as uspecified value for a port).
       
    96 @param aSrcAddrMask Source address mask. Port must have value <= 65535 
       
    97 (0 is used as uspecified value for a port).
       
    98 @param aDstAddr Destination address. Port must have value <= 65535 
       
    99 (0 is used as uspecified value for a port).
       
   100 @param aDstAddrMask Destination address mask. Port must have value <= 65535
       
   101 (0 is used as uspecified value for a port).
       
   102 @param aProtocol Protocol ID.
       
   103 @param aSrcPortMax Maximum source port. Must have value <= 65535 
       
   104 (0 is used as uspecified value for a port).
       
   105 @param aDstPortMax Maximum destination port. Must have value <= 65535 
       
   106 (0 is used as uspecified value for a port).
       
   107 @return KErrNone, if parameters have valid values; otherwise, KErrArgument.
       
   108 */
       
   109 EXPORT_C TInt TQoSSelector::SetAddr(const TInetAddr& aSrcAddr, 
       
   110 									const TInetAddr& aSrcAddrMask, 
       
   111 									const TInetAddr& aDstAddr, 
       
   112 									const TInetAddr& aDstAddrMask, 
       
   113 									TUint aProtocol, 
       
   114 									TUint aSrcPortMax, 
       
   115 									TUint aDstPortMax)
       
   116 	{
       
   117 	if (aSrcAddr.Port() > KMaxPort || aDstAddr.Port() > KMaxPort || 
       
   118 		aSrcPortMax > KMaxPort || aDstPortMax > KMaxPort)
       
   119 		return KErrArgument;
       
   120 
       
   121 	iSrc = aSrcAddr;
       
   122 	iDst = aDstAddr;
       
   123 
       
   124 	iSrcMask = aSrcAddrMask;
       
   125 	iDstMask = aDstAddrMask;
       
   126 	iProtocol = aProtocol;
       
   127 	iSrcPortMax = aSrcPortMax;
       
   128 	iDstPortMax = aDstPortMax;
       
   129 	iIapId = 0;
       
   130 	return KErrNone;
       
   131 	}
       
   132 
       
   133 //lint -e{708} does not like union initializers
       
   134 const TIp6Addr KInet6AddrMask = {{{0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
       
   135 								  0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff}}};
       
   136 
       
   137 //
       
   138 // RSocket must be connected before this!!
       
   139 //
       
   140 /**
       
   141 Sets the addresses for selector. 
       
   142  
       
   143 RSocket is used to fetch addresses and ports for a selector. 
       
   144 The resulting selector will match only for one socket. 
       
   145  
       
   146 Note that RSocket::Connect() must be called before calling this function.
       
   147 
       
   148 @publishedPartner
       
   149 @released
       
   150 @capability NetworkControl Restrict QoS policy operations because they may 
       
   151 affect several data flows.
       
   152 @param aSocket RSocket object that is used to set the selector variables. 
       
   153 Note: RSocket must be connected
       
   154 @return KErrNone, if parameters have valid values; otherwise, KErrArgument.
       
   155 */
       
   156 EXPORT_C TInt TQoSSelector::SetAddr(RSocket& aSocket)
       
   157 	{
       
   158 	TSockAddr aRemoteAddr, aLocalAddr;
       
   159 	TProtocolDesc aDesc;
       
   160 	aSocket.LocalName(aLocalAddr);
       
   161 	aSocket.RemoteName(aRemoteAddr);
       
   162 
       
   163 	if (aLocalAddr.Port() < 1 || aLocalAddr.Port() > KMaxPort)
       
   164 		return KErrArgument;
       
   165 	if (aRemoteAddr.Port() < 1 || aRemoteAddr.Port() > KMaxPort)
       
   166 		return KErrArgument;
       
   167 	if (iDst.Family() == KAFUnspec)
       
   168 		return KErrArgument;
       
   169 
       
   170 	iSrc = TInetAddr(aLocalAddr);
       
   171 	iDst = TInetAddr(aRemoteAddr);
       
   172 
       
   173 	// Srcaddr is not used when a selector is made from RSocket: 
       
   174 	// srcaddr might not be set in Connect()
       
   175 	iSrc.SetFamily(KAFUnspec);
       
   176 	iSrc.SetAddress(KInet6AddrNone);
       
   177 
       
   178 	if (iDst.Family() == KAfInet)
       
   179 		iDst.ConvertToV4Mapped();
       
   180 	//
       
   181 	// Set prefixes.
       
   182 	//
       
   183 	iSrcMask.SetAddress(KInet6AddrMask);
       
   184 	iDstMask.SetAddress(KInet6AddrMask);
       
   185 
       
   186 	iSrcPortMax = iSrc.Port();
       
   187 	iDstPortMax = iDst.Port();
       
   188 	aSocket.Info(aDesc);
       
   189 	iProtocol = aDesc.iProtocol;
       
   190 	return KErrNone;
       
   191 	}
       
   192 
       
   193 /**
       
   194 Sets the source address for selector.
       
   195 
       
   196 @publishedPartner
       
   197 @released
       
   198 @capability NetworkControl Restrict QoS policy operations because they may 
       
   199 affect several data flows.
       
   200 @param aAddr Source address. Port must have value <= 65535 (0 is used as 
       
   201 uspecified value for a port).
       
   202 @return KErrNone, if parameters have valid values; otherwise, KErrArgument.
       
   203 */
       
   204 EXPORT_C TInt TQoSSelector::SetSrc(const TInetAddr& aAddr)
       
   205 	{
       
   206 	if (aAddr.Port() > KMaxPort)
       
   207 		return KErrArgument;
       
   208 	iSrc = aAddr;
       
   209 	return KErrNone;
       
   210 	}
       
   211 
       
   212 /**
       
   213 Sets the destination address for selector.
       
   214 
       
   215 @publishedPartner
       
   216 @released
       
   217 @capability NetworkControl Restrict QoS policy operations because they may 
       
   218 affect several data flows.
       
   219 @param aAddr Destination address. Port must have value <= 65535 (0 is used 
       
   220 as uspecified value for a port).
       
   221 @return KErrNone, if parameters have valid values; otherwise, KErrArgument.
       
   222 */
       
   223 EXPORT_C TInt TQoSSelector::SetDst(const TInetAddr& aAddr)
       
   224 	{
       
   225 	if (aAddr.Port() > KMaxPort)
       
   226 		return KErrArgument;
       
   227 	iDst = aAddr;
       
   228 	return KErrNone;
       
   229 	}
       
   230 
       
   231 /**
       
   232 Gets the current source address.
       
   233 
       
   234 @publishedPartner
       
   235 @released
       
   236 @capability NetworkControl Restrict QoS policy operations because they may 
       
   237 affect several data flows.
       
   238 @return Source address.
       
   239 */
       
   240 EXPORT_C TInetAddr TQoSSelector::GetSrc() const
       
   241 	{
       
   242 	return iSrc;
       
   243 	}
       
   244 
       
   245 /**
       
   246 Gets the current destination address.
       
   247 
       
   248 @publishedPartner
       
   249 @released
       
   250 @capability NetworkControl Restrict QoS policy operations because they may 
       
   251 affect several data flows.
       
   252 @return Destination address.
       
   253 */
       
   254 EXPORT_C TInetAddr TQoSSelector::GetDst() const
       
   255 	{
       
   256 	return iDst;
       
   257 	}
       
   258 
       
   259 /**
       
   260 Sets the source address mask.
       
   261 
       
   262 @publishedPartner
       
   263 @released
       
   264 @capability NetworkControl Restrict QoS policy operations because they may 
       
   265 affect several data flows.
       
   266 @param aMask Source address mask. Port must have value <= 65535 (0 is used 
       
   267 as uspecified value for a port).
       
   268 @return KErrNone, if parameters have valid values; otherwise, KErrArgument.
       
   269 */
       
   270 EXPORT_C TInt TQoSSelector::SetSrcMask(const TInetAddr& aMask)
       
   271 	{
       
   272 	if (aMask.Port() > KMaxPort)
       
   273 		return KErrArgument;
       
   274 	iSrcMask = aMask;
       
   275 	return KErrNone;
       
   276 	}
       
   277 
       
   278 /**
       
   279 Gets the current source address mask.
       
   280 
       
   281 @publishedPartner
       
   282 @released
       
   283 @capability NetworkControl Restrict QoS policy operations because they may 
       
   284 affect several data flows.
       
   285 @return Source address mask.
       
   286 */
       
   287 EXPORT_C TInetAddr TQoSSelector::GetSrcMask() const
       
   288 	{
       
   289 	return iSrcMask;
       
   290 	}
       
   291 
       
   292 /**
       
   293 Sets the destination address mask.
       
   294 
       
   295 @publishedPartner
       
   296 @released
       
   297 @capability NetworkControl Restrict QoS policy operations because they may 
       
   298 affect several data flows.
       
   299 @param aMask Destination address mask. Port must have value <= 65535 (0 is 
       
   300 used as uspecified value for a port).
       
   301 @return KErrNone, if parameters have valid values; otherwise, KErrArgument.
       
   302 */
       
   303 EXPORT_C TInt TQoSSelector::SetDstMask(const TInetAddr& aMask)
       
   304 	{
       
   305 	if (aMask.Port() > KMaxPort)
       
   306 		return KErrArgument;
       
   307 	iDstMask = aMask;
       
   308 	return KErrNone;
       
   309 	}
       
   310 
       
   311 /**
       
   312 Gets the current destination address mask.
       
   313 
       
   314 @publishedPartner
       
   315 @released
       
   316 @capability NetworkControl Restrict QoS policy operations because they may 
       
   317 affect several data flows.
       
   318 @return Destination address mask.
       
   319 */
       
   320 EXPORT_C TInetAddr TQoSSelector::GetDstMask() const
       
   321 	{
       
   322 	return iDstMask;
       
   323 	}
       
   324 
       
   325 /**
       
   326 Sets the Internet access point identifier. 
       
   327 
       
   328 0 is used as unspecified value.
       
   329 
       
   330 @publishedPartner
       
   331 @released
       
   332 @capability NetworkControl Restrict QoS policy operations because they may 
       
   333 affect several data flows.
       
   334 @param aIapId Value to which to set the IapId.
       
   335 */
       
   336 EXPORT_C void TQoSSelector::SetIapId(TInt aIapId)
       
   337 	{
       
   338 	iIapId = aIapId;
       
   339 	}
       
   340 
       
   341 /**
       
   342 Gets the current Internet access point identifier.
       
   343 
       
   344 @publishedPartner
       
   345 @released
       
   346 @capability NetworkControl Restrict QoS policy operations because they may 
       
   347 affect several data flows.
       
   348 @return Internet access point identifier.
       
   349 */
       
   350 EXPORT_C TInt TQoSSelector::IapId() const
       
   351 	{
       
   352 	return iIapId;
       
   353 	}
       
   354 
       
   355 /**
       
   356 Sets the protocol identifier. 
       
   357 
       
   358 0 is used as unspecified value.
       
   359 
       
   360 @publishedPartner
       
   361 @released
       
   362 @capability NetworkControl Restrict QoS policy operations because they may 
       
   363 affect several data flows.
       
   364 @param aProtocol Value to which to set the protocol ID.
       
   365 */
       
   366 EXPORT_C void TQoSSelector::SetProtocol(TUint aProtocol)
       
   367 	{
       
   368 	iProtocol = aProtocol;
       
   369 	}
       
   370 
       
   371 /**
       
   372 Gets the current protocol identifier.
       
   373 
       
   374 @publishedPartner
       
   375 @released
       
   376 @capability NetworkControl Restrict QoS policy operations because they may 
       
   377 affect several data flows.
       
   378 @return Protocol identifier.
       
   379 */
       
   380 EXPORT_C TUint TQoSSelector::Protocol() const
       
   381 	{
       
   382 	return iProtocol;
       
   383 	}
       
   384 
       
   385 /**
       
   386 Sets the maximum source port. 
       
   387 
       
   388 Port must have value <= 65535 (0 is used as unspecified value).
       
   389 
       
   390 @publishedPartner
       
   391 @released
       
   392 @capability NetworkControl Restrict QoS policy operations because they may 
       
   393 affect several data flows.
       
   394 @param aMaxPort Value to which to set maximum source port.
       
   395 @return KErrNone, if aMaxPort has valid values; otherwise, KErrArgument.
       
   396 */
       
   397 EXPORT_C TInt TQoSSelector::SetMaxPortSrc(TUint aMaxPort)
       
   398 	{
       
   399 	if (aMaxPort > KMaxPort)
       
   400 		return KErrArgument;
       
   401 	iSrcPortMax = aMaxPort;
       
   402 	return KErrNone;
       
   403 	}
       
   404 
       
   405 /**
       
   406 Sets the maximum destination port. 
       
   407 
       
   408 Port must have value <= 65535 (0 is used as unspecified value).
       
   409 
       
   410 @publishedPartner
       
   411 @released
       
   412 @capability NetworkControl Restrict QoS policy operations because they may 
       
   413 affect several data flows.
       
   414 @param aMaxPort Value to which to set maximum destination port.
       
   415 @return KErrNone, if aMaxPort has valid values; otherwise, KErrArgument.
       
   416 */
       
   417 EXPORT_C TInt TQoSSelector::SetMaxPortDst(TUint aMaxPort)
       
   418 	{
       
   419 	if (aMaxPort > KMaxPort)
       
   420 		return KErrArgument;
       
   421 	iDstPortMax = aMaxPort;
       
   422 	return KErrNone;
       
   423 	}
       
   424 
       
   425 /**
       
   426 Gets the maximum source port.
       
   427 
       
   428 @publishedPartner
       
   429 @released
       
   430 @capability NetworkControl Restrict QoS policy operations because they may 
       
   431 affect several data flows.
       
   432 @return Maximum source port.
       
   433 */
       
   434 EXPORT_C TUint TQoSSelector::MaxPortSrc() const
       
   435 	{
       
   436 	return iSrcPortMax;
       
   437 	}
       
   438 
       
   439 /**
       
   440 Gets the maximum destination port.
       
   441 
       
   442 @publishedPartner
       
   443 @released
       
   444 @capability NetworkControl Restrict QoS policy operations because they may 
       
   445 affect several data flows.
       
   446 @return Maximum destination port.
       
   447 */
       
   448 EXPORT_C TUint TQoSSelector::MaxPortDst() const
       
   449 	{
       
   450 	return iDstPortMax;
       
   451 	}
       
   452