加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

linux – 如何使用nix的ioctl?

发布时间:2020-12-14 02:21:08 所属栏目:Linux 来源:网络整理
导读:我想从Rust那里打电话给ioctl.我知道我应该使用 the nix crate,但究竟是怎么回事?从文档中不清楚. 我有这个C: int tun_open(char *devname){ struct ifreq ifr; int fd,err; if ( (fd = open("/dev/net/tun",O_RDWR)) == -1 ) { perror("open /dev/net/tun
我想从Rust那里打电话给ioctl.我知道我应该使用 the nix crate,但究竟是怎么回事?从文档中不清楚.

我有这个C:

int tun_open(char *devname)
{
  struct ifreq ifr;
  int fd,err;

  if ( (fd = open("/dev/net/tun",O_RDWR)) == -1 ) {
       perror("open /dev/net/tun");exit(1);
  }
  memset(&ifr,sizeof(ifr));
  ifr.ifr_flags = IFF_TUN;
  strncpy(ifr.ifr_name,devname,IFNAMSIZ);  

  /* ioctl will use if_name as the name of TUN 
   * interface to open: "tun0",etc. */
  if ( (err = ioctl(fd,TUNSETIFF,(void *) &ifr)) == -1 ) {
    perror("ioctl TUNSETIFF");close(fd);exit(1);
  }
  //..........

我如何使用nix箱子做同样的事情? nix crate中没有TUN *常量,并且不清楚如何使用ioctl宏.

解决方法

在 rust-spidev中有一些示例用法.我将尝试将其应用于您的代码.

TUNSETIFF是defined:

#define TUNSETIFF     _IOW('T',202,int)

这将是使用nix的Rust中的这个:

const TUN_IOC_MAGIC: u8 = 'T' as u8;
const TUN_IOC_SET_IFF: u8 = 202;
ioctl!(write tun_set_iff with TUN_IOC_MAGIC,TUN_IOC_SET_IFF; u32);

上面的宏将定义函数,你可以这样调用:

let err = unsafe { tun_set_iff(fd,ifr) }; // assuming ifr is an u32

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读