Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
R
re6stnet
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
2
Issues
2
List
Boards
Labels
Milestones
Merge Requests
4
Merge Requests
4
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
re6stnet
Commits
6452f083
Commit
6452f083
authored
Jul 13, 2012
by
Ulysse Beaugnon
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'master' of
https://git.erp5.org/repos/vifibnet
parents
bde5fdec
1d46fa73
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
66 additions
and
53 deletions
+66
-53
ipchange
ipchange
+4
-0
registry.py
registry.py
+16
-15
setup.py
setup.py
+2
-2
vifibnet.py
vifibnet.py
+44
-36
No files found.
ipchange
0 → 100755
View file @
6452f083
#!/usr/bin/python -S
import
os
,
sys
os
.
write
(
int
(
sys
.
argv
[
1
]),
'%(script_type)s %(external_ip)s
\
n
'
%
os
.
environ
)
registry.py
View file @
6452f083
...
@@ -20,10 +20,6 @@ class main(object):
...
@@ -20,10 +20,6 @@ class main(object):
parser
=
argparse
.
ArgumentParser
(
parser
=
argparse
.
ArgumentParser
(
description
=
'Peer discovery http server for vifibnet'
)
description
=
'Peer discovery http server for vifibnet'
)
_
=
parser
.
add_argument
_
=
parser
.
add_argument
_
(
'--prefix'
,
required
=
True
,
help
=
'Prefix of the network deployed ( example : 2001:db8:42'
)
_
(
'--prefix-len'
,
required
=
True
,
type
=
int
,
help
=
'Prefix length'
)
_
(
'--db'
,
required
=
True
,
_
(
'--db'
,
required
=
True
,
help
=
'Path to database file'
)
help
=
'Path to database file'
)
_
(
'--ca'
,
required
=
True
,
_
(
'--ca'
,
required
=
True
,
...
@@ -52,9 +48,7 @@ class main(object):
...
@@ -52,9 +48,7 @@ class main(object):
email text,
email text,
cert text)"""
)
cert text)"""
)
except
sqlite3
.
OperationalError
,
e
:
except
sqlite3
.
OperationalError
,
e
:
if
e
.
args
[
0
]
==
'table vifib already exists'
:
if
e
.
args
[
0
]
!=
'table vifib already exists'
:
pass
else
:
raise
RuntimeError
raise
RuntimeError
else
:
else
:
self
.
db
.
execute
(
"INSERT INTO vifib VALUES ('',null,null)"
)
self
.
db
.
execute
(
"INSERT INTO vifib VALUES ('',null,null)"
)
...
@@ -67,6 +61,7 @@ class main(object):
...
@@ -67,6 +61,7 @@ class main(object):
self
.
key
=
crypto
.
load_privatekey
(
crypto
.
FILETYPE_PEM
,
f
.
read
())
self
.
key
=
crypto
.
load_privatekey
(
crypto
.
FILETYPE_PEM
,
f
.
read
())
# Get vifib network prefix
# Get vifib network prefix
self
.
network
=
bin
(
self
.
ca
.
get_serial_number
())[
3
:]
self
.
network
=
bin
(
self
.
ca
.
get_serial_number
())[
3
:]
print
"Network prefix : %s/%u"
%
(
self
.
network
,
len
(
self
.
network
))
# Starting server
# Starting server
server
=
SimpleXMLRPCServer
((
"localhost"
,
8000
),
requestHandler
=
RequestHandler
,
allow_none
=
True
)
server
=
SimpleXMLRPCServer
((
"localhost"
,
8000
),
requestHandler
=
RequestHandler
,
allow_none
=
True
)
...
@@ -147,22 +142,28 @@ class main(object):
...
@@ -147,22 +142,28 @@ class main(object):
client_address
,
_
=
handler
.
client_address
client_address
,
_
=
handler
.
client_address
# For Testing purposes only
# For Testing purposes only
client_address
=
"2001:db8:42::"
client_address
=
"2001:db8:42::"
assert
(
client_address
.
startswith
(
self
.
config
.
prefix
))
ip1
,
ip2
=
struct
.
unpack
(
'>QQ'
,
socket
.
inet_pton
(
socket
.
AF_INET6
,
client_address
))
ip1
,
ip2
=
struct
.
unpack
(
'>QQ'
,
socket
.
inet_pton
(
socket
.
AF_INET6
,
client_address
))
ip
1
=
bin
(
ip1
)[
2
:].
rjust
(
64
,
'0'
)
ip
=
bin
(
ip1
)[
2
:].
rjust
(
64
,
'0'
)
+
bin
(
ip2
)[
2
:].
rjust
(
64
,
'0'
)
i
p2
=
bin
(
ip2
)[
2
:].
rjust
(
64
,
'0'
)
i
f
ip
.
startswith
(
self
.
network
):
prefix
=
(
ip1
+
ip2
)[
self
.
config
.
prefix_len
:]
prefix
=
ip
[
len
(
self
.
network
)
:]
prefix
,
=
self
.
db
.
execute
(
"SELECT prefix FROM vifib WHERE prefix <= ? ORDER BY prefix DESC"
,
(
prefix
,)).
next
()
prefix
,
=
self
.
db
.
execute
(
"SELECT prefix FROM vifib WHERE prefix <= ? ORDER BY prefix DESC"
,
(
prefix
,)).
next
()
ip
,
port
,
proto
=
address
ip
,
port
,
proto
=
address
self
.
db
.
execute
(
"INSERT OR REPLACE INTO peers VALUES (?,?,?,?)"
,
(
prefix
,
ip
,
port
,
proto
))
self
.
db
.
execute
(
"INSERT OR REPLACE INTO peers VALUES (?,?,?,?)"
,
(
prefix
,
ip
,
port
,
proto
))
return
True
else
:
print
"Unauthorized connection from %s which does not start with %s"
%
(
ip
,
self
.
network
)
return
False
def
getPeerList
(
self
,
handler
,
n
,
address
):
def
getPeerList
(
self
,
handler
,
n
,
address
):
assert
0
<
n
<
1000
assert
0
<
n
<
1000
print
"declaring new node"
print
"declaring new node"
self
.
declare
(
handler
,
address
)
if
not
self
.
declare
(
handler
,
address
):
# TODO: do something intelligent
raise
RuntimeError
print
"sending peers"
print
"sending peers"
return
self
.
db
.
execute
(
"SELECT ip, port, proto FROM peers ORDER BY random() LIMIT ?"
,
(
n
,)).
fetchall
()
return
self
.
db
.
execute
(
"SELECT ip, port, proto FROM peers ORDER BY random() LIMIT ?"
,
(
n
,)).
fetchall
()
if
__name__
==
"__main__"
:
if
__name__
==
"__main__"
:
main
()
main
()
setup.py
View file @
6452f083
...
@@ -18,7 +18,7 @@ def main():
...
@@ -18,7 +18,7 @@ def main():
config
=
parser
.
parse_args
()
config
=
parser
.
parse_args
()
if
config
.
req
and
len
(
config
.
req
)
%
2
==
1
:
if
config
.
req
and
len
(
config
.
req
)
%
2
==
1
:
print
"Sorry, request argument was incorrect, there must be an even number of request arguments"
print
"Sorry, request argument was incorrect, there must be an even number of request arguments"
os
.
exit
(
1
)
exit
(
1
)
# Get token
# Get token
email
=
raw_input
(
'Please enter your email address : '
)
email
=
raw_input
(
'Please enter your email address : '
)
...
@@ -44,7 +44,7 @@ def main():
...
@@ -44,7 +44,7 @@ def main():
# Get certificates
# Get certificates
ca
=
s
.
getCa
()
ca
=
s
.
getCa
()
cert
=
s
.
requestCertificate
(
token
,
req
)
cert
=
s
.
requestCertificate
(
token
,
req
)
# Generating dh file
# Generating dh file
subprocess
.
call
([
'openssl'
,
'dhparam'
,
'-out'
,
os
.
path
.
join
(
config
.
dir
,
'dh2048.pem'
),
'2048'
])
subprocess
.
call
([
'openssl'
,
'dhparam'
,
'-out'
,
os
.
path
.
join
(
config
.
dir
,
'dh2048.pem'
),
'2048'
])
...
...
vifibnet.py
View file @
6452f083
...
@@ -7,13 +7,12 @@ import openvpn
...
@@ -7,13 +7,12 @@ import openvpn
import
random
import
random
import
log
import
log
VIFIB_NET
=
"2001:db8:42:"
VIFIB_NET
=
''
VIFIB_LEN
=
48
connection_dict
=
{}
# to remember current connections we made
connection_dict
=
{}
# to remember current connections we made
free_interface_set
=
set
((
'client1'
,
'client2'
,
'client3'
,
'client4'
,
'client5'
,
free_interface_set
=
set
((
'client1'
,
'client2'
,
'client3'
,
'client4'
,
'client5'
,
'client6'
,
'client7'
,
'client8'
,
'client9'
,
'client10'
))
'client6'
,
'client7'
,
'client8'
,
'client9'
,
'client10'
))
# TODO
: flag in some way the peers that are connected to us so we don't connect to them
# TODO: flag in some way the peers that are connected to us so we don't connect to them
# Or maybe we just don't care
# Or maybe we just don't care
class
PeersDB
:
class
PeersDB
:
...
@@ -57,21 +56,24 @@ class PeersDB:
...
@@ -57,21 +56,24 @@ class PeersDB:
log
.
log
(
'Updating peers database : unusing peer '
+
str
(
id
),
5
)
log
.
log
(
'Updating peers database : unusing peer '
+
str
(
id
),
5
)
self
.
db
.
execute
(
"UPDATE peers SET used = 0 WHERE id = ?"
,
(
id
,))
self
.
db
.
execute
(
"UPDATE peers SET used = 0 WHERE id = ?"
,
(
id
,))
# TODO: do everything using 'binary' strings
def
ipFromBin
(
prefix
):
prefix
=
hex
(
int
(
prefix
,
2
))[
2
:]
ip
=
''
for
i
in
xrange
(
0
,
len
(
prefix
)
-
1
,
4
):
ip
+=
prefix
[
i
:
i
+
4
]
+
':'
return
ip
.
rstrip
(
':'
)
def
ipFromPrefix
(
prefix
,
prefix_len
):
def
ipFromPrefix
(
prefix
,
prefix_len
):
tmp
=
hex
(
int
(
prefix
))[
2
:]
prefix
=
bin
(
int
(
prefix
))[
2
:].
rjust
(
prefix_len
,
'0'
)
tmp
=
tmp
.
rjust
(
int
((
math
.
ceil
(
float
(
prefix_len
)
/
4
))),
'0'
)
ip_t
=
(
config
.
vifibnet
+
prefix
).
ljust
(
128
,
'0'
)
ip
=
VIFIB_NET
return
ipFromBin
(
ip_t
)
for
i
in
xrange
(
0
,
len
(
tmp
),
4
):
ip
+=
tmp
[
i
:
i
+
4
]
+
':'
return
ip
+
':'
def
startBabel
(
**
kw
):
def
startBabel
(
**
kw
):
args
=
[
'babeld'
,
args
=
[
'babeld'
,
'-C'
,
'redistribute local ip %s'
%
(
config
.
ip
),
'-C'
,
'redistribute local ip %s'
%
(
config
.
ip
),
'-C'
,
'redistribute local deny'
,
'-C'
,
'redistribute local deny'
,
# Route VIFIB ip adresses
# Route VIFIB ip adresses
'-C'
,
'in ip %s:
/%u'
%
(
VIFIB_NET
,
VIFIB_LEN
),
'-C'
,
'in ip %s:
:/%u'
%
(
ipFromBin
(
config
.
vifibnet
),
len
(
config
.
vifibnet
)
),
# Route only addresse in the 'local' network,
# Route only addresse in the 'local' network,
# or other entire networks
# or other entire networks
#'-C', 'in ip %s' % (config.ip),
#'-C', 'in ip %s' % (config.ip),
...
@@ -98,7 +100,7 @@ def getConfig():
...
@@ -98,7 +100,7 @@ def getConfig():
help
=
'Path to vifibnet logs directory'
)
help
=
'Path to vifibnet logs directory'
)
_
(
'--client-count'
,
default
=
2
,
type
=
int
,
_
(
'--client-count'
,
default
=
2
,
type
=
int
,
help
=
'Number of client connections'
)
help
=
'Number of client connections'
)
# TODO
: use maxpeer
# TODO: use maxpeer
_
(
'--max-clients'
,
default
=
10
,
type
=
int
,
_
(
'--max-clients'
,
default
=
10
,
type
=
int
,
help
=
'the number of peers that can connect to the server'
)
help
=
'the number of peers that can connect to the server'
)
_
(
'--refresh-time'
,
default
=
60
,
type
=
int
,
_
(
'--refresh-time'
,
default
=
60
,
type
=
int
,
...
@@ -113,57 +115,63 @@ def getConfig():
...
@@ -113,57 +115,63 @@ def getConfig():
help
=
'Path to babeld state-file'
)
help
=
'Path to babeld state-file'
)
_
(
'--verbose'
,
'-v'
,
default
=
0
,
type
=
int
,
_
(
'--verbose'
,
'-v'
,
default
=
0
,
type
=
int
,
help
=
'Defines the verbose level'
)
help
=
'Defines the verbose level'
)
_
(
'--ca'
,
required
=
True
,
help
=
'Path to the certificate authority file'
)
_
(
'--cert'
,
required
=
True
,
_
(
'--cert'
,
required
=
True
,
help
=
'Path to the certificate file'
)
help
=
'Path to the certificate file'
)
# Temporary args - to be removed
# Can be removed, should ip be a global variable ?
_
(
'--ip'
,
required
=
True
,
help
=
'IPv6 of the server'
)
# Openvpn options
# Openvpn options
_
(
'openvpn_args'
,
nargs
=
argparse
.
REMAINDER
,
_
(
'openvpn_args'
,
nargs
=
argparse
.
REMAINDER
,
help
=
"Common OpenVPN options (e.g. certificates)"
)
help
=
"Common OpenVPN options (e.g. certificates)"
)
openvpn
.
config
=
config
=
parser
.
parse_args
()
openvpn
.
config
=
config
=
parser
.
parse_args
()
log
.
verbose
=
config
.
verbose
log
.
verbose
=
config
.
verbose
# Get network prefix from ca.crt
with
open
(
config
.
ca
,
'r'
)
as
f
:
ca
=
crypto
.
load_certificate
(
crypto
.
FILETYPE_PEM
,
f
.
read
())
config
.
vifibnet
=
bin
(
ca
.
get_serial_number
())[
3
:]
# Get ip from cert.crt
with
open
(
config
.
cert
,
'r'
)
as
f
:
with
open
(
config
.
cert
,
'r'
)
as
f
:
cert
=
crypto
.
load_certificate
(
crypto
.
FILETYPE_PEM
,
f
.
read
())
cert
=
crypto
.
load_certificate
(
crypto
.
FILETYPE_PEM
,
f
.
read
())
subject
=
cert
.
get_subject
()
subject
=
cert
.
get_subject
()
prefix
,
prefix_len
=
subject
.
serialNumber
.
split
(
'/'
)
prefix
,
prefix_len
=
subject
.
serialNumber
.
split
(
'/'
)
ip
=
ipFromPrefix
(
prefix
,
int
(
prefix_len
))
config
.
ip
=
ipFromPrefix
(
prefix
,
int
(
prefix_len
))
log
.
log
(
'Intranet ip : %s'
%
(
ip
,),
3
)
log
.
log
(
'Intranet ip : %s'
%
(
config
.
ip
,),
3
)
# Treat openvpn arguments
if
config
.
openvpn_args
[
0
]
==
"--"
:
if
config
.
openvpn_args
[
0
]
==
"--"
:
del
config
.
openvpn_args
[
0
]
del
config
.
openvpn_args
[
0
]
config
.
openvpn_args
.
append
(
'--ca'
)
config
.
openvpn_args
.
append
(
config
.
ca
)
config
.
openvpn_args
.
append
(
'--cert'
)
config
.
openvpn_args
.
append
(
'--cert'
)
config
.
openvpn_args
.
append
(
config
.
cert
)
config
.
openvpn_args
.
append
(
config
.
cert
)
log
.
log
(
"Configuration completed"
,
1
)
log
.
log
(
"Configuration completed"
,
1
)
def
startNewConnection
(
n
,
write_pipe
):
def
startNewConnection
(
n
,
write_pipe
):
try
:
try
:
for
id
,
ip
,
port
,
proto
in
peers_db
.
getUnusedPeers
(
n
):
for
peer_
id
,
ip
,
port
,
proto
in
peers_db
.
getUnusedPeers
(
n
):
log
.
log
(
'Establishing a connection with id %s (%s:%s)'
%
(
id
,
ip
,
port
),
2
)
log
.
log
(
'Establishing a connection with id %s (%s:%s)'
%
(
peer_id
,
ip
,
port
),
2
)
iface
=
free_interface_set
.
pop
()
iface
=
free_interface_set
.
pop
()
connection_dict
[
id
]
=
(
openvpn
.
client
(
ip
,
write_pipe
,
'--dev'
,
iface
,
'--proto'
,
proto
,
'--rport'
,
str
(
port
),
connection_dict
[
peer_
id
]
=
(
openvpn
.
client
(
ip
,
write_pipe
,
'--dev'
,
iface
,
'--proto'
,
proto
,
'--rport'
,
str
(
port
),
stdout
=
os
.
open
(
os
.
path
.
join
(
config
.
log
,
'vifibnet.client.%s.log'
%
(
id
,)),
stdout
=
os
.
open
(
os
.
path
.
join
(
config
.
log
,
'vifibnet.client.%s.log'
%
(
peer_
id
,)),
os
.
O_WRONLY
|
os
.
O_CREAT
|
os
.
O_TRUNC
)
),
os
.
O_WRONLY
|
os
.
O_CREAT
|
os
.
O_TRUNC
)
),
iface
)
iface
)
peers_db
.
usePeer
(
id
)
peers_db
.
usePeer
(
peer_
id
)
except
KeyError
:
except
KeyError
:
log
.
log
(
"Can't establish connection with %s : no available interface"
%
ip
,
2
)
log
.
log
(
"Can't establish connection with %s : no available interface"
%
ip
,
2
)
pass
except
Exception
:
except
Exception
:
traceback
.
print_exc
()
traceback
.
print_exc
()
def
killConnection
(
id
):
def
killConnection
(
peer_
id
):
try
:
try
:
log
.
log
(
'Killing the connection with id '
+
str
(
id
),
2
)
log
.
log
(
'Killing the connection with id '
+
str
(
peer_
id
),
2
)
p
,
iface
=
connection_dict
.
pop
(
id
)
p
,
iface
=
connection_dict
.
pop
(
peer_
id
)
p
.
kill
()
p
.
kill
()
free_interface_set
.
add
(
iface
)
free_interface_set
.
add
(
iface
)
peers_db
.
unusePeer
(
id
)
peers_db
.
unusePeer
(
peer_
id
)
except
KeyError
:
except
KeyError
:
log
.
log
(
"Can't kill connection to "
+
peer
+
": no existing connection"
,
1
)
log
.
log
(
"Can't kill connection to "
+
peer
_id
+
": no existing connection"
,
1
)
pass
pass
except
Exception
:
except
Exception
:
log
.
log
(
"Can't kill connection to "
+
peer
+
": uncaught error"
,
1
)
log
.
log
(
"Can't kill connection to "
+
peer
_id
+
": uncaught error"
,
1
)
pass
pass
def
checkConnections
():
def
checkConnections
():
...
@@ -175,23 +183,23 @@ def checkConnections():
...
@@ -175,23 +183,23 @@ def checkConnections():
peers_db
.
unusePeer
(
id
)
peers_db
.
unusePeer
(
id
)
del
connection_dict
[
id
]
del
connection_dict
[
id
]
def
refreshConnections
():
def
refreshConnections
(
write_pipe
):
checkConnections
()
checkConnections
()
# Kill some random connections
# Kill some random connections
try
:
try
:
for
i
in
range
(
0
,
max
(
0
,
len
(
connection_dict
)
-
config
.
client_count
+
config
.
refresh_count
)):
for
i
in
range
(
0
,
max
(
0
,
len
(
connection_dict
)
-
config
.
client_count
+
config
.
refresh_count
)):
id
=
random
.
choice
(
connection_dict
.
keys
())
peer_
id
=
random
.
choice
(
connection_dict
.
keys
())
killConnection
(
id
)
killConnection
(
peer_
id
)
except
Exception
:
except
Exception
:
pass
pass
# Establish new connections
# Establish new connections
startNewConnection
(
config
.
client_count
-
len
(
connection_dict
))
startNewConnection
(
config
.
client_count
-
len
(
connection_dict
)
,
write_pipe
)
def
handle_message
(
msg
):
def
handle_message
(
msg
):
script_type
,
arg
=
msg
.
split
()
script_type
,
arg
=
msg
.
split
()
if
script_type
==
'client-connect'
:
if
script_type
==
'client-connect'
:
log
.
log
(
'Incomming connection from %s'
%
(
arg
,),
3
)
log
.
log
(
'Incomming connection from %s'
%
(
arg
,),
3
)
# TODO
:
check if we are not already connected to it
# TODO
:
check if we are not already connected to it
elif
script_type
==
'client-disconnect'
:
elif
script_type
==
'client-disconnect'
:
log
.
log
(
'%s has disconnected'
%
(
arg
,),
3
)
log
.
log
(
'%s has disconnected'
%
(
arg
,),
3
)
elif
script_type
==
'ipchange'
:
elif
script_type
==
'ipchange'
:
...
@@ -237,7 +245,7 @@ def main():
...
@@ -237,7 +245,7 @@ def main():
if
ready
:
if
ready
:
handle_message
(
read_pipe
.
readline
())
handle_message
(
read_pipe
.
readline
())
if
time
.
time
()
>=
next_refresh
:
if
time
.
time
()
>=
next_refresh
:
refreshConnections
()
refreshConnections
(
write_pipe
)
next_refresh
=
time
.
time
()
+
config
.
refresh_time
next_refresh
=
time
.
time
()
+
config
.
refresh_time
except
KeyboardInterrupt
:
except
KeyboardInterrupt
:
return
0
return
0
...
...
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