C 代码激活网卡并配置IP与子网掩码

1、background

        最近项目中需要接入tun,我将wireguard中的tun模块(GO版本)摘了出来,在里面拔了半天没发现有设置网络地址信息的接口,只有根据interfaceName创建tun的接口,所以剩下的事情只能自己做了:激活网卡,设置IP和子网掩码。

        我使用的C编写的接口,使用Go的import C调用。

2、代码

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <net/route.h>
int setIfaceAddress(unsigned char *ifname, unsigned char *ipAddr, unsigned char *mask)
{
 int fd, res = 0;
 struct ifreq ifr;
 struct sockaddr_in *sin;
 struct rtentry  rt;
 fd = socket(AF_INET, SOCK_DGRAM, 0);
 memset(&ifr,0,sizeof(ifr));
 strcpy(ifr.ifr_name,ifname);

 //set nic up
 ioctl(fd, SIOCGIFFLAGS, &ifr);
 ifr.ifr_flags |= IFF_UP | IFF_RUNNING;
 // disable nic
 // ifr.ifr_flags &= (!IFF_UP)
 ioctl(fd, SIOCSIFFLAGS, &ifr);

 sin = (struct sockaddr_in*)&ifr.ifr_addr;
 sin->sin_family = AF_INET;

 //set ip
 inet_aton(Ipaddr,&(sin->sin_addr));
 res = ioctl(fd,SIOCSIFADDR,&ifr);
 if (res < 0) return -1;

 //set netmask
 inet_aton(mask,&(sin->sin_addr));
 res = ioctl(fd, SIOCSIFNETMASK, &ifr);
 if (res < 0) return -2;

 close(fd);
 return res;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
虚拟网卡驱动源代码(原版): /* * snull.c -- the Simple Network Utility * * Copyright (C) 2001 Alessandro Rubini and Jonathan Corbet * Copyright (C) 2001 O'Reilly & Associates * * The source code in this file can be freely used, adapted, * and redistributed in source or binary form, so long as an * acknowledgment appears in derived source files. The citation * should list that the code comes from the book "Linux Device * Drivers" by Alessandro Rubini and Jonathan Corbet, published * by O'Reilly & Associates. No warranty is attached; * we cannot take responsibility for errors or fitness for use. * * $Id: snull.c,v 1.21 2004/11/05 02:36:03 rubini Exp $ */ #include #include #include #include #include #include /* printk() */ #include /* kmalloc() */ #include /* error codes */ #include /* size_t */ #include /* mark_bh */ #include #include /* struct device, and other headers */ #include /* eth_type_trans */ #include /* struct iphdr */ #include /* struct tcphdr */ #include #include "snull.h" #include #include MODULE_AUTHOR("Alessandro Rubini, Jonathan Corbet"); MODULE_LICENSE("Dual BSD/GPL"); /* * Transmitter lockup simulation, normally disabled. */ static int lockup = 0; module_param(lockup, int, 0); static int timeout = SNULL_TIMEOUT; module_param(timeout, int, 0); /* * Do we run in NAPI mode? */ static int use_napi = 0; module_param(use_napi, int, 0); /* * A structure representing an in-flight packet. */ struct snull_packet { struct snull_packet *next; struct net_device *dev; int datalen; u8 data[ETH_DATA_LEN]; }; int pool_size = 8; module_param(pool_size, int, 0); /* * This structure is private to each device. It is used to

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

我要出家当道士

打赏是不可能,这辈子都不可能

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

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

打赏作者

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

抵扣说明:

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

余额充值