erlang网络库
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

118 righe
3.2 KiB

5 anni fa
5 anni fa
5 anni fa
5 anni fa
5 anni fa
5 anni fa
5 anni fa
5 anni fa
  1. -module(ntCom).
  2. -compile([export_all, nowarn_export_all]).
  3. -spec mergeOpts(Defaults :: list(), Options :: list()) -> list().
  4. mergeOpts(Defaults, Options) ->
  5. lists:foldl(
  6. fun({Opt, Val}, Acc) ->
  7. lists:keystore(Opt, 1, Acc, {Opt, Val});
  8. (Opt, Acc) ->
  9. lists:usort([Opt | Acc])
  10. end,
  11. Defaults, Options).
  12. mergeAddr({Addr, _Port}, SockOpts) ->
  13. lists:keystore(ip, 1, SockOpts, {ip, Addr});
  14. mergeAddr(_Port, SockOpts) ->
  15. SockOpts.
  16. getPort({_Addr, Port}) -> Port;
  17. getPort(Port) -> Port.
  18. fixIpPort(IpOrStr, Port) ->
  19. if
  20. is_list(IpOrStr), is_integer(Port) ->
  21. {ok, IP} = inet:parse_address(v),
  22. {IP, Port};
  23. is_tuple(IpOrStr), is_integer(Port) ->
  24. case isIpv4OrIpv6(IpOrStr) of
  25. true ->
  26. {IpOrStr, Port};
  27. false ->
  28. error({invalid_ip, IpOrStr})
  29. end;
  30. true ->
  31. error({invalid_ip_port, IpOrStr, Port})
  32. end.
  33. parseAddr({Addr, Port}) when is_list(Addr), is_integer(Port) ->
  34. {ok, IPAddr} = inet:parse_address(Addr),
  35. {IPAddr, Port};
  36. parseAddr({Addr, Port}) when is_tuple(Addr), is_integer(Port) ->
  37. case isIpv4OrIpv6(Addr) of
  38. true ->
  39. {Addr, Port};
  40. false ->
  41. error(invalid_ipaddr)
  42. end;
  43. parseAddr(Port) ->
  44. Port.
  45. isIpv4OrIpv6({A, B, C, D}) ->
  46. A >= 0 andalso A =< 255 andalso
  47. B >= 0 andalso B =< 255 andalso
  48. C >= 0 andalso C =< 255 andalso
  49. D >= 0 andalso D =< 255;
  50. isIpv4OrIpv6({A, B, C, D, E, F, G, H}) ->
  51. A >= 0 andalso A =< 65535 andalso
  52. B >= 0 andalso B =< 65535 andalso
  53. C >= 0 andalso C =< 65535 andalso
  54. D >= 0 andalso D =< 65535 andalso
  55. E >= 0 andalso E =< 65535 andalso
  56. F >= 0 andalso F =< 65535 andalso
  57. G >= 0 andalso G =< 65535 andalso
  58. H >= 0 andalso H =< 65535;
  59. isIpv4OrIpv6(_) ->
  60. false.
  61. %% @doc Return true if the value is an ipv4 address
  62. isIpv4({A, B, C, D}) ->
  63. A >= 0 andalso A =< 255 andalso
  64. B >= 0 andalso B =< 255 andalso
  65. C >= 0 andalso C =< 255 andalso
  66. D >= 0 andalso D =< 255;
  67. isIpv4(_) ->
  68. false.
  69. %% @doc Return true if the value is an ipv6 address
  70. isIpv6({A, B, C, D, E, F, G, H}) ->
  71. A >= 0 andalso A =< 65535 andalso
  72. B >= 0 andalso B =< 65535 andalso
  73. C >= 0 andalso C =< 65535 andalso
  74. D >= 0 andalso D =< 65535 andalso
  75. E >= 0 andalso E =< 65535 andalso
  76. F >= 0 andalso F =< 65535 andalso
  77. G >= 0 andalso G =< 65535 andalso
  78. H >= 0 andalso H =< 65535;
  79. isIpv6(_) ->
  80. false.
  81. getListValue(Key, List, Default) ->
  82. case lists:keyfind(Key, 1, List) of
  83. false ->
  84. Default;
  85. {Key, Value} ->
  86. Value
  87. end.
  88. serverName(PoolName, Index) ->
  89. list_to_atom(atom_to_list(PoolName) ++ "_" ++ integer_to_list(Index)).
  90. asName(tcp, PrName) ->
  91. binary_to_atom(<<(atom_to_binary(PrName))/binary, "TAs">>);
  92. asName(ssl, PrName) ->
  93. binary_to_atom(<<(atom_to_binary(PrName))/binary, "SAs">>);
  94. asName(udp, PrName) ->
  95. binary_to_atom(<<(atom_to_binary(PrName))/binary, "UOs">>).
  96. lsName(tcp, PrName) ->
  97. binary_to_atom(<<(atom_to_binary(PrName))/binary, "TLs">>);
  98. lsName(ssl, PrName) ->
  99. binary_to_atom(<<(atom_to_binary(PrName))/binary, "SLs">>);
  100. lsName(udp, PrName) ->
  101. binary_to_atom(<<(atom_to_binary(PrName))/binary, "URs">>).