Commit cc56c896 authored by Federico Sismondi's avatar Federico Sismondi

support for no_ipv6_forwarding on OS running agent

parent 5e1d3b0a
......@@ -53,21 +53,25 @@ class TunConsumer(BaseConsumer):
- stop_tun
"""
if self.tun is None:
if self.tun is not None:
self.log.warning('Received open tun control message, but TUN already created')
return
else:
self.log.info('starting tun interface')
try:
ipv6_host = msg.get("ipv6_host", None)
ipv6_prefix = msg.get("ipv6_prefix", None)
ipv6_no_forwarding = msg.get("ipv6_no_forwarding", None)
ipv4_host = msg.get("ipv4_host", None)
ipv4_network = msg.get("ipv4_network", None)
ipv4_netmask = msg.get("ipv4_netmask", None)
except AttributeError as ae:
self.log.error(
'Wrong message format: {0}'.format(msg.payload)
)
return
params = {
'rmq_connection' : self.connection,
'name' : self.name,
......@@ -76,6 +80,7 @@ class TunConsumer(BaseConsumer):
'ipv4_host' : ipv4_host,
'ipv4_network' : ipv4_network,
'ipv4_netmask' : ipv4_netmask,
'ipv6_no_forwarding' : ipv6_no_forwarding
}
if sys.platform.startswith('win32'):
......@@ -101,6 +106,7 @@ class TunConsumer(BaseConsumer):
'ipv4_host' : ipv4_host,
'ipv4_network' : ipv4_network,
'ipv4_netmask' : ipv4_netmask,
'ipv6_no_forwarding' : ipv6_no_forwarding,
}
self.log.info("Tun started. Publishing msg: %s"%json.dumps(msg))
......@@ -111,8 +117,7 @@ class TunConsumer(BaseConsumer):
routing_key='control.tun.fromAgent.%s'%self.name
)
else:
self.log.warning('Received open tun control message, but TUN already created')
def handle_data(self, body, message):
"""
......
......@@ -3,6 +3,7 @@ import sys
import os
import json
import pika
import argparse
import time
logging.getLogger('pika').setLevel(logging.INFO)
......@@ -13,12 +14,13 @@ queue_name = 'unittest_packet_router'
AMQP_URL = str(os.environ['AMQP_URL'])
AMQP_EXCHANGE = str(os.environ['AMQP_EXCHANGE'])
def publish_tun_start(channel, agent_id, ipv6_host, ipv6_prefix):
def publish_tun_start(channel, agent_id, ipv6_host, ipv6_prefix, ipv6_no_forwarding = False):
d = {
"_type": "tun.start",
"name": agent_id,
"ipv6_host": ipv6_host,
"ipv6_prefix": ipv6_prefix,
"ipv6_no_forwarding" : ipv6_no_forwarding
}
channel.basic_publish(
......@@ -48,22 +50,25 @@ def check_response(channel,queue_name, agent_id):
if __name__ == "__main__":
try:
agent_id = sys.argv[1]
ipv6_prefix = sys.argv[2]
ipv6_host = sys.argv[3]
parser = argparse.ArgumentParser()
parser.add_argument("agent_name", help="Name of the agent")
parser.add_argument("ipv6_prefix", help="ipv6 prefix")
parser.add_argument("ipv6_host", help="ipv6 host network")
parser.add_argument("-nf", "--no_forwarding", help="activate or not IPv6 forwarding", action="store_true")
args = parser.parse_args()
agent_id = args.name
ipv6_prefix = args.ipv6_prefix
ipv6_host = args.ipv6_host
ipv6_no_forwarding = args.no_forwarding
except:
print("Error, please execute as: python3 ...bootstrap_agent.py <agent_name> <ipv6_prefix> <ipv6_host>")
print("Example python3 ...bootstrap_agent.py coap_client_agent bbbb :1 ")
print("Error, please see help (-h)")
sys.exit(1)
connection = pika.BlockingConnection(pika.connection.URLParameters(AMQP_URL))
channel = connection.channel()
agent_event_q = 'agent_bootstrap'
result = channel.queue_declare(queue=agent_event_q)
callback_queue = result.method.queue
......@@ -77,7 +82,7 @@ if __name__ == "__main__":
for i in range (1,4):
logging.debug("Let's start the bootstrap the agent %s try number %d" %(agent_id,i))
publish_tun_start(channel, agent_id, ipv6_host, ipv6_prefix)
publish_tun_start(channel, agent_id, ipv6_host, ipv6_prefix, ipv6_no_forwarding)
time.sleep(4)
if check_response(channel,agent_event_q, agent_id):
logging.debug("Agent tun bootstrapped")
......
......@@ -326,7 +326,7 @@ class OpenTunLinux(object):
"""
def __init__(self, name, rmq_connection, exchange="default",
ipv6_prefix=None, ipv6_host=None,
ipv6_prefix=None, ipv6_host=None, ipv6_no_forwarding = None,
ipv4_host=None, ipv4_network=None, ipv4_netmask=None):
# RMQ setups
......@@ -349,6 +349,10 @@ class OpenTunLinux(object):
self.ipv6_host = ipv6_host
if ipv6_no_forwarding is None:
ipv6_no_forwarding = False
self.ipv6_no_forwarding = ipv6_no_forwarding
if ipv4_host is None:
ipv4_host = "2.2.2.2"
......@@ -365,6 +369,7 @@ class OpenTunLinux(object):
log.debug("IP info")
log.debug(self.ipv6_prefix)
log.debug(self.ipv6_host)
log.debug(self.ipv6_no_forwarding)
log.debug(self.ipv4_host)
log.debug(self.ipv4_network)
log.debug(self.ipv4_netmask)
......@@ -448,8 +453,13 @@ class OpenTunLinux(object):
# os.system('ip -6 route add ' + ipv6_prefixStr + '::/64 via ' + IPv6Prefix + ':' + ipv6_hostStr + '/64')
# =====
log.info("enabling IPv6 forwarding...")
os.system('echo 1 > /proc/sys/net/ipv6/conf/all/forwarding')
if self.ipv6_no_forwarding:
log.info("disabling IPv6 forwarding...")
os.system('echo 0 > /proc/sys/net/ipv6/conf/all/forwarding')
else:
log.info("enabling IPv6 forwarding...")
os.system('echo 1 > /proc/sys/net/ipv6/conf/all/forwarding')
# =====
log.info('\ncreated following virtual interface:')
......@@ -540,7 +550,7 @@ class OpenTunMACOS(object):
'''
def __init__(self, name, rmq_connection, exchange="default",
ipv6_prefix=None, ipv6_host=None,
ipv6_prefix=None, ipv6_host=None, ipv6_no_forwarding = None,
ipv4_host=None, ipv4_network=None, ipv4_netmask=None):
# RMQ setups
......@@ -555,18 +565,19 @@ class OpenTunMACOS(object):
if ipv6_prefix is None:
# self.ipv6_prefix = [0xbb, 0xbb, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
ipv6_prefix = DEFAULT_IPV6_PREFIX
self.ipv6_prefix = ipv6_prefix
if ipv6_host is None:
# self.ipv6_host = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01]
ipv6_host = "1"
self.ipv6_host = ipv6_host
if ipv6_no_forwarding is None:
ipv6_no_forwarding = False
self.ipv6_no_forwarding = ipv6_no_forwarding
if ipv4_host is None:
ipv4_host = "2.2.2.2"
self.ipv4_host = ipv4_host
if ipv4_network is None:
......@@ -577,12 +588,14 @@ class OpenTunMACOS(object):
ipv4_netmask = [255, 255, 0, 0]
self.ipv4_netmask = ipv4_netmask
log.debug("IP info")
log.debug(self.ipv6_prefix)
log.debug(self.ipv6_host)
log.debug(self.ipv4_host)
log.debug(self.ipv4_network)
log.debug(self.ipv4_netmask)
log.debug('ipv6_prefix: ' + self.ipv6_prefix)
log.debug('ipv6_host: ' + self.ipv6_host)
log.debug('ipv6_no_forwarding: ' + str(self.ipv6_no_forwarding))
log.debug('ipv4_host: ' + self.ipv4_host)
log.debug('ipv4_network: ' + str(self.ipv4_network))
log.debug('ipv4_netmask: ' + str(self.ipv4_netmask))
# local variables
self.tunIf = self._createTunIf()
......@@ -660,18 +673,20 @@ class OpenTunMACOS(object):
# os.system('ip -6 route add ' + prefixStr + '::/64 via ' + IPv6Prefix + ':' + hostStr + '/64')
# =====
log.info("enabling IPv6 forwarding...")
# os.system('echo 1 > /proc/sys/net/ipv6/conf/all/forwarding')
os.system('sysctl -w net.inet6.ip6.forwarding=1')
if self.ipv6_no_forwarding:
log.info("disabling IPv6 forwarding...")
os.system('sysctl -w net.inet6.ip6.forwarding=0')
else:
log.info("enabling IPv6 forwarding...")
os.system('sysctl -w net.inet6.ip6.forwarding=1')
# =====
print('\ncreated following virtual interface:')
log.info('\ncreated following virtual interface:')
os.system('ifconfig {0}'.format(self.ifname))
# =====start radvd
# os.system('radvd start')
return f
def _createTunReadThread(self):
......
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