1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
|
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
static void print (int const ip[4], char const *cmt)
{
int i;
for (i = 0; i < 4; i++)
{
printf ("%4d", ip[i]);
}
printf (" %s\n", cmt);
}
// Si une IP est nulle
static int ip_isnul (int const ip[4])
{
return (ip[0] == 0 && ip[1] == 0 && ip[2] == 0 && ip[3] == 0);
}
// Si une IP égale une autre IP
static int ip_isequal (int const ip1[4], int const ip2[4])
{
return (ip1[0] == ip2[0] && ip1[1] == ip2[1] && ip1[2] == ip2[2] && ip1[3] == ip2[3]);
}
// Si une IP inf une autre IP
static int ip_isinf (int const ip1[4], int const ip2[4])
{
size_t i;
for (i = 0; i < 4; i++)
{
if (ip1[i] > ip2[i])
{
return 0;
}
}
return 1;
}
// Si une IP sup une autre IP
static int ip_issup (int const ip1[4], int const ip2[4])
{
size_t i;
for (i = 0; i < 4; i++)
{
if (ip1[i] < ip2[i])
{
return 0;
}
}
return 1;
}
static int Verife (int const ip[4])
{
int ok = !ip_isnul (ip);
if (ok)
{
typedef struct
{
int deb[4];
int fin[4];
}
t_plageIP;
static const t_plageIP tabPlage[] =
{
{
{
10, 0, 0, 0},
{
10, 255, 255, 255}
},
{
{
172, 16, 0, 0},
{
172, 31, 255, 255}
},
{
{
192, 168, 0, 0},
{
192, 168, 255, 255}
},
{
{
224, 0, 0, 0},
{
239, 255, 255, 255}
},
{
{
240, 0, 0, 0},
{
255, 255, 255, 255}
},
{
{
0, 0, 0, 0},
{
0, 0, 0, 0}}
};
t_plageIP const *ptPlage;
// Balayage des plages
for (ptPlage = tabPlage; ok && !ip_isnul (ptPlage->deb); ptPlage++)
{
if (ip_issup (ip, ptPlage->deb) && ip_isinf (ip, ptPlage->fin))
{
// IP appartient plage interdite
ok = 0;
}
#if 0
print (ip, "IP");
print (ptPlage->deb, "deb");
print (ptPlage->fin, "fin");
printf ("ok=%d\n", ok);
#endif
}
}
return ok;
}
static int *tabIP (char const *chaine)
{
int *ip = malloc (sizeof *ip * 4);
if (ip != NULL)
{
int n = sscanf (chaine, "%d.%d.%d.%d", ip, ip + 1, ip + 2, ip + 3);
if (n != 4)
{
printf ("erreur format\n");
free (ip), ip = NULL;
}
else
{
if (!Verife (ip))
{
free (ip), ip = NULL;
}
}
}
else
{
perror ("erreur allocation\n");
}
return ip;
}
static int tu (char const *sip)
{
int ok;
int *t = tabIP (sip);
if (t == NULL)
{
printf ("%s %s\n", sip, "ERR");
ok = 0;
}
else
{
print (t, "OK");
free (t);
ok = 1;
}
return ok;
}
int main (void)
{
struct test
{
size_t n;
char const *sip;
int ok;
};
struct test a_test[] =
{
{
10, "0.0.0.0", 0},
{
20, "0.0.0.1", 1},
{
21, "9.255.255.255", 1},
{
30, "10.0.0.0", 0},
{
31, "10.255.255.255", 0},
{
40, "11.0.0.0", 1},
{
41, "172.15.255.255", 1},
{
50, "172.16.0.0", 0},
{
51, "172.31.255.255", 0},
{
60, "172.32.0.0", 1},
{
61, "192.167.255.255", 1},
{
70, "192.168.0.0", 0},
{
71, "192.168.255.255", 0},
{
80, "192.169.0.0", 1},
{
81, "223.255.255.255", 1},
{
90, "224.0.0.0", 0},
{
91, "239.255.255.255", 0},
{
100, "240.0.0.0", 0},
{
101, "255.255.255.255", 0},
};
size_t i;
for (i = 0; i < sizeof a_test / sizeof *a_test; i++)
{
struct test const *p = a_test + i;
int ok = tu (p->sip);
if (ok != p->ok)
{
printf ("error at test %u\n", (unsigned) p->n);
break;
}
}
if (i == sizeof a_test / sizeof *a_test)
{
puts ("\nP A S S E D");
}
return 0;
} |
Partager