On 17.06.2017 11:36, Tim Rühsen wrote: > On Samstag, 17. Juni 2017 13:53:06 CEST Didik Setiawan wrote: >> On Tue, Jun 13, 2017 at 06:37:27PM +0200, Darshit Shah wrote: >>>> - http_server_port still hardcoded. >>> >>> This is important. The port should be a random number. Usually, passing 0 >>> in the port number makes the kernel choose an open one for you. >>> >>> Having a randomised port is important to ensure that multiple runs don't >>> step on each other. >> >> I still don't know how to accomplish this. Maybe, it just my understanding >> that when I pass 0 in MHD port number, the result is still 0. >> Another approach, when I look into the old code, it generate port number by >> calling wget_tcp_get_local_port(). But, I need to call wget_tcp_init() and >> wget_tcp_listen() respectively in order to get proper result. >> Conclusion, do I need to use existing wget_tcp_get_local_port() to get the >> port, or maybe there is a function in MHD to do that? > > All you need is the socket descriptor. How to call getsockname() + > getnameinfo() to retrieve the port number you see in libwget/net.c/ > wget_tcp_get_local_port(). > > If MHD doesn't have such a function, either try to get the socket descriptor > or extend MHD with a small function (similar code as in > wget_tcp_get_local_port).
There are several ways to implement it.
First one:
Initialise socket externally and start listen() on it. You can use any
bind and port detection function.
Pass socket FD to MHD_start_daemon() by MHD_OPTION_LISTEN_SOCKET.
Second one:
Use MHD_start_daemon() with "0" as port number then use
MHD_get_daemon_info() with MHD_DAEMON_INFO_LISTEN_FD to get listen
socket FD. Use any port detection.
Third one:
Implemented two day ago, works with MHD_VERSION >= 0x00095501 and on
platforms that support getsockname().
Use MHD_start_daemon() with "0" as port number then use
MHD_get_daemon_info() with MHD_DAEMON_INFO_BIND_PORT to get port number.
You can combine second and third methods:
-----------------------------------------------
int port_num;
if(0) {}
#if MHD_VERSION >= 0x00095501
else if (MHD_NO !=
MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT))
{
const union MHD_DaemonInfo *dinfo;
dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT);
if (NULL == dinfo || 0 == dinfo->port)
{
/* Insert code to handle error. */
return -1;
}
port_num = (int)dinfo->port;
}
#endif /* MHD_VERSION >= 0x00095501 */
else
{
const union MHD_DaemonInfo *dinfo;
MHD_socket sock_fd;
dinfo = MHD_get_daemon_info (d, MHD_OPTION_LISTEN_SOCKET);
if (NULL == dinfo)
{
/* Insert code to handle error. */
return -1;
}
sock_fd = dinfo->listen_fd;
/* Insert code to use port detection on provided socket. */
}
-----------------------------------------------
Note that socket type used by MHD functions is 'MHD_socket'.
MHD_socket is 'int' on POSIX platforms (Linux, *BSD, Unix, Darwin) and
'SOCKET' on Windows platform (excluding Cygwin, where MHD_socket is 'int').
--
Best Wishes,
Evgeny Grin
signature.asc
Description: OpenPGP digital signature
