On Sat, Jun 17, 2017 at 01:30:57PM +0300, Evgeny Grin wrote: > > On 17.06.2017 11:36, Tim Rühsen wrote: > > > 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.
Thanks for the detailed explanations. That's what I'm looking for, to fill the missing piece. > You can combine second and third methods: I use this combination. > ----------------------------------------------- > 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); Don't you mean: dinfo = MHD_get_daemon_info(d, MHD_DAEMON_INFO_LISTEN_FD); Because when I tried using MHD version < 0.9.55 the result is not as expected. > 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 > Regards, Didik Setiawan