/** Create a UDP Socket and bind it to an arbitrary address @param aSocket The socket @param aAddress On return, contains the local socket name @return ETrue on success, EFalse otherwise */ TBool CEsockTest9_6::CreateUdpSocket( RSocket& aSocket, TSockAddr& aAddress ) { // Open the socket TInt nRet = aSocket.Open( iEsockSuite->iSocketServer, KAfInet, KSockDatagram, KProtocolInetUdp ); if( KErrNone != nRet ) { Logger().WriteFormat(_L("Failed to open socket: return value = <%d>"), nRet ); return EFalse; } // Bind to any address. //TInetAddr myAddress( KInetAddrAny, KInetPortAny ); TInetAddr myAddress( KInetAddrLoop, KInetPortAny ); nRet = aSocket.Bind( myAddress ); if( KErrNone != nRet ) { Logger().WriteFormat(_L("Failed to bind socket: return value = <%d>"), nRet ); return EFalse; } // Retrieve the bound address aSocket.LocalName( aAddress ); // Happy shiny return ETrue; }
/** * Get the local IP address of the device * * @note Requires at least one IP packet sent in advance! */ int net_if_getaddr4(const char *ifname, int af, struct sa *ip) { (void)ifname; if (AF_INET != af) return EAFNOSUPPORT; /* Already cached? */ if (sa_isset(&local_ip, SA_ADDR)) { sa_cpy(ip, &local_ip); return 0; } RSocketServ ss; RSocket s; TInt ret; ret = ss.Connect(); if (KErrNone != ret) { DEBUG_WARNING("connecting to socket server fail (ret=%d)\n", ret); return ECONNREFUSED; } ret = s.Open(ss, KAfInet, KSockDatagram, KProtocolInetUdp); if (KErrNone != ret) { DEBUG_WARNING("open socket failed (ret=%d)\n", ret); return ECONNREFUSED; } TInetAddr bind; bind.SetPort(0); bind.SetAddress(KInetAddrAny); ret = s.Bind(bind); if (KErrNone != ret) { DEBUG_WARNING("bind socket failed (ret=%d)\n", ret); return ECONNREFUSED; } TInetAddr local; s.LocalName(local); s.Close(); ss.Close(); sa_set_in(&local_ip, local.Address(), local.Port()); DEBUG_NOTICE("local IP addr: %j\n", &local_ip); if (!sa_isset(&local_ip, SA_ADDR)) return EINVAL; sa_cpy(ip, &local_ip); return 0; }
void CJavaDebugAgentDriver::ClientDisconnected(CTcpServerHandler* aHandler) { TInt handlerPos = iHandlers.Find(aHandler); if (handlerPos >= 0) { TInetAddr remote; RSocket* socket = aHandler->Socket(); socket->LocalName(remote); // Delete a matching KeepAlive object. It doesn't matter which one. // Ideally we shouldn't create more than one KeepAlive per address // but we normally have no more than one client, so it's doesn't // really matter. for (TInt i = 0; i<iKeepAliveList.Count(); i++) { if (remote.Match(iKeepAliveList[i]->Address())) { delete iKeepAliveList[i]; iKeepAliveList.Remove(i); break; } } // It's not obvious how we can recover from the situation when // we can't this handler to iDeadHandlers array. It's not safe // to delete the handler here because this callback is invoked // by the handler, and it may access its this pointer after this // callback has returned. It that case, let's leave it in iHandlers // array. It screws things up but at least we don't leak memory. // Hopefully, this won't happen too often... if (iDeadHandlers.Append(aHandler) == KErrNone) { iHandlers.Remove(handlerPos); if (iHandlers.Count() == 0) { // It was our last client SetState(iServer ? EListening : EDisconnected); } // Schedule callback on a clean stack where we can safely // delete the handler iAsyncCleanup->CallBack(); } } //Dima: Cleanup on start in InitL conflicts with installation //seemingly due to errors in Installer (ECOM framework panics //on loading midp2installerplugin.dll on installation after cleanup) //so we clean up on client disconnect. if (IsAutoCleanEnabled()) { Log(_L("Cleaning up old data...")); iEngine->Cleanup(); Log(_L("Done cleaning...")); } }
// // Handle asynchronous Accept() completion. // CPjSocket *CIoqueueCallback::HandleAcceptCompletion() { CPjSocket *pjNewSock = new CPjSocket (get_pj_socket()->GetAf(), get_pj_socket()->GetSockType(), blank_sock_); int addrlen = 0; if (pending_data_.accept_.new_sock_) { *pending_data_.accept_.new_sock_ = (pj_sock_t) pjNewSock; pending_data_.accept_.new_sock_ = NULL; } if (pending_data_.accept_.local_) { TInetAddr aAddr; pj_sockaddr *ptr_sockaddr; blank_sock_.LocalName (aAddr); ptr_sockaddr = (pj_sockaddr*) pending_data_.accept_.local_; addrlen = *pending_data_.accept_.addrlen_; PjSymbianOS::Addr2pj (aAddr, *ptr_sockaddr, &addrlen); pending_data_.accept_.local_ = NULL; } if (pending_data_.accept_.remote_) { TInetAddr aAddr; pj_sockaddr *ptr_sockaddr; blank_sock_.RemoteName (aAddr); ptr_sockaddr = (pj_sockaddr*) pending_data_.accept_.remote_; addrlen = *pending_data_.accept_.addrlen_; PjSymbianOS::Addr2pj (aAddr, *ptr_sockaddr, &addrlen); pending_data_.accept_.remote_ = NULL; } if (pending_data_.accept_.addrlen_) { if (addrlen == 0) { if (pjNewSock->GetAf() == PJ_AF_INET) addrlen = sizeof (pj_sockaddr_in); else if (pjNewSock->GetAf() == PJ_AF_INET6) addrlen = sizeof (pj_sockaddr_in6); else { pj_assert (!"Unsupported address family"); } } *pending_data_.accept_.addrlen_ = addrlen; pending_data_.accept_.addrlen_ = NULL; } return pjNewSock; }
int ILibSocketWrapper_getsockname(int socketObject, struct sockaddr* local, int* length) { struct sockaddr_in* localAddr = (struct sockaddr_in*)local; RSocket *s = (RSocket*)SocketArray[socketObject]; TInetAddr sockAddr; // get the local name s->LocalName(sockAddr); // convert from Symbian localAddr->sin_family = sockAddr.Family(); localAddr->sin_port = sockAddr.Port(); localAddr->sin_addr.s_addr = ntohl(sockAddr.Address()); return 0; }
SYSCALL(int, maConnGetAddr(MAHandle conn, MAConnAddr* addr)) { LOGST("maConnGetAddr(%i, 0x%08X)", conn, addr); if(conn == HANDLE_LOCAL) { if(addr->family == CONN_FAMILY_BT) { TBTDevAddr btaddr; //TPckgBuf<TBTDevAddr> pckg(btaddr); //old style, might work on Symbian 7.0 and earlier //update: doesn't work on 6630. #if 0//!defined(__SERIES60_3X__) RSocket socket; socket.Open(gSocketServ, KBTAddrFamily, KSockSeqPacket, KL2CAP); TRequestStatus status; socket.Ioctl(KHCILocalAddressIoctl, status, &pckg, KSolBtHCI); User::WaitForRequest(status); LOG("KHCILocalAddressIoctl result: %i\n", status.Int()); socket.Close(); if(status != KErrNone) { return CONNERR_GENERIC; } #else #if 0 //doesn't work; bug in Symbian. int result = RProperty::Get(KUidSystemCategory, KPropertyKeyBluetoothGetLocalDeviceAddress, pckg); LOG("KPropertyKeyBluetoothGetLocalDeviceAddress result: %i\n", result); if(result != KErrNone) { return CONNERR_GENERIC; } #else //open a server socket and read its address. RSocket socket; socket.Open(gSocketServ, KBTAddrFamily, KSockStream, KRFCOMM); int channel; LHEL_BASE(socket.GetOpt(KRFCOMMGetAvailableServerChannel, KSolBtRFCOMM, channel), return CONNERR_GENERIC); TBTSockAddr btsockaddr; btsockaddr.SetPort(channel); LHEL_BASE(socket.Bind(btsockaddr), return CONNERR_GENERIC); socket.LocalName(btsockaddr); socket.Close(); btaddr = btsockaddr.BTAddr(); #endif //0 #endif //0//__SERIES60_3X__ storeBtAddr(btaddr, addr); return 1; } else { //TCP server return CONNERR_INTERNAL;