ipfs.c (145554) | ipfs.c (161357) |
---|---|
1/* $FreeBSD: head/contrib/ipfilter/tools/ipfs.c 145554 2005-04-26 15:18:45Z darrenr $ */ | 1/* $FreeBSD: head/contrib/ipfilter/tools/ipfs.c 161357 2006-08-16 12:23:02Z guido $ */ |
2 3/* 4 * Copyright (C) 1999-2001, 2003 by Darren Reed. 5 * 6 * See the IPFILTER.LICENCE file for details on licencing. 7 */ 8#ifdef __FreeBSD__ 9# ifndef __FreeBSD_cc_version --- 445 unchanged lines hidden (view full) --- 455 456 /* 457 * 1. Read all state information in. 458 */ 459 do { 460 i = read(sfd, &ips, sizeof(ips)); 461 if (i == -1) { 462 perror("read"); | 2 3/* 4 * Copyright (C) 1999-2001, 2003 by Darren Reed. 5 * 6 * See the IPFILTER.LICENCE file for details on licencing. 7 */ 8#ifdef __FreeBSD__ 9# ifndef __FreeBSD_cc_version --- 445 unchanged lines hidden (view full) --- 455 456 /* 457 * 1. Read all state information in. 458 */ 459 do { 460 i = read(sfd, &ips, sizeof(ips)); 461 if (i == -1) { 462 perror("read"); |
463 close(sfd); 464 return 1; | 463 goto freeipshead; |
465 } 466 if (i == 0) 467 break; 468 if (i != sizeof(ips)) { 469 fprintf(stderr, "state:incomplete read: %d != %d\n", 470 i, (int)sizeof(ips)); | 464 } 465 if (i == 0) 466 break; 467 if (i != sizeof(ips)) { 468 fprintf(stderr, "state:incomplete read: %d != %d\n", 469 i, (int)sizeof(ips)); |
471 close(sfd); 472 return 1; | 470 goto freeipshead; |
473 } 474 is = (ipstate_save_t *)malloc(sizeof(*is)); | 471 } 472 is = (ipstate_save_t *)malloc(sizeof(*is)); |
475 if(!is) { | 473 if (is == NULL) { |
476 fprintf(stderr, "malloc failed\n"); | 474 fprintf(stderr, "malloc failed\n"); |
477 return 1; | 475 goto freeipshead; |
478 } 479 480 bcopy((char *)&ips, (char *)is, sizeof(ips)); 481 482 /* 483 * Check to see if this is the first state entry that will 484 * reference a particular rule and if so, flag it as such 485 * else just adjust the rule pointer to become a pointer to --- 21 unchanged lines hidden (view full) --- 507 } while (1); 508 509 close(sfd); 510 511 obj.ipfo_rev = IPFILTER_VERSION; 512 obj.ipfo_size = sizeof(*is); 513 obj.ipfo_type = IPFOBJ_STATESAVE; 514 | 476 } 477 478 bcopy((char *)&ips, (char *)is, sizeof(ips)); 479 480 /* 481 * Check to see if this is the first state entry that will 482 * reference a particular rule and if so, flag it as such 483 * else just adjust the rule pointer to become a pointer to --- 21 unchanged lines hidden (view full) --- 505 } while (1); 506 507 close(sfd); 508 509 obj.ipfo_rev = IPFILTER_VERSION; 510 obj.ipfo_size = sizeof(*is); 511 obj.ipfo_type = IPFOBJ_STATESAVE; 512 |
515 for (is = ipshead; is; is = is->ips_next) { | 513 while ((is = ipshead) != NULL) { |
516 if (opts & OPT_VERBOSE) 517 printf("Loading new state table entry\n"); 518 if (is->ips_is.is_flags & SI_NEWFR) { 519 if (opts & OPT_VERBOSE) 520 printf("Loading new filter rule\n"); 521 } 522 523 obj.ipfo_ptr = is; 524 if (!(opts & OPT_DONOTHING)) 525 if (ioctl(fd, SIOCSTPUT, &obj)) { 526 perror("SIOCSTPUT"); | 514 if (opts & OPT_VERBOSE) 515 printf("Loading new state table entry\n"); 516 if (is->ips_is.is_flags & SI_NEWFR) { 517 if (opts & OPT_VERBOSE) 518 printf("Loading new filter rule\n"); 519 } 520 521 obj.ipfo_ptr = is; 522 if (!(opts & OPT_DONOTHING)) 523 if (ioctl(fd, SIOCSTPUT, &obj)) { 524 perror("SIOCSTPUT"); |
527 return 1; | 525 goto freeipshead; |
528 } 529 530 if (is->ips_is.is_flags & SI_NEWFR) { 531 if (opts & OPT_VERBOSE) 532 printf("Real rule addr %p\n", is->ips_rule); 533 for (is1 = is->ips_next; is1; is1 = is1->ips_next) 534 if (is1->ips_rule == (frentry_t *)&is->ips_rule) 535 is1->ips_rule = is->ips_rule; 536 } | 526 } 527 528 if (is->ips_is.is_flags & SI_NEWFR) { 529 if (opts & OPT_VERBOSE) 530 printf("Real rule addr %p\n", is->ips_rule); 531 for (is1 = is->ips_next; is1; is1 = is1->ips_next) 532 if (is1->ips_rule == (frentry_t *)&is->ips_rule) 533 is1->ips_rule = is->ips_rule; 534 } |
535 536 ipshead = is->ips_next; 537 free(is); |
|
537 } 538 539 return 0; | 538 } 539 540 return 0; |
541 542freeipshead: 543 while ((is = ipshead) != NULL) { 544 ipshead = is->ips_next; 545 free(is); 546 } 547 if (sfd != -1) 548 close(sfd); 549 return 1; |
|
540} 541 542 543int readnat(fd, file) 544int fd; 545char *file; 546{ 547 nat_save_t ipn, *in, *ipnhead = NULL, *in1, *ipntail = NULL; --- 22 unchanged lines hidden (view full) --- 570 571 /* 572 * 1. Read all state information in. 573 */ 574 do { 575 i = read(nfd, &ipn, sizeof(ipn)); 576 if (i == -1) { 577 perror("read"); | 550} 551 552 553int readnat(fd, file) 554int fd; 555char *file; 556{ 557 nat_save_t ipn, *in, *ipnhead = NULL, *in1, *ipntail = NULL; --- 22 unchanged lines hidden (view full) --- 580 581 /* 582 * 1. Read all state information in. 583 */ 584 do { 585 i = read(nfd, &ipn, sizeof(ipn)); 586 if (i == -1) { 587 perror("read"); |
578 close(nfd); 579 return 1; | 588 goto freenathead; |
580 } 581 if (i == 0) 582 break; 583 if (i != sizeof(ipn)) { 584 fprintf(stderr, "nat:incomplete read: %d != %d\n", 585 i, (int)sizeof(ipn)); | 589 } 590 if (i == 0) 591 break; 592 if (i != sizeof(ipn)) { 593 fprintf(stderr, "nat:incomplete read: %d != %d\n", 594 i, (int)sizeof(ipn)); |
586 close(nfd); 587 return 1; | 595 goto freenathead; |
588 } 589 590 in = (nat_save_t *)malloc(ipn.ipn_dsize); | 596 } 597 598 in = (nat_save_t *)malloc(ipn.ipn_dsize); |
591 if (!in) 592 break; | 599 if (in == NULL) { 600 fprintf(stderr, "nat:cannot malloc nat save atruct\n"); 601 goto freenathead; 602 } |
593 594 if (ipn.ipn_dsize > sizeof(ipn)) { 595 n = ipn.ipn_dsize - sizeof(ipn); 596 if (n > 0) { 597 s = in->ipn_data + sizeof(in->ipn_data); 598 i = read(nfd, s, n); 599 if (i == 0) 600 break; 601 if (i != n) { 602 fprintf(stderr, 603 "nat:incomplete read: %d != %d\n", 604 i, n); | 603 604 if (ipn.ipn_dsize > sizeof(ipn)) { 605 n = ipn.ipn_dsize - sizeof(ipn); 606 if (n > 0) { 607 s = in->ipn_data + sizeof(in->ipn_data); 608 i = read(nfd, s, n); 609 if (i == 0) 610 break; 611 if (i != n) { 612 fprintf(stderr, 613 "nat:incomplete read: %d != %d\n", 614 i, n); |
605 close(nfd); 606 return 1; | 615 goto freenathead; |
607 } 608 } 609 } 610 bcopy((char *)&ipn, (char *)in, sizeof(ipn)); 611 612 /* 613 * Check to see if this is the first NAT entry that will 614 * reference a particular rule and if so, flag it as such --- 25 unchanged lines hidden (view full) --- 640 } while (1); 641 642 close(nfd); 643 nfd = -1; 644 645 obj.ipfo_rev = IPFILTER_VERSION; 646 obj.ipfo_type = IPFOBJ_NATSAVE; 647 | 616 } 617 } 618 } 619 bcopy((char *)&ipn, (char *)in, sizeof(ipn)); 620 621 /* 622 * Check to see if this is the first NAT entry that will 623 * reference a particular rule and if so, flag it as such --- 25 unchanged lines hidden (view full) --- 649 } while (1); 650 651 close(nfd); 652 nfd = -1; 653 654 obj.ipfo_rev = IPFILTER_VERSION; 655 obj.ipfo_type = IPFOBJ_NATSAVE; 656 |
648 for (in = ipnhead; in; in = in->ipn_next) { | 657 while ((in = ipnhead) != NULL) { |
649 if (opts & OPT_VERBOSE) 650 printf("Loading new NAT table entry\n"); 651 nat = &in->ipn_nat; 652 if (nat->nat_flags & SI_NEWFR) { 653 if (opts & OPT_VERBOSE) 654 printf("Loading new filter rule\n"); 655 } 656 --- 8 unchanged lines hidden (view full) --- 665 666 if (nat->nat_flags & SI_NEWFR) { 667 if (opts & OPT_VERBOSE) 668 printf("Real rule addr %p\n", nat->nat_fr); 669 for (in1 = in->ipn_next; in1; in1 = in1->ipn_next) 670 if (in1->ipn_rule == &in->ipn_fr) 671 in1->ipn_rule = nat->nat_fr; 672 } | 658 if (opts & OPT_VERBOSE) 659 printf("Loading new NAT table entry\n"); 660 nat = &in->ipn_nat; 661 if (nat->nat_flags & SI_NEWFR) { 662 if (opts & OPT_VERBOSE) 663 printf("Loading new filter rule\n"); 664 } 665 --- 8 unchanged lines hidden (view full) --- 674 675 if (nat->nat_flags & SI_NEWFR) { 676 if (opts & OPT_VERBOSE) 677 printf("Real rule addr %p\n", nat->nat_fr); 678 for (in1 = in->ipn_next; in1; in1 = in1->ipn_next) 679 if (in1->ipn_rule == &in->ipn_fr) 680 in1->ipn_rule = nat->nat_fr; 681 } |
682 683 ipnhead = in->ipn_next; 684 free(in); |
|
673 } 674 675 return 0; | 685 } 686 687 return 0; |
688 689freenathead: 690 while ((in = ipnhead) != NULL) { 691 ipnhead = in->ipn_next; 692 free(in); 693 } 694 if (nfd != -1) 695 close(nfd); 696 return 1; |
|
676} 677 678 679int writenat(fd, file) 680int fd; 681char *file; 682{ 683 nat_save_t *ipnp = NULL, *next = NULL; --- 172 unchanged lines hidden --- | 697} 698 699 700int writenat(fd, file) 701int fd; 702char *file; 703{ 704 nat_save_t *ipnp = NULL, *next = NULL; --- 172 unchanged lines hidden --- |