⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 使用ioctl来读取本地ip和netmask值
💻 C
字号:
#include <stdio.h>#include <stdlib.h>#include <errno.h>#include <string.h>#include <unistd.h>#include <stdarg.h>#include <netdb.h>#include <sys/socket.h>#include <sys/types.h>#include <sys/stat.h>#include <sys/ioctl.h>#include <linux/if.h>#include <arpa/inet.h>#include <fcntl.h>#include <netinet/in.h>#include <linux/if_arp.h>#include <linux/if_ether.h>int main(){    struct ifreq req;    strncpy( req.ifr_name, "eth0", IFNAMSIZ);    struct sockaddr_in *sin;    int fd = socket(AF_INET, SOCK_DGRAM, 0);        if( fd < 0 ){        perror("error: ");        return -1;    }    sin = (struct sockaddr_in *)&req.ifr_addr;    if( ioctl( fd, SIOCGIFADDR, &req) == 0 )        printf("local ip %s\n", inet_ntoa(sin->sin_addr) );    else        perror("ioctl error: ");    sin = (struct sockaddr_in *)&req.ifr_broadaddr;    if( ioctl( fd, SIOCGIFBRDADDR, &req) == 0 )        printf("ip broadcast %s\n", inet_ntoa(sin->sin_addr) );    else        perror("ioctl error: ");    sin = (struct sockaddr_in *)&req.ifr_dstaddr;    if( ioctl( fd, SIOCGIFDSTADDR, &req) == 0 )        printf("dst ip %s\n", inet_ntoa(sin->sin_addr) );    else        perror("ioctl error: ");    sin = (struct sockaddr_in *)&req.ifr_netmask;    if( ioctl( fd, SIOCGIFNETMASK, &req) == 0 )        printf("net mask %s\n", inet_ntoa(sin->sin_addr) );    else        perror("ioctl error: ");        close( fd );    return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -