Software /
code /
prosody
Annotate
net/connlisteners.lua @ 4322:aff627b1ce95
connlistener, server_select, prosody: Add support for binding to multiple addresses
author | Florian Zeitz <florob@babelmonkeys.de> |
---|---|
date | Wed, 15 Jun 2011 23:44:18 +0200 |
parent | 4220:05f4db0459b1 |
child | 4797:e239668aa6d2 |
rev | line source |
---|---|
1523
841d61be198f
Remove version number from copyright headers
Matthew Wild <mwild1@gmail.com>
parents:
1099
diff
changeset
|
1 -- Prosody IM |
2923
b7049746bd29
Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents:
2076
diff
changeset
|
2 -- Copyright (C) 2008-2010 Matthew Wild |
b7049746bd29
Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents:
2076
diff
changeset
|
3 -- Copyright (C) 2008-2010 Waqas Hussain |
519
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
471
diff
changeset
|
4 -- |
758 | 5 -- This project is MIT/X11 licensed. Please see the |
6 -- COPYING file in the source package for more information. | |
519
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
471
diff
changeset
|
7 -- |
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
471
diff
changeset
|
8 |
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
471
diff
changeset
|
9 |
99
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
10 |
471
727d7bd97cd2
Fix for loading connlisteners when running without CFG_SOURCEDIR
Matthew Wild <mwild1@gmail.com>
parents:
467
diff
changeset
|
11 local listeners_dir = (CFG_SOURCEDIR or ".").."/net/"; |
658
1952fdcf1017
Fix specifying ports in config, and SSL support
Matthew Wild <mwild1@gmail.com>
parents:
624
diff
changeset
|
12 local server = require "net.server"; |
99
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
13 local log = require "util.logger".init("connlisteners"); |
2036
0f9c121713e1
connlisteners: Localize tostring, fixes possible traceback when LuaSec not installed
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
14 local tostring = tostring; |
4322
aff627b1ce95
connlistener, server_select, prosody: Add support for binding to multiple addresses
Florian Zeitz <florob@babelmonkeys.de>
parents:
4220
diff
changeset
|
15 local type = type |
aff627b1ce95
connlistener, server_select, prosody: Add support for binding to multiple addresses
Florian Zeitz <florob@babelmonkeys.de>
parents:
4220
diff
changeset
|
16 local ipairs = ipairs |
99
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
17 |
4220
05f4db0459b1
net.connlisteners: Log traceback on errors in listener loading.
Waqas Hussain <waqas20@gmail.com>
parents:
3540
diff
changeset
|
18 local dofile, xpcall, error = |
05f4db0459b1
net.connlisteners: Log traceback on errors in listener loading.
Waqas Hussain <waqas20@gmail.com>
parents:
3540
diff
changeset
|
19 dofile, xpcall, error |
05f4db0459b1
net.connlisteners: Log traceback on errors in listener loading.
Waqas Hussain <waqas20@gmail.com>
parents:
3540
diff
changeset
|
20 |
05f4db0459b1
net.connlisteners: Log traceback on errors in listener loading.
Waqas Hussain <waqas20@gmail.com>
parents:
3540
diff
changeset
|
21 local debug_traceback = debug.traceback; |
99
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
22 |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
23 module "connlisteners" |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
24 |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
25 local listeners = {}; |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
26 |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 function register(name, listener) |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
28 if listeners[name] and listeners[name] ~= listener then |
1099
127e6ae089f8
net.connlisteners: Lower log level of multiple listeners warning (not interesting to end-users)
Matthew Wild <mwild1@gmail.com>
parents:
908
diff
changeset
|
29 log("debug", "Listener %s is already registered, not registering any more", name); |
99
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
30 return false; |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
31 end |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
32 listeners[name] = listener; |
1099
127e6ae089f8
net.connlisteners: Lower log level of multiple listeners warning (not interesting to end-users)
Matthew Wild <mwild1@gmail.com>
parents:
908
diff
changeset
|
33 log("debug", "Registered connection listener %s", name); |
99
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
34 return true; |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
35 end |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
36 |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
37 function deregister(name) |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
38 listeners[name] = nil; |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
39 end |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
40 |
127 | 41 function get(name) |
42 local h = listeners[name]; | |
99
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
43 if not h then |
4220
05f4db0459b1
net.connlisteners: Log traceback on errors in listener loading.
Waqas Hussain <waqas20@gmail.com>
parents:
3540
diff
changeset
|
44 local ok, ret = xpcall(function() dofile(listeners_dir..name:gsub("[^%w%-]", "_").."_listener.lua") end, debug_traceback); |
2076
de2ae849b0b3
net.connlisteners: Log an error when a listener fails to load.
Waqas Hussain <waqas20@gmail.com>
parents:
2036
diff
changeset
|
45 if not ok then |
de2ae849b0b3
net.connlisteners: Log an error when a listener fails to load.
Waqas Hussain <waqas20@gmail.com>
parents:
2036
diff
changeset
|
46 log("error", "Error while loading listener '%s': %s", tostring(name), tostring(ret)); |
de2ae849b0b3
net.connlisteners: Log an error when a listener fails to load.
Waqas Hussain <waqas20@gmail.com>
parents:
2036
diff
changeset
|
47 return nil, ret; |
de2ae849b0b3
net.connlisteners: Log an error when a listener fails to load.
Waqas Hussain <waqas20@gmail.com>
parents:
2036
diff
changeset
|
48 end |
99
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
49 h = listeners[name]; |
127 | 50 end |
51 return h; | |
52 end | |
53 | |
54 function start(name, udata) | |
721
51233a8ae3d4
net.connlisteners: Fix to report errors loading connlisteners
Matthew Wild <mwild1@gmail.com>
parents:
661
diff
changeset
|
55 local h, err = get(name); |
127 | 56 if not h then |
721
51233a8ae3d4
net.connlisteners: Fix to report errors loading connlisteners
Matthew Wild <mwild1@gmail.com>
parents:
661
diff
changeset
|
57 error("No such connection module: "..name.. (err and (" ("..err..")") or ""), 0); |
99
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
58 end |
658
1952fdcf1017
Fix specifying ports in config, and SSL support
Matthew Wild <mwild1@gmail.com>
parents:
624
diff
changeset
|
59 |
4322
aff627b1ce95
connlistener, server_select, prosody: Add support for binding to multiple addresses
Florian Zeitz <florob@babelmonkeys.de>
parents:
4220
diff
changeset
|
60 local interfaces = (udata and udata.interface) or h.default_interface or "*"; |
aff627b1ce95
connlistener, server_select, prosody: Add support for binding to multiple addresses
Florian Zeitz <florob@babelmonkeys.de>
parents:
4220
diff
changeset
|
61 if type(interfaces) == "string" then interfaces = {interfaces}; end |
2104
466bd2cac62a
net.connlisteners: Standardise on new syntax for addserver(), and clean up a bit
Matthew Wild <mwild1@gmail.com>
parents:
2076
diff
changeset
|
62 local port = (udata and udata.port) or h.default_port or error("Can't start listener "..name.." because no port was specified, and it has no default port", 0); |
466bd2cac62a
net.connlisteners: Standardise on new syntax for addserver(), and clean up a bit
Matthew Wild <mwild1@gmail.com>
parents:
2076
diff
changeset
|
63 local mode = (udata and udata.mode) or h.default_mode or 1; |
466bd2cac62a
net.connlisteners: Standardise on new syntax for addserver(), and clean up a bit
Matthew Wild <mwild1@gmail.com>
parents:
2076
diff
changeset
|
64 local ssl = (udata and udata.ssl) or nil; |
466bd2cac62a
net.connlisteners: Standardise on new syntax for addserver(), and clean up a bit
Matthew Wild <mwild1@gmail.com>
parents:
2076
diff
changeset
|
65 local autossl = udata and udata.type == "ssl"; |
466bd2cac62a
net.connlisteners: Standardise on new syntax for addserver(), and clean up a bit
Matthew Wild <mwild1@gmail.com>
parents:
2076
diff
changeset
|
66 |
2551
5f15f21014c4
net.connlisteners: Return an error if no SSL context is supplied for a connection of type 'ssl'
Matthew Wild <mwild1@gmail.com>
parents:
2547
diff
changeset
|
67 if autossl and not ssl then |
5f15f21014c4
net.connlisteners: Return an error if no SSL context is supplied for a connection of type 'ssl'
Matthew Wild <mwild1@gmail.com>
parents:
2547
diff
changeset
|
68 return nil, "no ssl context"; |
739
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
721
diff
changeset
|
69 end |
4322
aff627b1ce95
connlistener, server_select, prosody: Add support for binding to multiple addresses
Florian Zeitz <florob@babelmonkeys.de>
parents:
4220
diff
changeset
|
70 |
aff627b1ce95
connlistener, server_select, prosody: Add support for binding to multiple addresses
Florian Zeitz <florob@babelmonkeys.de>
parents:
4220
diff
changeset
|
71 ok, err = true, {}; |
aff627b1ce95
connlistener, server_select, prosody: Add support for binding to multiple addresses
Florian Zeitz <florob@babelmonkeys.de>
parents:
4220
diff
changeset
|
72 for _, interface in ipairs(interfaces) do |
aff627b1ce95
connlistener, server_select, prosody: Add support for binding to multiple addresses
Florian Zeitz <florob@babelmonkeys.de>
parents:
4220
diff
changeset
|
73 local handler |
aff627b1ce95
connlistener, server_select, prosody: Add support for binding to multiple addresses
Florian Zeitz <florob@babelmonkeys.de>
parents:
4220
diff
changeset
|
74 handler, err[interface] = server.addserver(interface, port, h, mode, autossl and ssl or nil); |
aff627b1ce95
connlistener, server_select, prosody: Add support for binding to multiple addresses
Florian Zeitz <florob@babelmonkeys.de>
parents:
4220
diff
changeset
|
75 ok = ok and handler; |
aff627b1ce95
connlistener, server_select, prosody: Add support for binding to multiple addresses
Florian Zeitz <florob@babelmonkeys.de>
parents:
4220
diff
changeset
|
76 end |
aff627b1ce95
connlistener, server_select, prosody: Add support for binding to multiple addresses
Florian Zeitz <florob@babelmonkeys.de>
parents:
4220
diff
changeset
|
77 |
aff627b1ce95
connlistener, server_select, prosody: Add support for binding to multiple addresses
Florian Zeitz <florob@babelmonkeys.de>
parents:
4220
diff
changeset
|
78 return ok, err; |
99
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
79 end |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
80 |
467
66f145f5c932
Update Makefile to now pass config paths to prosody. Update prosody, modulemanager and connectionlisteners to obey these paths.
Matthew Wild <mwild1@gmail.com>
parents:
380
diff
changeset
|
81 return _M; |