2016-03-11 19:13:52 +00:00
|
|
|
|
#!/usr/bin/env python
|
|
|
|
|
#
|
|
|
|
|
# Copyright (C) 2016 GNS3 Technologies Inc.
|
|
|
|
|
#
|
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
# (at your option) any later version.
|
|
|
|
|
#
|
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
|
#
|
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
|
import asyncio
|
2016-04-21 10:14:09 +00:00
|
|
|
|
import aiohttp
|
2016-03-11 19:13:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from .link import Link
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class UDPLink(Link):
|
2016-03-14 16:40:27 +00:00
|
|
|
|
|
2016-04-21 11:49:29 +00:00
|
|
|
|
def __init__(self, project):
|
|
|
|
|
super().__init__(project)
|
2016-05-11 17:35:36 +00:00
|
|
|
|
self._capture_node = None
|
2016-03-14 16:40:27 +00:00
|
|
|
|
|
2016-03-11 19:13:52 +00:00
|
|
|
|
@asyncio.coroutine
|
|
|
|
|
def create(self):
|
|
|
|
|
"""
|
2016-05-11 17:35:36 +00:00
|
|
|
|
Create the link on the nodes
|
2016-03-11 19:13:52 +00:00
|
|
|
|
"""
|
2016-03-14 16:40:27 +00:00
|
|
|
|
|
2016-05-11 17:35:36 +00:00
|
|
|
|
node1 = self._nodes[0]["node"]
|
|
|
|
|
adapter_number1 = self._nodes[0]["adapter_number"]
|
|
|
|
|
port_number1 = self._nodes[0]["port_number"]
|
|
|
|
|
node2 = self._nodes[1]["node"]
|
|
|
|
|
adapter_number2 = self._nodes[1]["adapter_number"]
|
|
|
|
|
port_number2 = self._nodes[1]["port_number"]
|
2016-03-11 19:13:52 +00:00
|
|
|
|
|
2016-03-11 19:34:01 +00:00
|
|
|
|
# Reserve a UDP port on both side
|
2016-05-11 17:35:36 +00:00
|
|
|
|
response = yield from node1.compute.post("/projects/{}/ports/udp".format(self._project.id))
|
|
|
|
|
self._node1_port = response.json["udp_port"]
|
|
|
|
|
response = yield from node2.compute.post("/projects/{}/ports/udp".format(self._project.id))
|
|
|
|
|
self._node2_port = response.json["udp_port"]
|
2016-03-11 19:13:52 +00:00
|
|
|
|
|
|
|
|
|
# Create the tunnel on both side
|
|
|
|
|
data = {
|
2016-05-11 17:35:36 +00:00
|
|
|
|
"lport": self._node1_port,
|
|
|
|
|
"rhost": node2.compute.host,
|
|
|
|
|
"rport": self._node2_port,
|
2016-03-11 19:13:52 +00:00
|
|
|
|
"type": "nio_udp"
|
|
|
|
|
}
|
2016-05-11 17:35:36 +00:00
|
|
|
|
yield from node1.post("/adapters/{adapter_number}/ports/{port_number}/nio".format(adapter_number=adapter_number1, port_number=port_number1), data=data)
|
2016-03-11 19:13:52 +00:00
|
|
|
|
|
|
|
|
|
data = {
|
2016-05-11 17:35:36 +00:00
|
|
|
|
"lport": self._node2_port,
|
|
|
|
|
"rhost": node1.compute.host,
|
|
|
|
|
"rport": self._node1_port,
|
2016-03-11 19:13:52 +00:00
|
|
|
|
"type": "nio_udp"
|
|
|
|
|
}
|
2016-05-11 17:35:36 +00:00
|
|
|
|
yield from node2.post("/adapters/{adapter_number}/ports/{port_number}/nio".format(adapter_number=adapter_number2, port_number=port_number2), data=data)
|
2016-03-14 16:40:27 +00:00
|
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
|
def delete(self):
|
|
|
|
|
"""
|
2016-05-11 17:35:36 +00:00
|
|
|
|
Delete the link and free the resources
|
2016-03-14 16:40:27 +00:00
|
|
|
|
"""
|
2016-05-18 16:37:18 +00:00
|
|
|
|
try:
|
|
|
|
|
node1 = self._nodes[0]["node"]
|
|
|
|
|
adapter_number1 = self._nodes[0]["adapter_number"]
|
|
|
|
|
port_number1 = self._nodes[0]["port_number"]
|
|
|
|
|
except IndexError:
|
|
|
|
|
return
|
2016-03-14 16:40:27 +00:00
|
|
|
|
|
2016-05-11 17:35:36 +00:00
|
|
|
|
yield from node1.delete("/adapters/{adapter_number}/ports/{port_number}/nio".format(adapter_number=adapter_number1, port_number=port_number1))
|
2016-05-18 16:37:18 +00:00
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
node2 = self._nodes[1]["node"]
|
|
|
|
|
adapter_number2 = self._nodes[1]["adapter_number"]
|
|
|
|
|
port_number2 = self._nodes[1]["port_number"]
|
|
|
|
|
except IndexError:
|
|
|
|
|
return
|
2016-05-11 17:35:36 +00:00
|
|
|
|
yield from node2.delete("/adapters/{adapter_number}/ports/{port_number}/nio".format(adapter_number=adapter_number2, port_number=port_number2))
|
2016-04-21 10:14:09 +00:00
|
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
2016-04-26 15:10:33 +00:00
|
|
|
|
def start_capture(self, data_link_type="DLT_EN10MB", capture_file_name=None):
|
2016-04-21 10:14:09 +00:00
|
|
|
|
"""
|
|
|
|
|
Start capture on a link
|
|
|
|
|
"""
|
2016-04-26 15:10:33 +00:00
|
|
|
|
if not capture_file_name:
|
|
|
|
|
capture_file_name = self.default_capture_file_name()
|
2016-05-11 17:35:36 +00:00
|
|
|
|
self._capture_node = self._choose_capture_side()
|
2016-04-21 10:14:09 +00:00
|
|
|
|
data = {
|
2016-04-26 15:10:33 +00:00
|
|
|
|
"capture_file_name": capture_file_name,
|
2016-04-21 11:49:29 +00:00
|
|
|
|
"data_link_type": data_link_type
|
2016-04-21 10:14:09 +00:00
|
|
|
|
}
|
2016-05-11 17:35:36 +00:00
|
|
|
|
yield from self._capture_node["node"].post("/adapters/{adapter_number}/ports/{port_number}/start_capture".format(adapter_number=self._capture_node["adapter_number"], port_number=self._capture_node["port_number"]), data=data)
|
2016-04-26 15:10:33 +00:00
|
|
|
|
yield from super().start_capture(data_link_type=data_link_type, capture_file_name=capture_file_name)
|
2016-04-21 10:14:09 +00:00
|
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
|
def stop_capture(self):
|
|
|
|
|
"""
|
|
|
|
|
Stop capture on a link
|
|
|
|
|
"""
|
2016-05-11 17:35:36 +00:00
|
|
|
|
if self._capture_node:
|
|
|
|
|
yield from self._capture_node["node"].post("/adapters/{adapter_number}/ports/{port_number}/stop_capture".format(adapter_number=self._capture_node["adapter_number"], port_number=self._capture_node["port_number"]))
|
|
|
|
|
self._capture_node = None
|
2016-04-26 15:10:33 +00:00
|
|
|
|
yield from super().stop_capture()
|
2016-04-21 10:14:09 +00:00
|
|
|
|
|
|
|
|
|
def _choose_capture_side(self):
|
|
|
|
|
"""
|
|
|
|
|
Run capture on the best candidate.
|
|
|
|
|
|
2016-05-14 00:48:10 +00:00
|
|
|
|
The ideal candidate is a node who support capture on controller server
|
2016-04-21 10:14:09 +00:00
|
|
|
|
|
2016-05-11 17:35:36 +00:00
|
|
|
|
:returns: Node where the capture should run
|
2016-04-21 10:14:09 +00:00
|
|
|
|
"""
|
|
|
|
|
|
2016-05-11 17:35:36 +00:00
|
|
|
|
# use the local node first to save bandwidth
|
|
|
|
|
for node in self._nodes:
|
|
|
|
|
if node["node"].compute.id == "local" and node["node"].node_type not in ["qemu", "vpcs"]:
|
|
|
|
|
return node
|
2016-04-21 10:14:09 +00:00
|
|
|
|
|
2016-05-11 17:35:36 +00:00
|
|
|
|
for node in self._nodes:
|
|
|
|
|
if node["node"].node_type not in ["qemu", "vpcs"]:
|
|
|
|
|
return node
|
2016-04-21 10:14:09 +00:00
|
|
|
|
|
|
|
|
|
raise aiohttp.web.HTTPConflict(text="Capture is not supported for this link")
|
2016-04-22 14:22:03 +00:00
|
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
2016-04-26 15:10:33 +00:00
|
|
|
|
def read_pcap_from_source(self):
|
2016-04-22 14:22:03 +00:00
|
|
|
|
"""
|
|
|
|
|
Return a FileStream of the Pcap from the compute node
|
|
|
|
|
"""
|
2016-05-11 17:35:36 +00:00
|
|
|
|
if self._capture_node:
|
|
|
|
|
compute = self._capture_node["node"].compute
|
2016-05-11 21:19:00 +00:00
|
|
|
|
return compute.steam_file(self._project, "tmp/captures/" + self._capture_file_name)
|