Skip to content

Commit

Permalink
Change print statements into log ones (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
leoplo committed Jan 7, 2024
1 parent eeb6baa commit 273b90c
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 17 deletions.
11 changes: 5 additions & 6 deletions mld/Interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import threading
import netifaces
import ipaddress
import traceback
import logging
from fcntl import ioctl
from abc import ABCMeta, abstractmethod

Expand Down Expand Up @@ -43,9 +43,8 @@ def receive(self):
(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
except Exception as e:
logging.error(e, exc_info=True)

@abstractmethod
def _receive(self, raw_bytes, ancdata, src_addr):
Expand Down Expand Up @@ -123,8 +122,8 @@ def get_mtu(self):
try:
ifs = ioctl(s, SIOCGIFMTU, ifr)
mtu = struct.unpack('<H', ifs[16:18])[0]
except:
traceback.print_exc()
except Exception as e:
logging.error(e, exc_info=True)
raise

#log.debug('get_mtu: mtu of {0} = {1}'.format(self.ifname, mtu))
Expand Down
17 changes: 9 additions & 8 deletions mld/InterfaceMLD.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import struct
import netifaces
import ipaddress
import logging
from socket import if_nametoindex
from ipaddress import IPv6Address
from .Interface import Interface
Expand Down Expand Up @@ -123,10 +124,10 @@ def _receive(self, raw_bytes, ancdata, src_addr):
if raw_bytes:
raw_bytes = raw_bytes[14:]
src_addr = (socket.inet_ntop(socket.AF_INET6, raw_bytes[8:24]),)
print("MLD IP_SRC bf= ", src_addr)
logging.debug("MLD IP_SRC bf= %s", src_addr)
dst_addr = raw_bytes[24:40]
(next_header,) = struct.unpack("B", raw_bytes[6:7])
print("NEXT HEADER:", next_header)
logging.debug("NEXT HEADER:%s", next_header)
payload_starts_at_len = 40
if next_header == 0:
# Hop by Hop options
Expand All @@ -141,11 +142,11 @@ def _receive(self, raw_bytes, ancdata, src_addr):

raw_bytes = raw_bytes[payload_starts_at_len:]
ancdata = [(socket.IPPROTO_IPV6, socket.IPV6_PKTINFO, dst_addr)]
print("RECEIVE MLD")
print("ANCDATA: ", ancdata, "; SRC_ADDR: ", src_addr)
logging.debug("RECEIVE MLD")
logging.debug("ANCDATA: %s ; SRC_ADDR: %s", ancdata, src_addr)
packet = ReceivedPacket(raw_bytes, ancdata, src_addr, 58, self)
ip_src = packet.ip_header.ip_src
print("MLD IP_SRC = ", ip_src)
logging.debug("MLD IP_SRC = %s", ip_src)
if not (ip_src == "::" or IPv6Address(ip_src).is_multicast):
self.PKT_FUNCTIONS.get(packet.payload.get_mld_type(), InterfaceMLD.receive_unknown_type)(self, packet)
"""
Expand All @@ -158,7 +159,7 @@ def _receive(self, raw_bytes, ancdata, src_addr):
# Recv packets
###########################################
def receive_multicast_listener_report(self, packet):
print("RECEIVE MULTICAST LISTENER REPORT")
logging.debug("RECEIVE MULTICAST LISTENER REPORT")
ip_dst = packet.ip_header.ip_dst
mld_group = packet.payload.group_address
ipv6_group = IPv6Address(mld_group)
Expand All @@ -167,14 +168,14 @@ def receive_multicast_listener_report(self, packet):
self.interface_state.receive_report(packet)

def receive_multicast_listener_done(self, packet):
print("RECEIVE MULTICAST LISTENER DONE")
logging.debug("RECEIVE MULTICAST LISTENER DONE")
ip_dst = packet.ip_header.ip_dst
mld_group = packet.payload.group_address
if IPv6Address(ip_dst) == self.IPv6_LINK_SCOPE_ALL_ROUTERS and IPv6Address(mld_group).is_multicast:
self.interface_state.receive_done(packet)

def receive_multicast_listener_query(self, packet):
print("RECEIVE MULTICAST LISTENER QUERY")
logging.debug("RECEIVE MULTICAST LISTENER QUERY")
ip_dst = packet.ip_header.ip_dst
mld_group = packet.payload.group_address
ipv6_group = IPv6Address(mld_group)
Expand Down
5 changes: 3 additions & 2 deletions mld/mld1/GroupState.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def __init__(self, router_state: 'RouterState', group_ip: str):
#logger
extra_dict_logger = router_state.router_state_logger.extra.copy()
extra_dict_logger['tree'] = '(*,' + group_ip + ')'
extra_dict_logger['routername'] = None
self.group_state_logger = logging.LoggerAdapter(GroupState.LOGGER, extra_dict_logger)

#timers and state
Expand Down Expand Up @@ -149,7 +150,7 @@ def notify_routing_add(self):
Notify all tree entries that MLD considers to have hosts interested in this group
"""
with self.multicast_interface_state_lock:
print("notify+", self.multicast_interface_state)
logging.debug("notify+ %s", self.multicast_interface_state)
for interface_state in self.multicast_interface_state:
interface_state.notify_membership(has_members=True)

Expand All @@ -158,7 +159,7 @@ def notify_routing_remove(self):
Notify all tree entries that MLD considers to have not hosts interested in this group
"""
with self.multicast_interface_state_lock:
print("notify-", self.multicast_interface_state)
logging.debug("notify- %s", self.multicast_interface_state)
for interface_state in self.multicast_interface_state:
interface_state.notify_membership(has_members=False)

Expand Down
3 changes: 2 additions & 1 deletion mld/packet/PacketIpHeader.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import struct
import socket
import logging


class PacketIpHeader:
Expand All @@ -24,7 +25,7 @@ def __len__(self):
def parse_bytes(data: bytes):
(verhlen, ) = struct.unpack(PacketIpHeader.IP_HDR, data[:PacketIpHeader.IP_HDR_LEN])
ver = (verhlen & 0xF0) >> 4
print("ver:", ver)
logging.debug("ver: %d", ver)
return PACKET_HEADER.get(ver).parse_bytes(data)


Expand Down

0 comments on commit 273b90c

Please sign in to comment.