device: renew dhcp leases on awake for software devices
[NetworkManager.git] / libnm-core / nm-property-compare.c
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2
3 /*
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301 USA.
18  *
19  * Copyright 2007 - 2014 Red Hat, Inc.
20  * Copyright 2007 - 2008 Novell, Inc.
21  */
22
23 #include "nm-default.h"
24
25 #include <string.h>
26 #include <math.h>
27 #include <netinet/in.h>
28
29 #include "nm-property-compare.h"
30
31 static gint
32 _nm_property_compare_collection (GVariant *value1, GVariant *value2)
33 {
34         GVariant *child1, *child2;
35         int i, len1, len2;
36         int ret;
37
38         len1 = g_variant_n_children (value1);
39         len2 = g_variant_n_children (value2);
40
41         if (len1 != len2)
42                 return len1 < len2 ? -1 : len1 > len2;
43
44         for (i = 0; i < len1; i++) {
45                 child1 = g_variant_get_child_value (value1, i);
46                 child2 = g_variant_get_child_value (value2, i);
47
48                 ret = nm_property_compare (child1, child2);
49                 g_variant_unref (child1);
50                 g_variant_unref (child2);
51
52                 if (ret)
53                         return ret;
54         }
55
56         return 0;
57 }
58
59 static gint
60 _nm_property_compare_strdict (GVariant *value1, GVariant *value2)
61 {
62         GVariantIter iter;
63         int len1, len2;
64         const char *key, *val1, *val2;
65         int ret;
66
67         len1 = g_variant_n_children (value1);
68         len2 = g_variant_n_children (value2);
69
70         if (len1 != len2)
71                 return len1 < len2 ? -1 : len1 > len2;
72
73         g_variant_iter_init (&iter, value1);
74         while (g_variant_iter_next (&iter, "{&s&s}", &key, &val1)) {
75                 if (!g_variant_lookup (value2, key, "&s", &val2))
76                         return -1;
77
78                 ret = strcmp (val1, val2);
79                 if (ret)
80                         return ret;
81         }
82
83         return 0;
84 }
85
86 int
87 nm_property_compare (GVariant *value1, GVariant *value2)
88 {
89         const GVariantType *type1;
90         const GVariantType *type2;
91         gint ret;
92
93         if (value1 == value2)
94                 return 0;
95         if (!value1)
96                 return 1;
97         if (!value2)
98                 return -1;
99
100         type1 = g_variant_get_type (value1);
101         type2 = g_variant_get_type (value2);
102
103         if (!g_variant_type_equal (type1, type2))
104                 return type1 < type2 ? -1 : type1 > type2;
105
106         if (g_variant_type_is_basic (type1))
107                 ret = g_variant_compare (value1, value2);
108         else if (g_variant_is_of_type (value1, G_VARIANT_TYPE ("a{ss}")))
109                 ret = _nm_property_compare_strdict (value1, value2);
110         else if (g_variant_type_is_array (type1))
111                 ret = _nm_property_compare_collection (value1, value2);
112         else if (g_variant_type_is_tuple (type1))
113                 ret = _nm_property_compare_collection (value1, value2);
114         else {
115                 g_warning ("Don't know how to compare variant type '%s'", (const char *) type1);
116                 ret = value1 == value2;
117         }
118
119         return ret;
120 }