1 # -*- coding: utf-8 -*-
4 # UDP link implementation
6 # (C) 2017 by Vadim Yanitskiy <axilirator@gmail.com>
10 # This program is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 2 of the License, or
13 # (at your option) any later version.
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
23 def __init__(self
, remote_addr
, remote_port
, bind_addr
= '0.0.0.0', bind_port
= 0):
24 self
.sock
= socket
.socket(socket
.AF_INET
, socket
.SOCK_DGRAM
)
25 self
.sock
.setsockopt(socket
.SOL_SOCKET
, socket
.SO_REUSEADDR
, 1)
26 self
.sock
.bind((bind_addr
, bind_port
))
27 self
.sock
.setblocking(False)
30 self
.remote_addr
= remote_addr
31 self
.remote_port
= remote_port
37 (bind_addr
, bind_port
) = self
.sock
.getsockname()
39 return "L:%s:%u <-> R:%s:%u" \
40 % (bind_addr
, bind_port
, self
.remote_addr
, self
.remote_port
)
43 if type(data
) not in [bytearray
, bytes
]:
46 self
.sock
.sendto(data
, (self
.remote_addr
, self
.remote_port
))
48 def sendto(self
, data
, remote
):
49 if type(data
) not in [bytearray
, bytes
]:
52 self
.sock
.sendto(data
, remote
)