Skip to content

Commit

Permalink
IGMPv2 Implementation (#4)
Browse files Browse the repository at this point in the history
* add latest code updates from HPIM-DM/PIM-DM implementations
  • Loading branch information
pedrofran12 authored Aug 7, 2020
1 parent b5079cc commit 6b757ec
Show file tree
Hide file tree
Showing 38 changed files with 2,353 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/*
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.
59 changes: 58 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,58 @@
# igmp
# IGMP

This repository stores the implementation of IGMPv2 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 IGMP 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)

This implementation was performed during my Master thesis and has since then been updated to fix issues, add new features and in the future to include the implementation of IGMPv3 as well.


# Documents detailing the initial work of IGMPv2 implementation

- [Python implementation of IGMPv2, PIM-DM and HPIM-DM](https://github.com/pedrofran12/hpim_dm/tree/master/docs/PythonImplementations.pdf)
- [Test to Python implementation of IGMPv2, PIM-DM, and HPIM-DM](https://github.com/pedrofran12/hpim_dm/tree/master/docs/PythonTests.pdf)


# Requirements

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


# Installation

```
pip3 install igmp
```

# How to use it?

```python
# import module
from igmp import InterfaceIGMP

intf = InterfaceIGMP(interface_name="eth0")

# get information from a given multicast group
multicast_group_obj = intf.interface_state.get_group_state(group_ip="224.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 224.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)
```
130 changes: 130 additions & 0 deletions igmp/Interface.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
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, src_addr) = self._recv_socket.recvfrom(256 * 1024)
if raw_bytes:
self._receive(raw_bytes, src_addr)
except Exception:
traceback.print_exc()
continue

@abstractmethod
def _receive(self, raw_bytes, 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 6b757ec

Please sign in to comment.