Software /
code /
prosody
Comparison
core/portmanager.lua @ 4797:e239668aa6d2
Merge 0.9->trunk
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Sun, 29 Apr 2012 02:10:55 +0100 |
parent | 4789:bed0c6117358 |
child | 4800:7ce502f21270 |
comparison
equal
deleted
inserted
replaced
4796:04a34287dc12 | 4797:e239668aa6d2 |
---|---|
1 local config = require "core.configmanager"; | |
2 local server = require "net.server"; | |
3 | |
4 local log = require "util.logger".init("portmanager"); | |
5 local multitable = require "util.multitable"; | |
6 local set = require "util.set"; | |
7 | |
8 local table, package = table, package; | |
9 local setmetatable, rawset, rawget = setmetatable, rawset, rawget; | |
10 local type, tonumber = type, tonumber; | |
11 | |
12 local prosody = prosody; | |
13 local fire_event = prosody.events.fire_event; | |
14 | |
15 module "portmanager"; | |
16 | |
17 --- Config | |
18 | |
19 local default_interfaces = { "*" }; | |
20 local default_local_interfaces = { "127.0.0.1" }; | |
21 if config.get("*", "use_ipv6") then | |
22 table.insert(default_interfaces, "::"); | |
23 table.insert(default_local_interfaces, "::1"); | |
24 end | |
25 | |
26 --- Private state | |
27 | |
28 -- service_name -> { service_info, ... } | |
29 local services = setmetatable({}, { __index = function (t, k) rawset(t, k, {}); return rawget(t, k); end }); | |
30 | |
31 -- service_name, interface (string), port (number) | |
32 local active_services = multitable.new(); | |
33 | |
34 --- Private helpers | |
35 | |
36 local function error_to_friendly_message(service_name, port, err) | |
37 local friendly_message = err; | |
38 if err:match(" in use") then | |
39 -- FIXME: Use service_name here | |
40 if port == 5222 or port == 5223 or port == 5269 then | |
41 friendly_message = "check that Prosody or another XMPP server is " | |
42 .."not already running and using this port"; | |
43 elseif port == 80 or port == 81 then | |
44 friendly_message = "check that a HTTP server is not already using " | |
45 .."this port"; | |
46 elseif port == 5280 then | |
47 friendly_message = "check that Prosody or a BOSH connection manager " | |
48 .."is not already running"; | |
49 else | |
50 friendly_message = "this port is in use by another application"; | |
51 end | |
52 elseif err:match("permission") then | |
53 friendly_message = "Prosody does not have sufficient privileges to use this port"; | |
54 elseif err == "no ssl context" then | |
55 if not config.get("*", "core", "ssl") then | |
56 friendly_message = "there is no 'ssl' config under Host \"*\" which is " | |
57 .."require for legacy SSL ports"; | |
58 else | |
59 friendly_message = "initializing SSL support failed, see previous log entries"; | |
60 end | |
61 end | |
62 return friendly_message; | |
63 end | |
64 | |
65 prosody.events.add_handler("item-added/net-provider", function (event) | |
66 local item = event.item; | |
67 register_service(item.name, item); | |
68 end); | |
69 prosody.events.add_handler("item-removed/net-provider", function (event) | |
70 local item = event.item; | |
71 unregister_service(item.name, item); | |
72 end); | |
73 | |
74 --- Public API | |
75 | |
76 function activate(service_name) | |
77 local service_info = services[service_name][1]; | |
78 if not service_info then | |
79 return nil, "Unknown service: "..service_name; | |
80 end | |
81 | |
82 local listener = service_info.listener; | |
83 | |
84 local config_prefix = (service_info.config_prefix or service_name).."_"; | |
85 if config_prefix == "_" then | |
86 config_prefix = ""; | |
87 end | |
88 | |
89 local bind_interfaces = config.get("*", config_prefix.."interfaces") | |
90 or config.get("*", config_prefix.."interface") -- COMPAT w/pre-0.9 | |
91 or (service_info.private and default_local_interfaces) | |
92 or config.get("*", "interfaces") | |
93 or config.get("*", "interface") -- COMPAT w/pre-0.9 | |
94 or listener.default_interface -- COMPAT w/pre0.9 | |
95 or default_interfaces | |
96 bind_interfaces = set.new(type(bind_interfaces)~="table" and {bind_interfaces} or bind_interfaces); | |
97 | |
98 local bind_ports = set.new(config.get("*", config_prefix.."ports") | |
99 or service_info.default_ports | |
100 or {service_info.default_port | |
101 or listener.default_port -- COMPAT w/pre-0.9 | |
102 }); | |
103 | |
104 local mode = listener.default_mode or "*a"; | |
105 local ssl; | |
106 if service_info.encryption == "ssl" then | |
107 ssl = prosody.global_ssl_ctx; | |
108 if not ssl then | |
109 return nil, "global-ssl-context-required"; | |
110 end | |
111 end | |
112 | |
113 for interface in bind_interfaces do | |
114 for port in bind_ports do | |
115 port = tonumber(port); | |
116 if #active_services:search(nil, interface, port) > 0 then | |
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>"); | |
118 else | |
119 local handler, err = server.addserver(interface, port, listener, mode, ssl); | |
120 if not handler then | |
121 log("error", "Failed to open server port %d on %s, %s", port, interface, error_to_friendly_message(service_name, port, err)); | |
122 else | |
123 log("debug", "Added listening service %s to [%s]:%d", service_name, interface, port); | |
124 active_services:add(service_name, interface, port, { | |
125 server = handler; | |
126 service = service_info; | |
127 }); | |
128 end | |
129 end | |
130 end | |
131 end | |
132 log("info", "Activated service '%s'", service_name); | |
133 return true; | |
134 end | |
135 | |
136 function deactivate(service_name) | |
137 local active = active_services:search(service_name)[1]; | |
138 if not active then return; end | |
139 for interface, ports in pairs(active) do | |
140 for port, active_service in pairs(ports) do | |
141 close(interface, port); | |
142 end | |
143 end | |
144 log("info", "Deactivated service '%s'", service_name); | |
145 end | |
146 | |
147 function register_service(service_name, service_info) | |
148 table.insert(services[service_name], service_info); | |
149 | |
150 if not active_services:get(service_name) then | |
151 log("debug", "No active service for %s, activating...", service_name); | |
152 local ok, err = activate(service_name); | |
153 if not ok then | |
154 log("error", "Failed to activate service '%s': %s", service_name, err or "unknown error"); | |
155 end | |
156 end | |
157 | |
158 fire_event("service-added", { name = service_name, service = service_info }); | |
159 return true; | |
160 end | |
161 | |
162 function unregister_service(service_name, service_info) | |
163 local service_info_list = services[service_name]; | |
164 for i, service in ipairs(service_info_list) do | |
165 if service == service_info then | |
166 table.remove(service_info_list, i); | |
167 end | |
168 end | |
169 if active_services[service_name] == service_info then | |
170 deactivate(service_name); | |
171 if #service_info_list > 0 then -- Other services registered with this name | |
172 activate(service_name); -- Re-activate with the next available one | |
173 end | |
174 end | |
175 fire_event("service-removed", { name = service_name, service = service_info }); | |
176 end | |
177 | |
178 function close(interface, port) | |
179 local service, server = get_service_at(interface, port); | |
180 if not service then | |
181 return false, "port-not-open"; | |
182 end | |
183 server:close(); | |
184 active_services:remove(service.name, interface, port); | |
185 log("debug", "Removed listening service %s from [%s]:%d", service.name, interface, port); | |
186 return true; | |
187 end | |
188 | |
189 function get_service_at(interface, port) | |
190 local data = active_services:search(nil, interface, port)[1][1]; | |
191 return data.service, data.server; | |
192 end | |
193 | |
194 function get_service(service_name) | |
195 return services[service_name]; | |
196 end | |
197 | |
198 function get_active_services(...) | |
199 return active_services; | |
200 end | |
201 | |
202 function get_registered_services() | |
203 return services; | |
204 end | |
205 | |
206 return _M; |