Check that hostname and domainname is valid
[connman] / plugins / ethernet.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 <errno.h>
27 #include <net/if.h>
28
29 #ifndef IFF_LOWER_UP
30 #define IFF_LOWER_UP    0x10000
31 #endif
32
33 #include <glib.h>
34
35 #define CONNMAN_API_SUBJECT_TO_CHANGE
36 #include <connman/plugin.h>
37 #include <connman/device.h>
38 #include <connman/rtnl.h>
39 #include <connman/log.h>
40
41 #include "inet.h"
42
43 struct ethernet_data {
44         int index;
45         unsigned flags;
46         unsigned int watch;
47 };
48
49 static void ethernet_newlink(unsigned flags, unsigned change, void *user_data)
50 {
51         struct connman_device *device = user_data;
52         struct ethernet_data *ethernet = connman_device_get_data(device);
53
54         DBG("index %d flags %ld change %ld", ethernet->index, flags, change);
55
56         if ((ethernet->flags & IFF_UP) != (flags & IFF_UP)) {
57                 if (flags & IFF_UP) {
58                         DBG("power on");
59                         connman_device_set_powered(device, TRUE);
60                 } else {
61                         DBG("power off");
62                         connman_device_set_powered(device, FALSE);
63                 }
64         }
65
66         if ((ethernet->flags & IFF_LOWER_UP) != (flags & IFF_LOWER_UP)) {
67                 if (flags & IFF_LOWER_UP) {
68                         DBG("carrier on");
69                         connman_device_set_carrier(device, TRUE);
70                 } else {
71                         DBG("carrier off");
72                         connman_device_set_carrier(device, FALSE);
73                 }
74         }
75
76         ethernet->flags = flags;
77 }
78
79 static int ethernet_probe(struct connman_device *device)
80 {
81         struct ethernet_data *ethernet;
82
83         DBG("device %p", device);
84
85         ethernet = g_try_new0(struct ethernet_data, 1);
86         if (ethernet == NULL)
87                 return -ENOMEM;
88
89         connman_device_set_data(device, ethernet);
90
91         ethernet->index = connman_device_get_index(device);
92         ethernet->flags = 0;
93
94         ethernet->watch = connman_rtnl_add_newlink_watch(ethernet->index,
95                                                 ethernet_newlink, device);
96
97         connman_rtnl_send_getlink();
98
99         return 0;
100 }
101
102 static void ethernet_remove(struct connman_device *device)
103 {
104         struct ethernet_data *ethernet = connman_device_get_data(device);
105
106         DBG("device %p", device);
107
108         connman_device_set_data(device, NULL);
109
110         connman_rtnl_remove_watch(ethernet->watch);
111
112         g_free(ethernet);
113 }
114
115 static int ethernet_enable(struct connman_device *device)
116 {
117         struct ethernet_data *ethernet = connman_device_get_data(device);
118
119         DBG("device %p", device);
120
121         return inet_ifup(ethernet->index);
122 }
123
124 static int ethernet_disable(struct connman_device *device)
125 {
126         struct ethernet_data *ethernet = connman_device_get_data(device);
127
128         DBG("device %p", device);
129
130         return inet_ifdown(ethernet->index);
131 }
132
133 static struct connman_device_driver ethernet_driver = {
134         .name           = "ethernet",
135         .type           = CONNMAN_DEVICE_TYPE_ETHERNET,
136         .probe          = ethernet_probe,
137         .remove         = ethernet_remove,
138         .enable         = ethernet_enable,
139         .disable        = ethernet_disable,
140 };
141
142 static int ethernet_init(void)
143 {
144         return connman_device_driver_register(&ethernet_driver);
145 }
146
147 static void ethernet_exit(void)
148 {
149         connman_device_driver_unregister(&ethernet_driver);
150 }
151
152 CONNMAN_PLUGIN_DEFINE(ethernet, "Ethernet interface plugin", VERSION,
153                 CONNMAN_PLUGIN_PRIORITY_DEFAULT, ethernet_init, ethernet_exit)