libmicrohttpd
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[libmicrohttpd] MHD_CONNECTION_INFO_CLIENT_ADDRESS results don't include


From: Bruce Eng
Subject: [libmicrohttpd] MHD_CONNECTION_INFO_CLIENT_ADDRESS results don't include port
Date: Fri, 10 Dec 2021 18:38:29 -0600

Not sure emailing here is the right way to get help/info...

If it is, I am using libmicrohttpd 0.9.73 built with MinGW-w64 i686 and I am a bit lost on what results MHD_CONNECTION_INFO_CLIENT_ADDRESS is returning. Here is my testing so far:

const MHD_ConnectionInfo* info = MHD_get_connection_info(connection, MHD_CONNECTION_INFO_CLIENT_ADDRESS);
  const sockaddr* so_client = (info ? info->client_addr : NULL);

  //something is wrong with the port...
  //search for "s = accept (fd,". This is where the address structure is first set.
  if (so_client->sa_family == AF_INET) {
    std::cout << "IP: " << (int)((unsigned char)so_client->sa_data[0]) << ",";
    std::cout << (int)((unsigned char)so_client->sa_data[1]) << ",";
    std::cout << (int)((unsigned char)so_client->sa_data[2]) << ",";
    std::cout << (int)((unsigned char)so_client->sa_data[3]) << ",";
    std::cout << (int)((unsigned char)so_client->sa_data[4]) << ",";
    std::cout << (int)((unsigned char)so_client->sa_data[5]) << ",";
    std::cout << (int)((unsigned char)so_client->sa_data[6]) << ",";
    std::cout << (int)((unsigned char)so_client->sa_data[7]) << "\n";
  }

As expected, elements 2,3,4,5 = "127,0,0,1" when connecting from localhost, but elements 0 and 1 appear sort of random. They are the same for each connection, but when I restart my program and reconnect, they are different. Also if I connect with different hostnames like "localhost" vs "127.0.0.1" they change. All the documentation I found seemed to suggest that if the sa_family was AF_INET that these would give the port.

Additionally, if I do this:

const MHD_ConnectionInfo* info2 = MHD_get_connection_info (connection, MHD_CONNECTION_INFO_CONNECTION_FD);
  struct sockaddr_in sin;
  int addrlen = sizeof(sin);
  if(getsockname(info2->connect_fd, (struct sockaddr *)&sin, &addrlen) == 0 &&
    sin.sin_family == AF_INET &&
    addrlen == sizeof(sin))
  {
      int local_port = ntohs(sin.sin_port);
      std::cout << "Port: " << local_port << "|" << (void*)sin.sin_port << "\n";
  }

I am able to retrieve the correct port so getsockname seems to correctly fill out the port portion of a sockaddr structure. 

So my question is: what is sa_data[0] and sa_data[1] when using MHD_CONNECTION_INFO_CLIENT_ADDRESS?

Thanks,

Bruce

reply via email to

[Prev in Thread] Current Thread [Next in Thread]