Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Michal Zima
coincer
Commits
54fae336
Commit
54fae336
authored
Nov 26, 2018
by
xpetrak2
Browse files
Daemon messages parsing by parts
parent
1f0ff7fd
Changes
3
Hide whitespace changes
Inline
Side-by-side
src/daemon_messages_processor.c
View file @
54fae336
...
...
@@ -37,8 +37,9 @@
#define ADV_GAP_TIME 10
static
enum
process_message_result
process_message
(
const
message_t
*
message
,
process_message
(
message_t
*
message
,
const
char
*
json_message
,
const
char
*
json_data
,
neighbour_t
*
sender
,
global_state_t
*
global_state
);
...
...
@@ -91,6 +92,7 @@ enum process_message_result
{
char
*
json_message_body
;
message_t
message
;
char
*
json_data
;
enum
process_message_result
ret
;
/* decode JSON message; if parsing JSON message into message_t failed */
...
...
@@ -102,7 +104,6 @@ enum process_message_result
if
(
message
.
version
!=
PROTOCOL_VERSION
)
{
free
(
json_message_body
);
message_delete
(
&
message
);
return
PMR_ERR_VERSION
;
}
...
...
@@ -111,15 +112,29 @@ enum process_message_result
log_warn
(
"Someone tampered with a received message"
);
log_debug
(
"The tampered message:
\n
%s"
,
json_message
);
free
(
json_message_body
);
message_delete
(
&
message
);
return
PMR_ERR_INTEGRITY
;
}
/* integrity verification done */
/* message body intact; decode it */
if
(
decode_message_body
(
json_message_body
,
&
message
.
body
,
&
json_data
))
{
log_debug
(
"process_encoded_message - decoding message body has "
"failed. The message body:
\n
%s"
,
json_message_body
);
free
(
json_message_body
);
return
PMR_ERR_PARSING
;
}
free
(
json_message_body
);
/* process the message */
ret
=
process_message
(
&
message
,
json_message
,
sender
,
global_state
);
ret
=
process_message
(
&
message
,
json_message
,
json_data
,
sender
,
global_state
);
if
(
json_data
!=
NULL
)
{
free
(
json_data
);
}
message_delete
(
&
message
);
return
ret
;
}
...
...
@@ -128,18 +143,21 @@ enum process_message_result
* Process a message received from its forwarder/sender (our neighbour).
*
* @param message The received message.
* @param json_message The received message in the JSON format.
* @param json_message The received message in JSON format.
* @param json_data The received message's JSON data.
* @param sender The message sender/forwarder.
* @param global_state The global state.
*
* @return PMR_DONE The received message was successfully
* processed.
* @return PMR_ERR_INTERNAL Internal processing error.
* @return PMR_ERR_PARSING Parsing failure.
* @return PMR_ERR_SEMANTIC Semantic error.
*/
static
enum
process_message_result
process_message
(
const
message_t
*
message
,
process_message
(
message_t
*
message
,
const
char
*
json_message
,
const
char
*
json_data
,
neighbour_t
*
sender
,
global_state_t
*
global_state
)
{
...
...
@@ -147,7 +165,7 @@ static enum process_message_result
linkedlist_t
*
hosts
;
linkedlist_t
*
identities
;
identity_t
*
identity
;
const
message_body_t
*
msg_body
;
message_body_t
*
msg_body
;
const
linkedlist_t
*
msg_traces
;
enum
message_type
msg_type
;
linkedlist_t
*
neighbours
;
...
...
@@ -177,6 +195,13 @@ static enum process_message_result
"before p2p.hello"
);
return
PMR_ERR_SEMANTIC
;
}
if
(
decode_message_data
(
json_data
,
msg_type
,
&
msg_body
->
data
))
{
log_debug
(
"process_message - decoding message data "
"has failed. The data:
\n
%s"
,
json_data
);
return
PMR_ERR_PARSING
;
}
res
=
process_p2p_hello
(
message
,
sender
,
neighbours
,
...
...
@@ -309,6 +334,12 @@ static enum process_message_result
/* the message is meant for us; process it */
if
(
decode_message_data
(
json_data
,
msg_type
,
&
msg_body
->
data
))
{
log_debug
(
"process_encoded_message - decoding message data "
"has failed. The data:
\n
%s"
,
json_data
);
return
PMR_ERR_PARSING
;
}
switch
(
msg_type
)
{
case
P2P_BYE
:
res
=
process_p2p_bye
(
message
,
...
...
@@ -431,7 +462,7 @@ static int process_p2p_hello(const message_t *message,
/* don't take hello from already active neighbour */
if
(
sender
->
flags
&
NEIGHBOUR_ACTIVE
)
{
return
1
;
return
0
;
}
/* check self-neighbouring; if we have a neighbour with 'my_pseudonym'
...
...
@@ -445,7 +476,7 @@ static int process_p2p_hello(const message_t *message,
compare_neighbour_my_pseudonyms
)))
{
linkedlist_delete_safely
(
neighbour
->
node
,
clear_neighbour
);
linkedlist_delete_safely
(
sender
->
node
,
clear_neighbour
);
return
1
;
return
0
;
}
hello
=
(
p2p_hello_t
*
)
message
->
body
.
data
;
...
...
@@ -480,7 +511,6 @@ static int process_p2p_hello(const message_t *message,
* @param hosts Our known hosts.
*
* @return 0 Successfully processed.
* @return 1 Failure.
*/
static
int
process_p2p_peers_adv
(
const
message_t
*
message
,
neighbour_t
*
sender
,
...
...
@@ -501,7 +531,7 @@ static int process_p2p_peers_adv(const message_t *message,
/* if we haven't asked this neighbour for addresses */
if
(
!
(
sender
->
flags
&
NEIGHBOUR_ADDRS_REQ
))
{
log_debug
(
"process_p2p_peers_adv - unwanted addrs arrived"
);
return
1
;
return
0
;
}
unset_neighbour_flags
(
sender
,
NEIGHBOUR_ADDRS_REQ
);
...
...
@@ -511,7 +541,7 @@ static int process_p2p_peers_adv(const message_t *message,
* which is 4 chars) */
if
(
strlen
(
peers_adv
->
addresses
)
<
4
)
{
log_debug
(
"process_p2p_peers_adv - wrong addrs format"
);
return
1
;
return
0
;
}
/* set initial pointer position; skip the outer list '[' */
...
...
@@ -528,7 +558,7 @@ static int process_p2p_peers_adv(const message_t *message,
/* the tuple can not have more than 54 chars (plus '\0') */
if
(
n
>
54
)
{
log_debug
(
"process_p2p_peers_adv - wrong addrs format"
);
return
1
;
return
0
;
}
/* copy the chars between '[' and ']', both boundary
...
...
@@ -539,7 +569,7 @@ static int process_p2p_peers_adv(const message_t *message,
/* get string addr and numerical port from the tuple */
if
(
sscanf
(
tuple
,
" %[^,], %hu "
,
addr_str
,
&
port
)
!=
2
)
{
log_debug
(
"process_p2p_peers_adv - wrong addrs format"
);
return
1
;
return
0
;
}
/* if conversion of addr_str into addr succeeded */
...
...
src/json_parser.c
View file @
54fae336
...
...
@@ -27,18 +27,18 @@
/* TODO: Implement */
/**
* Decode a JSON message
(including its JSON body)
into daemon message.
* Decode a JSON message into
a
daemon message.
*
* @param json_message Decode this JSON message.
* @param message Store decoded data in here.
* @param json_
message_
body Store the message's JSON body in here.
* @param json_body
Store the message's JSON body in here.
*
* @return 0 Decoding successful.
* @return 1 Failure.
*/
int
decode_message
(
const
char
*
json_message
,
message_t
*
message
,
char
**
json_
message_
body
)
char
**
json_body
)
{
char
from_hex
[
65
];
char
to_hex
[
65
];
...
...
@@ -94,16 +94,91 @@ int decode_message(const char *json_message,
* Decode a JSON format message body into a daemon message body.
*
* @param json_body Decode this JSON message body.
* @param message Store decoded body in here.
* @param body Store decoded body in here.
* @param json_data Store the message's JSON data in here, if any.
* Otherwise set to NULL.
*
* @return 0 Decoding successful.
* @return 1 Failure.
*/
int
decode_message_body
(
const
char
*
json_body
,
message_body_t
*
body
)
int
decode_message_body
(
const
char
*
json_body
,
message_body_t
*
body
,
char
**
json_data
)
{
*
json_data
=
NULL
;
return
0
;
}
/* TODO: Implement */
/**
* Decode a JSON format message data into an appropriate structure.
*
* @param json_data Decode this JSON data. This can be NULL
* depending on message type.
* @param type Type of the message data.
* @param data Dynamically allocated structure storing the
* message data. NULL if the message has no data.
*
* @return 0 Decoding successful.
* @return 1 Failure.
*/
int
decode_message_data
(
const
char
*
json_data
,
const
enum
message_type
type
,
void
**
data
)
{
*
data
=
NULL
;
switch
(
type
)
{
case
P2P_BYE
:
return
0
;
case
P2P_HELLO
:
if
(
json_data
==
NULL
)
{
return
1
;
}
*
data
=
(
p2p_hello_t
*
)
malloc
(
sizeof
(
p2p_hello_t
));
if
(
!*
data
)
{
log_error
(
"Allocating p2p.hello"
);
return
1
;
}
return
0
;
case
P2P_PEERS_ADV
:
if
(
json_data
==
NULL
)
{
return
1
;
}
*
data
=
(
p2p_peers_adv_t
*
)
malloc
(
sizeof
(
p2p_peers_adv_t
));
if
(
!*
data
)
{
log_error
(
"Allocating p2p.peers.adv"
);
return
1
;
}
return
0
;
case
P2P_PEERS_SOL
:
return
0
;
case
P2P_PING
:
return
0
;
case
P2P_PONG
:
return
0
;
case
P2P_ROUTE_ADV
:
return
0
;
case
P2P_ROUTE_SOL
:
if
(
json_data
==
NULL
)
{
return
1
;
}
*
data
=
(
p2p_route_sol_t
*
)
malloc
(
sizeof
(
p2p_route_sol_t
));
if
(
!*
data
)
{
log_error
(
"Allocating p2p.route.sol"
);
return
1
;
}
return
0
;
default:
log_error
(
"Unknown message type parsing"
);
return
1
;
}
return
1
;
}
/* TODO: Implement */
/**
* Encode a daemon message into JSON format output.
...
...
src/json_parser.h
View file @
54fae336
...
...
@@ -27,7 +27,7 @@ static const char *msg_type_str[] = {
"p2p.bye"
,
"p2p.hello"
,
"p2p.peers.adv"
,
"p2p.peers.sol"
"p2p.peers.sol"
,
"p2p.ping"
,
"p2p.pong"
,
"p2p.route.adv"
,
...
...
@@ -36,8 +36,14 @@ static const char *msg_type_str[] = {
int
decode_message
(
const
char
*
json_message
,
message_t
*
message
,
char
**
json_message_body
);
int
decode_message_body
(
const
char
*
json_body
,
message_body_t
*
body
);
char
**
json_body
);
int
decode_message_body
(
const
char
*
json_body
,
message_body_t
*
body
,
char
**
json_data
);
int
decode_message_data
(
const
char
*
json_data
,
const
enum
message_type
type
,
void
**
data
);
int
encode_message
(
const
message_t
*
message
,
char
**
json_message
);
int
encode_message_body
(
const
message_body_t
*
body
,
char
**
json_body
);
...
...
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