Comparison

core/portmanager.lua @ 4542:50aca1e0bfbd

portmanager: One manager to, in the darkness, bind them
author Matthew Wild <mwild1@gmail.com>
date Sun, 22 Jan 2012 23:55:48 +0000
child 4546:c686860ef410
comparison
equal deleted inserted replaced
4541:05f5ec99da77 4542:50aca1e0bfbd
1
2 local multitable = require "util.multitable";
3 local fire_event = prosody.events.fire_event;
4
5 --- Config
6
7 local default_interfaces = { "*" };
8 local default_local_interfaces = { "127.0.0.1" };
9 if config.get("*", "use_ipv6") then
10 table.insert(default_interfaces, "::");
11 table.insert(default_local_interfaces, "::1");
12 end
13
14 --- Private state
15
16 -- service_name -> service_info
17 local services = {};
18
19 -- service_name, interface (string), port (number)
20 local active_services = multitable.new();
21
22 --- Private helpers
23
24 local function error_to_friendly_message(service_name, err)
25 local friendly_message = err;
26 if err:match(" in use") then
27 -- FIXME: Use service_name here
28 if port == 5222 or port == 5223 or port == 5269 then
29 friendly_message = "check that Prosody or another XMPP server is "
30 .."not already running and using this port";
31 elseif port == 80 or port == 81 then
32 friendly_message = "check that a HTTP server is not already using "
33 .."this port";
34 elseif port == 5280 then
35 friendly_message = "check that Prosody or a BOSH connection manager "
36 .."is not already running";
37 else
38 friendly_message = "this port is in use by another application";
39 end
40 elseif err:match("permission") then
41 friendly_message = "Prosody does not have sufficient privileges to use this port";
42 elseif err == "no ssl context" then
43 if not config.get("*", "core", "ssl") then
44 friendly_message = "there is no 'ssl' config under Host \"*\" which is "
45 .."require for legacy SSL ports";
46 else
47 friendly_message = "initializing SSL support failed, see previous log entries";
48 end
49 end
50 return friendly_message;
51 end
52
53 module("portmanager", package.seeall);
54
55 --- Public API
56
57 function activate_service(service_name)
58 local service_info = services[service_name];
59 if not service_info then
60 return nil, "Unknown service: "..service_name;
61 end
62
63 local bind_interfaces = set.new(config.get("*", service_name.."_interfaces")
64 or config.get("*", service_name.."_interface") -- COMPAT w/pre-0.9
65 or config.get("*", "interfaces")
66 or config.get("*", "interface") -- COMPAT w/pre-0.9
67 or (service_info.private and default_local_interfaces)
68 or service_info.default_interface -- COMPAT w/pre0.9
69 or default_interfaces);
70
71 local bind_ports = set.new(config.get("*", service_name.."_ports")
72 or (service_info.multiplex and config.get("*", "ports"))
73 or service_info.default_ports
74 or {service_info.default_port});
75
76 local listener = service_info.listener;
77 local mode = listener.default_mode or "*a";
78 local ssl;
79 if service_info.encryption == "ssl" then
80 ssl = prosody.global_ssl_ctx;
81 if not ssl then
82 return nil, "global-ssl-context-required";
83 end
84 end
85
86 for interface in bind_interfaces do
87 for port in bind_ports do
88 if not service_info.multiplex and #active_services:search(nil, interface, port) > 0 then
89 log("error", "Multiple services configured to listen on the same port: %s, %s", table.concat(active_services:search(nil, interface, port), ", "), service_name);
90 else
91 local handler, err = server.addserver(interface, port, listener, mode, ssl);
92 if not handler then
93 log("error", "Failed to open server port %d on %s, %s", port, interface, error_to_friendly_message(service_name, err));
94 else
95 log("debug", "Added listening service %s to [%s]:%d", service_name, interface, port);
96 active_services:add(service_name, interface, port, {
97 server = handler;
98 service = service_info;
99 });
100 end
101 end
102 end
103 end
104 log("info", "Activated service '%s'", service_name);
105 end
106
107 function deactivate(service_name)
108 local active = active_services:search(service_name)[1];
109 if not active then return; end
110 for interface, ports in pairs(active) do
111 for port, active_service in pairs(ports) do
112 active_service:close();
113 active_services:remove(service_name, interface, port, active_service);
114 log("debug", "Removed listening service %s from [%s]:%d", service_name, interface, port);
115 end
116 end
117 log("info", "Deactivated service '%s'", service_name);
118 end
119
120 function register_service(service_name, service_info)
121 services[service_name] = service_info;
122
123 if not active_services[service_name] then
124 activate_service(service_name);
125 end
126
127 fire_event("service-added", { name = service_name, service = service_info });
128 return true;
129 end
130
131
132 return _M;