Commit graph

79 commits

Author SHA1 Message Date
Thomas Haller
2012b49219 connectivity/trivial: move code
nm_connectivity_state_to_string() is entirely independent of the GObject implementation
of NMConnectivity. Move it to the beginning of the source file. It will be useful next
because we will *always* build "nm-connectivity.c" source file, but disable various
parts with #if. Hence, move the part that should always be build to the top.
2018-03-19 14:46:55 +01:00
Thomas Haller
89ccfa8dde connectivity: fix evaluating @what argument of CURLMOPT_SOCKETFUNCTION
It's not a bitfields, it's documented to be an enum with 4
possible values.
2018-03-19 14:46:55 +01:00
Thomas Haller
bfe60cb7b9 connectivity: fix timeout handling for curl
g_timeout_add() expects the timeout in milliseconds, not seconds.

Reported-by: Serban Iorga <serban300@gmail.com>

https://bugzilla.gnome.org/show_bug.cgi?id=794464

Fixes: 7307dea9c4
2018-03-19 14:46:23 +01:00
Thomas Haller
265ae98fef core/trivial: rename internal structure 2018-03-19 14:39:09 +01:00
Thomas Haller
1bba43432a core/trivial: move code 2018-03-19 14:39:09 +01:00
Beniamino Galvani
43960d4b15 connectivity: fix wrong memory access
Don't use message data after calling curl_multi_remove_handle(). Fixes
the following asan error:

=================================================================
==13238==ERROR: AddressSanitizer: heap-use-after-free on address 0x608000091ad0 at pc 0x55750f8d9a10 bp 0x7ffeb7f5f210 sp 0x7ffeb7f5f200
READ of size 8 at 0x608000091ad0 thread T0
    #0 0x55750f8d9a0f in curl_check_connectivity (/usr/sbin/NetworkManager+0x190a0f)
    #1 0x55750f8da7dd in curl_socketevent_cb (/usr/sbin/NetworkManager+0x1917dd)
    #2 0x7f73cb64e8f8 in g_main_context_dispatch (/lib64/libglib-2.0.so.0+0x4a8f8)
    #3 0x7f73cb64ec57  (/lib64/libglib-2.0.so.0+0x4ac57)
    #4 0x7f73cb64ef29 in g_main_loop_run (/lib64/libglib-2.0.so.0+0x4af29)
    #5 0x55750f85c3f4  (/usr/sbin/NetworkManager+0x1133f4)
    #6 0x7f73c9f19384 in __libc_start_main (/lib64/libc.so.6+0x22384)
    #7 0x55750f85d7f7  (/usr/sbin/NetworkManager+0x1147f7)

0x608000091ad0 is located 48 bytes inside of 88-byte region [0x608000091aa0,0x608000091af8)
freed by thread T0 here:
    #0 0x7f73cd61f508 in __interceptor_free (/lib64/libasan.so.4+0xde508)
    #1 0x7f73ca710eaa in curl_multi_remove_handle (/lib64/libcurl.so.4+0x32eaa)

previously allocated by thread T0 here:
    #0 0x7f73cd61fa88 in __interceptor_calloc (/lib64/libasan.so.4+0xdea88)
    #1 0x7f73ca710b3d in curl_multi_add_handle (/lib64/libcurl.so.4+0x32b3d)

SUMMARY: AddressSanitizer: heap-use-after-free (/usr/sbin/NetworkManager+0x190a0f)
2018-02-15 15:34:03 +01:00
Aleksander Morgado
a25d2e0a17 connectivity: fix portal detection when using HTTP 204 based checks
If we're going to use a 'no content' URL (HTTP 204) to check
connectivity, do not try to match prefix when the content is being
received. This issue was making the check not work properly, as the content
returned by the captive portal was assumed as expected (given that
g_str_has_prefix(str,"") always returns TRUE!).

Also, rework a log message that was being emitted on portal detection
to avoid specifying that the reason is the content being shorter than
expected, as that same logic now applies to the case where too much
content is received and none was expected.

