コード例 #1
0
int RedisClient::Connect(std::string host, long port)
{
    try
    {
        if(this->socket != NULL)
        {
            this->socket->close();
            this->socket = NULL;
        }

        std::string port_str(boost::lexical_cast<std::string>(port));

        this->resolver = new boost::asio::ip::tcp::resolver(this->io_service);
        boost::asio::ip::tcp::resolver::query query(host, port_str);

        this->endpoint = this->resolver->resolve(query);

        this->socket = new boost::asio::ip::tcp::socket(this->io_service);
        boost::asio::connect(*(this->socket), this->endpoint);

    }
    catch(std::exception &e)
    {
        std::cerr << "Client connect error." << std::endl;
        std::cerr << e.what() << std::endl;
        if(this->socket)
        {
            this->socket->close();
        }
        this->socket = NULL;
        return -1;
    }
    return 0;
}
コード例 #2
0
ファイル: netschedule_key.cpp プロジェクト: swuecho/igblast
CNetScheduleKeyGenerator::CNetScheduleKeyGenerator(
        const string& host, unsigned port, const string& queue_name)
{
    SNetScheduleAPIImpl::VerifyQueueNameAlphabet(queue_name);

    m_UseIPv4Addr = CSocketAPI::isip(host, true);
    if (m_UseIPv4Addr)
        m_HostIPv4Addr = CSocketAPI::gethostbyname(host);
    else
        m_HostName = host;
    m_Port = (unsigned short) port;
    m_QueueName = queue_name;

    string port_str(NStr::IntToString(port));

    unsigned queue_prefix_len = g_NumberOfUnderscoresPlusOne(queue_name);

    m_V1HostPortQueue.reserve(1 + host.size() + 1 + port_str.size() +
            queue_prefix_len + queue_name.size());

    m_V1HostPortQueue.push_back('_');
    m_V1HostPortQueue.append(host);
    m_V1HostPortQueue.push_back('_');
    m_V1HostPortQueue.append(port_str);
    m_V1HostPortQueue.append(queue_prefix_len, '_');
    m_V1HostPortQueue.append(queue_name);
}
コード例 #3
0
ファイル: udp_address.cpp プロジェクト: jcfr/libzmq
int zmq::udp_address_t::resolve (const char *name_)
{
    //  Find the ':' at end that separates address from the port number.
    const char *delimiter = strrchr (name_, ':');
    if (!delimiter) {
        errno = EINVAL;
        return -1;
    }

    //  Separate the address/port.
    std::string addr_str (name_, delimiter - name_);
    std::string port_str (delimiter + 1);

    //  Remove square brackets around the address, if any, as used in IPv6
    if (addr_str.size () >= 2 && addr_str [0] == '[' &&
          addr_str [addr_str.size () - 1] == ']')
        addr_str = addr_str.substr (1, addr_str.size () - 2);

    //  Parse the port number (0 is not a valid port).
    uint16_t port = (uint16_t) atoi (port_str.c_str ());
    if (port == 0) {
        errno = EINVAL;
        return -1;
    }

    dest_address.sin_family = AF_INET;
    dest_address.sin_port = htons (port);
    dest_address.sin_addr.s_addr = inet_addr (addr_str.c_str ());

    if (dest_address.sin_addr.s_addr == INADDR_NONE) {
        errno = EINVAL;
        return -1;
    }

    // we will check only first byte of IP
    // and if it from 224 to 239, then it can
    // represent multicast IP.
    int i = dest_address.sin_addr.s_addr & 0xFF;
    if(i >=  224 && i <= 239) {
        multicast = dest_address.sin_addr;
        is_mutlicast = true;
    }
    else
        is_mutlicast = false;

    interface.s_addr = htons (INADDR_ANY);
    if (interface.s_addr == INADDR_NONE) {
        errno = EINVAL;
        return -1;
    }

    bind_address.sin_family = AF_INET;
    bind_address.sin_port = htons (port);
    bind_address.sin_addr.s_addr = htons (INADDR_ANY);

    address = name_;

    return 0;
}
コード例 #4
0
boost::tuple<std::string, std::string, std::string, std::string>
HttpRequest::ParseUrl(const std::string & url)
{
    //for remove regex lib
    size_t pos_protocol = url.find("://");
    std::string protocol("http");
    if (pos_protocol != std::string::npos)
    {
        protocol = url.substr(0, pos_protocol);
    }

    size_t pos_host_beg = (pos_protocol == std::string::npos ? 0 : pos_protocol +3);
    size_t pos_path_beg = -1;
    std::string host_str("");
    std::string port_str("80");
    size_t pos_host = url.find_first_of(':', pos_host_beg);
    size_t pos_slash = url.find_first_of('/', pos_host_beg);
    if (pos_host != std::string::npos && pos_host < pos_slash)
    {
        host_str = url.substr(pos_host_beg, pos_host - pos_host_beg);
        size_t pos_port_beg = pos_host + 1;
        size_t pos_port = url.find_first_of('/', pos_port_beg);
        if (pos_port != std::string ::npos)
        {
            port_str = url.substr(pos_port_beg, pos_port - pos_port_beg);
            pos_path_beg = pos_port;
        }
    }
    else {
        pos_host = url.find_first_of('/', pos_host_beg);
        if (pos_host != std::string::npos)
        {
            host_str = url.substr(pos_host_beg, pos_host - pos_host_beg);
            pos_path_beg = pos_host;
        }
    }

    std::string path_str("");
    if (pos_path_beg > 0)
        path_str = url.substr(pos_path_beg);

    //boost::regex reg("^(([A-Za-z]+)://)?([^:/]+)(:([0-9]+))?(.*)");
    //boost::smatch sm;

    //if (false == boost::regex_match(url, sm, reg))
    //{
    //    return boost::make_tuple("", "", "", "");
    //}

    //std::string protocol(sm[2].matched ? std::string(sm[2].first, sm[2].second) : "http");
    //std::string host_str(sm[3].first, sm[3].second);
    //std::string port_str(sm[5].matched ? std::string(sm[5].first, sm[5].second) : "");
    //std::string path_str(sm[6].matched ? std::string(sm[6].first, sm[6].second) : "/");
    if (path_str.empty()) path_str = "/";

    return boost::make_tuple(protocol, host_str, port_str, path_str);
}
コード例 #5
0
// split server:port string, doesn't always set the port
static void splitServerPort(const std::string& server,std::string& address,int *port)
{
    unsigned int colon=server.find(':',0);
    if(colon==std::string::npos) {
        address=server;
    }
    else {
        address=server.substr(0,colon);
        colon++;
        std::string port_str(server.substr(colon,server.length()-colon));
        port[0]=atoi(port_str.c_str());
    }
}
コード例 #6
0
ファイル: tcp_address.cpp プロジェクト: Artesian/libzmq
int zmq::tcp_address_t::resolve (const char *name_, bool local_, bool ipv4only_)
{
    //  Find the ':' at end that separates address from the port number.
    const char *delimiter = strrchr (name_, ':');
    if (!delimiter) {
        errno = EINVAL;
        return -1;
    }

    //  Separate the address/port.
    std::string addr_str (name_, delimiter - name_);
    std::string port_str (delimiter + 1);

    //  Remove square brackets around the address, if any.
    if (addr_str.size () >= 2 && addr_str [0] == '[' &&
          addr_str [addr_str.size () - 1] == ']')
        addr_str = addr_str.substr (1, addr_str.size () - 2);

    uint16_t port;
    //  Allow 0 specifically, to detect invalid port error in atoi if not
    if (port_str == "*" || port_str == "0")
        //  Resolve wildcard to 0 to allow autoselection of port
        port = 0;
    else {
        //  Parse the port number (0 is not a valid port).
        port = (uint16_t) atoi (port_str.c_str ());
        if (port == 0) {
            errno = EINVAL;
            return -1;
        }
    }

    //  Resolve the IP address.
    int rc;
    if (local_)
        rc = resolve_interface (addr_str.c_str (), ipv4only_);
    else
        rc = resolve_hostname (addr_str.c_str (), ipv4only_);
    if (rc != 0)
        return -1;

    //  Set the port into the address structure.
    if (address.generic.sa_family == AF_INET6)
        address.ipv6.sin6_port = htons (port);
    else
        address.ipv4.sin_port = htons (port);

    return 0;
}
コード例 #7
0
ファイル: llhost.cpp プロジェクト: AlexRa/Kirstens-clone
LLHost::LLHost(const std::string& ip_and_port)
{
	std::string::size_type colon_index = ip_and_port.find(":");
	if (colon_index == std::string::npos)
	{
		mIP = ip_string_to_u32(ip_and_port.c_str());
		mPort = 0;
	}
	else
	{
		std::string ip_str(ip_and_port, 0, colon_index);
		std::string port_str(ip_and_port, colon_index+1);

		mIP = ip_string_to_u32(ip_str.c_str());
		mPort = atol(port_str.c_str());
	}
}
コード例 #8
0
ファイル: udp_engine.cpp プロジェクト: zhouxinlzu/libzmq
int zmq::udp_engine_t::resolve_raw_address (char *name_, size_t length_)
{
    memset (&raw_address, 0, sizeof raw_address);

    const char *delimiter = NULL;

    // Find delimiter, cannot use memrchr as it is not supported on windows
    if (length_ != 0) {
        int chars_left = (int) length_;
        char *current_char = name_ + length_;
        do {
            if (*(--current_char) == ':') {
                delimiter = current_char;
                break;
            }
        } while (--chars_left != 0);
    }

    if (!delimiter) {
        errno = EINVAL;
        return -1;
    }

    std::string addr_str (name_, delimiter - name_);
    std::string port_str (delimiter + 1, name_ + length_ - delimiter - 1);

    //  Parse the port number (0 is not a valid port).
    uint16_t port = (uint16_t) atoi (port_str.c_str ());
    if (port == 0) {
        errno = EINVAL;
        return -1;
    }

    raw_address.sin_family = AF_INET;
    raw_address.sin_port = htons (port);
    raw_address.sin_addr.s_addr = inet_addr (addr_str.c_str ());

    if (raw_address.sin_addr.s_addr == INADDR_NONE) {
        errno = EINVAL;
        return -1;
    }

    return 0;
}
コード例 #9
0
ファイル: tcp_address.cpp プロジェクト: chsticksel/ocamlczmq
int zmq::tcp_address_t::resolve (const char *name_, bool local_, bool ipv6_, bool is_src_)
{
    if (!is_src_) {
        // Test the ';' to know if we have a source address in name_
        const char *src_delimiter = strrchr (name_, ';');
        if (src_delimiter) {
            std::string src_name (name_, src_delimiter - name_);
            const int rc = resolve (src_name.c_str (), local_, ipv6_, true);
            if (rc != 0)
                return -1;
            name_ = src_delimiter + 1;
            _has_src_addr = true;
        }
    }

    //  Find the ':' at end that separates address from the port number.
    const char *delimiter = strrchr (name_, ':');
    if (!delimiter) {
        errno = EINVAL;
        return -1;
    }

    //  Separate the address/port.
    std::string addr_str (name_, delimiter - name_);
    std::string port_str (delimiter + 1);

    //  Remove square brackets around the address, if any, as used in IPv6
    if (addr_str.size () >= 2 && addr_str [0] == '[' &&
          addr_str [addr_str.size () - 1] == ']')
        addr_str = addr_str.substr (1, addr_str.size () - 2);

    // Test the '%' to know if we have an interface name / zone_id in the address
    // Reference: https://tools.ietf.org/html/rfc4007
    std::size_t pos = addr_str.rfind("%");
    uint32_t zone_id = 0;
    if (pos != std::string::npos) {
        std::string if_str = addr_str.substr(pos + 1);
        addr_str = addr_str.substr(0, pos);
        if (isalpha (if_str.at (0)))
            zone_id = if_nametoindex(if_str.c_str());
        else
            zone_id = (uint32_t) atoi (if_str.c_str ());
        if (zone_id == 0) {
            errno = EINVAL;
            return -1;
        }
    }

    //  Allow 0 specifically, to detect invalid port error in atoi if not
    uint16_t port;
    if (port_str == "*" || port_str == "0")
        //  Resolve wildcard to 0 to allow autoselection of port
        port = 0;
    else {
        //  Parse the port number (0 is not a valid port).
        port = (uint16_t) atoi (port_str.c_str ());
        if (port == 0) {
            errno = EINVAL;
            return -1;
        }
    }

    //  Resolve the IP address.
    int rc;
    if (local_ || is_src_)
        rc = resolve_interface (addr_str.c_str (), ipv6_, is_src_);
    else
        rc = resolve_hostname (addr_str.c_str (), ipv6_, is_src_);
    if (rc != 0)
        return -1;

    //  Set the port into the address structure.
    if (is_src_) {
        if (source_address.generic.sa_family == AF_INET6) {
            source_address.ipv6.sin6_port = htons (port);
            source_address.ipv6.sin6_scope_id = zone_id;
        }
        else
            source_address.ipv4.sin_port = htons (port);
    }
    else {
        if (address.generic.sa_family == AF_INET6) {
            address.ipv6.sin6_port = htons (port);
            address.ipv6.sin6_scope_id = zone_id;
        }
        else
            address.ipv4.sin_port = htons (port);
    }

    return 0;
}
コード例 #10
0
ファイル: udp_address.cpp プロジェクト: AmesianX/libzmq
int zmq::udp_address_t::resolve (const char *name_, bool bind_)
{
    //  Find the ':' at end that separates address from the port number.
    const char *delimiter = strrchr (name_, ':');
    if (!delimiter) {
        errno = EINVAL;
        return -1;
    }

    //  Separate the address/port.
    std::string addr_str (name_, delimiter - name_);
    std::string port_str (delimiter + 1);

    //  Parse the port number (0 is not a valid port).
    uint16_t port = (uint16_t) atoi (port_str.c_str ());
    if (port == 0) {
        errno = EINVAL;
        return -1;
    }

    dest_address.sin_family = AF_INET;
    dest_address.sin_port = htons (port);

    //  Only when the udp should bind we allow * as the address
    if (addr_str == "*" && bind_)
        dest_address.sin_addr.s_addr = htonl (INADDR_ANY);
    else
        dest_address.sin_addr.s_addr = inet_addr (addr_str.c_str ());

    if (dest_address.sin_addr.s_addr == INADDR_NONE) {
        errno = EINVAL;
        return -1;
    }

    // we will check only first byte of IP
    // and if it from 224 to 239, then it can
    // represent multicast IP.
    int i = dest_address.sin_addr.s_addr & 0xFF;
    if(i >=  224 && i <= 239) {
        multicast = dest_address.sin_addr;
        is_multicast = true;
    }
    else
        is_multicast = false;

    iface.s_addr = htonl (INADDR_ANY);
    if (iface.s_addr == INADDR_NONE) {
        errno = EINVAL;
        return -1;
    }

    //  If a should bind and not a multicast, the dest address
    //  is actually the bind address
    if (bind_ && !is_multicast)
        bind_address = dest_address;
    else {
        bind_address.sin_family = AF_INET;
        bind_address.sin_port = htons (port);
        bind_address.sin_addr.s_addr = htonl (INADDR_ANY);
    }

    address = name_;

    return 0;
}
コード例 #11
0
//---------------------------------------------------------------------------
void InitUDP(CConceptClient *owner, int port) {
    char                    buffer[0xFF];
    int                     sockfd;
    struct addrinfo         hints;
    struct addrinfo         *res = 0;
    struct sockaddr_storage cliaddr;

    if (!owner)
        return;

    if (owner->RTSOCKET) {
        closesocket(owner->RTSOCKET);
        owner->RTSOCKET = INVALID_SOCKET;
    }

    if (port < 0)
        return;

    memset(&hints, 0, sizeof hints);
    hints.ai_family   = AF_UNSPEC;
    hints.ai_socktype = SOCK_DGRAM;
    hints.ai_protocol = IPPROTO_UDP;

    AnsiString port_str((long)port);
    int        gerr = getaddrinfo(owner->Called_HOST, port_str.c_str(), &hints, &res);
    if ((gerr != 0) || (!res)) {
        AnsiString err((char *)"Error in getaddrinfo(): ");
        err += owner->Called_HOST;
        err += (char *)":";
        err += port_str;
        err += (char *)" ";
        err += (char *)gai_strerror(gerr);
        return;
    }

    if ((res->ai_family != AF_INET) && (res->ai_family != AF_INET6)) {
        freeaddrinfo(res);
        return;
    }

    sockfd = socket(AF_INET, SOCK_DGRAM, 0);
    if (sockfd < 0) {
        freeaddrinfo(res);
        return;
    }

    int flag2 = 0x10;
    setsockopt(sockfd, IPPROTO_IP, IP_TOS, (char *)&flag2, sizeof(int));

    flag2 = 0xFFFF;
    setsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, (char *)&flag2, sizeof(int));

    memcpy(&serveraddr, res->ai_addr, res->ai_addrlen);
    server_len = res->ai_addrlen;
    socklen_t cllen = sizeof(cliaddr);
    if (sendto(sockfd, "1", 1, 0, (struct sockaddr *)&serveraddr, server_len) == 1) {
        if (sock_eof_timeout(sockfd, 500) == 0) {
            if (recvfrom(sockfd, buffer, 1, 0, (struct sockaddr *)&cliaddr, &cllen) == 1) {
                sendto(sockfd, "2", 1, 0, (struct sockaddr *)&serveraddr, server_len);
                owner->RTSOCKET = sockfd;
            }
        }
    }
    freeaddrinfo(res);
}