Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1300078
  • 博文数量: 548
  • 博客积分: 7597
  • 博客等级: 少将
  • 技术积分: 4224
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-15 13:21
个人简介

嵌入式软件工程师&&太极拳

文章分类

全部博文(548)

文章存档

2014年(10)

2013年(76)

2012年(175)

2011年(287)

分类: 嵌入式

2012-04-24 15:40:30

#include
#include
#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;
 if (all == 1)
 {
  for (i; i < count; i++)
  {
   printf("%s\n", result->ifr_name);
   get_ipaddr(result->ifr_name);
   get_mac(result->ifr_name);
   get_netmask(result->ifr_name);
   get_broadcast(result->ifr_name);
  }
 } else
 {
  struct ifreq tmp;
  int s;
  if ((s = socket(PF_INET, SOCK_STREAM, 0)) < 0)
  {
   perror("Socket");
   exit(-1);
  }
  for (i; i < count; i++)
  {
   strcpy(tmp.ifr_name, result->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);
}
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;
}
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;
}
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;
}
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;
}
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;
}
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;
}
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 < 58 && mac_addr > 47)
   tmp = mac_addr - 48;
  if (mac_addr < 71 && mac_addr > 64)
   tmp = mac_addr - 55;
  if (mac_addr < 103 && mac_addr > 96)
   tmp = mac_addr - 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;
}

 
阅读(965) | 评论(0) | 转发(0) |
0

上一篇:时间程序

下一篇:tree无法显示中文

给主人留下些什么吧!~~