ifcfg-eth0文件参数PREFIX 和 NETMASK的配置不一致问题

http://blog.csdn.net/aikui0621/article/details/9148997

搭建一个简单的网络测试环境,现有服务器ip配置为10.131.4.33 掩码为255.255.254.0网关为 10.131.5.254, 需要访问 
ip为10.1.7.110 的服务器,需要手动配置ifcfg-eth0文件,系统中自带了个PREFIX参数,由于不知道什么用就没有删除,
结果在修改NETMASK的过程中悲剧就发生了。
原始系统自带配置文件(为接手此服务器时的配置文件)如下:
 
 
由于不太了解PREFIX参数具体作用,就没有删除,仅修改IPADDR、NETMASK和GATEWAY,修改后如下:
 
 
执行 $services network restart 后 执行 $ ifconfig eth0 会发现netmask 项值并没有更改,而是255.0.0.0
一直找不到原因,进入图形查看网络设备信息发现 掩码项 为配置值 255.255.254.0 (此处未截图)
百思不得其解,不知道为什么配置正常后,掩码没有修改,反复检查配置文件后依然无果,无奈之下,写了脚本,
在重启网卡后,手动键入命令 $ ifconfig eth0 netmask 255.255.254.0 up   发现 $ ifconfig eth0 显示掩码配置正常,
但是依然ping不通 10.1.7.110的服务器,进入了死胡同,网上查找不到相关问题,最后尝试将配置文件精简到最简,版本如下:
 
发现奇迹般的正常了,可以ping同10.1.7.110服务器,问题解决,但是此时一肚子的疑惑, 开始查找原因!
首先对比发现前后配置文件缺失一个关键选项 PREFIX。回过头来注意此选项怀疑跟掩码有关,经验证果然如此,
在PREFIX设置8-23之间的值时,$ ifconfig eth0发现 netmask会根据此值相应改变,配置项NETMASK并未生效。但是
在将PREFIX项配置错误(即NETMASK为255.255.254.0时,PREFIX值在24-32)时,执行 $services network restart 时报错,
出错信息如下:
 
 
查找到一个相关的帖子,地址如下:
不过其中有个问题是仅配置PREFIX项不配置NETMASK会出现不稳定问题,未进行测试。现在就剩下当配置PREFIX时,
NETMASK选项会不生效的问题,先查看 /etc/sysconfig/network-scripts/network-functions 脚本发现如下代码:
 
[plain] view plaincopy
 
  1. 133 expand_config ()  
  2. 134 {  
  3. 135     if [ -z "${NETMASK}" ]; then  
  4. 136         eval `/bin/ipcalc --netmask ${IPADDR}`  
  5. 137     fi  
  6. 138   
  7. 139     if [ -z "${PREFIX}" ]; then  
  8. 140         eval `/bin/ipcalc --prefix ${IPADDR} ${NETMASK}`  
  9. 141     fi  
  10. 142   
  11. 143     if [ -z "${BROADCAST}" ]; then  
  12. 144         eval `/bin/ipcalc --broadcast ${IPADDR} ${NETMASK}`  
  13. 145     fi  
  14. 146   
  15. 147     if [ -z "${NETWORK}" ]; then  
  16. 148         eval `/bin/ipcalc --network ${IPADDR} ${NETMASK}`  
  17. 149     fi  
  18. 150 }  
 
发现脚本会读取ifcfg-eth0中配置项,作为ipcalc工具的参数进行配置,接下来查找ipcalc工具源码,发现有prel脚本编写的也有c编写的,
由于对prel不是很熟悉,贴出C源码如下:
 
