Skip to content

Commit be15a06

Browse files
committed
refactor: (cont) merge transport/windows -> platform/windows
1 parent c0b634e commit be15a06

File tree

10 files changed

+102
-100
lines changed

10 files changed

+102
-100
lines changed

src/ndn/app_support/dispatcher.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
# -----------------------------------------------------------------------------
1818
from typing import Optional
1919
from ..encoding import NonStrictName, Name, BinaryStr, InterestParam, FormalName
20-
from ..types import Validator, Route
20+
from ..types import Route
2121
from ..name_tree import NameTrie, PrefixTreeNode
2222

2323

src/ndn/client_conf.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ def resolve_location(item: str, value: str) -> str:
8585

8686

8787
def default_keychain(pib: str, tpm: str) -> Keychain:
88-
pib_schema, pib_loc = pib.split(':')
89-
tpm_schema, tpm_loc = tpm.split(':')
88+
pib_schema, pib_loc = pib.split(':', 1)
89+
tpm_schema, tpm_loc = tpm.split(':', 1)
9090
if tpm_schema == 'tpm-file':
9191
tpm = TpmFile(tpm_loc)
9292
elif tpm_schema == 'tpm-osxkeychain':

src/ndn/encoding/tlv_model.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,7 @@ def encoded_length(self, markers: Optional[dict] = None) -> int:
683683
ret = 0
684684
for field in self._encoded_fields:
685685
ret += field.encoded_length(field.get_value(self), markers)
686-
markers[f'##encoded_length'] = ret
686+
markers['##encoded_length'] = ret
687687
return ret
688688

