Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
M
multicast-study
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
multicast-study
Commits
830d73db
Commit
830d73db
authored
Apr 28, 2022
by
Kirill Smelkov
Committed by
Léo-Paul Géneau
Apr 29, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
.
parent
ff84a428
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
38 additions
and
17 deletions
+38
-17
namespace_demo/tmcast.py
namespace_demo/tmcast.py
+38
-17
No files found.
namespace_demo/tmcast.py
View file @
830d73db
...
@@ -5,9 +5,10 @@ from __future__ import print_function
...
@@ -5,9 +5,10 @@ from __future__ import print_function
import
sys
,
time
import
sys
,
time
from
socket
import
socket
,
AF_INET
,
SOCK_DGRAM
,
IPPROTO_UDP
,
SOL_SOCKET
,
\
from
socket
import
socket
,
AF_INET
,
AF_INET6
,
SOCK_DGRAM
,
IPPROTO_UDP
,
\
SO_REUSEADDR
,
IPPROTO_IP
,
IP_ADD_MEMBERSHIP
,
IP_MULTICAST_TTL
,
IP_MULTICAST_LOOP
,
\
SOL_SOCKET
,
SO_REUSEADDR
,
IPPROTO_IP
,
IPPROTO_IPV6
,
IP_ADD_MEMBERSHIP
,
\
inet_aton
,
INADDR_ANY
IPV6_JOIN_GROUP
,
IP_MULTICAST_TTL
,
IPV6_MULTICAST_HOPS
,
IP_MULTICAST_LOOP
,
\
IPV6_MULTICAST_LOOP
,
inet_pton
import
socket
as
net
import
socket
as
net
from
struct
import
pack
from
struct
import
pack
...
@@ -16,41 +17,61 @@ from golang import sync, context
...
@@ -16,41 +17,61 @@ from golang import sync, context
group4
=
'224.1.1.1'
group4
=
'224.1.1.1'
group6
=
'
XXX
'
group6
=
'
ff15:7079:7468:6f6e:6465:6d6f:6d63:6173
'
port
=
5678
port
=
5678
def
_udpsockfor
(
ipaddr
):
# -> (sk, isipv6)
ip6
=
(
':'
in
ipaddr
)
# autodetect ip4/ip6 based on group addr
if
ip6
:
sk
=
socket
(
AF_INET6
,
SOCK_DGRAM
,
IPPROTO_UDP
)
else
:
sk
=
socket
(
AF_INET
,
SOCK_DGRAM
,
IPPROTO_UDP
)
return
(
sk
,
ip6
)
# mjoin_tx returns socket prepated to send/receive to/from multicast group:port.
# mjoin_tx returns socket prepated to send/receive to/from multicast group:port.
def
mjoin_tx
(
group
,
port
,
ttl
=
2
):
def
mjoin_tx
(
group
,
port
,
ttl
=
2
):
# XXX autodetect ip4/ip6 based on group addr
sk
,
ip6
=
_udpsockfor
(
group
)
sk
=
socket
(
AF_INET
,
SOCK_DGRAM
,
IPPROTO_UDP
)
# set ttl and default destination address (for tx)
# set ttl and default destination address (for tx)
sk
.
setsockopt
(
IPPROTO_IP
,
IP_MULTICAST_TTL
,
pack
(
'b'
,
ttl
))
bttl
=
pack
(
'b'
,
ttl
)
if
ip6
:
sk
.
setsockopt
(
IPPROTO_IPV6
,
IPV6_MULTICAST_HOPS
,
bttl
)
else
:
sk
.
setsockopt
(
IPPROTO_IP
,
IP_MULTICAST_TTL
,
bttl
)
sk
.
connect
((
group
,
port
))
sk
.
connect
((
group
,
port
))
# so that what we send is received locally as well
# so that what we send is received locally as well
sk
.
setsockopt
(
IPPROTO_IP
,
IP_MULTICAST_LOOP
,
1
)
if
ip6
:
sk
.
setsockopt
(
IPPROTO_IPV6
,
IPV6_MULTICAST_LOOP
,
1
)
else
:
sk
.
setsockopt
(
IPPROTO_IP
,
IP_MULTICAST_LOOP
,
1
)
return
sk
return
sk
# mjoin_rx returns socket prepated to send/receive to/from multicast group:port.
# mjoin_rx returns socket prepated to send/receive to/from multicast group:port.
def
mjoin_rx
(
group
,
port
):
def
mjoin_rx
(
group
,
port
):
# XXX autodetect ip4/ip6 based on group addr
sk
,
ip6
=
_udpsockfor
(
group
)
sk
=
socket
(
AF_INET
,
SOCK_DGRAM
,
IPPROTO_UDP
)
sk
.
setsockopt
(
SOL_SOCKET
,
SO_REUSEADDR
,
1
)
sk
.
setsockopt
(
SOL_SOCKET
,
SO_REUSEADDR
,
1
)
sk
.
bind
((
group
,
port
))
# works for reception even from multiple ifaces
sk
.
bind
((
group
,
port
))
# works for reception even from multiple ifaces
#sk.bind(('', port)) # XXX and later IP_MULTICAST_ALL=0 ?
#sk.bind(('', port)) # XXX and later IP_MULTICAST_ALL=0 ?
## join the group (for rx)
#mreq = pack("4sl", inet_aton(group), INADDR_ANY)
#sk.setsockopt(IPPROTO_IP, IP_ADD_MEMBERSHIP, mreq)
# join the group on all interfaces
# join the group on all interfaces
for
ifidx
,
ifname
in
net
.
if_nameindex
():
for
ifidx
,
ifname
in
net
.
if_nameindex
():
print
(
"joining %s @ [%d]%s"
%
(
group
,
ifidx
,
ifname
))
print
(
"joining %s @ [%d]%s"
%
(
group
,
ifidx
,
ifname
))
mreq
=
pack
(
"4sii"
,
inet_aton
(
group
),
INADDR_ANY
,
ifidx
)
bifidx
=
pack
(
"@i"
,
ifidx
)
#print(repr(mreq), len(mreq))
if
ip6
:
sk
.
setsockopt
(
IPPROTO_IP
,
IP_ADD_MEMBERSHIP
,
mreq
)
mreq
=
inet_pton
(
AF_INET6
,
group
)
+
\
bifidx
sk
.
setsockopt
(
IPPROTO_IPV6
,
IPV6_JOIN_GROUP
,
mreq
)
# = IPV6_ADD_MEMBERSHIP
else
:
mreq
=
inet_pton
(
AF_INET
,
group
)
+
\
inet_pton
(
AF_INET
,
'0.0.0.0'
)
+
\
bifidx
#print(repr(mreq), len(mreq))
sk
.
setsockopt
(
IPPROTO_IP
,
IP_ADD_MEMBERSHIP
,
mreq
)
return
sk
return
sk
...
...
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