Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
opendht
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Deploy
Model registry
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
savoirfairelinux
opendht
Commits
cb2d14e9
Commit
cb2d14e9
authored
Feb 23, 2015
by
Adrien Béraud
Browse files
Options
Downloads
Patches
Plain Diff
add DhtMessage type for node-to-node communication
parent
4452452e
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
include/opendht/value.h
+41
-5
41 additions, 5 deletions
include/opendht/value.h
src/securedht.cpp
+4
-2
4 additions, 2 deletions
src/securedht.cpp
src/value.cpp
+52
-20
52 additions, 20 deletions
src/value.cpp
tools/dhtnode.cpp
+1
-1
1 addition, 1 deletion
tools/dhtnode.cpp
with
98 additions
and
28 deletions
include/opendht/value.h
+
41
−
5
View file @
cb2d14e9
...
@@ -324,19 +324,19 @@ struct Value : public Serializable
...
@@ -324,19 +324,19 @@ struct Value : public Serializable
/* "Peer" announcement
/* "Peer" announcement
*/
*/
struct
ServiceAnnouncement
:
public
Serializable
struct
Ip
ServiceAnnouncement
:
public
Serializable
{
{
ServiceAnnouncement
(
in_port_t
p
=
0
)
{
Ip
ServiceAnnouncement
(
in_port_t
p
=
0
)
{
ss
.
ss_family
=
0
;
ss
.
ss_family
=
0
;
setPort
(
p
);
setPort
(
p
);
}
}
ServiceAnnouncement
(
const
sockaddr
*
sa
,
socklen_t
sa_len
)
{
Ip
ServiceAnnouncement
(
const
sockaddr
*
sa
,
socklen_t
sa_len
)
{
if
(
sa
)
if
(
sa
)
std
::
copy_n
((
const
uint8_t
*
)
sa
,
sa_len
,
(
uint8_t
*
)
&
ss
);
std
::
copy_n
((
const
uint8_t
*
)
sa
,
sa_len
,
(
uint8_t
*
)
&
ss
);
}
}
ServiceAnnouncement
(
const
Blob
&
b
)
{
Ip
ServiceAnnouncement
(
const
Blob
&
b
)
{
unpackBlob
(
b
);
unpackBlob
(
b
);
}
}
...
@@ -358,10 +358,46 @@ struct ServiceAnnouncement : public Serializable
...
@@ -358,10 +358,46 @@ struct ServiceAnnouncement : public Serializable
static
bool
storePolicy
(
InfoHash
,
std
::
shared_ptr
<
Value
>&
,
InfoHash
,
const
sockaddr
*
,
socklen_t
);
static
bool
storePolicy
(
InfoHash
,
std
::
shared_ptr
<
Value
>&
,
InfoHash
,
const
sockaddr
*
,
socklen_t
);
/** print value for debugging */
/** print value for debugging */
friend
std
::
ostream
&
operator
<<
(
std
::
ostream
&
,
const
ServiceAnnouncement
&
);
friend
std
::
ostream
&
operator
<<
(
std
::
ostream
&
,
const
Ip
ServiceAnnouncement
&
);
private
:
private
:
sockaddr_storage
ss
;
sockaddr_storage
ss
;
};
};
struct
DhtMessage
:
public
Serializable
{
DhtMessage
(
uint16_t
s
,
Blob
msg
=
{})
:
service
(
s
),
message
(
msg
)
{}
DhtMessage
(
const
Blob
&
b
)
{
unpackBlob
(
b
);
}
virtual
void
pack
(
Blob
&
res
)
const
;
virtual
void
unpack
(
Blob
::
const_iterator
&
begin
,
Blob
::
const_iterator
&
end
);
static
const
ValueType
TYPE
;
static
bool
storePolicy
(
InfoHash
,
std
::
shared_ptr
<
Value
>&
,
InfoHash
,
const
sockaddr
*
,
socklen_t
);
/** print value for debugging */
friend
std
::
ostream
&
operator
<<
(
std
::
ostream
&
,
const
DhtMessage
&
);
private
:
uint16_t
service
;
Blob
message
;
};
const
std
::
array
<
std
::
reference_wrapper
<
const
ValueType
>
,
2
>
DEFAULT_TYPES
{
ValueType
::
USER_DATA
,
DhtMessage
::
TYPE
,
};
const
std
::
array
<
std
::
reference_wrapper
<
const
ValueType
>
,
1
>
DEFAULT_INSECURE_TYPES
{
IpServiceAnnouncement
::
TYPE
};
}
}
This diff is collapsed.
Click to expand it.
src/securedht.cpp
+
4
−
2
View file @
cb2d14e9
...
@@ -54,8 +54,10 @@ SecureDht::SecureDht(int s, int s6, crypto::Identity id)
...
@@ -54,8 +54,10 @@ SecureDht::SecureDht(int s, int s6, crypto::Identity id)
if
(
certId
!=
key_
->
getPublicKey
().
getId
())
if
(
certId
!=
key_
->
getPublicKey
().
getId
())
throw
DhtException
(
"SecureDht: provided certificate doesn't match private key."
);
throw
DhtException
(
"SecureDht: provided certificate doesn't match private key."
);
registerType
(
ValueType
::
USER_DATA
);
for
(
const
auto
&
type
:
DEFAULT_TYPES
)
registerInsecureType
(
ServiceAnnouncement
::
TYPE
);
registerType
(
type
);
for
(
const
auto
&
type
:
DEFAULT_INSECURE_TYPES
)
registerInsecureType
(
type
);
registerInsecureType
(
CERTIFICATE_TYPE
);
registerInsecureType
(
CERTIFICATE_TYPE
);
Dht
::
put
(
certId
,
Value
{
Dht
::
put
(
certId
,
Value
{
...
...
This diff is collapsed.
Click to expand it.
src/value.cpp
+
52
−
20
View file @
cb2d14e9
...
@@ -41,8 +41,8 @@ std::ostream& operator<< (std::ostream& s, const Value& v)
...
@@ -41,8 +41,8 @@ std::ostream& operator<< (std::ostream& s, const Value& v)
if
(
v
.
flags
.
isEncrypted
())
if
(
v
.
flags
.
isEncrypted
())
s
<<
"encrypted "
;
s
<<
"encrypted "
;
else
{
else
{
if
(
v
.
type
==
ServiceAnnouncement
::
TYPE
.
id
)
{
if
(
v
.
type
==
Ip
ServiceAnnouncement
::
TYPE
.
id
)
{
s
<<
ServiceAnnouncement
(
v
.
data
);
s
<<
Ip
ServiceAnnouncement
(
v
.
data
);
}
else
if
(
v
.
type
==
CERTIFICATE_TYPE
.
id
)
{
}
else
if
(
v
.
type
==
CERTIFICATE_TYPE
.
id
)
{
s
<<
"Certificate"
;
s
<<
"Certificate"
;
try
{
try
{
...
@@ -65,21 +65,6 @@ std::ostream& operator<< (std::ostream& s, const Value& v)
...
@@ -65,21 +65,6 @@ std::ostream& operator<< (std::ostream& s, const Value& v)
const
ValueType
ValueType
::
USER_DATA
=
{
0
,
"User Data"
};
const
ValueType
ValueType
::
USER_DATA
=
{
0
,
"User Data"
};
bool
ServiceAnnouncement
::
storePolicy
(
InfoHash
,
std
::
shared_ptr
<
Value
>&
v
,
InfoHash
,
const
sockaddr
*
from
,
socklen_t
fromlen
)
{
ServiceAnnouncement
request
{};
request
.
unpackBlob
(
v
->
data
);
if
(
request
.
getPort
()
==
0
)
return
false
;
ServiceAnnouncement
sa_addr
{
from
,
fromlen
};
sa_addr
.
setPort
(
request
.
getPort
());
// argument v is modified (not the value).
v
=
std
::
make_shared
<
Value
>
(
ServiceAnnouncement
::
TYPE
,
sa_addr
,
v
->
id
);
return
true
;
}
const
ValueType
ServiceAnnouncement
::
TYPE
=
{
1
,
"Service Announcement"
,
std
::
chrono
::
minutes
(
15
),
ServiceAnnouncement
::
storePolicy
,
ValueType
::
DEFAULT_EDIT_POLICY
};
void
void
Value
::
packToSign
(
Blob
&
res
)
const
Value
::
packToSign
(
Blob
&
res
)
const
...
@@ -169,7 +154,7 @@ Value::unpack(Blob::const_iterator& begin, Blob::const_iterator& end)
...
@@ -169,7 +154,7 @@ Value::unpack(Blob::const_iterator& begin, Blob::const_iterator& end)
unpackBody
(
begin
,
end
);
unpackBody
(
begin
,
end
);
}
}
std
::
ostream
&
operator
<<
(
std
::
ostream
&
s
,
const
ServiceAnnouncement
&
v
)
std
::
ostream
&
operator
<<
(
std
::
ostream
&
s
,
const
Ip
ServiceAnnouncement
&
v
)
{
{
s
<<
"Peer: "
;
s
<<
"Peer: "
;
s
<<
"port "
<<
v
.
getPort
();
s
<<
"port "
<<
v
.
getPort
();
...
@@ -184,7 +169,7 @@ std::ostream& operator<< (std::ostream& s, const ServiceAnnouncement& v)
...
@@ -184,7 +169,7 @@ std::ostream& operator<< (std::ostream& s, const ServiceAnnouncement& v)
}
}
void
void
ServiceAnnouncement
::
pack
(
Blob
&
res
)
const
Ip
ServiceAnnouncement
::
pack
(
Blob
&
res
)
const
{
{
serialize
<
in_port_t
>
(
getPort
(),
res
);
serialize
<
in_port_t
>
(
getPort
(),
res
);
if
(
ss
.
ss_family
==
AF_INET
)
{
if
(
ss
.
ss_family
==
AF_INET
)
{
...
@@ -197,7 +182,7 @@ ServiceAnnouncement::pack(Blob& res) const
...
@@ -197,7 +182,7 @@ ServiceAnnouncement::pack(Blob& res) const
}
}
void
void
ServiceAnnouncement
::
unpack
(
Blob
::
const_iterator
&
begin
,
Blob
::
const_iterator
&
end
)
Ip
ServiceAnnouncement
::
unpack
(
Blob
::
const_iterator
&
begin
,
Blob
::
const_iterator
&
end
)
{
{
setPort
(
deserialize
<
in_port_t
>
(
begin
,
end
));
setPort
(
deserialize
<
in_port_t
>
(
begin
,
end
));
size_t
addr_size
=
end
-
begin
;
size_t
addr_size
=
end
-
begin
;
...
@@ -216,5 +201,52 @@ ServiceAnnouncement::unpack(Blob::const_iterator& begin, Blob::const_iterator& e
...
@@ -216,5 +201,52 @@ ServiceAnnouncement::unpack(Blob::const_iterator& begin, Blob::const_iterator& e
}
}
}
}
bool
IpServiceAnnouncement
::
storePolicy
(
InfoHash
,
std
::
shared_ptr
<
Value
>&
v
,
InfoHash
,
const
sockaddr
*
from
,
socklen_t
fromlen
)
{
IpServiceAnnouncement
request
{};
request
.
unpackBlob
(
v
->
data
);
if
(
request
.
getPort
()
==
0
)
return
false
;
IpServiceAnnouncement
sa_addr
{
from
,
fromlen
};
sa_addr
.
setPort
(
request
.
getPort
());
// argument v is modified (not the value).
v
=
std
::
make_shared
<
Value
>
(
IpServiceAnnouncement
::
TYPE
,
sa_addr
,
v
->
id
);
return
true
;
}
const
ValueType
IpServiceAnnouncement
::
TYPE
=
{
2
,
"Internet Service Announcement"
,
std
::
chrono
::
minutes
(
15
),
IpServiceAnnouncement
::
storePolicy
,
ValueType
::
DEFAULT_EDIT_POLICY
};
std
::
ostream
&
operator
<<
(
std
::
ostream
&
s
,
const
DhtMessage
&
v
)
{
s
<<
"DhtMessage: service "
<<
v
.
service
<<
std
::
endl
;
s
.
write
((
const
char
*
)
v
.
message
.
data
(),
v
.
message
.
size
());
return
s
;
}
void
DhtMessage
::
pack
(
Blob
&
res
)
const
{
serialize
<
int16_t
>
(
service
,
res
);
serialize
<
Blob
>
(
message
,
res
);
}
void
DhtMessage
::
unpack
(
Blob
::
const_iterator
&
begin
,
Blob
::
const_iterator
&
end
)
{
service
=
deserialize
<
int16_t
>
(
begin
,
end
);
message
=
deserialize
<
Blob
>
(
begin
,
end
);
}
bool
DhtMessage
::
storePolicy
(
InfoHash
,
std
::
shared_ptr
<
Value
>&
v
,
InfoHash
,
const
sockaddr
*
from
,
socklen_t
fromlen
)
{
DhtMessage
request
{
v
->
data
};
if
(
request
.
service
==
0
)
return
false
;
return
true
;
}
const
ValueType
DhtMessage
::
TYPE
=
{
1
,
"DHT message"
,
std
::
chrono
::
minutes
(
5
),
DhtMessage
::
storePolicy
,
ValueType
::
DEFAULT_EDIT_POLICY
};
}
}
This diff is collapsed.
Click to expand it.
tools/dhtnode.cpp
+
1
−
1
View file @
cb2d14e9
...
@@ -276,7 +276,7 @@ main(int argc, char **argv)
...
@@ -276,7 +276,7 @@ main(int argc, char **argv)
else
if
(
op
==
"a"
)
{
else
if
(
op
==
"a"
)
{
in_port_t
port
;
in_port_t
port
;
iss
>>
port
;
iss
>>
port
;
dht
.
put
(
id
,
dht
::
Value
{
dht
::
ServiceAnnouncement
::
TYPE
.
id
,
dht
::
ServiceAnnouncement
(
port
)},
[](
bool
ok
)
{
dht
.
put
(
id
,
dht
::
Value
{
dht
::
Ip
ServiceAnnouncement
::
TYPE
.
id
,
dht
::
Ip
ServiceAnnouncement
(
port
)},
[](
bool
ok
)
{
std
::
cout
<<
"Announce done !"
<<
ok
<<
std
::
endl
;
std
::
cout
<<
"Announce done !"
<<
ok
<<
std
::
endl
;
});
});
}
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment