File: oobs-iface-ethernet.c

package info (click to toggle)
liboobs 3.0.0-4.4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,284 kB
  • sloc: sh: 10,131; ansic: 9,920; makefile: 176; xml: 1
file content (531 lines) | stat: -rw-r--r-- 14,907 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
/* -*- Mode: C; c-file-style: "gnu"; tab-width: 8 -*- */
/* Copyright (C) 2004-2005 Carlos Garnacho
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
 *
 * Authors: Carlos Garnacho Parro  <carlosg@gnome.org>
 */

#include <glib-object.h>
#include <string.h>
#include "oobs-ifacesconfig.h"
#include "oobs-iface-ethernet.h"
#include "oobs-iface.h"

/**
 * SECTION:oobs-iface-ethernet
 * @title: OobsIfaceEthernet
 * @short_description: Object that represents an individual Ethernet interface
 * @see_also: #OobsIface, #OobsIfacesConfig, #OobsIfaceIRLan,
 *     #OobsIfacePlip, #OobsIfacePPP, #OobsIfaceWireless
 **/

#define OOBS_IFACE_ETHERNET_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), OOBS_TYPE_IFACE_ETHERNET, OobsIfaceEthernetPrivate))

typedef struct _OobsIfaceEthernetPrivate OobsIfaceEthernetPrivate;

struct _OobsIfaceEthernetPrivate
{
  OobsObject *config;
  gchar *configuration_method;

  gchar *address;
  gchar *netmask;
  gchar *gateway;

  gchar *network;
  gchar *broadcast;
};

static void oobs_iface_ethernet_class_init (OobsIfaceEthernetClass *class);
static void oobs_iface_ethernet_init       (OobsIfaceEthernet      *iface);
static void oobs_iface_ethernet_finalize   (GObject                *object);

static gboolean oobs_iface_ethernet_has_gateway   (OobsIface *iface);
static gboolean oobs_iface_ethernet_is_configured (OobsIface *iface);

static void oobs_iface_ethernet_set_property (GObject      *object,
					      guint         prop_id,
					      const GValue *value,
					      GParamSpec   *pspec);
static void oobs_iface_ethernet_get_property (GObject      *object,
					      guint         prop_id,
					      GValue       *value,
					      GParamSpec   *pspec);

enum {
  PROP_0,
  PROP_ADDRESS,
  PROP_NETMASK,
  PROP_GATEWAY,
  PROP_NETWORK,
  PROP_BROADCAST,
  PROP_CONFIGURATION_METHOD
};

G_DEFINE_TYPE (OobsIfaceEthernet, oobs_iface_ethernet, OOBS_TYPE_IFACE);

static void
oobs_iface_ethernet_class_init (OobsIfaceEthernetClass *class)
{
  GObjectClass *object_class = G_OBJECT_CLASS (class);
  OobsIfaceClass *iface_class = OOBS_IFACE_CLASS (class);

  object_class->set_property = oobs_iface_ethernet_set_property;
  object_class->get_property = oobs_iface_ethernet_get_property;
  object_class->finalize     = oobs_iface_ethernet_finalize;

  iface_class->has_gateway   = oobs_iface_ethernet_has_gateway;
  iface_class->is_configured = oobs_iface_ethernet_is_configured;

  g_object_class_install_property (object_class,
				   PROP_ADDRESS,
				   g_param_spec_string ("ip_address",
							"Iface address",
							"Address for the iface",
							NULL,
							G_PARAM_READWRITE));
  g_object_class_install_property (object_class,
				   PROP_NETMASK,
				   g_param_spec_string ("ip_mask",
							"Iface netmask",
							"Netmask for the iface",
							NULL,
							G_PARAM_READWRITE));
  g_object_class_install_property (object_class,
				   PROP_GATEWAY,
				   g_param_spec_string ("gateway_address",
							"Iface gateway",
							"Gateway address for the iface",
							NULL,
							G_PARAM_READWRITE));
  g_object_class_install_property (object_class,
				   PROP_NETWORK,
				   g_param_spec_string ("network_address",
							"Iface network address",
							"Network address for the iface",
							NULL,
							G_PARAM_READWRITE));
  g_object_class_install_property (object_class,
				   PROP_BROADCAST,
				   g_param_spec_string ("broadcast_address",
							"Iface broadcast",
							"Network broadcast for the iface",
							NULL,
							G_PARAM_READWRITE));
  g_object_class_install_property (object_class,
				   PROP_CONFIGURATION_METHOD,
				   g_param_spec_string ("config_method",
							"Iface configuration method",
							"Network configuration method for the iface",
							NULL,
							G_PARAM_READWRITE));
  g_type_class_add_private (object_class,
			    sizeof (OobsIfaceEthernetPrivate));
}

