libwebsockets
Lightweight C library for HTML5 websockets
|
Wifi devices may face some interception of their connection to the internet, it's very common for, eg, coffee shop wifi to present some kind of login or other clickthrough before access to the Internet is granted. Devices may need to understand that they are in this situation, and there are several different techniques for trying to gague it.
Sequence-wise the device has been granted a DHCP lease and has been configured with DNS, but the DNS may be wrongly resolving everything to an address on the LAN or a portal on the net.
Whether there is a captive portal active should be a sticky state for a given connection if there is not going to be any attempt to login or pass the landing page, it only needs checking for after DHCP acquisition then. If there will be an attempt to satisfy the landing page, the test should be repeated after the attempt.
The most popular detection scheme by numbers is Android's method, which is to make an HTTP client GET to http://connectivitycheck.android.com/generate_204
and see if a 204 is coming back... if intercepted, typically there'll be a 3xx redirect to the portal, perhaps on https. Or, it may reply on http with a 200 and show the portal directly... either way it won't deliver a 204 like the real remote server does.
Variations include expecting a 200 but with specific http body content, and doing a DNS lookup for a static IP that the device knows; if it's resolved to something else, it knows there's monkey business implying a captive portal.
Other schemes involve https connections going out and detecting that the cert of the server it's actually talking to doesn't check out, although this is potentially ambiguous.
Yet more methods are possible outside of tcp or http.
lws provides a generic api to start captive portal detection...
and two states in lws_system
states to trigger it from, either LWS_SYSTATE_CPD_PRE_TIME
which happens after DHCP acquisition but before ntpclient and is suitable for non https-based scheme where the time doesn't need to be known, or the alternative LWS_SYSTATE_CPD_POST_TIME
state which happens after ntpclient has completed and we know the time.
The actual platform implementation is set using lws_system_ops_t
function pointer captive_portal_detect_request
, ie
User platform code can provide this to implement whatever scheme they want, when it has arrived at a result, it can call the lws api lws_system_cpd_result()
to inform lws. If there isn't any captive portal, this will also try to advance the system state towards OPERATIONAL.