Skip to content

Commit

Permalink
MLDv1 Implementation
Browse files Browse the repository at this point in the history
add latest code updates from HPIM-DM/PIM-DM implementations (#4)
  • Loading branch information
pedrofran12 authored Aug 7, 2020
1 parent 568ebec commit 5df5690
Show file tree
Hide file tree
Showing 37 changed files with 2,172 additions and 1 deletion.
54 changes: 54 additions & 0 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
name: "CodeQL"

on:
push:
branches: [master, ]
pull_request:
# The branches below must be a subset of the branches above
branches: [master]
schedule:
- cron: '0 4 * * 0'

jobs:
analyse:
name: Analyse
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v2
with:
# We must fetch at least the immediate parents so that if this is
# a pull request then we can checkout the head.
fetch-depth: 2

# If this run was triggered by a pull request event, then checkout
# the head of the pull request instead of the merge commit.
- run: git checkout HEAD^2
if: ${{ github.event_name == 'pull_request' }}

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v1
# Override language selection by uncommenting this and choosing your languages
# with:
# languages: go, javascript, csharp, python, cpp, java

# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@v1

# ℹ️ Command-line programs to run using the OS shell.
# 📚 https://git.io/JvXDl

# ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
# and modify them (or add more) to build your code if your project
# uses a compiled language

#- run: |
# make bootstrap
# make release

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v1
27 changes: 27 additions & 0 deletions .github/workflows/pythonpublish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Upload Python Package

on:
release:
types: [created]

jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.x'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install setuptools wheel twine
- name: Build and publish
env:
TWINE_USERNAME: ${{ secrets.PYPI_USERNAME }}
TWINE_PASSWORD: ${{ secrets.PYPI_PASSWORD }}
run: |
python setup.py sdist bdist_wheel
twine upload dist/*
101 changes: 101 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
env/
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# pyenv
.python-version

# celery beat schedule file
celerybeat-schedule

# SageMath parsed files
*.sage.py

# dotenv
.env

# virtualenv
.venv
venv/
ENV/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 Pedro Oliveira

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
51 changes: 50 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,50 @@
# mld
# MLD

This repository stores the implementation of MLDv1 router-side state machines. This can be used to detect multicast interest of directly connected hosts.

The goal of this repository/module is to facilitate maintainability of this MLD implementation since its code is used by other Python projects/modules:

- [HPIM-DM](https://github.com/pedrofran12/hpim_dm)
- [PIM-DM](https://github.com/pedrofran12/pim_dm)


# Requirements

- Linux machine
- Python3 (we have written all code to be compatible with at least Python v3.3)
- pip (to install all dependencies)


# Installation

```
pip3 install py-mld
```

# How to use it?

```python
# import module
from mld import InterfaceMLD

intf = InterfaceMLD(interface_name="eth0")

# get information from a given multicast group
multicast_group_obj = intf.interface_state.get_group_state(group_ip="ff05:10:11:12")

interest = multicast_group_obj.has_members() # boolean that informs if there is multicast interest in this group
group_state = multicast_group_obj.state.print_state() # get string identifying the state in which this group is at

# get notified of interest changes on this group
class MulticastGroupNotifier:
def notify_membership(self, has_members):
print(has_members)

notifier = MulticastGroupNotifier()
multicast_group_obj.add_multicast_routing_entry(notifier)

# when there is a change of multicast interest (for example group ff05::10:11:12 gets interested receivers), the object associated to this object is notified through "notify_membership" method with has_members=True

# if you no longer want to monitor the interest of 224.10.11.12, remove the notifier from the group
multicast_group_obj.remove_multicast_routing_entry(notifier)
```
131 changes: 131 additions & 0 deletions mld/Interface.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import socket
import struct
import threading
import netifaces
import ipaddress
import traceback
from fcntl import ioctl
from abc import ABCMeta, abstractmethod

SIOCGIFMTU = 0x8921


class Interface(metaclass=ABCMeta):

def __init__(self, interface_name, recv_socket, send_socket, vif_index):
self.interface_name = interface_name

# virtual interface index for the multicast routing table
self.vif_index = vif_index

# set receive socket and send socket
self._send_socket = send_socket
self._recv_socket = recv_socket
self.interface_enabled = False

def enable(self):
"""
Enable this interface
This will start a thread to be executed in the background to be used in the reception of control packets
"""
self.interface_enabled = True
# run receive method in background
receive_thread = threading.Thread(target=self.receive)
receive_thread.daemon = True
receive_thread.start()

def receive(self):
"""
Method that will be executed in the background for the reception of control packets
"""
while self.interface_enabled:
try:
(raw_bytes, ancdata, _, src_addr) = self._recv_socket.recvmsg(256 * 1024, 500)
if raw_bytes:
self._receive(raw_bytes, ancdata, src_addr)
except Exception:
traceback.print_exc()
continue

@abstractmethod
def _receive(self, raw_bytes, ancdata, src_addr):
"""
Subclass method to be implemented
This method will be invoked whenever a new control packet is received
"""
raise NotImplementedError

def send(self, data: bytes, group_ip: str):
"""
Send a control packet through this interface
Explicitly destined to group_ip (can be unicast or multicast IP)
"""
if self.interface_enabled and data:
try:
self._send_socket.sendto(data, (group_ip, 0))
except socket.error:
pass

def remove(self):
"""
This interface is no longer active....
Clear all state regarding it
"""
self.interface_enabled = False
try:
self._recv_socket.shutdown(socket.SHUT_RDWR)
except Exception:
pass
self._recv_socket.close()
self._send_socket.close()

def is_enabled(self):
"""
Verify if this interface is enabled
"""
return self.interface_enabled

@abstractmethod
def get_ip(self):
"""
Get IP of this interface
"""
raise NotImplementedError

def get_all_interface_networks(self):
"""
Get all subnets associated with this interface.
Used to verify if interface is directly connected to a multicast source
This is extremely relevant on IPv6, where an interface can be connected to multiple subnets (global, link-local,
unique-local)
"""
all_networks = set()
for if_addr in netifaces.ifaddresses(self.interface_name)[self._get_address_family()]:
ip_addr = if_addr["addr"].split("%")[0]
netmask = if_addr["netmask"].split("/")[0]
prefix_length = str(bin(int(ipaddress.ip_address(netmask).packed.hex(), 16)).count('1'))
network = ip_addr + "/" + prefix_length
all_networks.add(str(ipaddress.ip_interface(network).network))
return all_networks

@staticmethod
@abstractmethod
def _get_address_family():
raise NotImplementedError

def get_mtu(self):
"""
Get MTU of this interface
"""
'''Use socket ioctl call to get MTU size'''
s = socket.socket(type=socket.SOCK_DGRAM)
ifr = self.interface_name + '\x00'*(32-len(self.interface_name))
try:
ifs = ioctl(s, SIOCGIFMTU, ifr)
mtu = struct.unpack('<H', ifs[16:18])[0]
except:
traceback.print_exc()
raise

#log.debug('get_mtu: mtu of {0} = {1}'.format(self.ifname, mtu))
return mtu
Loading

0 comments on commit 5df5690

Please sign in to comment.