Skip to content

Commit

Permalink
interfaces-plugin: split ipv4/6 load functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
andrej committed Oct 17, 2022
1 parent 9dfe598 commit aabca20
Show file tree
Hide file tree
Showing 6 changed files with 187 additions and 141 deletions.
68 changes: 68 additions & 0 deletions src/interfaces/src/plugin/api/interfaces/interface/ipv4/load.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include "load.h"


int interfaces_add_address_ipv4(interfaces_interface_ipv4_address_element_t **address, char *ip, char *netmask)
{
int prefix_length = 0;
int error = 0;

interfaces_interface_ipv4_address_element_t* new_element = NULL;

new_element = interfaces_interface_ipv4_address_element_new();

interfaces_interface_ipv4_address_element_set_ip(&new_element, ip);
SRPC_SAFE_CALL(interfaces_interface_ipv4_address_netmask2prefix(netmask, prefix_length), out);
interfaces_interface_ipv4_address_element_set_subnet(&new_element, netmask, interfaces_interface_ipv4_address_subnet_prefix_length);
interfaces_interface_ipv4_address_add_element(address, new_element);

out:
return error;
}

int interfaces_add_neighbor_ipv4(interfaces_interface_ipv4_neighbor_element_t** neighbor, char *dst_addr, char *ll_addr)
{
interfaces_interface_ipv4_neighbor_element_t* new_element = NULL;

new_element = interfaces_interface_ipv4_neighbor_element_new();

interfaces_interface_ipv4_neighbor_element_set_ip(&new_element, dst_addr);
interfaces_interface_ipv4_neighbor_element_set_link_layer_address(&new_element, ll_addr);
interfaces_interface_ipv4_neighbor_add_element(neighbor, new_element);

return 0;
}

unsigned int interfaces_get_ipv4_mtu(struct rtnl_link* link, interfaces_interface_t* interface)
{
unsigned int mtu = 0;

mtu = rtnl_link_get_mtu(link);

interface->ipv4.mtu = mtu;

return 0;
}

unsigned int interfaces_get_ipv4_enabled(interfaces_interface_t* interface)
{
const char *ipv4_base = "/proc/sys/net/ipv4/conf";

/* TODO: figure out how to enable/disable ipv4 */
/* since disable_ipv4 doesn't exist in /proc/sys/net/ipv6/conf/interface_name */

}

unsigned int interfaces_get_ipv4_forwarding(interfaces_interface_t* interface)
{
int error = 0;
int forwarding = 0;

const char *ipv4_base = "/proc/sys/net/ipv4/conf";

SRPC_SAFE_CALL(read_from_proc_file(ipv4_base, interface->name, "forwarding", &forwarding), out);

interface->ipv4.forwarding = forwarding;

out:
return error;
}
21 changes: 21 additions & 0 deletions src/interfaces/src/plugin/api/interfaces/interface/ipv4/load.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef INTERFACES_PLUGIN_API_INTERFACES_IPV4_LOAD_H
#define INTERFACES_PLUGIN_API_INTERFACES_IPV4_LOAD_H

#include "plugin/context.h"
#include "plugin/data/interfaces/interface.h"
#include "plugin/data/interfaces/interface/linked_list.h"
#include "plugin/types.h"

int interfaces_add_address_ipv4(interfaces_interface_ipv4_address_element_t **address, char *ip, char *netmask);

int interfaces_add_neighbor_ipv4(interfaces_interface_ipv4_neighbor_element_t** neighbor, char *dst_addr, char *ll_addr);

unsigned int interfaces_get_ipv4_mtu(struct rtnl_link* link, interfaces_interface_t* interface);

unsigned int interfaces_get_ipv4_enabled(interfaces_interface_t* interface);

unsigned int interfaces_get_ipv4_forwarding(interfaces_interface_t* interface);


#endif /* INTERFACES_PLUGIN_API_INTERFACES_IPV4_LOAD_H */

75 changes: 75 additions & 0 deletions src/interfaces/src/plugin/api/interfaces/interface/ipv6/load.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#include "load.h"

int interfaces_add_address_ipv6(interfaces_interface_ipv6_address_element_t **address, char *ip, char *netmask)
{
int error = 0;
uint8_t prefix_length = 0;

interfaces_interface_ipv6_address_element_t* new_element = NULL;

new_element = interfaces_interface_ipv6_address_element_new();

interfaces_interface_ipv6_address_element_set_ip(&new_element, ip);
SRPC_SAFE_CALL(interfaces_interface_ipv4_address_netmask2prefix(netmask, prefix_length), out);
interfaces_interface_ipv6_address_element_set_prefix_length(&new_element, prefix_length);
interfaces_interface_ipv6_address_add_element(address, new_element);

out:
return error;
}

