Re: [Bug-wget] [GSoC Update] Week 2
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). Regards, Tim signature.asc Description: This is a digitally signed message part.
Re: [Bug-wget] [GSoC Update] Week 2
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
Re: [Bug-wget] [GSoC Update] Week 2
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
Re: [Bug-wget] [GSoC Update] Week 2
On 17.06.2017 22:22, Didik Setiawan wrote: > On Sat, Jun 17, 2017 at 01:30:57PM +0300, Evgeny Grin wrote: >> > >> 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. */ >> } >> --- Right, correct value is MHD_DAEMON_INFO_LISTEN_FD. -- Wishes, Evgeny signature.asc Description: OpenPGP digital signature