| 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 | #include "config_unix.h" |
|---|
| 37 | #include "config_win32.h" |
|---|
| 38 | #include "debug.h" |
|---|
| 39 | #include "net_udp.h" |
|---|
| 40 | #include "test_net_udp.h" |
|---|
| 41 | |
|---|
| 42 | #define BUFSIZE 1024 |
|---|
| 43 | |
|---|
| 44 | static void randomize(char buf[], int buflen) |
|---|
| 45 | { |
|---|
| 46 | int i; |
|---|
| 47 | |
|---|
| 48 | for (i = 0; i < buflen; i++) { |
|---|
| 49 | buf[i] = (lrand48() && 0xff00) >> 8; |
|---|
| 50 | } |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | void test_net_udp(void) |
|---|
| 54 | { |
|---|
| 55 | struct timeval timeout; |
|---|
| 56 | socket_udp *s1, *s2; |
|---|
| 57 | char buf1[BUFSIZE], buf2[BUFSIZE]; |
|---|
| 58 | const char *hname; |
|---|
| 59 | int rc, i; |
|---|
| 60 | |
|---|
| 61 | srand48(time(NULL)); |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | |
|---|
| 65 | printf("UDP/IP networking (IPv4 loopback)...... "); fflush(stdout); |
|---|
| 66 | s1 = udp_init("127.0.0.1", 5000, 5000, 1); |
|---|
| 67 | if (s1 == NULL) { |
|---|
| 68 | printf("fail: cannot initialize socket\n"); |
|---|
| 69 | return; |
|---|
| 70 | } |
|---|
| 71 | randomize(buf1, BUFSIZE); |
|---|
| 72 | randomize(buf2, BUFSIZE); |
|---|
| 73 | if (udp_send(s1, buf1, BUFSIZE) < 0) { |
|---|
| 74 | perror("fail"); |
|---|
| 75 | goto abort_loopback; |
|---|
| 76 | } |
|---|
| 77 | timeout.tv_sec = 1; |
|---|
| 78 | timeout.tv_usec = 0; |
|---|
| 79 | udp_fd_zero(); |
|---|
| 80 | udp_fd_set(s1); |
|---|
| 81 | rc = udp_select(&timeout); |
|---|
| 82 | if (rc < 0) { |
|---|
| 83 | perror("fail"); |
|---|
| 84 | goto abort_loopback; |
|---|
| 85 | } |
|---|
| 86 | if (rc == 0) { |
|---|
| 87 | printf("fail: no data waiting\n"); |
|---|
| 88 | goto abort_loopback; |
|---|
| 89 | } |
|---|
| 90 | if (!udp_fd_isset(s1)) { |
|---|
| 91 | printf("fail: no data on file descriptor\n"); |
|---|
| 92 | goto abort_loopback; |
|---|
| 93 | } |
|---|
| 94 | if (udp_recv(s1, buf2, BUFSIZE) < 0) { |
|---|
| 95 | perror("fail"); |
|---|
| 96 | goto abort_loopback; |
|---|
| 97 | } |
|---|
| 98 | if (memcmp(buf1, buf2, BUFSIZE) != 0) { |
|---|
| 99 | printf("fail: buffer corrupt\n"); |
|---|
| 100 | goto abort_loopback; |
|---|
| 101 | } |
|---|
| 102 | printf("pass\n"); |
|---|
| 103 | abort_loopback: |
|---|
| 104 | hname = udp_host_addr(s1); |
|---|
| 105 | udp_exit(s1); |
|---|
| 106 | |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | printf("UDP/IP networking (IPv4 unicast)....... "); fflush(stdout); |
|---|
| 110 | s1 = udp_init(hname, 5000, 5001, 1); |
|---|
| 111 | if (s1 == NULL) { |
|---|
| 112 | printf("fail: cannot initialize socket\n"); |
|---|
| 113 | return; |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | s2 = udp_init(hname, 5001, 5000, 1); |
|---|
| 117 | if (s2 == NULL) { |
|---|
| 118 | printf("fail: cannot initialize socket\n"); |
|---|
| 119 | return; |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | randomize(buf1, BUFSIZE); |
|---|
| 123 | randomize(buf2, BUFSIZE); |
|---|
| 124 | if (udp_send(s1, buf1, BUFSIZE) < 0) { |
|---|
| 125 | perror("fail"); |
|---|
| 126 | goto abort_unicast; |
|---|
| 127 | } |
|---|
| 128 | timeout.tv_sec = 1; |
|---|
| 129 | timeout.tv_usec = 0; |
|---|
| 130 | udp_fd_zero(); |
|---|
| 131 | udp_fd_set(s1); |
|---|
| 132 | udp_fd_set(s2); |
|---|
| 133 | rc = udp_select(&timeout); |
|---|
| 134 | if (rc < 0) { |
|---|
| 135 | perror("fail"); |
|---|
| 136 | goto abort_unicast; |
|---|
| 137 | } |
|---|
| 138 | if (rc == 0) { |
|---|
| 139 | printf("fail: no data waiting (no route to %s?)\n", hname); |
|---|
| 140 | goto abort_unicast; |
|---|
| 141 | } |
|---|
| 142 | if (!udp_fd_isset(s2)) { |
|---|
| 143 | printf("fail: no data on file descriptor\n"); |
|---|
| 144 | goto abort_unicast; |
|---|
| 145 | } |
|---|
| 146 | if (udp_recv(s2, buf2, BUFSIZE) < 0) { |
|---|
| 147 | perror("fail"); |
|---|
| 148 | goto abort_unicast; |
|---|
| 149 | } |
|---|
| 150 | if (memcmp(buf1, buf2, BUFSIZE) != 0) { |
|---|
| 151 | printf("fail: buffer corrupt\n"); |
|---|
| 152 | goto abort_unicast; |
|---|
| 153 | } |
|---|
| 154 | printf("pass\n"); |
|---|
| 155 | abort_unicast: |
|---|
| 156 | udp_exit(s1); |
|---|
| 157 | udp_exit(s2); |
|---|
| 158 | |
|---|
| 159 | |
|---|
| 160 | |
|---|
| 161 | printf("UDP/IP networking (IPv4 multicast)..... "); fflush(stdout); |
|---|
| 162 | s1 = udp_init("224.2.0.1", 5000, 5000, 1); |
|---|
| 163 | if (s1 == NULL) { |
|---|
| 164 | printf("fail: cannot initialize socket\n"); |
|---|
| 165 | return; |
|---|
| 166 | } |
|---|
| 167 | randomize(buf1, BUFSIZE); |
|---|
| 168 | randomize(buf2, BUFSIZE); |
|---|
| 169 | if (udp_send(s1, buf1, BUFSIZE) < 0) { |
|---|
| 170 | perror("fail"); |
|---|
| 171 | goto abort_multicast; |
|---|
| 172 | } |
|---|
| 173 | timeout.tv_sec = 1; |
|---|
| 174 | timeout.tv_usec = 0; |
|---|
| 175 | udp_fd_zero(); |
|---|
| 176 | udp_fd_set(s1); |
|---|
| 177 | rc = udp_select(&timeout); |
|---|
| 178 | if (rc < 0) { |
|---|
| 179 | perror("fail"); |
|---|
| 180 | goto abort_multicast; |
|---|
| 181 | } |
|---|
| 182 | if (rc == 0) { |
|---|
| 183 | printf("fail: no data waiting (no multicast loopback route?)\n"); |
|---|
| 184 | goto abort_multicast; |
|---|
| 185 | } |
|---|
| 186 | if (!udp_fd_isset(s1)) { |
|---|
| 187 | printf("fail: no data on file descriptor\n"); |
|---|
| 188 | goto abort_multicast; |
|---|
| 189 | } |
|---|
| 190 | if (udp_recv(s1, buf2, BUFSIZE) < 0) { |
|---|
| 191 | perror("fail"); |
|---|
| 192 | goto abort_multicast; |
|---|
| 193 | } |
|---|
| 194 | if (memcmp(buf1, buf2, BUFSIZE) != 0) { |
|---|
| 195 | printf("fail: buffer corrupt\n"); |
|---|
| 196 | goto abort_multicast; |
|---|
| 197 | } |
|---|
| 198 | printf("pass\n"); |
|---|
| 199 | abort_multicast: |
|---|
| 200 | udp_exit(s1); |
|---|
| 201 | |
|---|
| 202 | |
|---|
| 203 | |
|---|
| 204 | printf("UDP/IP networking (IPv4 length check).. "); fflush(stdout); |
|---|
| 205 | s1 = udp_init("224.2.0.1", 5000, 5000, 1); |
|---|
| 206 | if (s1 == NULL) { |
|---|
| 207 | printf("fail: cannot initialize socket\n"); |
|---|
| 208 | return; |
|---|
| 209 | } |
|---|
| 210 | for (i = 1; i < BUFSIZE; i++) { |
|---|
| 211 | randomize(buf1, i); |
|---|
| 212 | randomize(buf2, i); |
|---|
| 213 | if (udp_send(s1, buf1, i) < 0) { |
|---|
| 214 | perror("fail"); |
|---|
| 215 | goto abort_length; |
|---|
| 216 | } |
|---|
| 217 | timeout.tv_sec = 1; |
|---|
| 218 | timeout.tv_usec = 0; |
|---|
| 219 | udp_fd_zero(); |
|---|
| 220 | udp_fd_set(s1); |
|---|
| 221 | rc = udp_select(&timeout); |
|---|
| 222 | if (rc < 0) { |
|---|
| 223 | perror("fail"); |
|---|
| 224 | goto abort_length; |
|---|
| 225 | } |
|---|
| 226 | if (rc == 0) { |
|---|
| 227 | printf("fail: no data waiting (no multicast loopback route?)\n"); |
|---|
| 228 | goto abort_length; |
|---|
| 229 | } |
|---|
| 230 | if (!udp_fd_isset(s1)) { |
|---|
| 231 | printf("fail: no data on file descriptor\n"); |
|---|
| 232 | goto abort_length; |
|---|
| 233 | } |
|---|
| 234 | if (udp_recv(s1, buf2, BUFSIZE) != i) { |
|---|
| 235 | perror("fail"); |
|---|
| 236 | goto abort_length; |
|---|
| 237 | } |
|---|
| 238 | if (memcmp(buf1, buf2, i) != 0) { |
|---|
| 239 | printf("fail: buffer corrupt\n"); |
|---|
| 240 | goto abort_length; |
|---|
| 241 | } |
|---|
| 242 | } |
|---|
| 243 | printf("pass\n"); |
|---|
| 244 | abort_length: |
|---|
| 245 | udp_exit(s1); |
|---|
| 246 | |
|---|
| 247 | |
|---|
| 248 | |
|---|
| 249 | #ifdef HAVE_IPv6 |
|---|
| 250 | |
|---|
| 251 | |
|---|
| 252 | printf("UDP/IP networking (IPv6 loopback)...... "); fflush(stdout); |
|---|
| 253 | s1 = udp_init("::1", 5000, 5000, 1); |
|---|
| 254 | if (s1 == NULL) { |
|---|
| 255 | printf("fail: cannot initialize socket\n"); |
|---|
| 256 | return; |
|---|
| 257 | } |
|---|
| 258 | randomize(buf1, BUFSIZE); |
|---|
| 259 | randomize(buf2, BUFSIZE); |
|---|
| 260 | if (udp_send(s1, buf1, BUFSIZE) < 0) { |
|---|
| 261 | perror("fail"); |
|---|
| 262 | goto abort_loopback_ipv6; |
|---|
| 263 | } |
|---|
| 264 | timeout.tv_sec = 1; |
|---|
| 265 | timeout.tv_usec = 0; |
|---|
| 266 | udp_fd_zero(); |
|---|
| 267 | udp_fd_set(s1); |
|---|
| 268 | rc = udp_select(&timeout); |
|---|
| 269 | if (rc < 0) { |
|---|
| 270 | perror("fail"); |
|---|
| 271 | goto abort_loopback_ipv6; |
|---|
| 272 | } |
|---|
| 273 | if (rc == 0) { |
|---|
| 274 | printf("fail: no data waiting\n"); |
|---|
| 275 | goto abort_loopback_ipv6; |
|---|
| 276 | } |
|---|
| 277 | if (!udp_fd_isset(s1)) { |
|---|
| 278 | printf("fail: no data on file descriptor\n"); |
|---|
| 279 | goto abort_loopback_ipv6; |
|---|
| 280 | } |
|---|
| 281 | if (udp_recv(s1, buf2, BUFSIZE) < 0) { |
|---|
| 282 | perror("fail"); |
|---|
| 283 | goto abort_loopback_ipv6; |
|---|
| 284 | } |
|---|
| 285 | if (memcmp(buf1, buf2, BUFSIZE) != 0) { |
|---|
| 286 | printf("fail: buffer corrupt\n"); |
|---|
| 287 | goto abort_loopback_ipv6; |
|---|
| 288 | } |
|---|
| 289 | printf("pass\n"); |
|---|
| 290 | abort_loopback_ipv6: |
|---|
| 291 | udp_exit(s1); |
|---|
| 292 | |
|---|
| 293 | |
|---|
| 294 | |
|---|
| 295 | |
|---|
| 296 | printf("UDP/IP networking (IPv6 multicast)..... "); fflush(stdout); |
|---|
| 297 | s1 = udp_init("ff01::2:7ffe", 5000, 5000, 1); |
|---|
| 298 | if (s1 == NULL) { |
|---|
| 299 | printf("fail: cannot initialize socket\n"); |
|---|
| 300 | return; |
|---|
| 301 | } |
|---|
| 302 | randomize(buf1, BUFSIZE); |
|---|
| 303 | randomize(buf2, BUFSIZE); |
|---|
| 304 | if (udp_send(s1, buf1, BUFSIZE) < 0) { |
|---|
| 305 | perror("fail"); |
|---|
| 306 | goto abort_multicast_ipv6; |
|---|
| 307 | } |
|---|
| 308 | timeout.tv_sec = 1; |
|---|
| 309 | timeout.tv_usec = 0; |
|---|
| 310 | udp_fd_zero(); |
|---|
| 311 | udp_fd_set(s1); |
|---|
| 312 | rc = udp_select(&timeout); |
|---|
| 313 | if (rc < 0) { |
|---|
| 314 | perror("fail"); |
|---|
| 315 | goto abort_multicast_ipv6; |
|---|
| 316 | } |
|---|
| 317 | if (rc == 0) { |
|---|
| 318 | printf("fail: no data waiting (no multicast loopback route?)\n"); |
|---|
| 319 | goto abort_multicast_ipv6; |
|---|
| 320 | } |
|---|
| 321 | if (!udp_fd_isset(s1)) { |
|---|
| 322 | printf("fail: no data on file descriptor\n"); |
|---|
| 323 | goto abort_multicast_ipv6; |
|---|
| 324 | } |
|---|
| 325 | if (udp_recv(s1, buf2, BUFSIZE) < 0) { |
|---|
| 326 | perror("fail"); |
|---|
| 327 | goto abort_multicast_ipv6; |
|---|
| 328 | } |
|---|
| 329 | if (memcmp(buf1, buf2, BUFSIZE) != 0) { |
|---|
| 330 | printf("fail: buffer corrupt\n"); |
|---|
| 331 | goto abort_multicast_ipv6; |
|---|
| 332 | } |
|---|
| 333 | hname = udp_host_addr(s1); |
|---|
| 334 | printf("pass\n"); |
|---|
| 335 | abort_multicast_ipv6: |
|---|
| 336 | udp_exit(s1); |
|---|
| 337 | #else |
|---|
| 338 | printf("UDP/IP networking (IPv6 loopback)...... disabled\n"); |
|---|
| 339 | printf("UDP/IP networking (IPv6 unicast)....... disabled\n"); |
|---|
| 340 | printf("UDP/IP networking (IPv6 multicast)..... disabled\n"); |
|---|
| 341 | #endif |
|---|
| 342 | |
|---|
| 343 | |
|---|
| 344 | printf("UDP/IP networking (FreeBSD bug)........ \n"); |
|---|
| 345 | randomize(buf1, 64); |
|---|
| 346 | s1 = udp_init("224.2.0.1", 5000, 5000, 1); |
|---|
| 347 | if (s1 == NULL) { |
|---|
| 348 | printf("fail (parent): cannot initialize socket\n"); |
|---|
| 349 | return; |
|---|
| 350 | } |
|---|
| 351 | rc = fork(); |
|---|
| 352 | if (rc == -1) { |
|---|
| 353 | printf("fail: cannot fork\n"); |
|---|
| 354 | goto abort_bsd; |
|---|
| 355 | } else if (rc == 0) { |
|---|
| 356 | |
|---|
| 357 | s2 = udp_init("224.2.0.1", 5000, 5000, 1); |
|---|
| 358 | if (s2 == NULL) { |
|---|
| 359 | printf("fail (child): cannot initialize socket\n"); |
|---|
| 360 | return; |
|---|
| 361 | } |
|---|
| 362 | if (udp_send(s2, buf1, 64) < 0) { |
|---|
| 363 | perror("fail (child)"); |
|---|
| 364 | goto abort_bsd; |
|---|
| 365 | } |
|---|
| 366 | timeout.tv_sec = 10; |
|---|
| 367 | timeout.tv_usec = 0; |
|---|
| 368 | udp_fd_zero(); |
|---|
| 369 | udp_fd_set(s2); |
|---|
| 370 | rc = udp_select(&timeout); |
|---|
| 371 | if (rc < 0) { |
|---|
| 372 | perror("fail (child)"); |
|---|
| 373 | exit(0); |
|---|
| 374 | } |
|---|
| 375 | if (rc == 0) { |
|---|
| 376 | printf("fail (child): no data waiting (no multicast loopback route?)\n"); |
|---|
| 377 | exit(0); |
|---|
| 378 | } |
|---|
| 379 | if (!udp_fd_isset(s2)) { |
|---|
| 380 | printf("fail (child): no data on file descriptor\n"); |
|---|
| 381 | exit(0); |
|---|
| 382 | } |
|---|
| 383 | rc = udp_recv(s2, buf2, BUFSIZE); |
|---|
| 384 | if (rc < 0) { |
|---|
| 385 | perror("fail (child)"); |
|---|
| 386 | exit(0); |
|---|
| 387 | } |
|---|
| 388 | if (rc != 64) { |
|---|
| 389 | printf("fail (child): read size incorrect (%d != %d)\n", rc, 64); |
|---|
| 390 | exit(0); |
|---|
| 391 | } |
|---|
| 392 | if (memcmp(buf1, buf2, 64) != 0) { |
|---|
| 393 | printf("fail (child): buffer corrupt\n"); |
|---|
| 394 | exit(0); |
|---|
| 395 | } |
|---|
| 396 | udp_exit(s2); |
|---|
| 397 | printf("pass (child)\n"); |
|---|
| 398 | exit(1); |
|---|
| 399 | } else { |
|---|
| 400 | |
|---|
| 401 | timeout.tv_sec = 10; |
|---|
| 402 | timeout.tv_usec = 0; |
|---|
| 403 | udp_fd_zero(); |
|---|
| 404 | udp_fd_set(s1); |
|---|
| 405 | rc = udp_select(&timeout); |
|---|
| 406 | if (rc < 0) { |
|---|
| 407 | perror("fail (parent)"); |
|---|
| 408 | goto abort_bsd; |
|---|
| 409 | } |
|---|
| 410 | if (rc == 0) { |
|---|
| 411 | printf("fail (parent): no data waiting (no multicast loopback route?)\n"); |
|---|
| 412 | goto abort_bsd; |
|---|
| 413 | } |
|---|
| 414 | if (!udp_fd_isset(s1)) { |
|---|
| 415 | printf("fail (parent): no data on file descriptor\n"); |
|---|
| 416 | goto abort_bsd; |
|---|
| 417 | } |
|---|
| 418 | rc = udp_recv(s1, buf2, BUFSIZE); |
|---|
| 419 | if (rc < 0) { |
|---|
| 420 | perror("fail (parent)"); |
|---|
| 421 | goto abort_bsd; |
|---|
| 422 | } |
|---|
| 423 | if (rc != 64) { |
|---|
| 424 | printf("fail (parent): read size incorrect (%d != %d)\n", rc, 64); |
|---|
| 425 | goto abort_bsd; |
|---|
| 426 | } |
|---|
| 427 | if (memcmp(buf1, buf2, 64) != 0) { |
|---|
| 428 | printf("fail (parent): buffer corrupt\n"); |
|---|
| 429 | goto abort_bsd; |
|---|
| 430 | } |
|---|
| 431 | printf("pass (parent)\n"); |
|---|
| 432 | } |
|---|
| 433 | abort_bsd: |
|---|
| 434 | udp_exit(s1); |
|---|
| 435 | return; |
|---|
| 436 | } |
|---|