static int Resolve(AG_NetAddrList *nal, const char *host, const char *port, Uint flags) { struct addrinfo hints, *res, *res0; enum ag_net_addr_family af; AG_NetAddr *na; int rv; memset(&hints, 0, sizeof(hints)); hints.ai_family = PF_UNSPEC; hints.ai_socktype = SOCK_STREAM; if (flags & AG_NET_NUMERIC_HOST) { hints.ai_flags |= AI_NUMERICHOST; } if ((rv = getaddrinfo(host, port, &hints, &res0)) != 0) { AG_SetError("%s:%s: %s", host, port, gai_strerror(rv)); return (-1); } for (res = res0; res != NULL; res = res->ai_next) { if ((af = GetAddrFamily(res->ai_family)) == 0) { continue; } if ((na = SockAddrToNetAddr(af, res->ai_addr)) != NULL) TAILQ_INSERT_TAIL(nal, na, addrs); } freeaddrinfo(res0); return (0); }
static int GetIfConfig(AG_NetAddrList *nal) { #ifdef HAVE_SIOCGIFCONF enum ag_net_addr_family af; AG_NetAddr *na; char buf[4096]; struct ifconf conf; struct ifreq *ifr; int sock; AG_NetAddrListClear(nal); if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { AG_SetError("socket: %s", strerror(errno)); return (-1); } conf.ifc_len = sizeof(buf); conf.ifc_buf = (caddr_t)buf; if (ioctl(sock, SIOCGIFCONF, &conf) < 0) { AG_SetError("SIOCGIFCONF: %s", strerror(errno)); goto fail; } #if !defined(_SIZEOF_ADDR_IFREQ) #define _SIZEOF_ADDR_IFREQ sizeof #endif for (ifr = (struct ifreq *)buf; (char *)ifr < &buf[conf.ifc_len]; ifr = (struct ifreq *)((char *)ifr + _SIZEOF_ADDR_IFREQ(*ifr))) { if ((af = GetAddrFamily(ifr->ifr_addr.sa_family)) == 0) { continue; } if ((na = SockAddrToNetAddr(af, &ifr->ifr_addr)) == NULL) { goto fail; } na->port = 0; TAILQ_INSERT_TAIL(nal, na, addrs); } close(sock); return (0); fail: close(sock); return (-1); #else AG_SetError("GetIfConfig: SIOCGIFCONF unavailable"); return (-1); #endif /* !HAVE_SIOCGIFCONF */ }
void CT_DataRSocketServ::DoCmdStopProtocolL(const TDesC& aSection, const TInt aAsyncErrorIndex) { TUint addrFamily = iProtocolDesc.iAddrFamily; TPtrC addrFamilyStr; if( GetStringFromConfig(aSection, KAddrFamily(), addrFamilyStr) ) { if( GetAddrFamily( addrFamilyStr, addrFamily ) ) { INFO_PRINTF3(_L("RSocketServ::StopProtocol() - Using addrFamily %S (%d)"), &addrFamilyStr, addrFamily); } else { TInt addrFamilyInt; if( GetIntFromConfig(aSection, KAddrFamily(), addrFamilyInt) ) { addrFamily = addrFamilyInt; INFO_PRINTF2(_L("RSocketServ::StopProtocol() - Using addrFamily %d"), addrFamily); } else { ERR_PRINTF2(_L("RSocketServ::StopProtocol() - Given addrFamily (%S) is invalid"), &addrFamilyStr); SetBlockResult(EFail); } } } TUint sockType = iProtocolDesc.iSockType; TPtrC sockTypeStr; if( GetStringFromConfig(aSection, KSockType(), sockTypeStr) ) { if( GetSockType( sockTypeStr, sockType ) ) { INFO_PRINTF3(_L("RSocketServ::StopProtocol() - Using sockType %S (%d)"), &sockTypeStr, sockType); } else { TInt sockTypeInt; if( GetIntFromConfig(aSection, KSockType(), sockTypeInt) ) { sockType = sockTypeInt; INFO_PRINTF2(_L("RSocketServ::StopProtocol() - Using sockType %d"), sockType); } else { ERR_PRINTF2(_L("RSocketServ::StopProtocol() - Given sockType (%S) is invalid"), &sockTypeStr); SetBlockResult(EFail); } } } TUint protocol = iProtocolDesc.iProtocol; TPtrC protocolStr; if( GetStringFromConfig(aSection, KProtocol(), protocolStr) ) { if( GetProtocol( protocolStr, protocol ) ) { INFO_PRINTF3(_L("RSocketServ::StopProtocol() - Using protocol %S (%d)"), &protocolStr, protocol); } else { TInt protocolInt; if( GetIntFromConfig(aSection, KProtocol(), protocolInt) ) { protocol = protocolInt; INFO_PRINTF2(_L("RSocketServ::StopProtocol() - Using protocol %d"), protocol); } else { ERR_PRINTF2(_L("RSocketServ::StopProtocol() - Given protocol (%S) is invalid"), &protocolStr); SetBlockResult(EFail); } } } INFO_PRINTF4(_L("Calling RSocketServ::StopProtocol(%d, %d, %d)"), addrFamily, sockType, protocol); iSocketServ->StopProtocol(addrFamily, sockType, protocol, iActiveStopProtocol->iStatus); iActiveStopProtocol->Activate(aAsyncErrorIndex); IncOutstanding(); }
void CT_DataRSocketServ::DoCmdFindProtocol(const TDesC& aSection) { TInt err = KErrNone; TPtrC protocolName; if( GetStringFromConfig(aSection, KNamedProtocol(), protocolName) ) { // We have specified a protocol name INFO_PRINTF2(_L("Calling RSocketServ::FindProtocol(%S)"), &protocolName); err = iSocketServ->FindProtocol(protocolName, iProtocolDesc); if( err!=KErrNone ) { ERR_PRINTF3(_L("RSocketServ::FindProtocol(%S) failed with error %d"), &protocolName, err); SetError(err); } else { PrintProtocol(_L("RSocketServ::FindProtocol()"), iProtocolDesc, ETrue); // Get the expected results from the ini file TPtrC expectedName; if( GetStringFromConfig(aSection, KExpectedValueName(), expectedName) ) { if( expectedName != iProtocolDesc.iName ) { ERR_PRINTF4(_L("RSocketServ::FindProtocol(%S) expected name(%S) != actual name(%S)"), &protocolName, &expectedName, &iProtocolDesc.iName); SetBlockResult(EFail); } } TUint expectedAddrFamily; TPtrC expectedAddrFamilyStr; if( GetStringFromConfig(aSection, KExpectedValueAddrFamily(), expectedAddrFamilyStr) ) { if( GetAddrFamily( expectedAddrFamilyStr, expectedAddrFamily ) ) { INFO_PRINTF3(_L("RSocketServ::FindProtocol() - Using expected addrFamily %S (%d)"), &expectedAddrFamilyStr, expectedAddrFamily); if( expectedAddrFamily != iProtocolDesc.iAddrFamily ) { ERR_PRINTF4(_L("RSocketServ::FindProtocol(%S) expected Addr Family(%d) != actual Addr Family(%d)"), &protocolName, expectedAddrFamily, iProtocolDesc.iAddrFamily); SetBlockResult(EFail); } } else { ERR_PRINTF2(_L("RSocketServ::FindProtocol() - Given addrFamily (%S) is invalid"), &expectedAddrFamilyStr); SetBlockResult(EFail); } } TUint expectedProtocol; TPtrC expectedProtocolStr; if( GetStringFromConfig(aSection, KExpectedValueProtocol(), expectedProtocolStr) ) { if( GetProtocol( expectedProtocolStr, expectedProtocol ) ) { INFO_PRINTF3(_L("RSocketServ::FindProtocol() - Using expected protocol %S (%d)"), &expectedProtocolStr, expectedProtocol); if( expectedProtocol != iProtocolDesc.iProtocol ) { ERR_PRINTF4(_L("RSocketServ::FindProtocol(%S) expected protocol(%d) != actual protocol(%d)"), &protocolName, expectedProtocol, iProtocolDesc.iProtocol); SetBlockResult(EFail); } } else { ERR_PRINTF2(_L("RSocketServ::FindProtocol() - Given protocol (%S) is invalid"), &expectedProtocolStr); SetBlockResult(EFail); } } } } else { ERR_PRINTF2(_L("RSocketServ::FindProtocol() error - No %S given"), &KNamedProtocol()); SetBlockResult(EFail); } }
void CT_DataRSocketServ::DoCmdConnect(const TDesC& aSection) { TInt messageSlots=KESockDefaultMessageSlots; GetIntFromConfig(aSection, KMessageSlots(), messageSlots); TUint protocol = 0; TPtrC protocolStr; TBool hasProtocol = GetStringFromConfig(aSection, KProtocol(), protocolStr); if( hasProtocol ) { if( GetProtocol( protocolStr, protocol ) ) { INFO_PRINTF3(_L("RSocketServ::Connect() - Using protocol %S (%d)"), &protocolStr, protocol); } else { TInt protocolInt; if( GetIntFromConfig(aSection, KProtocol(), protocolInt) ) { protocol = protocolInt; INFO_PRINTF2(_L("RSocketServ::Connect() - Using protocol %d"), protocol); } else { ERR_PRINTF2(_L("RSocketServ::Connect() - Given protocol (%S) is invalid"), &protocolStr); SetBlockResult(EFail); } } } TUint addrFamily = 0; TPtrC addrFamilyStr; TBool hasAddrFamily = GetStringFromConfig(aSection, KAddrFamily(), addrFamilyStr); if( hasAddrFamily ) { if( GetAddrFamily( addrFamilyStr, addrFamily ) ) { INFO_PRINTF3(_L("RSocketServ::Connect() - Using addrFamily %S (%d)"), &addrFamilyStr, addrFamily); } else { TInt addrFamilyInt; if( GetIntFromConfig(aSection, KAddrFamily(), addrFamilyInt) ) { addrFamily = addrFamilyInt; INFO_PRINTF2(_L("RSocketServ::Connect() - Using addrFamily %d"), addrFamily); } else { ERR_PRINTF2(_L("RSocketServ::Connect() - Given addrFamily (%S) is invalid"), &addrFamilyStr); SetBlockResult(EFail); } } } TInt err; if( hasProtocol || hasAddrFamily ) { TSessionPref preference; preference.iProtocol = protocol; preference.iAddrFamily = addrFamily; INFO_PRINTF4(_L("Calling RSocketServ::Connect({%d, %d}, %d)"), preference.iProtocol, preference.iAddrFamily, messageSlots); err = iSocketServ->Connect(preference, messageSlots); } else { INFO_PRINTF2(_L("Calling RSocketServ::Connect(%d)"), messageSlots); err = iSocketServ->Connect(messageSlots); } if( err!=KErrNone ) { ERR_PRINTF2(_L("RSocketServ::Connect() failed with error %d"), err); SetError(err); } }