/* * 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 #include #include #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; gboolean has_brd; gboolean has_local; uint32_t flags; unsigned char scope; }; 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_dispose (GObject *obj) { NIIP *ni_ip; GObjectClass *parent_class = g_type_class_peek (G_TYPE_OBJECT); ni_ip = NI_IP (obj); /* Quitar la referencia hacia el ni_interface */ g_object_unref (ni_ip->priv->ni_interface); parent_class->dispose (obj); } static void ni_ip_finalize (GObject *obj) { NIIP *ni_ip; GObjectClass *parent_class = g_type_class_peek (G_TYPE_OBJECT); ni_ip = NI_IP (obj); /* Nada que hacer, por el momento */ parent_class->finalize (obj); } 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)); g_object_ref (ni_ip->priv->ni_interface); 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; object_class->dispose = ni_ip_dispose; object_class->finalize = ni_ip_finalize; 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; } 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) { NIIP *ni_ip; g_return_val_if_fail (NI_IS_INTERFACE (ni_interface), 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; 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; }