Fixes: 88416394f8

https://mail.gnome.org/archives/networkmanager-list/2018-February/msg00009.html
2018-02-12 19:41:38 +01:00
Thomas Haller
5f1c2e16c9 connectivity: cleanup conditions in curl_check_connectivity()
Refactor the nested ifs to if-else-if-else.
2018-02-09 21:17:54 +01:00
Aleksander Morgado
88416394f8 connectivity: allow 204 (no content) as connectivity test
If the user is requesting an empty response ("") as expected string,
let the connectivity check succeed if we actually get a 204 HTTP
response code (reporting a successful request but without content).

This allows using e.g. Android's default URLs for the connectivity
check purpose:

    [connectivity]
    uri=http://google.com/generate_204
    interval=60
    response=

https://mail.gnome.org/archives/networkmanager-list/2018-February/msg00005.html
2018-02-09 21:17:54 +01:00
Beniamino Galvani
92e8ec5e1e connectivity: fix memory leak
The server response, allocated in easy_write_cb(), was not freed.

Fixes: 7307dea9c4
2017-10-10 10:12:42 +02:00
James Henstridge
9a58ee0705 config: add an API to disable connectivity check via internal config file.
https://bugzilla.gnome.org/show_bug.cgi?id=785117
2017-08-17 22:31:47 +02:00
Beniamino Galvani
7204472de5 connectivity: fix memory leak
Fixes: 9d43869e47
2017-07-19 22:14:05 +02:00
Thomas Haller
f1eb1619f1 connectivity: fix scheduling periodic connectivity checks
commit a955639 (connectivity: don't do periodic checks on interval=0)
broke scheduling connectivity checks.

That is because the timer is on only scheduled if
nm_connectivity_check_enabled(), which in turn only returns TRUE
if curl_mhandle is set. However, nm_connectivity_init() would only
initialize curl_mhandle after update_config(), missing to schedule
the periodic task.

https://mail.gnome.org/archives/networkmanager-list/2017-May/msg00076.html

Fixes: a95563996f
2017-06-02 19:15:30 +02:00
Thomas Haller
7f8815a9c3 connectivity: avoid compiler warning for argument of curl_easy_getinfo()
libcurl employs some typechecking via "curl/typecheck-gcc.h". When
compling with --enable-lto, compilation fails otherwise with:

    make[2]: Entering directory '/data/src/NetworkManager'
      CC       src/src_libNetworkManager_la-nm-connectivity.lo
      CCLD     src/libNetworkManager.la
      CCLD     src/libNetworkManagerTest.la
      CCLD     src/dhcp/tests/test-dhcp-dhclient
    src/nm-connectivity.c: In function 'curl_check_connectivity':
    src/nm-connectivity.c:147:10: error: call to '_curl_easy_getinfo_err_string' declared with attribute warning: curl_easy_getinfo expects a pointer to char * for this info [-Werror]
       eret = curl_easy_getinfo (msg->easy_handle, CURLINFO_PRIVATE, &cb_data);
              ^
    lto1: all warnings being treated as errors
    lto-wrapper: fatal error: /usr/bin/gcc returned 1 exit status
    compilation terminated.
    /usr/bin/ld: error: lto-wrapper failed
2017-05-12 09:55:46 +02:00
Lubomir Rintel
a95563996f connectivity: don't do periodic checks on interval=0
https://bugzilla.redhat.com/show_bug.cgi?id=1449296

Fixes: 7307dea9c4
2017-05-10 13:17:31 +02:00
Francesco Giudici
7a2c31a54a connectivity: fix typo in error message 2017-05-05 12:39:43 +02:00
Francesco Giudici
2524a6f852 device: add default route penalty only if concheck is enabled
If we don't have connection checking functionality just avoid adding
a penalty to the defaut route of newly activated connections.
2017-05-04 11:07:40 +02:00
Lubomir Rintel
9d43869e47 core: make connectivity checking per-device
This moves tracking of connectivity to NMDevice and makes the NMManager
negotiate the best of known connectivity states of devices. The NMConnectivity
singleton handles its own configuration and scheduling of the permission
checks, but otherwise greatly simplifies it.

This will be useful to determine correct metrics for multiple default routes
depending on actual internet connectivity.

The per-device connection checks is not yet exposed on the D-Bus, since they
probably should be per-address-family as well.
2017-03-28 15:26:47 +02:00
Thomas Haller
9e4f3655f0 connectivity: remove verbose trace logging 2017-03-23 12:08:05 +01:00
Thomas Haller
3ac07f381e connectivity: fix clearing timer-id in curl_timeout_cb()
Fixes: 7307dea9c4
2017-03-22 21:00:24 +01:00
Lubomir Rintel
08cc81d450 connectivity: fix the connectivity check timeout
CURLOPT_CONNECTTIMEOUT or CURLOPT_TIMEOUT only make sense if libcurl is
handling the I/O loop (the "easy" interface); we need to implement our
own timeout.
2017-03-22 19:06:41 +00:00
Lubomir Rintel
d7e470b0aa connectivity: conclude the check as soon as we see enough bytes
No need to read the full response into memory.
2017-03-22 18:52:56 +00:00
Lubomir Rintel
ac0f454cfb connectivity: conclude the check as soon as we see the magic header
No need to read the rest of the reponse.
2017-03-22 18:52:56 +00:00
Lubomir Rintel
71b8d16eb8 connectivity: split out the finish of the connectivity checking
Factor out the conclusion of the connectivity check. This will allow us
to finish the connectivity check on other occassions than a successful
connection end. Most importantly on timeouts; but it will also allow us
to short-circuit the check when we conclude it without reading the full
response.
2017-03-22 18:52:56 +00:00
Lubomir Rintel
6bc3ada0ec connectivity: cosmetic fixes 2017-03-22 18:52:50 +00:00
Francesco Giudici
7307dea9c4 connectivity: switch connectivity checking to libcurl
[lkundrak@v3.sk: removed libsoup altogether, implemented TODOs and fixed
the poll condition handling]

Co-authored-by: Lubomir Rintel <lkundrak@v3.sk>
2017-03-22 12:09:39 +01:00
Thomas Haller
8987de7cc0 core/dispatcher: cleanup nm_dispatcher_call_connectivity()
Remove the redundant action argument. It is clear, that
nm_dispatcher_call_connectivity() is called with action
NM_DISPATCHER_ACTION_CONNECTIVITY_CHANGE.

On the other hand, add the async callbacks. Altough they are
not used at the moment, it seems more correct that an async
API has a callback and a call-id to cancel the invocation.
2017-03-16 18:27:33 +01:00
Thomas Haller
2b72cc2693 core/trivial: give names in src/nm-dispatcher.h header an "NM" prefix
Stuff defined in header files should have an NM prefix, although
this is a project-internal header.

Rename.
2017-03-16 18:27:33 +01:00
Beniamino Galvani
92a8cfac69 core: introduce default logging macros 2016-10-14 15:57:43 +02:00
Thomas Haller
4d37f7a1e9 core: refactor private data in "src"
- use _NM_GET_PRIVATE() and _NM_GET_PRIVATE_PTR() everywhere.

- reorder statements, to have GObject related functions (init, dispose,
  constructed) at the bottom of each file and in a consistent order w.r.t.
  each other.

- unify whitespaces in signal and properties declarations.

- use NM_GOBJECT_PROPERTIES_DEFINE() and _notify()

- drop unused signal slots in class structures

- drop unused header files for device factories
2016-10-04 09:50:56 +02:00
Thomas Haller
a83eb773ce all: modify line separator comments to be 80 chars wide
sed 's#^/\*\{5\}\*\+/$#/*****************************************************************************/#' $(git grep -l '\*\{5\}' | grep '\.[hc]$') -i
2016-10-03 12:01:15 +02:00
Mario Sanchez Prada
dfd9d85beb nm-dispatcher: Added new 'connectivity-state' parameter to private D-Bus API
In order to pass the connectivity state to the relevant hooks along with
the event itself, we need to add this parameter for the 'Action' method
of then internal 'org.freedesktop.nm_dispatcher' interface, which will
be sent by the network manager main process to the dispatcher.

https://bugzilla.gnome.org/show_bug.cgi?id=768969
2016-07-28 22:22:14 +02:00
Mario Sanchez Prada
283562ef18 nm-dispatcher: Added new action for 'connectivity-change' events
The purpose of this action is to provide a hook for the OS to act
on global changes regarding to connectivity, which can currently
be one of 'none', 'portal', 'limited', 'full' or 'unknown'.

Check the documentation for a more information on each of those states:
https://developer.gnome.org/NetworkManager/stable/nm-dbus-types.html#NMConnectivityState

https://bugzilla.gnome.org/show_bug.cgi?id=768969
2016-07-28 22:22:14 +02:00
Thomas Haller
8bace23beb all: cleanup includes and let "nm-default.h" include "config.h"
- All internal source files (except "examples", which are not internal)
  should include "config.h" first. As also all internal source
  files should include "nm-default.h", let "config.h" be included
  by "nm-default.h" and include "nm-default.h" as first in every
  source file.
  We already wanted to include "nm-default.h" before other headers
  because it might contains some fixes (like "nm-glib.h" compatibility)
  that is required first.

- After including "nm-default.h", we optinally allow for including the
  corresponding header file for the source file at hand. The idea
  is to ensure that each header file is self contained.

- Don't include "config.h" or "nm-default.h" in any header file
  (except "nm-sd-adapt.h"). Public headers anyway must not include
  these headers, and internal headers are never included after
  "nm-default.h", as of the first previous point.

- Include all internal headers with quotes instead of angle brackets.
  In practice it doesn't matter, because in our public headers we must
  include other headers with angle brackets. As we use our public
  headers also to compile our interal source files, effectively the
  result must be the same. Still do it for consistency.

- Except for <config.h> itself. Include it with angle brackets as suggested by
  https://www.gnu.org/software/autoconf/manual/autoconf.html#Configuration-Headers
2016-02-19 17:53:25 +01:00
Thomas Haller
f2879e1ba2 connectivity: implement nm_connectivity_state_to_string() as NM_UTILS_LOOKUP_STR_DEFINE_WARN() 2016-02-01 14:52:55 +01:00
Thomas Haller
2bf4960ec1 connectivity: fix calling parent dispose() 2016-02-01 13:24:52 +01:00
Beniamino Galvani
fbd3286955 core,libnm: use nm_clear_g_source() where possible
Replacement was done with commands:

spatch --sp-file nm_clear_g_source.cocci --in-place --smpl-spacing --dir src
spatch --sp-file nm_clear_g_source.cocci --in-place --smpl-spacing --dir libnm

where nm_clear_g_source.cocci contains:

@@
expression e;
@@
- if (e) {
-    g_source_remove (e);
-    e = 0;
- }
+ nm_clear_g_source (&e);
2016-01-06 21:25:55 +01:00
Thomas Haller
ad7cdfc766 logging: declare default logging macros in "nm-logging.h"
The logging macros _LOGD(), etc. are specific to each
file as they format the message according to their context.

Still, they were cumbersome to define and their implementation
was repeated over and over (slightly different at times).

Move the declaration of these macros to "nm-logging.h".
The source file now only needs to define _NMLOG(), and either
_NMLOG_ENABLED() or _NMLOG_DOMAIN.

This reduces code duplication and encourages a common implementation
and usage of these macros.
2015-08-20 11:15:13 +02:00
Thomas Haller
19c3ea948a all: make use of new header file "nm-default.h" 2015-08-05 15:32:40 +02:00
Dan Winship
3452ee2a0e all: rename nm-glib-compat.h to nm-glib.h, use everywhere
Rather than randomly including one or more of <glib.h>,
<glib-object.h>, and <gio/gio.h> everywhere (and forgetting to include
"nm-glib-compat.h" most of the time), rename nm-glib-compat.h to
nm-glib.h, include <gio/gio.h> from there, and then change all .c
files in NM to include "nm-glib.h" rather than including the glib
headers directly.

(Public headers files still have to include the real glib headers,
since nm-glib.h isn't installed...)

Also, remove glib includes from header files that are already
including a base object header file (which must itself already include
the glib headers).
2015-07-24 13:25:47 -04:00
Thomas Haller
eab32a5252 connectivity: log warning when using https:// URI
https://bugzilla.gnome.org/show_bug.cgi?id=747866
2015-07-16 17:08:55 +02:00
Dan Williams
6a81daf1cb connectivity: explicitly check for 511/Network Authentication Required (RFC6585) (bgo #670394)
If the response affirmatively indicates you're behind a portal, we might as well
use that information.

https://bugzilla.gnome.org/show_bug.cgi?id=670394
2015-07-16 10:04:44 -05:00
Thomas Haller
27bd7dc938 config: change examples for command line arguments to system default 2015-07-02 15:50:03 +02:00
Lubomir Rintel
eb2b8c2798 build: use compat version of g_clear_pointer()
Ubuntu 12.04 has an ancient version of glib, which we nevertheless support.
2015-06-02 12:30:03 +02:00
Thomas Haller
652853e0d0 connectivity: use default on missing connectivity.interval config
Manual page claims that a missing configuration option for connectivity
interval means "300". That was not the case for a long time (never?).

https://bugzilla.gnome.org/show_bug.cgi?id=723350

Based-on-patch-by: Mathieu Trudel-Lapierre <mathieu.trudel-lapierre@canonical.com>
2015-05-19 15:34:41 +02:00
Dan Williams
90692e3eff connectivity: disable HTTP keepalive for connectivity checks
There won't be any further requests, so there's no point in keeping
the connection alive.  Even if the HTTP server doesn't care, proxy
servers in-between might keep the connection open for a couple seconds
for keepalive, and we might as well be nice to them and tell them we
don't need to keep it alive.
2015-03-16 10:45:25 -05:00
Lubomir Rintel
e5499c558f connectivity: memleak: drop async result reference on complete
==5177== 104 (+104) bytes in 1 (+1) blocks are definitely lost in loss record 5,502 of 6,581
==5177==    at 0x4C29BCF: malloc (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==5177==    by 0x7F4A6F5: g_malloc (gmem.c:97)
==5177==    by 0x7F6159F: g_slice_alloc (gslice.c:1007)
==5177==    by 0x7F61B6D: g_slice_alloc0 (gslice.c:1032)
==5177==    by 0x7CDB9A3: g_type_create_instance (gtype.c:1847)
==5177==    by 0x7CBF356: g_object_new_internal (gobject.c:1774)
==5177==    by 0x7CC0D4C: g_object_newv (gobject.c:1922)
==5177==    by 0x7CC14E3: g_object_new (gobject.c:1614)
==5177==    by 0x79A4C57: g_simple_async_result_new (gsimpleasyncresult.c:319)
==5177==    by 0x515B34: nm_connectivity_check_async (nm-connectivity.c:289)
==5177==    by 0x515D74: run_check (nm-connectivity.c:217)
==5177==    by 0x515DBF: idle_start_periodic_checks (nm-connectivity.c:229)
2015-02-18 18:10:47 +01:00
Thomas Haller
ac9dd4c832 connectivity: make NMConnectivity independent of NMConfig 2015-02-03 13:01:53 +01:00
Thomas Haller
50fce5a860 config: use NMConfigData in NMConfig 2015-02-03 13:01:53 +01:00
Thomas Haller
3ed4aa271a connectivity: add logging macros to nm-connectivity.c
They add a common prefix to all logging lines.
2015-02-03 13:01:52 +01:00