|
72 | 72 | stanchion_data/0, |
73 | 73 | camel_case/1, |
74 | 74 | capitalize/1, |
75 | | - this_host_addr/0 |
| 75 | + this_host_addresses/0, |
| 76 | + select_addr_for_stanchion/0 |
76 | 77 | ]). |
77 | 78 |
|
78 | 79 | -include("riak_cs.hrl"). |
@@ -536,35 +537,58 @@ capitalize("") -> ""; |
536 | 537 | capitalize([H|T]) -> string:to_upper([H]) ++ T. |
537 | 538 |
|
538 | 539 |
|
539 | | - |
540 | | -this_host_addr() -> |
| 540 | +-spec select_addr_for_stanchion() -> string(). |
| 541 | +select_addr_for_stanchion() -> |
541 | 542 | {ok, Ifs} = inet:getifaddrs(), |
| 543 | + Mask = riak_cs_config:stanchion_netmask(), |
| 544 | + {ok, {M1, M2, M3, M4}} = inet:parse_address(Mask), |
542 | 545 | case lists:filtermap( |
543 | | - fun({_If, PL}) -> |
544 | | - case proplists:get_value(addr, PL) of |
545 | | - AA when AA /= undefined, |
546 | | - AA /= {127,0,0,1}, |
547 | | - AA /= {0,0,0,0}, |
548 | | - size(AA) == 4 -> |
549 | | - {A1, A2, A3, A4} = AA, |
550 | | - {true, {_If, lists:flatten(io_lib:format("~b.~b.~b.~b", [A1, A2, A3, A4]))}}; |
551 | | - _ -> |
| 546 | + fun({_If, IfOpts}) -> |
| 547 | + {A1, A2, A3, A4} = Addr = extract_addr(IfOpts), |
| 548 | + if (M1 band A1) > 0 andalso |
| 549 | + (M2 band A2) > 0 andalso |
| 550 | + (M3 band A3) > 0 andalso |
| 551 | + (M4 band A4) > 0 -> |
| 552 | + {true, Addr}; |
| 553 | + el/=se -> |
552 | 554 | false |
553 | 555 | end |
554 | | - end, Ifs) of |
555 | | - [{If, IP}] -> |
556 | | - ?LOG_DEBUG("this host address is ~s on iface ~s", [IP, If]), |
557 | | - IP; |
558 | | - [{If, IP}|_] -> |
559 | | - logger:warning("This host has multiple network interfaces configured:" |
560 | | - " selecting ~p on ~s", [IP, If]), |
561 | | - IP; |
| 556 | + end, |
| 557 | + Ifs) of |
| 558 | + [{A1, A2, A3, A4}|_] -> |
| 559 | + lists:flatten(io_lib:format("~b.~b.~b.~b", [A1, A2, A3, A4])); |
562 | 560 | [] -> |
563 | | - logger:warning("This host has no network interfaces with assigned addresses:" |
564 | | - " falling back to 127.0.0.1", []), |
| 561 | + logger:warning("No network interfaces with assigned addresses matching ~s:" |
| 562 | + " falling back to 127.0.0.1", [Mask]), |
565 | 563 | "127.0.0.1" |
566 | 564 | end. |
567 | 565 |
|
| 566 | +extract_addr(IfItem) -> |
| 567 | + case proplists:get_value(addr, IfItem) of |
| 568 | + AA when AA /= undefined, |
| 569 | + AA /= {0,0,0,0}, |
| 570 | + size(AA) == 4 -> |
| 571 | + AA; |
| 572 | + _ -> |
| 573 | + {127,0,0,1} |
| 574 | + end. |
| 575 | + |
| 576 | +-spec this_host_addresses() -> [string()]. |
| 577 | +this_host_addresses() -> |
| 578 | + {ok, Ifs} = inet:getifaddrs(), |
| 579 | + lists:filtermap( |
| 580 | + fun({_If, PL}) -> |
| 581 | + case proplists:get_value(addr, PL) of |
| 582 | + AA when AA /= undefined, |
| 583 | + AA /= {0,0,0,0}, |
| 584 | + size(AA) == 4 -> |
| 585 | + {A1, A2, A3, A4} = AA, |
| 586 | + {true, lists:flatten(io_lib:format("~b.~b.~b.~b", [A1, A2, A3, A4]))}; |
| 587 | + _ -> |
| 588 | + false |
| 589 | + end |
| 590 | + end, Ifs). |
| 591 | + |
568 | 592 | -ifdef(TEST). |
569 | 593 |
|
570 | 594 | camel_case_test() -> |
|
0 commit comments