static void
oobs_iface_ethernet_init (OobsIfaceEthernet *iface)
{
  OobsIfaceEthernetPrivate *priv;

  g_return_if_fail (OOBS_IS_IFACE_ETHERNET (iface));

  priv = OOBS_IFACE_ETHERNET_GET_PRIVATE (iface);

  priv->config  = oobs_ifaces_config_get ();
  priv->address = NULL;
  priv->netmask = NULL;
  priv->gateway = NULL;
  priv->network = NULL;
  priv->broadcast = NULL;
  priv->configuration_method = NULL;
  iface->_priv = priv;
}

static void
oobs_iface_ethernet_finalize (GObject *object)
{
  OobsIfaceEthernetPrivate *priv;

  g_return_if_fail (OOBS_IS_IFACE_ETHERNET (object));

  priv = OOBS_IFACE_ETHERNET (object)->_priv;

  if (priv)
    {
      g_free (priv->address);
      g_free (priv->netmask);
      g_free (priv->gateway);
      g_free (priv->network);
      g_free (priv->broadcast);
      g_free (priv->configuration_method);
    }

  if (G_OBJECT_CLASS (oobs_iface_ethernet_parent_class)->finalize)
    (* G_OBJECT_CLASS (oobs_iface_ethernet_parent_class)->finalize) (object);
}

static void
oobs_iface_ethernet_set_property (GObject      *object,
				  guint         prop_id,
				  const GValue *value,
				  GParamSpec   *pspec)
{
  OobsIfaceEthernetPrivate *priv;

  g_return_if_fail (OOBS_IS_IFACE_ETHERNET (object));

  priv = OOBS_IFACE_ETHERNET (object)->_priv;

  switch (prop_id)
    {
    case PROP_ADDRESS:
      g_free (priv->address);
      priv->address = g_value_dup_string (value);
      break;
    case PROP_NETMASK:
      g_free (priv->netmask);
      priv->netmask = g_value_dup_string (value);
      break;
    case PROP_GATEWAY:
      g_free (priv->gateway);
      priv->gateway = g_value_dup_string (value);
      break;
    case PROP_NETWORK:
      g_free (priv->network);
      priv->network = g_value_dup_string (value);
      break;
    case PROP_BROADCAST:
      g_free (priv->broadcast);
      priv->broadcast = g_value_dup_string (value);
      break;
    case PROP_CONFIGURATION_METHOD:
      g_free (priv->configuration_method);
      priv->configuration_method = g_value_dup_string (value);
      break;
    }
}

static void
oobs_iface_ethernet_get_property (GObject      *object,
				  guint         prop_id,
				  GValue       *value,
				  GParamSpec   *pspec)
{
  OobsIfaceEthernetPrivate *priv;

  g_return_if_fail (OOBS_IS_IFACE_ETHERNET (object));

  priv = OOBS_IFACE_ETHERNET (object)->_priv;

  switch (prop_id)
    {
    case PROP_ADDRESS:
      g_value_set_string (value, priv->address);
      break;
    case PROP_NETMASK:
      g_value_set_string (value, priv->netmask);
      break;
    case PROP_GATEWAY:
      g_value_set_string (value, priv->gateway);
      break;
    case PROP_NETWORK:
      g_value_set_string (value, priv->network);
      break;
    case PROP_BROADCAST:
      g_value_set_string (value, priv->broadcast);
      break;
    case PROP_CONFIGURATION_METHOD:
      g_value_set_string (value, priv->configuration_method);
      break;
    }
}

