admin管理员组

文章数量:1530518

2024年3月25日发(作者:)

69 p++;

70 }

71 printf("n");

72 for (l=0; l < 4; l++)

73 printf("%d ",net[l]);

74 printf("/ ");

75 for (l=0; l < 4; l++)

76 printf("%d ",mask[l]);

77 printf("ncal ip & mask:n");

78 for (l=0; l < 4; l++)

79 {

80 printf(" %d ",ip[l]&mask[l]);

81 if ((ip[l]&mask[l]) != net[l])

82 break;

83 int temp = mask[l];

84 while(temp)

85 {

86

87 if (temp & 0x00000001){

88 sum++;

89 }

90 temp = temp >> 1;

91 }

92 }

93 if (l >= 4)

94 {

95 printf("n前缀长度:%d",sum);

96 if (len <= sum)

97 {

98 *n = i;

99 len = sum;

100 }

101 }

102 sum = 0;

103 i++;

104 l = 0;

105 printf("n");

106 }

107 printf("n");

108 }

109 int main()

110 {

111 char ip_addr[20] = "192.168.1.100";

112

113 //ip_addr[13] = '0';

114

115 const char *net_addr_array[100] =

116

117 {

118 "192.168.1.128/255.255.255.192",

119 "192.168.1.0/255.255.255.0",

120 "192.168.1.64/255.255.255.192",

121 "0.0.0.0/0.0.0.0",

122 ""

123 };

124 int *n;

125 n = (int*)malloc(sizeof(int));

126 max_prefix_match(ip_addr, net_addr_array, n);

127 printf("n = %dn",*n);

128

129 }

本文标签: 匹配长度IP地址前缀