int interfaces_add_neighbor_ipv6(interfaces_interface_ipv6_neighbor_element_t** neighbor, char *dst_addr, char *ll_addr)
{
interfaces_interface_ipv6_neighbor_element_t* new_element = NULL;

new_element = interfaces_interface_ipv6_neighbor_element_new();

interfaces_interface_ipv6_neighbor_element_set_ip(&new_element, dst_addr);
interfaces_interface_ipv6_neighbor_element_set_link_layer_address(&new_element, ll_addr);
interfaces_interface_ipv6_neighbor_add_element(neighbor, new_element);

return 0;
}

unsigned int interfaces_get_ipv6_mtu(struct rtnl_link* link, interfaces_interface_t* interface)
{
unsigned int mtu = 0;

mtu = rtnl_link_get_mtu(link);

interface->ipv6.mtu = mtu;

return 0;

}

unsigned int interfaces_get_ipv6_enabled(interfaces_interface_t* interface)
{
int error = 0;
int enabled = 0;

const char *ipv6_base = "/proc/sys/net/ipv6/conf";

SRPC_SAFE_CALL(read_from_proc_file(ipv6_base, interface->name, "disable_ipv6", &enabled), out);

interface->ipv6.enabled = enabled;

out:
return error;
}

unsigned int interfaces_get_ipv6_forwarding(interfaces_interface_t* interface)
{
int error = 0;
int forwarding = 0;

const char *ipv6_base = "/proc/sys/net/ipv6/conf";

SRPC_SAFE_CALL(read_from_proc_file(ipv6_base, interface->name, "forwarding", &forwarding), out);

interface->ipv6.forwarding = forwarding;

out:
return error;
}

20 changes: 20 additions & 0 deletions src/interfaces/src/plugin/api/interfaces/interface/ipv6/load.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef INTERFACES_PLUGIN_API_INTERFACES_IPV6_LOAD_H
#define INTERFACES_PLUGIN_API_INTERFACES_IPV6_LOAD_H

#include "plugin/context.h"
#include "plugin/data/interfaces/interface.h"
#include "plugin/data/interfaces/interface/linked_list.h"
#include "plugin/types.h"

int interfaces_add_address_ipv6(interfaces_interface_ipv6_address_element_t **address, char *ip, char *netmask);

int interfaces_add_neighbor_ipv6(interfaces_interface_ipv6_neighbor_element_t** neighbor, char *dst_addr, char *ll_addr);

unsigned int interfaces_get_ipv6_mtu(struct rtnl_link* link, interfaces_interface_t* interface);

unsigned int interfaces_get_ipv6_enabled(interfaces_interface_t* interface);

unsigned int interfaces_get_ipv6_forwarding(interfaces_interface_t* interface);

#endif /* INTERFACES_PLUGIN_API_INTERFACES_IPV6_LOAD_H */

139 changes: 2 additions & 137 deletions src/interfaces/src/plugin/api/interfaces/load.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "load.h"
#include "read.h"
#include "plugin/common.h"
#include "interface/ipv4/load.h"
#include "interface/ipv6/load.h"
#include "utils/memory.h"
#include "utlist.h"

Expand Down Expand Up @@ -31,39 +33,6 @@ enum interfaces_load_exit_status {
interfaces_load_continue = 1,
};

static int interfaces_add_address_ipv4(interfaces_interface_ipv4_address_element_t **address, char *ip, char *netmask)
{
int prefix_length = 0;
int error = 0;

interfaces_interface_ipv4_address_element_t* new_element = NULL;

new_element = interfaces_interface_ipv4_address_element_new();

interfaces_interface_ipv4_address_element_set_ip(&new_element, ip);
SRPC_SAFE_CALL(interfaces_interface_ipv4_address_netmask2prefix(netmask, prefix_length), out);
interfaces_interface_ipv4_address_element_set_subnet(&new_element, netmask, interfaces_interface_ipv4_address_subnet_prefix_length);
interfaces_interface_ipv4_address_add_element(address, new_element);

out:
return error;
}

static int interfaces_add_address_ipv6(interfaces_interface_ipv6_address_element_t **address, char *ip, char *netmask)
{
int prefix_length = 0;

interfaces_interface_ipv6_address_element_t* new_element = NULL;

new_element = interfaces_interface_ipv6_address_element_new();

interfaces_interface_ipv6_address_element_set_ip(&new_element, ip);
prefix_length = interfaces_interface_netmask_to_prefix_length(netmask);
interfaces_interface_ipv6_address_element_set_prefix_length(&new_element, prefix_length);
interfaces_interface_ipv6_address_add_element(address, new_element);

}

