breakdeps/usocket.cpp
changeset 20 37dc158a4522
child 21 4a02a61ca23a
equal deleted inserted replaced
19:b1cc137d8adb 20:37dc158a4522
       
     1 /*
       
     2 * Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <sys/types.h>
       
    20 #include <string.h>		// for memcpy
       
    21 #include <fcntl.h>		// for struct stat
       
    22 #include <sys/errno.h>		// for ENOTSOCK
       
    23 #include <sys/socket.h>
       
    24 #include <sys/ioctl.h>
       
    25 #include <sys/stat.h>
       
    26 #include <netinet/in.h>		// for htons
       
    27 #include <sys/sockio.h>
       
    28 #include <bt_sock.h>     // Symbian BT constantss needed for cooperation with Symbian sockets
       
    29 #include <arpa/inet.h>   
       
    30 #include <es_sock.h>
       
    31 #include <in_sock.h>
       
    32 #include <net/if.h>
       
    33 #include <commdb.h>
       
    34 #include <cdbcols.h>
       
    35 #include <commdbconnpref.h>
       
    36 #include <e32base.h>
       
    37 #include <e32err.h>
       
    38 #include <utf.h>
       
    39 #include <e32std.h>
       
    40 #include <net/route.h>
       
    41 #include <in_iface.h>
       
    42 #include "fdesc.h"
       
    43 #include "lposix.h"
       
    44 #include "sysif.h"
       
    45 
       
    46 // Support for struct sockaddr conversion
       
    47 TUSockAddr::TUSockAddr (TAny* aBuf) 
       
    48 : TSockAddr(), iError(0)
       
    49 		{
       
    50 		Prepare(aBuf);
       
    51 		}
       
    52 
       
    53 void TUSockAddr::Prepare (TAny* aBuf) 
       
    54 //
       
    55 // Prepare a TUSockAddr to receive an address (used in RecvFrom)
       
    56 // Setting the length to 0 indicates that we don't really want this address anyway.
       
    57 //
       
    58 	{
       
    59 	if (aBuf==0)
       
    60 		{
       
    61 		SetLength(0);
       
    62 		iError = EFAULT;
       
    63 		return;
       
    64 		}
       
    65 	}
       
    66 
       
    67 EXPORT_C TUSockAddr::TUSockAddr (const TAny* aBuf, TUint aLen) 
       
    68 : TSockAddr(), iError(0)
       
    69 		{
       
    70 		Set(aBuf,aLen);
       
    71 		}
       
    72 
       
    73 void TUSockAddr::Set (const TAny* aBuf, TUint aLen) 
       
    74 //
       
    75 // Construct an ESOCK TSockAddr from a struct sockaddr.
       
    76 // We have to deal with the network byte ordering of AF_INET addresses
       
    77 //
       
    78 	{
       
    79 	if (!aBuf)
       
    80 		{
       
    81 		iError = EFAULT;
       
    82 		SetLength(0);
       
    83 		return;
       
    84 		}
       
    85 	
       
    86 	const TUint dataOffset = (TUint)&((struct sockaddr *)0)->sa_data[0];
       
    87 	// aLen should be atleast have minimum size.
       
    88 	if (aLen < dataOffset)
       
    89 		{
       
    90 		iError = EINVAL;
       
    91 		SetLength(0);
       
    92 		return;
       
    93 		}
       
    94 
       
    95 	const struct sockaddr* sp= (const struct sockaddr *)(aBuf);
       
    96 	TUint8 *from=(TUint8*)aBuf;	
       
    97 
       
    98 	if (sp->sa_family==AF_INET)
       
    99 		{		
       
   100 		// byte-swap sockaddr_in back into host ordering
       
   101 		TUint port=(from[2]<<8)+from[3];
       
   102 		from+=4;
       
   103 		TUint32 addr=(from[0]<<24)+(from[1]<<16)+(from[2]<<8)+from[3];
       
   104 		from+=4;
       
   105 		SetFamily(AF_INET);
       
   106 		SetPort(port);
       
   107 		*(TUint32*)UserPtr()=addr;
       
   108 		SetUserLen(4);
       
   109 		return;
       
   110 		}
       
   111 	else if (sp->sa_family == AF_INET6)
       
   112 		{
       
   113 		SetFamily(sp->sa_family);
       
   114 		SetPort(ByteOrder::Swap16(sp->sa_port));
       
   115 		from+=4;
       
   116 		// copy the rest of the data as given
       
   117 		TUint8 *to=UserPtr();
       
   118 		if (aLen<4)
       
   119 			aLen=4;
       
   120 		aLen-=4;	// skip fmaily and port
       
   121 		if (aLen>24)
       
   122 			aLen=24;
       
   123 		memcpy(to,from,aLen);
       
   124 		SetUserLen(aLen);    	
       
   125 		return;
       
   126 		}	
       
   127 	else if (sp->sa_family == KBTAddrFamily)
       
   128 		{
       
   129 		// Make a TBTSockAddr from a sockaddr_bt...
       
   130 		TBTSockAddr tmpAddr;
       
   131 
       
   132 		// Family is always Bluetooth:
       
   133 		tmpAddr.SetFamily(KBTAddrFamily);
       
   134 
       
   135 		// The port number has the correct byte order already, as the
       
   136 		// POSIX Bluetooth APIs assume litle-endian format:
       
   137 		tmpAddr.SetPort(sp->sa_port);
       
   138 
       
   139 		// For the address we need to reverse the byte ordering, as
       
   140 		// Symbian appears to index Bluetooth addresses in a
       
   141 		// big-endian manner:
       
   142 
       
   143 		const TInt KBTAddrLength = 6;  // Bytes in a Bluetooth address
       
   144 		const TInt KBTAddrOffset = 4;  // Offset of address in sockaddr_bt
       
   145 
       
   146 		TBTDevAddr devAddr;
       
   147 		const TInt KMinLength = KBTAddrLength + KBTAddrOffset;
       
   148 
       
   149 		if (aLen >= KMinLength)
       
   150 			{
       
   151 			for (TInt i=0; i<KBTAddrLength; ++i)
       
   152 				{
       
   153 				devAddr[i] = from[KMinLength - 1 - i];
       
   154 				}
       
   155 			}
       
   156 
       
   157 		tmpAddr.SetBTAddr(devAddr);
       
   158 
       
   159 		// Security settings:
       
   160 		TBTServiceSecurity sec;
       
   161 		sec.SetUid(TUid::Uid(0));
       
   162 		sec.SetAuthentication(EFalse);
       
   163 		sec.SetEncryption(EFalse);
       
   164 		sec.SetAuthorisation(EFalse);
       
   165 		sec.SetDenied(EFalse);
       
   166 		tmpAddr.SetSecurity(sec);
       
   167 
       
   168 		// Now we must copy the contents of the "tmpAddr" TBTSockAddr,
       
   169 		// but unfortunately there isn't an assignment operator for us
       
   170 		// to use. Still, we know it is a self-contained T class for
       
   171 		// which a direct shallow copy using memcopy() will be ok:
       
   172 		//
       
   173 		memcpy(this, &tmpAddr, sizeof(TBTSockAddr));
       
   174 
       
   175 		return;
       
   176 		}
       
   177 	else
       
   178 		{
       
   179 		// Not AF_INET or KBTAddrFamily:
       
   180 
       
   181 		// expand the family and port
       
   182 		SetFamily(sp->sa_family);
       
   183 		SetPort(sp->sa_port);
       
   184 		from+=4;
       
   185 		// copy the rest of the data as given
       
   186 		TUint8 *to=UserPtr();
       
   187 		if (aLen<4)
       
   188 			aLen=4;
       
   189 		aLen-=4;	// skip fmaily and port
       
   190 		if (aLen>24)
       
   191 			aLen=24;
       
   192 		memcpy(to,from,aLen);
       
   193 		SetUserLen(aLen);
       
   194 		}
       
   195 	}
       
   196 
       
   197 /*
       
   198  * Extract a struct sockaddr from a TSockAddr
       
   199  */
       
   200 EXPORT_C void TUSockAddr::Get(TAny* addr, unsigned long* len)
       
   201 	{
       
   202 	if (addr==0)
       
   203 		{
       
   204 		iError = EFAULT;
       
   205 		SetLength(0);
       
   206 		return;
       
   207 		}
       
   208 	const TUint dataOffset = (TUint)&((struct sockaddr *)0)->sa_data[0];
       
   209 	// aLen should be atleast have minimum size.
       
   210 	if (*len < dataOffset)
       
   211 		{
       
   212 		iError = EINVAL;
       
   213 		SetLength(0);
       
   214 		return;
       
   215 		};
       
   216 
       
   217 	struct sockaddr* sp=(struct sockaddr*)addr;
       
   218 	TUint16 port=(TUint16)Port();
       
   219 
       
   220 	//The ipv4 address could be obtained either as
       
   221 	//plain 32 bit address or as ipv4 mapped ipv6 address
       
   222 	TInetAddr inetAddr(*this);
       
   223 	TBool isV4Mapped = (Family()==AF_INET6) && (inetAddr.IsV4Mapped() || inetAddr.IsV4Compat());
       
   224 	if (Family()==AF_INET || isV4Mapped)
       
   225 		{
       
   226 		sp->sa_family=AF_INET;
       
   227 		sp->sa_port= ByteOrder::Swap16(port);
       
   228 		TUint8* from=UserPtr();
       
   229 		TUint32 fromaddr;
       
   230 		//If the address is v4 mapped, we take the last 4 bytes from the 16 bytes long v6 structure. 
       
   231 		if(isV4Mapped)
       
   232 			fromaddr = (from[15]<<24)+(from[14]<<16)+(from[13]<<8)+from[12];
       
   233 		else
       
   234 			fromaddr = (from[0]<<24)+(from[1]<<16)+(from[2]<<8)+ from[3];
       
   235 		*(TUint32*)sp->sa_data=fromaddr;
       
   236 		*len=8;
       
   237 		return;
       
   238 		}
       
   239 	else if (Family()==AF_INET6)
       
   240 		{
       
   241 
       
   242 		sp->sa_family=(TUint16)Family();
       
   243 		sp->sa_port=ByteOrder::Swap16(port);
       
   244 		TUint ulen=GetUserLen();
       
   245 		if (ulen+4>(*len))
       
   246 			ulen=(*len)-4;
       
   247 		*len=ulen+4;
       
   248 		memcpy(sp->sa_data,UserPtr(),ulen);
       
   249 		sp->sa_len = ulen;
       
   250 		return;
       
   251 		}		
       
   252 	else if (Family()==KBTAddrFamily)
       
   253 		{
       
   254 		sp->sa_family=KBTAddrFamily;
       
   255 		sp->sa_port=port;
       
   256 
       
   257 		const TInt KBTAddrLength = 6;
       
   258 		if (GetUserLen() == KBTAddrLength)
       
   259 			{
       
   260 			for (TInt i=0; i<KBTAddrLength; ++i)
       
   261 				{
       
   262 				sp->sa_data[KBTAddrLength - 1 - i] = UserPtr()[i];
       
   263 				}
       
   264 			}
       
   265 		return;
       
   266 		}
       
   267 	else
       
   268 		{
       
   269 		// Not AF_INET or KBTAddrFamily:
       
   270 		sp->sa_family=(TUint16)Family();
       
   271 		sp->sa_port=port;
       
   272 		TUint ulen=GetUserLen();
       
   273 		if (ulen+4>(*len))
       
   274 			ulen=(*len)-4;
       
   275 		*len=ulen+4;
       
   276 		memcpy(sp->sa_data,UserPtr(),ulen);
       
   277 		}
       
   278 	}
       
   279 
       
   280 // The Socket descriptor class
       
   281 CSocketDesc::CSocketDesc()
       
   282 :iIoctlBuf(0,0),
       
   283 iSocketPtr(NULL),
       
   284 iConnPref(),
       
   285 iConnectionPtr(NULL),
       
   286 iSubConnectionPtr(NULL),
       
   287 iRConnectionIndex(-1),		
       
   288 iSockServPtr(NULL)
       
   289 		{
       
   290 
       
   291 		};
       
   292 
       
   293 
       
   294 TInt CSocketDesc::Socket(RSocketServ& aSs, int family, int style, int protocol)
       
   295 	{
       
   296 	if (protocol == 0)
       
   297 		{
       
   298 		if (family == AF_INET)
       
   299 			{
       
   300 			switch (style)
       
   301 				{
       
   302 				case SOCK_STREAM:
       
   303 					protocol = IPPROTO_TCP;
       
   304 					break;
       
   305 
       
   306 				case SOCK_DGRAM:
       
   307 					protocol = IPPROTO_UDP;
       
   308 					break;
       
   309 				}
       
   310 			}
       
   311 		else
       
   312 			{
       
   313 			protocol = KUndefinedProtocol;
       
   314 			}
       
   315 		}
       
   316 
       
   317 	iFcntlFlag = 0;
       
   318 	TInt err = CreateLock();
       
   319 	if (err)
       
   320 		{
       
   321 		return err;
       
   322 		}
       
   323 
       
   324 	if (family == AF_INET6)
       
   325 		{
       
   326 		err = iSocket.Open(aSs, KAfInet, style, protocol);
       
   327 		}
       
   328 	else
       
   329 		{
       
   330 		err = iSocket.Open(aSs, family, style, protocol);
       
   331 		}
       
   332 
       
   333 	if (err == KErrNone)
       
   334 		{
       
   335 		iStyle = style;
       
   336 		iSocket.Close(); // We will close now, to open later using connection preferences
       
   337 		iAddrFamily = family;
       
   338 		iProtocol = protocol; 
       
   339 		iSockServPtr = &aSs; // Saving the const session pointer for later use
       
   340 		}
       
   341 	return err;
       
   342 	}
       
   343 
       
   344 TInt CSocketDesc::FinalClose()
       
   345 	{
       
   346 	CLocalSystemInterface* backend = Backend();
       
   347 	backend->DefConnLock().Wait();
       
   348 	RHeap* oheap = User::SwitchHeap(backend->Heap());	
       
   349 	if (iSocketPtr != NULL)
       
   350 		{
       
   351 		iSocketPtr = NULL;	
       
   352 		StopInterface(NULL); // Checks for RConnection as well as RSubConnection
       
   353 	    backend->RemoveSocket(this);
       
   354 		}
       
   355 
       
   356 	CSockDescBase::FinalClose();
       
   357 	User::SwitchHeap(oheap);
       
   358 	backend->DefConnLock().Signal();
       
   359 	return KErrNone;
       
   360 	}
       
   361 
       
   362 
       
   363 void CSocketDesc::Read (TDes8& aBuf, TRequestStatus& aStatus)
       
   364 	{
       
   365 	//Acquire the Lock before read and release it later
       
   366 	iReadLock.Wait();
       
   367 	if (iSocketPtr == NULL)
       
   368 		{
       
   369 		TInt ret = OpenUsingPreference();
       
   370 		if (ret != KErrNone)	// Error in open
       
   371 			{
       
   372 			Complete(aStatus,ret);
       
   373 			iReadLock.Signal();
       
   374 			return;
       
   375 			}
       
   376 		}
       
   377 	CSockDescBase::Read (aBuf,aStatus);
       
   378 	iReadLock.Signal();
       
   379 	}
       
   380 
       
   381 TInt CSocketDesc::ReadCompletion(TDes8& aBuf, TInt aStatus)
       
   382 	{
       
   383 	return CSockDescBase::ReadCompletion(aBuf, aStatus);
       
   384 	}
       
   385 
       
   386 void CSocketDesc::ReadCancel()
       
   387 	{
       
   388 	if (iSocketPtr != NULL)
       
   389 		{
       
   390 		CSockDescBase::ReadCancel();
       
   391 		}
       
   392 	}
       
   393 
       
   394 void CSocketDesc::Write (TDes8& aBuf, TRequestStatus& aStatus)
       
   395 	{
       
   396 
       
   397     TInt err = maybe_reopen_socket();
       
   398     if (err != KErrNone)
       
   399         {
       
   400         Complete(aStatus, err);
       
   401         return;
       
   402         }
       
   403 	
       
   404 	iWriteLock.Wait();
       
   405 	CSockDescBase::Write(aBuf, aStatus);
       
   406 	iWriteLock.Signal();	
       
   407 	}
       
   408 
       
   409 void CSocketDesc::WriteCancel()
       
   410 	{
       
   411 	if (iSocketPtr != NULL)
       
   412 		{
       
   413 		CSockDescBase::WriteCancel();
       
   414 		}
       
   415 	}
       
   416 
       
   417 
       
   418 TInt CSocketDesc::Bind(const struct sockaddr* aAddr, unsigned long aSize)
       
   419 	{
       
   420 	TInt ret;
       
   421 	TUSockAddr addr(aAddr,aSize);
       
   422 
       
   423 	if (addr.iError != 0)
       
   424 		{
       
   425 		return addr.iError;
       
   426 		}
       
   427 	
       
   428 	ret = maybe_reopen_socket();
       
   429 	if (ret != KErrNone)
       
   430 	    return ret;
       
   431 	ATOMICSOCKETOP(ret = iSocket.Bind(addr),return KErrBadHandle)
       
   432 	return ret;
       
   433 	}
       
   434 
       
   435 TInt CSocketDesc::Listen(TUint qSize)
       
   436 	{
       
   437 
       
   438     TInt ret;
       
   439 	if (iStyle == SOCK_DGRAM) // Listen on UDP socket, crashing at RSocket::Listen().
       
   440 		{
       
   441 		return EOPNOTSUPP;
       
   442 		}
       
   443 	
       
   444 	ret = maybe_reopen_socket();
       
   445 	if (ret != KErrNone)
       
   446 	    return ret;
       
   447 
       
   448 	return CSockDescBase::Listen(qSize);
       
   449 	}
       
   450 
       
   451 TInt CSocketDesc::SockName(int anEnd, struct sockaddr* anAddr,unsigned long* aSize)
       
   452 	{
       
   453 
       
   454 
       
   455 	if (!anAddr)
       
   456         {
       
   457         return EFAULT;
       
   458         }
       
   459 
       
   460 	TInt ret = maybe_reopen_socket();
       
   461 	if (ret != KErrNone)
       
   462 	    return ret;
       
   463 	
       
   464    
       
   465     struct sockaddr temp;
       
   466     unsigned long len = sizeof( temp );
       
   467     
       
   468 	TUSockAddr nSockAddr(&temp, len );
       
   469 
       
   470 	ret = CSockDescBase::SockName(anEnd,nSockAddr);
       
   471 	if(ret == KErrNone )
       
   472 	    {
       
   473             nSockAddr.Get(&temp,&len );
       
   474             if( *aSize > len )
       
   475                 *aSize = len;
       
   476             memcpy( anAddr,&temp,*aSize);
       
   477             
       
   478             }
       
   479 	
       
   480 	return ret;
       
   481 	}
       
   482 
       
   483 TInt CSocketDesc::GetSockOpt(TInt anOptionName, TInt anOptionLevel, TDes8& anOption)
       
   484 	{
       
   485 	TInt ret;
       
   486 	if (anOption.Ptr() == NULL)
       
   487 		{
       
   488 		return EFAULT;
       
   489 		}
       
   490 	if (anOption.Length() == 0)
       
   491 		{
       
   492 		return EINVAL;
       
   493 		}
       
   494 	
       
   495 	ret = maybe_reopen_socket();
       
   496 	if (ret != KErrNone)
       
   497 	    return ret;
       
   498 
       
   499 	if (SO_TYPE == anOptionName && SOL_SOCKET == anOptionLevel)
       
   500 		{
       
   501 		TProtocolDesc protocolInfo;
       
   502 		ATOMICSOCKETOP(ret = iSocket.Info(protocolInfo), ret = KErrBadHandle)
       
   503 		if (KErrNone == ret )
       
   504 			{
       
   505 			// Copy the Socket Type to the buffer
       
   506 			TInt size = (anOption.Length() < sizeof(protocolInfo.iSockType))? anOption.Length(): sizeof(protocolInfo.iSockType);
       
   507 			Mem::Copy((unsigned char*)anOption.Ptr(), &protocolInfo.iSockType, size);
       
   508 			anOption.SetLength(size);
       
   509 			}
       
   510 		return ret;
       
   511 		}
       
   512 
       
   513 	if(IPPROTO_IP == anOptionLevel && IP_MULTICAST_IF == anOptionName)
       
   514 		{
       
   515 		TUSockAddr addr;
       
   516 		struct sockaddr_in sockAddress;
       
   517 		sockAddress.sin_family = AF_INET;
       
   518 		ATOMICSOCKETOP(sockAddress.sin_port = iSocket.LocalPort(),return KErrBadHandle)			
       
   519 		ATOMICSOCKETOP(iSocket.LocalName(addr);,return KErrBadHandle)		
       
   520 		TInt a = sizeof(sockAddress);
       
   521 		addr.Get(&sockAddress,(unsigned long*)&a);  
       
   522 		TInt size = (anOption.Length() < sizeof(sockAddress.sin_addr))? anOption.Length(): sizeof(sockAddress.sin_addr);
       
   523 		Mem::Copy((unsigned char*)anOption.Ptr(), &(sockAddress.sin_addr), size); 
       
   524 		anOption.SetLength(size);
       
   525 		return KErrNone;
       
   526 		}
       
   527 
       
   528 	switch(anOptionLevel)
       
   529 		{
       
   530 		case IPPROTO_TCP:
       
   531 			anOptionLevel=SOL_TCP;
       
   532 			break;
       
   533 		case IPPROTO_IP:
       
   534 			anOptionLevel=SOL_IP;
       
   535 			break;
       
   536 		case SOL_SOCKET:
       
   537 			{
       
   538 			switch(anOptionName)
       
   539 				{
       
   540 				case SO_REUSEADDR: 
       
   541 					anOptionLevel=SOL_IP;
       
   542 					break;
       
   543 				case SO_OOBINLINE:
       
   544 				case SO_KEEPALIVE:
       
   545 					anOptionLevel=SOL_TCP;
       
   546 					break; 
       
   547 				case SO_BROADCAST:
       
   548 					*((TInt*)anOption.Ptr()) = 1;
       
   549 					return KErrNone;
       
   550 				}
       
   551 			}
       
   552 		} 
       
   553 
       
   554 	switch(anOptionName)
       
   555 		{
       
   556 		case IP_MULTICAST_TTL:
       
   557 			anOptionLevel=KSolInetIp;
       
   558 			anOptionName=KSoIp6MulticastHops;
       
   559 			break;
       
   560 		case IP_MULTICAST_LOOP:
       
   561 			anOptionLevel=KSolInetIp;
       
   562 			anOptionName=KSoIp6MulticastLoop;
       
   563 			break;
       
   564 		}
       
   565 	ATOMICSOCKETOP(ret = iSocket.GetOpt(anOptionName,anOptionLevel,anOption), return KErrBadHandle)
       
   566 	return ret;
       
   567 	}
       
   568 
       
   569 TInt CSocketDesc::GetInterfaceIndex(TUint32 anAddr)
       
   570 	{
       
   571 	TPckgBuf<TSoInetIfQuery> ifq;
       
   572 	TInt aIndex = -1;
       
   573 	if (KInetAddrAny == anAddr)
       
   574 		{
       
   575 		aIndex = 0;
       
   576 		return aIndex;
       
   577 		}
       
   578 	else 
       
   579 		{
       
   580 		TInt ret = KErrNone;
       
   581 		ATOMICSOCKETOP(ret = iSocket.SetOpt(KSoInetEnumInterfaces, KSolInetIfCtrl), ret = KErrBadHandle)
       
   582 		if (ret != KErrNone)
       
   583 			return KErrGeneral;
       
   584 
       
   585 		TPckgBuf<TSoInetInterfaceInfo>iface;
       
   586 		ATOMICSOCKETOP( ret = iSocket.GetOpt(KSoInetNextInterface, KSolInetIfCtrl, iface), ret = KErrBadHandle )
       
   587 		while(ret == KErrNone)
       
   588 			{
       
   589 			TSoInetInterfaceInfo &info = iface();
       
   590 			TInt result;
       
   591 			if(info.iState == EIfUp)
       
   592 				{
       
   593 				if (anAddr == info.iAddress.Address()) 
       
   594 					{      	
       
   595 					ifq().iName = info.iName;
       
   596 					ATOMICSOCKETOP( result = iSocket.GetOpt(KSoInetIfQueryByName, KSolInetIfQuery, ifq), result = KErrBadHandle )
       
   597 					if (result == KErrNone)
       
   598 						aIndex = ifq().iIndex;
       
   599 
       
   600 					}
       
   601 				}
       
   602 			ATOMICSOCKETOP( ret = iSocket.GetOpt(KSoInetNextInterface, KSolInetIfCtrl, iface), ret = KErrBadHandle )
       
   603 			}   	
       
   604 		}
       
   605 	return aIndex;
       
   606 	}
       
   607 
       
   608 TInt CSocketDesc::SetSockOpt(TInt anOptionName, TInt anOptionLevel, TDesC8& anOption)
       
   609 	{
       
   610 	TInt ret;
       
   611 	if (anOption.Ptr() == NULL)
       
   612 		{
       
   613 		return EFAULT;
       
   614 		}
       
   615 	if (anOption.Length() == 0)
       
   616 		{
       
   617 		return EINVAL;
       
   618 		}
       
   619 	
       
   620 	ret = maybe_reopen_socket();
       
   621 	if (ret != KErrNone)
       
   622 	    return ret;
       
   623 
       
   624 	switch(anOptionLevel)
       
   625 		{
       
   626 		case IPPROTO_TCP:
       
   627 			anOptionLevel=SOL_TCP;
       
   628 			break;
       
   629 		case IPPROTO_IP:
       
   630 			anOptionLevel=SOL_IP;
       
   631 			break;
       
   632 		case SOL_SOCKET:
       
   633 			{
       
   634 			switch(anOptionName)
       
   635 				{
       
   636 				case SO_REUSEADDR: 
       
   637 					anOptionLevel=SOL_IP;
       
   638 					break;
       
   639 				case SO_OOBINLINE:
       
   640 				case SO_KEEPALIVE:
       
   641 					anOptionLevel=SOL_TCP;
       
   642 					break; 
       
   643 				}
       
   644 			}
       
   645 
       
   646 		}
       
   647 	TPckgBuf<TIp6Mreq> req;
       
   648 	struct ip_mreq *mreq;
       
   649 	TInt aIndex = -1;
       
   650 	TUint8 *from;
       
   651 	TUint32 srcAddr;
       
   652 	TUint32 maddr;
       
   653 	TInetAddr multiAddr;
       
   654 	TInt *Option = NULL;
       
   655 	TInt ttlValue = 0;
       
   656 	switch(anOptionName) 
       
   657 		{
       
   658 		case IP_ADD_MEMBERSHIP:  //KSoIp6JoinGroup
       
   659 		case IP_DROP_MEMBERSHIP: //KSoIp6LeaveGroup
       
   660 
       
   661 			mreq = (struct ip_mreq *)anOption.Ptr();
       
   662 			from = (TUint8 *)anOption.Ptr(); 
       
   663 			from+=4;
       
   664 			// swaping the byte order
       
   665 			srcAddr = ByteOrder::Swap32( mreq->imr_interface.s_addr); 
       
   666 			//fetch the interface index
       
   667 			aIndex = GetInterfaceIndex(srcAddr);
       
   668 
       
   669 			// Set the multicast addr
       
   670 			from = (TUint8 *)anOption.Ptr();
       
   671 			maddr=(from[0]<<24)+(from[1]<<16)+(from[2]<<8)+from[3];
       
   672 			multiAddr.SetAddress( maddr);
       
   673 			multiAddr.ConvertToV4Mapped();
       
   674 			if (multiAddr.IsMulticast())
       
   675 			    {
       
   676                 req().iAddr = multiAddr.Ip6Address();
       
   677                 req().iInterface = aIndex;  
       
   678 			    }
       
   679 			ATOMICSOCKETOP( ret = iSocket.SetOpt(anOptionName, anOptionLevel, req), return KErrBadHandle )
       
   680 			return ret;
       
   681 
       
   682 		case IP_MULTICAST_TTL:
       
   683 			anOptionLevel=KSolInetIp;
       
   684 			anOptionName=KSoIp6MulticastHops;
       
   685 			Option = (TInt*)anOption.Ptr();
       
   686 			ttlValue = *Option;
       
   687 			ATOMICSOCKETOP( ret = iSocket.SetOpt(anOptionName,anOptionLevel,ttlValue), return KErrBadHandle )
       
   688 			return ret;
       
   689 			
       
   690 		case SO_BROADCAST: 
       
   691 			//check if user is trying to disable broadcast
       
   692 			Option = (TInt*)anOption.Ptr();
       
   693 			return (*Option == 0 ? KErrNotSupported : KErrNone);
       
   694 
       
   695 		case IP_MULTICAST_IF:
       
   696 			{
       
   697 			//No support for equivalent flag KsoIp6MulticastIf presently
       
   698 			//call bind instead	    
       
   699 			struct in_addr *inAddress = (struct in_addr*)anOption.Ptr();	    
       
   700 			struct sockaddr_in sockAddress; 	 		    
       
   701 			sockAddress.sin_family = AF_INET;
       
   702 			ATOMICSOCKETOP(sockAddress.sin_port = iSocket.LocalPort();,return KErrBadHandle)         
       
   703 			sockAddress.sin_addr.s_addr = inAddress->s_addr;
       
   704 			TUSockAddr ifAddress(&sockAddress, sizeof(sockAddress));
       
   705 			ATOMICSOCKETOP( ret = iSocket.Bind(ifAddress), return KErrBadHandle )
       
   706 			return ret;
       
   707 			}
       
   708 
       
   709 		case IP_MULTICAST_LOOP:
       
   710 			anOptionLevel=KSolInetIp;
       
   711 			anOptionName=KSoIp6MulticastLoop;
       
   712 			break;	    	
       
   713 
       
   714 
       
   715 
       
   716 		}
       
   717 	ATOMICSOCKETOP( ret = iSocket.SetOpt(anOptionName,anOptionLevel,anOption), return KErrBadHandle )
       
   718 	return ret;
       
   719 	}
       
   720 
       
   721 void CSocketDesc::Sync (TRequestStatus& aStatus)
       
   722 	{
       
   723 	// Judging from the Solaris man pages, this does nothing.
       
   724 	Complete(aStatus,KErrNone);
       
   725 	}
       
   726 
       
   727 void CSocketDesc::RecvFrom(TDes8& aDesc, TSockAddr& from, int flags, TRequestStatus& aStatus)
       
   728 	{
       
   729     TInt err = maybe_reopen_socket();
       
   730     if (err != KErrNone)
       
   731         {
       
   732         Complete(aStatus, err);
       
   733         return;
       
   734         }
       
   735 
       
   736 	iReadLock.Wait();
       
   737 	CSockDescBase::RecvFrom(aDesc, from, flags, aStatus);
       
   738 	iReadLock.Signal();
       
   739 	}
       
   740 
       
   741 void CSocketDesc::RecvFromCancel()
       
   742 	{
       
   743 	if (iSocketPtr != NULL)
       
   744 		{
       
   745 		CSockDescBase::RecvFromCancel();
       
   746 		}
       
   747 	}
       
   748 
       
   749 
       
   750 void CSocketDesc::SendTo(TDes8& aDesc, const struct sockaddr* anAddr, unsigned long aAddrLen, int flags, TRequestStatus& aStatus)
       
   751 	{
       
   752     TInt err = maybe_reopen_socket();
       
   753     if (err != KErrNone)
       
   754         {
       
   755         Complete(aStatus, err);
       
   756         return;
       
   757         }
       
   758     
       
   759 	TUSockAddr toAddr(anAddr, aAddrLen);
       
   760 	
       
   761 	iWriteLock.Wait();  
       
   762 	CSockDescBase::SendTo(aDesc, toAddr, flags, aStatus);
       
   763 	iWriteLock.Signal();
       
   764 	}
       
   765 
       
   766 void CSocketDesc::SendToCancel()
       
   767 	{
       
   768 // Should we use atomic loads here?
       
   769 	if (iSocketPtr != NULL)
       
   770 		{
       
   771 		CSockDescBase::SendToCancel();
       
   772 		}
       
   773 	}
       
   774 
       
   775 void CSocketDesc::Shutdown(TUint aHow,TRequestStatus& aStatus)
       
   776 	{
       
   777 
       
   778     if (__e32_atomic_load_acq32(&iSocketPtr) == NULL) // Not opened at all. Nothing to do.
       
   779 		{
       
   780 		Complete(aStatus,KErrNone);
       
   781 		return;
       
   782 		}
       
   783 
       
   784 	CSockDescBase::Shutdown(aHow,aStatus);
       
   785 	return;
       
   786 	}
       
   787 
       
   788 void CSocketDesc::Accept(CFileDescBase*& aNewSocket, TRequestStatus& aStatus, RSocketServ& aSs, TSockAddr * /*aAddr*/)
       
   789 	{
       
   790     aNewSocket = NULL;
       
   791 	TInt err = maybe_reopen_socket();
       
   792 	if (err != KErrNone)
       
   793 	    {
       
   794         Complete(aStatus, err);
       
   795         return;
       
   796 	    }
       
   797 	
       
   798 	iReadLock.Wait();
       
   799 	// what are the below coverity thingummichs?
       
   800 	//coverity[alloc_fn]
       
   801 	//coverity[assign]
       
   802 	CSocketDesc *newSocket = new CSocketDesc;
       
   803 	if (!newSocket)
       
   804 	    {
       
   805         Complete(aStatus, KErrNoMemory);
       
   806         iReadLock.Signal();
       
   807         return;
       
   808 	    }
       
   809 	
       
   810 	err = newSocket->CreateLock();
       
   811 	if (err)
       
   812 		{
       
   813         Complete(aStatus, KErrNoMemory);
       
   814         delete newSocket;
       
   815         iReadLock.Signal();
       
   816         //coverity[memory_leak]
       
   817         return;
       
   818 		}
       
   819 
       
   820 	err = newSocket->iSocket.Open(aSs);
       
   821 	if (err)
       
   822 		{
       
   823 		Complete(aStatus, err);
       
   824 		newSocket->FinalClose();  // this will Close locks
       
   825 		delete newSocket;
       
   826 
       
   827 		iReadLock.Signal();
       
   828 		//coverity[memory_leak]
       
   829 		return;
       
   830 		}
       
   831 	newSocket->iSocketPtr = &newSocket->iSocket;
       
   832 	newSocket->iStyle = iStyle;
       
   833 	err = KErrNone;
       
   834 	ATOMICSOCKETOP( iSocket.Accept(newSocket->iSocket,aStatus), err = KErrBadHandle )
       
   835 	if( err )
       
   836 	    {
       
   837 	    Complete(aStatus, err);
       
   838 	    newSocket->FinalClose();  // this will Close locks
       
   839 	    delete newSocket;
       
   840 	    }
       
   841 	else
       
   842 	    {
       
   843 	    aNewSocket = newSocket;
       
   844 	    }
       
   845 	iReadLock.Signal();
       
   846 	}
       
   847 
       
   848 void CSocketDesc::AcceptCancel()
       
   849 	{
       
   850 	if (iSocketPtr != NULL)
       
   851 		{
       
   852 		ATOMICSOCKETOP( iSocket.CancelAccept(), NOP )
       
   853 		}
       
   854 	}
       
   855 
       
   856 void CSocketDesc::Connect(const struct sockaddr* aAddr,unsigned long size,TRequestStatus& aStatus)
       
   857 	{
       
   858 	TUSockAddr addr(aAddr,size);
       
   859 
       
   860 	if (addr.iError != 0)
       
   861 		{
       
   862 		aStatus = addr.iError;
       
   863 		return;
       
   864 		}
       
   865 	
       
   866 	TInt err = maybe_reopen_socket();
       
   867 	if (err != KErrNone)
       
   868 	    {
       
   869         aStatus = err;
       
   870         return;
       
   871 	    }
       
   872 	
       
   873 	iWriteLock.Wait();
       
   874 	if( GetConnectionProgress() == EFalse )
       
   875 	    {
       
   876 		ATOMICSOCKETOP(iSocket.Connect(addr, aStatus), Complete(aStatus,KErrBadHandle))
       
   877 	    User::WaitForRequest(aStatus);
       
   878 	    if( aStatus.Int() == KErrWouldBlock )
       
   879 	        SetConnectionProgress(ETrue);
       
   880 	    }
       
   881 	else
       
   882 	    {
       
   883 	    aStatus = EALREADY;
       
   884 	    }
       
   885 	iWriteLock.Signal();
       
   886 	}
       
   887 
       
   888 void CSocketDesc::ConnectCancel()
       
   889 	{
       
   890 	if (iSocketPtr != NULL)
       
   891 		{
       
   892 		ATOMICSOCKETOP(iSocket.CancelConnect(),NOP)
       
   893 		}
       
   894 	}
       
   895 
       
   896 void CSocketDesc::Ioctl(int aCmd, void* aParam, TRequestStatus& aStatus)
       
   897 	{
       
   898 	TInt ret = KErrNone;
       
   899 	int* param = reinterpret_cast<int*>(aParam);
       
   900 
       
   901 	ret = maybe_reopen_socket();
       
   902 	if (ret != KErrNone)
       
   903 	    {
       
   904         Complete(aStatus, ret);
       
   905         return;
       
   906 	    }
       
   907 
       
   908 	switch ((unsigned)aCmd)
       
   909 		{
       
   910 		case FIONREAD:
       
   911 		case E32IONREAD:
       
   912 		    {
       
   913 			ATOMICSOCKETOP( ret=iSocket.GetOpt(KSOReadBytesPending,KSOLSocket,*param), ret = KErrBadHandle )
       
   914 		    }
       
   915 			break;
       
   916 		case E32IOSELECT:
       
   917 			{
       
   918 			iIoctlBuf.Set((TText8*)aParam,4,4);
       
   919 			iIoctlLock.Wait();
       
   920 			iIoctlFlag = ETrue;		
       
   921 			ATOMICSOCKETOP(iSocket.Ioctl(KIOctlSelect,aStatus,&iIoctlBuf,KSOLSocket), Complete(aStatus,KErrBadHandle))
       
   922 			}
       
   923 			return;
       
   924 		case SIOCGIFCONF:
       
   925 			ret = GetInterfaceList(aParam);
       
   926 			break;
       
   927 		case SIOCGIFINDEX:
       
   928 			ret = GetInterfaceIndexByName(aParam);
       
   929 			break;
       
   930 		case SIOCGIFACTIVECONF:
       
   931 			ret = GetActiveInterfaceList(aParam);
       
   932 			break;
       
   933 		case SIOCSIFNAME:
       
   934 			ret = SetInterfaceByName(aParam);
       
   935 			break;
       
   936 		case SIOCIFSTART:
       
   937 			ret = StartInterface(aParam);
       
   938 			if(KErrNone == ret)
       
   939 				{
       
   940 				iSocket.Close();
       
   941 				iSocketPtr = NULL;
       
   942 				}
       
   943 			break;
       
   944 		case SIOCIFACTIVESTART:
       
   945 			ret = StartActiveInterface(aParam);
       
   946 			if(KErrNone == ret)
       
   947 				{
       
   948 				iSocket.Close();
       
   949 				iSocketPtr = NULL;
       
   950 				}
       
   951 			break;
       
   952 		case SIOCIFSTOP:
       
   953 			ret = StopInterface(aParam);
       
   954 			break;	
       
   955 		case SIOCATMARK:
       
   956 			ATOMICSOCKETOP(ret=iSocket.GetOpt(KSoTcpRcvAtMark,KSolInetTcp,*param), ret = KErrBadHandle)
       
   957 			break;	
       
   958 		case SIOCGIFADDR:
       
   959 			ret = GetIpAddress(aParam);
       
   960 			break;	
       
   961 		case SIOCGIFNUM:
       
   962 			ret = GetInterafceNumber(aParam);
       
   963 			break;	
       
   964 		case SIOCADDRT:
       
   965 			ret = RouteRequest(KSoInetAddRoute, aParam);
       
   966 			break;	
       
   967 		case SIOCDELRT:		
       
   968 			ret = RouteRequest(KSoInetDeleteRoute, aParam);
       
   969 			break;	
       
   970 		case SIOCGIFHWADDR:
       
   971 			ret = GetInterfaceHWAddress(aParam);
       
   972 		    break;
       
   973 		case SIOCGIFACTIVEIAP:
       
   974 		    ret = GetActiveInterface( aParam);
       
   975 		    break;
       
   976 		
       
   977 		default:
       
   978 			ret=KErrNotSupported;
       
   979 			break;
       
   980 		}
       
   981 	Complete(aStatus,ret);
       
   982 	}
       
   983 
       
   984 TInt CSocketDesc::IoctlCompletion(int /*aCmd*/, void* /*aParam*/, TInt aStatus)
       
   985 	{
       
   986 	if(iIoctlFlag)
       
   987 		{
       
   988 		iIoctlLock.Signal();
       
   989 		iIoctlFlag = EFalse;
       
   990 		}
       
   991 	return aStatus;
       
   992 	}
       
   993 
       
   994 void CSocketDesc::IoctlCancel()
       
   995 	{
       
   996 	if (iSocketPtr && iIoctlFlag)
       
   997 		{
       
   998 		ATOMICSOCKETOP(iSocket.CancelIoctl(), NOP)
       
   999 		iIoctlLock.Signal();
       
  1000 		iIoctlFlag = EFalse;
       
  1001 		}
       
  1002 	}
       
  1003 // -----------------------------------------------------------------------------
       
  1004 // CSocketDesc::Fcntl
       
  1005 // Symbian Socket Specific Implementation for fcntl
       
  1006 // Implementation of fcntl for Socket will make use of  
       
  1007 // RSocket::SetOpt(KSONonBlockingIO/KSOBlockingIO, KSOLSocket)
       
  1008 // As of now fcntl supports F_SETFL and F_GETFL
       
  1009 // -----------------------------------------------------------------------------
       
  1010 //
       
  1011 TInt CSocketDesc::Fcntl(TUint anArg, TUint aCmd)
       
  1012 	{
       
  1013 
       
  1014     TInt err = maybe_reopen_socket();
       
  1015     if (err != KErrNone)
       
  1016         return err;
       
  1017 
       
  1018     return CSockDescBase::Fcntl(anArg, aCmd);
       
  1019 	}
       
  1020 
       
  1021 TInt CSocketDesc :: GetIpAddress( void *aParam )
       
  1022 	{
       
  1023 	TInetAddr myAddr;
       
  1024 	ATOMICSOCKETOP( iSocket.LocalName(myAddr), return KErrBadHandle )
       
  1025 	TUint32 myIP = myAddr.Address();			
       
  1026 	if (myIP == 0)
       
  1027 		{
       
  1028 		return KErrGeneral;
       
  1029 		}
       
  1030 
       
  1031 	ifreq *ifr = (ifreq *)aParam;
       
  1032 	((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr.s_addr = myIP;
       
  1033 	return KErrNone;
       
  1034 	}
       
  1035 
       
  1036 TInt CSocketDesc :: GetRemoteIpAddress( void *aParam )
       
  1037 	{
       
  1038 	TInetAddr remoteAddr;
       
  1039 	ATOMICSOCKETOP( iSocket.RemoteName(remoteAddr), return KErrBadHandle )
       
  1040 	TUint32 remoteIP = remoteAddr.Address();			
       
  1041 
       
  1042 	if (remoteIP == 0)
       
  1043 		{
       
  1044 		return KErrGeneral;
       
  1045 		}
       
  1046 	ifreq *ifr = (ifreq *)aParam;
       
  1047 	((struct sockaddr_in *)&ifr->ifr_dstaddr)->sin_addr.s_addr = remoteIP;
       
  1048 	return KErrNone;
       
  1049 	}
       
  1050 
       
  1051 TInt CSocketDesc :: GetInterafceNumber( void *aParam )
       
  1052 	{
       
  1053 	TInt count = 0;	
       
  1054 	TInt ret = KErrNone;
       
  1055 	TRAP(ret, AccessPointCountL(count));
       
  1056 	*(TInt *)aParam = count;
       
  1057 	return ret;	
       
  1058 	}
       
  1059 
       
  1060 #ifdef __SYMBIAN_COMPILE_UNUSED__
       
  1061 TInt CSocketDesc :: GetInterafceParamInfo( void *aParam,TInt aType)
       
  1062 	{
       
  1063 	TInt ret = KErrNone;	
       
  1064 	TAccessPointRecord tempRecord;
       
  1065 	ifreq *ifr = (ifreq *)aParam;
       
  1066 	TRAP(ret,FindConnectionInfoL(tempRecord,ifr->ifr_name));
       
  1067 	if(ret != KErrNone)
       
  1068 		return ret;
       
  1069 	wchar_t *str = (wchar_t *)( tempRecord.iServiceType.PtrZ());	 
       
  1070 	TInt servFlag = 0;
       
  1071 	if(WcsCmp(str,L"LANService") == 0)
       
  1072 		{
       
  1073 		servFlag = 0;
       
  1074 		}
       
  1075 	else if(WcsCmp(str,L"OutgoingGPRS") == 0)
       
  1076 		{
       
  1077 		servFlag  = 1;	
       
  1078 		}
       
  1079 	if(aType == EACCESS_GETMETRIC)
       
  1080 		{
       
  1081 		ret = GetInterfaceDetails(aParam,servFlag,EACCESS_GETMETRIC);					
       
  1082 		}	
       
  1083 	else if(aType == EACCESS_GETMTU)
       
  1084 		{
       
  1085 		ret = GetInterfaceDetails(aParam,servFlag,EACCESS_GETMTU);					
       
  1086 		}
       
  1087 	else if(aType == EACCESS_GETNETMASK)
       
  1088 		{
       
  1089 		ret = GetInterfaceDetails(aParam,servFlag,EACCESS_GETNETMASK);					
       
  1090 		}
       
  1091 	else if(aType == EACCESS_GETBROADCAST)
       
  1092 		{
       
  1093 		ret = GetInterfaceDetails(aParam,servFlag,EACCESS_GETBROADCAST);					
       
  1094 		}
       
  1095 	else if(aType == EACCESS_GETPHYSADDR)
       
  1096 		{
       
  1097 		ret = GetInterfaceDetails(aParam,servFlag,EACCESS_GETPHYSADDR);					
       
  1098 		}
       
  1099 	else if(aType == EACCESS_GETFLAGS)
       
  1100 		{
       
  1101 		ret = GetInterfaceDetails(aParam,servFlag,EACCESS_GETFLAGS);					
       
  1102 		}
       
  1103 	return ret;	
       
  1104 	}
       
  1105 
       
  1106 TInt CSocketDesc :: SetInterafceParamInfo( void *aParam,TInt aType)
       
  1107 	{
       
  1108 	TInt ret = KErrNone;	
       
  1109 	TAccessPointRecord tempRecord;
       
  1110 	ifreq *ifr = (ifreq *)aParam;
       
  1111 	TRAP(ret,FindConnectionInfoL(tempRecord,ifr->ifr_name));
       
  1112 	if(ret != KErrNone)
       
  1113 		return ret;
       
  1114 	wchar_t *str = (wchar_t *)(tempRecord.iServiceType.PtrZ());	 
       
  1115 	TInt servFlag = 0;
       
  1116 	if(WcsCmp(str,L"LANService") == 0)
       
  1117 		{
       
  1118 		servFlag = 0;
       
  1119 		}
       
  1120 	else if(WcsCmp(str,L"OutgoingGPRS") == 0)
       
  1121 		{
       
  1122 		servFlag  = 1;	
       
  1123 		}
       
  1124 	if(aType == EACCESS_SETMETRIC)
       
  1125 		{
       
  1126 		ret = SetInterfaceDetails(aParam,servFlag,EACCESS_SETMETRIC);					
       
  1127 		}	
       
  1128 	else if(aType == EACCESS_SETMTU)
       
  1129 		{
       
  1130 		ret = SetInterfaceDetails(aParam,servFlag,EACCESS_SETMTU);					
       
  1131 		}		
       
  1132 	else if(aType == EACCESS_SETFLAGS)
       
  1133 		{
       
  1134 		ret = SetInterfaceDetails(aParam,servFlag,EACCESS_SETFLAGS);					
       
  1135 		}
       
  1136 	else if(aType == EACCESS_SETPHYSADDR)
       
  1137 		{
       
  1138 		ret = SetInterfaceDetails(aParam,servFlag,EACCESS_SETPHYSADDR);					
       
  1139 		}	
       
  1140 	else if(aType == EACCESS_SETNETMASK)
       
  1141 		{
       
  1142 		ret = SetInterfaceDetails(aParam,servFlag,EACCESS_SETNETMASK);					
       
  1143 		}	
       
  1144 	else if(aType == EACCESS_SETBROADCAST)
       
  1145 		{
       
  1146 		ret = SetInterfaceDetails(aParam,servFlag,EACCESS_SETBROADCAST);					
       
  1147 		}		
       
  1148 	return ret;	
       
  1149 
       
  1150 	}
       
  1151 
       
  1152 void CSocketDesc::FindConnectionInfoL(TAccessPointRecord &aRecord,char *ptr)
       
  1153 	{
       
  1154 	//TAccessPointRecord tempRecord;
       
  1155 	CCommsDatabase* iapDatabase;
       
  1156 	CCommsDbTableView* view;
       
  1157 	TInt res;	
       
  1158 	TPtrC8 tptr((TText8*)ptr);
       
  1159 	TBuf<KCommsDbSvrMaxColumnNameLength> temp;
       
  1160 	res = CnvUtfConverter::ConvertToUnicodeFromUtf8(temp, tptr);
       
  1161 	if(res < KErrNone)
       
  1162 		{
       
  1163 		User::Leave(res);
       
  1164 		}
       
  1165 	else if(res > KErrNone)
       
  1166 		{
       
  1167 		User::Leave(KErrOverflow);
       
  1168 		}
       
  1169 	// This function can leave on insufficient memory
       
  1170 	OpenIapTableLC(&iapDatabase, &view);	
       
  1171 	res = view->GotoFirstRecord();
       
  1172 	while (res == KErrNone)
       
  1173 		{    	
       
  1174 		// On error continue with the next record	
       
  1175 		TRAP( res,ReadRecordFromIapTableL(view, aRecord));
       
  1176 		if (res == KErrNone)
       
  1177 			{   
       
  1178 			if(WcsCmp ((wchar_t*)aRecord.iName.PtrZ(),(wchar_t*)temp.PtrZ()) == 0)
       
  1179 				break; 	
       
  1180 			}    	
       
  1181 		res = view->GotoNextRecord();
       
  1182 		}
       
  1183 	CleanupStack::PopAndDestroy();// view
       
  1184 	CleanupStack::PopAndDestroy(iapDatabase);	
       
  1185 	return;
       
  1186 	}
       
  1187 
       
  1188 TInt CSocketDesc :: SetInterfaceDetails( void *aParam ,TInt aFlag, TInt aType )
       
  1189 	{
       
  1190 	ifreq *ifr = (ifreq *)aParam;
       
  1191 	TPckgBuf<TSoInetIfQuery> ifq;
       
  1192 	TBuf8 <25> ipBuf8;
       
  1193 	TName aBuf;			
       
  1194 	TInt ret = KErrNone;
       
  1195 	ATOMICSOCKETOP( ret = iSocket.SetOpt(KSoInetEnumInterfaces, KSolInetIfCtrl), ret = KErrBadHandle )
       
  1196 	if (ret != KErrNone)
       
  1197 		{
       
  1198 		return KErrGeneral;
       
  1199 		}    	
       
  1200 	TPckgBuf<TSoInet6InterfaceInfo> info;
       
  1201 	TSoInet6InterfaceInfo &in = info();	
       
  1202 	ATOMICSOCKETOP( ret = iSocket.GetOpt(KSoInetNextInterface, KSolInetIfCtrl, info), ret = KErrBadHandle )
       
  1203 	while(ret == KErrNone)
       
  1204 		{			
       
  1205 		if(info().iName != _L("") && info().iName != _L("loop6") && info().iName != _L("loop4"))
       
  1206 			{   			
       
  1207 			TDes16& aName = info().iName;							
       
  1208 			if( ((aFlag == 0 ) && ( aName.FindC(_L("WLAN")) != KErrNotFound )) ||
       
  1209 					((aFlag == 1) && (aName.FindC(_L("Generic")) != KErrNotFound )) )
       
  1210 				{
       
  1211 				switch(aType)
       
  1212 					{				
       
  1213 					case  EACCESS_SETMETRIC:
       
  1214 						if(info().iState == EIfUp)										 
       
  1215 							{
       
  1216 							info().iSpeedMetric =  ifr->ifr_metric;
       
  1217 							}
       
  1218 						goto setout;
       
  1219 
       
  1220 					case  EACCESS_SETMTU:										 
       
  1221 						if(info().iState == EIfUp)
       
  1222 							{
       
  1223 							info().iMtu = ifr->ifr_mtu ;		
       
  1224 							}		
       
  1225 						goto setout;
       
  1226 
       
  1227 					case  EACCESS_SETNETMASK :																																																					
       
  1228 						// Presently netmask address is NULL											
       
  1229 						if((info().iState == EIfUp) && (ifr->ifr_addr.sa_data !=NULL))
       
  1230 							{																																																		
       
  1231 							/*
       
  1232 							CharToTBuf8(ifr->ifr_addr.sa_data,ipBuf8);											
       
  1233 											if (CnvUtfConverter::ConvertToUnicodeFromUtf8( aBuf,ipBuf8 ) == KErrNone)
       
  1234 											{													
       
  1235 												ret = info().iNetMask.Input(aBuf);												
       
  1236 											}																							
       
  1237 							info().iNetMask.SetAddress(INET_ADDR(255,255,255,0));	
       
  1238 							*/
       
  1239 							return KErrNotSupported;
       
  1240 							}
       
  1241 						break;	
       
  1242 					case  EACCESS_SETBROADCAST :										
       
  1243 						if((info().iState == EIfUp) && (ifr->ifr_broadaddr.sa_data !=NULL))
       
  1244 							{																																																		
       
  1245 							/*CharToTBuf8(ifr->ifr_broadaddr.sa_data,ipBuf8);											
       
  1246 											if (CnvUtfConverter::ConvertToUnicodeFromUtf8( aBuf,ipBuf8 ) == KErrNone)
       
  1247 											{													
       
  1248 												ret = info().iBrdAddr.Input(aBuf);												
       
  1249 											} 
       
  1250 							*/												
       
  1251 							return KErrNotSupported;
       
  1252 							}
       
  1253 
       
  1254 						break;
       
  1255 					case  EACCESS_SETPHYSADDR :										
       
  1256 						// Currently no imeplentation is given as KIfHasHardwareAddr is always 
       
  1257 						// set to 0 for wlan and GPRS
       
  1258 						if(info().iFeatures&KIfHasHardwareAddr)
       
  1259 							{
       
  1260 							return KErrNotSupported;
       
  1261 							}
       
  1262 						break;									
       
  1263 					case  EACCESS_SETFLAGS :										
       
  1264 						info().iFeatures = 0;
       
  1265 						// Interface UP
       
  1266 						if((ifr->ifr_flags & IFF_UP) && (ifr->ifr_flags & IFF_DRV_RUNNING)) 
       
  1267 							{
       
  1268 							info().iState = EIfUp;																						
       
  1269 							}																						
       
  1270 						else
       
  1271 							{
       
  1272 							info().iState = EIfDown;																																	
       
  1273 							}																																	
       
  1274 
       
  1275 						// Loopback										
       
  1276 						if(ifr->ifr_flags & IFF_LOOPBACK)
       
  1277 							{
       
  1278 							info().iFeatures |= KIfIsLoopback;																				            													           
       
  1279 							}																				            													           
       
  1280 
       
  1281 						// point to point support
       
  1282 						if(ifr->ifr_flags &  IFF_POINTOPOINT)
       
  1283 							{
       
  1284 							info().iFeatures |= KIfIsPointToPoint; 							                
       
  1285 							} 							                
       
  1286 
       
  1287 						// Broadcast
       
  1288 						if(ifr->ifr_flags & IFF_BROADCAST)
       
  1289 							{
       
  1290 							info().iFeatures |=KIfCanBroadcast; 
       
  1291 							} 
       
  1292 
       
  1293 
       
  1294 						// Multicast
       
  1295 						if(ifr->ifr_flagshigh & IFF_MULTICAST)
       
  1296 							{
       
  1297 							info().iFeatures = KIfCanMulticast;							            	      
       
  1298 							}
       
  1299 						//these flags details are available in symbian but not used by lib layer.
       
  1300 						/* if(info().iFeatures&KIfCanSetMTU)			            	
       
  1301 							               if(info().iFeatures&KIfHasHardwareAddr)    
       
  1302 							               if(info().iFeatures&KIfCanSetHardwareAddr) */
       
  1303 						goto setout;							            	
       
  1304 
       
  1305 					default:
       
  1306 						break;					 
       
  1307 					}
       
  1308 				}
       
  1309 
       
  1310 			}
       
  1311 		ATOMICSOCKETOP( ret = iSocket.GetOpt(KSoInetNextInterface, KSolInetIfCtrl, info), ret = KErrBadHandle )
       
  1312 		}
       
  1313 	setout:	
       
  1314 	TPckgBuf<TSoInet6InterfaceInfo> changeToNew(info());
       
  1315 	ATOMICSOCKETOP(ret = iSocket.SetOpt(KSoInetConfigInterface, KSolInetIfCtrl,changeToNew), return KErrBadHandle )
       
  1316 	return ret;
       
  1317 	}
       
  1318 #endif // __SYMBIAN_COMPILE_UNUSED__
       
  1319 
       
  1320 TInt CSocketDesc::GetInterfaceDetails( void *aParam ,TInt aFlag, TInt aType )
       
  1321 	{
       
  1322 	TPckgBuf<TSoInetIfQuery> ifq;
       
  1323 
       
  1324 	TInt ret = KErrNone; 
       
  1325 	ATOMICSOCKETOP( ret = iSocket.SetOpt(KSoInetEnumInterfaces, KSolInetIfCtrl), ret = KErrBadHandle )
       
  1326 	if (ret != KErrNone)
       
  1327 		{
       
  1328 		return KErrGeneral;
       
  1329 		}
       
  1330 	
       
  1331 	ifreq *ifr = (ifreq *)aParam;
       
  1332     *(ifr->ifr_addr.sa_data) = '\0';
       
  1333 
       
  1334 	TPckgBuf<TSoInetInterfaceInfo> info;
       
  1335 	ATOMICSOCKETOP( ret = iSocket.GetOpt(KSoInetNextInterface, KSolInetIfCtrl, info), ret = KErrBadHandle )
       
  1336 	while( ret == KErrNone)
       
  1337 		{
       
  1338 
       
  1339 		if(info().iName != _L("") && info().iName != _L("loop6") && info().iName != _L("loop4"))
       
  1340 			{   
       
  1341 			TDes16& aName = info().iName;
       
  1342 			TName aBuf;
       
  1343 			TBuf8<KMaxName> ipAddr;
       
  1344 			if( ((aFlag == 0 ) && ( aName.FindC(_L("WLAN")) != KErrNotFound )) ||
       
  1345 					((aFlag == 1) && (aName.FindC(_L("Generic")) != KErrNotFound )) )
       
  1346 				{
       
  1347 				switch(aType)
       
  1348 					{
       
  1349 
       
  1350 					case  EACTIVE_GETIP :
       
  1351 						if((info().iState == EIfUp) && (info().iAddress.Address() != NULL))
       
  1352 							{
       
  1353 							if(!((info().iAddress.IsLinkLocal()) || (info().iAddress.IsSiteLocal())))
       
  1354 								{
       
  1355 								info().iAddress.Output(aBuf);  
       
  1356 								if (CnvUtfConverter::ConvertFromUnicodeToUtf8( ipAddr, aBuf ) == KErrNone)
       
  1357 									{			
       
  1358 									StrlCopy(ifr->ifr_addr.sa_data,(const char *) ipAddr.PtrZ(),ipAddr.Length()+1);														
       
  1359 									}  
       
  1360 								}
       
  1361 							}
       
  1362 						break;
       
  1363 					case  EACCESS_GETMETRIC:
       
  1364 						ifr->ifr_metric = 0;
       
  1365 						if (info().iState == EIfUp)
       
  1366 							{
       
  1367 							ifr->ifr_metric = info().iSpeedMetric;
       
  1368 							}
       
  1369 						break;
       
  1370 					case  EACCESS_GETMTU:
       
  1371 						ifr->ifr_mtu = 0;
       
  1372 						if (info().iState == EIfUp)
       
  1373 							{
       
  1374 							ifr->ifr_mtu = info().iMtu;
       
  1375 							}
       
  1376 						break;	
       
  1377 					case  EACCESS_GETNETMASK :
       
  1378 						*(ifr->ifr_addr.sa_data) = '\0';
       
  1379 						// Presently netmask address is NULL
       
  1380 						if((info().iState == EIfUp) && (info().iNetMask.Address() != NULL))
       
  1381 							{
       
  1382 							//anAddr = info().iNetMask.Address();	
       
  1383 							info().iNetMask.Output(aBuf);  
       
  1384 							if (CnvUtfConverter::ConvertFromUnicodeToUtf8( ipAddr, aBuf ) == KErrNone)
       
  1385 								{			
       
  1386 								StrlCopy(ifr->ifr_addr.sa_data,(const char *) ipAddr.PtrZ(),ipAddr.Length()+1);												
       
  1387 								}  											
       
  1388 							}
       
  1389 						break;	
       
  1390 					case  EACCESS_GETBROADCAST :
       
  1391 						*(ifr->ifr_broadaddr.sa_data) = '\0';
       
  1392 						// Presently Breaodcast address is NULL
       
  1393 						if((info().iState == EIfUp) && (info().iBrdAddr.Address() != NULL))
       
  1394 							{
       
  1395 
       
  1396 							//anAddr = info().iBrdAddr.Address();	
       
  1397 							info().iBrdAddr.Output(aBuf);  
       
  1398 							if (CnvUtfConverter::ConvertFromUnicodeToUtf8( ipAddr, aBuf ) == KErrNone)
       
  1399 								{			
       
  1400 								StrlCopy(ifr->ifr_broadaddr.sa_data,(const char *) ipAddr.PtrZ(),ipAddr.Length()+1);												
       
  1401 								}											
       
  1402 							}
       
  1403 						break;
       
  1404 					case  EACCESS_GETPHYSADDR :
       
  1405 						ifr->ifr_phys = 0;
       
  1406 						// Currently no imeplentation is given as KIfHasHardwareAddr is always 
       
  1407 						// set to 0 for wlan and GPRS
       
  1408 						if(info().iFeatures&KIfHasHardwareAddr)
       
  1409 							{
       
  1410 							//nada.
       
  1411 							}
       
  1412 						break;									
       
  1413 					case  EACCESS_GETFLAGS :
       
  1414 						ifr->ifr_flags = 0;
       
  1415 						ifr->ifr_flagshigh=0;
       
  1416 						// Interface UP
       
  1417 						if(info().iState == EIfUp)
       
  1418 							{
       
  1419 							ifr->ifr_flags |= IFF_UP; 
       
  1420 							ifr->ifr_flags |= IFF_DRV_RUNNING;
       
  1421 							}
       
  1422 						// Loopback
       
  1423 						if(info().iFeatures&KIfIsLoopback)
       
  1424 							{
       
  1425 							ifr->ifr_flags |= IFF_LOOPBACK;
       
  1426 							}										            													           
       
  1427 
       
  1428 						// point to point support
       
  1429 						if(info().iFeatures&KIfIsPointToPoint) 
       
  1430 							{
       
  1431 							ifr->ifr_flags |= IFF_POINTOPOINT;	
       
  1432 							}
       
  1433 						
       
  1434 						// Broadcast
       
  1435 						if(info().iFeatures&KIfCanBroadcast)
       
  1436 							{
       
  1437 							ifr->ifr_flags |= IFF_BROADCAST;
       
  1438 							}      
       
  1439 
       
  1440 						// Multicast
       
  1441 						if(info().iFeatures&KIfCanMulticast)
       
  1442 							{
       
  1443 							ifr->ifr_flagshigh |= ((IFF_MULTICAST & 0xff00) >> 8);
       
  1444 							}
       
  1445 						//these flags details are available in symbian but not used by lib layer.
       
  1446 						/* if(info().iFeatures&KIfCanSetMTU)			            	
       
  1447 							               if(info().iFeatures&KIfHasHardwareAddr)    
       
  1448 							               if(info().iFeatures&KIfCanSetHardwareAddr) */
       
  1449 
       
  1450 						break;																 				 
       
  1451 				 
       
  1452 					}
       
  1453 				}
       
  1454 
       
  1455 			}
       
  1456 		ATOMICSOCKETOP( ret = iSocket.GetOpt(KSoInetNextInterface, KSolInetIfCtrl, info), ret = KErrBadHandle )
       
  1457 		}
       
  1458 
       
  1459 	return KErrNone;	
       
  1460 	}
       
  1461 
       
  1462 TInt CSocketDesc::Poll(TPollMode aMode,TBool& aReadyStatus,TInt& aErrno)
       
  1463 	{
       
  1464 	TInt ret = maybe_reopen_socket();
       
  1465     if (ret != KErrNone)
       
  1466         {
       
  1467         return ret;
       
  1468         }
       
  1469 
       
  1470     TInt status;
       
  1471     ATOMICSOCKETOP(ret = iSocket.GetOpt(KSOSelectPoll, KSOLSocket, status), ret = KErrBadHandle)
       
  1472     if (ret == KErrNone)
       
  1473 		{
       
  1474     	aReadyStatus = status & aMode;	
       
  1475 		}
       
  1476     
       
  1477     return MapError(ret, aErrno);
       
  1478 	}
       
  1479 
       
  1480 /* The synchronous - non-blocking Poll */
       
  1481 TInt CSocketDesc::Poll(TUint aEvents)
       
  1482 	{
       
  1483 
       
  1484     TInt err = maybe_reopen_socket();
       
  1485 	if (err != KErrNone)
       
  1486 	    return err;
       
  1487 		
       
  1488 	return CSockDescBase::Poll(aEvents);
       
  1489 	}
       
  1490 
       
  1491 
       
  1492 /* Cancel an outstanding notification request */
       
  1493 void CSocketDesc::CancelNotify()
       
  1494 	{
       
  1495 	CSockDescBase::CancelNotify();
       
  1496 	}
       
  1497 
       
  1498 TAccessPointRecord::TAccessPointRecord()
       
  1499 :iId(0), 
       
  1500 iName(0),
       
  1501 iDialogPref(ECommDbDialogPrefDoNotPrompt),
       
  1502 iDirection(ECommDbConnectionDirectionUnknown), 
       
  1503 iService(0),
       
  1504 iServiceType(0), // Text value
       
  1505 iBearer(0),
       
  1506 iBearerType(0),	// Text value
       
  1507 iNetwork(0),
       
  1508 iNetworkWeighting(0),
       
  1509 iLocation(0)
       
  1510 		{
       
  1511 		}
       
  1512 
       
  1513 inline TInt CSocketDesc::GetInterfaceList(void *aParam)
       
  1514 	{
       
  1515 	return GetInterface(aParam, EACCESS_POINT);
       
  1516 	}
       
  1517 
       
  1518 TInt CSocketDesc::GetInterfaceIndexByName(void *aParam)
       
  1519 	{
       
  1520 	ifreq *ifrQuery = (ifreq*)aParam;	
       
  1521 	ifconf ifc;
       
  1522 
       
  1523 	// Know how many interfaces
       
  1524 	ifc.ifc_len = 0;
       
  1525 	ifc.ifc_buf = NULL;
       
  1526 	TInt ret = GetInterface(&ifc, EACCESS_POINT);
       
  1527 	if (ret != KErrNone)
       
  1528 		{
       
  1529 		return ret;
       
  1530 		}
       
  1531 
       
  1532 	// Allocate the memory to get the detail interface information
       
  1533 	ifc.ifc_buf = (caddr_t)Backend()->Alloc( ifc.ifc_len);
       
  1534 	if (ifc.ifc_buf == NULL)
       
  1535 		{
       
  1536 		return KErrNoMemory;
       
  1537 		}
       
  1538 	ret = GetInterface(&ifc, EACCESS_POINT);
       
  1539 	if (ret != KErrNone)
       
  1540 		{
       
  1541 		Backend()->Free(ifc.ifc_buf);
       
  1542 		return ret;
       
  1543 		}
       
  1544 	ifreq *ifr = ifc.ifc_req;
       
  1545 	TInt ifCount = ifc.ifc_len / sizeof(ifreq);
       
  1546 	TInt i = 0;
       
  1547 
       
  1548 	// We wouldn't need this if we were using a StrNcmp below
       
  1549 	ifrQuery->ifr_name[IFNAMSIZ-1] = 0; // don't assume NULL terminated input
       
  1550 	// Search for the interface name
       
  1551 	for (; i < ifCount; i++, ifr++)
       
  1552 		{
       
  1553 		if ( StrCmp(ifrQuery->ifr_name, ifr->ifr_name) == 0)
       
  1554 			{
       
  1555 			ifrQuery->ifr_index = ifr->ifr_index;
       
  1556 			break;
       
  1557 			}
       
  1558 		}
       
  1559 
       
  1560 	if (i == ifCount)
       
  1561 		{
       
  1562 		ret = KErrNotFound;
       
  1563 		}
       
  1564 	Backend()->Free(ifc.ifc_buf);
       
  1565 	return ret;
       
  1566 	}
       
  1567 
       
  1568 inline TInt CSocketDesc::GetActiveInterfaceList(void *aParam)
       
  1569 	{
       
  1570 	return GetInterface(aParam, EACTIVE_CONNECTION);
       
  1571 	}
       
  1572 
       
  1573 TInt CSocketDesc::GetInterface(void *aParam, TInt aType)
       
  1574 	{
       
  1575 	TInt ret = KErrNone;
       
  1576 	ifconf *ifc = (ifconf*)aParam;	
       
  1577 	TInt length = (ifc->ifc_len) / sizeof(ifreq);
       
  1578 	TInt count = 0;
       
  1579 	// If the length is zero, fill the number of interface information
       
  1580 	if ( length <= 0 || ifc->ifc_buf == NULL )
       
  1581 		{
       
  1582 		if (aType == EACCESS_POINT)
       
  1583 			{
       
  1584 			TRAP(ret, AccessPointCountL(count));
       
  1585 			}
       
  1586 		else if (aType == EACTIVE_CONNECTION)
       
  1587 			{
       
  1588 			ret = ActiveConnectionCount(count);
       
  1589 			}
       
  1590 		else
       
  1591 			{
       
  1592 			ret = KErrArgument;
       
  1593 			}
       
  1594 
       
  1595 		if (ret == KErrNone)
       
  1596 		    {
       
  1597             ifc->ifc_len = sizeof(ifreq) * count;
       
  1598 		    }
       
  1599 		return ret;
       
  1600 		}
       
  1601 
       
  1602 	// length is not 0, read 'length' number of records
       
  1603 	CArrayFixFlat<TAccessPointRecord> *apArray;
       
  1604 	wchar_t *str;
       
  1605 	TInt apIndex;
       
  1606 	TInt result = KErrNone;
       
  1607 	ifreq *ifr = ifc->ifc_req;
       
  1608 	TInt ActiveSet = 0;
       
  1609 
       
  1610 	// Fixed array of 'length'
       
  1611 	apArray = new CArrayFixFlat<TAccessPointRecord>(length);
       
  1612 	if (apArray == NULL)
       
  1613 		{
       
  1614 		return KErrNoMemory;
       
  1615 		}
       
  1616 	if (aType == EACCESS_POINT)
       
  1617 		{
       
  1618 		// On return length would contain the number of access points.
       
  1619 		TRAP(result, AccessPointListL(apArray, length));
       
  1620 		}
       
  1621 	else if (aType == EACTIVE_CONNECTION)
       
  1622 		{
       
  1623 		TRAP(ret, ret=ActiveConnectionListL(apArray, length));
       
  1624 		if( ret == KErrNone )
       
  1625 			{
       
  1626 			TRAP(ret,FindConnectionDetailsL(apArray, length));
       
  1627 			ActiveSet = 1;  	
       
  1628 			}
       
  1629 		}
       
  1630 	if (ret != KErrNone)
       
  1631 		{
       
  1632 		delete apArray;
       
  1633 		return ret;
       
  1634 		}
       
  1635 	for (apIndex = 0; apIndex < length; apIndex++,ifr++)
       
  1636 		{
       
  1637 		TAccessPointRecord& ref = (*apArray)[apIndex];
       
  1638 		ifr->ifr_name[IFNAMSIZ-1] = 0; // don't assume NULL terminated input
       
  1639 		TPtr8 ptr((TText8*)ifr->ifr_name, IFNAMSIZ);
       
  1640 
       
  1641 		ret = CnvUtfConverter::ConvertFromUnicodeToUtf8(ptr, ref.iName);
       
  1642 		if(ret == KErrNone)
       
  1643 			{
       
  1644 			if(ptr.Length() < ptr.MaxLength())
       
  1645 				{
       
  1646 				ptr.ZeroTerminate();
       
  1647 				} 
       
  1648 			else
       
  1649 				{
       
  1650 				return KErrOverflow;
       
  1651 				} 
       
  1652 			}
       
  1653 		else if (ret > KErrNone)
       
  1654 			{
       
  1655 			return KErrOverflow;
       
  1656 			} 
       
  1657 		else
       
  1658 			{
       
  1659 			return ret;
       
  1660 			}
       
  1661 
       
  1662 		// Copy theinterface index
       
  1663 		ifr->ifr_index = ref.iId;
       
  1664 		if(ActiveSet == 1)
       
  1665 			{
       
  1666 			str = (wchar_t *)(ref.iServiceType.PtrZ() ); 
       
  1667 			TInt servFlag = 0;
       
  1668 			if(WcsCmp(str,L"LANService") == 0)
       
  1669 				{
       
  1670 				servFlag = 0;
       
  1671 				}
       
  1672 			else if(WcsCmp(str,L"OutgoingGPRS") == 0)
       
  1673 				{
       
  1674 				servFlag  = 1;	
       
  1675 				}
       
  1676 			GetInterfaceDetails(ifr,servFlag,EACTIVE_GETIP);
       
  1677 			}
       
  1678 		}
       
  1679 	ifc->ifc_len = sizeof(ifreq) * apIndex;
       
  1680 	delete apArray;	
       
  1681 	return KErrNone;
       
  1682 	}
       
  1683 
       
  1684 TInt CSocketDesc::SetInterfaceByName(void *aParam)
       
  1685 	{
       
  1686 	ifreq *ifr = (ifreq *)aParam;
       
  1687 	if (!ifr)
       
  1688 		{
       
  1689 		return KErrArgument;
       
  1690 		}
       
  1691 
       
  1692 	ifr->ifr_name[IFNAMSIZ-1] = 0; // don't assume NULL terminated input
       
  1693 	TPtrC8 ptr((TText8*)ifr->ifr_name);
       
  1694 	TInt ret = CnvUtfConverter::ConvertToUnicodeFromUtf8(iConnPref.iName,ptr);
       
  1695 	if(ret > KErrNone )
       
  1696 		{
       
  1697 		return KErrOverflow;
       
  1698 		}
       
  1699 	return ret; 
       
  1700 	}
       
  1701 
       
  1702 inline TInt CSocketDesc::StartInterface(void *aParam)
       
  1703 	{
       
  1704 	return StartConnection(aParam);	
       
  1705 	}
       
  1706 
       
  1707 inline TInt CSocketDesc::StartActiveInterface(void *aParam)
       
  1708 	{
       
  1709 	return StartSubConnection(aParam);	
       
  1710 	}
       
  1711 
       
  1712 TInt CSocketDesc::StopInterface(void *)
       
  1713 	{
       
  1714 	if (iConnectionPtr != NULL)
       
  1715 		{
       
  1716 		StopConnection();
       
  1717 		return KErrNone;
       
  1718 		}
       
  1719 	else if (iSubConnectionPtr != NULL)
       
  1720 		{
       
  1721 		StopSubConnection();
       
  1722 		return KErrNone;
       
  1723 		}	
       
  1724 	return KErrNotFound;
       
  1725 	}
       
  1726 
       
  1727 TInt CSocketDesc::OpenUsingPreference()
       
  1728 	{
       
  1729 	TInt ret = 0;	
       
  1730     // Update when supporting INET6
       
  1731     TInt addrfamily = (iAddrFamily == AF_INET6 ? KAfInet : iAddrFamily);	
       
  1732 	if (iConnectionPtr != NULL) // iConnection initialized
       
  1733 		{
       
  1734 		ret = iSocket.Open(*iSockServPtr,addrfamily,iStyle,iProtocol,iConnection);
       
  1735 		}
       
  1736 	else if (iSubConnectionPtr != NULL) // iSubConnection initialized
       
  1737 		{
       
  1738 		ret = iSocket.Open(*iSockServPtr,addrfamily,iStyle,iProtocol,iSubConnection);
       
  1739 		}
       
  1740 	else
       
  1741 	    {
       
  1742 	    RConnection& defConnection = Backend()->GetDefaultConnection();
       
  1743 	    //Now check if the default connection is intialized. This is given lesser 
       
  1744 	     //priority than the socket specific preferences checked above.
       
  1745 	     if(defConnection.SubSessionHandle() != 0)
       
  1746 	         {
       
  1747 	         ret = iSocket.Open(*iSockServPtr,addrfamily,iStyle,iProtocol,defConnection);	         
       
  1748 	         if (!ret)
       
  1749 	             {
       
  1750 	             Backend()->AddSocket(this);
       
  1751 	             }
       
  1752 	         }
       
  1753 	     else // No connection preference is set
       
  1754 	         {
       
  1755 	         ret = ECONNABORTED;
       
  1756             }
       
  1757         }
       
  1758     
       
  1759     if (ret == KErrNone)
       
  1760         {
       
  1761         __e32_atomic_store_rel32(&iSocketPtr, (unsigned long)&iSocket);
       
  1762         }
       
  1763     
       
  1764     iConnectInProgress = EFalse;
       
  1765 
       
  1766     return ret;
       
  1767     }
       
  1768 
       
  1769 void CSocketDesc::TempClose()
       
  1770     {
       
  1771     TUint32 ret = __e32_atomic_ior_ord32((void *)&iCount,0x8000);
       
  1772     if( ret >= 0x8000 )
       
  1773         {
       
  1774         // This indicates a TempClose has already been done from one of the threads
       
  1775         return;
       
  1776         }
       
  1777     // loop and yeild till no more references are held to the iSocket
       
  1778     while( iCount != 0x8000 )
       
  1779         {
       
  1780         // Yeild for 1 ms
       
  1781         User::After(1000);
       
  1782         }
       
  1783     if (iSocket.SubSessionHandle() != 0)
       
  1784         {
       
  1785 	    iSocketPtr = NULL;
       
  1786         iSocket.CancelAll();    
       
  1787         TRequestStatus status;
       
  1788         iSocket.Shutdown(RSocket::EImmediate, status);
       
  1789         User::WaitForRequest(status);
       
  1790         iSocket.Close();
       
  1791         }
       
  1792     }
       
  1793 
       
  1794 void CSocketDesc::AccessPointListL(CArrayFixFlat<TAccessPointRecord> *&aRecordPtr,
       
  1795 		TInt &aCount)
       
  1796 	{
       
  1797 	TAccessPointRecord tempRecord;
       
  1798 	CCommsDatabase* iapDatabase;
       
  1799 	CCommsDbTableView* view;
       
  1800 
       
  1801 	// This function can leave on insufficient memory
       
  1802 	OpenIapTableLC(&iapDatabase, &view);
       
  1803 	TInt count = 0;
       
  1804 	TInt ret = view->GotoFirstRecord();
       
  1805     while (ret == KErrNone && count < aCount)
       
  1806 		{
       
  1807     	TRAP(ret, ReadRecordFromIapTableL(view, tempRecord));
       
  1808     	if (ret == KErrNone)
       
  1809 			{
       
  1810 			aRecordPtr->AppendL(tempRecord);
       
  1811 			count++;
       
  1812 			}	
       
  1813     	ret = view->GotoNextRecord();
       
  1814 		}
       
  1815     
       
  1816 	CleanupStack::PopAndDestroy();// view
       
  1817 	CleanupStack::PopAndDestroy(iapDatabase);
       
  1818 	aCount = aRecordPtr->Count();
       
  1819 	}
       
  1820 
       
  1821 void CSocketDesc::FindConnectionDetailsL(CArrayFixFlat<TAccessPointRecord> *&aRecordPtr,
       
  1822 		TInt &aCount)
       
  1823 	{
       
  1824 	TAccessPointRecord tempRecord;
       
  1825 	CCommsDatabase* iapDatabase;
       
  1826 	CCommsDbTableView* view;
       
  1827 
       
  1828 	// This function can leave on insufficient memory
       
  1829 	OpenIapTableLC(&iapDatabase, &view);
       
  1830 	TInt count = 0;
       
  1831 	TInt ret = view->GotoFirstRecord();
       
  1832     while (ret == KErrNone && count < aCount)
       
  1833 		{
       
  1834     	TRAP(ret, ReadRecordFromIapTableL(view, tempRecord));
       
  1835     	if (ret == KErrNone)
       
  1836 			{    		
       
  1837 			if((*aRecordPtr)[count].iId == tempRecord.iId )    			
       
  1838 				{    			    		
       
  1839 				(*aRecordPtr)[count].iServiceType = tempRecord.iServiceType;
       
  1840 				ret = view->GotoFirstRecord();
       
  1841 				count++;    		
       
  1842 				continue;
       
  1843 				}    		
       
  1844     		ret = view->GotoNextRecord();
       
  1845 			}
       
  1846 		}
       
  1847     
       
  1848 	CleanupStack::PopAndDestroy();// view
       
  1849 	CleanupStack::PopAndDestroy(iapDatabase);
       
  1850 	aCount = aRecordPtr->Count();
       
  1851 		}
       
  1852 
       
  1853 void CSocketDesc::AccessPointCountL(TInt &aCount)
       
  1854 	{
       
  1855 	CCommsDatabase* iapDatabase;
       
  1856 	CCommsDbTableView* view;
       
  1857 
       
  1858 	OpenIapTableLC(&iapDatabase, &view);
       
  1859 	aCount = 0;
       
  1860 	TInt ret = view->GotoFirstRecord();
       
  1861 	while(ret == KErrNone)
       
  1862 		{
       
  1863 		aCount++;
       
  1864 		ret = view->GotoNextRecord();
       
  1865 		}
       
  1866 	CleanupStack::PopAndDestroy();// view
       
  1867 	CleanupStack::PopAndDestroy(iapDatabase);
       
  1868 	}
       
  1869 
       
  1870 void CSocketDesc::ReadRecordFromIapTableL(CCommsDbTableView* aView, TAccessPointRecord &aRecord)
       
  1871 	{
       
  1872 	aView->ReadUintL(TPtrC(COMMDB_ID), aRecord.iId);
       
  1873 	aView->ReadTextL(TPtrC(COMMDB_NAME), aRecord.iName); 
       
  1874 	aRecord.iDialogPref = ECommDbDialogPrefDoNotPrompt;
       
  1875 	aView->ReadUintL(TPtrC(IAP_SERVICE), aRecord.iService);
       
  1876 	aView->ReadTextL(TPtrC(IAP_SERVICE_TYPE), aRecord.iServiceType);
       
  1877 	aView->ReadUintL(TPtrC(IAP_BEARER), aRecord.iBearer);
       
  1878 	/*
       
  1879 	 * IAP_BEARER_TYPE is not a column of IAP table even though its 
       
  1880 	 * listed in header. So not trying to read it.
       
  1881 	 */
       
  1882 	aView->ReadUintL(TPtrC(IAP_NETWORK), aRecord.iNetwork);
       
  1883 	aView->ReadUintL(TPtrC(IAP_NETWORK_WEIGHTING), aRecord.iNetworkWeighting);
       
  1884 	aView->ReadUintL(TPtrC(IAP_LOCATION), aRecord.iLocation);
       
  1885 	}
       
  1886 
       
  1887 void CSocketDesc::OpenIapTableLC(CCommsDatabase **aIapDatabase, CCommsDbTableView **aView)
       
  1888 	{
       
  1889 	// Get IAP names and ids from the database
       
  1890 	*aIapDatabase = CCommsDatabase::NewL(EDatabaseTypeIAP);
       
  1891 	CleanupStack::PushL(*aIapDatabase);
       
  1892 
       
  1893 	(*aIapDatabase)->ShowHiddenRecords();
       
  1894 
       
  1895 	// This is LC code, that means code has pushed, but not popped
       
  1896 	(*aView) = (*aIapDatabase)->OpenTableLC(TPtrC(IAP));
       
  1897 	}
       
  1898 
       
  1899 inline TInt CSocketDesc::ActiveConnectionCount(TInt &aCount)
       
  1900 	{
       
  1901 	aCount = ((CFileTable*)iFids)->RConnectionCount();
       
  1902 	return KErrNone;
       
  1903 	}
       
  1904 
       
  1905 TInt CSocketDesc::ActiveConnectionListL(CArrayFixFlat<TAccessPointRecord> *aRecordPtr, TInt &aLength)
       
  1906 	{
       
  1907 	TInt rcIndex;
       
  1908 	RConnection *rc;
       
  1909 	TAccessPointRecord tempRecord;
       
  1910 
       
  1911 	for (rcIndex = 0;rcIndex < aLength; rcIndex++)
       
  1912 		{
       
  1913 		if  ( ((CFileTable*)iFids)->RConnectionAt(rcIndex, rc) != KErrNone )
       
  1914 			{
       
  1915 			break;
       
  1916 			}
       
  1917 		// Read index and name of the connection
       
  1918 		if  ( GetRConnectionDetails(rc, tempRecord) != KErrNone )
       
  1919 			{
       
  1920 			break;
       
  1921 			}
       
  1922 		aRecordPtr->AppendL(tempRecord);
       
  1923 		}
       
  1924 	aLength = aRecordPtr->Count();
       
  1925 	return KErrNone;
       
  1926 	}
       
  1927 
       
  1928 TInt CSocketDesc::GetRConnectionDetails(RConnection *aRc, TAccessPointRecord &aApr)
       
  1929 	{
       
  1930 	_LIT(KName, "IAP\\Name");
       
  1931 	_LIT(KId, "IAP\\Id");
       
  1932 
       
  1933 	TInt ret = aRc->GetDesSetting(KName, aApr.iName);
       
  1934 	if (ret == KErrNone)
       
  1935 		{
       
  1936 		ret = aRc->GetIntSetting(KId, aApr.iId);
       
  1937 		}
       
  1938 	
       
  1939 		return ret;
       
  1940 		}
       
  1941 
       
  1942 TInt CSocketDesc::StartConnection(void * /* ptr */)
       
  1943 	{
       
  1944 	if (iConnectionPtr != NULL)
       
  1945 		{
       
  1946 		return KErrAlreadyExists;
       
  1947 		}
       
  1948 	TCommDbConnPref connPref;
       
  1949 	TInt ret = GetConnectionPreference(connPref);
       
  1950 	if (ret != KErrNone)
       
  1951 		{
       
  1952 		return ret;
       
  1953 		}
       
  1954 	ret = iConnection.Open(*iSockServPtr);
       
  1955 	if (ret != KErrNone)
       
  1956 		{
       
  1957 		return ret;
       
  1958 		}	
       
  1959 	ret = iConnection.Start(connPref);
       
  1960 	if (ret != KErrNone)
       
  1961 		{
       
  1962 		iConnection.Close();
       
  1963 		return ret;
       
  1964 		}
       
  1965 	iConnectionPtr = &iConnection;
       
  1966 	ret = ((CFileTable*)iFids)->AddRConnectionPtr(iConnectionPtr, iRConnectionIndex);
       
  1967 	if (ret != KErrNone)
       
  1968 		{
       
  1969 		iConnection.Close();
       
  1970 		iConnectionPtr = NULL;
       
  1971 		}
       
  1972 	return ret;
       
  1973 	}
       
  1974 
       
  1975 TInt CSocketDesc::GetConnectionPreference(TCommDbConnPref &aCommPref)
       
  1976 	{
       
  1977 	if (iConnPref.iName.Length() == 0)
       
  1978 		{
       
  1979 		return KErrArgument;
       
  1980 		}
       
  1981 	
       
  1982 	TAccessPointRecord apr;	
       
  1983 	TInt ret;
       
  1984 
       
  1985 	// Connection preference is set
       
  1986 		TRAP(ret, GetIapDetailsByNameL(iConnPref.iName, apr));
       
  1987 
       
  1988 	if (ret == KErrNone)
       
  1989 		{
       
  1990 		aCommPref.SetIapId(apr.iId);
       
  1991 		aCommPref.SetNetId(apr.iNetwork);
       
  1992 		aCommPref.SetBearerSet(apr.iBearer);		
       
  1993 		aCommPref.SetDialogPreference((TCommDbDialogPref)apr.iDialogPref);
       
  1994 		aCommPref.SetDirection((TCommDbConnectionDirection)apr.iDirection);
       
  1995 		}
       
  1996 	return ret;	
       
  1997 	}
       
  1998 
       
  1999 void CSocketDesc::GetIapDetailsByNameL(
       
  2000 		TBuf<KCommsDbSvrMaxColumnNameLength> aIapName, 
       
  2001 		TAccessPointRecord &aRecord)
       
  2002 	{
       
  2003 	TAccessPointRecord tempRecord;
       
  2004 	CCommsDatabase* iapDatabase;
       
  2005 	CCommsDbTableView* view;
       
  2006 	TInt ret = KErrNotFound;
       
  2007 
       
  2008 	OpenIapTableLC(&iapDatabase, &view);
       
  2009 	ret = view->GotoFirstRecord();
       
  2010 	while (ret == KErrNone)
       
  2011 		{
       
  2012 		TRAP(ret, ReadRecordFromIapTableL(view, tempRecord));
       
  2013 		if (ret == KErrNone)
       
  2014 			{
       
  2015 			if (aIapName == tempRecord.iName)
       
  2016 				{
       
  2017 				aRecord = tempRecord;
       
  2018 				ret = KErrNone;
       
  2019 				break;
       
  2020 				}
       
  2021 			}    	
       
  2022 		ret = view->GotoNextRecord();
       
  2023 		}
       
  2024 	CleanupStack::PopAndDestroy();// view
       
  2025 	CleanupStack::PopAndDestroy(iapDatabase);
       
  2026 
       
  2027 	if (ret != KErrNone)
       
  2028 		{
       
  2029 		User::Leave(KErrNotFound);
       
  2030 		}
       
  2031 	}
       
  2032 
       
  2033 void CSocketDesc::StopConnection()
       
  2034 	{
       
  2035 	iConnection.Close();
       
  2036 	iConnectionPtr = NULL;
       
  2037 	((CFileTable*)iFids)->RemoveRConnectionPtrAt(iRConnectionIndex);
       
  2038 	iRConnectionIndex = -1;
       
  2039 	}
       
  2040 
       
  2041 void CSocketDesc::StopSubConnection()
       
  2042 	{
       
  2043 	iSubConnection.Close();
       
  2044 	iSubConnectionPtr = NULL;
       
  2045 	}
       
  2046 
       
  2047 TInt CSocketDesc::StartSubConnection(void *)
       
  2048 	{
       
  2049 	if (iSubConnectionPtr != NULL)
       
  2050 		{
       
  2051 		return KErrAlreadyExists;
       
  2052 		}
       
  2053 
       
  2054 	TInt ret = KErrArgument;
       
  2055 	TInt rcIndex = 0;
       
  2056 	RConnection *rc;
       
  2057 	TAccessPointRecord record;	
       
  2058 	do
       
  2059 		{
       
  2060 		if (((CFileTable*)iFids)->RConnectionAt(rcIndex, rc) != KErrNone)
       
  2061 			{
       
  2062 			break;
       
  2063 			}
       
  2064 
       
  2065 		// Read index and name of the connection
       
  2066 		GetRConnectionDetails(rc, record);
       
  2067 		if (record.iName == iConnPref.iName)
       
  2068 			{
       
  2069 			ret = KErrNone; // RConnection found
       
  2070 			break;
       
  2071 			}
       
  2072 		rcIndex++;	
       
  2073 		}
       
  2074 	while (ETrue); //Loop over all active connections
       
  2075 
       
  2076 	if (ret == KErrNone)
       
  2077 		{
       
  2078 		ret = iSubConnection.Open(*iSockServPtr, RSubConnection::EAttachToDefault, *rc);
       
  2079 		if (ret == KErrNone)
       
  2080 			{
       
  2081 			iSubConnectionPtr = &iSubConnection;
       
  2082 			}
       
  2083 		}
       
  2084 
       
  2085 	return ret;
       
  2086 	}
       
  2087 
       
  2088 inline void CSocketDesc::SetFids(void *aFids)
       
  2089 	{
       
  2090 	iFids = aFids;
       
  2091 	}
       
  2092 
       
  2093 //Add an entry in the interface routing table	
       
  2094 TInt CSocketDesc::RouteRequest(TInt aReq, void *aParam)
       
  2095 	{
       
  2096 	struct rtentry* rt=reinterpret_cast<struct rtentry*>(aParam);
       
  2097 
       
  2098 	if (!rt)
       
  2099 		{
       
  2100 		return KErrArgument;	
       
  2101 		}
       
  2102 
       
  2103 	TInt ret;
       
  2104 	TPckgBuf<TSoInetRouteInfo>iroute;	
       
  2105 	iroute().iType=ERtNormal;
       
  2106 	iroute().iState=ERtReady; 
       
  2107 	iroute().iMetric=rt->rt_metric;
       
  2108 	if (rt->rt_dev)
       
  2109 		{
       
  2110 		TPckgBuf<TSoInetInterfaceInfo> iface;		
       
  2111 		TFileName name;
       
  2112 		name.Copy(TPtrC8((TText8*)rt->rt_dev));
       
  2113 
       
  2114 		if((ret=GetInterfaceByName(name, iface)) != KErrNone)
       
  2115 			return ret;
       
  2116 
       
  2117 		Copy(iroute().iIfAddr, iface().iAddress);
       
  2118 		ConvertRtEntry(iroute(), rt);
       
  2119 
       
  2120 		//add the entry
       
  2121 		ATOMICSOCKETOP( ret = iSocket.SetOpt(aReq, KSolInetRtCtrl, iroute),return KErrBadHandle )
       
  2122 		return ret;
       
  2123 		}
       
  2124 
       
  2125 	return KErrUnknown;				
       
  2126 	}
       
  2127 
       
  2128 
       
  2129 TInt CSocketDesc::Copy(TInetAddr& aDest, TInetAddr& aSrc)
       
  2130 	{
       
  2131 	if (aSrc.IsV4Mapped() || aSrc.IsV4Compat())
       
  2132 		{
       
  2133 		aSrc.ConvertToV4();
       
  2134 		}
       
  2135 
       
  2136 	aDest.SetAddress(aSrc.Address());
       
  2137 	aDest.SetFamily(aSrc.Family());
       
  2138 	aDest.SetPort(aSrc.Port());
       
  2139 
       
  2140 	return KErrNone;
       
  2141 	}
       
  2142 
       
  2143 TInt CSocketDesc::ConvertSockAddr(TInetAddr& aInetAddr, struct sockaddr_in *aSockAddr)
       
  2144 	{
       
  2145 	if (aSockAddr == NULL)
       
  2146 		{
       
  2147 		return KErrArgument;
       
  2148 		}
       
  2149 
       
  2150 		//convert the address and port from network to host byte order...
       
  2151 		aInetAddr.SetAddress(ByteOrder::Swap32(aSockAddr->sin_addr.s_addr));
       
  2152 		aInetAddr.SetFamily(aSockAddr->sin_family);
       
  2153 		aInetAddr.SetPort(ByteOrder::Swap16(aSockAddr->sin_port));
       
  2154 	return KErrNone;	
       
  2155 	}
       
  2156 
       
  2157 TInt CSocketDesc::ConvertRtEntry(TSoInetRouteInfo& aRouteInfo, struct rtentry *aRouteEntry)
       
  2158 	{
       
  2159 	if(aRouteEntry == NULL)	
       
  2160 		{
       
  2161 		return KErrArgument;
       
  2162 		}
       
  2163 
       
  2164 		ConvertSockAddr(aRouteInfo.iDstAddr, (struct sockaddr_in *)&aRouteEntry->rt_dst);
       
  2165 		ConvertSockAddr(aRouteInfo.iNetMask, (struct sockaddr_in *)&aRouteEntry->rt_genmask);
       
  2166 		ConvertSockAddr(aRouteInfo.iGateway, (struct sockaddr_in *)&aRouteEntry->rt_gateway);			
       
  2167 	return KErrNone;	
       
  2168 	}
       
  2169 
       
  2170 TInt CSocketDesc::GetInterfaceByName(const TDesC& aIfName, TPckgBuf<TSoInetInterfaceInfo>& aIface)
       
  2171 	{
       
  2172 	TInt ret = KErrNone;
       
  2173 	ATOMICSOCKETOP(ret = iSocket.SetOpt(KSoInetEnumInterfaces, KSolInetIfCtrl), ret = KErrBadHandle)
       
  2174 	if (ret != KErrNone)
       
  2175 		{
       
  2176 		return ret;
       
  2177 		}
       
  2178 	TPckgBuf<TSoInetInterfaceInfo> iface;
       
  2179 	ATOMICSOCKETOP(ret = iSocket.GetOpt(KSoInetNextInterface, KSolInetIfCtrl, iface), ret = KErrBadHandle)
       
  2180 	while(ret == KErrNone)
       
  2181 		{
       
  2182 		if (!iface().iAddress.IsUnspecified() && iface().iName.CompareF(aIfName) == 0)
       
  2183 			{
       
  2184 			aIface = iface;
       
  2185 			return ret;    	        
       
  2186 			}
       
  2187 		ATOMICSOCKETOP( ret = iSocket.GetOpt(KSoInetNextInterface, KSolInetIfCtrl, iface), ret = KErrBadHandle )
       
  2188 		}   	
       
  2189 	return KErrUnknown;	
       
  2190 	}
       
  2191 
       
  2192 TInt CSocketDesc::GetInterfaceHWAddress(void *aParam)
       
  2193 	{
       
  2194 	ifreq *ifr = reinterpret_cast<ifreq *>(aParam);
       
  2195 
       
  2196 	if (!ifr)
       
  2197 		{
       
  2198 		return KErrArgument;	
       
  2199 		}
       
  2200 
       
  2201 	if (ifr->ifr_name[0] != '\0')
       
  2202 		{
       
  2203         TPckgBuf<TSoInetInterfaceInfo> iface;
       
  2204         TFileName name;
       
  2205         ifr->ifr_name[IFNAMSIZ-1] = 0; // don't assume NULL terminated input
       
  2206         name.Copy(TPtrC8((TText8*)ifr->ifr_name));
       
  2207     
       
  2208         TInt ret = GetInterfaceByName(name, iface); 		
       
  2209         if (ret != KErrNone)
       
  2210             return ret;
       
  2211     
       
  2212         if (iface().iHwAddr.Length() > sizeof(SSockAddr))		
       
  2213             {
       
  2214             Mem::Copy(&(ifr->ifr_hwaddr.sa_data[0]),&(iface().iHwAddr[sizeof(SSockAddr)]), 6);
       
  2215             ifr->ifr_hwaddr.sa_family = (TUint16)iface().iHwAddr.Family();
       
  2216             ifr->ifr_hwaddr.sa_port = ByteOrder::Swap16(iface().iHwAddr.Port());				
       
  2217             return ret;
       
  2218             }
       
  2219 		}
       
  2220 
       
  2221 	return KErrUnknown;
       
  2222 	}
       
  2223 
       
  2224 //This function retrieves the Currently active IAP ID using the RSocket Query.
       
  2225 //First get the interface index of the active connection using KSoInterfaceIndex
       
  2226 //Then with the help of the interface index, fetch the TSoInetIfQuery, under which iZone
       
  2227 //will contain the snap id. User is supposed to call if_indextoname() api to get the IAP name
       
  2228 //from the ID retured with this Ioctl implementation.
       
  2229 TInt CSocketDesc::GetActiveInterface( void *aParam)
       
  2230     {
       
  2231     TInt ifindex = -1;
       
  2232     TInt ret = KErrNone;
       
  2233     ATOMICSOCKETOP(ret = iSocket.GetOpt(KSoInterfaceIndex, KSolInetIp , ifindex), ret = KErrBadHandle)
       
  2234     if(ret!=KErrNone)
       
  2235         {
       
  2236         return ret;
       
  2237         }   
       
  2238     TPckgBuf<TSoInetIfQuery> opt;
       
  2239     opt().iIndex = ifindex;
       
  2240     ATOMICSOCKETOP(ret = iSocket.GetOpt(KSoInetIfQueryByIndex, KSolInetIfQuery, opt), ret = KErrBadHandle)
       
  2241     if(ret!=KErrNone)
       
  2242         {
       
  2243         return ret;
       
  2244         }
       
  2245     ifreq *ifr = (ifreq * )aParam;
       
  2246     ifr->ifr_index  = opt().iZone[1]; //IAP_ID
       
  2247     return KErrNone;
       
  2248     }