static gboolean
oobs_iface_ethernet_has_gateway (OobsIface *iface)
{
  OobsIfaceEthernetPrivate *priv;
  GList *methods = NULL;

  priv = OOBS_IFACE_ETHERNET (iface)->_priv;
  methods = oobs_ifaces_config_get_available_configuration_methods (OOBS_IFACES_CONFIG (priv->config));

  /* assume that only the "static" configuration
   * method needs setting a gateway by hand
   */
  return (priv->configuration_method &&
	  ((strcmp (priv->configuration_method, "static") == 0 && priv->gateway) ||
	   g_list_find_custom (methods, priv->configuration_method, (GCompareFunc) strcmp)));
}

static gboolean
oobs_iface_ethernet_is_configured (OobsIface *iface)
{
  OobsIfaceEthernetPrivate *priv;
  GList *methods = NULL;

  priv = OOBS_IFACE_ETHERNET (iface)->_priv;
  methods = oobs_ifaces_config_get_available_configuration_methods (OOBS_IFACES_CONFIG (priv->config));

  /* assume that all supported configuration methods
   * except "static" do not require aditional configuration
   */
  if (!priv->configuration_method)
    return FALSE;

  if (strcmp (priv->configuration_method, "static") == 0)
    return (priv->address && priv->netmask);

  return (g_list_find_custom (methods, priv->configuration_method, (GCompareFunc) strcmp) != NULL);
}

/**
 * oobs_iface_ethernet_get_ip_address:
 * @iface: An #OobsIfaceEthernet.
 * 
 * Returns the IP address that this interface uses.
 * 
 * Return Value: A pointer to the IP address as a string. This
 *               string must not be freed, modified or stored.
 **/
G_CONST_RETURN gchar*
oobs_iface_ethernet_get_ip_address (OobsIfaceEthernet *iface)
{
  OobsIfaceEthernetPrivate *priv;

  g_return_val_if_fail (OOBS_IS_IFACE_ETHERNET (iface), NULL);
  
  priv = iface->_priv;

  return priv->address;
}

/**
 * oobs_iface_ethernet_set_ip_address:
 * @iface: An #OobsIfaceEthernet.
 * @address: a new IP address for the interface.
 * 
 * Sets a new IP address for the interface,
 * overwriting the previous one.
 **/
void
oobs_iface_ethernet_set_ip_address (OobsIfaceEthernet *iface,
				    const gchar       *address)
{
  g_return_if_fail (OOBS_IS_IFACE_ETHERNET (iface));

  /* FIXME: should validate IP address */

  g_object_set (G_OBJECT (iface), "ip-address", address, NULL);
}

/**
 * oobs_iface_ethernet_get_network_mask:
 * @iface: An #OobsIfaceEthernet.
 * 
 * Returns the IP network mask that this interface uses.
 * 
 * Return Value: A pointer to the network mask as a string. This
 *               string must not be freed, modified or stored.
 **/
G_CONST_RETURN gchar*
oobs_iface_ethernet_get_network_mask (OobsIfaceEthernet *iface)
{
  OobsIfaceEthernetPrivate *priv;

  g_return_val_if_fail (OOBS_IS_IFACE_ETHERNET (iface), NULL);
  
  priv = iface->_priv;

  return priv->netmask;
}

/**
 * oobs_iface_ethernet_set_network_mask:
 * @iface: An #OobsIfaceEthernet.
 * @mask: a new IP network mask for the interface.
 * 
 * Sets a new IP network mask for the interface,
 * overwriting the previous one.
 **/
void
oobs_iface_ethernet_set_network_mask (OobsIfaceEthernet *iface,
				      const gchar       *mask)
{
  g_return_if_fail (OOBS_IS_IFACE_ETHERNET (iface));

  /* FIXME: should validate IP address */

  g_object_set (G_OBJECT (iface), "ip-mask", mask, NULL);
}

/**
 * oobs_iface_ethernet_get_gateway_address:
 * @iface: An #OobsIfaceEthernet.
 * 
 * Returns the gateway IP address that this interface uses.
 * 
 * Return Value: A pointer to the gateway address as a string. This
 *               string must not be freed, modified or stored.
 **/
G_CONST_RETURN gchar*
oobs_iface_ethernet_get_gateway_address (OobsIfaceEthernet *iface)
{
  OobsIfaceEthernetPrivate *priv;

  g_return_val_if_fail (OOBS_IS_IFACE_ETHERNET (iface), NULL);
  
  priv = iface->_priv;

  return priv->gateway;
}