[cpp] view plaincopy
 
  1. /* vi: set sw=4 ts=4: */  
  2. /* 
  3. * Mini ipcalc implementation for busybox 
  4. * By Jordan Crouse <jordan@cosmicpenguin.net> 
  5.  *    Stephan Linz  <linz@li-pro.net> 
  6. * This is a complete reimplementation of the ipcalc program 
  7.  * from Red Hat.  I didn't look at their source code, but there 
  8. * is no denying that this is a loving reimplementation 
  9. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. 
  10. */  
  11. #include "busybox.h"  
  12. #include <ctype.h>  
  13. #include <getopt.h>  
  14. #include <sys/socket.h>  
  15. #include <arpa/inet.h>  
  16. #define CLASS_A_NETMASK    ntohl(0xFF000000)  
  17. #define CLASS_B_NETMASK    ntohl(0xFFFF0000)  
  18. #define CLASS_C_NETMASK    ntohl(0xFFFFFF00)  
  19. static unsigned long get_netmask(unsigned long ipaddr)  
  20. {  
  21.     ipaddr = htonl(ipaddr);  
  22.     if ((ipaddr & 0xC0000000) == 0xC0000000)  
  23.         return CLASS_C_NETMASK;  
  24.     else if ((ipaddr & 0x80000000) == 0x80000000)  
  25.         return CLASS_B_NETMASK;  
  26.     else if ((ipaddr & 0x80000000) == 0)  
  27.         return CLASS_A_NETMASK;  
  28.     else  
  29.         return 0;  
  30. }  
  31. #ifdef CONFIG_FEATURE_IPCALC_FANCY  
  32. static int get_prefix(unsigned long netmask)  
  33. {  
  34.     unsigned long msk = 0x80000000;  
  35.     int ret = 0;  
  36.     netmask = htonl(netmask);  
  37.     while (msk) {  
  38.         if (netmask & msk)  
  39.             ret++;  
  40.         msk >>= 1;  
  41.     }  
  42.     return ret;  
  43. }  
  44. #else  
  45. int get_prefix(unsigned long netmask);  
  46. #endif  
  47. #define NETMASK   0x01  
  48. #define BROADCAST 0x02  
  49. #define NETWORK   0x04  
  50. #define NETPREFIX 0x08  
  51. #define HOSTNAME  0x10  
  52. #define SILENT    0x20  
  53. #if ENABLE_FEATURE_IPCALC_LONG_OPTIONS  
  54. static const struct option long_options[] = {  
  55.     { "netmask",     no_argument, NULL, 'm' },  
  56.     { "broadcast",   no_argument, NULL, 'b' },  
  57.     { "network",     no_argument, NULL, 'n' },  
  58. # if ENABLE_FEATURE_IPCALC_FANCY  
  59.     { "prefix",      no_argument, NULL, 'p' },  
  60.     { "hostname",     no_argument, NULL, 'h' },  
  61.     { "silent",      no_argument, NULL, 's' },  
  62. # endif  
  63.     { NULL, 0, NULL, 0 }  
  64. };  
  65. #endif  
  66. int ipcalc_main(int argc, char **argv)  
  67. {  
  68.     unsigned opt;  
  69.     int have_netmask = 0;  
  70.     in_addr_t netmask, broadcast, network, ipaddr;  
  71.     struct in_addr a;  
  72.     char *ipstr;  
  73. #if ENABLE_FEATURE_IPCALC_LONG_OPTIONS  
  74.     applet_long_options = long_options;  
  75. #endif  
  76.     opt = getopt32(argc, argv, "mbn" USE_FEATURE_IPCALC_FANCY("phs"));  
  77.     argc -= optind;  
  78.     argv += optind;  
  79.     if (opt & (BROADCAST | NETWORK | NETPREFIX)) {  
  80.         if (argc > 2 || argc <= 0)  
  81.             bb_show_usage();  
  82.     } else {  
  83.         if (argc != 1)  
  84.             bb_show_usage();  
  85.     }  
  86.     if (opt & SILENT)  
  87.         logmode = LOGMODE_NONE; /* Suppress error_msg() output */  
  88.     ipstr = argv[0];  
  89.     if (ENABLE_FEATURE_IPCALC_FANCY) {  
  90.         unsigned long netprefix = 0;  
  91.         char *prefixstr;  
  92.         prefixstr = ipstr;  
  93.         while (*prefixstr) {  
  94.             if (*prefixstr == '/') {  
  95.                 *prefixstr = (char)0;  
  96.                 prefixstr++;  
  97.                 if (*prefixstr) {  
  98.                     unsigned msk;  
  99.                     netprefix = xatoul_range(prefixstr, 0, 32);  
  100.                     netmask = 0;  
  101.                     msk = 0x80000000;  
  102.                     while (netprefix > 0) {  
  103.                         netmask |= msk;  
  104.                         msk >>= 1;  
  105.                         netprefix--;  
  106.                     }  
  107.                     netmask = htonl(netmask);  
  108.                     /* Even if it was 0, we will signify that we have a netmask. This allows */  
  109.                     /* for specification of default routes, etc which have a 0 netmask/prefix */  
  110.                     have_netmask = 1;  
  111.                 }  
  112.                 break;  
  113.             }  
  114.             prefixstr++;  
  115.         }  
  116.     }  
  117.     ipaddr = inet_aton(ipstr, &a);  
  118.     if (ipaddr == 0) {  
  119.         bb_error_msg_and_die("bad IP address: %s", argv[0]);  
  120.     }  
  121.     ipaddr = a.s_addr;  
  122.     if (argc == 2) {  
  123.         if (ENABLE_FEATURE_IPCALC_FANCY && have_netmask) {  
  124.             bb_error_msg_and_die("use prefix or netmask, not both");  
  125.         }  
  126.         netmask = inet_aton(argv[1], &a);  
  127.         if (netmask == 0) {  
  128.             bb_error_msg_and_die("bad netmask: %s", argv[1]);  
  129.         }  
  130.         netmask = a.s_addr;  
  131.     } else {  
  132.         /* JHC - If the netmask wasn't provided then calculate it */  
  133.         if (!ENABLE_FEATURE_IPCALC_FANCY || !have_netmask)  
  134.             netmask = get_netmask(ipaddr);  
  135.     }  
  136.     if (opt & NETMASK) {  
  137.         printf("NETMASK=%sn", inet_ntoa((*(struct in_addr *) &netmask)));  
  138.     }  
  139.     if (opt & BROADCAST) {  
  140.         broadcast = (ipaddr & netmask) | ~netmask;  
  141.         printf("BROADCAST=%sn", inet_ntoa((*(struct in_addr *) &broadcast)));  
  142.     }  
  143.     if (opt & NETWORK) {  
  144.         network = ipaddr & netmask;  
  145.         printf("NETWORK=%sn", inet_ntoa((*(struct in_addr *) &network)));  
  146.     }  
  147.     if (ENABLE_FEATURE_IPCALC_FANCY) {  
  148.         if (opt & NETPREFIX) {  
  149.             printf("PREFIX=%in", get_prefix(netmask));  
  150.         }  
  151.         if (opt & HOSTNAME) {  
  152.             struct hostent *hostinfo;  
  153.             int x;  
  154.             hostinfo = gethostbyaddr((char *) &ipaddr, sizeof(ipaddr), AF_INET);  
  155.             if (!hostinfo) {  
  156.                 bb_herror_msg_and_die("cannot find hostname for %s", argv[0]);  
  157.             }  
  158.             for (x = 0; hostinfo->h_name[x]; x++) {  
  159.                 hostinfo->h_name[x] = tolower(hostinfo->h_name[x]);  
  160.             }  
  161.             printf("HOSTNAME=%sn", hostinfo->h_name);  
  162.         }  
  163.     }  
  164.     return EXIT_SUCCESS;  
  165. }  

阅读源码后发现,选项PREFIX的配置值在此处并未生效,此工具完全根据输入的ip地址和掩码进行分析,应该不会出现ifcfg-eth0中的掩码配置不生效的问题。
测试环境暂时搭建完成,NETMASK和PREFIX配置冲突问题还未找到合理的解释!!!
posted @ 2014-10-24 17:00  陳聽溪  阅读(999)  评论(0)    收藏  举报