zoukankan      html  css  js  c++  java
  • c/c++ 网络编程 UDP 改变网关和网卡名字

    网络编程 UDP 改变网关和网卡名字

    在程序里动态改变网关和网卡名字

    1,改变网卡名字

    #include <stdio.h>
    #include <string.h>
    #include <unistd.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <sys/ioctl.h>
    #include <netinet/in.h>
    #include <net/if.h>
    
    int main(){
      int fd;
      ifreq ifr;
    
      fd = socket(AF_INET, SOCK_DGRAM, 0);
    
      strncpy(ifr.ifr_name, "lo", IFNAMSIZ - 1);
      strncpy(ifr.ifr_newname, "newloname", IFNAMSIZ - 1);
      if(ioctl(fd, SIOCSIFNAME, &ifr) != 0){
        perror("ioctl");
        return 1;
      }
      close(fd);
      return 0;
    }
    
    

    github源代码

    2,改变网关

    #include <stdio.h>
    #include <string.h>
    #include <unistd.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <sys/ioctl.h>
    #include <netinet/in.h>
    #include <net/if.h>
    #include <arpa/inet.h>
    
    int main(){
      int fd;
      ifreq ifr;
      sockaddr_in *s_in;
    
      fd = socket(AF_INET, SOCK_DGRAM, 0);
    
      s_in = (sockaddr_in*)&ifr.ifr_addr;
    
      s_in->sin_family = AF_INET;
      inet_pton(AF_INET, "255.0.0.0", &s_in->sin_addr);
    
      strncpy(ifr.ifr_name, "enp0s3", IFNAMSIZ - 1);
    
      if(ioctl(fd, SIOCSIFNETMASK, &ifr) != 0){
        perror("ioctl");
        return 1;
      }
      close(fd);
      return 0;
    }
    
    

    github源代码

    c/c++ 学习互助QQ群:877684253

    本人微信:xiaoshitou5854

  • 相关阅读:
    Set和Multiset 怎么用咧↓↓↓
    sql server 复习笔记2
    sql server 复习笔记1
    数据分析相关学习 -1 numpy
    复习2
    scrapy 4 学习 crawl spider
    scrapy3 中间件的使用
    scapy2 爬取全站,以及使用post请求
    复习1
    scrapy 学习笔记2 数据持久化
  • 原文地址:https://www.cnblogs.com/xiaoshiwang/p/9795806.html
Copyright © 2011-2022 走看看