forked from P2PSP/core
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added workaround to force new port allocation on SYMSP NATs.
- Loading branch information
Showing
3 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# This code is distributed under the GNU General Public License (see | ||
# THE_GENERAL_GNU_PUBLIC_LICENSE.txt for extending this information). | ||
# Copyright (C) 2014, the P2PSP team. | ||
# http://www.p2psp.org | ||
|
||
# The P2PSP.org project has been supported by the Junta de Andalucia | ||
# through the Proyecto Motriz "Codificacion de Video Escalable y su | ||
# Streaming sobre Internet" (P10-TIC-6548). | ||
|
||
# {{{ Imports | ||
import sys | ||
import socket | ||
from peer_dbs import Peer_DBS | ||
from color import Color | ||
from symsp_socket import symsp_socket | ||
# }}} | ||
|
||
# Some useful definitions. | ||
ADDR = 0 | ||
PORT = 1 | ||
|
||
class Symsp_Peer(Peer_DBS): | ||
# {{{ | ||
|
||
PORT_STEP = 0 | ||
|
||
def __init__(self, peer): | ||
# {{{ | ||
|
||
sys.stdout.write(Color.yellow) | ||
sys.stdout.write("Symsp Peer\n") | ||
sys.stdout.write(Color.none) | ||
|
||
# }}} | ||
|
||
def listen_to_the_team(self): | ||
# {{{ Create "team_socket" (UDP) as a copy of "splitter_socket" (TCP) | ||
|
||
self.team_socket = symsp_socket(self.PORT_STEP, socket.AF_INET, socket.SOCK_DGRAM) | ||
try: | ||
# In Windows systems this call doesn't work! | ||
self.team_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) | ||
except Exception as e: | ||
print ("NTS:", e) | ||
pass | ||
self.team_socket.bind(('', self.splitter_socket.getsockname()[PORT])) | ||
|
||
# This is the maximum time the peer will wait for a chunk | ||
# (from the splitter or from another peer). | ||
self.team_socket.settimeout(1) | ||
|
||
# }}} | ||
|
||
# }}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# -*- coding: iso-8859-15 -*- | ||
|
||
# The P2PSP.org project has been supported by the Junta de Andalucía | ||
# through the Proyecto Motriz "Codificación de Vídeo Escalable y su | ||
# Streaming sobre Internet" (P10-TIC-6548). | ||
|
||
# Overwrites the "sendto" method of the socket library, in order to | ||
# force sequential source port allocation. | ||
|
||
import socket | ||
|
||
class symsp_socket(): | ||
|
||
def __init__(self, port_step, *p): | ||
self._sock = socket.socket(*p) | ||
self.destinations = [] | ||
self.port_step = port_step | ||
|
||
def sendto(self, message, destination): | ||
# {{{ | ||
|
||
# For each destination endpoint that was not sent to yet, this socket | ||
# wrapper creates a temporary socket and connects to the destination a | ||
# specified number of times before sending from the original socket. | ||
# This forces the NAT to create a new mapping entry. | ||
|
||
if destination not in self.destinations: | ||
for _ in range(self.port_step): | ||
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | ||
sock.sendto('', destination) | ||
sock.close() | ||
self.destinations.append(destination) | ||
return self._sock.sendto(message, destination) | ||
|
||
# }}} | ||
|
||
def bind(self, *p): | ||
return self._sock.bind(*p) | ||
|
||
def settimeout(self, *p): | ||
return self._sock.settimeout(*p) | ||
|
||
def getsockname(self, *p): | ||
return self._sock.getsockname(*p) | ||
|
||
def recvfrom(self, *p): | ||
return self._sock.recvfrom(*p) | ||
|
||
def setsockopt(self, *p): | ||
return self._sock.setsockopt(*p) |