| 713 | looks for unused triplets: (dest addr, dest port, alias port). */ |
| 714 | |
| 715 | int |
| 716 | FindNewPortGroup(struct libalias *la, |
| 717 | struct in_addr dst_addr, |
| 718 | struct in_addr alias_addr, |
| 719 | u_short src_port, |
| 720 | u_short dst_port, |
| 721 | u_short port_count, |
| 722 | u_char proto, |
| 723 | u_char align) |
| 724 | { |
| 725 | int i, j; |
| 726 | int max_trials; |
| 727 | u_short port_sys; |
| 728 | int link_type; |
| 729 | |
| 730 | LIBALIAS_LOCK_ASSERT(la); |
| 731 | /* |
| 732 | * Get link_type from protocol |
| 733 | */ |
| 734 | |
| 735 | switch (proto) { |
| 736 | case IPPROTO_UDP: |
| 737 | link_type = LINK_UDP; |
| 738 | break; |
| 739 | case IPPROTO_TCP: |
| 740 | link_type = LINK_TCP; |
| 741 | break; |
| 742 | default: |
| 743 | return (0); |
| 744 | break; |
| 745 | } |
| 746 | |
| 747 | /* |
| 748 | * The aliasing port is automatically selected by one of two |
| 749 | * methods below: |
| 750 | */ |
| 751 | max_trials = GET_NEW_PORT_MAX_ATTEMPTS; |
| 752 | |
| 753 | if (la->packetAliasMode & PKT_ALIAS_SAME_PORTS) { |
| 754 | /* |
| 755 | * When the ALIAS_SAME_PORTS option is chosen, the first |
| 756 | * try will be the actual source port. If this is already |
| 757 | * in use, the remainder of the trials will be random. |
| 758 | */ |
| 759 | port_sys = ntohs(src_port); |
| 760 | |
| 761 | } else { |
| 762 | /* First trial and all subsequent are random. */ |
| 763 | if (align == FIND_EVEN_ALIAS_BASE) |
| 764 | port_sys = arc4random() & ALIAS_PORT_MASK_EVEN; |
| 765 | else |
| 766 | port_sys = arc4random() & ALIAS_PORT_MASK; |
| 767 | |
| 768 | port_sys += ALIAS_PORT_BASE; |
| 769 | } |
| 770 | |
| 771 | /* Port number search */ |
| 772 | for (i = 0; i < max_trials; i++) { |
no test coverage detected