-
Notifications
You must be signed in to change notification settings - Fork 12
/
DNRelay.py
167 lines (149 loc) · 5.86 KB
/
DNRelay.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# Copyright (c) 2015 Sippy Software, Inc. All rights reserved.
#
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation and/or
# other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
from threading import Thread, Condition
from errno import EINTR, EPIPE, ENOTCONN, ECONNRESET
import socket, select
from sippy.CLIManager import CLIConnectionManager
_MAX_RECURSE = 10
class _DNRLWorker(Thread):
spath = None
s = None
poller = None
wi_available = None
wi = None
sip_logger = None
def __init__(self, spath, sip_logger):
if spath.startswith('unix:'):
spath = spath[5:]
self.spath = spath
self.sip_logger = sip_logger
self.wi_available = Condition()
self.wi = []
Thread.__init__(self)
self.setDaemon(True)
self.start()
def connect(self):
self.s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
self.s.connect(self.spath)
self.poller = select.poll()
self.poller.register(self.s, select.POLLIN)
def deliver_dnotify(self, dnstring, _recurse = 0):
if self.s == None:
self.connect()
if _recurse > _MAX_RECURSE:
raise Exception('Cannot reconnect: %s', self.spath)
if not dnstring.endswith('\n'):
dnstring += '\n'
while True:
try:
self.s.send(dnstring)
break
except socket.error as why:
if why[0] == EINTR:
continue
elif why[0] in (EPIPE, ENOTCONN, ECONNRESET):
self.s = None
return self.deliver_dnotify(dnstring, _recurse + 1)
raise why
# Clean any incoming data on the socket
if len(self.poller.poll(0)) > 0:
try:
self.s.recv(1024)
except:
pass
return
def run(self):
while True:
self.wi_available.acquire()
while len(self.wi) == 0:
self.wi_available.wait()
wi = self.wi.pop(0)
self.wi_available.release()
if wi == None:
# Shutdown request
break
try:
self.deliver_dnotify(wi)
except Exception as e:
self.sip_logger.write(' cannot deliver notification "%s" to the "%s": %s' % \
(wi, self.spath, str(e)))
else:
self.sip_logger.write(' notification "%s" delivered to the "%s"' % \
(wi, self.spath))
self.sip_logger = None
def send_dnotify(self, dnstring):
self.wi_available.acquire()
self.wi.append(dnstring)
self.wi_available.notify()
self.wi_available.release()
class DNRelay(object):
clim = None
workers = None
dest_sprefix = None
in_address = None
sip_logger = None
def __init__(self, dnconfig, sip_logger):
self.workers = {}
self.clim = CLIConnectionManager(self.recv_dnotify, dnconfig.in_address, tcp = True)
self.clim.accept_list = []
self.dest_sprefix = dnconfig.dest_sprefix
self.in_address = dnconfig.in_address
self.sip_logger = sip_logger
def recv_dnotify(self, clim, dnstring):
#print 'DNRelay.recv_dnotify(%s)' % dnstring
if clim.raddr != None:
self.sip_logger.write('Disconnect notification from %s received on %s: "%s"' \
% (str(clim.raddr), str(self.in_address), dnstring))
else:
self.sip_logger.write('Disconnect notification received on %s: "%s"' \
% (str(self.in_address), dnstring))
ssufx, dnstring = dnstring.split(None, 1)
spath = self.dest_sprefix + ssufx
dnw = self.workers.get(spath, None)
if dnw == None:
dnw = _DNRLWorker(spath, self.sip_logger)
self.workers[spath] = dnw
self.sip_logger.write(' forwarding notification to "%s": "%s"' % (spath, dnstring))
dnw.send_dnotify(dnstring)
def shutdown(self):
for dnw in self.workers.itervalues():
dnw.send_dnotify(None)
dnw.join()
self.clim.shutdown()
self.sip_logger = None
def cmpconfig(self, dnconfig):
if dnconfig.dest_sprefix != self.dest_sprefix:
return False
if dnconfig.in_address != self.in_address:
return False
return True
def allow_from(self, address):
self.clim.accept_list.append(address[0])
def disallow_from(self, address):
self.clim.accept_list.remove(address[0])
def get_allow_list(self):
return tuple(self.clim.accept_list)
def set_allow_list(self, accept_list):
self.clim.accept_list = list(accept_list)