/**
 * oobs_iface_ethernet_set_gateway_address:
 * @iface: An #OobsIfaceEthernet.
 * @address: a new gateway IP address for the interface.
 * 
 * Sets a new gateway IP address for the interface,
 * overwriting the previous one.
 **/
void
oobs_iface_ethernet_set_gateway_address (OobsIfaceEthernet *iface,
					 const gchar       *address)
{
  g_return_if_fail (OOBS_IS_IFACE_ETHERNET (iface));

  /* FIXME: should validate IP address */

  g_object_set (G_OBJECT (iface), "gateway-address", address, NULL);
}

/**
 * oobs_iface_ethernet_get_network_address:
 * @iface: An #OobsIfaceEthernet.
 * 
 * Returns the network address for this interface.
 * 
 * Return Value: A pointer to the network address as a string. This
 *               string must not be freed, modified or stored.
 **/
G_CONST_RETURN gchar*
oobs_iface_ethernet_get_network_address (OobsIfaceEthernet *iface)
{
  OobsIfaceEthernetPrivate *priv;

  g_return_val_if_fail (OOBS_IS_IFACE_ETHERNET (iface), NULL);
  
  priv = iface->_priv;

  return priv->network;
}

/**
 * oobs_iface_ethernet_set_network_address:
 * @iface: An #OobsIfaceEthernet.
 * @address: a new network address for the interface.
 * 
 * Sets a new network address for the interface,
 * overwriting the previous one.
 **/
void
oobs_iface_ethernet_set_network_address (OobsIfaceEthernet *iface,
					 const gchar       *address)
{
  g_return_if_fail (OOBS_IS_IFACE_ETHERNET (iface));

  /* FIXME: should validate IP address */

  g_object_set (G_OBJECT (iface), "network-address", address, NULL);
}

/**
 * oobs_iface_ethernet_get_broadcast_address:
 * @iface: An #OobsIfaceEthernet.
 * 
 * Returns the broadcast address for this interface.
 * 
 * Return Value: A pointer to the broadcast address as a string. This
 *               string must not be freed, modified or stored.
 **/
G_CONST_RETURN gchar*
oobs_iface_ethernet_get_broadcast_address (OobsIfaceEthernet *iface)
{
  OobsIfaceEthernetPrivate *priv;

  g_return_val_if_fail (OOBS_IS_IFACE_ETHERNET (iface), NULL);
  
  priv = iface->_priv;

  return priv->broadcast;
}

/**
 * oobs_iface_ethernet_set_broadcast_address:
 * @iface: An #OobsIfaceEthernet.
 * @address: a new broadcast address for the interface.
 * 
 * Sets a new broadcast address for the interface,
 * overwriting the previous one.
 **/
void
oobs_iface_ethernet_set_broadcast_address (OobsIfaceEthernet *iface,
					   const gchar       *address)
{
  g_return_if_fail (OOBS_IS_IFACE_ETHERNET (iface));

  /* FIXME: should validate IP address */

  g_object_set (G_OBJECT (iface), "broadcast-address", address, NULL);
}

/**
 * oobs_iface_ethernet_get_configuration_method:
 * @iface: An #OobsIfaceEthernet.
 * 
 * Returns the configuration method for the interface.
 * 
 * Return Value: The configuration method that the interface uses.
 * This value must not be modified nor freed.
 **/
G_CONST_RETURN gchar*
oobs_iface_ethernet_get_configuration_method (OobsIfaceEthernet *iface)
{
  OobsIfaceEthernetPrivate *priv;

  g_return_val_if_fail (OOBS_IS_IFACE_ETHERNET (iface), NULL);
  
  priv = iface->_priv;

  return priv->configuration_method;
}

/**
 * oobs_iface_ethernet_set_configuration_method:
 * @iface: An #OobsIfaceEthernet.
 * @method: A new configuration method for the interface, or %NULL.
 * 
 * Sets the configuration method that the interface will use. The valid methods are
 * provided by oobs_ifaces_config_get_available_configuration_methods().
 **/
void
oobs_iface_ethernet_set_configuration_method (OobsIfaceEthernet *iface,
					      const gchar       *method)
{
  g_return_if_fail (OOBS_IS_IFACE_ETHERNET (iface));

  g_object_set (G_OBJECT (iface), "config-method", method, NULL);
}