181 lines
4.7 KiB
C
181 lines
4.7 KiB
C
#ifndef IPCONFIG_H_
|
|
#define IPCONFIG_H_
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <stdlib.h>
|
|
#include <errno.h>
|
|
#include <unistd.h>
|
|
#include <sys/types.h>
|
|
#include <sys/socket.h>
|
|
#include <netinet/in.h>
|
|
#include <arpa/inet.h>
|
|
#include <sys/ioctl.h>
|
|
#include <net/if.h>
|
|
#include <linux/ethtool.h>
|
|
#include <linux/sockios.h>
|
|
|
|
#define MAX_IF_NUM 32
|
|
#define DOT_IP_STR 32
|
|
#define DOT_IPV6_STR 64
|
|
#define IF_INFO_STR 32
|
|
|
|
#define INTERFACE_NAMSIZ 20
|
|
/* Max bit/byte length of IPv4 address. */
|
|
#define IPV4_MAX_BYTELEN 4
|
|
#define IPV4_MAX_BITLEN 32
|
|
#define IPV4_MAX_PREFIXLEN 32
|
|
#define IPV4_DEFAULT_PREFIXLEN 16
|
|
#define IPV4_DEFAULT_NETMASK "255.255.0.0"
|
|
|
|
#define IPV4_ADDR_CMP(D,S) memcmp ((D), (S), IPV4_MAX_BYTELEN)
|
|
|
|
#define IPV4_ADDR_COPY(D,S) ipv4_addr_copy((D), (S))
|
|
|
|
#define IPV4_NET0(a) ((((uint)(a)) & 0xff000000) == 0x00000000)
|
|
#define IPV4_NET127(a) ((((uint)(a)) & 0xff000000) == 0x7f000000)
|
|
#define IPV4_LINKLOCAL(a) ((((uint)(a)) & 0xffff0000) == 0xa9fe0000)
|
|
#define IPV4_CLASS_DE(a) ((((uint)(a)) & 0xe0000000) == 0xe0000000)
|
|
#define IPV4_MC_LINKLOCAL(a) ((((uint)(a)) & 0xffffff00) == 0xe0000000)
|
|
#define IPV4_ADDR_SAME(A,B) ipv4_addr_same((A), (B))
|
|
|
|
#define IPV4_ADDR_CMP(D,S) memcmp ((D), (S), IPV4_MAX_BYTELEN)
|
|
|
|
#define IPV6_MAX_BYTELEN 16
|
|
#define IPV6_MAX_PREFIXLEN 128
|
|
#define IPV6_DEFAULT_PREFIXLEN 64
|
|
#define IPV6_ADDR_NUM 8
|
|
|
|
#define IPV6_ADDR_CMP(D,S) memcmp ((D), (S), IPV6_MAX_BYTELEN)
|
|
|
|
static inline boolean ipv4_addr_same(const struct in_addr *a,
|
|
const struct in_addr *b)
|
|
{
|
|
return (a->s_addr == b->s_addr);
|
|
}
|
|
|
|
|
|
static inline void ipv4_addr_copy(struct in_addr *dst,
|
|
const struct in_addr *src)
|
|
{
|
|
dst->s_addr = src->s_addr;
|
|
}
|
|
|
|
/* NOTE: This routine expects the address argument in network byte order. */
|
|
static inline int ipv4_martian(struct in_addr *addr)
|
|
{
|
|
in_addr_t ip = ntohl(addr->s_addr);
|
|
|
|
if (IPV4_NET0(ip) || IPV4_NET127(ip) || IPV4_CLASS_DE(ip))
|
|
{
|
|
return TRUE;
|
|
}
|
|
|
|
return FALSE;
|
|
}
|
|
|
|
static inline int ipv6_martian(struct in6_addr *ip)
|
|
{
|
|
if (IN6_IS_ADDR_LOOPBACK(ip) || IN6_IS_ADDR_LINKLOCAL(ip) || IN6_IS_ADDR_MULTICAST(ip))
|
|
{
|
|
return TRUE;
|
|
}
|
|
|
|
return FALSE;
|
|
}
|
|
|
|
struct _if_v4addr_str {
|
|
char addr[DOT_IP_STR];
|
|
char prefixlen[DOT_IP_STR];
|
|
};
|
|
typedef struct _if_v4addr_str v4addr_str;
|
|
|
|
struct _if_v6addr_str {
|
|
char addr[DOT_IPV6_STR];
|
|
char prefixlen[IF_INFO_STR];
|
|
};
|
|
typedef struct _if_v6addr_str v6addr_str;
|
|
|
|
|
|
struct _v4addr_str {
|
|
struct in_addr addr;
|
|
struct in_addr mask;
|
|
};
|
|
typedef struct _v4addr_str v4addr_t;
|
|
|
|
struct _v6addr_str {
|
|
struct in6_addr addr;
|
|
int prefixlen;
|
|
};
|
|
|
|
typedef struct _v6addr_str v6addr_t;
|
|
|
|
/* 配置消息 */
|
|
struct _ip_config_str {
|
|
char ifname[INTERFACE_NAMSIZ];
|
|
uchar family;
|
|
v4addr_t ipv4;
|
|
v6addr_t ipv6;
|
|
};
|
|
|
|
struct _ip_config_string {
|
|
int config_type;
|
|
char ifname[INTERFACE_NAMSIZ];
|
|
int family;
|
|
char ipaddr[DOT_IPV6_STR];
|
|
char prefixlen[DOT_IPV6_STR];
|
|
};
|
|
|
|
typedef struct _ip_config_str ip_config_t;
|
|
typedef struct _ip_config_string ip_config_string_t;
|
|
|
|
struct _if_count_str {
|
|
char rcv_packets[IF_INFO_STR];
|
|
char snd_packets[IF_INFO_STR];
|
|
char rcv_bytes[IF_INFO_STR];
|
|
char snd_bytes[IF_INFO_STR];
|
|
};
|
|
typedef struct _if_count_str if_cnt_t;
|
|
|
|
struct _if_info_str {
|
|
char ifname[INTERFACE_NAMSIZ];
|
|
char ipenable[IF_INFO_STR];
|
|
char v4protocol[IF_INFO_STR];
|
|
char v6protocol[IF_INFO_STR];
|
|
char role[INTERFACE_NAMSIZ];
|
|
char maxspeed[IF_INFO_STR];
|
|
char hwaddr[IF_INFO_STR];
|
|
char state[IF_INFO_STR];
|
|
v4addr_str ipv4;
|
|
v6addr_str ipv6[IPV6_ADDR_NUM];
|
|
if_cnt_t stats;
|
|
};
|
|
typedef struct _if_info_str if_info_t;
|
|
|
|
|
|
void masklen2ip(const int masklen, struct in_addr *netmask);
|
|
uchar ip_masklen(struct in_addr netmask);
|
|
|
|
|
|
/* 业务模块函数声明 */
|
|
|
|
/* ip config */
|
|
ret_code ip_config_chk(uint source, uint *config_type,
|
|
pointer input, int *input_len,
|
|
pointer output, int *output_len);
|
|
|
|
ret_code ip_config_proc(uint source, uint config_type,
|
|
pointer input, int input_len,
|
|
pointer output, int *output_len);
|
|
|
|
ret_code ip_config_get(uint source,
|
|
pointer input, int input_len,
|
|
pointer output, int *output_len);
|
|
|
|
ret_code ip_config_get_all(uint source,
|
|
pointer output, int *output_len);
|
|
|
|
|
|
|
|
#endif /* IPCONFIG_H_ */
|
|
|