创建启动虚拟网卡

本文通过C语言实现了一个简单的网络编程实例,展示了如何创建虚拟网络设备,并控制其启动与关闭状态。具体步骤包括使用系统调用打开TUN/TAP设备、设置设备名称以及通过ioctl系统调用来控制设备的状态。
#include <unistd.h>   
#include <stdio.h>   
#include <curses.h>   
#include <string.h>   
#include <assert.h>   
#include <sys/types.h>   
#include <sys/socket.h>   
#include <netinet/in.h>   
#include <signal.h>   
#include <unistd.h>   
#include <linux/if_tun.h>   
#include <netinet/in.h>   
#include <sys/ioctl.h>   
#include <sys/time.h>   
#include <linux/if.h>   
#include <netinet/in.h>   
#include <arpa/inet.h>   
#include <errno.h>  
#include <fcntl.h>

//启动网卡接口 
int SetEtherStatusUP(const char *ethNum)
{
 struct ifreq ifr;
 int sockfd;
 if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
 {
  printf("Create socket fails!\n");
  return -1; 
 }
 strcpy(ifr.ifr_name, ethNum);
 if (ioctl(sockfd, SIOCGIFFLAGS, &ifr) < 0)
 {
  printf("ioctl SIOCGIFFLAGS fails!\n");
  close(sockfd);
  return -1; 
 }
 ifr.ifr_flags |= IFF_UP;
 if (ioctl(sockfd, SIOCSIFFLAGS, &ifr) < 0)
 {
  printf("ioctl SIOCSIFFLAGS fails!\n");
  close(sockfd);
  return -1; 
 }
 close(sockfd);
 return 1;
}
//关闭网卡接口 
int SetEtherStatusDOWN(const char *ethNum)
{
 struct ifreq ifr;
 int sockfd;
 if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
 {
  printf("Create socket fails!\n");
  return -1; 
 }
}
int tun_create(char *dev, int flags)
{
    struct ifreq ifr;
    int fd, err;
    if ((fd = open("/dev/net/tun", O_RDWR)) < 0) {
        return fd;
    }
    memset(&ifr, 0, sizeof(ifr));
    ifr.ifr_flags |= flags;
    if (*dev != '\0')
        strncpy(ifr.ifr_name, dev, IFNAMSIZ);
    if ((err = ioctl(fd, TUNSETIFF, (void *)&ifr)) < 0)
    {
        close(fd);
        return err;
    }
    strcpy(dev, ifr.ifr_name);
    return fd;
}

int main()
{
 int tun4;
 char tun4_name[IFNAMSIZ]="tun4"; 
 //tun4_name[0]='\0';
 tun4 = tun_create(tun4_name, IFF_TAP | IFF_NO_PI);
 //tun4 = tun_create(tun4_name, IFF_TUN | IFF_NO_PI);
 printf("dev=%s\n",tun4_name);
 getchar();
 SetEtherStatusUP(tun4_name);
 printf("up\n");
 getchar();
 SetEtherStatusDOWN(tun4_name);
 printf("down\n");
 
 return 0;
}

转载于:https://my.oschina.net/guyson/blog/223383

虚拟网卡驱动源代码(原版): /* * 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
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值