Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
P
pim_dm
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
nexedi
pim_dm
Commits
179c9207
Commit
179c9207
authored
Jul 23, 2017
by
Pedro Oliveira
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
encoding addresses
parent
05a69151
Changes
11
Hide whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
229 additions
and
26 deletions
+229
-26
Main.py
Main.py
+2
-4
Packet/PacketIpHeader.py
Packet/PacketIpHeader.py
+1
-1
Packet/PacketPimEncodedGroupAddress.py
Packet/PacketPimEncodedGroupAddress.py
+46
-0
Packet/PacketPimEncodedSourceAddress.py
Packet/PacketPimEncodedSourceAddress.py
+46
-0
Packet/PacketPimEncodedUnicastAddress.py
Packet/PacketPimEncodedUnicastAddress.py
+40
-0
Packet/PacketPimHeader.py
Packet/PacketPimHeader.py
+7
-1
Packet/PacketPimHello.py
Packet/PacketPimHello.py
+19
-1
Packet/PacketPimJoinPrune.py
Packet/PacketPimJoinPrune.py
+18
-5
Packet/PacketPimJoinPruneMulticastGroup.py
Packet/PacketPimJoinPruneMulticastGroup.py
+45
-10
Packet/ReceivedPacket.py
Packet/ReceivedPacket.py
+3
-3
requirements.txt
requirements.txt
+2
-1
No files found.
Main.py
View file @
179c9207
...
...
@@ -60,8 +60,6 @@ def list_neighbors():
check_time
=
time
.
time
()
t
=
PrettyTable
([
'Neighbor IP'
,
'KeepAlive'
,
"Generation ID"
,
"Uptime"
])
for
ip
,
neighbor
in
list
(
neighbors
.
items
()):
import
socket
,
struct
# TODO atualmente conversao manual de numero para string ip
ip
=
socket
.
inet_ntoa
(
struct
.
pack
(
'!L'
,
ip
))
uptime
=
check_time
-
neighbor
.
time_of_last_update
uptime
=
0
if
(
uptime
<
0
)
else
uptime
...
...
@@ -78,8 +76,8 @@ def list_enabled_interfaces():
from
Packet.PacketPimJoinPrune
import
PacketPimJoinPrune
from
Packet.PacketPimJoinPruneMulticastGroup
import
PacketPimJoinPruneMulticastGroup
ph
=
PacketPimJoinPrune
(
167772173
,
2
0
)
ph
.
add_multicast_group
(
PacketPimJoinPruneMulticastGroup
(
3708422657
,
[
3708422657
],
[]))
ph
=
PacketPimJoinPrune
(
"10.0.0.13"
,
21
0
)
ph
.
add_multicast_group
(
PacketPimJoinPruneMulticastGroup
(
"239.123.123.123"
,
[
"1.1.1.1"
],
[]))
pckt
=
Packet
(
pim_header
=
PacketPimHeader
(
ph
))
interfaces
[
interface
].
send
(
pckt
.
bytes
())
...
...
Packet/PacketIpHeader.py
View file @
179c9207
...
...
@@ -2,7 +2,7 @@ import struct
class
PacketIpHeader
:
IP_HDR
=
"! BBH HH BBH
LL
"
IP_HDR
=
"! BBH HH BBH
4s 4s
"
#IP_HDR2 = "! B"
IP_HDR_LEN
=
struct
.
calcsize
(
IP_HDR
)
...
...
Packet/PacketPimEncodedGroupAddress.py
0 → 100644
View file @
179c9207
import
ipaddress
import
struct
import
socket
'''
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Addr Family | Encoding Type |B| Reserved |Z| Mask Len |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Group Multicast Address
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+...
'''
class
PacketPimEncodedGroupAddress
:
PIM_ENCODED_GROUP_ADDRESS_HDR
=
"! BBBB 4s"
PIM_ENCODED_GROUP_ADDRESS_HDR_LEN
=
struct
.
calcsize
(
PIM_ENCODED_GROUP_ADDRESS_HDR
)
FAMILY_RESERVED
=
0
FAMILY_IPV4
=
1
FAMILY_IPV6
=
2
RESERVED
=
0
def
__init__
(
self
,
group_address
,
mask_len
=
32
):
if
type
(
group_address
)
not
in
(
str
,
bytes
):
raise
Exception
if
type
(
group_address
)
is
bytes
:
group_address
=
socket
.
inet_ntoa
(
group_address
)
self
.
group_address
=
group_address
self
.
mask_len
=
mask_len
def
bytes
(
self
)
->
bytes
:
addr_family
=
self
.
get_addr_family
(
self
.
group_address
)
ip
=
socket
.
inet_aton
(
self
.
group_address
)
msg
=
struct
.
pack
(
PacketPimEncodedGroupAddress
.
PIM_ENCODED_GROUP_ADDRESS_HDR
,
addr_family
,
0
,
PacketPimEncodedGroupAddress
.
RESERVED
,
self
.
mask_len
,
ip
)
return
msg
def
get_addr_family
(
self
,
ip
):
version
=
ipaddress
.
ip_address
(
ip
).
version
if
version
==
4
:
return
PacketPimEncodedGroupAddress
.
FAMILY_IPV4
elif
version
==
6
:
return
PacketPimEncodedGroupAddress
.
FAMILY_IPV6
else
:
raise
Exception
Packet/PacketPimEncodedSourceAddress.py
0 → 100644
View file @
179c9207
import
ipaddress
import
struct
import
socket
'''
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Addr Family | Encoding Type | Rsrvd |S|W|R| Mask Len |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Source Address
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+...
'''
class
PacketPimEncodedSourceAddress
:
PIM_ENCODED_SOURCE_ADDRESS_HDR
=
"! BBBB 4s"
PIM_ENCODED_SOURCE_ADDRESS_HDR_LEN
=
struct
.
calcsize
(
PIM_ENCODED_SOURCE_ADDRESS_HDR
)
FAMILY_RESERVED
=
0
FAMILY_IPV4
=
1
FAMILY_IPV6
=
2
RESERVED_AND_SWR_BITS
=
0
def
__init__
(
self
,
source_address
,
mask_len
=
32
):
if
type
(
source_address
)
not
in
(
str
,
bytes
):
raise
Exception
if
type
(
source_address
)
is
bytes
:
source_address
=
socket
.
inet_ntoa
(
source_address
)
self
.
source_address
=
source_address
self
.
mask_len
=
mask_len
def
bytes
(
self
)
->
bytes
:
addr_family
=
self
.
get_addr_family
(
self
.
source_address
)
ip
=
socket
.
inet_aton
(
self
.
source_address
)
msg
=
struct
.
pack
(
PacketPimEncodedSourceAddress
.
PIM_ENCODED_SOURCE_ADDRESS_HDR
,
addr_family
,
0
,
PacketPimEncodedSourceAddress
.
RESERVED_AND_SWR_BITS
,
self
.
mask_len
,
ip
)
return
msg
def
get_addr_family
(
self
,
ip
):
version
=
ipaddress
.
ip_address
(
ip
).
version
if
version
==
4
:
return
PacketPimEncodedSourceAddress
.
FAMILY_IPV4
elif
version
==
6
:
return
PacketPimEncodedSourceAddress
.
FAMILY_IPV6
else
:
raise
Exception
Packet/PacketPimEncodedUnicastAddress.py
0 → 100644
View file @
179c9207
import
ipaddress
import
struct
import
socket
'''
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Addr Family | Encoding Type | Unicast Address
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+...
'''
class
PacketPimEncodedUnicastAddress
:
PIM_ENCODED_UNICAST_ADDRESS_HDR
=
"! BB 4s"
PIM_ENCODED_UNICAST_ADDRESS_HDR_LEN
=
struct
.
calcsize
(
PIM_ENCODED_UNICAST_ADDRESS_HDR
)
FAMILY_RESERVED
=
0
FAMILY_IPV4
=
1
FAMILY_IPV6
=
2
def
__init__
(
self
,
unicast_address
):
if
type
(
unicast_address
)
not
in
(
str
,
bytes
):
raise
Exception
if
type
(
unicast_address
)
is
bytes
:
unicast_address
=
socket
.
inet_ntoa
(
unicast_address
)
self
.
unicast_address
=
unicast_address
def
bytes
(
self
)
->
bytes
:
addr_family
=
self
.
get_addr_family
(
self
.
unicast_address
)
ip
=
socket
.
inet_aton
(
self
.
unicast_address
)
msg
=
struct
.
pack
(
PacketPimEncodedUnicastAddress
.
PIM_ENCODED_UNICAST_ADDRESS_HDR
,
addr_family
,
0
,
ip
)
return
msg
def
get_addr_family
(
self
,
ip
):
version
=
ipaddress
.
ip_address
(
ip
).
version
if
version
==
4
:
return
PacketPimEncodedUnicastAddress
.
FAMILY_IPV4
elif
version
==
6
:
return
PacketPimEncodedUnicastAddress
.
FAMILY_IPV6
else
:
raise
Exception
Packet/PacketPimHeader.py
View file @
179c9207
import
struct
from
utils
import
checksum
'''
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|PIM Ver| Type | Reserved | Checksum |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
'''
class
PacketPimHeader
:
PIM_VERSION
=
2
...
...
Packet/PacketPimHello.py
View file @
179c9207
import
struct
'''
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Option Type | Option Length |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Option Value |
| ... |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| . |
| . |
| . |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Option Type | Option Length |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Option Value |
| ... |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
'''
class
PacketPimHello
:
PIM_TYPE
=
0
PIM_HDR_OPTS
=
"! HH"
...
...
Packet/PacketPimJoinPrune.py
View file @
179c9207
import
struct
import
socket
from
Packet.PacketPimEncodedUnicastAddress
import
PacketPimEncodedUnicastAddress
'''
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Upstream Neighbor Address (Encoded Unicast Format) |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Reserved | Num Groups | Hold Time |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
'''
class
PacketPimJoinPrune
:
PIM_TYPE
=
3
PIM_HDR_JOIN_PRUNE
=
"!
4s BBH
"
PIM_HDR_JOIN_PRUNE
=
"!
"
+
str
(
PacketPimEncodedUnicastAddress
.
PIM_ENCODED_UNICAST_ADDRESS_HDR_LEN
)
+
"s BBH
"
PIM_HDR_JOIN_PRUNE_LEN
=
struct
.
calcsize
(
PIM_HDR_JOIN_PRUNE
)
def
__init__
(
self
,
upstream_neighbor_address
,
hold_time
):
if
type
(
upstream_neighbor_address
)
not
in
(
str
,
bytes
):
raise
Exception
if
type
(
upstream_neighbor_address
)
is
bytes
:
upstream_neighbor_address
=
socket
.
inet_ntoa
(
upstream_neighbor_address
)
self
.
groups
=
[]
self
.
upstream_neighbor_address
=
upstream_neighbor_address
self
.
hold_time
=
hold_time
...
...
@@ -16,8 +29,8 @@ class PacketPimJoinPrune:
self
.
groups
.
append
(
group
)
def
bytes
(
self
)
->
bytes
:
# TODO esta a converter upstream neighbor (considera que recebe int) para bytes
msg
=
struct
.
pack
(
PacketPimJoinPrune
.
PIM_HDR_JOIN_PRUNE
,
self
.
upstream_neighbor_address
.
to_bytes
(
4
,
byteorder
=
'big'
)
,
0
,
len
(
self
.
groups
),
self
.
hold_time
)
upstream_neighbor_address
=
PacketPimEncodedUnicastAddress
(
self
.
upstream_neighbor_address
).
bytes
()
msg
=
struct
.
pack
(
PacketPimJoinPrune
.
PIM_HDR_JOIN_PRUNE
,
upstream_neighbor_address
,
0
,
len
(
self
.
groups
),
self
.
hold_time
)
for
multicast_group
in
self
.
groups
:
msg
+=
multicast_group
.
bytes
()
...
...
Packet/PacketPimJoinPruneMulticastGroup.py
View file @
179c9207
import
struct
import
socket
from
Packet.PacketPimEncodedGroupAddress
import
PacketPimEncodedGroupAddress
from
Packet.PacketPimEncodedSourceAddress
import
PacketPimEncodedSourceAddress
'''
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Multicast Group Address 1 (Encoded Group Format) |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Number of Joined Sources | Number of Pruned Sources |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Joined Source Address 1 (Encoded Source Format) |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| . |
| . |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Joined Source Address n (Encoded Source Format) |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Pruned Source Address 1 (Encoded Source Format) |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| . |
| . |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Pruned Source Address n (Encoded Source Format) |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
'''
class
PacketPimJoinPruneMulticastGroup
:
PIM_HDR_JOIN_PRUNE_MULTICAST_GROUP
=
"!
4
s HH"
PIM_HDR_JOIN_PRUNE_MULTICAST_GROUP
=
"!
"
+
str
(
PacketPimEncodedGroupAddress
.
PIM_ENCODED_GROUP_ADDRESS_HDR_LEN
)
+
"
s HH"
PIM_HDR_JOIN_PRUNE_MULTICAST_GROUP_LEN
=
struct
.
calcsize
(
PIM_HDR_JOIN_PRUNE_MULTICAST_GROUP
)
PIM_HDR_JOINED_PRUNED_SOURCE
=
"!
4
s"
PIM_HDR_JOINED_PRUNED_SOURCE
=
"!
"
+
str
(
PacketPimEncodedSourceAddress
.
PIM_ENCODED_SOURCE_ADDRESS_HDR_LEN
)
+
"
s"
PIM_HDR_JOINED_PRUNED_SOURCE_LEN
=
struct
.
calcsize
(
PIM_HDR_JOINED_PRUNED_SOURCE
)
def
__init__
(
self
,
multicast_group
,
joined_src_addresses
,
pruned_src_addresses
):
def
__init__
(
self
,
multicast_group
,
joined_src_addresses
:
list
,
pruned_src_addresses
:
list
):
if
type
(
multicast_group
)
not
in
(
str
,
bytes
):
raise
Exception
elif
type
(
multicast_group
)
is
bytes
:
self
.
multicast_group
=
socket
.
inet_ntoa
(
self
.
multicast_group
)
if
type
(
joined_src_addresses
)
is
not
list
:
raise
Exception
if
type
(
pruned_src_addresses
)
is
not
list
:
raise
Exception
self
.
multicast_group
=
multicast_group
self
.
joined_src_addresses
=
joined_src_addresses
self
.
pruned_src_addresses
=
pruned_src_addresses
def
bytes
(
self
)
->
bytes
:
# TODO: verificar multicast_group
msg
=
struct
.
pack
(
self
.
PIM_HDR_JOIN_PRUNE_MULTICAST_GROUP
,
self
.
multicast_group
.
to_bytes
(
4
,
byteorder
=
'big'
)
,
len
(
self
.
joined_src_addresses
),
len
(
self
.
pruned_src_addresses
))
multicast_group_address
=
PacketPimEncodedGroupAddress
(
self
.
multicast_group
).
bytes
()
msg
=
struct
.
pack
(
self
.
PIM_HDR_JOIN_PRUNE_MULTICAST_GROUP
,
multicast_group_address
,
len
(
self
.
joined_src_addresses
),
len
(
self
.
pruned_src_addresses
))
for
joined_src_address
in
self
.
joined_src_addresses
:
msg
+=
struct
.
pack
(
self
.
PIM_HDR_JOINED_PRUNED_SOURCE
,
joined_src_address
.
to_bytes
(
4
,
byteorder
=
'big'
))
joined_src_address_bytes
=
PacketPimEncodedSourceAddress
(
joined_src_address
).
bytes
()
msg
+=
struct
.
pack
(
self
.
PIM_HDR_JOINED_PRUNED_SOURCE
,
joined_src_address_bytes
)
for
pruned_src_address
in
self
.
pruned_src_addresses
:
msg
+=
struct
.
pack
(
self
.
PIM_HDR_JOINED_PRUNED_SOURCE
,
pruned_src_address
.
to_bytes
(
4
,
byteorder
=
'big'
)
)
# TODO: verificar pruned e joined addrss
pruned_src_address_bytes
=
PacketPimEncodedSourceAddress
(
pruned_src_address
).
bytes
(
)
msg
+=
struct
.
pack
(
self
.
PIM_HDR_JOINED_PRUNED_SOURCE
,
pruned_src_address_bytes
)
return
msg
Packet/ReceivedPacket.py
View file @
179c9207
...
...
@@ -5,7 +5,7 @@ from Packet.PacketPimHeader import PacketPimHeader
from
Packet.PacketPimHello
import
PacketPimHello
from
Packet.PacketPimJoinPrune
import
PacketPimJoinPrune
from
Packet.PacketPimJoinPruneMulticastGroup
import
PacketPimJoinPruneMulticastGroup
import
socket
from
utils
import
checksum
...
...
@@ -55,8 +55,8 @@ class ReceivedPacket(Packet):
"TTL"
:
ttl
,
"PROTO"
:
proto
,
"CKSUM"
:
cksum
,
"SRC"
:
s
rc
,
"DST"
:
dst
"SRC"
:
s
ocket
.
inet_ntoa
(
src
)
,
"DST"
:
socket
.
inet_ntoa
(
dst
)
}
def
parsePimHdr
(
msg
):
...
...
requirements.txt
View file @
179c9207
PrettyTable
netifaces
\ No newline at end of file
netifaces
ipaddress
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment