一个类似ifconfig功能的程序代码--C语言(ZZ)
时间:2010-12-27 来源:netrangering
摘自http://hi.baidu.com/zengzhaonong/blog/item/30836c8d9329cb19b31bba8f.html
001 #include <sys/ioctl.h> 002 #include <net/if.h> 003 #include <sys/socket.h> 004 #include <sys/types.h> 005 #include <stdio.h> 006 #include <netinet/in.h> 007 #include <string.h> 008 #include <stdlib.h> 009 010 int info_list(int all); 011 012 int get_interface_info(char *interface_name); 013 int set_interface(char *interface_name); 014 int get_ipaddr(char *interface_name); 015 int get_mac(char *interface_name); 016 int get_netmask(char *interface_name); 017 int get_broadcast(char *interface_name); 018 int set_ipaddr(char *interface_name, char *ip); 019 int set_mac(char *interface_name, char *mac); 020 int set_netmask(char *interface_name, char *netmask); 021 int set_broadcast(char *interface_name, char *broadcast); 022 023 int interface_up(char *interface_name); 024 int interface_down(char *interface_name); 025 026 int usage(); 027 028 int main(int argc, char *argv[]) 029 { 030 if (argc == 1) 031 { 032 info_list(0); 033 return 0; 034 } 035 036 if (argc == 2) 037 { 038 if (strcmp(argv[1], "all") == 0) 039 { 040 info_list(1); 041 return 0; 042 } 043 044 get_interface_info(argv[1]); 045 046 return 0; 047 } 048 049 if (argc == 3) 050 { 051 if (strcmp(argv[2], "set") == 0) 052 { 053 set_interface(argv[1]); 054 return 0; 055 } 056 if (strcmp(argv[2], "up") == 0) 057 { 058 interface_up(argv[1]); 059 return 0; 060 } 061 if (strcmp(argv[2], "down") == 0) 062 { 063 interface_down(argv[1]); 064 return 0; 065 } 066 067 usage(); 068 069 return 0; 070 } 071 072 usage(); 073 074 return 0; 075 } 076 077 int info_list(int all) 078 { 079 int len; 080 char *ifreq_pointer; 081 len = 10 * sizeof(struct ifreq); 082 ifreq_pointer = (char *) malloc(len); 083 084 struct ifconf get_info; 085 get_info.ifc_len = len; 086 get_info.ifc_ifcu.ifcu_buf = ifreq_pointer; 087 088 int sockfd; 089 sockfd = socket(PF_INET, SOCK_STREAM, 0); 090 ioctl(sockfd, SIOCGIFCONF, &get_info); 091 092 int count; 093 count = get_info.ifc_len / sizeof(struct ifreq); 094 095 struct sockaddr_in *return_sockaddr; 096 097 struct ifreq *result = (struct ifreq *) ifreq_pointer; 098 int i = 0; 099 if (all == 1) 100 { 101 for (i; i < count; i++) 102 { 103 printf("%s\n", result->ifr_name); 104 get_ipaddr(result->ifr_name); 105 get_mac(result->ifr_name); 106 get_netmask(result->ifr_name); 107 get_broadcast(result->ifr_name); 108 } 109 } else 110 { 111 struct ifreq tmp; 112 int s; 113 if ((s = socket(PF_INET, SOCK_STREAM, 0)) < 0) 114 { 115 perror("Socket"); 116 exit(-1); 117 } 118 119 for (i; i < count; i++) 120 { 121 strcpy(tmp.ifr_name, result->ifr_name); 122 if (ioctl(s, SIOCGIFFLAGS, &tmp) < 0) 123 { 124 perror("ioctl"); 125 exit(-1); 126 } 127 if ((tmp.ifr_ifru.ifru_flags & IFF_UP) == 0) 128 continue; 129 printf("%s\n", tmp.ifr_name); 130 get_ipaddr(tmp.ifr_name); 131 get_mac(tmp.ifr_name); 132 get_netmask(tmp.ifr_name); 133 get_broadcast(tmp.ifr_name); 134 } 135 } 136 137 exit(0); 138 } 139 140 int get_ipaddr(char *interface_name) 141 { 142 int s; 143 if ((s = socket(PF_INET, SOCK_STREAM, 0)) < 0) 144 { 145 perror("Socket"); 146 return -1; 147 } 148 149 struct ifreq ifr; 150 strcpy(ifr.ifr_name, interface_name); 151 152 if (ioctl(s, SIOCGIFADDR, &ifr) < 0) 153 { 154 perror("ioctl"); 155 return -1; 156 } 157 158 struct sockaddr_in *ptr; 159 ptr = (struct sockaddr_in *) &ifr.ifr_ifru.ifru_addr; 160 161 printf(" IP:%s\n", inet_ntoa(ptr->sin_addr)); 162 163 return 0; 164 } 165 166 int get_mac(char *interface_name) 167 { 168 if (strcmp(interface_name, "lo") == 0) 169 { 170 return 0; 171 } 172 173 int s; 174 175 if ((s = socket(PF_INET, SOCK_STREAM, 0)) < 0) 176 { 177 perror("Socket"); 178 return -1; 179 } 180 181 struct ifreq ifr; 182 183 strcpy(ifr.ifr_name, interface_name); 184 185 if (ioctl(s, SIOCGIFHWADDR, &ifr) != 0) 186 { 187 perror("ioctl"); 188 return -1; 189 } 190 191 u_char *ptr; 192 ptr = (u_char *) & ifr.ifr_ifru.ifru_hwaddr.sa_data[0]; 193 printf(" MAC:%02x-%02x-%02x-%02x-%02x-%02x\n", *ptr, *(ptr + 1), 194 *(ptr + 2), *(ptr + 3), *(ptr + 4), *(ptr + 5)); 195 196 return 0; 197 } 198 199 int get_netmask(char *interface_name) 200 { 201 int s; 202 203 if ((s = socket(PF_INET, SOCK_STREAM, 0)) < 0) 204 { 205 perror("Socket"); 206 return -1; 207 } 208 209 struct ifreq ifr; 210 strcpy(ifr.ifr_name, interface_name); 211 212 if (ioctl(s, SIOCGIFNETMASK, &ifr) < 0) 213 { 214 perror("ioctl"); 215 return -1; 216 } 217 218 struct sockaddr_in *ptr; 219 ptr = (struct sockaddr_in *) &ifr.ifr_ifru.ifru_netmask; 220 221 printf(" Netmask:%s\n", inet_ntoa(ptr->sin_addr)); 222 223 return 0; 224 } 225 226 int get_broadcast(char *interface_name) 227 { 228 int s; 229 230 if ((s = socket(PF_INET, SOCK_STREAM, 0)) < 0) 231 { 232 perror("Socket"); 233 return -1; 234 } 235 236 struct ifreq ifr; 237 strcpy(ifr.ifr_name, interface_name); 238 239 if (ioctl(s, SIOCGIFBRDADDR, &ifr) < 0) 240 { 241 perror("ioctl"); 242 return -1; 243 } 244 245 struct sockaddr_in *ptr; 246 ptr = (struct sockaddr_in *) &ifr.ifr_ifru.ifru_broadaddr; 247 248 printf(" Broadcast:%s\n", inet_ntoa(ptr->sin_addr)); 249 250 return 0; 251 252 } 253 254 int get_interface_info(char *interface_name) 255 { 256 get_ipaddr(interface_name); 257 get_mac(interface_name); 258 get_netmask(interface_name); 259 get_broadcast(interface_name); 260 261 return 0; 262 } 263 264 int set_interface(char *interface_name) 265 { 266 char ip[18]; 267 char mac_addr[20]; 268 char netmask[18]; 269 char broadcast[18]; 270 271 printf("Change "); 272 get_ipaddr(interface_name); 273 printf("To: "); 274 fgets(ip, 17, stdin); 275 if (strcmp(ip, "\n") == 0) 276 goto set_mac; 277 set_ipaddr(interface_name, ip); 278 279 set_mac: 280 printf("Change "); 281 get_mac(interface_name); 282 printf("To: "); 283 fgets(mac_addr, 19, stdin); 284 if (strcmp(mac_addr, "\n") == 0) 285 goto set_netmask; 286 set_mac(interface_name, mac_addr); 287 288 set_netmask: 289 printf("Change "); 290 get_netmask(interface_name); 291 printf("To: "); 292 fgets(netmask, 17, stdin); 293 if (strcmp(netmask, "\n") == 0) 294 goto set_broadcast; 295 set_netmask(interface_name, netmask); 296 297 set_broadcast: 298 printf("Change "); 299 get_broadcast(interface_name); 300 printf("To: "); 301 fgets(broadcast, 17, stdin); 302 if (strcmp(broadcast, "\n") == 0) 303 return 0; 304 set_broadcast(interface_name, broadcast); 305 306 return 0; 307 } 308 309 int interface_up(char *interface_name) 310 { 311 int s; 312 313 if ((s = socket(PF_INET, SOCK_STREAM, 0)) < 0) 314 { 315 perror("Socket"); 316 return -1; 317 } 318 319 struct ifreq ifr; 320 strcpy(ifr.ifr_name, interface_name); 321 322 short flag; 323 flag = IFF_UP; 324 if (ioctl(s, SIOCGIFFLAGS, &ifr) < 0) 325 { 326 perror("ioctl"); 327 return -1; 328 } 329 330 ifr.ifr_ifru.ifru_flags |= flag; 331 332 if (ioctl(s, SIOCSIFFLAGS, &ifr) < 0) 333 { 334 perror("ioctl"); 335 return -1; 336 } 337 338 return 0; 339 340 } 341 342 int interface_down(char *interface_name) 343 { 344 if (strcmp(interface_name, "lo") == 0) 345 { 346 printf("You can't pull down interface lo\n"); 347 return 0; 348 } 349 350 int s; 351 352 if ((s = socket(PF_INET, SOCK_STREAM, 0)) < 0) 353 { 354 perror("Socket"); 355 return -1; 356 } 357 358 struct ifreq ifr; 359 strcpy(ifr.ifr_name, interface_name); 360 361 short flag; 362 flag = ~IFF_UP; 363 if (ioctl(s, SIOCGIFFLAGS, &ifr) < 0) 364 { 365 perror("ioctl"); 366 return -1; 367 } 368 369 ifr.ifr_ifru.ifru_flags &= flag; 370 371 if (ioctl(s, SIOCSIFFLAGS, &ifr) < 0) 372 { 373 perror("ioctl"); 374 return -1; 375 } 376 377 return 0; 378 } 379 380 int set_ipaddr(char *interface_name, char *ip) 381 { 382 int s; 383 384 if ((s = socket(PF_INET, SOCK_STREAM, 0)) < 0) 385 { 386 perror("Socket"); 387 return -1; 388 } 389 390 struct ifreq ifr; 391 strcpy(ifr.ifr_name, interface_name); 392 393 struct sockaddr_in addr; 394 bzero(&addr, sizeof(struct sockaddr_in)); 395 addr.sin_family = PF_INET; 396 inet_aton(ip, &addr.sin_addr); 397 398 memcpy(&ifr.ifr_ifru.ifru_addr, &addr, sizeof(struct sockaddr_in)); 399 400 if (ioctl(s, SIOCSIFADDR, &ifr) < 0) 401 { 402 perror("ioctl"); 403 return -1; 404 } 405 406 return 0; 407 } 408 409 int set_mac(char *interface_name, char *mac_addr) 410 { 411 int s; 412 if ((s = socket(PF_INET, SOCK_STREAM, 0)) < 0) 413 { 414 perror("Socket"); 415 return -1; 416 } 417 418 struct ifreq ifr; 419 strcpy(ifr.ifr_name, interface_name); 420 421 if (ioctl(s, SIOCGIFHWADDR, &ifr) < 0) 422 { 423 perror("ioctl"); 424 return -1; 425 } 426 427 sa_family_t get_family; 428 get_family = ifr.ifr_ifru.ifru_hwaddr.sa_family; 429 430 interface_down(interface_name); 431 432 bzero(&ifr, sizeof(struct ifreq)); 433 strcpy(ifr.ifr_name, interface_name); 434 ifr.ifr_ifru.ifru_hwaddr.sa_family = get_family; 435 436 short tmp; 437 int i, j; 438 j = 0; 439 for (i = 0; i < 17; i += 3) 440 { 441 if (*mac_addr < 58 && *mac_addr > 47) 442 tmp = *mac_addr - 48; 443 if (*mac_addr < 71 && *mac_addr > 64) 444 tmp = *mac_addr - 55; 445 if (*mac_addr < 103 && *mac_addr > 96) 446 tmp = *mac_addr - 87; 447 tmp = tmp << 4; 448 if (mac_addr[i + 1] < 58 && mac_addr[i + 1] > 47) 449 tmp |= (mac_addr[i + 1] - 48); 450 if (mac_addr[i + 1] < 71 && mac_addr[i + 1] > 64) 451 tmp |= (mac_addr[i + 1] - 55); 452 if (mac_addr[i + 1] < 103 && mac_addr[i + 1] > 96) 453 tmp |= (mac_addr[i + 1] - 87); 454 memcpy(&ifr.ifr_ifru.ifru_hwaddr.sa_data[j++], &tmp, 1); 455 } 456 457 if (ioctl(s, SIOCSIFHWADDR, &ifr) < 0) 458 { 459 perror("ioctl"); 460 return -1; 461 } 462 463 interface_up(interface_name); 464 465 return 0; 466 } 467 468 int set_netmask(char *interface_name, char *netmask) 469 { 470 int s; 471 472 if ((s = socket(PF_INET, SOCK_STREAM, 0)) < 0) 473 { 474 perror("Socket"); 475 return -1; 476 } 477 478 struct ifreq ifr; 479 strcpy(ifr.ifr_name, interface_name); 480 481 struct sockaddr_in netmask_addr; 482 bzero(&netmask_addr, sizeof(struct sockaddr_in)); 483 netmask_addr.sin_family = PF_INET; 484 inet_aton(netmask, &netmask_addr.sin_addr); 485 486 memcpy(&ifr.ifr_ifru.ifru_netmask, &netmask_addr, 487 sizeof(struct sockaddr_in)); 488 489 if (ioctl(s, SIOCSIFNETMASK, &ifr) < 0) 490 { 491 perror("ioctl"); 492 return -1; 493 } 494 495 return 0; 496 497 } 498 499 int set_broadcast(char *interface_name, char *broadcast) 500 { 501 int s; 502 503 if ((s = socket(PF_INET, SOCK_STREAM, 0)) < 0) 504 { 505 perror("Socket"); 506 return -1; 507 } 508 509 struct ifreq ifr; 510 strcpy(ifr.ifr_name, interface_name); 511 512 struct sockaddr_in broadcast_addr; 513 bzero(&broadcast_addr, sizeof(struct sockaddr_in)); 514 broadcast_addr.sin_family = PF_INET; 515 inet_aton(broadcast, &broadcast_addr.sin_addr); 516 517 memcpy(&ifr.ifr_ifru.ifru_broadaddr, &broadcast_addr, 518 sizeof(struct sockaddr_in)); 519 520 if (ioctl(s, SIOCSIFBRDADDR, &ifr) < 0) 521 { 522 perror("ioctl"); 523 return -1; 524 } 525 526 return 0; 527 } 528 529 int usage(void) 530 { 531 printf("Usage: myifconfig [all]|[interface [set|up|down]]\n"); 532 return 0; 533 } 把每行开头的空格换成0 :%s/^\ /0/ 去掉每行开头的行号(数字) :%s/^[0-9]*// |
相关阅读 更多 +