libmicrohttpd
[Top][All Lists]
Advanced

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

Re: [libmicrohttpd] MHD_CONNECTION_INFO_CLIENT_ADDRESS results don't inc


From: Evgeny Grin
Subject: Re: [libmicrohttpd] MHD_CONNECTION_INFO_CLIENT_ADDRESS results don't include port
Date: Sat, 11 Dec 2021 13:31:05 +0300
User-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Thunderbird/91.3.2

Hi Bruce,

First of all, you should not read 'struct sockaddr' directly except 'sa_family' member. The idea of 'struct sockaddr' is to be abstract structure "large enough" to hold any specific protocol information. Once you find that 'sa_family' is 'AF_INET' you should cast your 'so_client' pointer to 'struct sockaddr_in*' and then read the data. 'sa_data' form 'struct sockaddr' is opaque data, not designed to be read directly.
You can easily find more information in sockets API documentation.

When you connect the client to the server, you have two sides: the server side and the client side. If both client and server are located on the same machine, you may have the same IP address for both of them, but ports will be different. Unless you 'bind()' your client socket to some specific port, the client port will be chosen automatically from freely available ports. The 'MHD_CONNECTION_INFO_CLIENT_ADDRESS' gives you an information about the *client* side. If the client is remote, you will see remote address here. The 'MHD_CONNECTION_INFO_CONNECTION_FD' gives you local socket. If you use 'getsockname()' on it, you will get your local IP address (and port). If you would use 'getpeername()' on the same socket, you will get IP address (and port) of the client.

--
Evgeny


On 11.12.2021 3:38, Bruce Eng wrote:
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

Attachment: OpenPGP_signature
Description: OpenPGP digital signature


reply via email to

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