Commit 128edb83 authored by Julien Muchembled's avatar Julien Muchembled

rina: recover from cases where normal.DIF is already registered at startup

Which happens when only re6stnet is restarted. The DIF must even be recreated
if the local certificate has changed, so that we can reconfigure the shim DIF.
parent 3e1a6244
...@@ -111,6 +111,7 @@ class Shim(object): ...@@ -111,6 +111,7 @@ class Shim(object):
def __init__(self, ipcp_id, dif): def __init__(self, ipcp_id, dif):
self.ipcp_id = ipcp_id self.ipcp_id = ipcp_id
self.dif = dif self.dif = dif
self._asking_info = {}
self._enabled = weakref.WeakValueDictionary() self._enabled = weakref.WeakValueDictionary()
def _kernel(self, **kw): def _kernel(self, **kw):
...@@ -131,7 +132,7 @@ class Shim(object): ...@@ -131,7 +132,7 @@ class Shim(object):
len(ap), ap, len(ip), ip, len(port), port)) len(ap), ap, len(ip), ip, len(port), port))
self._enabled[prefix] = tm._getPeer(prefix) self._enabled[prefix] = tm._getPeer(prefix)
def update(self, tm): def init(self, tm):
global resolve_thread global resolve_thread
if resolve_thread is None: if resolve_thread is None:
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
...@@ -142,8 +143,10 @@ class Shim(object): ...@@ -142,8 +143,10 @@ class Shim(object):
resolve_thread.start() resolve_thread.start()
prefix = tm._prefix prefix = tm._prefix
self._enabled[prefix] = tm.cert self._enabled[prefix] = tm.cert
self._asking_info = {prefix: float('inf')} self._asking_info[prefix] = float('inf')
def update(tm):
def update(self, tm):
enabled = self._enabled
ap = ap_name(tm._prefix) ap = ap_name(tm._prefix)
name = ap + "-1--" name = ap + "-1--"
step = 0 # do never retry immediately step = 0 # do never retry immediately
...@@ -151,30 +154,38 @@ class Shim(object): ...@@ -151,30 +154,38 @@ class Shim(object):
normal_id = None normal_id = None
for ipcp in ipcm.iterIpcp(): for ipcp in ipcm.iterIpcp():
if ipcp[0] == self.ipcp_id: if ipcp[0] == self.ipcp_id:
registered = name in ipcp[4] registered = ipcp[4]
elif ipcp[1] == name: elif ipcp[1] == name:
normal_id = ipcp[0] normal_id = ipcp[0]
normal_status = ipcp[3] normal_status = ipcp[3]
if registered: elif ipcp[1] in registered:
if step or not ipcm("destroy-ipcp", ipcp[0]):
return
step = 1
if name in registered:
break break
if normal_id is None: if normal_id is None:
if step or not ipcm("create-ipcp", ap, 1, "normal-ipc"): if step > 1 or not ipcm("create-ipcp", ap, 1, "normal-ipc"):
return return
step = 1 step = 2
elif normal_status == "INITIALIZED": elif normal_status == "INITIALIZED":
if step > 1 or not ipcm("assign-to-dif", normal_id, if step > 2 or not ipcm("assign-to-dif", normal_id,
NORMAL_DIF, DEFAULT_DIF): NORMAL_DIF, DEFAULT_DIF):
return return
step = 2 step = 3
elif normal_status.startswith("ASSIGNED TO DIF"): elif normal_status.startswith("ASSIGNED TO DIF"):
if step > 2 or not ipcm("register-at-dif", enabled.clear()
normal_id, self.dif): self.init(tm)
port = str(PORT)
self._kernel(
hostname=utils.ipFromBin(tm._network + tm._prefix, '1'),
expReg="1:%s:%s0:%s:%s" % (len(ap), ap, len(port), port))
if step > 3 or not ipcm("register-at-dif", normal_id, self.dif):
return return
step = 3 step = 4
else: else:
return return
asking_info = self._asking_info asking_info = self._asking_info
enabled = self._enabled
enrolled = set(ap_prefix(neigh[0].split('-', 1)[0]) enrolled = set(ap_prefix(neigh[0].split('-', 1)[0])
for neigh in ipcm.iterNeigh(normal_id)) for neigh in ipcm.iterNeigh(normal_id))
now = time.time() now = time.time()
...@@ -194,28 +205,17 @@ class Shim(object): ...@@ -194,28 +205,17 @@ class Shim(object):
# so don't block for too long. # so don't block for too long.
if now + 1 < time.time(): if now + 1 < time.time():
return return
continue elif asking_info.get(prefix, 0) < now and tm.askInfo(prefix):
if asking_info.get(prefix, 0) < now and tm.askInfo(prefix):
self._enroll(tm, prefix) self._enroll(tm, prefix)
self._asking_info[prefix] = now + 60 asking_info[prefix] = now + 60
ap = ap_name(prefix)
port = str(PORT)
self._kernel(hostname=utils.ipFromBin(tm._network + prefix, '1'),
expReg="1:%s:%s0:%s:%s" % (len(ap), ap, len(port), port))
self.update = update
update(tm)
def enabled(self, tm, prefix, enroll): def enabled(self, tm, prefix, enroll):
logging.debug("RINA: enabled(%s, %s)", prefix, enroll) logging.debug("RINA: enabled(%s, %s)", prefix, enroll)
try:
asking_info = self._asking_info
except AttributeError:
return
if enroll: if enroll:
asking_info.pop(prefix, None) self._asking_info.pop(prefix, None)
self._enroll(tm, prefix) self._enroll(tm, prefix)
else: else:
asking_info[prefix] = float('inf') self._asking_info[prefix] = float('inf')
@staticmethod @staticmethod
def _resolve(sock): def _resolve(sock):
...@@ -287,9 +287,9 @@ if os.path.isdir("/sys/rina"): ...@@ -287,9 +287,9 @@ if os.path.isdir("/sys/rina"):
return False return False
dif = sysfs_read(ipcp + "/dif") dif = sysfs_read(ipcp + "/dif")
if dif.endswith("///"): if dif.endswith("///"):
host_name = sysfs_read(ipcp + "/host_name") if shim is None:
if not host_name:
shim = Shim(int(ipcp.rsplit("/", 1)[1]), dif[:-3]) shim = Shim(int(ipcp.rsplit("/", 1)[1]), dif[:-3])
shim.init(tunnel_manager)
if route_dumped: if route_dumped:
shim.update(tunnel_manager) shim.update(tunnel_manager)
return True return True
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment