原文地址:http://blog.csdn.net/lqrensn/article/details/8078676
-
/*Linux ifconfig,c简易源代码*/
-
-
#include
-
#include
-
#include
-
#include
-
#include
-
#include
-
int info_list(int all);
-
int get_interface_info(char * interface_name);
-
int set_interface(char * interface_name);
-
int get_ipaddr(char * interface_name);
-
int get_mac(char * interface_name);
-
int get_netmask(char * interface_name);
-
int get_broadcast(char * interface_name);
-
int set_ipaddr(char * interface_name,char * ip);
-
int set_mac(char * interface_name,char * mac);
-
int set_netmask(char * interface_name,char * netmask);
-
int set_broadcast(char * interface_name,char * broadcast);
-
int interface_up(char * interface_name);
-
int interface_down(char * interface_name);
-
int usage();
-
int main(int argc,char * argv[])
-
{
-
if(argc == 1)
-
{
-
info_list(0);
-
return 0;
-
}
-
if(argc == 2)
-
{
-
if(strcmp(argv[1],"all") == 0)
-
{
-
info_list(1);
-
return 0;
-
}
-
get_interface_info(argv[1]);
-
return 0;
-
}
-
if(argc == 3)
-
{
-
if(strcmp(argv[2],"set") == 0)
-
{
-
set_interface(argv[1]);
-
return 0;
-
}
-
if(strcmp(argv[2],"up") == 0)
-
{
-
interface_up(argv[1]);
-
return 0;
-
}
-
if(strcmp(argv[2],"down") == 0)
-
{
-
interface_down(argv[1]);
-
return 0;
-
}
-
usage();
-
return 0;
-
}
-
usage();
-
return 0;
-
}
-
int info_list(int all)
-
{
-
int len;
-
char *ifreq_pointer;
-
len = 10*sizeof(struct ifreq);
-
ifreq_pointer = (char *)malloc(len);
-
struct ifconf get_info;
-
get_info.ifc_len = len;
-
get_info.ifc_ifcu.ifcu_buf = ifreq_pointer;
-
int sockfd;
-
sockfd = socket(PF_INET,SOCK_STREAM,0);
-
ioctl(sockfd,SIOCGIFCONF,&get_info);
-
int count;
-
//获取网络接口数
-
count = get_info.ifc_len / sizeof(struct ifreq);
-
struct sockaddr_in * return_sockaddr;
-
struct ifreq * result = (struct ifreq *)ifreq_pointer;
-
int i = 0;
-
//获取myifconfig all的参数
-
if(all == 1)
-
for(i;i
-
{
-
printf("%s\n",result[i].ifr_name);
-
get_ipaddr(result[i].ifr_name);
-
get_mac(result[i].ifr_name);
-
get_netmask(result[i].ifr_name);
-
get_broadcast(result[i].ifr_name);
-
}
-
else
-
{
-
struct ifreq tmp;
-
int s;
-
if((s = socket(PF_INET,SOCK_STREAM,0)) < 0)
-
{
-
perror("Socket");
-
exit(-1);
-
}
-
//获取每个接口参数
-
for(i;i
-
{
-
strcpy(tmp.ifr_name,result[i].ifr_name);
-
if(ioctl(s,SIOCGIFFLAGS,&tmp) < 0)
-
{
-
perror("ioctl");
-
exit(-1);
-
}
-
if((tmp.ifr_ifru.ifru_flags & IFF_UP) == 0)
-
continue;
-
printf("%s\n",tmp.ifr_name);
-
get_ipaddr(tmp.ifr_name);
-
get_mac(tmp.ifr_name);
-
get_netmask(tmp.ifr_name);
-
get_broadcast(tmp.ifr_name);
-
}
-
}
-
exit(0);
-
}
-
//获取IP:SIOCGIFADDR
-
int get_ipaddr(char * interface_name)
-
{
-
int s;
-
if((s = socket(PF_INET,SOCK_STREAM,0)) < 0)
-
{
-
perror("Socket");
-
return -1;
-
}
-
struct ifreq ifr;
-
strcpy(ifr.ifr_name,interface_name);
-
if(ioctl(s,SIOCGIFADDR,&ifr) < 0)
-
{
-
perror("ioctl");
-
return -1;
-
}
-
struct sockaddr_in * ptr;
-
ptr = (struct sockaddr_in *)&ifr.ifr_ifru.ifru_addr;
-
printf(" IP:%s\n",inet_ntoa(ptr->sin_addr));
-
return 0;
-
}
-
//获取MAC:SIOCGIFHWADDR
-
int get_mac(char * interface_name)
-
{
-
if(strcmp(interface_name,"lo") == 0)
-
{
-
return 0;
-
}
-
int s;
-
if((s = socket(PF_INET,SOCK_STREAM,0)) < 0)
-
{
-
perror("Socket");
-
return -1;
-
}
-
struct ifreq ifr;
-
strcpy(ifr.ifr_name,interface_name);
-
if(ioctl(s,SIOCGIFHWADDR,&ifr) != 0)
-
{
-
perror("ioctl");
-
return -1;
-
}
-
u_char * ptr;
-
ptr =(u_char *)&ifr.ifr_ifru.ifru_hwaddr.sa_data[0];
-
printf(" MAC:%02x-%02x-%02x-%02x-%02x-%02x\n",*ptr,*(ptr+1),*(ptr+2),*(ptr+3),*(ptr+4),*(ptr+5));
-
return 0;
-
}
-
//获取netmask:SIOCGIFNETMASK
-
int get_netmask(char * interface_name)
-
{
-
int s;
-
if((s = socket(PF_INET,SOCK_STREAM,0)) < 0)
-
{
-
perror("Socket");
-
return -1;
-
}
-
struct ifreq ifr;
-
strcpy(ifr.ifr_name,interface_name);
-
if(ioctl(s,SIOCGIFNETMASK,&ifr) < 0)
-
{
-
perror("ioctl");
-
return -1;
-
}
-
struct sockaddr_in * ptr;
-
ptr = (struct sockaddr_in *)&ifr.ifr_ifru.ifru_netmask;
-
printf(" Netmask:%s\n",inet_ntoa(ptr->sin_addr));
-
return 0;
-
}
-
//获取广播地址:SIOCGIFBRDADDR
-
int get_broadcast(char * interface_name)
-
{
-
int s;
-
if((s = socket(PF_INET,SOCK_STREAM,0)) < 0)
-
{
-
perror("Socket");
-
return -1;
-
}
-
struct ifreq ifr;
-
strcpy(ifr.ifr_name,interface_name);
-
if(ioctl(s,SIOCGIFBRDADDR,&ifr) < 0)
-
{
-
perror("ioctl");
-
return -1;
-
}
-
struct sockaddr_in * ptr;
-
ptr = (struct sockaddr_in *)&ifr.ifr_ifru.ifru_broadaddr;
-
printf(" Broadcast:%s\n",inet_ntoa(ptr->sin_addr));
-
return 0;
-
}
-
//获取接口信息
-
int get_interface_info(char * interface_name)
-
{
-
get_ipaddr(interface_name);
-
get_mac(interface_name);
-
get_netmask(interface_name);
-
get_broadcast(interface_name);
-
return 0;
-
}
-
//设置接口信息
-
int set_interface(char * interface_name)
-
{
-
char ip[18];
-
char mac_addr[20];
-
char netmask[18];
-
char broadcast[18];
-
printf("Change ");
-
get_ipaddr(interface_name);
-
printf("To: ");
-
fgets(ip,17,stdin);
-
if(strcmp(ip,"\n") == 0)
-
goto set_mac;
-
set_ipaddr(interface_name,ip);
-
set_mac:
-
printf("Change ");
-
get_mac(interface_name);
-
printf("To: ");
-
fgets(mac_addr,19,stdin);
-
if(strcmp(mac_addr,"\n") == 0)
-
goto set_netmask;
-
set_mac(interface_name,mac_addr);
-
set_netmask:
-
printf("Change ");
-
get_netmask(interface_name);
-
printf("To: ");
-
fgets(netmask,17,stdin);
-
if(strcmp(netmask,"\n") == 0)
-
goto set_broadcast;
-
set_netmask(interface_name,netmask);
-
set_broadcast:
-
printf("Change ");
-
get_broadcast(interface_name);
-
printf("To: ");
-
fgets(broadcast,17,stdin);
-
if(strcmp(broadcast,"\n") == 0)
-
return 0;
-
set_broadcast(interface_name,broadcast);
-
return 0;
-
}
-
//ifconfig up接口:SIOCSIFFLAGS
-
int interface_up(char * interface_name)
-
{
-
int s;
-
if((s = socket(PF_INET,SOCK_STREAM,0)) < 0)
-
{
-
perror("Socket");
-
return -1;
-
}
-
struct ifreq ifr;
-
strcpy(ifr.ifr_name,interface_name);
-
short flag;
-
flag = IFF_UP;
-
if(ioctl(s,SIOCGIFFLAGS,&ifr) < 0)
-
{
-
perror("ioctl");
-
return -1;
-
}
-
ifr.ifr_ifru.ifru_flags |= flag;
-
if(ioctl(s,SIOCSIFFLAGS,&ifr) < 0)
-
{
-
perror("ioctl");
-
return -1;
-
}
-
return 0;
-
}
-
//关闭接口
-
int interface_down(char * interface_name)
-
{
-
if(strcmp(interface_name,"lo") == 0)
-
{
-
printf("You can't pull down interface lo\n");
-
return 0;
-
}
-
int s;
-
if((s = socket(PF_INET,SOCK_STREAM,0)) < 0)
-
{
-
perror("Socket");
-
return -1;
-
}
-
struct ifreq ifr;
-
strcpy(ifr.ifr_name,interface_name);
-
short flag;
-
flag = ~IFF_UP;
-
if(ioctl(s,SIOCGIFFLAGS,&ifr) < 0)
-
{
-
perror("ioctl");
-
return -1;
-
}
-
ifr.ifr_ifru.ifru_flags &= flag;
-
if(ioctl(s,SIOCSIFFLAGS,&ifr) < 0)
-
{
-
perror("ioctl");
-
return -1;
-
}
-
return 0;
-
}
-
//设置IP
-
int set_ipaddr(char * interface_name,char * ip)
-
{
-
int s;
-
if((s = socket(PF_INET,SOCK_STREAM,0)) < 0)
-
{
-
perror("Socket");
-
return -1;
-
}
-
struct ifreq ifr;
-
strcpy(ifr.ifr_name,interface_name);
-
struct sockaddr_in addr;
-
bzero(&addr,sizeof(struct sockaddr_in));
-
addr.sin_family = PF_INET;
-
inet_aton(ip,&addr.sin_addr);
-
memcpy(&ifr.ifr_ifru.ifru_addr,&addr,sizeof(struct sockaddr_in));
-
if(ioctl(s,SIOCSIFADDR,&ifr) < 0)
-
{
-
perror("ioctl");
-
return -1;
-
}
-
return 0;
-
}
-
//设置MAC
-
int set_mac(char * interface_name,char * mac_addr)
-
{
-
int s;
-
if((s = socket(PF_INET,SOCK_STREAM,0)) < 0)
-
{
-
perror("Socket");
-
return -1;
-
}
-
struct ifreq ifr;
-
strcpy(ifr.ifr_name,interface_name);
-
if(ioctl(s,SIOCGIFHWADDR,&ifr) < 0)
-
{
-
perror("ioctl");
-
return -1;
-
}
-
sa_family_t get_family;
-
get_family = ifr.ifr_ifru.ifru_hwaddr.sa_family;
-
interface_down(interface_name);
-
bzero(&ifr,sizeof(struct ifreq));
-
strcpy(ifr.ifr_name,interface_name);
-
ifr.ifr_ifru.ifru_hwaddr.sa_family = get_family;
-
short tmp;
-
int i,j;
-
j = 0;
-
for(i = 0;i < 17; i += 3)
-
{
-
if(mac_addr[i] < 58 && mac_addr[i] > 47)
-
tmp = mac_addr[i]-48;
-
if(mac_addr[i] < 71 && mac_addr[i] > 64)
-
tmp = mac_addr[i]-55;
-
if(mac_addr[i] < 103 && mac_addr[i] > 96)
-
tmp = mac_addr[i]-87;
-
tmp = tmp << 4;
-
if(mac_addr[i+1] < 58 && mac_addr[i+1] > 47)
-
tmp |= (mac_addr[i+1]-48);
-
if(mac_addr[i+1] < 71 && mac_addr[i+1] > 64)
-
tmp |= (mac_addr[i+1]-55);
-
if(mac_addr[i+1] < 103 && mac_addr[i+1] > 96)
-
tmp |= (mac_addr[i+1]-87);
-
memcpy(&ifr.ifr_ifru.ifru_hwaddr.sa_data[j++],&tmp,1);
-
}
-
if(ioctl(s,SIOCSIFHWADDR,&ifr) < 0)
-
{
-
perror("ioctl");
-
return -1;
-
}
-
interface_up(interface_name);
-
return 0;
-
}
-
//设置掩码
-
int set_netmask(char * interface_name,char * netmask)
-
{
-
int s;
-
if((s = socket(PF_INET,SOCK_STREAM,0)) < 0)
-
{
-
perror("Socket");
-
return -1;
-
}
-
struct ifreq ifr;
-
strcpy(ifr.ifr_name,interface_name);
-
struct sockaddr_in netmask_addr;
-
bzero(&netmask_addr,sizeof(struct sockaddr_in));
-
netmask_addr.sin_family = PF_INET;
-
inet_aton(netmask,&netmask_addr.sin_addr);
-
memcpy(&ifr.ifr_ifru.ifru_netmask,&netmask_addr,sizeof(struct sockaddr_in));
-
if(ioctl(s,SIOCSIFNETMASK,&ifr) < 0)
-
{
-
perror("ioctl");
-
return -1;
-
}
-
return 0;
-
}
-
//设置广播地址
-
int set_broadcast(char * interface_name,char * broadcast)
-
{
-
int s;
-
if((s = socket(PF_INET,SOCK_STREAM,0)) < 0)
-
{
-
perror("Socket");
-
return -1;
-
}
-
struct ifreq ifr;
-
strcpy(ifr.ifr_name,interface_name);
-
struct sockaddr_in broadcast_addr;
-
bzero(&broadcast_addr,sizeof(struct sockaddr_in));
-
broadcast_addr.sin_family = PF_INET;
-
inet_aton(broadcast,&broadcast_addr.sin_addr);
-
memcpy(&ifr.ifr_ifru.ifru_broadaddr,&broadcast_addr,sizeof(struct sockaddr_in));
-
if(ioctl(s,SIOCSIFBRDADDR,&ifr) < 0)
-
{
-
perror("ioctl");
-
return -1;
-
}
-
return 0;
-
}
-
int usage(void)
-
{
-
printf("Usage: myifconfig [all]|[interface [set|up|down]]\n");
-
return 0;
-
}
阅读(1592) | 评论(0) | 转发(0) |