Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3170390
  • 博文数量: 685
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 5303
  • 用 户 组: 普通用户
  • 注册时间: 2014-04-19 14:17
个人简介

文章分类

全部博文(685)

文章存档

2015年(116)

2014年(569)

分类: 嵌入式

2014-09-18 15:31:13

原文地址:http://blog.csdn.net/lqrensn/article/details/8078676
  1. /*Linux ifconfig,c简易源代码*/
  2. #include
  3. #include
  4. #include
  5. #include
  6. #include
  7. #include
  8. int info_list(int all);
  9. int get_interface_info(char * interface_name);
  10. int set_interface(char * interface_name);
  11. int get_ipaddr(char * interface_name);
  12. int get_mac(char * interface_name);
  13. int get_netmask(char * interface_name);
  14. int get_broadcast(char * interface_name);
  15. int set_ipaddr(char * interface_name,char * ip);
  16. int set_mac(char * interface_name,char * mac);
  17. int set_netmask(char * interface_name,char * netmask);
  18. int set_broadcast(char * interface_name,char * broadcast);
  19. int interface_up(char * interface_name);
  20. int interface_down(char * interface_name);
  21. int usage();
  22. int main(int argc,char * argv[])
  23. {
  24. if(argc == 1)
  25. {
  26. info_list(0);
  27. return 0;
  28. }
  29. if(argc == 2)
  30. {
  31. if(strcmp(argv[1],"all") == 0)
  32. {
  33. info_list(1);
  34. return 0;
  35. }
  36. get_interface_info(argv[1]);
  37. return 0;
  38. }
  39. if(argc == 3)
  40. {
  41. if(strcmp(argv[2],"set") == 0)
  42. {
  43. set_interface(argv[1]);
  44. return 0;
  45. }
  46. if(strcmp(argv[2],"up") == 0)
  47. {
  48. interface_up(argv[1]);
  49. return 0;
  50. }
  51. if(strcmp(argv[2],"down") == 0)
  52. {
  53. interface_down(argv[1]);
  54. return 0;
  55. }
  56. usage();
  57. return 0;
  58. }
  59. usage();
  60. return 0;
  61. }
  62. int info_list(int all)
  63. {
  64. int len;
  65. char *ifreq_pointer;
  66. len = 10*sizeof(struct ifreq);
  67. ifreq_pointer = (char *)malloc(len);
  68. struct ifconf get_info;
  69. get_info.ifc_len = len;
  70. get_info.ifc_ifcu.ifcu_buf = ifreq_pointer;
  71. int sockfd;
  72. sockfd = socket(PF_INET,SOCK_STREAM,0);
  73. ioctl(sockfd,SIOCGIFCONF,&get_info);
  74. int count;
  75. //获取网络接口数
  76. count = get_info.ifc_len / sizeof(struct ifreq);
  77. struct sockaddr_in * return_sockaddr;
  78. struct ifreq * result = (struct ifreq *)ifreq_pointer;
  79. int i = 0;
  80. //获取myifconfig all的参数
  81. if(all == 1)
  82. for(i;i
  83. {
  84. printf("%s\n",result[i].ifr_name);
  85. get_ipaddr(result[i].ifr_name);
  86. get_mac(result[i].ifr_name);
  87. get_netmask(result[i].ifr_name);
  88. get_broadcast(result[i].ifr_name);
  89. }
  90. else
  91. {
  92. struct ifreq tmp;
  93. int s;
  94. if((s = socket(PF_INET,SOCK_STREAM,0)) < 0)
  95. {
  96. perror("Socket");
  97. exit(-1);
  98. }
  99. //获取每个接口参数
  100. for(i;i
  101. {
  102. strcpy(tmp.ifr_name,result[i].ifr_name);
  103. if(ioctl(s,SIOCGIFFLAGS,&tmp) < 0)
  104. {
  105. perror("ioctl");
  106. exit(-1);
  107. }
  108. if((tmp.ifr_ifru.ifru_flags & IFF_UP) == 0)
  109. continue;
  110. printf("%s\n",tmp.ifr_name);
  111. get_ipaddr(tmp.ifr_name);
  112. get_mac(tmp.ifr_name);
  113. get_netmask(tmp.ifr_name);
  114. get_broadcast(tmp.ifr_name);
  115. }
  116. }
  117. exit(0);
  118. }
  119. //获取IP:SIOCGIFADDR
  120. int get_ipaddr(char * interface_name)
  121. {
  122. int s;
  123. if((s = socket(PF_INET,SOCK_STREAM,0)) < 0)
  124. {
  125. perror("Socket");
  126. return -1;
  127. }
  128. struct ifreq ifr;
  129. strcpy(ifr.ifr_name,interface_name);
  130. if(ioctl(s,SIOCGIFADDR,&ifr) < 0)
  131. {
  132. perror("ioctl");
  133. return -1;
  134. }
  135. struct sockaddr_in * ptr;
  136. ptr = (struct sockaddr_in *)&ifr.ifr_ifru.ifru_addr;
  137. printf(" IP:%s\n",inet_ntoa(ptr->sin_addr));
  138. return 0;
  139. }
  140. //获取MAC:SIOCGIFHWADDR
  141. int get_mac(char * interface_name)
  142. {
  143. if(strcmp(interface_name,"lo") == 0)
  144. {
  145. return 0;
  146. }
  147. int s;
  148. if((s = socket(PF_INET,SOCK_STREAM,0)) < 0)
  149. {
  150. perror("Socket");
  151. return -1;
  152. }
  153. struct ifreq ifr;
  154. strcpy(ifr.ifr_name,interface_name);
  155. if(ioctl(s,SIOCGIFHWADDR,&ifr) != 0)
  156. {
  157. perror("ioctl");
  158. return -1;
  159. }
  160. u_char * ptr;
  161. ptr =(u_char *)&ifr.ifr_ifru.ifru_hwaddr.sa_data[0];
  162. printf(" MAC:%02x-%02x-%02x-%02x-%02x-%02x\n",*ptr,*(ptr+1),*(ptr+2),*(ptr+3),*(ptr+4),*(ptr+5));
  163. return 0;
  164. }
  165. //获取netmask:SIOCGIFNETMASK
  166. int get_netmask(char * interface_name)
  167. {
  168. int s;
  169. if((s = socket(PF_INET,SOCK_STREAM,0)) < 0)
  170. {
  171. perror("Socket");
  172. return -1;
  173. }
  174. struct ifreq ifr;
  175. strcpy(ifr.ifr_name,interface_name);
  176. if(ioctl(s,SIOCGIFNETMASK,&ifr) < 0)
  177. {
  178. perror("ioctl");
  179. return -1;
  180. }
  181. struct sockaddr_in * ptr;
  182. ptr = (struct sockaddr_in *)&ifr.ifr_ifru.ifru_netmask;
  183. printf(" Netmask:%s\n",inet_ntoa(ptr->sin_addr));
  184. return 0;
  185. }
  186. //获取广播地址:SIOCGIFBRDADDR
  187. int get_broadcast(char * interface_name)
  188. {
  189. int s;
  190. if((s = socket(PF_INET,SOCK_STREAM,0)) < 0)
  191. {
  192. perror("Socket");
  193. return -1;
  194. }
  195. struct ifreq ifr;
  196. strcpy(ifr.ifr_name,interface_name);
  197. if(ioctl(s,SIOCGIFBRDADDR,&ifr) < 0)
  198. {
  199. perror("ioctl");
  200. return -1;
  201. }
  202. struct sockaddr_in * ptr;
  203. ptr = (struct sockaddr_in *)&ifr.ifr_ifru.ifru_broadaddr;
  204. printf(" Broadcast:%s\n",inet_ntoa(ptr->sin_addr));
  205. return 0;
  206. }
  207. //获取接口信息
  208. int get_interface_info(char * interface_name)
  209. {
  210. get_ipaddr(interface_name);
  211. get_mac(interface_name);
  212. get_netmask(interface_name);
  213. get_broadcast(interface_name);
  214. return 0;
  215. }
  216. //设置接口信息
  217. int set_interface(char * interface_name)
  218. {
  219. char ip[18];
  220. char mac_addr[20];
  221. char netmask[18];
  222. char broadcast[18];
  223. printf("Change ");
  224. get_ipaddr(interface_name);
  225. printf("To: ");
  226. fgets(ip,17,stdin);
  227. if(strcmp(ip,"\n") == 0)
  228. goto set_mac;
  229. set_ipaddr(interface_name,ip);
  230. set_mac:
  231. printf("Change ");
  232. get_mac(interface_name);
  233. printf("To: ");
  234. fgets(mac_addr,19,stdin);
  235. if(strcmp(mac_addr,"\n") == 0)
  236. goto set_netmask;
  237. set_mac(interface_name,mac_addr);
  238. set_netmask:
  239. printf("Change ");
  240. get_netmask(interface_name);
  241. printf("To: ");
  242. fgets(netmask,17,stdin);
  243. if(strcmp(netmask,"\n") == 0)
  244. goto set_broadcast;
  245. set_netmask(interface_name,netmask);
  246. set_broadcast:
  247. printf("Change ");
  248. get_broadcast(interface_name);
  249. printf("To: ");
  250. fgets(broadcast,17,stdin);
  251. if(strcmp(broadcast,"\n") == 0)
  252. return 0;
  253. set_broadcast(interface_name,broadcast);
  254. return 0;
  255. }
  256. //ifconfig up接口:SIOCSIFFLAGS
  257. int interface_up(char * interface_name)
  258. {
  259. int s;
  260. if((s = socket(PF_INET,SOCK_STREAM,0)) < 0)
  261. {
  262. perror("Socket");
  263. return -1;
  264. }
  265. struct ifreq ifr;
  266. strcpy(ifr.ifr_name,interface_name);
  267. short flag;
  268. flag = IFF_UP;
  269. if(ioctl(s,SIOCGIFFLAGS,&ifr) < 0)
  270. {
  271. perror("ioctl");
  272. return -1;
  273. }
  274. ifr.ifr_ifru.ifru_flags |= flag;
  275. if(ioctl(s,SIOCSIFFLAGS,&ifr) < 0)
  276. {
  277. perror("ioctl");
  278. return -1;
  279. }
  280. return 0;
  281. }
  282. //关闭接口
  283. int interface_down(char * interface_name)
  284. {
  285. if(strcmp(interface_name,"lo") == 0)
  286. {
  287. printf("You can't pull down interface lo\n");
  288. return 0;
  289. }
  290. int s;
  291. if((s = socket(PF_INET,SOCK_STREAM,0)) < 0)
  292. {
  293. perror("Socket");
  294. return -1;
  295. }
  296. struct ifreq ifr;
  297. strcpy(ifr.ifr_name,interface_name);
  298. short flag;
  299. flag = ~IFF_UP;
  300. if(ioctl(s,SIOCGIFFLAGS,&ifr) < 0)
  301. {
  302. perror("ioctl");
  303. return -1;
  304. }
  305. ifr.ifr_ifru.ifru_flags &= flag;
  306. if(ioctl(s,SIOCSIFFLAGS,&ifr) < 0)
  307. {
  308. perror("ioctl");
  309. return -1;
  310. }
  311. return 0;
  312. }
  313. //设置IP
  314. int set_ipaddr(char * interface_name,char * ip)
  315. {
  316. int s;
  317. if((s = socket(PF_INET,SOCK_STREAM,0)) < 0)
  318. {
  319. perror("Socket");
  320. return -1;
  321. }
  322. struct ifreq ifr;
  323. strcpy(ifr.ifr_name,interface_name);
  324. struct sockaddr_in addr;
  325. bzero(&addr,sizeof(struct sockaddr_in));
  326. addr.sin_family = PF_INET;
  327. inet_aton(ip,&addr.sin_addr);
  328. memcpy(&ifr.ifr_ifru.ifru_addr,&addr,sizeof(struct sockaddr_in));
  329. if(ioctl(s,SIOCSIFADDR,&ifr) < 0)
  330. {
  331. perror("ioctl");
  332. return -1;
  333. }
  334. return 0;
  335. }
  336. //设置MAC
  337. int set_mac(char * interface_name,char * mac_addr)
  338. {
  339. int s;
  340. if((s = socket(PF_INET,SOCK_STREAM,0)) < 0)
  341. {
  342. perror("Socket");
  343. return -1;
  344. }
  345. struct ifreq ifr;
  346. strcpy(ifr.ifr_name,interface_name);
  347. if(ioctl(s,SIOCGIFHWADDR,&ifr) < 0)
  348. {
  349. perror("ioctl");
  350. return -1;
  351. }
  352. sa_family_t get_family;
  353. get_family = ifr.ifr_ifru.ifru_hwaddr.sa_family;
  354. interface_down(interface_name);
  355. bzero(&ifr,sizeof(struct ifreq));
  356. strcpy(ifr.ifr_name,interface_name);
  357. ifr.ifr_ifru.ifru_hwaddr.sa_family = get_family;
  358. short tmp;
  359. int i,j;
  360. j = 0;
  361. for(i = 0;i < 17; i += 3)
  362. {
  363. if(mac_addr[i] < 58 && mac_addr[i] > 47)
  364. tmp = mac_addr[i]-48;
  365. if(mac_addr[i] < 71 && mac_addr[i] > 64)
  366. tmp = mac_addr[i]-55;
  367. if(mac_addr[i] < 103 && mac_addr[i] > 96)
  368. tmp = mac_addr[i]-87;
  369. tmp = tmp << 4;
  370. if(mac_addr[i+1] < 58 && mac_addr[i+1] > 47)
  371. tmp |= (mac_addr[i+1]-48);
  372. if(mac_addr[i+1] < 71 && mac_addr[i+1] > 64)
  373. tmp |= (mac_addr[i+1]-55);
  374. if(mac_addr[i+1] < 103 && mac_addr[i+1] > 96)
  375. tmp |= (mac_addr[i+1]-87);
  376. memcpy(&ifr.ifr_ifru.ifru_hwaddr.sa_data[j++],&tmp,1);
  377. }
  378. if(ioctl(s,SIOCSIFHWADDR,&ifr) < 0)
  379. {
  380. perror("ioctl");
  381. return -1;
  382. }
  383. interface_up(interface_name);
  384. return 0;
  385. }
  386. //设置掩码
  387. int set_netmask(char * interface_name,char * netmask)
  388. {
  389. int s;
  390. if((s = socket(PF_INET,SOCK_STREAM,0)) < 0)
  391. {
  392. perror("Socket");
  393. return -1;
  394. }
  395. struct ifreq ifr;
  396. strcpy(ifr.ifr_name,interface_name);
  397. struct sockaddr_in netmask_addr;
  398. bzero(&netmask_addr,sizeof(struct sockaddr_in));
  399. netmask_addr.sin_family = PF_INET;
  400. inet_aton(netmask,&netmask_addr.sin_addr);
  401. memcpy(&ifr.ifr_ifru.ifru_netmask,&netmask_addr,sizeof(struct sockaddr_in));
  402. if(ioctl(s,SIOCSIFNETMASK,&ifr) < 0)
  403. {
  404. perror("ioctl");
  405. return -1;
  406. }
  407. return 0;
  408. }
  409. //设置广播地址
  410. int set_broadcast(char * interface_name,char * broadcast)
  411. {
  412. int s;
  413. if((s = socket(PF_INET,SOCK_STREAM,0)) < 0)
  414. {
  415. perror("Socket");
  416. return -1;
  417. }
  418. struct ifreq ifr;
  419. strcpy(ifr.ifr_name,interface_name);
  420. struct sockaddr_in broadcast_addr;
  421. bzero(&broadcast_addr,sizeof(struct sockaddr_in));
  422. broadcast_addr.sin_family = PF_INET;
  423. inet_aton(broadcast,&broadcast_addr.sin_addr);
  424. memcpy(&ifr.ifr_ifru.ifru_broadaddr,&broadcast_addr,sizeof(struct sockaddr_in));
  425. if(ioctl(s,SIOCSIFBRDADDR,&ifr) < 0)
  426. {
  427. perror("ioctl");
  428. return -1;
  429. }
  430. return 0;
  431. }
  432. int usage(void)
  433. {
  434. printf("Usage: myifconfig [all]|[interface [set|up|down]]\n");
  435. return 0;
  436. }  


阅读(1603) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~