forked from KroMignon/enc28j60-rpi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ethernet.py
79 lines (68 loc) · 2.15 KB
/
ethernet.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#! /usr/bin/python
# -*- coding: utf-8 -*-
#=============================================================================
# Name: ethernet.py
# Purpose: Microchip ENC28J60 Ethernet Interface Driver
#
# Author: Fabrice MOUSSET
#
# Created: 2013/01/05
# Licence: GPLv3 or newer
#=============================================================================
# Last commit info:
#
# $LastChangedDate:: xxxx/xx/xx xx:xx:xx $
# $Rev:: $
# $Author:: $
#=============================================================================
# Revision list :
#
# Date By Changes
#
#=============================================================================
class EthernetFrame(object):
def __init__(self, framebuffer):
self.__framebuffer = framebuffer
self.__destMAC = ":".join('%02x' % i for i in self.__framebuffer[0:6])
self.__srcMAC = ":".join('%02x' % i for i in self.__framebuffer[6:12])
self.__frameType = self.__framebuffer[12] * 256 + self.__framebuffer[13]
@property
def destMAC(self):
return self.__destMAC
@property
def srcMAC(self):
return self.__srcMAC
@property
def frameType(self):
return self.__frameType
@property
def isARP(self):
return bool(self.__frameType == 0x0806)
@property
def isIPv4(self):
return bool(self.__frameType == 0x0800)
@property
def isIPv6(self):
return bool(self.__frameType == 0x08DD)
@property
def payload(self):
return self.__framebuffer[14:]
def __str__(self):
if self.isARP:
return "ARP"
if self.isIPv4:
return "IPv4"
if self.isIPv6:
return "IPv6"
return "Unknown"
def make_header(self, buf, dst_mac=None):
pass
def make_reply(self, payload, mac=None):
buf = [self.__framebuffer[6+b] for b in range(6)]
if(mac):
buf.extend([int(m,16) for m in mac.split(':')])
else:
buf.extend(self.__framebuffer[0:6])
buf.extend(self.__framebuffer[12:14])
buf.extend(payload)
return buf