Skip to content

Commit 8b509a0

Browse files
committed
Correct handling of IP addresses in Python bindings.
This adds support for proper forward and reverse host resolution using the new BackendServerIP6 setting.
1 parent 6045fa4 commit 8b509a0

File tree

3 files changed

+39
-37
lines changed

3 files changed

+39
-37
lines changed

mythtv/bindings/python/MythTV/database.py

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,19 +1085,41 @@ def _check_schema(self, value, local, name='Database', update=None):
10851085
else:
10861086
update(self).run()
10871087

1088-
def gethostname(self):
1089-
return self.dbconn['LocalHostName']
1088+
def _gethostfromaddr(self, addr, value=None):
1089+
if value is None:
1090+
for value in ['BackendServerIP','BackendServerIP6']:
1091+
try:
1092+
return self._gethostfromaddr(addr, value)
1093+
except MythDBError:
1094+
pass
1095+
else:
1096+
raise MythDBError(MythError.DB_SETTING,
1097+
'BackendServerIP[6]', addr)
10901098

1091-
def getMasterBackend(self):
1092-
ip = self.settings.NULL.MasterServerIP
10931099
with self as cursor:
10941100
if cursor.execute("""SELECT hostname FROM settings
1095-
WHERE value='BackendServerIP'
1096-
AND data=?""", [ip]) == 0:
1097-
# no match found
1098-
raise MythDBError(MythError.DB_SETTING, 'BackendServerIP', ip)
1101+
WHERE value=? AND data=?""", [value, addr]) == 0:
1102+
raise MythDBError(MythError.DB_SETTING, value, addr)
10991103
return cursor.fetchone()[0]
11001104

1105+
def _getpreferredaddr(self, host):
1106+
ip6 = self.settings[host].BackendServerIP6
1107+
ip4 = self.settings[host].BackendServerIP
1108+
if ip6 is None:
1109+
if ip4 is None:
1110+
raise MythDBError(MythError.DB_SETTING,
1111+
'BackendServerIP[6]', host)
1112+
return ip4
1113+
elif (ip6 in ['::1', '0:0:0;0:0:0:0:1']) and (ip4 != '127.0.0.1'):
1114+
return ip4
1115+
return ip6
1116+
1117+
def gethostname(self):
1118+
return self.dbconn['LocalHostName']
1119+
1120+
def getMasterBackend(self):
1121+
return self._gethostfromaddr(self.settings.NULL.MasterServerIP)
1122+
11011123
def getStorageGroup(self, groupname=None, hostname=None):
11021124
"""
11031125
obj.getStorageGroup(groupname=None, hostname=None)

mythtv/bindings/python/MythTV/methodheap.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,13 +1100,7 @@ def __init__(self, backend=None, port=None, db=None):
11001100
if re.match('(?:\d{1,3}\.){3}\d{1,3}',backend) or \
11011101
check_ipv6(backend):
11021102
# process ip address
1103-
with self.db.cursor(self.log) as cursor:
1104-
if cursor.execute("""SELECT hostname FROM settings
1105-
WHERE value='BackendServerIP'
1106-
AND data=%s""", backend) == 0:
1107-
raise MythDBError(MythError.DB_SETTING,
1108-
backend+': BackendServerIP')
1109-
host = cursor.fetchone()[0]
1103+
host = self.db._gethostfromaddr(backend)
11101104
self.host = backend
11111105
self.port = int(self.db.settings[host].BackendStatusPort)
11121106
else:

mythtv/bindings/python/MythTV/mythproto.py

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -69,33 +69,25 @@ def __init__(self, backend=None, blockshutdown=False, events=False, db=None):
6969
if backend is None:
7070
# no backend given, use master
7171
self.host = self.db.settings.NULL.MasterServerIP
72+
self.hostname = self.db._gethostfromaddr(self.host)
7273

7374
else:
7475
backend = backend.strip('[]')
76+
query = "SELECT hostname FROM settings WHERE value=? AND data=?"
7577
if self._reip.match(backend):
7678
# given backend is IP address
7779
self.host = backend
80+
self.hostname = self.db._gethostfromaddr(
81+
backend, 'BackendServerIP')
7882
elif check_ipv6(backend):
7983
# given backend is IPv6 address
8084
self.host = backend
85+
self.hostname = self.db._gethostfromaddr(
86+
backend, 'BackendServerIP6')
8187
else:
8288
# given backend is hostname, pull address from database
8389
self.hostname = backend
84-
self.host = self.db.settings[backend].BackendServerIP
85-
if not self.host:
86-
raise MythDBError(MythError.DB_SETTING,
87-
'BackendServerIP', backend)
88-
89-
if self.hostname is None:
90-
# reverse lookup hostname from address
91-
with self.db.cursor(self.log) as cursor:
92-
if cursor.execute("""SELECT hostname FROM settings
93-
WHERE value='BackendServerIP'
94-
AND data=?""", [self.host]) == 0:
95-
# no match found
96-
raise MythDBError(MythError.DB_SETTING, 'BackendServerIP',
97-
self.host)
98-
self.hostname = cursor.fetchone()[0]
90+
self.host = self.db._getpreferredaddr(backend)
9991

10092
# lookup port from database
10193
self.port = int(self.db.settings[self.hostname].BackendServerPort)
@@ -248,13 +240,7 @@ def ftopen(file, mode, forceremote=False, nooverwrite=False, db=None, \
248240
# get full system name
249241
host = host.strip('[]')
250242
if reip.match(host) or check_ipv6(host):
251-
with db.cursor(log) as cursor:
252-
if cursor.execute("""SELECT hostname FROM settings
253-
WHERE value='BackendServerIP'
254-
AND data=%s""", host) == 0:
255-
raise MythDBError(MythError.DB_SETTING, \
256-
'BackendServerIP', backend)
257-
host = cursor.fetchone()[0]
243+
host = db._gethostfromaddr(host)
258244

259245
# user forced to remote access
260246
if forceremote:

0 commit comments

Comments
 (0)