689689
def encode(self,
@@ -706,8 +706,8 @@ def encode(self,
706706
"""
707707
if markers is None:
708708
markers = {}
709-
if f'##encoded_length' in markers:
710-
length = markers[f'##encoded_length']
709+
if '##encoded_length' in markers:
710+
length = markers['##encoded_length']
711711
else:
712712
length = self.encoded_length(markers)
713713
if wire is None:

src/ndn/platform/general.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,7 @@ def default_tpm_schema(self) -> str:
6464
@abc.abstractmethod
6565
def default_tpm_paths(self) -> List[str]:
6666
pass
67+
68+
@abc.abstractmethod
69+
async def open_unix_connection(self, path=None):
70+
pass

src/ndn/platform/linux.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
# limitations under the License.
1717
# -----------------------------------------------------------------------------
1818
import os
19+
import asyncio as aio
1920
from .general import Platform
2021

2122

@@ -40,3 +41,6 @@ def default_tpm_schema(self):
4041

4142
def default_tpm_paths(self):
4243
return [os.path.expanduser(r'~/.ndn/ndnsec-key-file')]
44+
45+
async def open_unix_connection(self, path=None):
46+
return await aio.open_unix_connection(path)

src/ndn/platform/osx.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
# -----------------------------------------------------------------------------
1818
import os
1919
import sys
20+
import asyncio as aio
2021
from ctypes import cdll, c_void_p, c_ubyte, POINTER, c_int32, c_ulong, c_uint16
2122
from .general import Platform
2223
if sys.platform == 'darwin':
@@ -133,3 +134,6 @@ def default_tpm_schema(self):
133134

134135
def default_tpm_paths(self):
135136
return [os.path.expanduser(r'~/.ndn/ndnsec-key-file')]
137+
138+
async def open_unix_connection(self, path=None):
139+
return await aio.open_unix_connection(path)

src/ndn/platform/windows.py

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,22 @@
1616
# limitations under the License.
1717
# -----------------------------------------------------------------------------
1818
import os
19+
import aenum
20+
import socket
21+
import asyncio as aio
1922
import ctypes as c
2023
from .general import Platform
2124

2225

26+
class SockaddrUn(c.Structure):
27+
_fields_ = [("sun_family", c.c_ushort), ("sun_path", c.c_char * 108)]
28+
29+
30+
aenum.extend_enum(socket.AddressFamily, "AF_UNIX", 1)
31+
AF_UNIX = socket.AddressFamily(1)
32+
NULL = 0
33+
34+
2335
class Cng:
2436
__instance = None
2537

@@ -45,7 +57,7 @@ def client_conf_paths(self):
4557

4658
def default_transport(self):
4759
# Note: %TEMP% won't be redirected even when the executable is a MSIX/MicrosoftStore app
48-
return 'unix://' + os.path.expandvars(r'%TEMP%\ndn\nfd.sock')
60+
return 'unix://' + os.path.expandvars(r'%TEMP%\nfd.sock')
4961

5062
def default_pib_schema(self):
5163
return 'pib-sqlite3'
@@ -60,3 +72,71 @@ def default_tpm_schema(self):
6072
def default_tpm_paths(self):
6173
return [os.path.expandvars(r'%LOCALAPPDATA%\ndn\ndnsec-key-file'),
6274
os.path.expandvars(r'%USERPROFILE%\ndn\ndnsec-key-file')]
75+
76+
@staticmethod
77+
def _iocp_connect(proactor, conn, address):
78+
# _overlapped.WSAConnect(conn.fileno(), address)
79+
addr = SockaddrUn(AF_UNIX.value, address.encode() + b"\0")
80+
winsock = c.windll.ws2_32
81+
winsock.connect(conn.fileno(), addr, 110)
82+
83+
fut = proactor._loop.create_future()
84+
fut.set_result(None)
85+
return fut
86+
87+
@staticmethod
88+
async def _create_unix_connection(
89+
loop, protocol_factory, path=None, *,
90+
ssl=None, sock=None,
91+
server_hostname=None,
92+
ssl_handshake_timeout=None):
93+
assert server_hostname is None or isinstance(server_hostname, str)
94+
if ssl:
95+
if server_hostname is None:
96+
raise ValueError(
97+
'you have to pass server_hostname when using ssl')
98+
else:
99+
if server_hostname is not None:
100+
raise ValueError('server_hostname is only meaningful with ssl')
101+
if ssl_handshake_timeout is not None:
102+
raise ValueError(
103+
'ssl_handshake_timeout is only meaningful with ssl')
104+
105+
if path is not None:
106+
if sock is not None:
107+
raise ValueError(
108+
'path and sock can not be specified at the same time')
109+
110+
path = os.fspath(path)
111+
sock = socket.socket(AF_UNIX, socket.SOCK_STREAM, 0)
112+
try:
113+
sock.setblocking(False)
114+
# await loop.sock_connect(sock, path)
115+
await Win32._iocp_connect(loop._proactor, sock, path)
116+
except OSError:
117+
sock.close()
118+
raise
119+
120+
else:
121+
if sock is None:
122+
raise ValueError('no path and sock were specified')
123+
if sock.family != AF_UNIX or sock.type != socket.SOCK_STREAM:
124+
raise ValueError(
125+
f'A UNIX Domain Stream Socket was expected, got {sock!r}')
126+
sock.setblocking(False)
127+
128+
transport, protocol = await loop._create_connection_transport(
129+
sock, protocol_factory, ssl, server_hostname,
130+
ssl_handshake_timeout=ssl_handshake_timeout)
131+
return transport, protocol
132+
133+
async def open_unix_connection(self, path=None):
134+
"""
135+
Similar to `open_connection` but works with UNIX Domain Sockets.
136+
"""
137+
loop = aio.events.get_running_loop()
138+
reader = aio.StreamReader(limit=2 ** 16, loop=loop)
139+
protocol = aio.StreamReaderProtocol(reader, loop=loop)
140+
transport, _ = await Win32._create_unix_connection(loop, lambda: protocol, path)
141+
writer = aio.StreamWriter(transport, protocol, reader, loop)
142+
return reader, writer

src/ndn/security/tpm/tpm_cng.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@
1616
# limitations under the License.
1717
# -----------------------------------------------------------------------------
1818
import sys
19-
if sys.platform == 'darwin':
19+
if sys.platform == 'win32':
2020
from ...platform.windows import Cng

src/ndn/transport/stream_socket.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,10 @@
1717
# -----------------------------------------------------------------------------
1818
import io
1919
import abc
20-
import sys
2120
import asyncio as aio
2221
from typing import Optional, Callable, Coroutine, Any
2322
from ..encoding.tlv_var import read_tl_num_from_stream
24-
if sys.platform.startswith('win32'):
25-
from .windows import open_unix_connection
23+
from ..platform import Platform
2624

2725

2826
class Face(metaclass=abc.ABCMeta):
@@ -84,10 +82,7 @@ def __init__(self, path: str = ''):
8482
self.path = path
8583

8684
async def open(self):
87-
if sys.platform.startswith('win32'):
88-
self.reader, self.writer = await open_unix_connection(self.path)
89-
else:
90-
self.reader, self.writer = await aio.open_unix_connection(self.path)
85+
self.reader, self.writer = await Platform().open_unix_connection(self.path)
9186
self.running = True
9287

9388

src/ndn/transport/windows.py

Lines changed: 0 additions & 85 deletions
This file was deleted.

0 commit comments

Comments
 (0)