GetandSetNetParamter.c
/*============================================================================
Name : GetandSetNetParamter.c
Author : Haier
Version : 1.01
Copyright : Copyright (c) 2014
Description : GetandSetNetParamter in C, Compile by Gcc
============================================================================*/
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <net/if_arp.h>
#include <string.h>
#include <linux/sockios.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
int main(int argc, char *argv[])
{
int fd;
int err = -1;
fd = socket(AF_INET, SOCK_DGRAM, 0);
if(fd < 0)
{
printf("Socket Error: %s\n", strerror(errno));
return -1;
}
{
struct ifreq ifr;
ifr.ifr_ifindex = 2;
err = ioctl(fd, SIOCGIFNAME, &ifr);
if(err)
{
printf("Ioctl Error: %s\n", strerror(errno));
return -1;
}
printf("the %dst interface is: %s\n", ifr.ifr_ifindex,ifr.ifr_name);
}
{
struct ifreq ifr;
memcpy(ifr.ifr_name, "eth1", 5);
err = ioctl(fd, SIOCGIFFLAGS, &ifr);
if(!err)
{
printf("SIOCGIFFLAGS: %s\n", ifr.ifr_flags&IFF_UP ? "IFF_UP" : "0");
}
err = ioctl(fd, SIOCGIFMETRIC, &ifr);
if(!err)
{
printf("SIOCGIFMETRIC: %d\n", ifr.ifr_metric);
}
err = ioctl(fd, SIOCGIFMTU, &ifr);
if(!err)
{
printf("SIOCGIFMTU: %d\n", ifr.ifr_mtu);
}
err = ioctl(fd, SIOCGIFHWADDR, &ifr);
if(!err)
{
unsigned char *hw = (unsigned char*)&ifr.ifr_hwaddr.sa_data;
printf("SIOCGIFHWADDR: %02X:%02X:%02X:%2X:%02X:%02X\n",hw[0],hw[1],hw[2],hw[3],hw[4],hw[5]);
}
err = ioctl(fd, SIOCGIFMAP, &ifr);
if(!err)
{
printf("SIOCGIFMAP, mem_start: %ld, mem_end: %ld, base_addr: %d, dma: %d, port: %d\n",
ifr.ifr_map.mem_start,
ifr.ifr_map.mem_end,
ifr.ifr_map.base_addr,
ifr.ifr_map.irq,
ifr.ifr_map.dma,
ifr.ifr_map.port
);
}
}
{
struct ifreq ifr;
struct sockaddr_in *sin = (struct sockaddr_in*)&ifr.ifr_addr;
char ip[16];
memset(ip, 0, 16);
memcpy(ifr.ifr_name, "eth1", 5);
err = ioctl(fd, SIOCGIFADDR, &ifr);
if(!err)
{
inet_ntop(AF_INET, &sin->sin_addr.s_addr, ip, 16);
printf("SIOCGIFADDR: %s\n", ip);
}
err = ioctl(fd, SIOCGIFBRDADDR, &ifr);
if(!err)
{
inet_ntop(AF_INET, &sin->sin_addr.s_addr, ip, 16);
printf("SIOCGIFBRDADDR: %s\n", ip);
}
err = ioctl(fd, SIOCGIFDSTADDR, &ifr);
if(!err)
{
inet_ntop(AF_INET, &sin->sin_addr.s_addr, ip, 16);
printf("SIOCGIFDSTADDR: %s\n", ip);
}
err = ioctl(fd, SIOCGIFNETMASK, &ifr);
if(!err)
{
inet_ntop(AF_INET, &sin->sin_addr.s_addr, ip, 16);
printf("SIOCGIFNETMASK: %s\n", ip);
}
}
{
struct ifreq ifr;
struct sockaddr_in *sin = (struct sockaddr_in *)&ifr.ifr_addr;
char ip[16];
int err = -1;
printf("Set Ip to 192.168.1.128\n");
memset(&ifr, 0, sizeof(ifr));
memcpy(ifr.ifr_name, "eth1", 5);
inet_pton(AF_INET, "192.168.1.128", &sin->sin_addr.s_addr);
sin->sin_family = AF_INET;
err = ioctl(fd, SIOCSIFADDR, &ifr);
if(err)
{
printf("SIOCSIFADDR Error: %s\n", strerror(errno));
}
printf("Current Ip : ");
memset(&ifr, 0, sizeof(ifr));
memcpy(ifr.ifr_name, "eth1", 5);
ioctl(fd, SIOCGIFADDR, &ifr);
inet_ntop(AF_INET, &sin->sin_addr.s_addr, ip, 16);
printf("%s\n", ip);
}
close(fd);
return 0;
}
运行示例:
[root@Linux Ping]# ./GetandSetNetParamter.o
the 2st interface is: eth1
SIOCGIFFLAGS: IFF_UP
SIOCGIFMETRIC: 0
SIOCGIFMTU: 1500
SIOCGIFHWADDR: 00:0C:29:AC:1B:2C
SIOCGIFMAP, mem_start: 0, mem_end: 0, base_addr: 8228, dma: 19, port: 0
SIOCGIFADDR: 192.168.1.128
SIOCGIFBRDADDR: 192.168.1.255
SIOCGIFDSTADDR: 192.168.1.128
SIOCGIFNETMASK: 255.255.255.0
Set Ip to 192.168.1.128
Current Ip : 192.168.1.128