NetworkInador/lib/getters.c

373 lines
9.2 KiB
C
Raw Normal View History

/*
* getters.c
* This file is part of Network-inador
*
* Copyright (C) 2025 - Félix Arreola Rodríguez
*
* Network-inador 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.
*
* Network-inador 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 Network-inador; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301 USA
*/
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include "network-inador-private.h"
#include "interfaces.h"
#include "ip-address.h"
#include "routes.h"
#include "rtables.h"
uint32_t *network_inador_list_ifaces (NetworkInadorHandle *handle) {
uint32_t *memory;
FList *g;
Interface *iface;
int c;
memory = (uint32_t *) malloc ((handle->interfaces_counter + 1) * sizeof (uint32_t));
if (memory == NULL) return NULL;
c = 0;
for (g = handle->interfaces; g != NULL; g = g->next) {
iface = (Interface *) g->data;
memory[c] = iface->index;
c++;
}
memory[c] = 0;
return memory;
}
Interface *network_inador_get_iface (NetworkInadorHandle *handle, uint32_t index) {
return _interfaces_locate_by_index (handle->interfaces, index);
}
Interface *network_inador_get_iface_by_name (NetworkInadorHandle *handle, const char *name) {
return _interfaces_locate_by_name (handle->interfaces, name);
}
const unsigned char *network_inador_iface_get_name (Interface *iface) {
return iface->name;
}
uint32_t network_inador_iface_get_index (Interface *iface) {
return iface->index;
}
uint32_t network_inador_iface_get_link_type (Interface *iface) {
return iface->link_type;
}
uint32_t network_inador_iface_get_master_index (Interface *iface) {
return iface->master_index;
}
uint32_t network_inador_iface_get_mtu (Interface *iface) {
return iface->mtu;
}
uint16_t network_inador_iface_get_flags (Interface *iface) {
return iface->flags;
}
uint8_t network_inador_iface_get_is_wireless (Interface *iface) {
return iface->is_wireless;
}
uint8_t network_inador_iface_get_num_ips (Interface *iface) {
return iface->address_counter;
}
IPAddr *network_inador_iface_get_ipaddr_by_index (Interface *iface, int index) {
2025-01-10 01:55:08 -06:00
return (IPAddr *) f_list_nth_data (f_list_first (iface->address), index);
}
Interface *network_inador_ipaddr_get_iface (IPAddr *ip_addr) {
return ip_addr->iface;
}
uint8_t network_inador_ipaddr_get_family (IPAddr *ip_addr) {
return ip_addr->family;
}
uint8_t network_inador_ipaddr_get_prefix (IPAddr *ip_addr) {
return ip_addr->prefix;
}
uint8_t network_inador_ipaddr_has_local (IPAddr *ip_addr) {
return ip_addr->has_local;
}
uint8_t network_inador_ipaddr_has_brd (IPAddr *ip_addr) {
return ip_addr->has_brd;
}
uint8_t network_inador_ipaddr_get_scope (IPAddr *ip_addr) {
return ip_addr->scope;
}
uint32_t network_inador_ipaddr_get_flags (IPAddr *ip_addr) {
return ip_addr->flags;
}
void network_inador_ipaddr_get_cacheinfo (IPAddr *ip_addr, struct ifa_cacheinfo *cacheinfo) {
memcpy (cacheinfo, &ip_addr->cacheinfo, sizeof (struct ifa_cacheinfo));
}
void network_inador_ipaddr_get_addr (IPAddr *ip_addr, void *addr, int *addr_size) {
int family_size = 0;
if (ip_addr->family == AF_INET) {
family_size = sizeof (struct in_addr);
} else if (ip_addr->family == AF_INET6) {
family_size = sizeof (struct in6_addr);
}
if (addr_size != NULL) {
/* Verificar que no supere el máximo de la dirección */
if (*addr_size < family_size) {
family_size = *addr_size;
}
}
memcpy (addr, &ip_addr->addr, family_size);
if (addr_size != NULL) {
*addr_size = family_size;
}
}
void network_inador_ipaddr_get_local_addr (IPAddr *ip_addr, void *addr, int *addr_size) {
int family_size = 0;
if (ip_addr->family == AF_INET) {
family_size = sizeof (struct in_addr);
} else if (ip_addr->family == AF_INET6) {
family_size = sizeof (struct in6_addr);
}
if (addr_size != NULL) {
/* Verificar que no supere el máximo de la dirección */
if (*addr_size < family_size) {
family_size = *addr_size;
}
}
memcpy (addr, &ip_addr->local_addr, family_size);
if (addr_size != NULL) {
*addr_size = family_size;
}
}
void network_inador_ipaddr_get_brd_addr (IPAddr *ip_addr, void *addr, int *addr_size) {
int family_size = 0;
if (ip_addr->family == AF_INET) {
family_size = sizeof (struct in_addr);
} else if (ip_addr->family == AF_INET6) {
family_size = sizeof (struct in6_addr);
}
if (addr_size != NULL) {
/* Verificar que no supere el máximo de la dirección */
if (*addr_size < family_size) {
family_size = *addr_size;
}
}
memcpy (addr, &ip_addr->brd_addr, family_size);
if (addr_size != NULL) {
*addr_size = family_size;
}
}
const unsigned char *network_inador_ipaddr_get_label (IPAddr *ip_addr) {
return ip_addr->label;
}
int network_inador_get_routes_count (NetworkInadorHandle *handle, int family) {
if (family == AF_UNSPEC) {
return handle->route_v4_counter + handle->route_v6_counter;
} else if (family == AF_INET) {
return handle->route_v4_counter;
} else if (family == AF_INET6) {
return handle->route_v6_counter;
}
return 0;
}
Route *network_inador_get_route_by_index (NetworkInadorHandle *handle, int family, int index) {
if (family == AF_INET) {
return (Route *) f_list_nth_data (f_list_first (handle->route_v4_tables), index);
} else if (family == AF_INET6) {
return (Route *) f_list_nth_data (f_list_first (handle->route_v6_tables), index);
}
return NULL;
}
uint8_t network_inador_route_get_family (Route *route) {
return route->family;
}
uint8_t network_inador_route_get_type (Route *route) {
return route->type;
}
uint32_t network_inador_route_get_table (Route *route) {
return route->table;
}
uint8_t network_inador_route_get_prefix (Route *route) {
return route->prefix;
}
uint8_t network_inador_route_get_protocol (Route *route) {
return route->protocol;
}
uint8_t network_inador_route_get_tos (Route *route) {
return route->tos;
}
uint8_t network_inador_route_get_scope (Route *route) {
return route->scope;
}
uint8_t network_inador_route_has_prefsrc (Route *route) {
return route->has_prefsrc;
}
uint32_t network_inador_route_get_priority (Route *route) {
return route->priority;
}
void network_inador_route_get_dest (Route *route, void *dest, int *addr_size) {
int family_size = 0;
if (route->family == AF_INET) {
family_size = sizeof (struct in_addr);
} else if (route->family == AF_INET6) {
family_size = sizeof (struct in6_addr);
}
if (addr_size != NULL) {
/* Verificar que no supere el máximo de la dirección */
if (*addr_size < family_size) {
family_size = *addr_size;
}
}
memcpy (dest, &route->dest, family_size);
if (addr_size != NULL) {
*addr_size = family_size;
}
}
void network_inador_route_get_prefsrc (Route *route, void *prefsrc, int *addr_size) {
int family_size = 0;
if (route->family == AF_INET) {
family_size = sizeof (struct in_addr);
} else if (route->family == AF_INET6) {
family_size = sizeof (struct in6_addr);
}
if (addr_size != NULL) {
/* Verificar que no supere el máximo de la dirección */
if (*addr_size < family_size) {
family_size = *addr_size;
}
}
memcpy (prefsrc, &route->prefsrc, family_size);
if (addr_size != NULL) {
*addr_size = family_size;
}
}
int network_inador_route_get_num_nexthops (Route *route) {
return f_list_length (route->nexthops);
}
RouteNH *network_inador_route_get_nexthop_by_index (Route *route, int index) {
return (RouteNH *) f_list_nth_data (f_list_first (route->nexthops), index);
}
uint8_t network_inador_nexthop_has_gw (RouteNH *nexthop) {
return nexthop->has_gw;
}
void network_inador_nexthop_get_gw (RouteNH *nexthop, void *gw, int *addr_size) {
int family_size = 0;
if (nexthop->family == AF_INET) {
family_size = sizeof (struct in_addr);
} else if (nexthop->family == AF_INET6) {
family_size = sizeof (struct in6_addr);
}
if (addr_size != NULL) {
/* Verificar que no supere el máximo de la dirección */
if (*addr_size < family_size) {
family_size = *addr_size;
}
}
memcpy (gw, &nexthop->gw, family_size);
if (addr_size != NULL) {
*addr_size = family_size;
}
}
uint8_t network_inador_nexthop_get_flags (RouteNH *nexthop) {
return nexthop->nh_flags;
}
uint8_t network_inador_nexthop_get_weight (RouteNH *nexthop) {
return nexthop->nh_weight;
}
uint32_t network_inador_nexthop_get_out_index (RouteNH *nexthop) {
return nexthop->out_index;
}
int network_inador_rtables_get_count_tables (NetworkInadorHandle *handle) {
return handle->rtables_counter;
}
void network_inador_rtables_get_table_by_index (NetworkInadorHandle *handle, uint32_t nth, uint32_t *table_index, unsigned char *table_name, size_t table_name_size) {
RouteTable *table;
table = (RouteTable *) f_list_nth_data (f_list_first (handle->route_tables_names), nth);
if (table_index != NULL) {
*table_index = table->table;
}
if (table_name != NULL) {
strncpy (table_name, table->name, table_name_size);
}
}