NetworkInador/src/common.h

266 lines
5.4 KiB
C

/*
* common.h
* This file is part of Network-inador
*
* Copyright (C) 2019, 2020 - 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
*/
#ifndef __COMMON_H__
#define __COMMON_H__
#include <stdint.h>
#include <time.h>
#include <netinet/in.h>
#include <net/ethernet.h>
#include <linux/if.h>
#include <glib.h>
#include <gmodule.h>
#ifndef FALSE
#define FALSE 0
#endif
#ifndef TRUE
#define TRUE !FALSE
#endif
#ifndef INFINITY_LIFE_TIME
#define INFINITY_LIFE_TIME 0xFFFFFFFFU
#endif
#define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})
typedef struct _NetworkInadorHandle NetworkInadorHandle;
typedef struct _NetworkInadorManager NetworkInadorManager;
typedef struct _Interface Interface;
typedef union {
struct in_addr v4;
struct in6_addr v6;
} struct_addr;
typedef struct _IPAddr {
sa_family_t family;
uint8_t prefix;
struct_addr local_addr;
struct_addr addr;
struct_addr brd_addr;
char label[256];
struct ifa_cacheinfo cacheinfo;
uint8_t is_p2p;
uint8_t has_brd;
uint8_t has_local;
uint32_t flags;
uint8_t scope;
Interface *iface;
} IPAddr;
#define SSID_MAX_LEN 32
typedef struct _WirelessBSS {
/** Number of counts without seeing this BSS */
unsigned int scan_miss_count;
/** Index of the last scan update */
unsigned int last_update_idx;
/** BSSID */
uint8_t bssid[ETHER_ADDR_LEN * 2 + 1];
/** HESSID */
//u8 hessid[ETHER_ADDR_LEN * 2 + 1];
/** SSID */
uint8_t ssid[SSID_MAX_LEN];
/** Length of SSID */
size_t ssid_len;
/** Frequency of the channel in MHz (e.g., 2412 = channel 1) */
int freq;
/** Capability information field in host byte order */
uint16_t caps;
/** Timestamp of last Beacon/Probe Response frame */
uint64_t tsf;
/** Time of the last update (i.e., Beacon or Probe Response RX) */
struct timespec last_update;
} WirelessBSS;
typedef struct _WirelessInfo {
int phy;
uint32_t *freqs;
int num_freqs;
uint32_t caps;
gboolean can_scan;
gboolean can_scan_ssid;
gboolean supported;
unsigned int bss_update_idx;
GList *aps;
} WirelessInfo;
/* Información del proceso de DHCP */
enum {
IFACE_NO_DHCP = 0,
IFACE_ISC_DHCLIENT,
IFACE_BUSYBOX_UDHCPC
};
/* FIXME: Revisar estos estados */
enum {
DHCP_CLIENT_SELECTING,
DHCP_CLIENT_BOUND,
DHCP_CLIENT_RENEWED,
DHCP_CLIENT_EXPIRED,
DHCP_CLIENT_FAILED,
DHCP_CLIENT_KILLED,
};
#define DHCP_CLIENT_FLAG_AUTO_RESTART 0x0001
typedef struct _InterfaceDHCPClientInfo {
int type;
uint32_t flags;
int dhcp_state;
/* Para vigilar el proceso */
GPid process_pid;
guint process_watch;
/* La información obtenida desde el cliente */
struct_addr ip, broadcast;
struct_addr gateways[7], dns[7];
char domain_name[256];
int gw_c, dns_c;
int prefix;
struct_addr dhcp_server_ip;
uint32_t lease_time;
} InterfaceDHCPClientInfo;
struct _Interface {
NetworkInadorHandle *handle;
uint32_t index;
char name[IFNAMSIZ];
uint32_t link_type;
uint16_t arp_type;
unsigned char real_hw[ETHER_ADDR_LEN * 2 + 1];
/* Para las interfaces dentro de un bridge */
uint32_t master_index;
uint32_t mtu;
/* Para las interfaces vlan */
unsigned int vlan_parent;
/* Banderas estilo ioctl */
short flags;
int is_wireless;
char wireless_protocol[IFNAMSIZ];
/* Tipo */
char rtnl_type[IFNAMSIZ];
GList *address;
InterfaceDHCPClientInfo dhcpc;
/* Información wireless */
WirelessInfo *wireless;
};
/* Para los clientes y sus respectivos eventos */
typedef struct {
int fd;
/* Los eventos que quieren ser recibidos en este cliente */
uint32_t wanted_events;
guint source;
NetworkInadorManager *manager;
} ManagerClientInfo;
struct _NetworkInadorManager {
int socket;
guint source;
GList *connected_client_list;
NetworkInadorHandle *handle;
};
/* Para vigilar eventos */
typedef struct _netlink_event_pair {
struct nl_sock * nl_sock;
guint source;
} NetlinkEventPair;
typedef struct _RouteNH {
struct_addr gw;
uint32_t out_index;
uint8_t nh_weight;
uint8_t nh_flags;
} RouteNH;
/* La tabla de ruteo */
typedef struct _Route {
sa_family_t family; /* AF_INET, AF_INET6 */
uint8_t type; /* Unicast, local, broadcast, etc... */
uint32_t table;
struct_addr dest;
uint8_t prefix;
uint8_t protocol;
uint8_t tos;
uint8_t scope;
struct_addr prefsrc;
uint32_t priority;
/* Los brincos */
GList *nexthops;
} Route;
struct _NetworkInadorHandle {
GList *interfaces;
GList *route_v4_tables;
GList *route_v6_tables;
NetworkInadorManager *manager;
struct nl_sock * nl_sock_route;
struct nl_sock * nl_sock_nl80211;
NetlinkEventPair route_events;
NetlinkEventPair nl80211_scan;
NetlinkEventPair nl80211_scan_results;
};
#endif /* __COMMON_H__ */