--- a/genericopenlibs/openenvcore/backend/src/corebackend/usocket.cpp Thu Jul 15 20:34:46 2010 +0300
+++ b/genericopenlibs/openenvcore/backend/src/corebackend/usocket.cpp Thu Aug 19 11:33:45 2010 +0300
@@ -76,7 +76,7 @@
// We have to deal with the network byte ordering of AF_INET addresses
//
{
- if (aBuf==0)
+ if (!aBuf)
{
iError = EFAULT;
SetLength(0);
@@ -194,10 +194,10 @@
}
}
+/*
+ * Extract a struct sockaddr from a TSockAddr
+ */
EXPORT_C void TUSockAddr::Get(TAny* addr, unsigned long* len)
-//
-// Extract a struct sockaddr from a TSockAddr
-//
{
if (addr==0)
{
@@ -393,18 +393,15 @@
void CSocketDesc::Write (TDes8& aBuf, TRequestStatus& aStatus)
{
- //Acquire the Lock before write and release it later
- iWriteLock.Wait();
- if (iSocketPtr == NULL)
- {
- TInt ret = OpenUsingPreference();
- if (ret != KErrNone) // Error in open
- {
- Complete(aStatus,ret);
- iWriteLock.Signal();
- return;
- }
- }
+
+ TInt err = maybe_reopen_socket();
+ if (err != KErrNone)
+ {
+ Complete(aStatus, err);
+ return;
+ }
+
+ iWriteLock.Wait();
CSockDescBase::Write(aBuf, aStatus);
iWriteLock.Signal();
}
@@ -427,52 +424,43 @@
{
return addr.iError;
}
-
- if (iSocketPtr == NULL)
- {
- ret = OpenUsingPreference();
- if (ret != KErrNone) // Error in open
- {
- return ret;
- }
- }
- return iSocket.Bind(addr);
+
+ ret = maybe_reopen_socket();
+ if (ret != KErrNone)
+ return ret;
+ ATOMICSOCKETOP(ret = iSocket.Bind(addr),return KErrBadHandle)
+ return ret;
}
TInt CSocketDesc::Listen(TUint qSize)
{
+
+ TInt ret;
if (iStyle == SOCK_DGRAM) // Listen on UDP socket, crashing at RSocket::Listen().
{
return EOPNOTSUPP;
}
- if (iSocketPtr == NULL)
- {
- TInt ret = OpenUsingPreference();
- if (ret != KErrNone) // Error in open
- {
- return ret;
- }
- }
+
+ ret = maybe_reopen_socket();
+ if (ret != KErrNone)
+ return ret;
+
return CSockDescBase::Listen(qSize);
}
TInt CSocketDesc::SockName(int anEnd, struct sockaddr* anAddr,unsigned long* aSize)
{
- TInt ret;
- if (iSocketPtr == NULL)
+
+
+ if (!anAddr)
{
- ret = OpenUsingPreference();
- if (ret != KErrNone) // Error in open
- {
- return ret;
- }
- }
-
- if ( anAddr == 0 ) // if the sockaddr passed is NULL return EFAULT.
- {
- return EFAULT;
+ return EFAULT;
}
+ TInt ret = maybe_reopen_socket();
+ if (ret != KErrNone)
+ return ret;
+
struct sockaddr temp;
unsigned long len = sizeof( temp );
@@ -503,24 +491,19 @@
{
return EINVAL;
}
- if (iSocketPtr == NULL)
- {
- ret = OpenUsingPreference();
- if (ret != KErrNone) // Error in open
- {
- return ret;
- }
- }
+
+ ret = maybe_reopen_socket();
+ if (ret != KErrNone)
+ return ret;
if (SO_TYPE == anOptionName && SOL_SOCKET == anOptionLevel)
{
TProtocolDesc protocolInfo;
- ret = iSocket.Info(protocolInfo);
+ ATOMICSOCKETOP(ret = iSocket.Info(protocolInfo), ret = KErrBadHandle)
if (KErrNone == ret )
{
- //Copy the Socket Type to the buffer
- TInt size;
- size = (anOption.Length() < sizeof(protocolInfo.iSockType))? anOption.Length(): sizeof(protocolInfo.iSockType);
+ // Copy the Socket Type to the buffer
+ TInt size = (anOption.Length() < sizeof(protocolInfo.iSockType))? anOption.Length(): sizeof(protocolInfo.iSockType);
Mem::Copy((unsigned char*)anOption.Ptr(), &protocolInfo.iSockType, size);
anOption.SetLength(size);
}
@@ -532,20 +515,16 @@
TUSockAddr addr;
struct sockaddr_in sockAddress;
sockAddress.sin_family = AF_INET;
- sockAddress.sin_port = iSocket.LocalPort();
- iSocket.LocalName(addr);
-
+ ATOMICSOCKETOP(sockAddress.sin_port = iSocket.LocalPort(),return KErrBadHandle)
+ ATOMICSOCKETOP(iSocket.LocalName(addr);,return KErrBadHandle)
TInt a = sizeof(sockAddress);
addr.Get(&sockAddress,(unsigned long*)&a);
- TInt size;
- size = (anOption.Length() < sizeof(sockAddress.sin_addr))? anOption.Length(): sizeof(sockAddress.sin_addr);
+ TInt size = (anOption.Length() < sizeof(sockAddress.sin_addr))? anOption.Length(): sizeof(sockAddress.sin_addr);
Mem::Copy((unsigned char*)anOption.Ptr(), &(sockAddress.sin_addr), size);
anOption.SetLength(size);
return KErrNone;
}
-
-
switch(anOptionLevel)
{
case IPPROTO_TCP:
@@ -583,8 +562,8 @@
anOptionName=KSoIp6MulticastLoop;
break;
}
-
- return iSocket.GetOpt(anOptionName,anOptionLevel,anOption);
+ ATOMICSOCKETOP(ret = iSocket.GetOpt(anOptionName,anOptionLevel,anOption), return KErrBadHandle)
+ return ret;
}
TInt CSocketDesc::GetInterfaceIndex(TUint32 anAddr)
@@ -598,12 +577,14 @@
}
else
{
- TInt ret = iSocket.SetOpt(KSoInetEnumInterfaces, KSolInetIfCtrl);
+ TInt ret = KErrNone;
+ ATOMICSOCKETOP(ret = iSocket.SetOpt(KSoInetEnumInterfaces, KSolInetIfCtrl), ret = KErrBadHandle)
if (ret != KErrNone)
return KErrGeneral;
TPckgBuf<TSoInetInterfaceInfo>iface;
- while(iSocket.GetOpt(KSoInetNextInterface, KSolInetIfCtrl, iface) == KErrNone)
+ ATOMICSOCKETOP( ret = iSocket.GetOpt(KSoInetNextInterface, KSolInetIfCtrl, iface), ret = KErrBadHandle )
+ while(ret == KErrNone)
{
TSoInetInterfaceInfo &info = iface();
TInt result;
@@ -612,13 +593,13 @@
if (anAddr == info.iAddress.Address())
{
ifq().iName = info.iName;
- result = iSocket.GetOpt(KSoInetIfQueryByName, KSolInetIfQuery, ifq);
+ ATOMICSOCKETOP( result = iSocket.GetOpt(KSoInetIfQueryByName, KSolInetIfQuery, ifq), result = KErrBadHandle )
if (result == KErrNone)
aIndex = ifq().iIndex;
}
}
-
+ ATOMICSOCKETOP( ret = iSocket.GetOpt(KSoInetNextInterface, KSolInetIfCtrl, iface), ret = KErrBadHandle )
}
}
return aIndex;
@@ -635,14 +616,10 @@
{
return EINVAL;
}
- if (iSocketPtr == NULL)
- {
- ret = OpenUsingPreference();
- if (ret != KErrNone) // Error in open
- {
- return ret;
- }
- }
+
+ ret = maybe_reopen_socket();
+ if (ret != KErrNone)
+ return ret;
switch(anOptionLevel)
{
@@ -694,30 +671,26 @@
maddr=(from[0]<<24)+(from[1]<<16)+(from[2]<<8)+from[3];
multiAddr.SetAddress( maddr);
multiAddr.ConvertToV4Mapped();
- if(multiAddr.IsMulticast()){
- req().iAddr = multiAddr.Ip6Address();
- req().iInterface = aIndex;
- }
- return iSocket.SetOpt(anOptionName, anOptionLevel, req);
+ if (multiAddr.IsMulticast())
+ {
+ req().iAddr = multiAddr.Ip6Address();
+ req().iInterface = aIndex;
+ }
+ ATOMICSOCKETOP( ret = iSocket.SetOpt(anOptionName, anOptionLevel, req), return KErrBadHandle )
+ return ret;
case IP_MULTICAST_TTL:
anOptionLevel=KSolInetIp;
anOptionName=KSoIp6MulticastHops;
Option = (TInt*)anOption.Ptr();
ttlValue = *Option;
- return iSocket.SetOpt(anOptionName,anOptionLevel,ttlValue);
-
+ ATOMICSOCKETOP( ret = iSocket.SetOpt(anOptionName,anOptionLevel,ttlValue), return KErrBadHandle )
+ return ret;
+
case SO_BROADCAST:
//check if user is trying to disable broadcast
Option = (TInt*)anOption.Ptr();
- if (*Option == 0)
- {
- return KErrNotSupported;
- }
- else
- {
- return KErrNone;
- }
+ return (*Option == 0 ? KErrNotSupported : KErrNone);
case IP_MULTICAST_IF:
{
@@ -726,10 +699,11 @@
struct in_addr *inAddress = (struct in_addr*)anOption.Ptr();
struct sockaddr_in sockAddress;
sockAddress.sin_family = AF_INET;
- sockAddress.sin_port = iSocket.LocalPort();
+ ATOMICSOCKETOP(sockAddress.sin_port = iSocket.LocalPort();,return KErrBadHandle)
sockAddress.sin_addr.s_addr = inAddress->s_addr;
- TUSockAddr ifAddress(&sockAddress, sizeof(sockAddress));
- return iSocket.Bind(ifAddress);
+ TUSockAddr ifAddress(&sockAddress, sizeof(sockAddress));
+ ATOMICSOCKETOP( ret = iSocket.Bind(ifAddress), return KErrBadHandle )
+ return ret;
}
case IP_MULTICAST_LOOP:
@@ -737,11 +711,11 @@
anOptionName=KSoIp6MulticastLoop;
break;
- default:
- break;
+
}
- return iSocket.SetOpt(anOptionName,anOptionLevel,anOption);
+ ATOMICSOCKETOP( ret = iSocket.SetOpt(anOptionName,anOptionLevel,anOption), return KErrBadHandle )
+ return ret;
}
void CSocketDesc::Sync (TRequestStatus& aStatus)
@@ -752,19 +726,14 @@
void CSocketDesc::RecvFrom(TDes8& aDesc, TSockAddr& from, int flags, TRequestStatus& aStatus)
{
+ TInt err = maybe_reopen_socket();
+ if (err != KErrNone)
+ {
+ Complete(aStatus, err);
+ return;
+ }
+
iReadLock.Wait();
- // RSocket::Open() is postponed from socket()
- if (iSocketPtr == NULL)
- {
- TInt ret = OpenUsingPreference();
- if (ret != KErrNone)
- {
- Complete(aStatus,ret); // Error in open
- iReadLock.Signal();
- return;
- }
- }
-
CSockDescBase::RecvFrom(aDesc, from, flags, aStatus);
iReadLock.Signal();
}
@@ -780,25 +749,23 @@
void CSocketDesc::SendTo(TDes8& aDesc, const struct sockaddr* anAddr, unsigned long aAddrLen, int flags, TRequestStatus& aStatus)
{
- iWriteLock.Wait();
-
- if (iSocketPtr == NULL)
- {
- TInt ret = OpenUsingPreference();
- if (ret != KErrNone)
- {
- Complete(aStatus,ret); // Error in open
- iWriteLock.Signal();
- return;
- }
- }
- TUSockAddr toAddr(anAddr,aAddrLen);
+ TInt err = maybe_reopen_socket();
+ if (err != KErrNone)
+ {
+ Complete(aStatus, err);
+ return;
+ }
+
+ TUSockAddr toAddr(anAddr, aAddrLen);
+
+ iWriteLock.Wait();
CSockDescBase::SendTo(aDesc, toAddr, flags, aStatus);
iWriteLock.Signal();
}
void CSocketDesc::SendToCancel()
{
+// Should we use atomic loads here?
if (iSocketPtr != NULL)
{
CSockDescBase::SendToCancel();
@@ -807,7 +774,8 @@
void CSocketDesc::Shutdown(TUint aHow,TRequestStatus& aStatus)
{
- if (iSocketPtr == NULL) // Not opened at all. Nothing to do.
+
+ if (__e32_atomic_load_acq32(&iSocketPtr) == NULL) // Not opened at all. Nothing to do.
{
Complete(aStatus,KErrNone);
return;
@@ -819,52 +787,61 @@
void CSocketDesc::Accept(CFileDescBase*& aNewSocket, TRequestStatus& aStatus, RSocketServ& aSs, TSockAddr * /*aAddr*/)
{
- //Acquire the Lock before accept and release it later
+ aNewSocket = NULL;
+ TInt err = maybe_reopen_socket();
+ if (err != KErrNone)
+ {
+ Complete(aStatus, err);
+ return;
+ }
+
iReadLock.Wait();
-
- TInt err = KErrNone;
- if (iSocketPtr == NULL)
- {
- err = OpenUsingPreference();
- if (err != KErrNone) // Error in open
- {
- Complete(aStatus,err);
- iReadLock.Signal();
- return;
- }
- }
-
+ // what are the below coverity thingummichs?
//coverity[alloc_fn]
//coverity[assign]
CSocketDesc *newSocket = new CSocketDesc;
- if (newSocket!=0)
+ if (!newSocket)
+ {
+ Complete(aStatus, KErrNoMemory);
+ iReadLock.Signal();
+ return;
+ }
+
+ err = newSocket->CreateLock();
+ if (err)
{
- err = newSocket->CreateLock();
- if (err)
- {
- Complete(aStatus, KErrNoMemory);
- delete newSocket;
- aNewSocket = NULL;
- iReadLock.Signal();
- //coverity[memory_leak]
- return;
- }
+ Complete(aStatus, KErrNoMemory);
+ delete newSocket;
+ iReadLock.Signal();
+ //coverity[memory_leak]
+ return;
+ }
- err=newSocket->iSocket.Open(aSs);
- }
- if (newSocket ==0 || err!=KErrNone)
+ err = newSocket->iSocket.Open(aSs);
+ if (err)
{
- Complete(aStatus,KErrNoMemory);
+ Complete(aStatus, err);
+ newSocket->FinalClose(); // this will Close locks
delete newSocket;
- aNewSocket = NULL;
+
iReadLock.Signal();
//coverity[memory_leak]
return;
}
newSocket->iSocketPtr = &newSocket->iSocket;
newSocket->iStyle = iStyle;
- iSocket.Accept(newSocket->iSocket,aStatus);
- aNewSocket = newSocket;
+ err = KErrNone;
+ ATOMICSOCKETOP( iSocket.Accept(newSocket->iSocket,aStatus), err = KErrBadHandle )
+ if( err )
+ {
+ Complete(aStatus, err);
+ newSocket->FinalClose(); // this will Close locks
+ delete newSocket;
+ }
+ else
+ {
+ aNewSocket = newSocket;
+ }
iReadLock.Signal();
}
@@ -872,7 +849,7 @@
{
if (iSocketPtr != NULL)
{
- iSocket.CancelAccept();
+ ATOMICSOCKETOP( iSocket.CancelAccept(), NOP )
}
}
@@ -885,56 +862,63 @@
aStatus = addr.iError;
return;
}
- if (iSocketPtr == NULL)
- {
- // RSocket::Open() is postponed from socket()
- TInt ret = OpenUsingPreference();
- if (ret != KErrNone) // error in open
- {
- aStatus = ret;
- return;
- }
- }
-
- iSocket.Connect(addr,aStatus);
- User::WaitForRequest(aStatus);
+
+ TInt err = maybe_reopen_socket();
+ if (err != KErrNone)
+ {
+ aStatus = err;
+ return;
+ }
+
+ iWriteLock.Wait();
+ if( GetConnectionProgress() == EFalse )
+ {
+ ATOMICSOCKETOP(iSocket.Connect(addr, aStatus), Complete(aStatus,KErrBadHandle))
+ User::WaitForRequest(aStatus);
+ if( aStatus.Int() == KErrWouldBlock )
+ SetConnectionProgress(ETrue);
+ }
+ else
+ {
+ aStatus = EALREADY;
+ }
+ iWriteLock.Signal();
}
void CSocketDesc::ConnectCancel()
{
if (iSocketPtr != NULL)
{
- iSocket.CancelConnect();
+ ATOMICSOCKETOP(iSocket.CancelConnect(),NOP)
}
}
void CSocketDesc::Ioctl(int aCmd, void* aParam, TRequestStatus& aStatus)
{
- TInt ret=KErrNone;
+ TInt ret = KErrNone;
int* param = reinterpret_cast<int*>(aParam);
- if (iSocketPtr == NULL)
- {
- ret = OpenUsingPreference();
- if (ret != KErrNone) // Error in open
- {
- Complete(aStatus,ret);
- return;
- }
- }
+ ret = maybe_reopen_socket();
+ if (ret != KErrNone)
+ {
+ Complete(aStatus, ret);
+ return;
+ }
switch ((unsigned)aCmd)
{
case FIONREAD:
case E32IONREAD:
- ret=iSocket.GetOpt(KSOReadBytesPending,KSOLSocket,*param);
+ {
+ ATOMICSOCKETOP( ret=iSocket.GetOpt(KSOReadBytesPending,KSOLSocket,*param), ret = KErrBadHandle )
+ }
break;
case E32IOSELECT:
{
iIoctlBuf.Set((TText8*)aParam,4,4);
iIoctlLock.Wait();
iIoctlFlag = ETrue;
- iSocket.Ioctl(KIOctlSelect,aStatus,&iIoctlBuf,KSOLSocket);
+ ATOMICSOCKETOP(iSocket.Ioctl(KIOctlSelect,aStatus,&iIoctlBuf,KSOLSocket), Complete(aStatus,KErrBadHandle))
}
return;
case SIOCGIFCONF:
@@ -969,7 +953,7 @@
ret = StopInterface(aParam);
break;
case SIOCATMARK:
- ret=iSocket.GetOpt(KSoTcpRcvAtMark,KSolInetTcp,*param);
+ ATOMICSOCKETOP(ret=iSocket.GetOpt(KSoTcpRcvAtMark,KSolInetTcp,*param), ret = KErrBadHandle)
break;
case SIOCGIFADDR:
ret = GetIpAddress(aParam);
@@ -1011,7 +995,7 @@
{
if (iSocketPtr && iIoctlFlag)
{
- iSocket.CancelIoctl();
+ ATOMICSOCKETOP(iSocket.CancelIoctl(), NOP)
iIoctlLock.Signal();
iIoctlFlag = EFalse;
}
@@ -1026,27 +1010,25 @@
//
TInt CSocketDesc::Fcntl(TUint anArg, TUint aCmd)
{
- if (iSocketPtr == NULL)
- {
- TInt ret = OpenUsingPreference();
- if (ret != KErrNone) // Error in open
- {
- return ret;
- }
- }
- return CSockDescBase::Fcntl(anArg, aCmd);
+
+ TInt err = maybe_reopen_socket();
+ if (err != KErrNone)
+ return err;
+
+ return CSockDescBase::Fcntl(anArg, aCmd);
}
TInt CSocketDesc :: GetIpAddress( void *aParam )
{
TInetAddr myAddr;
- iSocket.LocalName(myAddr);
+ ATOMICSOCKETOP( iSocket.LocalName(myAddr), return KErrBadHandle )
TUint32 myIP = myAddr.Address();
- ifreq *ifr = (ifreq *)aParam;
- if(myIP == 0)
+ if (myIP == 0)
{
return KErrGeneral;
}
+
+ ifreq *ifr = (ifreq *)aParam;
((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr.s_addr = myIP;
return KErrNone;
}
@@ -1054,13 +1036,14 @@
TInt CSocketDesc :: GetRemoteIpAddress( void *aParam )
{
TInetAddr remoteAddr;
- iSocket.RemoteName(remoteAddr);
+ ATOMICSOCKETOP( iSocket.RemoteName(remoteAddr), return KErrBadHandle )
TUint32 remoteIP = remoteAddr.Address();
- ifreq *ifr = (ifreq *)aParam;
- if(remoteIP == 0)
+
+ if (remoteIP == 0)
{
return KErrGeneral;
}
+ ifreq *ifr = (ifreq *)aParam;
((struct sockaddr_in *)&ifr->ifr_dstaddr)->sin_addr.s_addr = remoteIP;
return KErrNone;
}
@@ -1208,16 +1191,16 @@
TPckgBuf<TSoInetIfQuery> ifq;
TBuf8 <25> ipBuf8;
TName aBuf;
-
- TInt ret = iSocket.SetOpt(KSoInetEnumInterfaces, KSolInetIfCtrl);
+ TInt ret = KErrNone;
+ ATOMICSOCKETOP( ret = iSocket.SetOpt(KSoInetEnumInterfaces, KSolInetIfCtrl), ret = KErrBadHandle )
if (ret != KErrNone)
{
return KErrGeneral;
}
TPckgBuf<TSoInet6InterfaceInfo> info;
TSoInet6InterfaceInfo &in = info();
-
- while(iSocket.GetOpt(KSoInetNextInterface, KSolInetIfCtrl, info) == KErrNone)
+ ATOMICSOCKETOP( ret = iSocket.GetOpt(KSoInetNextInterface, KSolInetIfCtrl, info), ret = KErrBadHandle )
+ while(ret == KErrNone)
{
if(info().iName != _L("") && info().iName != _L("loop6") && info().iName != _L("loop4"))
{
@@ -1324,28 +1307,33 @@
}
}
- }
+ }
+ ATOMICSOCKETOP( ret = iSocket.GetOpt(KSoInetNextInterface, KSolInetIfCtrl, info), ret = KErrBadHandle )
}
setout:
TPckgBuf<TSoInet6InterfaceInfo> changeToNew(info());
- return iSocket.SetOpt(KSoInetConfigInterface, KSolInetIfCtrl,changeToNew);
+ ATOMICSOCKETOP(ret = iSocket.SetOpt(KSoInetConfigInterface, KSolInetIfCtrl,changeToNew), return KErrBadHandle )
+ return ret;
}
#endif // __SYMBIAN_COMPILE_UNUSED__
TInt CSocketDesc::GetInterfaceDetails( void *aParam ,TInt aFlag, TInt aType )
{
- ifreq *ifr = (ifreq *)aParam;
TPckgBuf<TSoInetIfQuery> ifq;
- TInt ret = iSocket.SetOpt(KSoInetEnumInterfaces, KSolInetIfCtrl);
+ TInt ret = KErrNone;
+ ATOMICSOCKETOP( ret = iSocket.SetOpt(KSoInetEnumInterfaces, KSolInetIfCtrl), ret = KErrBadHandle )
if (ret != KErrNone)
{
return KErrGeneral;
}
+
+ ifreq *ifr = (ifreq *)aParam;
*(ifr->ifr_addr.sa_data) = '\0';
TPckgBuf<TSoInetInterfaceInfo> info;
- while(iSocket.GetOpt(KSoInetNextInterface, KSolInetIfCtrl, info) == KErrNone)
+ ATOMICSOCKETOP( ret = iSocket.GetOpt(KSoInetNextInterface, KSolInetIfCtrl, info), ret = KErrBadHandle )
+ while( ret == KErrNone)
{
if(info().iName != _L("") && info().iName != _L("loop6") && info().iName != _L("loop4"))
@@ -1460,12 +1448,12 @@
if(info().iFeatures&KIfCanSetHardwareAddr) */
break;
- default:
- break;
+
}
}
- }
+ }
+ ATOMICSOCKETOP( ret = iSocket.GetOpt(KSoInetNextInterface, KSolInetIfCtrl, info), ret = KErrBadHandle )
}
return KErrNone;
@@ -1473,19 +1461,14 @@
TInt CSocketDesc::Poll(TPollMode aMode,TBool& aReadyStatus,TInt& aErrno)
{
- TInt ret;
-
- if (iSocketPtr == NULL)
- {
- ret = OpenUsingPreference();
- if (ret != KErrNone) // Error in open
- {
- return ret;
- }
- }
+ TInt ret = maybe_reopen_socket();
+ if (ret != KErrNone)
+ {
+ return ret;
+ }
TInt status;
- ret = iSocket.GetOpt(KSOSelectPoll, KSOLSocket, status);
+ ATOMICSOCKETOP(ret = iSocket.GetOpt(KSOSelectPoll, KSOLSocket, status), ret = KErrBadHandle)
if (ret == KErrNone)
{
aReadyStatus = status & aMode;
@@ -1497,16 +1480,11 @@
/* The synchronous - non-blocking Poll */
TInt CSocketDesc::Poll(TUint aEvents)
{
- TInt err = 0;
- if (iSocketPtr == NULL)
- {
- err = OpenUsingPreference();
- if (err != KErrNone) // Error in open
- {
- return err;
- }
- }
+ TInt err = maybe_reopen_socket();
+ if (err != KErrNone)
+ return err;
+
return CSockDescBase::Poll(aEvents);
}
@@ -1567,6 +1545,8 @@
TInt ifCount = ifc.ifc_len / sizeof(ifreq);
TInt i = 0;
+ // We wouldn't need this if we were using a StrNcmp below
+ ifrQuery->ifr_name[IFNAMSIZ-1] = 0; // don't assume NULL terminated input
// Search for the interface name
for (; i < ifCount; i++, ifr++)
{
@@ -1612,11 +1592,10 @@
ret = KErrArgument;
}
- if (ret != KErrNone)
- {
- return ret;
- }
- ifc->ifc_len = sizeof(ifreq) * count;
+ if (ret == KErrNone)
+ {
+ ifc->ifc_len = sizeof(ifreq) * count;
+ }
return ret;
}
@@ -1656,7 +1635,7 @@
for (apIndex = 0; apIndex < length; apIndex++,ifr++)
{
TAccessPointRecord& ref = (*apArray)[apIndex];
- // Catch the character pointer
+ ifr->ifr_name[IFNAMSIZ-1] = 0; // don't assume NULL terminated input
TPtr8 ptr((TText8*)ifr->ifr_name, IFNAMSIZ);
ret = CnvUtfConverter::ConvertFromUnicodeToUtf8(ptr, ref.iName);
@@ -1705,12 +1684,12 @@
TInt CSocketDesc::SetInterfaceByName(void *aParam)
{
ifreq *ifr = (ifreq *)aParam;
- if (ifr == NULL)
+ if (!ifr)
{
return KErrArgument;
}
- // Copy the null terminated interface name
+ ifr->ifr_name[IFNAMSIZ-1] = 0; // don't assume NULL terminated input
TPtrC8 ptr((TText8*)ifr->ifr_name);
TInt ret = CnvUtfConverter::ConvertToUnicodeFromUtf8(iConnPref.iName,ptr);
if(ret > KErrNone )
@@ -1773,20 +1752,34 @@
}
else // No connection preference is set
{
- ret = iSocket.Open(*iSockServPtr,addrfamily,iStyle,iProtocol);
- }
- }
-
- if(KErrNone == ret)
- {
- iSocketPtr = &iSocket;
- }
- iConnectInProgress = EFalse;
- return ret;
- }
+ ret = ECONNABORTED;
+ }
+ }
+
+ if (ret == KErrNone)
+ {
+ __e32_atomic_store_rel32(&iSocketPtr, (unsigned long)&iSocket);
+ }
+
+ iConnectInProgress = EFalse;
+
+ return ret;
+ }
void CSocketDesc::TempClose()
{
+ TUint32 ret = __e32_atomic_ior_ord32((void *)&iCount,0x8000);
+ if( ret >= 0x8000 )
+ {
+ // This indicates a TempClose has already been done from one of the threads
+ return;
+ }
+ // loop and yeild till no more references are held to the iSocket
+ while( iCount != 0x8000 )
+ {
+ // Yeild for 1 ms
+ User::After(1000);
+ }
if (iSocket.SubSessionHandle() != 0)
{
iSocketPtr = NULL;
@@ -2125,7 +2118,8 @@
ConvertRtEntry(iroute(), rt);
//add the entry
- return iSocket.SetOpt(aReq, KSolInetRtCtrl, iroute);
+ ATOMICSOCKETOP( ret = iSocket.SetOpt(aReq, KSolInetRtCtrl, iroute),return KErrBadHandle )
+ return ret;
}
return KErrUnknown;
@@ -2175,22 +2169,23 @@
TInt CSocketDesc::GetInterfaceByName(const TDesC& aIfName, TPckgBuf<TSoInetInterfaceInfo>& aIface)
{
- TInt ret = iSocket.SetOpt(KSoInetEnumInterfaces, KSolInetIfCtrl);
+ TInt ret = KErrNone;
+ ATOMICSOCKETOP(ret = iSocket.SetOpt(KSoInetEnumInterfaces, KSolInetIfCtrl), ret = KErrBadHandle)
if (ret != KErrNone)
{
return ret;
}
TPckgBuf<TSoInetInterfaceInfo> iface;
- while((ret = iSocket.GetOpt(KSoInetNextInterface, KSolInetIfCtrl, iface)) == KErrNone)
+ ATOMICSOCKETOP(ret = iSocket.GetOpt(KSoInetNextInterface, KSolInetIfCtrl, iface), ret = KErrBadHandle)
+ while(ret == KErrNone)
{
- if(!iface().iAddress.IsUnspecified()
- && iface().iName.CompareF(aIfName) == 0)
+ if (!iface().iAddress.IsUnspecified() && iface().iName.CompareF(aIfName) == 0)
{
aIface = iface;
return ret;
}
+ ATOMICSOCKETOP( ret = iSocket.GetOpt(KSoInetNextInterface, KSolInetIfCtrl, iface), ret = KErrBadHandle )
}
-
return KErrUnknown;
}
@@ -2203,27 +2198,25 @@
return KErrArgument;
}
- if (ifr->ifr_name[0]!='\0')
+ if (ifr->ifr_name[0] != '\0')
{
- TPckgBuf<TSoInetInterfaceInfo> iface;
- TFileName name;
- name.Copy(TPtrC8((TText8*)ifr->ifr_name));
-
- TInt ret = GetInterfaceByName(name, iface);
-
- if (ret != KErrNone)
- {
- return ret;
- }
-
- if(iface().iHwAddr.Length() > sizeof(SSockAddr))
- {
- Mem::Copy(&(ifr->ifr_hwaddr.sa_data[0]),&(iface().iHwAddr[sizeof(SSockAddr)]), 6);
- ifr->ifr_hwaddr.sa_family = (TUint16)iface().iHwAddr.Family();
- ifr->ifr_hwaddr.sa_port = ByteOrder::Swap16(iface().iHwAddr.Port());
- return ret;
+ TPckgBuf<TSoInetInterfaceInfo> iface;
+ TFileName name;
+ ifr->ifr_name[IFNAMSIZ-1] = 0; // don't assume NULL terminated input
+ name.Copy(TPtrC8((TText8*)ifr->ifr_name));
+
+ TInt ret = GetInterfaceByName(name, iface);
+ if (ret != KErrNone)
+ return ret;
+
+ if (iface().iHwAddr.Length() > sizeof(SSockAddr))
+ {
+ Mem::Copy(&(ifr->ifr_hwaddr.sa_data[0]),&(iface().iHwAddr[sizeof(SSockAddr)]), 6);
+ ifr->ifr_hwaddr.sa_family = (TUint16)iface().iHwAddr.Family();
+ ifr->ifr_hwaddr.sa_port = ByteOrder::Swap16(iface().iHwAddr.Port());
+ return ret;
+ }
}
- }
return KErrUnknown;
}
@@ -2236,14 +2229,15 @@
TInt CSocketDesc::GetActiveInterface( void *aParam)
{
TInt ifindex = -1;
- TInt ret = iSocket.GetOpt(KSoInterfaceIndex, KSolInetIp , ifindex);
+ TInt ret = KErrNone;
+ ATOMICSOCKETOP(ret = iSocket.GetOpt(KSoInterfaceIndex, KSolInetIp , ifindex), ret = KErrBadHandle)
if(ret!=KErrNone)
{
return ret;
}
TPckgBuf<TSoInetIfQuery> opt;
opt().iIndex = ifindex;
- ret = iSocket.GetOpt(KSoInetIfQueryByIndex, KSolInetIfQuery, opt);
+ ATOMICSOCKETOP(ret = iSocket.GetOpt(KSoInetIfQueryByIndex, KSolInetIfQuery, opt), ret = KErrBadHandle)
if(ret!=KErrNone)
{
return ret;