Skip to content

use real ip instead of url in lws_client_connect_via_info? #3508

@bethebest0622

Description

@bethebest0622

Host is ws.bitget.com, I fill the connection info as follows

std::string url = "ws.bitget.com";
struct lws_client_connect_info ccinfo = {0};
ccinfo.host = url.data();
ccinfo.address = url.data();
ccinfo.origin = "origin";
ccinfo.ssl_connection = LCCSCF_USE_SSL | LCCSCF_ALLOW_SELFSIGNED | LCCSCF_SKIP_SERVER_CERT_HOSTNAME_CHECK;

This is ok to run.

But then i want to connect to fixed ip of this url.

one of ip is 18.65.214.78.

When i replace the original code with this:

std::string url = "ws.bitget.com";
std::string ip = "18.65.214.78";
struct lws_client_connect_info ccinfo = {0};
ccinfo.host = url.data();
ccinfo.address = ip.data();
ccinfo.origin = "origin";
ccinfo.ssl_connection = LCCSCF_USE_SSL | LCCSCF_ALLOW_SELFSIGNED | LCCSCF_SKIP_SERVER_CERT_HOSTNAME_CHECK;

Then the connection failed to build.

How to connect to a fixed ip?

Could you help on this?

ps. There are many real ips behind the host, I use this code to find all fo these ips:

std::vector<std::string> GetIps(const std::string &host) {
  std::vector<std::string> ips;

  addrinfo hints{};
  hints.ai_family = AF_INET;
  hints.ai_socktype = SOCK_STREAM;

  addrinfo *res = nullptr;
  int err = getaddrinfo(host.c_str(), nullptr, &hints, &res);
  if (err != 0) {
    std::cerr << "getaddrinfo error: " << gai_strerror(err) << std::endl;
    return ips;
  }

  for (addrinfo *p = res; p != nullptr; p = p->ai_next) {
    char ipstr[INET_ADDRSTRLEN];
    sockaddr_in *ipv4 = (sockaddr_in *)p->ai_addr;
    inet_ntop(AF_INET, &(ipv4->sin_addr), ipstr, sizeof(ipstr));
    ips.push_back(ipstr);
  }

  freeaddrinfo(res);
  return ips;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions