Software /
code /
prosody
Annotate
core/portmanager.lua @ 4834:878f75ccc4fb
mod_s2s, mod_auth_anonymous, hostmanager: Remove disallow_s2s flag, deprecate the config option of the same name (disable mod_s2s instead), and add 'allow_anonymous_s2s' to separately control s2s for anonymous users
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Fri, 11 May 2012 00:56:18 +0100 |
parent | 4809:3068ec951ffd |
child | 4856:3e3e282f20a3 |
rev | line source |
---|---|
4741
0653476ac3a3
portmanager: Explicitly import some libraries
Matthew Wild <mwild1@gmail.com>
parents:
4687
diff
changeset
|
1 local config = require "core.configmanager"; |
0653476ac3a3
portmanager: Explicitly import some libraries
Matthew Wild <mwild1@gmail.com>
parents:
4687
diff
changeset
|
2 local server = require "net.server"; |
4542
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
3 |
4741
0653476ac3a3
portmanager: Explicitly import some libraries
Matthew Wild <mwild1@gmail.com>
parents:
4687
diff
changeset
|
4 local log = require "util.logger".init("portmanager"); |
4542
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
5 local multitable = require "util.multitable"; |
4741
0653476ac3a3
portmanager: Explicitly import some libraries
Matthew Wild <mwild1@gmail.com>
parents:
4687
diff
changeset
|
6 local set = require "util.set"; |
0653476ac3a3
portmanager: Explicitly import some libraries
Matthew Wild <mwild1@gmail.com>
parents:
4687
diff
changeset
|
7 |
4744
3be37768720d
portmanager: Fix breakage (import ALL the functions)
Matthew Wild <mwild1@gmail.com>
parents:
4743
diff
changeset
|
8 local table, package = table, package; |
3be37768720d
portmanager: Fix breakage (import ALL the functions)
Matthew Wild <mwild1@gmail.com>
parents:
4743
diff
changeset
|
9 local setmetatable, rawset, rawget = setmetatable, rawset, rawget; |
4809 | 10 local type, tonumber, ipairs, pairs = type, tonumber, ipairs, pairs; |
4744
3be37768720d
portmanager: Fix breakage (import ALL the functions)
Matthew Wild <mwild1@gmail.com>
parents:
4743
diff
changeset
|
11 |
4741
0653476ac3a3
portmanager: Explicitly import some libraries
Matthew Wild <mwild1@gmail.com>
parents:
4687
diff
changeset
|
12 local prosody = prosody; |
4542
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
13 local fire_event = prosody.events.fire_event; |
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
14 |
4742
23c2ece2c8bc
portmanager: Add module() definition
Matthew Wild <mwild1@gmail.com>
parents:
4741
diff
changeset
|
15 module "portmanager"; |
23c2ece2c8bc
portmanager: Add module() definition
Matthew Wild <mwild1@gmail.com>
parents:
4741
diff
changeset
|
16 |
4542
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
17 --- Config |
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 |
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 local default_interfaces = { "*" }; |
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
20 local default_local_interfaces = { "127.0.0.1" }; |
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
21 if config.get("*", "use_ipv6") then |
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
22 table.insert(default_interfaces, "::"); |
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
23 table.insert(default_local_interfaces, "::1"); |
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
24 end |
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
25 |
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
26 --- Private state |
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 |
4607
7f45b2cb3c03
portmanager: Add unregister_service(), and allow multiple services with the same name (they get queued)
Matthew Wild <mwild1@gmail.com>
parents:
4598
diff
changeset
|
28 -- service_name -> { service_info, ... } |
7f45b2cb3c03
portmanager: Add unregister_service(), and allow multiple services with the same name (they get queued)
Matthew Wild <mwild1@gmail.com>
parents:
4598
diff
changeset
|
29 local services = setmetatable({}, { __index = function (t, k) rawset(t, k, {}); return rawget(t, k); end }); |
4542
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
30 |
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
31 -- service_name, interface (string), port (number) |
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
32 local active_services = multitable.new(); |
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
33 |
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
34 --- Private helpers |
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
35 |
4546
c686860ef410
portmanager: Pass port to friendly_error_message()
Matthew Wild <mwild1@gmail.com>
parents:
4542
diff
changeset
|
36 local function error_to_friendly_message(service_name, port, err) |
4542
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
37 local friendly_message = err; |
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
38 if err:match(" in use") then |
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
39 -- FIXME: Use service_name here |
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
40 if port == 5222 or port == 5223 or port == 5269 then |
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
41 friendly_message = "check that Prosody or another XMPP server is " |
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
42 .."not already running and using this port"; |
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
43 elseif port == 80 or port == 81 then |
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
44 friendly_message = "check that a HTTP server is not already using " |
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
45 .."this port"; |
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
46 elseif port == 5280 then |
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
47 friendly_message = "check that Prosody or a BOSH connection manager " |
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
48 .."is not already running"; |
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
49 else |
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
50 friendly_message = "this port is in use by another application"; |
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
51 end |
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
52 elseif err:match("permission") then |
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
53 friendly_message = "Prosody does not have sufficient privileges to use this port"; |
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
54 elseif err == "no ssl context" then |
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
55 if not config.get("*", "core", "ssl") then |
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
56 friendly_message = "there is no 'ssl' config under Host \"*\" which is " |
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
57 .."require for legacy SSL ports"; |
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
58 else |
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
59 friendly_message = "initializing SSL support failed, see previous log entries"; |
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
60 end |
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
61 end |
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
62 return friendly_message; |
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
63 end |
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
64 |
4608
01d52f31b6b3
portmanager: Support item-added/net-provider (global and shared modules only!)
Matthew Wild <mwild1@gmail.com>
parents:
4607
diff
changeset
|
65 prosody.events.add_handler("item-added/net-provider", function (event) |
01d52f31b6b3
portmanager: Support item-added/net-provider (global and shared modules only!)
Matthew Wild <mwild1@gmail.com>
parents:
4607
diff
changeset
|
66 local item = event.item; |
01d52f31b6b3
portmanager: Support item-added/net-provider (global and shared modules only!)
Matthew Wild <mwild1@gmail.com>
parents:
4607
diff
changeset
|
67 register_service(item.name, item); |
01d52f31b6b3
portmanager: Support item-added/net-provider (global and shared modules only!)
Matthew Wild <mwild1@gmail.com>
parents:
4607
diff
changeset
|
68 end); |
01d52f31b6b3
portmanager: Support item-added/net-provider (global and shared modules only!)
Matthew Wild <mwild1@gmail.com>
parents:
4607
diff
changeset
|
69 prosody.events.add_handler("item-removed/net-provider", function (event) |
01d52f31b6b3
portmanager: Support item-added/net-provider (global and shared modules only!)
Matthew Wild <mwild1@gmail.com>
parents:
4607
diff
changeset
|
70 local item = event.item; |
01d52f31b6b3
portmanager: Support item-added/net-provider (global and shared modules only!)
Matthew Wild <mwild1@gmail.com>
parents:
4607
diff
changeset
|
71 unregister_service(item.name, item); |
01d52f31b6b3
portmanager: Support item-added/net-provider (global and shared modules only!)
Matthew Wild <mwild1@gmail.com>
parents:
4607
diff
changeset
|
72 end); |
01d52f31b6b3
portmanager: Support item-added/net-provider (global and shared modules only!)
Matthew Wild <mwild1@gmail.com>
parents:
4607
diff
changeset
|
73 |
4542
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
74 --- Public API |
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
75 |
4743
70d68e789d93
portmanager: Rename activate_service() to activate() (to match deactivate())
Matthew Wild <mwild1@gmail.com>
parents:
4742
diff
changeset
|
76 function activate(service_name) |
4607
7f45b2cb3c03
portmanager: Add unregister_service(), and allow multiple services with the same name (they get queued)
Matthew Wild <mwild1@gmail.com>
parents:
4598
diff
changeset
|
77 local service_info = services[service_name][1]; |
4542
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
78 if not service_info then |
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
79 return nil, "Unknown service: "..service_name; |
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
80 end |
4615
29a9988c1e1b
portmanager: Allow services to specify their config option prefix
Matthew Wild <mwild1@gmail.com>
parents:
4612
diff
changeset
|
81 |
4616
03d9fe1bcdd3
portmanager: Fix pre-0.9 compatibility by taking default_interface and default_port from the listener instead of service table
Matthew Wild <mwild1@gmail.com>
parents:
4615
diff
changeset
|
82 local listener = service_info.listener; |
03d9fe1bcdd3
portmanager: Fix pre-0.9 compatibility by taking default_interface and default_port from the listener instead of service table
Matthew Wild <mwild1@gmail.com>
parents:
4615
diff
changeset
|
83 |
4615
29a9988c1e1b
portmanager: Allow services to specify their config option prefix
Matthew Wild <mwild1@gmail.com>
parents:
4612
diff
changeset
|
84 local config_prefix = (service_info.config_prefix or service_name).."_"; |
29a9988c1e1b
portmanager: Allow services to specify their config option prefix
Matthew Wild <mwild1@gmail.com>
parents:
4612
diff
changeset
|
85 if config_prefix == "_" then |
29a9988c1e1b
portmanager: Allow services to specify their config option prefix
Matthew Wild <mwild1@gmail.com>
parents:
4612
diff
changeset
|
86 config_prefix = ""; |
29a9988c1e1b
portmanager: Allow services to specify their config option prefix
Matthew Wild <mwild1@gmail.com>
parents:
4612
diff
changeset
|
87 end |
4542
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
88 |
4687
bd3a852b949a
portmanager: Fix selecting bind_interfaces from pre-0.9 config options.
Kim Alvefur <zash@zash.se>
parents:
4677
diff
changeset
|
89 local bind_interfaces = config.get("*", config_prefix.."interfaces") |
4615
29a9988c1e1b
portmanager: Allow services to specify their config option prefix
Matthew Wild <mwild1@gmail.com>
parents:
4612
diff
changeset
|
90 or config.get("*", config_prefix.."interface") -- COMPAT w/pre-0.9 |
4583
6f2789939d35
core.portmanager: Make sure the private flag takes precedence over global interfaces
Kim Alvefur <zash@zash.se>
parents:
4546
diff
changeset
|
91 or (service_info.private and default_local_interfaces) |
4542
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
92 or config.get("*", "interfaces") |
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
93 or config.get("*", "interface") -- COMPAT w/pre-0.9 |
4616
03d9fe1bcdd3
portmanager: Fix pre-0.9 compatibility by taking default_interface and default_port from the listener instead of service table
Matthew Wild <mwild1@gmail.com>
parents:
4615
diff
changeset
|
94 or listener.default_interface -- COMPAT w/pre0.9 |
4687
bd3a852b949a
portmanager: Fix selecting bind_interfaces from pre-0.9 config options.
Kim Alvefur <zash@zash.se>
parents:
4677
diff
changeset
|
95 or default_interfaces |
bd3a852b949a
portmanager: Fix selecting bind_interfaces from pre-0.9 config options.
Kim Alvefur <zash@zash.se>
parents:
4677
diff
changeset
|
96 bind_interfaces = set.new(type(bind_interfaces)~="table" and {bind_interfaces} or bind_interfaces); |
4542
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
97 |
4616
03d9fe1bcdd3
portmanager: Fix pre-0.9 compatibility by taking default_interface and default_port from the listener instead of service table
Matthew Wild <mwild1@gmail.com>
parents:
4615
diff
changeset
|
98 local bind_ports = set.new(config.get("*", config_prefix.."ports") |
4542
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
99 or service_info.default_ports |
4624
3e4715d44561
portmanager: Support 'default_port' in service options
Matthew Wild <mwild1@gmail.com>
parents:
4618
diff
changeset
|
100 or {service_info.default_port |
3e4715d44561
portmanager: Support 'default_port' in service options
Matthew Wild <mwild1@gmail.com>
parents:
4618
diff
changeset
|
101 or listener.default_port -- COMPAT w/pre-0.9 |
3e4715d44561
portmanager: Support 'default_port' in service options
Matthew Wild <mwild1@gmail.com>
parents:
4618
diff
changeset
|
102 }); |
4542
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
103 |
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
104 local mode = listener.default_mode or "*a"; |
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
105 local ssl; |
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
106 if service_info.encryption == "ssl" then |
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
107 ssl = prosody.global_ssl_ctx; |
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
108 if not ssl then |
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
109 return nil, "global-ssl-context-required"; |
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
110 end |
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
111 end |
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
112 |
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
113 for interface in bind_interfaces do |
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
114 for port in bind_ports do |
4786
ad6d4ab40b20
portmanager: Ensure port is always a number (thanks Zash)
Matthew Wild <mwild1@gmail.com>
parents:
4744
diff
changeset
|
115 port = tonumber(port); |
4617
d8e56e6ac8d4
portmanager: Remove check for service_info.multiplex (now implemented in mod_net_multiplex)
Matthew Wild <mwild1@gmail.com>
parents:
4616
diff
changeset
|
116 if #active_services:search(nil, interface, port) > 0 then |
4609
83a5377ffea2
portmanager: Fix log message when multiple services are configured to use the same port
Matthew Wild <mwild1@gmail.com>
parents:
4608
diff
changeset
|
117 log("error", "Multiple services configured to listen on the same port ([%s]:%d): %s, %s", interface, port, active_services:search(nil, interface, port)[1][1].service.name or "<unnamed>", service_name or "<unnamed>"); |
4542
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
118 else |
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
119 local handler, err = server.addserver(interface, port, listener, mode, ssl); |
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
120 if not handler then |
4546
c686860ef410
portmanager: Pass port to friendly_error_message()
Matthew Wild <mwild1@gmail.com>
parents:
4542
diff
changeset
|
121 log("error", "Failed to open server port %d on %s, %s", port, interface, error_to_friendly_message(service_name, port, err)); |
4542
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
122 else |
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
123 log("debug", "Added listening service %s to [%s]:%d", service_name, interface, port); |
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
124 active_services:add(service_name, interface, port, { |
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
125 server = handler; |
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
126 service = service_info; |
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
127 }); |
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
128 end |
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
129 end |
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
130 end |
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
131 end |
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
132 log("info", "Activated service '%s'", service_name); |
4607
7f45b2cb3c03
portmanager: Add unregister_service(), and allow multiple services with the same name (they get queued)
Matthew Wild <mwild1@gmail.com>
parents:
4598
diff
changeset
|
133 return true; |
4542
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
134 end |
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
135 |
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
136 function deactivate(service_name) |
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
137 local active = active_services:search(service_name)[1]; |
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
138 if not active then return; end |
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
139 for interface, ports in pairs(active) do |
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
140 for port, active_service in pairs(ports) do |
4677
05d8b4099cf5
portmanager: Add get_service_at(interface, port) and close(interface, port)
Matthew Wild <mwild1@gmail.com>
parents:
4624
diff
changeset
|
141 close(interface, port); |
4542
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
142 end |
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
143 end |
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
144 log("info", "Deactivated service '%s'", service_name); |
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
145 end |
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
146 |
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
147 function register_service(service_name, service_info) |
4607
7f45b2cb3c03
portmanager: Add unregister_service(), and allow multiple services with the same name (they get queued)
Matthew Wild <mwild1@gmail.com>
parents:
4598
diff
changeset
|
148 table.insert(services[service_name], service_info); |
4542
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
149 |
4607
7f45b2cb3c03
portmanager: Add unregister_service(), and allow multiple services with the same name (they get queued)
Matthew Wild <mwild1@gmail.com>
parents:
4598
diff
changeset
|
150 if not active_services:get(service_name) then |
7f45b2cb3c03
portmanager: Add unregister_service(), and allow multiple services with the same name (they get queued)
Matthew Wild <mwild1@gmail.com>
parents:
4598
diff
changeset
|
151 log("debug", "No active service for %s, activating...", service_name); |
4743
70d68e789d93
portmanager: Rename activate_service() to activate() (to match deactivate())
Matthew Wild <mwild1@gmail.com>
parents:
4742
diff
changeset
|
152 local ok, err = activate(service_name); |
4607
7f45b2cb3c03
portmanager: Add unregister_service(), and allow multiple services with the same name (they get queued)
Matthew Wild <mwild1@gmail.com>
parents:
4598
diff
changeset
|
153 if not ok then |
7f45b2cb3c03
portmanager: Add unregister_service(), and allow multiple services with the same name (they get queued)
Matthew Wild <mwild1@gmail.com>
parents:
4598
diff
changeset
|
154 log("error", "Failed to activate service '%s': %s", service_name, err or "unknown error"); |
7f45b2cb3c03
portmanager: Add unregister_service(), and allow multiple services with the same name (they get queued)
Matthew Wild <mwild1@gmail.com>
parents:
4598
diff
changeset
|
155 end |
4542
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
156 end |
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
157 |
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
158 fire_event("service-added", { name = service_name, service = service_info }); |
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
159 return true; |
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
160 end |
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
161 |
4607
7f45b2cb3c03
portmanager: Add unregister_service(), and allow multiple services with the same name (they get queued)
Matthew Wild <mwild1@gmail.com>
parents:
4598
diff
changeset
|
162 function unregister_service(service_name, service_info) |
7f45b2cb3c03
portmanager: Add unregister_service(), and allow multiple services with the same name (they get queued)
Matthew Wild <mwild1@gmail.com>
parents:
4598
diff
changeset
|
163 local service_info_list = services[service_name]; |
7f45b2cb3c03
portmanager: Add unregister_service(), and allow multiple services with the same name (they get queued)
Matthew Wild <mwild1@gmail.com>
parents:
4598
diff
changeset
|
164 for i, service in ipairs(service_info_list) do |
7f45b2cb3c03
portmanager: Add unregister_service(), and allow multiple services with the same name (they get queued)
Matthew Wild <mwild1@gmail.com>
parents:
4598
diff
changeset
|
165 if service == service_info then |
7f45b2cb3c03
portmanager: Add unregister_service(), and allow multiple services with the same name (they get queued)
Matthew Wild <mwild1@gmail.com>
parents:
4598
diff
changeset
|
166 table.remove(service_info_list, i); |
7f45b2cb3c03
portmanager: Add unregister_service(), and allow multiple services with the same name (they get queued)
Matthew Wild <mwild1@gmail.com>
parents:
4598
diff
changeset
|
167 end |
7f45b2cb3c03
portmanager: Add unregister_service(), and allow multiple services with the same name (they get queued)
Matthew Wild <mwild1@gmail.com>
parents:
4598
diff
changeset
|
168 end |
7f45b2cb3c03
portmanager: Add unregister_service(), and allow multiple services with the same name (they get queued)
Matthew Wild <mwild1@gmail.com>
parents:
4598
diff
changeset
|
169 if active_services[service_name] == service_info then |
7f45b2cb3c03
portmanager: Add unregister_service(), and allow multiple services with the same name (they get queued)
Matthew Wild <mwild1@gmail.com>
parents:
4598
diff
changeset
|
170 deactivate(service_name); |
7f45b2cb3c03
portmanager: Add unregister_service(), and allow multiple services with the same name (they get queued)
Matthew Wild <mwild1@gmail.com>
parents:
4598
diff
changeset
|
171 if #service_info_list > 0 then -- Other services registered with this name |
7f45b2cb3c03
portmanager: Add unregister_service(), and allow multiple services with the same name (they get queued)
Matthew Wild <mwild1@gmail.com>
parents:
4598
diff
changeset
|
172 activate(service_name); -- Re-activate with the next available one |
7f45b2cb3c03
portmanager: Add unregister_service(), and allow multiple services with the same name (they get queued)
Matthew Wild <mwild1@gmail.com>
parents:
4598
diff
changeset
|
173 end |
7f45b2cb3c03
portmanager: Add unregister_service(), and allow multiple services with the same name (they get queued)
Matthew Wild <mwild1@gmail.com>
parents:
4598
diff
changeset
|
174 end |
4612
8bb93860fe46
portmanager: Fire service-removed on unregister
Matthew Wild <mwild1@gmail.com>
parents:
4609
diff
changeset
|
175 fire_event("service-removed", { name = service_name, service = service_info }); |
4607
7f45b2cb3c03
portmanager: Add unregister_service(), and allow multiple services with the same name (they get queued)
Matthew Wild <mwild1@gmail.com>
parents:
4598
diff
changeset
|
176 end |
7f45b2cb3c03
portmanager: Add unregister_service(), and allow multiple services with the same name (they get queued)
Matthew Wild <mwild1@gmail.com>
parents:
4598
diff
changeset
|
177 |
4677
05d8b4099cf5
portmanager: Add get_service_at(interface, port) and close(interface, port)
Matthew Wild <mwild1@gmail.com>
parents:
4624
diff
changeset
|
178 function close(interface, port) |
05d8b4099cf5
portmanager: Add get_service_at(interface, port) and close(interface, port)
Matthew Wild <mwild1@gmail.com>
parents:
4624
diff
changeset
|
179 local service, server = get_service_at(interface, port); |
05d8b4099cf5
portmanager: Add get_service_at(interface, port) and close(interface, port)
Matthew Wild <mwild1@gmail.com>
parents:
4624
diff
changeset
|
180 if not service then |
05d8b4099cf5
portmanager: Add get_service_at(interface, port) and close(interface, port)
Matthew Wild <mwild1@gmail.com>
parents:
4624
diff
changeset
|
181 return false, "port-not-open"; |
05d8b4099cf5
portmanager: Add get_service_at(interface, port) and close(interface, port)
Matthew Wild <mwild1@gmail.com>
parents:
4624
diff
changeset
|
182 end |
05d8b4099cf5
portmanager: Add get_service_at(interface, port) and close(interface, port)
Matthew Wild <mwild1@gmail.com>
parents:
4624
diff
changeset
|
183 server:close(); |
05d8b4099cf5
portmanager: Add get_service_at(interface, port) and close(interface, port)
Matthew Wild <mwild1@gmail.com>
parents:
4624
diff
changeset
|
184 active_services:remove(service.name, interface, port); |
05d8b4099cf5
portmanager: Add get_service_at(interface, port) and close(interface, port)
Matthew Wild <mwild1@gmail.com>
parents:
4624
diff
changeset
|
185 log("debug", "Removed listening service %s from [%s]:%d", service.name, interface, port); |
05d8b4099cf5
portmanager: Add get_service_at(interface, port) and close(interface, port)
Matthew Wild <mwild1@gmail.com>
parents:
4624
diff
changeset
|
186 return true; |
05d8b4099cf5
portmanager: Add get_service_at(interface, port) and close(interface, port)
Matthew Wild <mwild1@gmail.com>
parents:
4624
diff
changeset
|
187 end |
05d8b4099cf5
portmanager: Add get_service_at(interface, port) and close(interface, port)
Matthew Wild <mwild1@gmail.com>
parents:
4624
diff
changeset
|
188 |
05d8b4099cf5
portmanager: Add get_service_at(interface, port) and close(interface, port)
Matthew Wild <mwild1@gmail.com>
parents:
4624
diff
changeset
|
189 function get_service_at(interface, port) |
05d8b4099cf5
portmanager: Add get_service_at(interface, port) and close(interface, port)
Matthew Wild <mwild1@gmail.com>
parents:
4624
diff
changeset
|
190 local data = active_services:search(nil, interface, port)[1][1]; |
05d8b4099cf5
portmanager: Add get_service_at(interface, port) and close(interface, port)
Matthew Wild <mwild1@gmail.com>
parents:
4624
diff
changeset
|
191 return data.service, data.server; |
05d8b4099cf5
portmanager: Add get_service_at(interface, port) and close(interface, port)
Matthew Wild <mwild1@gmail.com>
parents:
4624
diff
changeset
|
192 end |
05d8b4099cf5
portmanager: Add get_service_at(interface, port) and close(interface, port)
Matthew Wild <mwild1@gmail.com>
parents:
4624
diff
changeset
|
193 |
4597
25d89c7d6aee
portmanager: Add get_service()
Matthew Wild <mwild1@gmail.com>
parents:
4583
diff
changeset
|
194 function get_service(service_name) |
25d89c7d6aee
portmanager: Add get_service()
Matthew Wild <mwild1@gmail.com>
parents:
4583
diff
changeset
|
195 return services[service_name]; |
25d89c7d6aee
portmanager: Add get_service()
Matthew Wild <mwild1@gmail.com>
parents:
4583
diff
changeset
|
196 end |
4542
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
197 |
4598
d2bcb959d713
portmanager: Add get_active_services()
Matthew Wild <mwild1@gmail.com>
parents:
4597
diff
changeset
|
198 function get_active_services(...) |
d2bcb959d713
portmanager: Add get_active_services()
Matthew Wild <mwild1@gmail.com>
parents:
4597
diff
changeset
|
199 return active_services; |
d2bcb959d713
portmanager: Add get_active_services()
Matthew Wild <mwild1@gmail.com>
parents:
4597
diff
changeset
|
200 end |
d2bcb959d713
portmanager: Add get_active_services()
Matthew Wild <mwild1@gmail.com>
parents:
4597
diff
changeset
|
201 |
4618
1ec8122ddffe
portmanager: Add get_registered_services() to the public API
Matthew Wild <mwild1@gmail.com>
parents:
4617
diff
changeset
|
202 function get_registered_services() |
1ec8122ddffe
portmanager: Add get_registered_services() to the public API
Matthew Wild <mwild1@gmail.com>
parents:
4617
diff
changeset
|
203 return services; |
1ec8122ddffe
portmanager: Add get_registered_services() to the public API
Matthew Wild <mwild1@gmail.com>
parents:
4617
diff
changeset
|
204 end |
1ec8122ddffe
portmanager: Add get_registered_services() to the public API
Matthew Wild <mwild1@gmail.com>
parents:
4617
diff
changeset
|
205 |
4542
50aca1e0bfbd
portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
206 return _M; |