First step towards a generic IP configuration infrastructure
[platform/upstream/connman.git] / src / ipconfig.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2009  Intel Corporation. All rights reserved.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 as
9  *  published by the Free Software Foundation.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include "connman.h"
27
28 struct connman_ipconfig {
29         gint refcount;
30         enum connman_ipconfig_method method;
31 };
32
33 /**
34  * connman_ipconfig_create:
35  *
36  * Allocate a new ipconfig structure.
37  *
38  * Returns: a newly-allocated #connman_ipconfig structure
39  */
40 struct connman_ipconfig *connman_ipconfig_create(void)
41 {
42         struct connman_ipconfig *ipconfig;
43
44         DBG("");
45
46         ipconfig = g_try_new0(struct connman_ipconfig, 1);
47         if (ipconfig == NULL)
48                 return NULL;
49
50         DBG("ipconfig %p", ipconfig);
51
52         return ipconfig;
53 }
54
55 /**
56  * connman_ipconfig_ref:
57  * @ipconfig: ipconfig structure
58  *
59  * Increase reference counter of ipconfig
60  */
61 struct connman_ipconfig *connman_ipconfig_ref(struct connman_ipconfig *ipconfig)
62 {
63         g_atomic_int_inc(&ipconfig->refcount);
64
65         return ipconfig;
66 }
67
68 /**
69  * connman_ipconfig_unref:
70  * @ipconfig: ipconfig structure
71  *
72  * Decrease reference counter of ipconfig
73  */
74 void connman_ipconfig_unref(struct connman_ipconfig *ipconfig)
75 {
76         if (g_atomic_int_dec_and_test(&ipconfig->refcount) == TRUE) {
77                 g_free(ipconfig);
78         }
79 }
80
81 static GSList *driver_list = NULL;
82
83 static gint compare_priority(gconstpointer a, gconstpointer b)
84 {
85         const struct connman_ipconfig_driver *driver1 = a;
86         const struct connman_ipconfig_driver *driver2 = b;
87
88         return driver2->priority - driver1->priority;
89 }
90
91 /**
92  * connman_ipconfig_driver_register:
93  * @driver: IP configuration driver
94  *
95  * Register a new IP configuration driver
96  *
97  * Returns: %0 on success
98  */
99 int connman_ipconfig_driver_register(struct connman_ipconfig_driver *driver)
100 {
101         DBG("driver %p name %s", driver, driver->name);
102
103         driver_list = g_slist_insert_sorted(driver_list, driver,
104                                                         compare_priority);
105
106         return 0;
107 }
108
109 /**
110  * connman_ipconfig_driver_unregister:
111  * @driver: IP configuration driver
112  *
113  * Remove a previously registered IP configuration driver.
114  */
115 void connman_ipconfig_driver_unregister(struct connman_ipconfig_driver *driver)
116 {
117         DBG("driver %p name %s", driver, driver->name);
118
119         driver_list = g_slist_remove(driver_list, driver);
120 }