/** * *this SocketAddress was created from the numeric IP address of a * connecting host. * The lo and hi addresses are the limit checks from the ACL file. * Return true if this address is in range of any of the address pairs * in the limit check range. * * This check is executed on every incoming connection. */ bool SocketAddress::inRange(const SocketAddress& lo, const SocketAddress& hi) const { (*this).firstAddress(); lo.firstAddress(); hi.firstAddress(); const ::addrinfo& thisInfo = getAddrInfo(*this); const ::addrinfo& loInfo = getAddrInfo(lo); const ::addrinfo& hiInfo = getAddrInfo(hi); if (inRange(thisInfo, loInfo, hiInfo)) { return true; } while (lo.nextAddress()) { if (!hi.nextAddress()) { assert (false); throw(Exception(QPID_MSG("Comparison iteration fails: " + lo.asString() + hi.asString()))); } const ::addrinfo& loInfo = getAddrInfo(lo); const ::addrinfo& hiInfo = getAddrInfo(hi); if (inRange(thisInfo, loInfo, hiInfo)) { return true; } } return false; }
void SocketAddress::set(const char* hostname, int family, uint16_t port) throw (UnknownHostException) { int addrLen = 0; MX_ASSERT(NULL != hostname); if (AF_UNSPEC == family) { if (!getAddrInfo(hostname, AF_INET6, &data_, &addrLen) && !getAddrInfo( hostname, AF_INET, &data_, &addrLen)) { THROW2(UnknownHostException, std::string("Unknown hostname: [") + hostname + "]"); } } else { if (!getAddrInfo(hostname, family, &data_, &addrLen)) { THROW2(UnknownHostException, std::string("Unknown hostname: [") + hostname + "]"); } } setPort(port); }
void DhtRunner::run(const char* ip4, const char* ip6, const char* service, DhtRunner::Config config) { auto res4 = getAddrInfo(ip4, service); auto res6 = getAddrInfo(ip6, service); run(res4.empty() ? nullptr : (sockaddr_in*) &res4.front().first, res6.empty() ? nullptr : (sockaddr_in6*)&res6.front().first, config); }
void DhtRunner::run(const char* ip4, const char* ip6, const char* service, const crypto::Identity identity, bool threaded, StatusCallback cb) { auto res4 = getAddrInfo(ip4, service); auto res6 = getAddrInfo(ip6, service); run(res4.empty() ? nullptr : (sockaddr_in*) &res4.front().first, res6.empty() ? nullptr : (sockaddr_in6*)&res6.front().first, identity, threaded, cb); }
void Socket::connect(const SocketAddress& addr) const { peername = addr.asString(false); createSocket(addr); const SOCKET& socket = impl->fd; int err; WSASetLastError(0); if ((::connect(socket, getAddrInfo(addr).ai_addr, getAddrInfo(addr).ai_addrlen) != 0) && ((err = ::WSAGetLastError()) != WSAEWOULDBLOCK)) throw qpid::Exception(QPID_MSG(strError(err) << ": " << peername)); }
int Socket::listen(const SocketAddress& addr, int backlog) const { createSocket(addr); const SOCKET& socket = impl->fd; BOOL yes=1; QPID_WINSOCK_CHECK(setsockopt(socket, SOL_SOCKET, SO_REUSEADDR, (char *)&yes, sizeof(yes))); if (::bind(socket, getAddrInfo(addr).ai_addr, getAddrInfo(addr).ai_addrlen) == SOCKET_ERROR) throw Exception(QPID_MSG("Can't bind to " << addr.asString() << ": " << strError(WSAGetLastError()))); if (::listen(socket, backlog) == SOCKET_ERROR) throw Exception(QPID_MSG("Can't listen on " <<addr.asString() << ": " << strError(WSAGetLastError()))); return getLocalPort(socket); }
struct sockaddr_in *creerSockAddr(char *name, char *port, int type) { //Création de la structure socketaddr : new struct addrinfo *res; res = getAddrInfo(name, port, type); return ((struct sockaddr_in *) res->ai_addr); }
int initTLS_S(struct tls* ctx) { tlsServer_conf = tls_config_new(); tls_config_set_cert_file(tlsServer_conf, getServerCertFile_v()); tls_config_set_key_file(tlsServer_conf, getServerCertFile_v()); tls_configure(ctx, tlsServer_conf); int sock; struct addrinfo *servinfo, *p; servinfo = getAddrInfo(); for(p = servinfo; p != NULL; p = p->ai_next) { if ((sock = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) { syslog(LOG_ERR, "socket error"); continue; } if (bind(sock, p->ai_addr, p->ai_addrlen) == -1) { close(sock); syslog(LOG_ERR, "bind error"); continue; } break; // if we get here, we must have connected successfully } freeaddrinfo(servinfo); syslog(LOG_DEBUG, "start socket\n"); return sock; }
void SocketAddress::firstAddress() const { if (addrInfo) { currentAddrInfo = addrInfo; } else { (void) getAddrInfo(*this); } }
int bindedSocket(char *nom, char* service, int type, struct sockaddr_in *addrin) { int sock = -1; int yes = 1; struct addrinfo *rp; rp = getAddrInfo(nom, service, type); if ((sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol)) <= 0) { perror("\n pb creation socket \n"); } setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof yes); #ifdef DEBUG printf("La socket num %d\n", sock); #endif if (bind(sock, rp->ai_addr, rp->ai_addrlen) < 0) { perror("\n pb de bind"); sock = -2; } if (addrin !=NULL) { //On copie l'adresse memcpy(addrin, rp->ai_addr, rp->ai_addrlen); } freeaddrinfo(rp); return (sock); }
std::string SocketAddress::asString(bool numeric) const { if (!numeric) return host + ":" + port; // Canonicalise into numeric id const ::addrinfo& ai = getAddrInfo(*this); return asString(ai.ai_addr, ai.ai_addrlen); }
std::string SocketAddress::asString(bool numeric, bool dispNameOnly, bool hideDecoration) const { if (!numeric) return host + ":" + port; // Canonicalise into numeric id const ::addrinfo& ai = getAddrInfo(*this); return asString(ai.ai_addr, ai.ai_addrlen, dispNameOnly, hideDecoration); }
void Socket::createSocket(const SocketAddress& sa) const { SOCKET& socket = impl->fd; if (socket != INVALID_SOCKET) Socket::close(); SOCKET s = ::socket (getAddrInfo(sa).ai_family, getAddrInfo(sa).ai_socktype, 0); if (s == INVALID_SOCKET) throw QPID_WINDOWS_ERROR(WSAGetLastError()); socket = s; try { if (nonblocking) setNonblocking(); if (nodelay) setTcpNoDelay(); } catch (std::exception&) { ::closesocket(s); socket = INVALID_SOCKET; throw; } }
struct sockaddr_in *creerSock(char *name, char *port, int type, int *sockId) { //Création de la structure socketaddr : new struct addrinfo *res; res = getAddrInfo(name, port, type); if ((*sockId = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) <= 0) { perror("\n pb creation socket \n"); } //TODO copier avent de renvoyer et libérer res : freeaddrinfo(res); return ((struct sockaddr_in *) res->ai_addr); }
/** * For ACL address matching make sure that the two addresses, *this * which is the low address and hiPeer which is the high address, are * both numeric ip addresses of the same family and that hi > *this. * * Note that if the addresses resolve to more than one struct addrinfo * then this and the hiPeer must be equal. This avoids having to do * difficult range checks where the this and hiPeer both resolve to * multiple IPv4 or IPv6 addresses. * * This check is run at acl file load time and not at run tme. */ bool SocketAddress::isComparable(const SocketAddress& hiPeer) const { try { // May only compare if this socket is IPv4 or IPv6 SocketAddress lo(*this); const ::addrinfo& peerLoInfo = getAddrInfo(lo); if (!(peerLoInfo.ai_family == AF_INET || peerLoInfo.ai_family == AF_INET6)) { return false; } try { // May only compare if peer socket is same family SocketAddress hi(hiPeer); const ::addrinfo& peerHiInfo = getAddrInfo(hi); if (peerLoInfo.ai_family != peerHiInfo.ai_family) { return false; } // Host names that resolve to lists are allowed if they are equal. // For example: localhost, or fjord.lab.example.com if ((*this).asString() == hiPeer.asString()) { return true; } // May only compare if this and peer resolve to single address. if (lo.nextAddress() || hi.nextAddress()) { return false; } // Make sure that the lo/hi relationship is ok int res; if (!compareAddresses(peerLoInfo, peerHiInfo, res) || res < 0) { return false; } return true; } catch (Exception) { // failed to resolve hi return false; } } catch (Exception) { // failed to resolve lo return false; } }
int FSocketDomain::connect(const char *addr, const in_port_t port) { struct addrinfo *res; if (getAddrInfo(addr, port, &res) != 0) { return -1; } int ret = connect(res); ::freeaddrinfo(res); return ret; }
bool Socket::bindImpl(const std::string& hostname,int port) { close(); addrinfo* p=getAddrInfo(hostname.c_str(),port,false); for(;p != 0; p = p->ai_next) { m_socket=socket(p->ai_family,p->ai_socktype,p->ai_protocol); if(m_socket == int(INVALID_SOCKET)) { std::cerr<<"Cannot create a socket: "<<getErrorMessage()<<std::endl; continue; } if(::bind(m_socket,p->ai_addr,p->ai_addrlen)==SOCKET_ERROR) { std::cerr<<"Cannot bind to port "<<port<<": "<<getErrorMessage()<<std::endl; close(); continue; } break; } freeAddrInfo(p); if(!p) { std::cerr<<"Cannot create a socket: "<<getErrorMessage()<<std::endl; return false; } if(listen(m_socket,10)==SOCKET_ERROR) { std::cerr<<"Cannot listen socket:"<<getErrorMessage()<<std::endl; close(); return false; } if(setNonBlock(m_socket) == false) { std::cerr<<"Cannot set socket to non-blocking mode: "<<getErrorMessage()<<std::endl; close(); return false; } return true; }
/** * this represents the low address of an ACL address range. * Given rangeHi that represents the high address, * return a string showing the numeric comparisons that the * inRange checks will do for address pair. */ std::string SocketAddress::comparisonDetails(const SocketAddress& rangeHi) const { std::ostringstream os; SocketAddress thisSa(*this); SocketAddress rangeHiSa(rangeHi); (void) getAddrInfo(thisSa); (void) getAddrInfo(rangeHiSa); os << "(" << thisSa.asString(true, true, false) << "," << rangeHiSa.asString(true, true, false) << ")"; while (thisSa.nextAddress()) { if (!rangeHiSa.nextAddress()) { throw(Exception(QPID_MSG("Comparison iteration fails: " + (*this).asString() + rangeHi.asString()))); } os << ",(" << thisSa.asString(true, true, false) << "," << rangeHiSa.asString(true, true, false) << ")"; } if (rangeHiSa.nextAddress()) { throw(Exception(QPID_MSG("Comparison iteration fails: " + (*this).asString() + rangeHi.asString()))); } std::string result = os.str(); return result; }
bool Socket::connectImpl(const std::string& hostname,const int port) { close(); addrinfo* p=getAddrInfo(hostname.c_str(),port,false); if(!p) return false; std::string errormsg; for(;p != 0; p = p->ai_next) { m_socket=socket(p->ai_family,p->ai_socktype,p->ai_protocol); if(m_socket == int(INVALID_SOCKET)) continue; if(::connect(m_socket,p->ai_addr,p->ai_addrlen) == SOCKET_ERROR) { errormsg=getErrorMessage(); close(); continue; } break; } freeAddrInfo(p); if(!p) { std::cerr<<"Cannot connect to \""<<hostname<<"\" port "<<port<<": "<<errormsg<<std::endl; return false; } if(setNonBlock(m_socket) == false) { std::cerr<<"Cannot set socket to non-blocking mode: "<<getErrorMessage()<<std::endl; close(); } return true; }
//------------------------------------------------------------------------------ //! RCP<ConnectedSocketDevice> Socket::connect( const Endpoint& ep ) { #if BASE_SOCKET_USE_BSD || BASE_SOCKET_USE_WINSOCK state( STATE_CONNECTING ); addrinfo* candidates = getAddrInfo( *this, ep.hostname().cstr(), ep.port() ); if( candidates == NULL ) { return NULL; } Family family = fromAPI_family ( candidates->ai_family ); Type type = fromAPI_type ( candidates->ai_socktype ); Protocol protocol = fromAPI_protocol( candidates->ai_protocol ); RCP<Socket> socket = new Socket( family, type, protocol ); int err = ::connect( socket->_impl->_sock, candidates->ai_addr, SocketSSizeT(candidates->ai_addrlen) ); if( err != 0 ) { printAPIError( "ERROR - Socket::connect() - connect failed" ); freeaddrinfo( candidates ); return NULL; } RCP<ConnectedSocketDevice> device = new ConnectedSocketDevice( socket.ptr() ); device->_remoteEndpoint = ep; freeaddrinfo( candidates ); state( STATE_CONNECTED ); return device; #else return NULL; #endif }
/** * Return true if this SocketAddress is IPv4 or IPv6 */ bool SocketAddress::isIp() const { const ::addrinfo& ai = getAddrInfo(*this); return ai.ai_family == AF_INET || ai.ai_family == AF_INET6; }
int MDnsSdListener::Handler::runCommand(SocketClient *cli, int argc, char **argv) { if (argc < 2) { char* msg = NULL; asprintf( &msg, "Invalid number of arguments to mdnssd: %i", argc); ALOGW("%s", msg); cli->sendMsg(ResponseCode::CommandParameterError, msg, false); free(msg); return -1; } char* cmd = argv[1]; if (strcmp(cmd, "discover") == 0) { if (argc != 4) { cli->sendMsg(ResponseCode::CommandParameterError, "Invalid number of arguments to mdnssd discover", false); return 0; } int requestId = atoi(argv[2]); char *serviceType = argv[3]; discover(cli, NULL, serviceType, NULL, requestId, 0); } else if (strcmp(cmd, "stop-discover") == 0) { stop(cli, argc, argv, "discover"); } else if (strcmp(cmd, "register") == 0) { if (argc != 6) { cli->sendMsg(ResponseCode::CommandParameterError, "Invalid number of arguments to mdnssd register", false); return 0; } int requestId = atoi(argv[2]); char *serviceName = argv[3]; char *serviceType = argv[4]; int port = atoi(argv[5]); char *interfaceName = NULL; // will use all char *domain = NULL; // will use default char *host = NULL; // will use default hostname int textLen = 0; void *textRecord = NULL; serviceRegister(cli, requestId, interfaceName, serviceName, serviceType, domain, host, port, textLen, textRecord); } else if (strcmp(cmd, "stop-register") == 0) { stop(cli, argc, argv, "register"); } else if (strcmp(cmd, "resolve") == 0) { if (argc != 6) { cli->sendMsg(ResponseCode::CommandParameterError, "Invalid number of arguments to mdnssd resolve", false); return 0; } int requestId = atoi(argv[2]); char *interfaceName = NULL; // will use all char *serviceName = argv[3]; char *regType = argv[4]; char *domain = argv[5]; resolveService(cli, requestId, interfaceName, serviceName, regType, domain); } else if (strcmp(cmd, "stop-resolve") == 0) { stop(cli, argc, argv, "resolve"); } else if (strcmp(cmd, "start-service") == 0) { if (mMonitor->startService()) { cli->sendMsg(ResponseCode::CommandOkay, "Service Started", false); } else { cli->sendMsg(ResponseCode::ServiceStartFailed, "Service already running", false); } } else if (strcmp(cmd, "stop-service") == 0) { if (mMonitor->stopService()) { cli->sendMsg(ResponseCode::CommandOkay, "Service Stopped", false); } else { cli->sendMsg(ResponseCode::ServiceStopFailed, "Service still in use", false); } } else if (strcmp(cmd, "sethostname") == 0) { if (argc != 4) { cli->sendMsg(ResponseCode::CommandParameterError, "Invalid number of arguments to mdnssd sethostname", false); return 0; } int requestId = atoi(argv[2]); char *hostname = argv[3]; setHostname(cli, requestId, hostname); } else if (strcmp(cmd, "stop-sethostname") == 0) { stop(cli, argc, argv, "sethostname"); } else if (strcmp(cmd, "getaddrinfo") == 0) { if (argc != 4) { cli->sendMsg(ResponseCode::CommandParameterError, "Invalid number of arguments to mdnssd getaddrinfo", false); return 0; } int requestId = atoi(argv[2]); char *hostname = argv[3]; char *interfaceName = NULL; // default int protocol = 0; // intelligient heuristic (both v4 + v6) getAddrInfo(cli, requestId, interfaceName, protocol, hostname); } else if (strcmp(cmd, "stop-getaddrinfo") == 0) { stop(cli, argc, argv, "getaddrinfo"); } else { if (VDBG) ALOGE("Unknown cmd %s", cmd); cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown mdnssd cmd", false); return 0; } return 0; }
int FSocketDomain::getAddrInfo(const char *addr, const in_port_t port, struct addrinfo **res) { return getAddrInfo(this->m_socketDomain, this->m_socketType, addr, port, res); }