Exemplo n.º 1
0
void DNS::aierror(int code, const std::string& arg)
{
#if defined(POCO_HAVE_IPv6)
	switch (code)
	{
	case EAI_AGAIN:
		throw DNSException("Temporary DNS error while resolving", arg);
	case EAI_FAIL:
		throw DNSException("Non recoverable DNS error while resolving", arg);
#if !defined(_WIN32) // EAI_NODATA and EAI_NONAME have the same value
	case EAI_NODATA:
		throw NoAddressFoundException(arg);
#endif
	case EAI_NONAME:
		throw HostNotFoundException(arg);
#if defined(EAI_SYSTEM)
	case EAI_SYSTEM:
		error(lastError(), arg);
		break;
#endif
#if defined(_WIN32)
	case WSANO_DATA: // may happen on XP
		throw HostNotFoundException(arg);
#endif
	default:
		throw DNSException("EAI", NumberFormatter::format(code));
	}
#endif // POCO_HAVE_IPv6
}
Exemplo n.º 2
0
void SocketAddress::init(const std::string& host, Poco::UInt16 port)
{
	IPAddress ip;
	if (IPAddress::tryParse(host, ip))
	{
		init(ip, port);
#if defined(POCO_HAVE_IPv6)
		std::string::size_type pos = host.rfind('%');
		if (std::string::npos != pos)
		{
			std::string scope(host, pos + 1);
		#ifdef _WIN32
			((sockaddr_in6*)addr())->sin6_scope_id = atoi(scope.c_str());
		#else
			if (']' == scope[scope.length() - 1])
			{
				scope.resize(scope.length() - 1);
			}
			((sockaddr_in6*)addr())->sin6_scope_id = if_nametoindex(scope.c_str());
		#endif
		}
#endif
	}
	else
	{
		HostEntry he = DNS::hostByName(host);
		if (he.addresses().size() > 0)
			init(he.addresses()[0], port);
		else throw HostNotFoundException("No address found for host", host);
	}
}
Exemplo n.º 3
0
void DNS::error(int code, const std::string& arg)
{
	switch (code)
	{
	case POCO_ESYSNOTREADY:
		throw NetException("Net subsystem not ready");
	case POCO_ENOTINIT:
		throw NetException("Net subsystem not initialized");
	case POCO_HOST_NOT_FOUND:
		throw HostNotFoundException(arg);
	case POCO_TRY_AGAIN:
		throw DNSException("Temporary DNS error while resolving", arg);
	case POCO_NO_RECOVERY:
		throw DNSException("Non recoverable DNS error while resolving", arg);
	case POCO_NO_DATA:
		throw NoAddressFoundException(arg);
	default:
		throw IOException(NumberFormatter::format(code));
	}
}
Exemplo n.º 4
0
void SocketAddress::init(const std::string& host, Poco::UInt16 port)
{
	IPAddress ip;
	if (IPAddress::tryParse(host, ip))
	{
		init(ip, port);
	}
	else
	{
		HostEntry he = DNS::hostByName(host);
		HostEntry::AddressList addresses = he.addresses();
		if (addresses.size() > 0)
		{
#if defined(POCO_HAVE_IPv6)
			// if we get both IPv4 and IPv6 addresses, prefer IPv4
			std::sort(addresses.begin(), addresses.end(), AFLT());
#endif
			init(addresses[0], port);
		}
		else throw HostNotFoundException("No address found for host", host);
	}
}
Exemplo n.º 5
0
	UnixSocket::UnixSocket(const char *address, unsigned short port) {
		int fd = socket (PF_INET, SOCK_STREAM, 0);
		if (fd == -1) {
			char message[200];
			snprintf(message, sizeof(message),
				"Cannot create socket: %s",
				strerror(errno));
			throw SocketException(message, errno);
		}

		struct hostent *ent;
		ent = gethostbyname(address);
		if (ent == NULL) {
			char message[200];
			snprintf(message, sizeof(message),
				"Host %s not found",
				address);
			close(fd);
			throw HostNotFoundException(message, h_errno);
		}

		sockaddr_in addr;
		addr.sin_family = AF_INET;
		addr.sin_port = htons(port);
		addr.sin_addr = *(struct in_addr *) ent->h_addr;
		if (connect(fd, (const struct sockaddr *) &addr, sizeof(addr)) == -1) {
			char message[300];
			snprintf(message, sizeof(message),
				"Cannot connect to %s:%d: %s",
				address, port, strerror(errno));
			close(fd);
			throw SocketException(message, errno);
		}

		construct(fd);
	}
Exemplo n.º 6
0
void Web::Request::Connect() {
   
	SOCKET httpSocket;
	int ret;
	struct sockaddr_in httpAddr;
	struct hostent *hostinfo;
	char buffer[REQUESTBUFFERSIZE + 1];
	int bufferSize;
   int timeout = REQUESTTIMEOUT;

   /* Choses settings for the socket to connect to */
   httpAddr.sin_family = AF_INET;

   /* Set the port */
   //httpAddr.sin_port = htons(url->getPort());
   httpAddr.sin_port = htons(80);

   /* Do a hostname lookup on the host */
   //hostinfo = gethostbyname (url->getHost());
   hostinfo = gethostbyname ("localhost");
   if (hostinfo == NULL) {
      throw HostNotFoundException();
   }
   
   /* Fill the httpAddr with IP data */
   httpAddr.sin_addr = *(struct in_addr *) hostinfo->h_addr;
    
	/* Creates the socket */
	httpSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP) ;
	
	/* Check for errors */
	if (httpSocket == INVALID_SOCKET)
	    throw SocketException();

   /* Add a timeout on all communiction*/
   ret = setsockopt(httpSocket, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout));

   /* Check for errors */
   if (ret == SOCKET_ERROR) {
      closesocket(httpSocket);
      throw SocketException();
   }

   /* Connect to the server */
   ret = connect(httpSocket,(LPSOCKADDR)&httpAddr, sizeof(struct sockaddr));

   /* Check for errors */
   if (ret == SOCKET_ERROR) {
      closesocket(httpSocket);
      throw ConnectionFailedException(WSAGetLastError());
   }

   /* Create a request */
   bufferSize = _snprintf(buffer, REQUESTBUFFERSIZE, "GET %s HTTP/1.0\r\n\r\n", url->getURL());

   if (bufferSize <= 0) {
      closesocket(httpSocket);
      throw Exception("Request Buffer Too Small");
   }
      
   /* Send a HTTP request */
   ret = send(httpSocket, buffer, bufferSize, 0);

   if (ret < bufferSize) {
      closesocket(httpSocket);
      throw TransferException();
   }
   
   reply.assign("");
   ret = REQUESTBUFFERSIZE;
   
   /* Now wait for the return and loop while its coming */
   while (ret > 0) {
      ret = recv(httpSocket, buffer, REQUESTBUFFERSIZE, 0);
      
      /* Append data to the buffer */
      if (ret > 0)
         reply.append(buffer, ret);
   }

   /* Clean up the socket */
   closesocket(httpSocket);

   /* Now if we didn't get any data throw a error */
   if (reply.length()==0) {
      throw TransferException();
   }

   /* Got some data let parse it */
   ret = (int)reply.find("\r\n\r\n");

   if (ret==-1) {
      throw InvalidDataException();
   }
   
   header = reply.substr(0, ret);
   ret += 4;
   body = reply.substr(ret, reply.length() - ret);  

}