NetworkInador/client-gtk/ni-ip.c

253 lines
6.3 KiB
C

/*
* ni-ip.c
* This file is part of NetworkInador
*
* Copyright (C) 2021 - Gatuno
*
* NetworkInador is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* NetworkInador 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 NetworkInador; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301 USA
*/
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include "ni-interface.h"
#include "ni-ip.h"
#include "../src/network-inador-manager.h"
struct _NIIPPrivate {
NIInterface *ni_interface;
guint family;
gint family_size;
guint prefix;
struct_addr local_addr;
struct_addr addr;
struct_addr brd_addr;
/* Timestamps */
guint32 ifa_prefered;
guint32 ifa_valid;
guint32 cstamp;
guint32 tstamp;
gboolean has_brd;
gboolean has_local;
uint32_t flags;
unsigned char scope;
char label[256];
};
enum {
PROP_NI_INTERFACE = 1,
N_PROPERTIES
};
G_DEFINE_TYPE_WITH_PRIVATE (NIIP, ni_ip, G_TYPE_OBJECT)
static GParamSpec *obj_properties[N_PROPERTIES] = { NULL, };
static void ni_ip_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec) {
NIIP *ni_ip = NI_IP (object);
g_return_if_fail (NI_IS_IP (object));
switch (prop_id) {
case PROP_NI_INTERFACE:
ni_ip->priv->ni_interface = NI_INTERFACE (g_value_get_object (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void ni_ip_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec) {
NIIP *ni_ip = NI_IP (object);
g_return_if_fail (NI_IS_IP (object));
switch (prop_id) {
case PROP_NI_INTERFACE:
g_value_set_object (value, ni_ip->priv->ni_interface);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void ni_ip_class_init (NIIPClass *klass) {
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->set_property = ni_ip_set_property;
object_class->get_property = ni_ip_get_property;
obj_properties[PROP_NI_INTERFACE] = g_param_spec_object (
"ni-interface",
"Network Inador Interface",
"The interface object",
NI_TYPE_INTERFACE,
G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
g_object_class_install_properties (object_class, N_PROPERTIES, obj_properties);
}
static void ni_ip_init (NIIP *ni_ip) {
NIIPPrivate *priv = ni_ip_get_instance_private (ni_ip);
ni_ip->priv = priv;
/* initialize all public and private members to reasonable default values.
* They are all automatically initialized to 0 to begin with. */
priv->ni_interface = NULL;
priv->prefix = 0;
priv->family = AF_UNSPEC;
priv->has_brd = FALSE;
priv->has_local = FALSE;
priv->flags = 0;
priv->scope = 0;
memset (&priv->addr, 0, sizeof (priv->addr));
memset (&priv->local_addr, 0, sizeof (priv->local_addr));
memset (&priv->brd_addr, 0, sizeof (priv->brd_addr));
}
/* Los métodos getters */
gboolean ni_ip_has_local (NIIP *ni_ip) {
g_return_val_if_fail (NI_IS_IP (ni_ip), FALSE);
return ni_ip->priv->has_local;
}
gboolean ni_ip_has_brd (NIIP *ni_ip) {
g_return_val_if_fail (NI_IS_IP (ni_ip), FALSE);
return ni_ip->priv->has_local;
}
guint ni_ip_get_family (NIIP *ni_ip) {
g_return_val_if_fail (NI_IS_IP (ni_ip), 0);
return ni_ip->priv->family;
}
guint ni_ip_get_prefix (NIIP *ni_ip) {
g_return_val_if_fail (NI_IS_IP (ni_ip), 0);
return ni_ip->priv->prefix;
}
uint32_t ni_ip_get_flags (NIIP *ni_ip) {
g_return_val_if_fail (NI_IS_IP (ni_ip), 0);
return ni_ip->priv->flags;
}
unsigned char ni_ip_get_scope (NIIP *ni_ip) {
g_return_val_if_fail (NI_IS_IP (ni_ip), 0);
return ni_ip->priv->scope;
}
const struct_addr *ni_ip_get_addr (NIIP *ni_ip) {
g_return_val_if_fail (NI_IS_IP (ni_ip), NULL);
return &ni_ip->priv->addr;
}
const struct_addr *ni_ip_get_local_addr (NIIP *ni_ip) {
g_return_val_if_fail (NI_IS_IP (ni_ip), NULL);
return &ni_ip->priv->local_addr;
}
const struct_addr *ni_ip_get_brd_addr (NIIP *ni_ip) {
g_return_val_if_fail (NI_IS_IP (ni_ip), NULL);
return &ni_ip->priv->brd_addr;
}
uint32_t ni_ip_get_ifa_prefered (NIIP *ni_ip) {
g_return_val_if_fail (NI_IS_IP (ni_ip), 0);
return ni_ip->priv->ifa_prefered;
}
uint32_t ni_ip_get_ifa_valid (NIIP *ni_ip) {
g_return_val_if_fail (NI_IS_IP (ni_ip), 0);
return ni_ip->priv->ifa_valid;
}
uint32_t ni_ip_get_cstamp (NIIP *ni_ip) {
g_return_val_if_fail (NI_IS_IP (ni_ip), 0);
return ni_ip->priv->cstamp;
}
uint32_t ni_ip_get_tstamp (NIIP *ni_ip) {
g_return_val_if_fail (NI_IS_IP (ni_ip), 0);
return ni_ip->priv->tstamp;
}
NIIP *ni_ip_new (NIInterface *ni_interface, int family, int prefix, struct_addr *addr, uint32_t flags, unsigned char scope, int has_local, struct_addr *local_addr, int has_brd, struct_addr *brd_addr, guint32 *cacheinfo, char *label) {
NIIP *ni_ip;
g_return_val_if_fail (NI_IS_INTERFACE (ni_interface), NULL);
g_return_val_if_fail (cacheinfo != NULL, NULL);
ni_ip = g_object_new (NI_TYPE_IP, "ni-interface", ni_interface, NULL);
/* Establecer y copiar los valores */
ni_ip->priv->family = family;
ni_ip->priv->prefix = prefix;
ni_ip->priv->has_local = has_local;
ni_ip->priv->has_brd = has_brd;
ni_ip->priv->flags = flags;
ni_ip->priv->scope = scope;
/* Copiar los valores del arreglo */
ni_ip->priv->ifa_prefered = cacheinfo[0];
ni_ip->priv->ifa_valid = cacheinfo[1];
ni_ip->priv->cstamp = cacheinfo[2];
ni_ip->priv->tstamp = cacheinfo[3];
ni_ip->priv->label[0] = 0;
if (label != NULL && label[0] != 0) {
strncpy (ni_ip->priv->label, label, sizeof (ni_ip->priv->label));
}
if (family == AF_INET) {
ni_ip->priv->family_size = sizeof (struct in_addr);
} else if (family == AF_INET6) {
ni_ip->priv->family_size = sizeof (struct in6_addr);
}
memcpy (&ni_ip->priv->addr, addr, ni_ip->priv->family_size);
if (has_local) {
memcpy (&ni_ip->priv->local_addr, local_addr, ni_ip->priv->family_size);
}
if (has_brd) {
memcpy (&ni_ip->priv->brd_addr, brd_addr, ni_ip->priv->family_size);
}
return ni_ip;
}