[ip link set dev ethx up]实现

#include <stdio.h>
#include <ctype.h>
#include <stdbool.h>
#include <stdlib.h>
#include <unistd.h>
#include <inttypes.h>
#include <fcntl.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/param.h>
#include <errno.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <fnmatch.h>
#include <time.h>

#include <linux/netdevice.h>
#include <linux/if_arp.h>
#include <linux/sockios.h>
#include <linux/net_namespace.h>
#include <linux/rtnetlink.h>

#define IPLINK_IOCTL_COMPAT	1
#ifndef LIBDIR
#define LIBDIR "/usr/lib"
#endif

#ifndef GSO_MAX_SIZE
#define GSO_MAX_SIZE		65536
#endif
#ifndef GSO_MAX_SEGS
#define GSO_MAX_SEGS		65535
#endif

#define IDXMAP_SIZE 1024
//已定义在netlink.h
//#define NLMSG_ALIGNTO 4U
//#define NLMSG_ALIGN(len) (((len) + NLMSG_ALIGNTO - 1) & ~(NLMSG_ALIGNTO - 1))
//#define NLMSG_ALIGN(len) (((len) + 4U - 1) & ~(4U - 1))
#define NLMSG_TAIL(nmsg) ((struct rtattr *)(((void *)(nmsg)) + NLMSG_ALIGN((nmsg)->nlmsg_len)))
#define rtnl_dump_filter(rth, filter, arg) rtnl_dump_filter_nc(rth, filter, arg, 0)
#define hlist_for_each(pos, head) for (pos = (head)->first; pos; pos = pos->next)
#define offsetof(type, member) ((size_t) & ((type *)0)->member)
#define container_of(ptr, type, member) ({  const typeof( ((type *)0)->member ) *__mptr = (ptr); (type *)( (char *)__mptr - offsetof(type,member) ); })
#define format_host_rta(af, rta) format_host(af, RTA_PAYLOAD(rta), RTA_DATA(rta))

typedef int (*rtnl_filter_t)(struct nlmsghdr *n, void *);
typedef int (*req_filter_fn_t)(struct nlmsghdr *nlh, int reqlen);
typedef int (*nl_ext_ack_fn_t)(const char *errmsg, uint32_t off, const struct nlmsghdr *inner_nlh);

struct iplink_req {
   
        struct nlmsghdr         n;
        struct ifinfomsg        i;
        char                    buf[1024];
};

struct rtnl_handle
{
   
        int fd;
        struct sockaddr_nl local;
        struct sockaddr_nl peer;
        __u32 seq;
        __u32 dump;
        int proto;
        FILE *dump_fp;
#define RTNL_HANDLE_F_LISTEN_ALL_NSID 0x01
#define RTNL_HANDLE_F_SUPPRESS_NLERR 0x02
        int flags;
};

enum output_type
{
   
        PRINT_FP = 1,
        PRINT_JSON = 2,
        PRINT_ANY = 4,
};

struct rtnl_dump_filter_arg
{
   
        rtnl_filter_t filter;
        void *arg1;
        __u16 nc_flags;
};

struct nlmsg_list
{
   
        struct nlmsg_list *next;
        struct nlmsghdr h;
};

struct nlmsg_chain
{
   
        struct nlmsg_list *head;
        struct nlmsg_list *tail;
};

struct hlist_head
{
   
        struct hlist_node *first;
};

struct hlist_node
{
   
        struct hli
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

胡致云

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值