static int interfaces_add_ips(interfaces_interface_t* interface, char *ip, int netmask, int addr_family)
{
switch (addr_family) {
Expand Down Expand Up @@ -172,32 +141,6 @@ static int interfaces_get_interface_ips(struct nl_sock* socket, struct rtnl_link
return interfaces_load_success;
}

static int interfaces_add_neighbor_ipv4(interfaces_interface_ipv4_neighbor_element_t** neighbor, char *dst_addr, char *ll_addr)
{
interfaces_interface_ipv4_neighbor_element_t* new_element = NULL;

new_element = interfaces_interface_ipv4_neighbor_element_new();

interfaces_interface_ipv4_neighbor_element_set_ip(&new_element, dst_addr);
interfaces_interface_ipv4_neighbor_element_set_link_layer_address(&new_element, ll_addr);
interfaces_interface_ipv4_neighbor_add_element(neighbor, new_element);

return 0;
}

static int interfaces_add_neighbor_ipv6(interfaces_interface_ipv6_neighbor_element_t** neighbor, char *dst_addr, char *ll_addr)
{
interfaces_interface_ipv6_neighbor_element_t* new_element = NULL;

new_element = interfaces_interface_ipv6_neighbor_element_new();

interfaces_interface_ipv6_neighbor_element_set_ip(&new_element, dst_addr);
interfaces_interface_ipv6_neighbor_element_set_link_layer_address(&new_element, ll_addr);
interfaces_interface_ipv6_neighbor_add_element(neighbor, new_element);

return 0;
}

static int interfaces_add_neighbor(interfaces_interface_t* interface, char *dst_addr, char *ll_addr, int addr_family)
{
switch (addr_family) {
Expand Down Expand Up @@ -299,98 +242,20 @@ static int interfaces_get_interface_ip_neighbors(struct nl_sock* socket, struct
return interfaces_load_success;
}

static unsigned int interfaces_get_ipv4_mtu(struct rtnl_link* link, interfaces_interface_t* interface)
{
unsigned int mtu = 0;

mtu = rtnl_link_get_mtu(link);

interface->ipv4.mtu = mtu;

return 0;
}

static unsigned int interfaces_get_ipv6_mtu(struct rtnl_link* link, interfaces_interface_t* interface)
{
unsigned int mtu = 0;

mtu = rtnl_link_get_mtu(link);

interface->ipv6.mtu = mtu;

return 0;

}

static unsigned int interfaces_get_interface_ip_mtu(struct rtnl_link* link, interfaces_interface_t* interface)
{
interfaces_get_ipv4_mtu(link, interface);

interfaces_get_ipv6_mtu(link, interface);
}

static unsigned int interfaces_get_ipv4_enabled(interfaces_interface_t* interface)
{
const char *ipv4_base = "/proc/sys/net/ipv4/conf";

/* TODO: figure out how to enable/disable ipv4 */
/* since disable_ipv4 doesn't exist in /proc/sys/net/ipv6/conf/interface_name */

}

static unsigned int interfaces_get_ipv6_enabled(interfaces_interface_t* interface)
{
int error = 0;
int enabled = 0;

const char *ipv6_base = "/proc/sys/net/ipv6/conf";

SRPC_SAFE_CALL(read_from_proc_file(ipv6_base, interface->name, "disable_ipv6", &enabled), out);

interface->ipv6.enabled = enabled;

out:
return error;
}

static unsigned int interfaces_get_interface_ip_enabled(interfaces_interface_t* interface)
{
interfaces_get_ipv4_enabled(interface);

interfaces_get_ipv6_enabled(interface);
}

static unsigned int interfaces_get_ipv4_forwarding(interfaces_interface_t* interface)
{
int error = 0;
int forwarding = 0;

const char *ipv4_base = "/proc/sys/net/ipv4/conf";

SRPC_SAFE_CALL(read_from_proc_file(ipv4_base, interface->name, "forwarding", &forwarding), out);

interface->ipv4.forwarding = forwarding;

out:
return error;
}

static unsigned int interfaces_get_ipv6_forwarding(interfaces_interface_t* interface)
{
int error = 0;
int forwarding = 0;

const char *ipv6_base = "/proc/sys/net/ipv6/conf";

SRPC_SAFE_CALL(read_from_proc_file(ipv6_base, interface->name, "forwarding", &forwarding), out);

interface->ipv6.forwarding = forwarding;

out:
return error;
}


static unsigned int interfaces_get_interface_ip_forwarding(interfaces_interface_t* interface)
{
interfaces_get_ipv4_forwarding(interface);
Expand Down
5 changes: 1 addition & 4 deletions src/interfaces/src/plugin/api/interfaces/load.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
#ifndef INTERFACES_PLUGIN_API_INTERFACES_LOAD_H
#define INTERFACES_PLUGIN_API_INTERFACES_LOAD_H

#include "plugin/context.h"
#include "plugin/data/interfaces/interface.h"
#include "plugin/data/interfaces/interface/linked_list.h"
#include "plugin/common.h"

int interfaces_load_interface(interfaces_ctx_t* ctx, interfaces_interface_hash_element_t** if_hash);

#endif // INTERFACES_PLUGIN_API_INTERFACES_LOAD_H

0 comments on commit aabca20

Please sign in to comment.