address add 8.8.8.8 dev eno1 label eno1:10 scope link 实现代码

本文详细介绍了如何通过编程实现将IP地址8.8.8.8添加到设备的eno1接口,并创建名为eno1:10的子接口,同时设置为链路本地范围。这一过程对于网络配置和管理至关重要。
摘要由CSDN通过智能技术生成
#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

#ifndef INFINITY_LIFE_TIME
#define     INFINITY_LIFE_TIME      0xFFFFFFFFU
#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))

enum {
        PREFIXLEN_SPECIFIED     = (1 << 0),
        ADDRTYPE_INET           = (1 << 1),
        ADDRTYPE_UNSPEC         = (1 << 2),
        ADDRTYPE_MULTI          = (1 << 3),

        ADDRTYPE_INET_UNSPEC    = ADDRTYPE_INET | ADDRTYPE_UNSPEC,
        ADDRTYPE_INET_MULTI     = ADDRTYPE_INET | ADDRTYPE_MULTI
};

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);

typedef struct
{
        __u16 flags;
        __u16 bytelen;
        __s16 bitlen;
        /* These next two fields match rtvia */
        __u16 family;
        __u32 data[64];
} inet_prefix;

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;
      
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

胡致云

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

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

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

打赏作者

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

抵扣说明:

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

余额充值